blob: bb05b984d5f67e2098ab035649bc18870bbf721f [file] [log] [blame]
Hans de Goede82aa0382013-10-21 08:53:31 +01001#include <linux/usb.h>
2#include <linux/usb/hcd.h>
Hans de Goede97172a62013-11-16 12:19:36 +01003#include "usb.h"
Hans de Goede82aa0382013-10-21 08:53:31 +01004
5static int uas_is_interface(struct usb_host_interface *intf)
6{
7 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
8 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
9 intf->desc.bInterfaceProtocol == USB_PR_UAS);
10}
11
12static int uas_isnt_supported(struct usb_device *udev)
13{
14 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
15
16 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
17 "support scatter-gather which is\n",
18 hcd->driver->description);
19 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
20 "alternative USB controller if you wish to use UAS.\n");
21 return -ENODEV;
22}
23
24static int uas_find_uas_alt_setting(struct usb_interface *intf)
25{
26 int i;
27 struct usb_device *udev = interface_to_usbdev(intf);
28 int sg_supported = udev->bus->sg_tablesize != 0;
29
30 for (i = 0; i < intf->num_altsetting; i++) {
31 struct usb_host_interface *alt = &intf->altsetting[i];
32
33 if (uas_is_interface(alt)) {
34 if (!sg_supported)
35 return uas_isnt_supported(udev);
36 return alt->desc.bAlternateSetting;
37 }
38 }
39
40 return -ENODEV;
41}
Hans de Goede79b4c062013-10-25 17:04:33 +010042
Hans de Goeded77adc022013-10-29 10:03:34 +010043static int uas_find_endpoints(struct usb_host_interface *alt,
44 struct usb_host_endpoint *eps[])
45{
46 struct usb_host_endpoint *endpoint = alt->endpoint;
47 unsigned i, n_endpoints = alt->desc.bNumEndpoints;
48
49 for (i = 0; i < n_endpoints; i++) {
50 unsigned char *extra = endpoint[i].extra;
51 int len = endpoint[i].extralen;
52 while (len >= 3) {
53 if (extra[1] == USB_DT_PIPE_USAGE) {
54 unsigned pipe_id = extra[2];
55 if (pipe_id > 0 && pipe_id < 5)
56 eps[pipe_id - 1] = &endpoint[i];
57 break;
58 }
59 len -= extra[0];
60 extra += extra[0];
61 }
62 }
63
64 if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
65 return -ENODEV;
66
67 return 0;
68}
69
Hans de Goede79b4c062013-10-25 17:04:33 +010070static int uas_use_uas_driver(struct usb_interface *intf,
71 const struct usb_device_id *id)
72{
Hans de Goede61340412013-11-16 11:37:41 +010073 struct usb_host_endpoint *eps[4] = { };
Hans de Goede97172a62013-11-16 12:19:36 +010074 struct usb_device *udev = interface_to_usbdev(intf);
Oliver Neukum14aec582014-02-11 20:36:04 +010075 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Hans de Goede79b4c062013-10-25 17:04:33 +010076 unsigned long flags = id->driver_info;
Hans de Goede61340412013-11-16 11:37:41 +010077 int r, alt;
Hans de Goede79b4c062013-10-25 17:04:33 +010078
Hans de Goede97172a62013-11-16 12:19:36 +010079 usb_stor_adjust_quirks(udev, &flags);
80
Hans de Goede79b4c062013-10-25 17:04:33 +010081 if (flags & US_FL_IGNORE_UAS)
82 return 0;
83
Oliver Neukum14aec582014-02-11 20:36:04 +010084 if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams)
85 return 0;
86
Hans de Goede61340412013-11-16 11:37:41 +010087 alt = uas_find_uas_alt_setting(intf);
88 if (alt < 0)
89 return 0;
90
91 r = uas_find_endpoints(&intf->altsetting[alt], eps);
92 if (r < 0)
93 return 0;
94
95 return 1;
Hans de Goede79b4c062013-10-25 17:04:33 +010096}