blob: 1e298ec4f4d0c22f4b281c8ec088165b192fe05e [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
Hans de Goede82aa0382013-10-21 08:53:31 +010012static int uas_find_uas_alt_setting(struct usb_interface *intf)
13{
14 int i;
Hans de Goede82aa0382013-10-21 08:53:31 +010015
16 for (i = 0; i < intf->num_altsetting; i++) {
17 struct usb_host_interface *alt = &intf->altsetting[i];
18
Hans de Goedecc4deaf2014-07-25 22:01:26 +020019 if (uas_is_interface(alt))
Hans de Goede82aa0382013-10-21 08:53:31 +010020 return alt->desc.bAlternateSetting;
Hans de Goede82aa0382013-10-21 08:53:31 +010021 }
22
23 return -ENODEV;
24}
Hans de Goede79b4c062013-10-25 17:04:33 +010025
Hans de Goeded77adc022013-10-29 10:03:34 +010026static int uas_find_endpoints(struct usb_host_interface *alt,
27 struct usb_host_endpoint *eps[])
28{
29 struct usb_host_endpoint *endpoint = alt->endpoint;
30 unsigned i, n_endpoints = alt->desc.bNumEndpoints;
31
32 for (i = 0; i < n_endpoints; i++) {
33 unsigned char *extra = endpoint[i].extra;
34 int len = endpoint[i].extralen;
35 while (len >= 3) {
36 if (extra[1] == USB_DT_PIPE_USAGE) {
37 unsigned pipe_id = extra[2];
38 if (pipe_id > 0 && pipe_id < 5)
39 eps[pipe_id - 1] = &endpoint[i];
40 break;
41 }
42 len -= extra[0];
43 extra += extra[0];
44 }
45 }
46
47 if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
48 return -ENODEV;
49
50 return 0;
51}
52
Hans de Goede79b4c062013-10-25 17:04:33 +010053static int uas_use_uas_driver(struct usb_interface *intf,
54 const struct usb_device_id *id)
55{
Hans de Goede61340412013-11-16 11:37:41 +010056 struct usb_host_endpoint *eps[4] = { };
Hans de Goede97172a62013-11-16 12:19:36 +010057 struct usb_device *udev = interface_to_usbdev(intf);
Oliver Neukum14aec582014-02-11 20:36:04 +010058 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Hans de Goede79b4c062013-10-25 17:04:33 +010059 unsigned long flags = id->driver_info;
Hans de Goede61340412013-11-16 11:37:41 +010060 int r, alt;
Hans de Goede79b4c062013-10-25 17:04:33 +010061
Hans de Goede79b4c062013-10-25 17:04:33 +010062
Hans de Goede61340412013-11-16 11:37:41 +010063 alt = uas_find_uas_alt_setting(intf);
64 if (alt < 0)
65 return 0;
66
67 r = uas_find_endpoints(&intf->altsetting[alt], eps);
68 if (r < 0)
69 return 0;
70
Hans de Goedea9c54ca2014-09-10 10:51:36 +020071 /*
72 * ASM1051 and older ASM1053 devices have the same usb-id, and UAS is
73 * broken on the ASM1051, use the number of streams to differentiate.
74 * New ASM1053-s also support 32 streams, but have a different prod-id.
75 */
76 if (udev->descriptor.idVendor == 0x174c &&
77 udev->descriptor.idProduct == 0x55aa) {
78 if (udev->speed < USB_SPEED_SUPER) {
79 /* No streams info, assume ASM1051 */
80 flags |= US_FL_IGNORE_UAS;
81 } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
82 flags |= US_FL_IGNORE_UAS;
83 }
84 }
85
86 usb_stor_adjust_quirks(udev, &flags);
87
88 if (flags & US_FL_IGNORE_UAS) {
89 dev_warn(&udev->dev,
90 "UAS is blacklisted for this device, using usb-storage instead\n");
91 return 0;
92 }
93
Hans de Goedecc4deaf2014-07-25 22:01:26 +020094 if (udev->bus->sg_tablesize == 0) {
95 dev_warn(&udev->dev,
96 "The driver for the USB controller %s does not support scatter-gather which is\n",
97 hcd->driver->description);
98 dev_warn(&udev->dev,
99 "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
100 return 0;
101 }
102
Hans de Goede43508be2014-07-25 22:01:27 +0200103 if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
104 dev_warn(&udev->dev,
105 "USB controller %s does not support streams, which are required by the UAS driver.\n",
106 hcd_to_bus(hcd)->bus_name);
107 dev_warn(&udev->dev,
108 "Please try an other USB controller if you wish to use UAS.\n");
109 return 0;
110 }
111
Hans de Goede61340412013-11-16 11:37:41 +0100112 return 1;
Hans de Goede79b4c062013-10-25 17:04:33 +0100113}