ALSA: usb-audio: refactor code
[safe/jmp/linux-2.6] / sound / usb / helper.c
1 /*
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>
19 #include <linux/usb.h>
20
21 #include "usbaudio.h"
22 #include "helper.h"
23
24 /*
25  * combine bytes and get an integer value
26  */
27 unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
28 {
29         switch (size) {
30         case 1:  return *bytes;
31         case 2:  return combine_word(bytes);
32         case 3:  return combine_triple(bytes);
33         case 4:  return combine_quad(bytes);
34         default: return 0;
35         }
36 }
37
38 /*
39  * parse descriptor buffer and return the pointer starting the given
40  * descriptor type.
41  */
42 void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
43 {
44         u8 *p, *end, *next;
45
46         p = descstart;
47         end = p + desclen;
48         for (; p < end;) {
49                 if (p[0] < 2)
50                         return NULL;
51                 next = p + p[0];
52                 if (next > end)
53                         return NULL;
54                 if (p[1] == dtype && (!after || (void *)p > after)) {
55                         return p;
56                 }
57                 p = next;
58         }
59         return NULL;
60 }
61
62 /*
63  * find a class-specified interface descriptor with the given subtype.
64  */
65 void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
66 {
67         unsigned char *p = after;
68
69         while ((p = snd_usb_find_desc(buffer, buflen, p,
70                                       USB_DT_CS_INTERFACE)) != NULL) {
71                 if (p[0] >= 3 && p[2] == dsubtype)
72                         return p;
73         }
74         return NULL;
75 }
76
77 /*
78  * Wrapper for usb_control_msg().
79  * Allocates a temp buffer to prevent dmaing from/to the stack.
80  */
81 int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
82                     __u8 requesttype, __u16 value, __u16 index, void *data,
83                     __u16 size, int timeout)
84 {
85         int err;
86         void *buf = NULL;
87
88         if (size > 0) {
89                 buf = kmemdup(data, size, GFP_KERNEL);
90                 if (!buf)
91                         return -ENOMEM;
92         }
93         err = usb_control_msg(dev, pipe, request, requesttype,
94                               value, index, buf, size, timeout);
95         if (size > 0) {
96                 memcpy(data, buf, size);
97                 kfree(buf);
98         }
99         return err;
100 }
101
102 unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
103                                          struct usb_host_interface *alts)
104 {
105         if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
106             get_endpoint(alts, 0)->bInterval >= 1 &&
107             get_endpoint(alts, 0)->bInterval <= 4)
108                 return get_endpoint(alts, 0)->bInterval - 1;
109         else
110                 return 0;
111 }
112