blob: 84aa265dd802c7690a92d563498b05f3103c05fc [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Macke5779992010-03-04 19:46:13 +01002/*
Daniel Macke5779992010-03-04 19:46:13 +01003 */
4
5#include <linux/init.h>
Stephen Rothwell36db0452010-03-29 16:02:50 +11006#include <linux/slab.h>
Daniel Macke5779992010-03-04 19:46:13 +01007#include <linux/usb.h>
8
9#include "usbaudio.h"
10#include "helper.h"
Daniel Mack2b58fd52012-09-04 10:23:07 +020011#include "quirks.h"
Daniel Macke5779992010-03-04 19:46:13 +010012
13/*
14 * combine bytes and get an integer value
15 */
16unsigned 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 */
31void *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 */
54void *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
66/*
67 * Wrapper for usb_control_msg().
68 * Allocates a temp buffer to prevent dmaing from/to the stack.
69 */
70int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
71 __u8 requesttype, __u16 value, __u16 index, void *data,
Clemens Ladisch17d900c2011-09-26 21:15:27 +020072 __u16 size)
Daniel Macke5779992010-03-04 19:46:13 +010073{
74 int err;
75 void *buf = NULL;
Daniel Schürmannb5f035d2013-04-20 23:06:17 +020076 int timeout;
Daniel Macke5779992010-03-04 19:46:13 +010077
78 if (size > 0) {
79 buf = kmemdup(data, size, GFP_KERNEL);
80 if (!buf)
81 return -ENOMEM;
82 }
Daniel Schürmannb5f035d2013-04-20 23:06:17 +020083
84 if (requesttype & USB_DIR_IN)
85 timeout = USB_CTRL_GET_TIMEOUT;
86 else
87 timeout = USB_CTRL_SET_TIMEOUT;
88
Daniel Macke5779992010-03-04 19:46:13 +010089 err = usb_control_msg(dev, pipe, request, requesttype,
Daniel Schürmannb5f035d2013-04-20 23:06:17 +020090 value, index, buf, size, timeout);
91
Daniel Macke5779992010-03-04 19:46:13 +010092 if (size > 0) {
93 memcpy(data, buf, size);
94 kfree(buf);
95 }
Daniel Mack2b58fd52012-09-04 10:23:07 +020096
97 snd_usb_ctl_msg_quirk(dev, pipe, request, requesttype,
98 value, index, data, size);
99
Daniel Macke5779992010-03-04 19:46:13 +0100100 return err;
101}
102
103unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
104 struct usb_host_interface *alts)
105{
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700106 switch (snd_usb_get_speed(chip->dev)) {
107 case USB_SPEED_HIGH:
Thomas Pugliese6d5eba52013-10-01 14:32:57 -0500108 case USB_SPEED_WIRELESS:
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700109 case USB_SPEED_SUPER:
Oliver Neukum748a1cc2016-05-04 14:18:39 +0200110 case USB_SPEED_SUPER_PLUS:
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700111 if (get_endpoint(alts, 0)->bInterval >= 1 &&
112 get_endpoint(alts, 0)->bInterval <= 4)
113 return get_endpoint(alts, 0)->bInterval - 1;
114 break;
115 default:
116 break;
117 }
118 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100119}
120