linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 5/6] usbhid: add 'quirks' module parameter
@ 2007-04-11 19:03 Paul Walmsley
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Walmsley @ 2007-04-11 19:03 UTC (permalink / raw)
  To: linux-input

From: Paul Walmsley <paul@booyaka.com>

Add internal support for dynamically-allocated HID quirks, "dquirks"
(for "dynamic quirks").  Includes several functions to add/modify quirks
from the list.  This code is used by the next patch to implement quirk
modification upon module load.


Signed-off-by: Paul Walmsley <paul@booyaka.com>

---
  drivers/hid/usbhid/hid-quirks.c |  147 ++++++++++++++++++++++++++++++++++++++--
  include/linux/hid-quirks.h      |    2
  2 files changed, 145 insertions(+), 4 deletions(-)

Index: hid/drivers/hid/usbhid/hid-quirks.c
===================================================================
--- hid.orig/drivers/hid/usbhid/hid-quirks.c
+++ hid/drivers/hid/usbhid/hid-quirks.c
@@ -39,7 +39,9 @@
  #include "usbhid.h"

  static const struct hid_blacklist *usbhid_exists_squirk(const u16 idVendor, const u16 idProduct);
-
+static struct hid_blacklist *usbhid_exists_dquirk(const u16 idVendor, const u16 idProduct);
+static void usbhid_remove_all_dquirks(void);
+

  #define USB_VENDOR_ID_A4TECH		0x09da
  #define USB_DEVICE_ID_A4TECH_WCP32PU	0x0006
@@ -470,6 +472,16 @@ static const struct hid_blacklist {



+/* Dynamic HID quirks list - specified at runtime	 */
+struct quirks_list_struct {
+	struct hid_blacklist hid_bl_item;
+	struct list_head node;
+};
+
+static LIST_HEAD(dquirks_list);
+static DECLARE_RWSEM(dquirks_rwsem);
+
+
  /**
   * usbhid_lookup_quirk: return any quirks associated with a USB HID device
   * @idVendor: the 16-bit USB vendor ID, in native byteorder
@@ -495,9 +507,16 @@ u32 usbhid_lookup_quirk(const u16 idVend
  		    idProduct <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
  			return 0;

-	ble = usbhid_exists_squirk(idVendor, idProduct);
-	if (ble)
-		quirks = ble->quirks;
+	down_read(&dquirks_rwsem);
+
+ 	ble = usbhid_exists_dquirk(idVendor, idProduct);
+ 	if (!ble)
+ 		ble = usbhid_exists_squirk(idVendor, idProduct);
+
+ 	if (ble)
+ 		quirks = ble->quirks;
+
+ 	up_read(&dquirks_rwsem);

  	return quirks;
  }
@@ -534,3 +553,123 @@ static const struct hid_blacklist *usbhi
  	return ble;
  }

+
+/* Runtime ("dynamic") quirks manipulation functions */
+
+/**
+ * usbhid_exists_dquirk: find any dynamic quirks for a USB HID device
+ * @idVendor: the 16-bit USB vendor ID, in native byteorder
+ * @idProduct: the 16-bit USB product ID, in native byteorder
+ *
+ * Description:
+ *         Scans dquirks_list for a matching dynamic quirk and returns
+ *         the pointer to the relevant struct hid_blacklist if found.
+ *         Must be called with a read lock held on dquirks_rwsem.
+ *
+ * Returns: NULL if no quirk found, struct hid_blacklist * if found.
+ */
+static struct hid_blacklist *usbhid_exists_dquirk(const u16 idVendor,
+						  const u16 idProduct)
+{
+	struct quirks_list_struct *q;
+	struct hid_blacklist *ble = NULL;
+
+	WARN_ON(idVendor == 0);
+
+	list_for_each_entry(q, &dquirks_list, node) {
+		if (q->hid_bl_item.idVendor == idVendor &&
+		    q->hid_bl_item.idProduct == idProduct) {
+			ble = &q->hid_bl_item;
+			break;
+		}
+	}
+
+	if (ble != NULL)
+		dbg("Found dynamic quirk 0x%x for USB HID vendor 0x%hx prod 0x%hx\n",
+		    ble->quirks, ble->idVendor,  ble->idProduct);
+
+	return ble;
+}
+
+
+/**
+ * usbhid_modify_dquirk: add/replace a HID quirk
+ * @idVendor: the 16-bit USB vendor ID, in native byteorder
+ * @idProduct: the 16-bit USB product ID, in native byteorder
+ * @quirks: the u32 quirks value to add/replace
+ *
+ * Description:
+ *         If an dynamic quirk exists in memory for this (idVendor,
+ *         idProduct) pair, replace its quirks value with what was
+ *         provided.  Otherwise, add the quirk to the dynamic quirks list.
+ *
+ * Returns: 0 OK, -error on failure.
+ */
+int usbhid_modify_dquirk(const u16 idVendor, const u16 idProduct,
+			 const u32 quirks)
+{
+	struct quirks_list_struct *q_new, *q;
+	int list_edited = 0;
+
+	if (!idVendor) {
+		dbg("Cannot add a quirk with idVendor = 0");
+		return -EINVAL;
+	}
+
+	q_new = kmalloc(sizeof(struct quirks_list_struct), GFP_KERNEL);
+	if (!q_new) {
+		dbg("Could not allocate quirks_list_struct");
+		return -ENOMEM;
+	}
+
+	q_new->hid_bl_item.idVendor = idVendor;
+	q_new->hid_bl_item.idProduct = idProduct;
+	q_new->hid_bl_item.quirks = quirks;
+
+	down_write(&dquirks_rwsem);
+
+	list_for_each_entry(q, &dquirks_list, node) {
+
+		if (q->hid_bl_item.idVendor == idVendor &&
+		    q->hid_bl_item.idProduct == idProduct) {
+
+			list_replace(&q->node, &q_new->node);
+			kfree(q);
+			list_edited = 1;
+			break;
+
+		}
+
+	}
+
+	if (!list_edited)
+		list_add_tail(&q_new->node, &dquirks_list);
+
+	up_write(&dquirks_rwsem);
+
+	return 0;
+}
+
+
+/**
+ * usbhid_remove_all_dquirks: remove all runtime HID quirks from memory
+ *
+ * Description:
+ *         Free all memory associated with dynamic quirks - called before
+ *         module unload.
+ *
+ */
+static void usbhid_remove_all_dquirks(void)
+{
+	struct quirks_list_struct *q, *temp;
+
+	down_write(&dquirks_rwsem);
+	list_for_each_entry_safe(q, temp, &dquirks_list, node) {
+		list_del(&q->node);
+		kfree(q);
+	}
+	up_write(&dquirks_rwsem);
+
+}
+
+
Index: hid/include/linux/hid-quirks.h
===================================================================
--- hid.orig/include/linux/hid-quirks.h
+++ hid/include/linux/hid-quirks.h
@@ -31,6 +31,8 @@

  u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct);

