blob: b1cc9499c57e1c59b88212a81c99f1a1e6905200 [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17
18#include <linux/init.h>
Stephen Rothwell36db0452010-03-29 16:02:50 +110019#include <linux/slab.h>
Daniel Macke5779992010-03-04 19:46:13 +010020#include <linux/usb.h>
21
22#include "usbaudio.h"
23#include "helper.h"
Daniel Mack2b58fd52012-09-04 10:23:07 +020024#include "quirks.h"
Daniel Macke5779992010-03-04 19:46:13 +010025
26/*
27 * combine bytes and get an integer value
28 */
29unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
30{
31 switch (size) {
32 case 1: return *bytes;
33 case 2: return combine_word(bytes);
34 case 3: return combine_triple(bytes);
35 case 4: return combine_quad(bytes);
36 default: return 0;
37 }
38}
39
40/*
41 * parse descriptor buffer and return the pointer starting the given
42 * descriptor type.
43 */
44void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
45{
46 u8 *p, *end, *next;
47
48 p = descstart;
49 end = p + desclen;
50 for (; p < end;) {
51 if (p[0] < 2)
52 return NULL;
53 next = p + p[0];
54 if (next > end)
55 return NULL;
56 if (p[1] == dtype && (!after || (void *)p > after)) {
57 return p;
58 }
59 p = next;
60 }
61 return NULL;
62}
63
64/*
65 * find a class-specified interface descriptor with the given subtype.
66 */
67void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
68{
69 unsigned char *p = after;
70
71 while ((p = snd_usb_find_desc(buffer, buflen, p,
72 USB_DT_CS_INTERFACE)) != NULL) {
73 if (p[0] >= 3 && p[2] == dsubtype)
74 return p;
75 }
76 return NULL;
77}
78
Takashi Iwai801ebf12019-06-24 15:08:28 +020079/* check the validity of pipe and EP types */
80int snd_usb_pipe_sanity_check(struct usb_device *dev, unsigned int pipe)
81{
82 static const int pipetypes[4] = {
83 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
84 };
85 struct usb_host_endpoint *ep;
86
87 ep = usb_pipe_endpoint(dev, pipe);
88 if (usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)])
89 return -EINVAL;
90 return 0;
91}
92
Daniel Macke5779992010-03-04 19:46:13 +010093/*
94 * Wrapper for usb_control_msg().
95 * Allocates a temp buffer to prevent dmaing from/to the stack.
96 */
97int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
98 __u8 requesttype, __u16 value, __u16 index, void *data,
Clemens Ladisch17d900c2011-09-26 21:15:27 +020099 __u16 size)
Daniel Macke5779992010-03-04 19:46:13 +0100100{
101 int err;
102 void *buf = NULL;
Daniel Schürmannb5f035d2013-04-20 23:06:17 +0200103 int timeout;
Daniel Macke5779992010-03-04 19:46:13 +0100104
Takashi Iwai801ebf12019-06-24 15:08:28 +0200105 if (snd_usb_pipe_sanity_check(dev, pipe))
106 return -EINVAL;
107
Daniel Macke5779992010-03-04 19:46:13 +0100108 if (size > 0) {
109 buf = kmemdup(data, size, GFP_KERNEL);
110 if (!buf)
111 return -ENOMEM;
112 }
Daniel Schürmannb5f035d2013-04-20 23:06:17 +0200113
114 if (requesttype & USB_DIR_IN)
115 timeout = USB_CTRL_GET_TIMEOUT;
116 else
117 timeout = USB_CTRL_SET_TIMEOUT;
118
Daniel Macke5779992010-03-04 19:46:13 +0100119 err = usb_control_msg(dev, pipe, request, requesttype,
Daniel Schürmannb5f035d2013-04-20 23:06:17 +0200120 value, index, buf, size, timeout);
121
Daniel Macke5779992010-03-04 19:46:13 +0100122 if (size > 0) {
123 memcpy(data, buf, size);
124 kfree(buf);
125 }
Daniel Mack2b58fd52012-09-04 10:23:07 +0200126
127 snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
128 value, index, data, size);
129
Daniel Macke5779992010-03-04 19:46:13 +0100130 return err;
131}
132
133unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
134 struct usb_host_interface *alts)
135{
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700136 switch (snd_usb_get_speed(chip->dev)) {
137 case USB_SPEED_HIGH:
Thomas Pugliese6d5eba52013-10-01 14:32:57 -0500138 case USB_SPEED_WIRELESS:
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700139 case USB_SPEED_SUPER:
Oliver Neukum748a1cc2016-05-04 14:18:39 +0200140 case USB_SPEED_SUPER_PLUS:
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700141 if (get_endpoint(alts, 0)->bInterval >= 1 &&
142 get_endpoint(alts, 0)->bInterval <= 4)
143 return get_endpoint(alts, 0)->bInterval - 1;
144 break;
145 default:
146 break;
147 }
148 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100149}
150