Original URL: http://blog.chinaunix.net/uid-29287950-id-4573481.html
BQ27501 driver compiled into kernel
First, the driver compiles into the kernel step
To add a program to the Linux kernel, you need to complete the following three tasks:
1. Copy the written source code to the appropriate directory of the Linux kernel source;
2. Add the compilation configuration option of the source code corresponding project in the Kconfig file of the directory;
3. Add a compile entry to the Xinyuan code in the directory's Makefile file.
The steps for compiling the bq27501 driver into the kernel are as follows:
1. First copy the drive Code bq27501 folder to the ti-davinci/drivers/directory.
Determine where the bq27501 driver module should be located in the kernel source tree.
The device driver is stored in the subdirectory of the kernel source root directory drivers/, within which the device driver files are organized in order by category, type, etc.
A. Character devices exist in the drivers/char/directory
B. Block devices are stored in the drivers/block/directory
C. The USB device is stored in the drivers/usb/directory.
Attention:
(1) The file organization rules here are not absolutely constant, for example: USB devices are also part of the character device, and can also be stored in the drivers/usb/directory.
(2) under the drivers/char/directory, there are a large number of C source code files and many other directories in this directory. All device drivers that have only one or two source files can be stored directly in that directory, but if the driver contains many source files and other auxiliary files, you can create a new subdirectory.
(3) bq27501 driver is a character device driver category, although the driver-related files only two, but in order to view, the relevant files in the bq27501 folder. Adding new devices to the drivers/char/directory is simple, but adding new devices directly under drivers/is slightly more complicated. So the following first gives the process of adding bq27501 drivers in drivers/below, and then simply explains the process added under the drivers/char/directory.
2. Create a new makefile file under/bq27501. Add code to the inside:
obj-$ (config_bq27501) +=bq27501.o
At this point, the build system runs into the BQ27501/directory and compiles the bq27501.c to BQ27501.O
3. Create a new Kconfig file under/bq27501. Add code:
Menu "bq27501 Driver"
Config BQ27501
TriState "BQ27501"
Default Y
---help---
Say ' Y ' here, it'll be compiled into Thekernel; If you choose ' M ', it'll be compiled into a module named Asbq27501.ko.
Endmenu
Note: The text in Help cannot be added with a carriage return, otherwise the make Menuconfig will error when compiling.
4. Modify the Kconfig file in the/drivers directory, add a statement before Endmenu ' source Drivers/bq27501/kconfig ' for the driver, Kconfig is usually in the same directory as the source code. If a new directory is created and you want the Kconfig file to exist in that directory, then it must be introduced in an existing Kconfig file, and it needs to be hooked up to Kconfig in the drivers directory using the above statement.
5. Modify the/drivers makefile file to add ' obj-$ (config_bq27501) +=bq27501/'. This line of compilation instructions tells the module building system to enter the BQ27501/subdirectory when compiling the module. The compilation of the driver at this time depends on a special configuration config_bq27501 configuration option.
6. Modify the Kconfig file in the Arch/arm directory and add the statement directly in the menu "Device drivers......endmenu"
[CPP]View Plaincopy
- SOURCE "Drivers/bq27501/kconfig"
7. When properly configured, using the Make Menuconfig command, under the Devicedrivers menu, you can find the options for bq27501 driver, as shown in (1):
Figure (1)
Use the Space key to select the "*" sign, which means that the module is compiled into the kernel. As shown in (2):
Figure (2)
8. Remove the driver module. Delete the bq27501 folder in the drivers directory first, and then delete the statements added in makefile and Kconfig.
9. The above will bq27501 driver in the drivers directory, if the driver module is placed in the Drivers/char directory, then the change is Drivers/char makefile and kconfig files, arch/arm/ Kconfig does not need to be modified.
Second, the drive module automatic execution
When the bq27501 driver is dynamically loaded into the kernel as a module, it is manually loaded using the "insmod Bq27501.ko" command. The bq27501 drive selection is programmed into the kernel, which automatically loads the kernel during kernel boot and invokes the Drive module initialization function that the Module_init function points to. So there is no need to add additional code to load the driver module.
Iii. automatic creation of device nodes
When dynamically loading the driver module, the device node is created manually using the Mknod command, which provides a set of functions that automatically creates the appropriate device node in the/dev directory when the module is loaded and deletes the node when the module is unloaded.
Add the following statement after Register_chrdev in the Bq27501_init function:
Bq27501_class=class_create (This_module, "Bq27501_class");
if (Is_err (Bq27501_class)) {
PRINTK (Kern_alert "err:failed in creating class.\n");
return-1;
}
Class_device_create (Bq27501_class,mkdev (bq27501_major,0), NULL, "bq27501", 0);
Class_create function, create your own class under/SYSFS, the first parameter specifies which module the class owner is, and the second parameter specifies the class name.
The Class_device_create function, registers the device in the SYSFS, and creates a corresponding node; the first parameter specifies the class to which the device is to be created, the second parameter is the device number, the third parameter is the parent device of the device, and if none is specified as NULL, The fourth parameter is the device name, and the fifth parameter is from the device number. (The new kernel version of this function is device_create, and the order of the parameters varies).
After the kernel boot is complete, use the "cat/proc/devices" command to see that the bq27501 has been successfully added to the system. As follows:
Character Devices:
1 mem
2 Pty
3 Ttyp
4/dev/vc/0
4 TTY
4 TTYs
5/dev/tty
5/dev/console
5/dev/ptmx
7 vcs
Ten Misc
Input
Sound
fb
Bayi Video4linux
About
Mtd
Ptm
136 pts
225 bq27501
249 CSL
Dm355_ipc_msgq
251 Dm350mmap
252 Cmem
253 DM355AEW
254 Dm355ipipe
Block Devices:
1 RAMDisk
7 loop
8 SD
Mtdblock
Sd
Sd
Sd
The SD
Sd
Sd
Sd
Sd
129 SD
Sd
131 SD
Sd
133 SD
134 SD
135 SD
253 Sbull
254 MMC
A part of the knowledge reference http://blog.csdn.net/tigerjb/article/details/6426321, here Express thanks!
"Go" Linux driver module compiled into the kernel