1
2#include <linux/usb.h>
3#include <linux/usb/hcd.h>
4#include "usb.h"
5
6static int uas_is_interface(struct usb_host_interface *intf)
7{
8 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
9 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
10 intf->desc.bInterfaceProtocol == USB_PR_UAS);
11}
12
13static struct usb_host_interface *uas_find_uas_alt_setting(
14 struct usb_interface *intf)
15{
16 int i;
17
18 for (i = 0; i < intf->num_altsetting; i++) {
19 struct usb_host_interface *alt = &intf->altsetting[i];
20
21 if (uas_is_interface(alt))
22 return alt;
23 }
24
25 return NULL;
26}
27
28static int uas_find_endpoints(struct usb_host_interface *alt,
29 struct usb_host_endpoint *eps[])
30{
31 struct usb_host_endpoint *endpoint = alt->endpoint;
32 unsigned i, n_endpoints = alt->desc.bNumEndpoints;
33
34 for (i = 0; i < n_endpoints; i++) {
35 unsigned char *extra = endpoint[i].extra;
36 int len = endpoint[i].extralen;
37 while (len >= 3) {
38 if (extra[1] == USB_DT_PIPE_USAGE) {
39 unsigned pipe_id = extra[2];
40 if (pipe_id > 0 && pipe_id < 5)
41 eps[pipe_id - 1] = &endpoint[i];
42 break;
43 }
44 len -= extra[0];
45 extra += extra[0];
46 }
47 }
48
49 if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
50 return -ENODEV;
51
52 return 0;
53}
54
55static int uas_use_uas_driver(struct usb_interface *intf,
56 const struct usb_device_id *id,
57 unsigned long *flags_ret)
58{
59 struct usb_host_endpoint *eps[4] = { };
60 struct usb_device *udev = interface_to_usbdev(intf);
61 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
62 unsigned long flags = id->driver_info;
63 struct usb_host_interface *alt;
64 int r;
65
66 alt = uas_find_uas_alt_setting(intf);
67 if (!alt)
68 return 0;
69
70 r = uas_find_endpoints(alt, eps);
71 if (r < 0)
72 return 0;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
99 (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 ||
100 le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) {
101 if (udev->actconfig->desc.bMaxPower == 0) {
102
103 } else if (udev->speed < USB_SPEED_SUPER) {
104
105 flags |= US_FL_IGNORE_UAS;
106 } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
107
108 flags |= US_FL_IGNORE_UAS;
109 } else {
110
111 flags |= US_FL_MAX_SECTORS_240;
112 }
113 }
114
115 usb_stor_adjust_quirks(udev, &flags);
116
117 if (flags & US_FL_IGNORE_UAS) {
118 dev_warn(&udev->dev,
119 "UAS is blacklisted for this device, using usb-storage instead\n");
120 return 0;
121 }
122
123 if (udev->bus->sg_tablesize == 0) {
124 dev_warn(&udev->dev,
125 "The driver for the USB controller %s does not support scatter-gather which is\n",
126 hcd->driver->description);
127 dev_warn(&udev->dev,
128 "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
129 return 0;
130 }
131
132 if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
133 dev_warn(&udev->dev,
134 "USB controller %s does not support streams, which are required by the UAS driver.\n",
135 hcd_to_bus(hcd)->bus_name);
136 dev_warn(&udev->dev,
137 "Please try an other USB controller if you wish to use UAS.\n");
138 return 0;
139 }
140
141 if (flags_ret)
142 *flags_ret = flags;
143
144 return 1;
145}
146