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 | |
| 12 | static int uas_isnt_supported(struct usb_device *udev) |
| 13 | { |
| 14 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 15 | |
| 16 | dev_warn(&udev->dev, "The driver for the USB controller %s does not " |
| 17 | "support scatter-gather which is\n", |
| 18 | hcd->driver->description); |
| 19 | dev_warn(&udev->dev, "required by the UAS driver. Please try an" |
| 20 | "alternative USB controller if you wish to use UAS.\n"); |
| 21 | return -ENODEV; |
| 22 | } |
| 23 | |
| 24 | static int uas_find_uas_alt_setting(struct usb_interface *intf) |
| 25 | { |
| 26 | int i; |
| 27 | struct usb_device *udev = interface_to_usbdev(intf); |
| 28 | int sg_supported = udev->bus->sg_tablesize != 0; |
| 29 | |
| 30 | for (i = 0; i < intf->num_altsetting; i++) { |
| 31 | struct usb_host_interface *alt = &intf->altsetting[i]; |
| 32 | |
| 33 | if (uas_is_interface(alt)) { |
| 34 | if (!sg_supported) |
| 35 | return uas_isnt_supported(udev); |
| 36 | return alt->desc.bAlternateSetting; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return -ENODEV; |
| 41 | } |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 42 | |
Hans de Goede | d77adc02 | 2013-10-29 10:03:34 +0100 | [diff] [blame] | 43 | static int uas_find_endpoints(struct usb_host_interface *alt, |
| 44 | struct usb_host_endpoint *eps[]) |
| 45 | { |
| 46 | struct usb_host_endpoint *endpoint = alt->endpoint; |
| 47 | unsigned i, n_endpoints = alt->desc.bNumEndpoints; |
| 48 | |
| 49 | for (i = 0; i < n_endpoints; i++) { |
| 50 | unsigned char *extra = endpoint[i].extra; |
| 51 | int len = endpoint[i].extralen; |
| 52 | while (len >= 3) { |
| 53 | if (extra[1] == USB_DT_PIPE_USAGE) { |
| 54 | unsigned pipe_id = extra[2]; |
| 55 | if (pipe_id > 0 && pipe_id < 5) |
| 56 | eps[pipe_id - 1] = &endpoint[i]; |
| 57 | break; |
| 58 | } |
| 59 | len -= extra[0]; |
| 60 | extra += extra[0]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!eps[0] || !eps[1] || !eps[2] || !eps[3]) |
| 65 | return -ENODEV; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 70 | static int uas_use_uas_driver(struct usb_interface *intf, |
| 71 | const struct usb_device_id *id) |
| 72 | { |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 73 | struct usb_host_endpoint *eps[4] = { }; |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 74 | struct usb_device *udev = interface_to_usbdev(intf); |
Oliver Neukum | 14aec58 | 2014-02-11 20:36:04 +0100 | [diff] [blame] | 75 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 76 | unsigned long flags = id->driver_info; |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 77 | int r, alt; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 78 | |
Hans de Goede | 97172a6 | 2013-11-16 12:19:36 +0100 | [diff] [blame] | 79 | usb_stor_adjust_quirks(udev, &flags); |
| 80 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 81 | if (flags & US_FL_IGNORE_UAS) |
| 82 | return 0; |
| 83 | |
Oliver Neukum | 14aec58 | 2014-02-11 20:36:04 +0100 | [diff] [blame] | 84 | if (udev->speed >= USB_SPEED_SUPER && !hcd->can_do_streams) |
| 85 | return 0; |
| 86 | |
Hans de Goede | 6134041 | 2013-11-16 11:37:41 +0100 | [diff] [blame] | 87 | alt = uas_find_uas_alt_setting(intf); |
| 88 | if (alt < 0) |
| 89 | return 0; |
| 90 | |
| 91 | r = uas_find_endpoints(&intf->altsetting[alt], eps); |
| 92 | if (r < 0) |
| 93 | return 0; |
| 94 | |
| 95 | return 1; |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 96 | } |