Chinaunix首页 | 论坛 | 博客
  • 博客访问: 274936
  • 博文数量: 111
  • 博客积分: 1591
  • 博客等级: 上尉
  • 技术积分: 877
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-20 15:14
文章分类

全部博文(111)

文章存档

2014年(1)

2012年(41)

2011年(69)

分类:

2012-03-15 16:38:57

之所以要引入子系统,是为了让程序更标准化。之前的按键程序,虽然最后相对比较完善,但是不是通用的,别人在不熟悉的情况下是不能随心所欲调用的。显然这与实际应用不合.input输入子系统是内核源码自带的,如果按他这种框架来写,写出的程序就可以很方便地被上层调用,而不用过份关注一些其他的问题。

首先,分析一个驱动程序,先从入口函数开始。static int __init input_init()中有一句err = register_chrdev(INPUT_MAJOR, "input", &input_fops);是用来注册设备的,接下来再看注册设备的信息。

static const struct file_operations input_fops = {
    .owner = THIS_MODULE,
    .open = input_open_file,
};
想调用open肯定要进入input_open_file中执行,打开input_open_file 分析见代码中的注释。根据实际情况构造新的new_fops.
err = new_fops->open(inode, file);   // 返回新的fops
应用程序中要读就要执行:
read > ... > file->f_op->read
input_table数组由谁构造
MODULE_DEVICE_TABLE(input, evdev_ids);   // 源自drivers\input\Evdev.c

static struct input_handler evdev_handler = {
    .event =    evdev_event,                   // 事件
    .connect =    evdev_connect,               // 连接
    .disconnect =    evdev_disconnect,         // 断开连接,关联
    .fops =        &evdev_fops,                // fops
    .minor =    EVDEV_MINOR_BASE,              // 次设备号
    .name =        "evdev",                    // 设备名
    .id_table =    evdev_ids,                  // id_table 表示可以支持的设备 如果id_table支持设备则调用connect函数。让input_device(硬件上的)和input_handler(软件上的)建立一个连接。
};

input_register_handler(软件上的)

注册input_handler:
input_register_handler:
input_table[handler->minor >> 5] = handler  // 数组
list_add_tail(&handler->node, &input_handler_list);  // 放入链表

list_for_each_entry(dev, &input_dev_list, node)      // 对于每一个input_dev调用input_attach_handler
    input_attach_handler(dev, handler);              // 根据input_handler中的id_table来判断能否支持这个input_dev
          
注册输入设备
input_register_device
    list_add_tail(&dev->node, &input_dev_list)   // 放入链表
   
    list_for_each_entry(handler, &input_handler_list, node) // 对input_handler_list中的每一个条目都调用input_attach_handler();
        input_attach_handler(dev, handler);  // 根据input_handler中的id_table来判断能否支持这个input_dev

   
   
input_attach_handler:

id = input_match_device(handler->id_table, dev);

error = handler->connect(handler, dev, id);

根据注册input_dev或input_handler时,会两两比较input_dev和input_handler
根据input_handler的id_table判断这个input_handler能否支持这个input_dev,如果可以则调用
input_handler的connect函数建立“连接”

怎么建立连接:
1,分配一个input_handle结构体
2,设置
    input_handle.dev = input_dev;
    input_handle.handler = input_handler;
3,注册
    input_handler->h_list = &input_handle
    input_dev->h_list     = &input_handle
   
Evdev.c
evdev_connect()

evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);    // 分配一个input_handle结构

    // 设置
    evdev->handle.dev = dev;            // 指向input_dev结构体
    evdev->handle.name = evdev->name;   //
    evdev->handle.handler = handler;    // 指向input_handler结构体
    evdev->handle.private = evdev;
   
error = input_register_handle(&evdev->handle);  //  注册这个handle
   
怎么读按键:
app: read
---------------------------------------------
   ..............
        evdev_read
            // 无数据并且是非阻塞方式打开,则立刻返回。
            if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
            return -EAGAIN; // client缓冲,如果环形缓冲头和尾是一样。表示数据是空的。并且文件是以非阻塞方式打开
                                 则再次尝试。
            // 否则休眠
            retval = wait_event_interruptible(evdev->wait  // 如果是以阻塞方式打开则马上进入休眠。
                client->head != client->tail || !evdev->exist);
               
        谁来唤醒:
        evdev_event事件里面唤醒
            wake_up_interruptible(&evdev->wait);
           
        evdev_event被谁来调用
        
        怎么写符合输入子系统框架的驱动程序?
        1,分配一个input_dev结构体
        2,设置
        3,注册
        4,硬件相关的代码,比如在中断服务程序里面上报事件。


