usb/hid: The HID Simple Driver Interface 0.4.0 (core)
PATCH 1/1 usb/hid: The HID Simple Driver Interface 0.4.0 (core)
Changelogs:
1. Two bugfixs in connect/clear processing. so the double reference counting problem is gone.
2. A lot of code refactoring, e.g. move push/pop operations into the connect function.
3. The printk() invokes clean from Vincent Legoll, thanks him/her.
4. Once one simple HID driver attached to a device, it will re-register its input device to the input subsystem, so userspace can receive new hotplug message with new feature information, instead of silent change input device for userspace.
This is one patch of The HID Simple Driver patches, for convenience, you can find them all in that mail:
[PATCH] usb/hid: The HID Simple Driver Patches 0.4.0 (all-in-one)
It can be applied on 2.6.18 at least.
Is this ready to merge? or What still is problem in them? Thanks.
drivers/usb/input/hid-core.c renamed from drivers/usb/input.orig/hid-core.c +199 -7
@@ -26,6 +26,7 @@#include <asm/byteorder.h> #include <linux/input.h> #include <linux/wait.h> +#include <linux/mutex.h> #undef DEBUG#undef DEBUG_DATA
@@ -33,6 +34,7 @@#include <linux/usb.h> #include "hid.h" +#include "hid-simple.h" #include <linux/hiddev.h> /*
@@ -46,6 +48,15 @@static char *hid_types[] = {"Device" ,"Pointer" ,"Mouse" ,"Device" ,"Joystick" ,"Gamepad" ,"Keyboard" ,"Keypad" ,"Multi-Axis Controller" }; + +/* +* The global data structure for simple device driver interface. +*/ +static DEFINE_MUTEX (matched_devices_lock); +static DEFINE_MUTEX (simple_drivers_lock); +static struct list_head matched_devices_list; +static struct list_head simple_drivers_list; +/* * Module parameters. */
@@ -785,8 +796,12 @@ static __inline__ int search(__s32 *arrastatic void hid_process_event (struct hid_device *hid,struct hid_field *field,struct hid_usage *usage,__s32 value,int interrupt,struct pt_regs *regs) {hid_dump_input (usage, value); -if (hid->claimed &HID_CLAIMED_INPUT ) +if (hid->claimed &HID_CLAIMED_INPUT ) { +if (!hidinput_simple_event_filter (hid, field, usage, value, regs)) +return ;hidinput_hid_event (hid, field, usage, value, regs); +hidinput_simple_event_post (hid, field, usage, value, regs); + }if (hid->claimed &HID_CLAIMED_HIDDEV && interrupt)hiddev_hid_event (hid, field, usage, value, regs); }
@@ -832,7 +847,6 @@ static void hid_input_field(struct hid_d && field->usage[field->value[n] - min].hid && search(value, field->value[n], count))hid_process_event (hid, field, &field->usage[field->value[n] - min],0 , interrupt, regs); -if (value[n] >= min && value[n] <= max && field->usage[value[n] - min].hid &&search (field->value, value[n], count))
@@ -2031,8 +2045,32 @@ static void hid_disconnect(struct usb_indel_timer_sync (&hid->io_retry);flush_scheduled_work (); -if (hid->claimed &HID_CLAIMED_INPUT ) +if (hid->claimed &HID_CLAIMED_INPUT ) { +struct list_head *node; +struct matched_device *matched =NULL ; +struct hidinput_simple_driver *simple = hid->simple; + +mutex_lock (&matched_devices_lock); +list_for_each (node, &matched_devices_list) { + matched = list_entry(node, struct matched_device, node); +if (matched->intf == intf) { +list_del (&matched->node); +break ; + } + matched =NULL ; + } +mutex_unlock (&matched_devices_lock); +/* disconnect simple device driver if need */ +if (matched && simple) { +hidinput_simple_driver_disconnect (hid, matched); +module_put (simple->owner); + } +if (matched) { + matched->intf =NULL ; +kfree (matched); + }hidinput_disconnect (hid); + }if (hid->claimed &HID_CLAIMED_HIDDEV )hiddev_disconnect (hid);
@@ -2045,6 +2083,140 @@ static void hid_disconnect(struct usb_inhid_free_device (hid); } +#ifdef CONFIG_HID_SIMPLE +static int hidinput_simple_driver_bind_one (struct hidinput_simple_driver *simple, +struct hid_device *hid, +struct matched_device *matched, +int reload) +{ +int ret; + +if (!try_module_get (simple->owner)) +return -ENODEV ; +/* +* If the simple driver register itself after device insert, +* we must send hotplug message to userspace again, elsewise, +* we will break down some applications. +*/ +if (reload &&hidinput_disconnect_core (hid)) { +printk (KERN_WARNING "The HID device \'%s\' disconnected!" , hid->name); +module_put (simple->owner); +return -ENODEV ; + } + ret =hidinput_simple_driver_connect (simple, hid, matched); + +if (reload) +hidinput_reconnect_core (hid);/* To activate this input_dev */ +if (!ret) +printk (KERN_INFO "The simple driver \'%s\' attach" +" to the device \'%s\'\n" , simple->name, hid->name); +else +module_put (simple->owner); +return 0 ; +} + +/* Only be called in hid_probe(), so we just use the hid of matched->intf. */ +static void hidinput_simple_driver_bind_foreach_simple ( +struct matched_device *matched) +{ +struct hidinput_simple_driver *simple; +struct list_head *node; +struct hid_device *hid; + +if (!matched || !matched->intf)/* should not get here */ +return ; + hid =usb_get_intfdata (matched->intf); +if (!hid || hid->simple) +return ; + +mutex_lock (&simple_drivers_lock); +list_for_each (node, &simple_drivers_list) { + simple = list_entry(node, struct hidinput_simple_driver, node); +if (usb_match_id (matched->intf, simple->id_table)) { +if (!hidinput_simple_driver_bind_one (simple, hid, matched,0 )) +break ; + } + } +mutex_unlock (&simple_drivers_lock); +} + +static void hidinput_simple_driver_bind_foreach_matched ( +struct hidinput_simple_driver *simple) +{ +struct list_head *node=NULL ; +struct matched_device *matched; +struct hid_device *hid=NULL ; + +if (!simple) +return ; + +mutex_lock (&matched_devices_lock); +list_for_each (node, &matched_devices_list) { + matched = list_entry(node, struct matched_device, node); + hid =usb_get_intfdata (matched->intf); +if (!hid || hid->simple) +continue ; +if (!usb_match_id (matched->intf, simple->id_table)) +continue ; +if (!hidinput_simple_driver_bind_one (simple, hid, matched,1 )) +break ; + } +mutex_unlock (&matched_devices_lock); +} + +static void hidinput_simple_driver_bind_foreach (void ) +{ +struct hidinput_simple_driver *simple; +struct matched_device *matched =NULL ; +struct list_head *matched_node =NULL , *simple_node =NULL ; +struct hid_device *hid =NULL ; + +mutex_lock (&matched_devices_lock); +list_for_each (matched_node, &matched_devices_list) { + matched = list_entry(matched_node, struct matched_device, node); + hid =usb_get_intfdata (matched->intf); +if (!hid || hid->simple) +continue ; +mutex_lock (&simple_drivers_lock); +list_for_each (simple_node, &simple_drivers_list) { + simple = list_entry(simple_node, struct hidinput_simple_driver, node); +if (!usb_match_id (matched->intf, simple->id_table)) +continue ; +hidinput_simple_driver_bind_one (simple, hid, matched,1 ); + } +mutex_unlock (&simple_drivers_lock); + } +mutex_unlock (&matched_devices_lock); +} + +int hidinput_register_simple_driver (struct hidinput_simple_driver *simple) +{ +if (!simple || !simple->name) +return -EINVAL ; + +hidinput_simple_driver_init (simple); +hidinput_simple_driver_bind_foreach_matched (simple); + +mutex_lock (&simple_drivers_lock); +list_add (&simple->node, &simple_drivers_list); +mutex_unlock (&simple_drivers_lock); + +return 0 ; +} +EXPORT_SYMBOL_GPL (hidinput_register_simple_driver); + +void hidinput_unregister_simple_driver (struct hidinput_simple_driver *simple) +{ +hidinput_simple_driver_clear (simple); +mutex_lock (&simple_drivers_lock); +list_del (&simple->node); +mutex_unlock (&simple_drivers_lock); +/* to active simple device driver that it is waiting */ +hidinput_simple_driver_bind_foreach (); +} +EXPORT_SYMBOL_GPL (hidinput_unregister_simple_driver); +#endif +static int hid_probe (struct usb_interface *intf,const struct usb_device_id *id) {struct hid_device *hid;
@@ -2061,15 +2233,25 @@ static int hid_probe(struct usb_interfachid_init_reports (hid);hid_dump_device (hid); -if (!hidinput_connect (hid)) +usb_set_intfdata (intf, hid); + +if (!hidinput_connect (hid)) { +struct matched_device *matched; + matched =kmalloc (sizeof (struct matched_device ),GFP_KERNEL ); +if (matched) { + matched->intf = intf; +mutex_lock (&matched_devices_lock); +list_add (&matched->node, &matched_devices_list); +mutex_unlock (&matched_devices_lock); +hidinput_simple_driver_bind_foreach_simple (matched); + } hid->claimed |=HID_CLAIMED_INPUT ; + }if (!hiddev_connect (hid)) hid->claimed |=HID_CLAIMED_HIDDEV ; -usb_set_intfdata (intf, hid); -if (!hid->claimed) { -printk ("HID device not claimed by input or hiddev\n" ); +printk (KERN_INFO "HID device not claimed by input or hiddev\n" );hid_disconnect (intf);return -ENODEV ; }
@@ -2168,6 +2350,8 @@ static int __init hid_init(void) retval =hiddev_init ();if (retval) goto hiddev_init_fail; +INIT_LIST_HEAD (&matched_devices_list); +INIT_LIST_HEAD (&simple_drivers_list); retval =usb_register (&hid_driver);if (retval) goto usb_register_fail;
@@ -2182,7 +2366,15 @@ hiddev_init_fail: static void __exit hid_exit(void) { +struct list_head *node, *tmp; +struct matched_device *matched; +usb_deregister (&hid_driver); +list_for_each_safe (node, tmp, &matched_devices_list) { + matched = list_entry(node, struct matched_device, node); +list_del (&matched->node); +kfree (matched); + }hiddev_exit (); }
drivers/usb/input/hid.h renamed from drivers/usb/input.orig/hid.h +15 -1
@@ -33,7 +33,7 @@#include <linux/list.h> #include <linux/timer.h> #include <linux/workqueue.h> - +#include <linux/mutex.h> /* * USB HID (Human Interface Device) interface class code */
@@ -389,8 +389,16 @@ struct hid_input {struct list_head list;struct hid_report *report;struct input_dev *input; +struct mutex input_lock;/* To protect accessing input_dev from +* hid-input processing. But it not +* necessary for accessing this input_dev +* instance from input subsystem. +*/ +void *private; }; +struct hidinput_simple_driver ; +struct hid_device {/* device report descriptor */ __u8 *rdesc;unsigned rsize;
@@ -454,6 +462,8 @@ struct hid_device { /* device repoint (*ff_event)(struct hid_device *hid,struct input_dev *input,unsigned int type,unsigned int code,int value); +struct hidinput_simple_driver *simple; +#ifdef CONFIG_USB_HIDINPUT_POWERBOOK unsigned long pb_pressed_fn[NBITS (KEY_MAX )];unsigned long pb_pressed_numlock[NBITS (KEY_MAX )];
@@ -508,12 +518,16 @@ extern void hidinput_hid_event(struct hiextern void hidinput_report_event (struct hid_device *hid,struct hid_report *report);extern int hidinput_connect (struct hid_device *);extern void hidinput_disconnect (struct hid_device *); +extern void hidinput_reconnect_core (struct hid_device *); +extern int hidinput_disconnect_core (struct hid_device *);#else #define IS_INPUT_APPLICATION (a) (0)static inline void hidinput_hid_event (struct hid_device *hid,struct hid_field *field,struct hid_usage *usage,__s32 value,struct pt_regs *regs) { }static inline void hidinput_report_event (struct hid_device *hid,struct hid_report *report) { }static inline int hidinput_connect (struct hid_device *hid) {return -ENODEV ; }static inline void hidinput_disconnect (struct hid_device *hid) { } +static inline void hidinput_reconnect_core (struct hid_device *hid) { } +static inline int hidinput_disconnect_core (struct hid_device *hid) {return -ENODEV ; }#endif int hid_open (struct hid_device *);
drivers/usb/input/hid-input.c renamed from drivers/usb/input.orig/hid-input.c +158 -49
388 lines not on this page · raw message
drivers/usb/input/hid-simple.c renamed from drivers/usb/input.orig/hid-simple.c +326 -0
326 lines not on this page · raw message
drivers/usb/input/hid-simple.h renamed from drivers/usb/input.orig/hid-simple.h +254 -0
254 lines not on this page · raw message
Re: [PATCH] usb/hid: The HID Simple Driver Interface 0.4.0 documentation
--- linux-2.6.18/Documentation/input/no-such.txt 1970-01-01 08:00:00.000000000 +0800
+++ linux-2.6.18/Documentation/input/simple-hid.txt 2006-10-12 09:34:42.000000000 +0800
@@ -0,0 +1,251 @@
+==================================
+HID device simple driver interface
+==================================
+
+------------------------
+Note
+------------------------
+
+ If you just begin to study from writing input device driver, please see the
+input-programming.txt, I am afraid this is not you want, do not confuse with the
+"simple" in this name.
+
+------------------------
+Version
+------------------------
+
+ This is the version 0.4.0
+
+--------------------------
+Overview
+--------------------------
+
+ Under standard HID device driver development means, we need to write +one interrupt handler for each new HID device to report event to +input subsystem. However, although the most of they can not merge into +mainstream kernel tree, they have only some extended keys, e.g. many +remote controllers. I think it seem break a fly on the wheel, which +write one new interrupt handler for this reason.
+
+ The basic idea is reuse the interrupt handler in hid-core.c. so writing +driver for new simple HID device will be more easier, quickly, and do not +need touch hid core.
+
+ In essence, this interface just is some hooks in HID core.
+
+ The hid-simple.h include this API. But, defore use this interface, you must
+include hid.h first.
+
+------------------------
+What's you will get from this interface.
+------------------------
+
+ Use me, you can:
+
+ 1. Write the driver of USB input device without write code take care of
+ details of setup and clear usage.
+ 2. A driver use this interface can be as a HID device input filter.
+ 3. Write USB force-feed driver without touch hid-input core. however, this
+ feature is confilct with HID_FF.
+
+ This interface can not support the more drivers handle one device at same time
+yet. I can not sure if we really need this feature.
+
+------------------------
+The driver use this interface
+------------------------
+
+ So far, there are two drivers:
+ 1. MS Natural Ergonomic Keyboard 4000 driver. (usbnek4k.c)
+ 2. Betop BTP-2118 joystick force-feed driver. (btp2118.c)
+
+-------------------------
+Requires
+-------------------------
+
+ Before use this interface, you must turn on these kernel configuration +items:
+
+ CONFIG_HID_SIMPLE : HID simple driver interface
+ CONFIG_HID_SIMPLE_FF : HID simple driver interface force feedback support +
+ Note: You can see the latter only if you turn off CONFIG_HID_FF and turn +on CONFIG_HID_SIMPLE.
+
+--------------------------
+Register and unregister
+--------------------------
+
+1. Register a simple driver.
+
+ int hidinput_register_simple_driver(struct hidinput_simple_driver *simple) +
+ Like any driver register function, it register your driver to kernel, +when the chance that match device come, your driver can probe it. At least, you
+must fill the member of parameter simple : name.
+ Return 0 mean register successfully. elsewise mean there have some +failures in register process.
+ So far, this function only can return 0 and -EINVAL.
+ When you driver matched one device, it will reregister it into input +subsystem (see the function hidinput_reconnect_core(), if you want). +
+2. Unregister a simple driver.
+
+ void hidinput_unregister_simple_driver(struct hidinput_simple_driver *simple)
+
+ Like any driver register function, it clean your driver from kernel, and +in this process, your driver will disconnect any device which it matched. However,
+it do not free your memory of driver structure, that's your task.
+ This may reregister the device into input subsystem (see the function +hidinput_reconnect_core(), if you want).
+
+----------------------------------
+Usage
+----------------------------------
+
+ Each simple driver have one data structure hidinput_simple_driver:
+
+struct hidinput_simple_driver {
+ /*
+ * The members for implement only are ignored here,
+ * please do not depend on them
+ */
+ /* public */
+ struct module *owner;
+ char *name;
+ int (*connect)(struct hid_device *, struct hid_input *);
+ void (*disconnect)(struct hid_device *, struct hid_input *);
+ void (*setup_usage)(struct hid_field *, struct hid_usage *);
+ void (*clear_usage)(struct hid_field *, struct hid_usage *);
+ int (*pre_event)(const struct hid_device *, const struct hid_field *,
+ const struct hid_usage *, const __s32,
+ const struct pt_regs *regs);
+ int (*post_event)(const struct hid_device *, const struct hid_field *,
+ const struct hid_usage *, const __s32,
+ const struct pt_regs *regs);
+
+ int (*open)(struct input_dev *dev);
+ void (*close)(struct input_dev *dev);
+ int (*upload_effect)(struct input_dev *dev, struct ff_effect *effect);
+ int (*erase_effect)(struct input_dev *dev, int effect_id);
+ int (*flush)(struct input_dev *dev, struct file *file);
+ int (*ff_event)(struct input_dev *dev, int type, int code, int value); +
+ void *private;
+ struct usb_device_id *id_table;
+ struct usage_page_block *usage_page_table;
+};
+
+The data member description:
+
+ struct module *owner;
+
+ In most cases, set this member to THIS_MODULE is your want to. +
+ char *name;
+
+ The name of your driver. you must fill this member before register it. +
+ void *private;
+
+ You can save the data that your driver use only here. HID simple driver +core do not take care of this.
+
+ struct usb_device_id *id_table;
+
+ As same with other USB device driver, this is used by matching device, +if so, then call probe()/connect() methods of your driver.
+ In general, you always want to fill this member with something. +
+ struct usage_page_block *usage_page_table;
+
+ Totally, there are three means you can complete setup and clean HID usage work,
+which use this member is one of them. Moreover, this means is the most simplest one.
+By fill this member, kernel will complete setup and clean usage work automatically.
+ It will spend many words to explain details, I think the best documentation
+of this is the example, you can find them in usbnek4k.c and btp2118.c. I believe
+you have enough intelligence to understand them, but if you had familiar +with HID first. :) When you read these examples, you also will find out some other
+related data structures ,however, don't worry, they are too simple to not discuss
+them.
+
+--------------------------------
+Generic Methods
+--------------------------------
+
+The simple driver methods description:
+
+ Although this simple driver have not direct relation with Linux +device driver architecture, but I still make its API like it on purpose. +The simple driver have follow methods:
+
+1. int (*connect)(struct hid_device *, struct hid_input *);
+2. void (*disconnect)(struct hid_device *, struct hid_input *); +
+ When you simple driver is to bind with one real HID device, we will +call connect() method first. To return 0 flag that it complete its mission +successfully, so we can continue, return any other value is looked as +error, any processing do not come.
+ When the device which your simple driver connect with is down, or +this simple driver is unregistered, we will call disconnect() method first. +
+3. void (*setup_usage)(struct hid_field *, struct hid_usage *); +4. void (*clear_usage)(struct hid_field *, struct hid_usage *); +
+ The setup_usage() method like hidinput_configure_usage() in +hid_input.c. You also can setup input_dev here. I think
+you should be fill the pointer slot for this method, or fill usage_page_table
+member of hidinput_simple_driver, elsewise the event() method do not work for
+you at all. Please see example in "MS Natural Ergonomic Keyboard 4000" driver.
+ The clear_usage() method is used to clear side-effect that came from +setup_usage() method when one driver disconnect one device.
+ Of course, you can do same things in connect/disconnect() method, but +these method can make your life more simpler.
+ At last, you can use these two methods and usage_page_table member +at same time. These two methods always will be called after kernel process +usage_page_table.
+
+6. int (*pre_event)(const struct hid_device *, const struct hid_field +*, const struct hid_usage *, const __s32, const struct pt_regs *regs); +
+ First, you can use this method send event to input subsystem, +moreover, you can use this as one usage code filter: this method is called +before all other event handling. If it return non-zero , any other event +handling do not be processed, even the hid-input itself. If this method +return zero, the normally event handling process will continue.
+ See the note [1].
+
+7. void (*post_event)(const struct hid_device *, const struct +hid_field *, const struct hid_usage *, const __s32, const struct pt_regs +*regs);
+
+ This is called after all other event handling. So if the pre_event() method
+return non-zero value, this method also do not called at all.
+ See the note [1].
+
+8. int (*open)(struct input_dev *dev);
+9. void (*close)(struct input_dev *dev);
+
+ They are same with corresponding methods of struct input_dev. I assume +you already familiar with them.
+
+---------------------------
+Force-feed support Methods
+---------------------------
+Follow methods are valid only if you turn on CONFIG_HID_SIMPLE_FF:
+
+10. int (*upload_effect)(struct input_dev *dev, struct ff_effect *effect); +11. int (*erase_effect)(struct input_dev *dev, int effect_id); +12. int (*flush)(struct input_dev *dev, struct file *file); +13. int (*ff_event)(struct input_dev *dev, int type, int code, int value); +
+ They are same with corresponding methods of struct input_dev. I assume +you already familiar with them.
+
+
+=================================================================================
+
+NOTE:
+
+[1] If you do not configure usage correctly, this method do not work as
+ you want.