Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 2 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/init.h> |
Stephen Rothwell | 36db045 | 2010-03-29 16:02:50 +1100 | [diff] [blame] | 6 | #include <linux/slab.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 7 | #include <linux/usb.h> |
| 8 | |
| 9 | #include "usbaudio.h" |
| 10 | #include "helper.h" |
Daniel Mack | 2b58fd5 | 2012-09-04 10:23:07 +0200 | [diff] [blame] | 11 | #include "quirks.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 12 | |
| 13 | /* |
| 14 | * combine bytes and get an integer value |
| 15 | */ |
| 16 | unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size) |
| 17 | { |
| 18 | switch (size) { |
| 19 | case 1: return *bytes; |
| 20 | case 2: return combine_word(bytes); |
| 21 | case 3: return combine_triple(bytes); |
| 22 | case 4: return combine_quad(bytes); |
| 23 | default: return 0; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /* |
| 28 | * parse descriptor buffer and return the pointer starting the given |
| 29 | * descriptor type. |
| 30 | */ |
| 31 | void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype) |
| 32 | { |
| 33 | u8 *p, *end, *next; |
| 34 | |
| 35 | p = descstart; |
| 36 | end = p + desclen; |
| 37 | for (; p < end;) { |
| 38 | if (p[0] < 2) |
| 39 | return NULL; |
| 40 | next = p + p[0]; |
| 41 | if (next > end) |
| 42 | return NULL; |
| 43 | if (p[1] == dtype && (!after || (void *)p > after)) { |
| 44 | return p; |
| 45 | } |
| 46 | p = next; |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * find a class-specified interface descriptor with the given subtype. |
| 53 | */ |
| 54 | void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype) |
| 55 | { |
| 56 | unsigned char *p = after; |
| 57 | |
| 58 | while ((p = snd_usb_find_desc(buffer, buflen, p, |
| 59 | USB_DT_CS_INTERFACE)) != NULL) { |
| 60 | if (p[0] >= 3 && p[2] == dsubtype) |
| 61 | return p; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
Takashi Iwai | 801ebf1 | 2019-06-24 15:08:28 +0200 | [diff] [blame] | 66 | /* check the validity of pipe and EP types */ |
| 67 | int snd_usb_pipe_sanity_check(struct usb_device *dev, unsigned int pipe) |
| 68 | { |
| 69 | static const int pipetypes[4] = { |
| 70 | PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT |
| 71 | }; |
| 72 | struct usb_host_endpoint *ep; |
| 73 | |
| 74 | ep = usb_pipe_endpoint(dev, pipe); |
Hillf Danton | 5d78e1c2 | 2019-07-30 17:24:36 +0800 | [diff] [blame] | 75 | if (!ep || usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)]) |
Takashi Iwai | 801ebf1 | 2019-06-24 15:08:28 +0200 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | return 0; |
| 78 | } |
| 79 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 80 | /* |
| 81 | * Wrapper for usb_control_msg(). |
| 82 | * Allocates a temp buffer to prevent dmaing from/to the stack. |
| 83 | */ |
| 84 | int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request, |
| 85 | __u8 requesttype, __u16 value, __u16 index, void *data, |
Clemens Ladisch | 17d900c | 2011-09-26 21:15:27 +0200 | [diff] [blame] | 86 | __u16 size) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 87 | { |
| 88 | int err; |
| 89 | void *buf = NULL; |
Daniel Schürmann | b5f035d | 2013-04-20 23:06:17 +0200 | [diff] [blame] | 90 | int timeout; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 91 | |
Takashi Iwai | 801ebf1 | 2019-06-24 15:08:28 +0200 | [diff] [blame] | 92 | if (snd_usb_pipe_sanity_check(dev, pipe)) |
| 93 | return -EINVAL; |
| 94 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 95 | if (size > 0) { |
| 96 | buf = kmemdup(data, size, GFP_KERNEL); |
| 97 | if (!buf) |
| 98 | return -ENOMEM; |
| 99 | } |
Daniel Schürmann | b5f035d | 2013-04-20 23:06:17 +0200 | [diff] [blame] | 100 | |
| 101 | if (requesttype & USB_DIR_IN) |
| 102 | timeout = USB_CTRL_GET_TIMEOUT; |
| 103 | else |
| 104 | timeout = USB_CTRL_SET_TIMEOUT; |
| 105 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 106 | err = usb_control_msg(dev, pipe, request, requesttype, |
Daniel Schürmann | b5f035d | 2013-04-20 23:06:17 +0200 | [diff] [blame] | 107 | value, index, buf, size, timeout); |
| 108 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 109 | if (size > 0) { |
| 110 | memcpy(data, buf, size); |
| 111 | kfree(buf); |
| 112 | } |
Daniel Mack | 2b58fd5 | 2012-09-04 10:23:07 +0200 | [diff] [blame] | 113 | |
| 114 | snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype, |
| 115 | value, index, data, size); |
| 116 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 117 | return err; |
| 118 | } |
| 119 | |
| 120 | unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip, |
| 121 | struct usb_host_interface *alts) |
| 122 | { |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 123 | switch (snd_usb_get_speed(chip->dev)) { |
| 124 | case USB_SPEED_HIGH: |
Thomas Pugliese | 6d5eba5 | 2013-10-01 14:32:57 -0500 | [diff] [blame] | 125 | case USB_SPEED_WIRELESS: |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 126 | case USB_SPEED_SUPER: |
Oliver Neukum | 748a1cc | 2016-05-04 14:18:39 +0200 | [diff] [blame] | 127 | case USB_SPEED_SUPER_PLUS: |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 128 | if (get_endpoint(alts, 0)->bInterval >= 1 && |
| 129 | get_endpoint(alts, 0)->bInterval <= 4) |
| 130 | return get_endpoint(alts, 0)->bInterval - 1; |
| 131 | break; |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 136 | } |
| 137 | |