Adding new driver code to linux source code
I have developed a Linux device driver. As of now I am compiling it on Ubuntu 12.04 with cross-compiler for arm and then insmoding it in my arm Linux image. But I want to learn how I can add it in Linux source code and give and option to add/remove through configuration of arm Linux, so that I can compile it with Linux source code compilation?
Any ideas?
To cross compile your own driver in the arm architecture you have to follow some steps as mentioned below.
Create a directory like my_drvr inside drivers(which is in the Linux source code) for your driver and put your driver (my_driver.c) file inside this directory. It will looks like
/linux_source_code/drivers/my_drvr/my_driver.c
Create one Makefile inside your driver directory (using vi any editor) and inside this put
obj-$(CONFIG_MY_DRIVER) += my_driver.o
and save this file. This will appears like/linux_source_code/drivers/my_drvr/Makefile
-
Create one
Kconfig
file inside your driver directory (using vi any editor) and inside this putconfig MY_DRIVER tristate "my driver" //gives your driver description like vendor name etc. depends on ARM default y if ARM help my driver module.
Save this file, this will appears like
/linux_source_code/drivers/my_drvr/Kconfig
Add both
Makefile
andKconfig
file in the Linux source driversMakefile
andKconfig
file which are at/linux_source_code/drivers/Makefile
and/linux_source_code/drivers/Kconfig
-
In the Makefile add below in last line
obj-y += my_drvr/
or
obj-$(CONFIG_MY_DRIVER) += my_drvr/
-
In Kconfig file add below in last line
source "drivers/my_drvr/Kconfig"
-
Finally have to add
Kconfig
file into architecture specific config file which will be atkernel_source/arch/arm/configs/--defconfig
in this add below line in the lastCONFIG_MY_DRIVER=y
Note:- Last step will differ according to your architecture, so that you have take care. Now you can compile your driver by using make command. (eg: sun7i_defconfig)
You need to add a config option in the Kconfig file of the kernel source subdirectory in which your device driver will be put. You also need to add lines to the Makefile of that directory. Obviously you need to copy the source files to that directory too.
Since your driver depends on the ARM architecture, in the Kconfig, you need to put an option of 'depends on' like:
config SND_ARMAACI
tristate "ARM PrimeCell PL041 AC Link support"
depends on ARM_AMBA
and your Makefile will look like
obj-$(CONFIG_SND_ARMAACI) += snd-aaci.o
snd-aaci-objs := aaci.o
So now when you do a make menuconfig in your kernel source tree, you will find the config option you put in the Kconfig and you will be able to select it to be compiled into the kernel or built as a module or not compiled at all.
Look for examples in the subsystem directory your driver is meant for.
Create patch and add applying of this patch as a step after decompressing kernel tarball and before configuring/compilation.