+int usbhid_modify_dquirk(const u16 idVendor, const u16 idProduct, const u32 quirks);
+


  #endif /* __HID_QUIRKS_H */

^ permalink raw reply	[flat|nested] 2+ messages in thread
* [PATCH 5/6] usbhid: add 'quirks' module parameter
@ 2007-04-11 19:09 Paul Walmsley
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Walmsley @ 2007-04-11 19:09 UTC (permalink / raw)
  To: linux-input

From: Paul Walmsley <paul@booyaka.com>

Add a 'quirks' module parameter for the usbhid module, so users can
add or modify quirks at module load time.


Signed-off-by: Paul Walmsley <paul@booyaka.com>

---
  Documentation/kernel-parameters.txt |    8 ++++++
  drivers/hid/usbhid/hid-core.c       |   26 +++++++++++++++++++
  drivers/hid/usbhid/hid-quirks.c     |   47 ++++++++++++++++++++++++++++++++++++
  include/linux/hid-quirks.h          |    7 +++--
  4 files changed, 86 insertions(+), 2 deletions(-)

Index: hid/drivers/hid/usbhid/hid-core.c
===================================================================
--- hid.orig/drivers/hid/usbhid/hid-core.c
+++ hid/drivers/hid/usbhid/hid-core.c
@@ -47,6 +47,10 @@

  static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
  				"Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
+
+int usbhid_quirks_init(char **quirks_param);
+void usbhid_quirks_exit(void);
+
  /*
   * Module parameters.
   */
@@ -56,6 +60,22 @@ module_param_named(mousepoll, hid_mousep
  MODULE_PARM_DESC(mousepoll, "Polling interval of mice");

  /*
+ * Quirks specified at module load time
+ */
+static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
+
+/*
+ * Example of modload quirks usage: passing "quirks=0xcd5:0x1:0x4" at
+ * boot tells the USB HID driver to ignore (0x4) vendor ID 0x0cd5, product
+ * ID 0x0001.  HID quirk values are defined in include/linux/hid-quirks.h
+ */
+module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
+MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
+		 " quirks=vendorID:productID:quirks"
+		 " where vendorID, productID, and quirks are all in"
+		 " 0x-prefixed hex");
+
+/*
   * Input submission and I/O error handler.
   */

