Skip to content

Commit 80da2e0

Browse files
pinchartlgregkh
authored andcommitted
usb: Add quirk detection based on interface information
When a whole class of devices (possibly from a specific vendor, or across multiple vendors) require a quirk, explictly listing all devices in the class make the quirks table unnecessarily large. Fix this by allowing matching devices based on interface information. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 66177cc commit 80da2e0

4 files changed

Lines changed: 106 additions & 39 deletions

File tree

‎drivers/usb/core/driver.c‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -607,22 +607,10 @@ int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
607607
}
608608

609609
/* returns 0 if no match, 1 if match */
610-
int usb_match_one_id(struct usb_interface *interface,
611-
const struct usb_device_id *id)
610+
int usb_match_one_id_intf(struct usb_device *dev,
611+
struct usb_host_interface *intf,
612+
const struct usb_device_id *id)
612613
{
613-
struct usb_host_interface *intf;
614-
struct usb_device *dev;
615-
616-
/* proc_connectinfo in devio.c may call us with id == NULL. */
617-
if (id == NULL)
618-
return 0;
619-
620-
intf = interface->cur_altsetting;
621-
dev = interface_to_usbdev(interface);
622-
623-
if (!usb_match_device(dev, id))
624-
return 0;
625-
626614
/* The interface class, subclass, protocol and number should never be
627615
* checked for a match if the device class is Vendor Specific,
628616
* unless the match record specifies the Vendor ID. */
@@ -652,6 +640,26 @@ int usb_match_one_id(struct usb_interface *interface,
652640

653641
return 1;
654642
}
643+
644+
/* returns 0 if no match, 1 if match */
645+
int usb_match_one_id(struct usb_interface *interface,
646+
const struct usb_device_id *id)
647+
{
648+
struct usb_host_interface *intf;
649+
struct usb_device *dev;
650+
651+
/* proc_connectinfo in devio.c may call us with id == NULL. */
652+
if (id == NULL)
653+
return 0;
654+
655+
intf = interface->cur_altsetting;
656+
dev = interface_to_usbdev(interface);
657+
658+
if (!usb_match_device(dev, id))
659+
return 0;
660+
661+
return usb_match_one_id_intf(dev, intf, id);
662+
}
655663
EXPORT_SYMBOL_GPL(usb_match_one_id);
656664

657665
/**

‎drivers/usb/core/hub.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,7 @@ static int usb_enumerate_device(struct usb_device *udev)
20692069
if (err < 0) {
20702070
dev_err(&udev->dev, "can't read configurations, error %d\n",
20712071
err);
2072-
goto fail;
2072+
return err;
20732073
}
20742074
}
20752075
if (udev->wusb == 1 && udev->authorized == 0) {
@@ -2085,8 +2085,12 @@ static int usb_enumerate_device(struct usb_device *udev)
20852085
udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
20862086
}
20872087
err = usb_enumerate_device_otg(udev);
2088-
fail:
2089-
return err;
2088+
if (err < 0)
2089+
return err;
2090+
2091+
usb_detect_interface_quirks(udev);
2092+
2093+
return 0;
20902094
}
20912095

20922096
static void set_usb_port_removable(struct usb_device *udev)

‎drivers/usb/core/quirks.c‎

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
#include <linux/usb/quirks.h>
1616
#include "usb.h"
1717

18-
/* List of quirky USB devices. Please keep this list ordered by:
18+
/* Lists of quirky USB devices, split in device quirks and interface quirks.
19+
* Device quirks are applied at the very beginning of the enumeration process,
20+
* right after reading the device descriptor. They can thus only match on device
21+
* information.
22+
*
23+
* Interface quirks are applied after reading all the configuration descriptors.
24+
* They can match on both device and interface information.
25+
*
26+
* Note that the DELAY_INIT and HONOR_BNUMINTERFACES quirks do not make sense as
27+
* interface quirks, as they only influence the enumeration process which is run
28+
* before processing the interface quirks.
29+
*
30+
* Please keep the lists ordered by:
1931
* 1) Vendor ID
2032
* 2) Product ID
2133
* 3) Class ID
22-
*
23-
* as we want specific devices to be overridden first, and only after that, any
24-
* class specific quirks.
25-
*
26-
* Right now the logic aborts if it finds a valid device in the table, we might
27-
* want to change that in the future if it turns out that a whole class of
28-
* devices is broken...
2934
*/
3035
static const struct usb_device_id usb_quirk_list[] = {
3136
/* CBM - Flash disk */
@@ -156,31 +161,64 @@ static const struct usb_device_id usb_quirk_list[] = {
156161
{ } /* terminating entry must be last */
157162
};
158163

159-
static const struct usb_device_id *find_id(struct usb_device *udev)
164+
static const struct usb_device_id usb_interface_quirk_list[] = {
165+
{ } /* terminating entry must be last */
166+
};
167+
168+
static bool usb_match_any_interface(struct usb_device *udev,
169+
const struct usb_device_id *id)
170+
{
171+
unsigned int i;
172+
173+
for (i = 0; i < udev->descriptor.bNumConfigurations; ++i) {
174+
struct usb_host_config *cfg = &udev->config[i];
175+
unsigned int j;
176+
177+
for (j = 0; j < cfg->desc.bNumInterfaces; ++j) {
178+
struct usb_interface_cache *cache;
179+
struct usb_host_interface *intf;
180+
181+
cache = cfg->intf_cache[j];
182+
if (cache->num_altsetting == 0)
183+
continue;
184+
185+
intf = &cache->altsetting[0];
186+
if (usb_match_one_id_intf(udev, intf, id))
187+
return true;
188+
}
189+
}
190+
191+
return false;
192+
}
193+
194+
static u32 __usb_detect_quirks(struct usb_device *udev,
195+
const struct usb_device_id *id)
160196
{
161-
const struct usb_device_id *id = usb_quirk_list;
197+
u32 quirks = 0;
162198

163-
for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
164-
id->driver_info; id++) {
165-
if (usb_match_device(udev, id))
166-
return id;
199+
for (; id->match_flags; id++) {
200+
if (!usb_match_device(udev, id))
201+
continue;
202+
203+
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_INFO) &&
204+
!usb_match_any_interface(udev, id))
205+
continue;
206+
207+
quirks |= (u32)(id->driver_info);
167208
}
168-
return NULL;
209+
210+
return quirks;
169211
}
170212

