The usage of Export_symbol in Linux (with: Own application) __linux

Source: Internet
Author: User

The functions or symbols defined within the Export_symbol tag are exposed to all kernel code and can be invoked directly in your kernel module without modifying the kernel code, that is, using export_symbol to export a function as a symbol to other modules.. You can also manually modify the kernel source code to export additional functions for testing after recompiling and loading the new kernel.


Linux Symbol Export Method:

[1] If we want export the symbol in a module, just use theExport_symbol (xxxx)In the C or H file.
and compile the module by adding the compile flag-dexport_symtab.
Then we can use thexxxxIn the other module.


[2] If we want export some symbol in Kernel this is isn't in a module such asxxxxIn the/ARCH/PPC/FEC.C.
Firstly, define thexxxxIn theFEC.C;
Secondly, make a new file which contain the"extern"Define thexxxx(for example, extern intxxxx);
Lastly, in thePPC_KSYMS.CWe includes the new file, and add theExport_symbol(xxxx).
Then we can use thexxxx.



Precautions to use:
#include <linux/module.h> files are required in the. c file using Export_symbol.
Write function First
Func_a ()
{

}
Use Export_symbol again
Export_symbol (FUNC_A);

One of my usages:
W90P710 Network card driver, there is automatic detection network cable is not plugged in this function interface. Because of the needs of the application, we need to return the network cable is plugged in this state. The first idea is to get the IOCTL () in the NIC driver, but the driver of the network device is not the same as the driver of the character device. Always report invalid this error when SOCKFD = socket () gets socket and then IOCTL (SOCKFD, CMD, &SLF).
(Link:http://www.mcuos.com/redirect.php?tid=1650&goto=lastpost#lastpost)

A trick not, another trick: think of

Export_symbolThe ability to export symbols to a module is visible, or the entire kernel is visible. So why don't I define a global variable in the driver of eth0 to keep the NIC plugged into this state, and then use him to export to the entire kernel space to be visible, and then return this value in this other driver. Tried it, and it was a success.
1, define global variables in LINUX-2.4.X/DRIVER/NET/W90P710_MAC.C:
All macplugstatus are add by Guowenxue 2008.8.1
int macplugstatus;

Then in the Resetphychip () function:-----system boot, when the network card is initialized, the network cable is not plugged in
When PRINTK ("Resetphychip failed 1/n"), set: Macplugstatus = 0;
When Trace_error ("ok/n"), set: Macplugstatus = 1;

In addition need in: W710_autodetect () function:----------system started, the network cable did not plug in;
When PRINTK ("MAC line-off.../n"), set macplugstatus = 0;
When PRINTK ("MAC line-on.../n"), set macplugstatus = 1;

2, export this global variable in the linux-2.4.x/driver/net/w90p710_mac.h header file:
Add by Guowenxue 2008.8.1
extern int macplugstatus;

3, another character device was created, specifically designed to get this state (luxury), and Malaysia they requested to do so. The code is as follows:
[Code]
#include "L200.h"
#include ". /.. /net/w90p710_mac.h "//contains the header file above

Export_symbol (Macplugstatus); Export Macplugstatus This global variable because he is visible throughout the kernel space.

static int Mac_open (struct inode *inode, struct file *file)
{
return 0;
}

static int mac_release (struct inode *inode, struct file *file)
{
return 0;
}

static int mac_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
Switch (CMD)
{
Case Getplugstatus://Whether network wire is plugin
return macplugstatus; The whole driver is just returning this value ~

Default
return-1;
}
return 0;
}


static struct File_operations mac_fops=
{
. IOCTL = Mac_ioctl,
. open = Mac_open,
. Release = Mac_release,
};

int l200_mac_init (void)
{

if ((Register_chrdev (Mac_major, Mac_name, &mac_fops)) < 0)
{
PRINTK ("Regist L200 MAC device failure.../n");
Return-enodev;
}
PRINTK ("Regist L200 MAC device success.../n");


return 0;

}

void L200_mac_cleanup (void)
{
Unregister_chrdev (Mac_major, mac_name);
PRINTK ("L200 MAC unregist ok!/n");
}


Module_license ("GPL");
[/code]

Then add L200_mac_init () in linux-2.4.x/driver/char/mem.c, modify config.in, reconfigure kernel, compile and run OK ~

PS: Although this is not a good method, but he himself thought of a way, can be counted to achieve the function. Just open the device on the application, IOCTL () OK. It seems that the embedded still need to learn something more ~

Posted in http://hi.baidu.com/kkernel/blog/item/ad01fd08e1ef3236e8248869.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.