Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new driver code to linux source code

Tags:

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?

like image 739
Ravi Avatar asked Jul 29 '12 14:07

Ravi


People also ask

Where is Linux driver source code?

If the kernel source code is present in your system, you can find it under the /usr/src/linux-<Version> directory, where <Version> must be replaced with the source code kernel version you are dealing with. You also can find the last kernel source code at https://github.com/torvalds/linux.

How do I register a device driver in Linux?

To register a character device, we need to use the register_chrdev function: int register_chrdev (unsigned int major, const char * name, const struct file_operations * fops);


1 Answers

To cross compile your own driver in the arm architecture you have to follow some steps as mentioned below.

  1. 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

  2. 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

  3. Create one Kconfig file inside your driver directory (using vi any editor) and inside this put

    config MY_DRIVER tristate "my driver" //gives your driver description like vendor name etc. depends on ARM default y if ARM help   my driver module.
  4. Save this file, this will appears like /linux_source_code/drivers/my_drvr/Kconfig

  5. Add both Makefile and Kconfig file in the Linux source drivers Makefile and Kconfig file which are at /linux_source_code/drivers/Makefile and /linux_source_code/drivers/Kconfig

  6. In the Makefile add below in last line

     obj-y    += my_drvr/ 

    or

     obj-$(CONFIG_MY_DRIVER)   += my_drvr/
  7. In Kconfig file add below in last line

    source "drivers/my_drvr/Kconfig"
  8. Finally have to add Kconfig file into architecture specific config file which will be at kernel_source/arch/arm/configs/--defconfig in this add below line in the last

    CONFIG_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)

like image 125
Deepak Singh Avatar answered Oct 25 '22 13:10

Deepak Singh