0001
0002 #include <linux/usb.h>
0003 #include <linux/usb/hcd.h>
0004 #include "usb.h"
0005
0006 static int uas_is_interface(struct usb_host_interface *intf)
0007 {
0008 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
0009 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
0010 intf->desc.bInterfaceProtocol == USB_PR_UAS);
0011 }
0012
0013 static struct usb_host_interface *uas_find_uas_alt_setting(
0014 struct usb_interface *intf)
0015 {
0016 int i;
0017
0018 for (i = 0; i < intf->num_altsetting; i++) {
0019 struct usb_host_interface *alt = &intf->altsetting[i];
0020
0021 if (uas_is_interface(alt))
0022 return alt;
0023 }
0024
0025 return NULL;
0026 }
0027
0028 static int uas_find_endpoints(struct usb_host_interface *alt,
0029 struct usb_host_endpoint *eps[])
0030 {
0031 struct usb_host_endpoint *endpoint = alt->endpoint;
0032 unsigned i, n_endpoints = alt->desc.bNumEndpoints;
0033
0034 for (i = 0; i < n_endpoints; i++) {
0035 unsigned char *extra = endpoint[i].extra;
0036 int len = endpoint[i].extralen;
0037 while (len >= 3) {
0038 if (extra[1] == USB_DT_PIPE_USAGE) {
0039 unsigned pipe_id = extra[2];
0040 if (pipe_id > 0 && pipe_id < 5)
0041 eps[pipe_id - 1] = &endpoint[i];
0042 break;
0043 }
0044 len -= extra[0];
0045 extra += extra[0];
0046 }
0047 }
0048
0049 if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
0050 return -ENODEV;
0051
0052 return 0;
0053 }
0054
0055 static int uas_use_uas_driver(struct usb_interface *intf,
0056 const struct usb_device_id *id,
0057 unsigned long *flags_ret)
0058 {
0059 struct usb_host_endpoint *eps[4] = { };
0060 struct usb_device *udev = interface_to_usbdev(intf);
0061 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
0062 unsigned long flags = id->driver_info;
0063 struct usb_host_interface *alt;
0064 int r;
0065
0066 alt = uas_find_uas_alt_setting(intf);
0067 if (!alt)
0068 return 0;
0069
0070 r = uas_find_endpoints(alt, eps);
0071 if (r < 0)
0072 return 0;
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
0099 (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 ||
0100 le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) {
0101 if (udev->actconfig->desc.bMaxPower == 0) {
0102
0103 } else if (udev->speed < USB_SPEED_SUPER) {
0104
0105 flags |= US_FL_IGNORE_UAS;
0106 } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
0107
0108 flags |= US_FL_IGNORE_UAS;
0109 } else {
0110
0111 flags |= US_FL_MAX_SECTORS_240;
0112 }
0113 }
0114
0115
0116 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0bc2)
0117 flags |= US_FL_NO_ATA_1X;
0118
0119 usb_stor_adjust_quirks(udev, &flags);
0120
0121 if (flags & US_FL_IGNORE_UAS) {
0122 dev_warn(&udev->dev,
0123 "UAS is ignored for this device, using usb-storage instead\n");
0124 return 0;
0125 }
0126
0127 if (udev->bus->sg_tablesize == 0) {
0128 dev_warn(&udev->dev,
0129 "The driver for the USB controller %s does not support scatter-gather which is\n",
0130 hcd->driver->description);
0131 dev_warn(&udev->dev,
0132 "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
0133 return 0;
0134 }
0135
0136 if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
0137 dev_warn(&udev->dev,
0138 "USB controller %s does not support streams, which are required by the UAS driver.\n",
0139 hcd_to_bus(hcd)->bus_name);
0140 dev_warn(&udev->dev,
0141 "Please try an other USB controller if you wish to use UAS.\n");
0142 return 0;
0143 }
0144
0145 if (flags_ret)
0146 *flags_ret = flags;
0147
0148 return 1;
0149 }