点击(此处)折叠或打开

  1. /*
  2.  * The input core
  3.  *
  4.  * Copyright (c) 1999-2002 Vojtech Pavlik
  5.  */

  6. /*
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License version 2 as published by
  9.  * the Free Software Foundation.
  10.  */

  11. #include <linux/init.h>
  12. #include <linux/input.h>
  13. #include <linux/module.h>
  14. #include <linux/random.h>
  15. #include <linux/major.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/poll.h>
  20. #include <linux/device.h>
  21. #include <linux/mutex.h>

  22. MODULE_AUTHOR("Vojtech Pavlik ");
  23. MODULE_DESCRIPTION("Input core");
  24. MODULE_LICENSE("GPL");

  25. #define INPUT_DEVICES    256

  26. static LIST_HEAD(input_dev_list);
  27. static LIST_HEAD(input_handler_list);

  28. static struct input_handler *input_table[8];

  29. /**
  30.  * input_event() - report new input event
  31.  * @dev: device that generated the event
  32.  * @type: type of the event
  33.  * @code: event code
  34.  * @value: value of the event
  35.  *
  36.  * This function should be used by drivers implementing various input devices
  37.  * See also input_inject_event()
  38.  */
  39. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  40. {
  41.     struct input_handle *handle;

  42.     if (type > EV_MAX || !test_bit(type, dev->evbit))
  43.         return;

  44.     add_input_randomness(type, code, value);

  45.     switch (type) {

  46.         case EV_SYN:
  47.             switch (code) {
  48.                 case SYN_CONFIG:
  49.                     if (dev->event)
  50.                         dev->event(dev, type, code, value);
  51.                     break;

  52.                 case SYN_REPORT:
  53.                     if (dev->sync)
  54.                         return;
  55.                     dev->sync = 1;
  56.                     break;
  57.             }
  58.             break;

  59.         case EV_KEY:

  60.             if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  61.                 return;

  62.             if (value == 2)
  63.                 break;

  64.             change_bit(code, dev->key);

  65.             if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
  66.                 dev->repeat_key = code;
  67.                 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  68.             }

  69.             break;

  70.         case EV_SW:

  71.             if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
  72.                 return;

  73.             change_bit(code, dev->sw);

  74.             break;

  75.         case EV_ABS:

  76.             if (code > ABS_MAX || !test_bit(code, dev->absbit))
  77.                 return;

  78.             if (dev->absfuzz[code]) {
  79.                 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
  80.                  (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
  81.                     return;

  82.                 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
  83.                  (value < dev->abs[code] + dev->absfuzz[code]))
  84.                     value = (dev->abs[code] * 3 + value) >> 2;

  85.                 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
  86.                  (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
  87.                     value = (dev->abs[code] + value) >> 1;
  88.             }

  89.             if (dev->abs[code] == value)
  90.                 return;

  91.             dev->abs[code] = value;
  92.             break;

  93.         case EV_REL:

  94.             if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
  95.                 return;

  96.             break;

  97.         case EV_MSC:

  98.             if (code > MSC_MAX || !test_bit(code, dev->mscbit))
  99.                 return;

  100.             if (dev->event)
  101.                 dev->event(dev, type, code, value);

  102.             break;

  103.         case EV_LED:

  104.             if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
  105.                 return;

  106.             change_bit(code, dev->led);

  107.             if (dev->event)
  108.                 dev->event(dev, type, code, value);

  109.             break;

  110.         case EV_SND:

  111.             if (code > SND_MAX || !test_bit(code, dev->sndbit))
  112.                 return;

  113.             if (!!test_bit(code, dev->snd) != !!value)
  114.                 change_bit(code, dev->snd);

  115.             if (dev->event)
  116.                 dev->event(dev, type, code, value);

  117.             break;

  118.         case EV_REP:

  119.             if (code > REP_MAX || value < 0 || dev->rep[code] == value)
  120.                 return;

  121.             dev->rep[code] = value;
  122.             if (dev->event)
  123.                 dev->event(dev, type, code, value);

  124.             break;

  125.         case EV_FF:

  126.             if (value < 0)
  127.                 return;

  128.             if (dev->event)
  129.                 dev->event(dev, type, code, value);
  130.             break;
  131.     }

  132.     if (type != EV_SYN)
  133.         dev->sync = 0;

  134.     if (dev->grab)
  135.         dev->grab->handler->event(dev->grab, type, code, value);
  136.     else
  137.         list_for_each_entry(handle, &dev->h_list, d_node)
  138.             if (handle->open)
  139.                 handle->handler->event(handle, type, code, value);
  140. }
  141. EXPORT_SYMBOL(input_event);

  142. /**
  143.  * input_inject_event() - send input event from input handler
  144.  * @handle: input handle to send event through
  145.  * @type: type of the event
  146.  * @code: event code
  147.  * @value: value of the event
  148.  *
  149.  * Similar to input_event() but will ignore event if device is "grabbed" and handle
  150.  * injecting event is not the one that owns the device.
  151.  */
  152. void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  153. {
  154.     if (!handle->dev->grab || handle->dev->grab == handle)
  155.         input_event(handle->dev, type, code, value);
  156. }
  157. EXPORT_SYMBOL(input_inject_event);

  158. static void input_repeat_key(unsigned long data)
  159. {
  160.     struct input_dev *dev = (void *) data;

  161.     if (!test_bit(dev->repeat_key, dev->key))
  162.         return;

  163.     input_event(dev, EV_KEY, dev->repeat_key, 2);
  164.     input_sync(dev);

  165.     if (dev->rep[REP_PERIOD])
  166.         mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
  167. }

  168. int input_grab_device(struct input_handle *handle)
  169. {
  170.     if (handle->dev->grab)
  171.         return -EBUSY;

  172.     handle->dev->grab = handle;
  173.     return 0;
  174. }
  175. EXPORT_SYMBOL(input_grab_device);

  176. void input_release_device(struct input_handle *handle)
  177. {
  178.     struct input_dev *dev = handle->dev;

  179.     if (dev->grab == handle) {
  180.         dev->grab = NULL;

  181.         list_for_each_entry(handle, &dev->h_list, d_node)
  182.             if (handle->handler->start)
  183.                 handle->handler->start(handle);
  184.     }
  185. }
  186. EXPORT_SYMBOL(input_release_device);

  187. int input_open_device(struct input_handle *handle)
  188. {
  189.     struct input_dev *dev = handle->dev;
  190.     int err;

  191.     err = mutex_lock_interruptible(&dev->mutex);
  192.     if (err)
  193.         return err;

  194.     handle->open++;

  195.     if (!dev->users++ && dev->open)
  196.         err = dev->open(dev);

  197.     if (err)
  198.         handle->open--;

  199.     mutex_unlock(&dev->mutex);

  200.     return err;
  201. }
  202. EXPORT_SYMBOL(input_open_device);

  203. int input_flush_device(struct input_handle* handle, struct file* file)
  204. {
  205.     if (handle->dev->flush)
  206.         return handle->dev->flush(handle->dev, file);

  207.     return 0;
  208. }
  209. EXPORT_SYMBOL(input_flush_device);

  210. void input_close_device(struct input_handle *handle)
  211. {
  212.     struct input_dev *dev = handle->dev;

  213.     input_release_device(handle);

  214.     mutex_lock(&dev->mutex);

  215.     if (!--dev->users && dev->close)
  216.         dev->close(dev);
  217.     handle->open--;

  218.     mutex_unlock(&dev->mutex);
  219. }
  220. EXPORT_SYMBOL(input_close_device);

  221. static int input_fetch_keycode(struct input_dev *dev, int scancode)
  222. {
  223.     switch (dev->keycodesize) {
  224.         case 1:
  225.             return ((u8 *)dev->keycode)[scancode];

  226.         case 2:
  227.             return ((u16 *)dev->keycode)[scancode];

  228.         default:
  229.             return ((u32 *)dev->keycode)[scancode];
  230.     }
  231. }

  232. static int input_default_getkeycode(struct input_dev *dev,
  233.                  int scancode, int *keycode)
  234. {
  235.     if (!dev->keycodesize)
  236.         return -EINVAL;

  237.     if (scancode < 0 || scancode >= dev->keycodemax)
  238.         return -EINVAL;

  239.     *keycode = input_fetch_keycode(dev, scancode);

  240.     return 0;
  241. }

  242. static int input_default_setkeycode(struct input_dev *dev,
  243.                  int scancode, int keycode)
  244. {
  245.     int old_keycode;
  246.     int i;

  247.     if (scancode < 0 || scancode >= dev->keycodemax)
  248.         return -EINVAL;

  249.     if (keycode < 0 || keycode > KEY_MAX)
  250.         return -EINVAL;

  251.     if (!dev->keycodesize)
  252.         return -EINVAL;

  253.     if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
  254.         return -EINVAL;

  255.     switch (dev->keycodesize) {
  256.         case 1: {
  257.             u8 *k = (u8 *)dev->keycode;
  258.             old_keycode = k[scancode];
  259.             k[scancode] = keycode;
  260.             break;
  261.         }
  262.         case 2: {
  263.             u16 *k = (u16 *)dev->keycode;
  264.             old_keycode = k[scancode];
  265.             k[scancode] = keycode;
  266.             break;
  267.         }
  268.         default: {
  269.             u32 *k = (u32 *)dev->keycode;
  270.             old_keycode = k[scancode];
  271.             k[scancode] = keycode;
  272.             break;
  273.         }
  274.     }

  275.     clear_bit(old_keycode, dev->keybit);
  276.     set_bit(keycode, dev->keybit);

  277.     for (i = 0; i < dev->keycodemax; i++) {
  278.         if (input_fetch_keycode(dev, i) == old_keycode) {
  279.             set_bit(old_keycode, dev->keybit);
  280.             break; /* Setting the bit twice is useless, so break */
  281.         }
  282.     }

  283.     return 0;
  284. }


  285. #define MATCH_BIT(bit, max) \
  286.         for (i = 0; i < NBITS(max); i++) \
  287.             if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  288.                 break; \
  289.         if (i != NBITS(max)) \
  290.             continue;

  291. static const struct input_device_id *input_match_device(const struct input_device_id *id,
  292.                             struct input_dev *dev)
  293. {
  294.     int i;

  295.     for (; id->flags || id->driver_info; id++) {

  296.         if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  297.             if (id->bustype != dev->id.bustype)
  298.                 continue;

  299.         if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  300.             if (id->vendor != dev->id.vendor)
  301.                 continue;

  302.         if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  303.             if (id->product != dev->id.product)
  304.                 continue;

  305.         if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  306.             if (id->version != dev->id.version)
  307.                 continue;

  308.         MATCH_BIT(evbit, EV_MAX);
  309.         MATCH_BIT(keybit, KEY_MAX);
  310.         MATCH_BIT(relbit, REL_MAX);
  311.         MATCH_BIT(absbit, ABS_MAX);
  312.         MATCH_BIT(mscbit, MSC_MAX);
  313.         MATCH_BIT(ledbit, LED_MAX);
  314.         MATCH_BIT(sndbit, SND_MAX);
  315.         MATCH_BIT(ffbit, FF_MAX);
  316.         MATCH_BIT(swbit, SW_MAX);

  317.         return id;
  318.     }

  319.     return NULL;
  320. }

  321. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  322. {
  323.     const struct input_device_id *id;
  324.     int error;

  325.     if (handler->blacklist && input_match_device(handler->blacklist, dev))
  326.         return -ENODEV;

  327.     id = input_match_device(handler->id_table, dev);
  328.     if (!id)
  329.         return -ENODEV;

  330.     error = handler->connect(handler, dev, id);
  331.     if (error && error != -ENODEV)
  332.         printk(KERN_ERR
  333.             "input: failed to attach handler %s to device %s, "
  334.             "error: %d\n",
  335.             handler->name, kobject_name(&dev->cdev.kobj), error);

  336.     return error;
  337. }


  338. #ifdef CONFIG_PROC_FS

  339. static struct proc_dir_entry *proc_bus_input_dir;
  340. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  341. static int input_devices_state;

  342. static inline void input_wakeup_procfs_readers(void)
  343. {
  344.     input_devices_state++;
  345.     wake_up(&input_devices_poll_wait);
  346. }

  347. static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
  348. {
  349.     int state = input_devices_state;

  350.     poll_wait(file, &input_devices_poll_wait, wait);
  351.     if (state != input_devices_state)
  352.         return POLLIN | POLLRDNORM;

  353.     return 0;
  354. }

  355. static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
  356. {
  357.     struct list_head *node;
  358.     loff_t i = 0;

  359.     list_for_each(node, list)
  360.         if (i++ == *pos)
  361.             return node;

  362.     return NULL;
  363. }

  364. static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
  365. {
  366.     if (element->next == list)
  367.         return NULL;

  368.     ++(*pos);
  369.     return element->next;
  370. }

  371. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  372. {
  373.     /* acquire lock here ... Yes, we do need locking, I knowi, I know... */

  374.     return list_get_nth_element(&input_dev_list, pos);
  375. }

  376. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  377. {
  378.     return list_get_next_element(&input_dev_list, v, pos);
  379. }

  380. static void input_devices_seq_stop(struct seq_file *seq, void *v)
  381. {
  382.     /* release lock here */
  383. }

  384. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  385.                  unsigned long *bitmap, int max)
  386. {
  387.     int i;

  388.     for (i = NBITS(max) - 1; i > 0; i--)
  389.         if (bitmap[i])
  390.             break;

  391.     seq_printf(seq, "B: %s=", name);
  392.     for (; i >= 0; i--)
  393.         seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  394.     seq_putc(seq, '\n');
  395. }

  396. static int input_devices_seq_show(struct seq_file *seq, void *v)
  397. {
  398.     struct input_dev *dev = container_of(v, struct input_dev, node);
  399.     const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  400.     struct input_handle *handle;

  401.     seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  402.          dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);

  403.     seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  404.     seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  405.     seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  406.     seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  407.     seq_printf(seq, "H: Handlers=");

  408.     list_for_each_entry(handle, &dev->h_list, d_node)
  409.         seq_printf(seq, "%s ", handle->name);
  410.     seq_putc(seq, '\n');

  411.     input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  412.     if (test_bit(EV_KEY, dev->evbit))
  413.         input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  414.     if (test_bit(EV_REL, dev->evbit))
  415.         input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  416.     if (test_bit(EV_ABS, dev->evbit))
  417.         input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  418.     if (test_bit(EV_MSC, dev->evbit))
  419.         input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  420.     if (test_bit(EV_LED, dev->evbit))
  421.         input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  422.     if (test_bit(EV_SND, dev->evbit))
  423.         input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  424.     if (test_bit(EV_FF, dev->evbit))
  425.         input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  426.     if (test_bit(EV_SW, dev->evbit))
  427.         input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);

  428.     seq_putc(seq, '\n');

  429.     kfree(path);
  430.     return 0;
  431. }

  432. static struct seq_operations input_devices_seq_ops = {
  433.     .start    = input_devices_seq_start,
  434.     .next    = input_devices_seq_next,
  435.     .stop    = input_devices_seq_stop,
  436.     .show    = input_devices_seq_show,
  437. };

  438. static int input_proc_devices_open(struct inode *inode, struct file *file)
  439. {
  440.     return seq_open(file, &input_devices_seq_ops);
  441. }

  442. static const struct file_operations input_devices_fileops = {
  443.     .owner        = THIS_MODULE,
  444.     .open        = input_proc_devices_open,
  445.     .poll        = input_proc_devices_poll,
  446.     .read        = seq_read,
  447.     .llseek        = seq_lseek,
  448.     .release    = seq_release,
  449. };

  450. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  451. {
  452.     /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  453.     seq->private = (void *)(unsigned long)*pos;
  454.     return list_get_nth_element(&input_handler_list, pos);
  455. }

  456. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  457. {
  458.     seq->private = (void *)(unsigned long)(*pos + 1);
  459.     return list_get_next_element(&input_handler_list, v, pos);
  460. }

  461. static void input_handlers_seq_stop(struct seq_file *seq, void *v)
  462. {
  463.     /* release lock here */
  464. }

  465. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  466. {
  467.     struct input_handler *handler = container_of(v, struct input_handler, node);

  468.     seq_printf(seq, "N: Number=%ld Name=%s",
  469.          (unsigned long)seq->private, handler->name);
  470.     if (handler->fops)
  471.         seq_printf(seq, " Minor=%d", handler->minor);
  472.     seq_putc(seq, '\n');

  473.     return 0;
  474. }
  475. static struct seq_operations input_handlers_seq_ops = {
  476.     .start    = input_handlers_seq_start,
  477.     .next    = input_handlers_seq_next,
  478.     .stop    = input_handlers_seq_stop,
  479.     .show    = input_handlers_seq_show,
  480. };

  481. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  482. {
  483.     return seq_open(file, &input_handlers_seq_ops);
  484. }

  485. static const struct file_operations input_handlers_fileops = {
  486.     .owner        = THIS_MODULE,
  487.     .open        = input_proc_handlers_open,
  488.     .read        = seq_read,
  489.     .llseek        = seq_lseek,
  490.     .release    = seq_release,
  491. };

  492. static int __init input_proc_init(void)
  493. {
  494.     struct proc_dir_entry *entry;

  495.     proc_bus_input_dir = proc_mkdir("input", proc_bus);
  496.     if (!proc_bus_input_dir)
  497.         return -ENOMEM;

  498.     proc_bus_input_dir->owner = THIS_MODULE;

  499.     entry = create_proc_entry("devices", 0, proc_bus_input_dir);
  500.     if (!entry)
  501.         goto fail1;

  502.     entry->owner = THIS_MODULE;
  503.     entry->proc_fops = &input_devices_fileops;

  504.     entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
  505.     if (!entry)
  506.         goto fail2;

  507.     entry->owner = THIS_MODULE;
  508.     entry->proc_fops = &input_handlers_fileops;

  509.     return 0;

  510.  fail2:    remove_proc_entry("devices", proc_bus_input_dir);
  511.  fail1: remove_proc_entry("input", proc_bus);
  512.     return -ENOMEM;
  513. }

  514. static void input_proc_exit(void)
  515. {
  516.     remove_proc_entry("devices", proc_bus_input_dir);
  517.     remove_proc_entry("handlers", proc_bus_input_dir);
  518.     remove_proc_entry("input", proc_bus);
  519. }

  520. #else /* !CONFIG_PROC_FS */
  521. static inline void input_wakeup_procfs_readers(void) { }
  522. static inline int input_proc_init(void) { return 0; }
  523. static inline void input_proc_exit(void) { }
  524. #endif

  525. #define INPUT_DEV_STRING_ATTR_SHOW(name)                    \
  526. static ssize_t input_dev_show_##name(struct class_device *dev, char *buf)    \
  527. {                                        \
  528.     struct input_dev *input_dev = to_input_dev(dev);            \
  529.                                         \
  530.     return scnprintf(buf, PAGE_SIZE, "%s\n",                \
  531.              input_dev->name ? input_dev->name : "");        \
  532. }                                        \
  533. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);

  534. INPUT_DEV_STRING_ATTR_SHOW(name);
  535. INPUT_DEV_STRING_ATTR_SHOW(phys);
  536. INPUT_DEV_STRING_ATTR_SHOW(uniq);

  537. static int input_print_modalias_bits(char *buf, int size,
  538.                  char name, unsigned long *bm,
  539.                  unsigned int min_bit, unsigned int max_bit)
  540. {
  541.     int len = 0, i;

  542.     len += snprintf(buf, max(size, 0), "%c", name);
  543.     for (i = min_bit; i < max_bit; i++)
  544.         if (bm[LONG(i)] & BIT(i))
  545.             len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  546.     return len;
  547. }

  548. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  549.                 int add_cr)
  550. {
  551.     int len;

  552.     len = snprintf(buf, max(size, 0),
  553.          "input:b%04Xv%04Xp%04Xe%04X-",
  554.          id->id.bustype, id->id.vendor,
  555.          id->id.product, id->id.version);

  556.     len += input_print_modalias_bits(buf + len, size - len,
  557.                 'e', id->evbit, 0, EV_MAX);
  558.     len += input_print_modalias_bits(buf + len, size - len,
  559.                 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  560.     len += input_print_modalias_bits(buf + len, size - len,
  561.                 'r', id->relbit, 0, REL_MAX);
  562.     len += input_print_modalias_bits(buf + len, size - len,
  563.                 'a', id->absbit, 0, ABS_MAX);
  564.     len += input_print_modalias_bits(buf + len, size - len,
  565.                 'm', id->mscbit, 0, MSC_MAX);
  566.     len += input_print_modalias_bits(buf + len, size - len,
  567.                 'l', id->ledbit, 0, LED_MAX);
  568.     len += input_print_modalias_bits(buf + len, size - len,
  569.                 's', id->sndbit, 0, SND_MAX);
  570.     len += input_print_modalias_bits(buf + len, size - len,
  571.                 'f', id->ffbit, 0, FF_MAX);
  572.     len += input_print_modalias_bits(buf + len, size - len,
  573.                 'w', id->swbit, 0, SW_MAX);

  574.     if (add_cr)
  575.         len += snprintf(buf + len, max(size - len, 0), "\n");

  576.     return len;
  577. }

  578. static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
  579. {
  580.     struct input_dev *id = to_input_dev(dev);
  581.     ssize_t len;

  582.     len = input_print_modalias(buf, PAGE_SIZE, id, 1);

  583.     return min_t(int, len, PAGE_SIZE);
  584. }
  585. static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);

  586. static struct attribute *input_dev_attrs[] = {
  587.     &class_device_attr_name.attr,
  588.     &class_device_attr_phys.attr,
  589.     &class_device_attr_uniq.attr,
  590.     &class_device_attr_modalias.attr,
  591.     NULL
  592. };

  593. static struct attribute_group input_dev_attr_group = {
  594.     .attrs    = input_dev_attrs,
  595. };

  596. #define INPUT_DEV_ID_ATTR(name)                            \
  597. static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf)    \
  598. {                                        \
  599.     struct input_dev *input_dev = to_input_dev(dev);            \
  600.     return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name);        \
  601. }                                        \
  602. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);

  603. INPUT_DEV_ID_ATTR(bustype);
  604. INPUT_DEV_ID_ATTR(vendor);
  605. INPUT_DEV_ID_ATTR(product);
  606. INPUT_DEV_ID_ATTR(version);

  607. static struct attribute *input_dev_id_attrs[] = {
  608.     &class_device_attr_bustype.attr,
  609.     &class_device_attr_vendor.attr,
  610.     &class_device_attr_product.attr,
  611.     &class_device_attr_version.attr,
  612.     NULL
  613. };

  614. static struct attribute_group input_dev_id_attr_group = {
  615.     .name    = "id",
  616.     .attrs    = input_dev_id_attrs,
  617. };

  618. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  619.              int max, int add_cr)
  620. {
  621.     int i;
  622.     int len = 0;

  623.     for (i = NBITS(max) - 1; i > 0; i--)
  624.         if (bitmap[i])
  625.             break;

  626.     for (; i >= 0; i--)
  627.         len += snprintf(buf + len, max(buf_size - len, 0),
  628.                 "%lx%s", bitmap[i], i > 0 ? " " : "");

  629.     if (add_cr)
  630.         len += snprintf(buf + len, max(buf_size - len, 0), "\n");

  631.     return len;
  632. }

  633. #define INPUT_DEV_CAP_ATTR(ev, bm)                        \
  634. static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf)    \
  635. {                                        \
  636.     struct input_dev *input_dev = to_input_dev(dev);            \
  637.     int len = input_print_bitmap(buf, PAGE_SIZE,                \
  638.                  input_dev->bm##bit, ev##_MAX, 1);        \
  639.     return min_t(int, len, PAGE_SIZE);                    \
  640. }                                        \
  641. static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);

  642. INPUT_DEV_CAP_ATTR(EV, ev);
  643. INPUT_DEV_CAP_ATTR(KEY, key);
  644. INPUT_DEV_CAP_ATTR(REL, rel);
  645. INPUT_DEV_CAP_ATTR(ABS, abs);
  646. INPUT_DEV_CAP_ATTR(MSC, msc);
  647. INPUT_DEV_CAP_ATTR(LED, led);
  648. INPUT_DEV_CAP_ATTR(SND, snd);
  649. INPUT_DEV_CAP_ATTR(FF, ff);
  650. INPUT_DEV_CAP_ATTR(SW, sw);

  651. static struct attribute *input_dev_caps_attrs[] = {
  652.     &class_device_attr_ev.attr,
  653.     &class_device_attr_key.attr,
  654.     &class_device_attr_rel.attr,
  655.     &class_device_attr_abs.attr,
  656.     &class_device_attr_msc.attr,
  657.     &class_device_attr_led.attr,
  658.     &class_device_attr_snd.attr,
  659.     &class_device_attr_ff.attr,
  660.     &class_device_attr_sw.attr,
  661.     NULL
  662. };

  663. static struct attribute_group input_dev_caps_attr_group = {
  664.     .name    = "capabilities",
  665.     .attrs    = input_dev_caps_attrs,
  666. };

  667. static struct attribute_group *input_dev_attr_groups[] = {
  668.     &input_dev_attr_group,
  669.     &input_dev_id_attr_group,
  670.     &input_dev_caps_attr_group,
  671.     NULL
  672. };

  673. static void input_dev_release(struct class_device *class_dev)
  674. {
  675.     struct input_dev *dev = to_input_dev(class_dev);

  676.     input_ff_destroy(dev);
  677.     kfree(dev);

  678.     module_put(THIS_MODULE);
  679. }

  680. /*
  681.  * Input uevent interface - loading event handlers based on
  682.  * device bitfields.
  683.  */
  684. static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
  685.                  char *buffer, int buffer_size, int *cur_len,
  686.                  const char *name, unsigned long *bitmap, int max)
  687. {
  688.     if (*cur_index >= num_envp - 1)
  689.         return -ENOMEM;

  690.     envp[*cur_index] = buffer + *cur_len;

  691.     *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
  692.     if (*cur_len >= buffer_size)
  693.         return -ENOMEM;

  694.     *cur_len += input_print_bitmap(buffer + *cur_len,
  695.                     max(buffer_size - *cur_len, 0),
  696.                     bitmap, max, 0) + 1;
  697.     if (*cur_len > buffer_size)
  698.         return -ENOMEM;

  699.     (*cur_index)++;
  700.     return 0;
  701. }

  702. static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
  703.                      char *buffer, int buffer_size, int *cur_len,
  704.                      struct input_dev *dev)
  705. {
  706.     if (*cur_index >= num_envp - 1)
  707.         return -ENOMEM;

  708.     envp[*cur_index] = buffer + *cur_len;

  709.     *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
  710.              "MODALIAS=");
  711.     if (*cur_len >= buffer_size)
  712.         return -ENOMEM;

  713.     *cur_len += input_print_modalias(buffer + *cur_len,
  714.                      max(buffer_size - *cur_len, 0),
  715.                      dev, 0) + 1;
  716.     if (*cur_len > buffer_size)
  717.         return -ENOMEM;

  718.     (*cur_index)++;
  719.     return 0;
  720. }

  721. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)                \
  722.     do {                                \
  723.         int err = add_uevent_var(envp, num_envp, &i,        \
  724.                     buffer, buffer_size, &len,    \
  725.                     fmt, val);            \
  726.         if (err)                        \
  727.             return err;                    \
  728.     } while (0)

  729. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max)                \
  730.     do {                                \
  731.         int err = input_add_uevent_bm_var(envp, num_envp, &i,    \
  732.                     buffer, buffer_size, &len,    \
  733.                     name, bm, max);            \
  734.         if (err)                        \
  735.             return err;                    \
  736.     } while (0)

  737. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)                \
  738.     do {                                \
  739.         int err = input_add_uevent_modalias_var(envp,        \
  740.                     num_envp, &i,            \
  741.                     buffer, buffer_size, &len,    \
  742.                     dev);                \
  743.         if (err)                        \
  744.             return err;                    \
  745.     } while (0)

  746. static int input_dev_uevent(struct class_device *cdev, char **envp,
  747.              int num_envp, char *buffer, int buffer_size)
  748. {
  749.     struct input_dev *dev = to_input_dev(cdev);
  750.     int i = 0;
  751.     int len = 0;

  752.     INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  753.                 dev->id.bustype, dev->id.vendor,
  754.                 dev->id.product, dev->id.version);
  755.     if (dev->name)
  756.         INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  757.     if (dev->phys)
  758.         INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  759.     if (dev->uniq)
  760.         INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);

  761.     INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  762.     if (test_bit(EV_KEY, dev->evbit))
  763.         INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  764.     if (test_bit(EV_REL, dev->evbit))
  765.         INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  766.     if (test_bit(EV_ABS, dev->evbit))
  767.         INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  768.     if (test_bit(EV_MSC, dev->evbit))
  769.         INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  770.     if (test_bit(EV_LED, dev->evbit))
  771.         INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  772.     if (test_bit(EV_SND, dev->evbit))
  773.         INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  774.     if (test_bit(EV_FF, dev->evbit))
  775.         INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  776.     if (test_bit(EV_SW, dev->evbit))
  777.         INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);

  778.     INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);

  779.     envp[i] = NULL;
  780.     return 0;
  781. }

  782. struct class input_class = {
  783.     .name            = "input",
  784.     .release        = input_dev_release,
  785.     .uevent            = input_dev_uevent,
  786. };
  787. EXPORT_SYMBOL_GPL(input_class);

  788. /**
  789.  * input_allocate_device - allocate memory for new input device
  790.  *
  791.  * Returns prepared struct input_dev or NULL.
  792.  *
  793.  * NOTE: Use input_free_device() to free devices that have not been
  794.  * registered; input_unregister_device() should be used for already
  795.  * registered devices.
  796.  */
  797. struct input_dev *input_allocate_device(void)
  798. {
  799.     struct input_dev *dev;

  800.     dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  801.     if (dev) {
  802.         dev->cdev.class = &input_class;
  803.         dev->cdev.groups = input_dev_attr_groups;
  804.         class_device_initialize(&dev->cdev);
  805.         mutex_init(&dev->mutex);
  806.         INIT_LIST_HEAD(&dev->h_list);
  807.         INIT_LIST_HEAD(&dev->node);

  808.         __module_get(THIS_MODULE);
  809.     }

  810.     return dev;
  811. }
  812. EXPORT_SYMBOL(input_allocate_device);

  813. /**
  814.  * input_free_device - free memory occupied by input_dev structure
  815.  * @dev: input device to free
  816.  *
  817.  * This function should only be used if input_register_device()
  818.  * was not called yet or if it failed. Once device was registered
  819.  * use input_unregister_device() and memory will be freed once last
  820.  * refrence to the device is dropped.
  821.  *
  822.  * Device should be allocated by input_allocate_device().
  823.  *
  824.  * NOTE: If there are references to the input device then memory
  825.  * will not be freed until last reference is dropped.
  826.  */
  827. void input_free_device(struct input_dev *dev)
  828. {
  829.     if (dev)
  830.         input_put_device(dev);
  831. }
  832. EXPORT_SYMBOL(input_free_device);

  833. /**
  834.  * input_set_capability - mark device as capable of a certain event
  835.  * @dev: device that is capable of emitting or accepting event
  836.  * @type: type of the event (EV_KEY, EV_REL, etc...)
  837.  * @code: event code
  838.  *
  839.  * In addition to setting up corresponding bit in appropriate capability
  840.  * bitmap the function also adjusts dev->evbit.
  841.  */
  842. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  843. {
  844.     switch (type) {
  845.     case EV_KEY:
  846.         __set_bit(code, dev->keybit);
  847.         break;

  848.     case EV_REL:
  849.         __set_bit(code, dev->relbit);
  850.         break;

  851.     case EV_ABS:
  852.         __set_bit(code, dev->absbit);
  853.         break;

  854.     case EV_MSC:
  855.         __set_bit(code, dev->mscbit);
  856.         break;

  857.     case EV_SW:
  858.         __set_bit(code, dev->swbit);
  859.         break;

  860.     case EV_LED:
  861.         __set_bit(code, dev->ledbit);
  862.         break;

  863.     case EV_SND:
  864.         __set_bit(code, dev->sndbit);
  865.         break;

  866.     case EV_FF:
  867.         __set_bit(code, dev->ffbit);
  868.         break;

  869.     default:
  870.         printk(KERN_ERR
  871.             "input_set_capability: unknown type %u (code %u)\n",
  872.             type, code);
  873.         dump_stack();
  874.         return;
  875.     }

  876.     __set_bit(type, dev->evbit);
  877. }
  878. EXPORT_SYMBOL(input_set_capability);

  879. int input_register_device(struct input_dev *dev)
  880. {
  881.     static atomic_t input_no = ATOMIC_INIT(0);
  882.     struct input_handler *handler;
  883.     const char *path;
  884.     int error;

  885.     set_bit(EV_SYN, dev->evbit);

  886.     /*
  887.      * If delay and period are pre-set by the driver, then autorepeating
  888.      * is handled by the driver itself and we don't do it in input.c.
  889.      */

  890.     init_timer(&dev->timer);
  891.     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  892.         dev->timer.data = (long) dev;
  893.         dev->timer.function = input_repeat_key;
  894.         dev->rep[REP_DELAY] = 250;
  895.         dev->rep[REP_PERIOD] = 33;
  896.     }

  897.     if (!dev->getkeycode)
  898.         dev->getkeycode = input_default_getkeycode;

  899.     if (!dev->setkeycode)
  900.         dev->setkeycode = input_default_setkeycode;

  901.     list_add_tail(&dev->node, &input_dev_list);

  902.     snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
  903.          "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);

  904.     if (!dev->cdev.dev)
  905.         dev->cdev.dev = dev->dev.parent;

  906.     error = class_device_add(&dev->cdev);
  907.     if (error)
  908.         return error;

  909.     path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  910.     printk(KERN_INFO "input: %s as %s\n",
  911.         dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  912.     kfree(path);

  913.     list_for_each_entry(handler, &input_handler_list, node)
  914.         input_attach_handler(dev, handler);

  915.     input_wakeup_procfs_readers();

  916.     return 0;
  917. }
  918. EXPORT_SYMBOL(input_register_device);

  919. void input_unregister_device(struct input_dev *dev)
  920. {
  921.     struct input_handle *handle, *next;
  922.     int code;

  923.     for (code = 0; code <= KEY_MAX; code++)
  924.         if (test_bit(code, dev->key))
  925.             input_report_key(dev, code, 0);
  926.     input_sync(dev);

  927.     del_timer_sync(&dev->timer);

  928.     list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  929.         handle->handler->disconnect(handle);
  930.     WARN_ON(!list_empty(&dev->h_list));

  931.     list_del_init(&dev->node);

  932.     class_device_unregister(&dev->cdev);

  933.     input_wakeup_procfs_readers();
  934. }
  935. EXPORT_SYMBOL(input_unregister_device);

  936. int input_register_handler(struct input_handler *handler)
  937. {
  938.     struct input_dev *dev;

  939.     INIT_LIST_HEAD(&handler->h_list);

  940.     if (handler->fops != NULL) {
  941.         if (input_table[handler->minor >> 5])
  942.             return -EBUSY;

  943.         input_table[handler->minor >> 5] = handler;
  944.     }

  945.     list_add_tail(&handler->node, &input_handler_list);

  946.     list_for_each_entry(dev, &input_dev_list, node)
  947.         input_attach_handler(dev, handler);

  948.     input_wakeup_procfs_readers();
  949.     return 0;
  950. }
  951. EXPORT_SYMBOL(input_register_handler);

  952. void input_unregister_handler(struct input_handler *handler)
  953. {
  954.     struct input_handle *handle, *next;

  955.     list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  956.         handler->disconnect(handle);
  957.     WARN_ON(!list_empty(&handler->h_list));

  958.     list_del_init(&handler->node);

  959.     if (handler->fops != NULL)
  960.         input_table[handler->minor >> 5] = NULL;

  961.     input_wakeup_procfs_readers();
  962. }
  963. EXPORT_SYMBOL(input_unregister_handler);

  964. int input_register_handle(struct input_handle *handle)
  965. {
  966.     struct input_handler *handler = handle->handler;

  967.     list_add_tail(&handle->d_node, &handle->dev->h_list);
  968.     list_add_tail(&handle->h_node, &handler->h_list);

  969.     if (handler->start)
  970.         handler->start(handle);

  971.     return 0;
  972. }
  973. EXPORT_SYMBOL(input_register_handle);

  974. void input_unregister_handle(struct input_handle *handle)
  975. {
  976.     list_del_init(&handle->h_node);
  977.     list_del_init(&handle->d_node);
  978. }
  979. EXPORT_SYMBOL(input_unregister_handle);

  980. static int input_open_file(struct inode *inode, struct file *file)
  981. {   // 根据所打开文件的次设备号,得到一个input_handler
  982.     struct input_handler *handler = input_table[iminor(inode) >> 5]; // 分配一个input_handler结构体
  983.     const struct file_operations *old_fops, *new_fops = NULL;
  984.     int err;

  985.     /* No load-on-demand here? */
  986.     if (!handler || !(new_fops = fops_get(handler->fops))) // new_fops与handler中的fops不一样则返回。
  987.         return -ENODEV;

  988.     /*
  989.      * That's _really_ odd. Usually NULL ->open means "nothing special",
  990.      * not "no device". Oh, well...
  991.      */
  992.     if (!new_fops->open) {       // 如果新的fops中的open没有被打开。
  993.         fops_put(new_fops);      // 就将fpos放入新的fops中。
  994.         return -ENODEV;
  995.     }                            // 否则将新fops写入旧的fops中。
  996.     old_fops = file->f_op;
  997.     file->f_op = new_fops;     // 将新的fops传入旧的fops

  998.     err = new_fops->open(inode, file);   // 返回新的fops

  999.     if (err) {                           // 如果新的fops被打开
  1000.         fops_put(file->f_op);
  1001.         file->f_op = fops_get(old_fops);
  1002.     }
  1003.     fops_put(old_fops);
  1004.     return err;                           //  返回fops
  1005. }

  1006. static const struct file_operations input_fops = {
  1007.     .owner = THIS_MODULE,
  1008.     .open = input_open_file,
  1009. };

  1010. static int __init input_init(void)
  1011. {
  1012.     int err;

  1013.     err = class_register(&input_class);
  1014.     if (err) {
  1015.         printk(KERN_ERR "input: unable to register input_dev class\n");
  1016.         return err;
  1017.     }

  1018.     err = input_proc_init();
  1019.     if (err)
  1020.         goto fail1;

  1021.     err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  1022.     if (err) {
  1023.         printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  1024.         goto fail2;
  1025.     }

  1026.     return 0;

  1027.  fail2:    input_proc_exit();
  1028.  fail1:    class_unregister(&input_class);
  1029.     return err;
  1030. }

  1031. static void __exit input_exit(void)
  1032. {
  1033.     input_proc_exit();
  1034.     unregister_chrdev(INPUT_MAJOR, "input");
  1035.     class_unregister(&input_class);
  1036. }

  1037. subsys_initcall(input_init);
  1038. module_exit(input_exit);

要看一个驱动程序,首先要从入口函数开始看。
阅读(1070) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~