往当前内核添加驱动程序

接上前面编译内核的文章

内核编译好了之后,自己要往内核中添加自己的驱动程序,如果加呢?

选择一个目录加入自己的驱动文件

nasri@ubuntu:~/Work/linux-source-3.13.0/$ 

因为前面编译内核是在上面这个目录下编译的

所以就在这里添加自己的驱动文件,在driver/char 下创建一个目录nasritest

nasri@ubuntu:~/Work/linux-source-3.13.0/drivers/char$ 

 在nasritest下建立驱动文件

nasri@ubuntu:~/Work/linux-source-3.13.0/drivers/char$ vim nasritest/nasritest.c

nasritest.c驱动文件如下

#include 
#include 
#include 
static int __init my_init(void)
{
    int i = 0;
    for(i = 0 ; i < 4 ; i++)
        printk("nasritest i = %d\n",i);

    printk("my_init");
    return 0;
}
static void __exit my_exit(void)
{
    printk("nasritest exit!");
}

module_init(my_init);
module_exit(my_exit);

当然少不了Makefile 和Kconfig

在nasrittest目录下吗建立Mafile文件,内容:

obj-$(CONFIG_NASRITEST) += nasritest.o

Kconfig文件

menu "NASRI TEST Driver "
comment "NASRI TEST Driver Config"

 config NASRITEST
    tristate "nasritest module test"
    default m
    help
        This is the hello test driver
endmenu

Kconfig是就是内核用来让内核配置工具识别的配置文件,有了这些Kconfig,你才可以通过menuconfig来很快的配置各种驱动

仅仅这些还不够

怎么和其他配置文件联系起来呢?或者怎么和上一级目录联系起来呢?

那直接找这个目录的上一级目录

我们看到上一级目录下有和nasritest类似的文件夹hw_random,我们就按照它的方式去添加我们自己的驱动配置,可以看到

source 一下我们刚才的Kconfig就可以了

config NWFLASH
    tristate "NetWinder flash support"
    depends on ARCH_NETWINDER
    ---help---
      If you say Y here and create a character device /dev/flash with
      major 10 and minor 160 you can manipulate the flash ROM containing
      the NetWinder firmware. Be careful as accidentally overwriting the
      flash contents can render your computer unbootable. On no account
      allow random users access to this device. :-)

      To compile this driver as a module, choose M here: the
      module will be called nwflash.

      If you're not sure, say N.

source "drivers/char/hw_random/Kconfig"
source "drivers/char/nasritest/Kconfig"

同样编译的时候也需要找到nasritest这个文件夹,那么这个char目录的Makefile中添加

obj-$(CONFIG_MWAVE)     += mwave/
obj-$(CONFIG_AGP)       += agp/
obj-$(CONFIG_NASRITEST)     += nasritest/
obj-$(CONFIG_PCMCIA)        += pcmcia/

到这里就好了,添加好了之后又两个作用:

1:我们可以通过 menuconfig 内核配置工具来配置想要的驱动

2:可以通过make命令编译到我们的驱动文件

那首先来看下menuconfig

nasri@ubuntu:~/Work/linux-source-3.13.0$ make menuconfig
scripts/kconfig/mconf Kconfig


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

 

在menuconfig界面上搜索下(“按斜杠/ 搜索nasri,就可以看到我们的驱动的配置路径”)

┌──────────────────────────────────────────────────────────── Search Results ────────────────────────────────────────────────────────────┐
  │ Symbol: NASRITEST [=m]                                                                                                                 │  
  │ Type  : tristate                                                                                                                       │  
  │ Prompt: nasritest module test                                                                                                          │  
  │   Location:                                                                                                                            │  
  │     -> Device Drivers                                                                                                                  │  
  │       -> Character devices                                                                                                             │  
  │ (1)     -> NASRI TEST Driver                                                                                                           │  
  │   Defined at drivers/char/nasritest/Kconfig:4                                                                                          │  
  │                                                 

找到这个路径下面,可以看到我们已经选中了这个模块

 .config - Linux/x86 3.13.11-ckt39 Kernel Configuration
 > Device Drivers > Character devices ────────────────────────────────────────────────────────────────────────────────────────────────────────
  ┌────────────────────────────────────────────────────────── Character devices ───────────────────────────────────────────────────────────┐
  │  Arrow keys navigate the menu.   selects submenus ---> (or empty submenus ----).  Highlighted letters are hotkeys.  Pressing    │  
  │   includes,  excludes,  modularizes features.  Press  to exit,  for Help,  for Search.  Legend: [*] built-in  │  
  │  [ ] excluded   module  < > module capable                                                                                          │  
  │                                                                                                                                        │  
  │ ┌───────────────────────────────^(-)─────────────────────────────────────────────────────────────────────────────────────────────────┐ │  
  │ │                                  HDLC line discipline support                                                                   │ │  
  │ │                                  GSM MUX line discipline support (EXPERIMENTAL)                                                 │ │  
  │ │                                  Trace data router for MIPI P1149.7 cJTAG standard                                              │ │  
  │ │                                  Trace data sink for MIPI P1149.7 cJTAG standard                                                │ │  
  │ │                               [ ] /dev/kmem virtual device support                                                                 │ │  
  │ │                                   Serial drivers  --->                                                                             │ │  
  │ │                               [*] TTY driver to output user messages via printk                                                    │ │  
  │ │                                Parallel printer support                                                                         │ │  
  │ │                               [ ]   Support for console on line printer                                                            │ │  
  │ │                                Support for user-space parallel port device drivers                                              │ │  
  │ │                               [*] Xen Hypervisor Console support                                                                   │ │  
  │ │                               [*]   Xen Hypervisor Multiple Consoles support                                                       │ │  
  │ │                               <*> Virtio console                                                                                   │ │  
  │ │                                IPMI top-level message handler  --->                                                             │ │  
  │ │                               <*> Hardware Random Number Generator Core support                                                    │ │  
  │ │                                  Timer IOMEM HW Random Number Generator support                                                 │ │  
  │ │                                  Intel HW Random Number Generator support                                                       │ │  
  │ │                                  AMD HW Random Number Generator support                                                         │ │  
  │ │                               < >   Atmel Random Number Generator support                                                          │ │  
  │ │                                  VIA HW Random Number Generator support                                                         │ │  
  │ │                                  VirtIO Random Number Generator support                                                         │ │  
  │ │                               < > EXYNOS HW random number generator support                                                        │ │  
  │ │                                TPM HW Random Number Generator support                                                           │ │  
  │ │                                   NASRI TEST Driver   --->                                                                         │ │  
  │ │                               {M} /dev/nvram support                                                                               │ │  
  │ │                                Siemens R3964 line discipline                                                                    │ │  
  │ │                                Applicom intelligent fieldbus card support                                                       │ │  
  │ └───────────────────────────────┴(+)─────────────────────────────────────────────────────────────────────────────────────────────────┘ │  
  ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤  
  │