Skip to content

Commit 8cef908

Browse files
paul10mJiri Kosina
authored andcommitted
USB HID: add support for dynamically-created quirks
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> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 8222fbe commit 8cef908

2 files changed

Lines changed: 150 additions & 14 deletions

File tree

‎drivers/hid/usbhid/hid-quirks.c‎

Lines changed: 149 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,152 @@ static const struct hid_blacklist {
447447

448448
{ 0, 0 }
449449
};
450+
451+
/* Dynamic HID quirks list - specified at runtime */
452+
struct quirks_list_struct {
453+
struct hid_blacklist hid_bl_item;
454+
struct list_head node;
455+
};
456+
457+
static LIST_HEAD(dquirks_list);
458+
static DECLARE_RWSEM(dquirks_rwsem);
459+
460+
/* Runtime ("dynamic") quirks manipulation functions */
461+
450462
/**
451-
* usbhid_exists_squirk: return any static quirks for a USB HID device
452-
* @idVendor: the 16-bit USB vendor ID, in native byteorder
453-
* @idProduct: the 16-bit USB product ID, in native byteorder
454-
*
455-
* Description:
456-
* Given a USB vendor ID and product ID, return a pointer to
457-
* the hid_blacklist entry associated with that device.
458-
*
459-
* Returns: pointer if quirk found, or NULL if no quirks found.
460-
*/
463+
* usbhid_exists_dquirk: find any dynamic quirks for a USB HID device
464+
* @idVendor: the 16-bit USB vendor ID, in native byteorder
465+
* @idProduct: the 16-bit USB product ID, in native byteorder
466+
*
467+
* Description:
468+
* Scans dquirks_list for a matching dynamic quirk and returns
469+
* the pointer to the relevant struct hid_blacklist if found.
470+
* Must be called with a read lock held on dquirks_rwsem.
471+
*
472+
* Returns: NULL if no quirk found, struct hid_blacklist * if found.
473+
*/
474+
static struct hid_blacklist *usbhid_exists_dquirk(const u16 idVendor,
475+
const u16 idProduct)
476+
{
477+
struct quirks_list_struct *q;
478+
struct hid_blacklist *bl_entry = NULL;
479+
480+
WARN_ON(idVendor == 0);
481+
482+
list_for_each_entry(q, &dquirks_list, node) {
483+
if (q->hid_bl_item.idVendor == idVendor &&
484+
q->hid_bl_item.idProduct == idProduct) {
485+
bl_entry = &q->hid_bl_item;
486+
break;
487+
}
488+
}
489+
490+
if (bl_entry != NULL)
491+
dbg("Found dynamic quirk 0x%x for USB HID vendor 0x%hx prod 0x%hx\n",
492+
bl_entry->quirks, bl_entry->idVendor,
493+
bl_entry->idProduct);
494+
495+
return bl_entry;
496+
}
497+
498+
499+
/**
500+
* usbhid_modify_dquirk: add/replace a HID quirk
501+
* @idVendor: the 16-bit USB vendor ID, in native byteorder
502+
* @idProduct: the 16-bit USB product ID, in native byteorder
503+
* @quirks: the u32 quirks value to add/replace
504+
*
505+
* Description:
506+
* If an dynamic quirk exists in memory for this (idVendor,
507+
* idProduct) pair, replace its quirks value with what was
508+
* provided. Otherwise, add the quirk to the dynamic quirks list.
509+
*
510+
* Returns: 0 OK, -error on failure.
511+
*/
512+
int usbhid_modify_dquirk(const u16 idVendor, const u16 idProduct,
513+
const u32 quirks)
514+
{
515+
struct quirks_list_struct *q_new, *q;
516+
int list_edited = 0;
517+
518+
if (!idVendor) {
519+
dbg("Cannot add a quirk with idVendor = 0");
520+
return -EINVAL;
521+
}
522+
523+
q_new = kmalloc(sizeof(struct quirks_list_struct), GFP_KERNEL);
524+
if (!q_new) {
525+
dbg("Could not allocate quirks_list_struct");
526+
return -ENOMEM;
527+
}
528+
529+
q_new->hid_bl_item.idVendor = idVendor;
530+
q_new->hid_bl_item.idProduct = idProduct;
531+
q_new->hid_bl_item.quirks = quirks;
532+
533+
down_write(&dquirks_rwsem);
534+
535+
list_for_each_entry(q, &dquirks_list, node) {
536+
537+
if (q->hid_bl_item.idVendor == idVendor &&
538+
q->hid_bl_item.idProduct == idProduct) {
539+
540+
list_replace(&q->node, &q_new->node);
541+
kfree(q);
542+
list_edited = 1;
543+
break;
544+
545+
}
546+
547+
}
548+
549+
if (!list_edited)
550+
list_add_tail(&q_new->node, &dquirks_list);
551+
552+
up_write(&dquirks_rwsem);
553+
554+
return 0;
555+
}
556+
557+
558+
/**
559+
* usbhid_remove_all_dquirks: remove all runtime HID quirks from memory
560+
*
561+
* Description:
562+
* Free all memory associated with dynamic quirks - called before
563+
* module unload.
564+
*
565+
*/
566+
static void usbhid_remove_all_dquirks(void)
567+
{
568+
struct quirks_list_struct *q, *temp;
569+
570+
down_write(&dquirks_rwsem);
571+
list_for_each_entry_safe(q, temp, &dquirks_list, node) {
572+
list_del(&q->node);
573+
kfree(q);
574+
}
575+
up_write(&dquirks_rwsem);
576+
577+
}
578+
579+
580+
/**
581+
* usbhid_exists_squirk: return any static quirks for a USB HID device
582+
* @idVendor: the 16-bit USB vendor ID, in native byteorder
583+
* @idProduct: the 16-bit USB product ID, in native byteorder
584+
*
585+
* Description:
586+
* Given a USB vendor ID and product ID, return a pointer to
587+
* the hid_blacklist entry associated with that device.
588+
*
589+
* Returns: pointer if quirk found, or NULL if no quirks found.
590+
*/
461591
static const struct hid_blacklist *usbhid_exists_squirk(const u16 idVendor,
462-
const u16 idProduct)
592+
const u16 idProduct)
463593
{
464-
const struct hid_blacklist *bl_entry = NULL;
465-
int n = 0;
594+
const struct hid_blacklist *bl_entry = NULL;
595+
int n = 0;
466596

467597
for (; hid_blacklist[n].idVendor; n++)
468598
if (hid_blacklist[n].idVendor == idVendor &&
@@ -502,9 +632,14 @@ u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct)
502632
idProduct <= USB_DEVICE_ID_CODEMERCS_IOW_LAST)
503633
return HID_QUIRK_IGNORE;
504634

505-
bl_entry = usbhid_exists_squirk(idVendor, idProduct);
635+
down_read(&dquirks_rwsem);
636+
bl_entry = usbhid_exists_dquirk(idVendor, idProduct);
637+
if (!bl_entry)
638+
bl_entry = usbhid_exists_squirk(idVendor, idProduct);
506639
if (bl_entry)
507640
quirks = bl_entry->quirks;
641+
up_read(&dquirks_rwsem);
642+
508643
return quirks;
509644
}
510645

‎include/linux/hid.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ void hid_free_device(struct hid_device *device);
496496
struct hid_device *hid_parse_report(__u8 *start, unsigned size);
497497

498498
u32 usbhid_lookup_quirk(const u16 idVendor, const u16 idProduct);
499+
int usbhid_modify_dquirk(const u16 idVendor, const u16 idProduct, const u32 quirks);
499500

500501
#ifdef CONFIG_HID_FF
501502
int hid_ff_init(struct hid_device *hid);

0 commit comments

Comments
 (0)