@@ -1107,6 +1127,9 @@ static struct usb_driver hid_driver = {
  static int __init hid_init(void)
  {
  	int retval;
+	retval = usbhid_quirks_init(quirks_param);
+	if (retval)
+		goto usbhid_quirks_init_fail;
  	retval = hiddev_init();
  	if (retval)
  		goto hiddev_init_fail;
@@ -1119,6 +1142,8 @@ static int __init hid_init(void)
  usb_register_fail:
  	hiddev_exit();
  hiddev_init_fail:
+	usbhid_quirks_exit();
+usbhid_quirks_init_fail:
  	return retval;
  }

@@ -1126,6 +1151,7 @@ static void __exit hid_exit(void)
  {
  	usb_deregister(&hid_driver);
  	hiddev_exit();
+	usbhid_quirks_exit();
  }

  module_init(hid_init);
Index: hid/drivers/hid/usbhid/hid-quirks.c
===================================================================
--- hid.orig/drivers/hid/usbhid/hid-quirks.c
+++ hid/drivers/hid/usbhid/hid-quirks.c
@@ -673,3 +673,50 @@ static void usbhid_remove_all_dquirks(vo
  }


+/* Module load/unload hooks for dquirks handling */
+
+/**
+ * usbhid_quirks_init: apply USB HID quirks specified at module load time
+ *
+ * Description:
+ *
+ *
+ * Returns: 0 OK, -error on failure.
+ */
+int usbhid_quirks_init(char **quirks_param)
+{
+	u16 idVendor, idProduct;
+	u32 quirks;
+	int n = 0, m;
+
+	for (; quirks_param[n] && n < MAX_USBHID_BOOT_QUIRKS; n++) {
+
+		m = sscanf(quirks_param[n], "0x%hx:0x%hx:0x%x",
+			   &idVendor, &idProduct, &quirks);
+
+		if (m != 3 ||
+		    usbhid_modify_dquirk(idVendor, idProduct, quirks) != 0) {
+			printk(KERN_WARNING
+			       "Could not parse HID quirk module param %s\n",
+			       quirks_param[n]);
+
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * usbhid_quirks_exit: release memory associated with dynamic_quirks
+ *
+ * Description:
+ *     Release all memory associated with dynamic quirks.  Called upon
+ *     module unload.
+ *
+ * Returns: nothing
+ */
+void usbhid_quirks_exit(void)
+{
+	usbhid_remove_all_dquirks();
+}
+
Index: hid/include/linux/hid-quirks.h
===================================================================
--- hid.orig/include/linux/hid-quirks.h
+++ hid/include/linux/hid-quirks.h
@@ -2,6 +2,11 @@
  #define __HID_QUIRKS_H

  /*
+ * Increase this if you need to configure more HID quirks at module load time
+ */
+#define MAX_USBHID_BOOT_QUIRKS 4
+
+/*
   * HID device quirks.
   */

@@ -33,6 +38,4 @@ u32 usbhid_lookup_quirk(const u16 idVend

  int usbhid_modify_dquirk(const u16 idVendor, const u16 idProduct, const u32 quirks);

-
-
  #endif /* __HID_QUIRKS_H */
Index: hid/Documentation/kernel-parameters.txt
===================================================================
--- hid.orig/Documentation/kernel-parameters.txt
+++ hid/Documentation/kernel-parameters.txt
@@ -1797,6 +1797,14 @@ and is between 256 and 4096 characters.
  	usbhid.mousepoll=
  			[USBHID] The interval which mice are to be polled at.

+	usbhid.quirks=
+			[USBHID] Add, modify, or remove USB HID device quirks.
+			The format is one or more strings of
+			vendorID:productID:quirks format; e.g.,
+			"usbhid.quirks=0xcd4:0x1:0x4" will add
+			a quirk 0x4 (or replace an existing quirk) for
+			vendor 0xcd4 product 0x1.
+
  	vdso=		[IA-32,SH]
  			vdso=1: enable VDSO (default)
  			vdso=0: disable VDSO mapping

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-04-11 19:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-11 19:03 [PATCH 5/6] usbhid: add 'quirks' module parameter Paul Walmsley
  -- strict thread matches above, loose matches on Subject: below --
2007-04-11 19:09 Paul Walmsley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).