uas-detect.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <linux/usb.h>
  2. #include <linux/usb/hcd.h>
  3. #include "usb.h"
  4. static int uas_is_interface(struct usb_host_interface *intf)
  5. {
  6. return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
  7. intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
  8. intf->desc.bInterfaceProtocol == USB_PR_UAS);
  9. }
  10. static int uas_find_uas_alt_setting(struct usb_interface *intf)
  11. {
  12. int i;
  13. for (i = 0; i < intf->num_altsetting; i++) {
  14. struct usb_host_interface *alt = &intf->altsetting[i];
  15. if (uas_is_interface(alt))
  16. return alt->desc.bAlternateSetting;
  17. }
  18. return -ENODEV;
  19. }
  20. static int uas_find_endpoints(struct usb_host_interface *alt,
  21. struct usb_host_endpoint *eps[])
  22. {
  23. struct usb_host_endpoint *endpoint = alt->endpoint;
  24. unsigned i, n_endpoints = alt->desc.bNumEndpoints;
  25. for (i = 0; i < n_endpoints; i++) {
  26. unsigned char *extra = endpoint[i].extra;
  27. int len = endpoint[i].extralen;
  28. while (len >= 3) {
  29. if (extra[1] == USB_DT_PIPE_USAGE) {
  30. unsigned pipe_id = extra[2];
  31. if (pipe_id > 0 && pipe_id < 5)
  32. eps[pipe_id - 1] = &endpoint[i];
  33. break;
  34. }
  35. len -= extra[0];
  36. extra += extra[0];
  37. }
  38. }
  39. if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
  40. return -ENODEV;
  41. return 0;
  42. }
  43. static int uas_use_uas_driver(struct usb_interface *intf,
  44. const struct usb_device_id *id,
  45. unsigned long *flags_ret)
  46. {
  47. struct usb_host_endpoint *eps[4] = { };
  48. struct usb_device *udev = interface_to_usbdev(intf);
  49. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  50. unsigned long flags = id->driver_info;
  51. int r, alt;
  52. alt = uas_find_uas_alt_setting(intf);
  53. if (alt < 0)
  54. return 0;
  55. r = uas_find_endpoints(&intf->altsetting[alt], eps);
  56. if (r < 0)
  57. return 0;
  58. /*
  59. * ASMedia has a number of usb3 to sata bridge chips, at the time of
  60. * this writing the following versions exist:
  61. * ASM1051 - no uas support version
  62. * ASM1051 - with broken (*) uas support
  63. * ASM1053 - with working uas support, but problems with large xfers
  64. * ASM1153 - with working uas support
  65. *
  66. * Devices with these chips re-use a number of device-ids over the
  67. * entire line, so the device-id is useless to determine if we're
  68. * dealing with an ASM1051 (which we want to avoid).
  69. *
  70. * The ASM1153 can be identified by config.MaxPower == 0,
  71. * where as the ASM105x models have config.MaxPower == 36.
  72. *
  73. * Differentiating between the ASM1053 and ASM1051 is trickier, when
  74. * connected over USB-3 we can look at the number of streams supported,
  75. * ASM1051 supports 32 streams, where as early ASM1053 versions support
  76. * 16 streams, newer ASM1053-s also support 32 streams, but have a
  77. * different prod-id.
  78. *
  79. * (*) ASM1051 chips do work with UAS with some disks (with the
  80. * US_FL_NO_REPORT_OPCODES quirk), but are broken with other disks
  81. */
  82. if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
  83. (le16_to_cpu(udev->descriptor.idProduct) == 0x5106 ||
  84. le16_to_cpu(udev->descriptor.idProduct) == 0x55aa)) {
  85. if (udev->actconfig->desc.bMaxPower == 0) {
  86. /* ASM1153, do nothing */
  87. } else if (udev->speed < USB_SPEED_SUPER) {
  88. /* No streams info, assume ASM1051 */
  89. flags |= US_FL_IGNORE_UAS;
  90. } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
  91. /* Possibly an ASM1051, disable uas */
  92. flags |= US_FL_IGNORE_UAS;
  93. } else {
  94. /* ASM1053, these have issues with large transfers */
  95. flags |= US_FL_MAX_SECTORS_240;
  96. }
  97. }
  98. usb_stor_adjust_quirks(udev, &flags);
  99. if (flags & US_FL_IGNORE_UAS) {
  100. dev_warn(&udev->dev,
  101. "UAS is blacklisted for this device, using usb-storage instead\n");
  102. return 0;
  103. }
  104. if (udev->bus->sg_tablesize == 0) {
  105. dev_warn(&udev->dev,
  106. "The driver for the USB controller %s does not support scatter-gather which is\n",
  107. hcd->driver->description);
  108. dev_warn(&udev->dev,
  109. "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
  110. return 0;
  111. }
  112. if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
  113. dev_warn(&udev->dev,
  114. "USB controller %s does not support streams, which are required by the UAS driver.\n",
  115. hcd_to_bus(hcd)->bus_name);
  116. dev_warn(&udev->dev,
  117. "Please try an other USB controller if you wish to use UAS.\n");
  118. return 0;
  119. }
  120. if (flags_ret)
  121. *flags_ret = flags;
  122. return 1;
  123. }