uas-detect.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 struct usb_host_interface *uas_find_uas_alt_setting(
  11. struct usb_interface *intf)
  12. {
  13. int i;
  14. for (i = 0; i < intf->num_altsetting; i++) {
  15. struct usb_host_interface *alt = &intf->altsetting[i];
  16. if (uas_is_interface(alt))
  17. return alt;
  18. }
  19. return NULL;
  20. }
  21. static int uas_find_endpoints(struct usb_host_interface *alt,
  22. struct usb_host_endpoint *eps[])
  23. {
  24. struct usb_host_endpoint *endpoint = alt->endpoint;
  25. unsigned i, n_endpoints = alt->desc.bNumEndpoints;
  26. for (i = 0; i < n_endpoints; i++) {
  27. unsigned char *extra = endpoint[i].extra;
  28. int len = endpoint[i].extralen;
  29. while (len >= 3) {
  30. if (extra[1] == USB_DT_PIPE_USAGE) {
  31. unsigned pipe_id = extra[2];
  32. if (pipe_id > 0 && pipe_id < 5)
  33. eps[pipe_id - 1] = &endpoint[i];
  34. break;
  35. }
  36. len -= extra[0];
  37. extra += extra[0];
  38. }
  39. }
  40. if (!eps[0] || !eps[1] || !eps[2] || !eps[3])
  41. return -ENODEV;
  42. return 0;
  43. }
  44. static int uas_use_uas_driver(struct usb_interface *intf,
  45. const struct usb_device_id *id,
  46. unsigned long *flags_ret)
  47. {
  48. struct usb_host_endpoint *eps[4] = { };
  49. struct usb_device *udev = interface_to_usbdev(intf);
  50. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  51. unsigned long flags = id->driver_info;
  52. struct usb_host_interface *alt;
  53. int r;
  54. alt = uas_find_uas_alt_setting(intf);
  55. if (!alt)
  56. return 0;
  57. r = uas_find_endpoints(alt, eps);
  58. if (r < 0)
  59. return 0;
  60. /*
  61. * ASM1051 and older ASM1053 devices have the same usb-id, and UAS is
  62. * broken on the ASM1051, use the number of streams to differentiate.
  63. * New ASM1053-s also support 32 streams, but have a different prod-id.
  64. */
  65. if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
  66. le16_to_cpu(udev->descriptor.idProduct) == 0x55aa) {
  67. if (udev->speed < USB_SPEED_SUPER) {
  68. /* No streams info, assume ASM1051 */
  69. flags |= US_FL_IGNORE_UAS;
  70. } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
  71. flags |= US_FL_IGNORE_UAS;
  72. } else {
  73. /* ASM1053, these have issues with large transfers */
  74. flags |= US_FL_MAX_SECTORS_240;
  75. }
  76. }
  77. usb_stor_adjust_quirks(udev, &flags);
  78. if (flags & US_FL_IGNORE_UAS) {
  79. dev_warn(&udev->dev,
  80. "UAS is blacklisted for this device, using usb-storage instead\n");
  81. return 0;
  82. }
  83. if (udev->bus->sg_tablesize == 0) {
  84. dev_warn(&udev->dev,
  85. "The driver for the USB controller %s does not support scatter-gather which is\n",
  86. hcd->driver->description);
  87. dev_warn(&udev->dev,
  88. "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
  89. return 0;
  90. }
  91. if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
  92. dev_warn(&udev->dev,
  93. "USB controller %s does not support streams, which are required by the UAS driver.\n",
  94. hcd_to_bus(hcd)->bus_name);
  95. dev_warn(&udev->dev,
  96. "Please try an other USB controller if you wish to use UAS.\n");
  97. return 0;
  98. }
  99. if (flags_ret)
  100. *flags_ret = flags;
  101. return 1;
  102. }