uas-detect.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. {
  46. struct usb_host_endpoint *eps[4] = { };
  47. struct usb_device *udev = interface_to_usbdev(intf);
  48. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  49. unsigned long flags = id->driver_info;
  50. int r, alt;
  51. alt = uas_find_uas_alt_setting(intf);
  52. if (alt < 0)
  53. return 0;
  54. r = uas_find_endpoints(&intf->altsetting[alt], eps);
  55. if (r < 0)
  56. return 0;
  57. /*
  58. * ASM1051 and older ASM1053 devices have the same usb-id, and UAS is
  59. * broken on the ASM1051, use the number of streams to differentiate.
  60. * New ASM1053-s also support 32 streams, but have a different prod-id.
  61. */
  62. if (le16_to_cpu(udev->descriptor.idVendor) == 0x174c &&
  63. le16_to_cpu(udev->descriptor.idProduct) == 0x55aa) {
  64. if (udev->speed < USB_SPEED_SUPER) {
  65. /* No streams info, assume ASM1051 */
  66. flags |= US_FL_IGNORE_UAS;
  67. } else if (usb_ss_max_streams(&eps[1]->ss_ep_comp) == 32) {
  68. flags |= US_FL_IGNORE_UAS;
  69. }
  70. }
  71. usb_stor_adjust_quirks(udev, &flags);
  72. if (flags & US_FL_IGNORE_UAS) {
  73. dev_warn(&udev->dev,
  74. "UAS is blacklisted for this device, using usb-storage instead\n");
  75. return 0;
  76. }
  77. if (udev->bus->sg_tablesize == 0) {
  78. dev_warn(&udev->dev,
  79. "The driver for the USB controller %s does not support scatter-gather which is\n",
  80. hcd->driver->description);
  81. dev_warn(&udev->dev,
  82. "required by the UAS driver. Please try an other USB controller if you wish to use UAS.\n");
  83. return 0;
  84. }
  85. if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) {
  86. dev_warn(&udev->dev,
  87. "USB controller %s does not support streams, which are required by the UAS driver.\n",
  88. hcd_to_bus(hcd)->bus_name);
  89. dev_warn(&udev->dev,
  90. "Please try an other USB controller if you wish to use UAS.\n");
  91. return 0;
  92. }
  93. return 1;
  94. }