How to add drivers to the Linux kernel

  
 I. Overview:
Adding a program to the Linux kernel requires the following three tasks:
1. Copy the source code to the corresponding directory of the Linux kernel source code
2. In the Kconfig file of the directory. Add the compile configuration option for the new source code corresponding project
3. Add the compile entry for the new source code in the Makefile of the directory
2.Instance
1. First assign the driver code usbtmc (folder) Go to /usr/src/linux2.6.32/drivers/char
First you need to know where your module should be in the kernel source tree.
1> The device driver is stored in the subdirectory of the driver/subdirectory of the kernel source tree. Within it, the device driver files are further organized in an orderly manner according to the category and type.
a. Character devices exist in the drivers/char/directory
b. Block devices are stored in the drivers/block/directory
c.USB devices are stored in the drivers/usb/directory.
Note:
(1) The file organization rules here are not absolutely the same. For example, USB devices are also character devices, and can also be stored in the drivers/usb/directory.
(2) For example, we store the driver usbtmc in the drivers/char/directory, then you should note that there are a large number of C source files and many other directories in the directory. All device drivers for just one or two source files can be placed directly in this directory, but if the driver contains many source files and other auxiliary files, you can create a new subdirectory.
Here, we put the usbtmc directory under the drivers/char directory
2. Modify the Kconfig and Makefile under the char directory<1> (1) Modify Kconfig
sudo gedit Kconfig Add the following sentence
source"drivers/char/usbtmc/Kconfig" It means that Kconfig in the usbtmc directory is mounted to Kconfig in the char directory (in order to make this Kconfig file work, we need to modify the Kconfig file of the parent directory) , join the source statement)
1> For the driver, Kconfig is usually in the same directory as the source code.
2> If you create a new directory and you want the Kconfig file to exist in it, you must import it in an existing Kconfig file, you need to use the
statement above Mounted in Kconfig in the drivers/char directory.
(2) Modify Makefile
Add a sentence:
obj-$(CONFIG_USBTMC)+=usbtmc/This line of compile instructions tells the module build system to enter the usbtmc/subdirectory when compiling the module. The compilation of the driver at this time depends on a special configuration CONFIG_USBTMC configuration option.
3. Now add Kconfig and Makefile in our own driver folder<1> (1) Modify Kconfig
Create a new Kconfig Add the following wordsmenu "USBTMC"comment"USBTMC Driver"config USBTMCtristate "USBTMC"defaultnhelpIfyou say Y here,support for the usbtmc with computer interface will becompiledinto he kernel and accessible via device node. You can also say M here and thedriver will be built as a module named usbtmc.ko.Ifunsure,say N After .endmenu is properly configured, after executing sudo make menuconfig under the source code, select the character devcie under Device Drivers in the Linux Kernel Configuration graphical interface, and you will see the newly added USBTCM menu,
(2 Modify Makefile
Create a new Makefile, add the following words
obj-$(CONFIG_USBTMC)+=usbtmc.o At this point, the build system will run into the usbtmc/directory and compile usbtmc.c to usbtmc .ko module
Note:
If there may be more than one driver source file, you can modify the Makefile as follows:
obj-$(CO NFIG_USBTMC) += usbtmc.ousbtmc-objs := usbtmc-main.o usbtmc-usb1.o At this point, usbtmc-main.c and usbtmc-usb1.c are compiled and connected to a block of usbtmc.ko.
4. Now Ok, now we can enter the linux kernel directory through menuconfig to find our USBTMC option (USBTMC can be found in the character devices under Device_Drivers) to select it. Then exit, compile the kernel, and you're done.
5. Delete:
Delete is also very simple, first delete your own driver folder in the drivers/char directory. Secondly, delete the things added before the Makefile and Kconfig, and you're done



Copyright © Windows knowledge All Rights Reserved