Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 1 | #include <linux/usb.h> |
| 2 | #include <linux/usb/hcd.h> |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 3 | #include "usb.h" |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 4 | |
| 5 | static 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 Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 12 | static int uas_find_uas_alt_setting(struct usb_interface *intf) |
| 13 | { |
| 14 | int i; |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 15 | |
| 16 | for (i = 0; i < intf->num_altsetting; i++) { |
| 17 | struct usb_host_interface *alt = &intf->altsetting[i]; |
| 18 | |
Hans de Goede | cc4deaf | 2014-07-25 22:01:26 +0200 | [diff] [blame] | 19 | if (uas_is_interface(alt)) |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 20 | return alt->desc.bAlternateSetting; |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | return -ENODEV; |
| 24 | } |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 25 | |
Hans de Goede | d77adc02 | 2013-10-29 10:03:34 +0100 | [diff] [blame] | 26 | static 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 Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 53 | static int uas_use_uas_driver(struct usb_interface *intf, |
| 54 | const struct usb_device_id *id) |
| 55 | { |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 56 | struct usb_host_endpoint *eps[4] = { }; |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 57 | struct usb_device *udev = interface_to_usbdev(intf); |
Oliver Neukum | 14aec58 | 2014-02-11 20:36:04 +0100 | [diff] [blame] | 58 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 59 | unsigned long flags = id->driver_info; |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 60 | int r, alt; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 61 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 62 | |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 63 | 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 Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 71 | /* |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 72 | * ASMedia has a number of usb3 to sata bridge chips, at the time of |
| 73 | * this writing the following versions exist: |
| 74 | * ASM1051 - no uas support version |
| 75 | * ASM1051 - with broken (*) uas support |
| 76 | * ASM1053 - with working uas support |
| 77 | * ASM1153 - with working uas support |
| 78 | * |
| 79 | * Devices with these chips re-use a number of device-ids over the |
| 80 | * entire line, so the device-id is useless to determine if we're |
| 81 | * dealing with an ASM1051 (which we want to avoid). |
| 82 | * |
| 83 | * The ASM1153 can be identified by config.MaxPower == 0, |
| 84 | * where as the ASM105x models have config.MaxPower == 36. |
| 85 | * |
| 86 | * Differentiating between the ASM1053 and ASM1051 is trickier, when |
| 87 | * connected over USB-3 we can look at the number of streams supported, |
| 88 | * ASM1051 supports 32 streams, where as early ASM1053 versions support |
| 89 | * 16 streams, newer ASM1053-s also support 32 streams, but have a |
| 90 | * different prod-id. |
| 91 | * |
| 92 | * (*) ASM1051 chips do work with UAS with some disks (with the |
| 93 | * US_FL_NO_REPORT_OPCODES quirk), but are broken with other disks |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 94 | */ |
Hans de Goede | a79e5bc | 2014-09-11 11:06:12 +0200 | [diff] [blame] | 95 | if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c && |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 96 | (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 || |
| 97 | le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) { |
| 98 | if (udev->actconfig->desc.bMaxPower == 0) { |
| 99 | /* ASM1153, do nothing */ |
| 100 | } else if (udev->speed < USB_SPEED_SUPER) { |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 101 | /* No streams info, assume ASM1051 */ |
| 102 | flags |= US_FL_IGNORE_UAS; |
| 103 | } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) { |
Hans de Goede | 078fd7d | 2015-01-08 15:15:14 +0100 | [diff] [blame] | 104 | /* Possibly an ASM1051, disable uas */ |
Hans de Goede | a9c54ca | 2014-09-10 10:51:36 +0200 | [diff] [blame] | 105 | flags |= US_FL_IGNORE_UAS; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | usb_stor_adjust_quirks(udev, &flags); |
| 110 | |
| 111 | if (flags & US_FL_IGNORE_UAS) { |
| 112 | dev_warn(&udev->dev, |
| 113 | "UAS is blacklisted for this device, using usb-storage instead\n"); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Hans de Goede | cc4deaf | 2014-07-25 22:01:26 +0200 | [diff] [blame] | 117 | if (udev->bus->sg_tablesize == 0) { |
| 118 | dev_warn(&udev->dev, |
| 119 | "The driver for the USB controller %s does not support scatter-gather which is\n", |
| 120 | hcd->driver->description); |
| 121 | dev_warn(&udev->dev, |
| 122 | "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n"); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Hans de Goede | 43508be | 2014-07-25 22:01:27 +0200 | [diff] [blame] | 126 | if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) { |
| 127 | dev_warn(&udev->dev, |
| 128 | "USB controller %s does not support streams, which are required by the UAS driver.\n", |
| 129 | hcd_to_bus(hcd)->bus_name); |
| 130 | dev_warn(&udev->dev, |
| 131 | "Please try an other USB controller if you wish to use UAS.\n"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 135 | return 1; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 136 | } |