171213
/*
172214
* Detect any quirks the device has, and do any housekeeping for it if needed.
173215
*/
174216
void usb_detect_quirks(struct usb_device *udev)
175217
{
176-
const struct usb_device_id *id = usb_quirk_list;
177-
178-
id = find_id(udev);
179-
if (id)
180-
udev->quirks = (u32)(id->driver_info);
218+
udev->quirks = __usb_detect_quirks(udev, usb_quirk_list);
181219
if (udev->quirks)
182220
dev_dbg(&udev->dev, "USB quirks for this device: %x\n",
183-
udev->quirks);
221+
udev->quirks);
184222

185223
/* For the present, all devices default to USB-PERSIST enabled */
186224
#if 0 /* was: #ifdef CONFIG_PM */
@@ -197,3 +235,16 @@ void usb_detect_quirks(struct usb_device *udev)
197235
udev->persist_enabled = 1;
198236
#endif /* CONFIG_PM */
199237
}
238+
239+
void usb_detect_interface_quirks(struct usb_device *udev)
240+
{
241+
u32 quirks;
242+
243+
quirks = __usb_detect_quirks(udev, usb_interface_quirk_list);
244+
if (quirks == 0)
245+
return;
246+
247+
dev_dbg(&udev->dev, "USB interface quirks for this device: %x\n",
248+
quirks);
249+
udev->quirks |= quirks;
250+
}

‎drivers/usb/core/usb.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern void usb_disable_device(struct usb_device *dev, int skip_ep0);
2626
extern int usb_deauthorize_device(struct usb_device *);
2727
extern int usb_authorize_device(struct usb_device *);
2828
extern void usb_detect_quirks(struct usb_device *udev);
29+
extern void usb_detect_interface_quirks(struct usb_device *udev);
2930
extern int usb_remove_device(struct usb_device *udev);
3031

3132
extern int usb_get_device_descriptor(struct usb_device *dev,
@@ -37,6 +38,9 @@ extern int usb_set_configuration(struct usb_device *dev, int configuration);
3738
extern int usb_choose_configuration(struct usb_device *udev);
3839

3940
extern void usb_kick_khubd(struct usb_device *dev);
41+
extern int usb_match_one_id_intf(struct usb_device *dev,
42+
struct usb_host_interface *intf,
43+
const struct usb_device_id *id);
4044
extern int usb_match_device(struct usb_device *dev,
4145
const struct usb_device_id *id);
4246
extern void usb_forced_unbind_intf(struct usb_interface *intf);

0 commit comments

Comments
 (0)