5 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the USB template for an USB Audio Device.
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usb_cdc.h>
56 #include <dev/usb/template/usb_template.h>
68 0x09, 0x04, /* American English */
70 #define STRING_AUDIO_PRODUCT \
71 'A', 0, 'u', 0, 'd', 0, 'i', 0, 'o', 0, ' ', 0, \
72 'T', 0, 'e', 0, 's', 0, 't', 0, ' ', 0, \
73 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, ' ', 0,
75 #define STRING_AUDIO_MIXER \
76 'M', 0, 'i', 0, 'x', 0, 'e', 0, 'r', 0, ' ', 0, \
77 'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0,
79 #define STRING_AUDIO_RECORD \
80 'R', 0, 'e', 0, 'c', 0, 'o', 0, 'r', 0, 'd', 0, ' ', 0, \
81 'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0,
83 #define STRING_AUDIO_PLAYBACK \
84 'P', 0, 'l', 0, 'a', 0, 'y', 0, 'b', 0, 'a', 0, 'c', 0, 'k', 0, ' ', 0, \
85 'i', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0,
88 /* make the real string descriptors */
90 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
91 USB_MAKE_STRING_DESC(STRING_AUDIO_MIXER, string_audio_mixer);
92 USB_MAKE_STRING_DESC(STRING_AUDIO_RECORD, string_audio_record);
93 USB_MAKE_STRING_DESC(STRING_AUDIO_PLAYBACK, string_audio_playback);
94 USB_MAKE_STRING_DESC(STRING_AUDIO_PRODUCT, string_audio_product);
99 * Audio Mixer description structures
101 * Some of the audio descriptors were dumped
102 * from a Creative Labs USB audio device.
105 static const uint8_t audio_raw_desc_0[] = {
106 0x0a, 0x24, 0x01, 0x00, 0x01, 0xa9, 0x00, 0x02,
110 static const uint8_t audio_raw_desc_1[] = {
111 0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
112 0x03, 0x00, 0x00, 0x00
115 static const uint8_t audio_raw_desc_2[] = {
116 0x0c, 0x24, 0x02, 0x02, 0x01, 0x02, 0x00, 0x02,
117 0x03, 0x00, 0x00, 0x00
120 static const uint8_t audio_raw_desc_3[] = {
121 0x0c, 0x24, 0x02, 0x03, 0x03, 0x06, 0x00, 0x02,
122 0x03, 0x00, 0x00, 0x00
125 static const uint8_t audio_raw_desc_4[] = {
126 0x0c, 0x24, 0x02, 0x04, 0x05, 0x06, 0x00, 0x02,
127 0x03, 0x00, 0x00, 0x00
130 static const uint8_t audio_raw_desc_5[] = {
131 0x09, 0x24, 0x03, 0x05, 0x05, 0x06, 0x00, 0x01,
135 static const uint8_t audio_raw_desc_6[] = {
136 0x09, 0x24, 0x03, 0x06, 0x01, 0x03, 0x00, 0x09,
140 static const uint8_t audio_raw_desc_7[] = {
141 0x09, 0x24, 0x03, 0x07, 0x01, 0x01, 0x00, 0x08,
145 static const uint8_t audio_raw_desc_8[] = {
146 0x09, 0x24, 0x05, 0x08, 0x03, 0x0a, 0x0b, 0x0c,
150 static const uint8_t audio_raw_desc_9[] = {
151 0x0a, 0x24, 0x06, 0x09, 0x0f, 0x01, 0x01, 0x02,
155 static const uint8_t audio_raw_desc_10[] = {
156 0x0a, 0x24, 0x06, 0x0a, 0x02, 0x01, 0x43, 0x00,
160 static const uint8_t audio_raw_desc_11[] = {
161 0x0a, 0x24, 0x06, 0x0b, 0x03, 0x01, 0x01, 0x02,
165 static const uint8_t audio_raw_desc_12[] = {
166 0x0a, 0x24, 0x06, 0x0c, 0x04, 0x01, 0x01, 0x00,
170 static const uint8_t audio_raw_desc_13[] = {
171 0x0a, 0x24, 0x06, 0x0d, 0x02, 0x01, 0x03, 0x00,
175 static const uint8_t audio_raw_desc_14[] = {
176 0x0a, 0x24, 0x06, 0x0e, 0x03, 0x01, 0x01, 0x02,
180 static const uint8_t audio_raw_desc_15[] = {
181 0x0f, 0x24, 0x04, 0x0f, 0x03, 0x01, 0x0d, 0x0e,
182 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
185 static const void *audio_raw_iface_0_desc[] = {
205 static const struct usb_temp_interface_desc audio_iface_0 = {
206 .ppEndpoints = NULL, /* no endpoints */
207 .ppRawDesc = audio_raw_iface_0_desc,
208 .bInterfaceClass = 1,
209 .bInterfaceSubClass = 1,
210 .bInterfaceProtocol = 0,
211 .iInterface = INDEX_AUDIO_MIXER,
214 static const uint8_t audio_raw_desc_20[] = {
215 0x07, 0x24, 0x01, 0x01, 0x03, 0x01, 0x00
219 static const uint8_t audio_raw_desc_21[] = {
220 0x0b, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x01,
225 static const uint8_t audio_raw_desc_22[] = {
226 0x07, 0x25, 0x01, 0x00, 0x01, 0x04, 0x00
229 static const void *audio_raw_iface_1_desc[] = {
235 static const void *audio_raw_ep_1_desc[] = {
240 static const struct usb_temp_packet_size audio_isoc_mps = {
241 .mps[USB_SPEED_FULL] = 0xC8,
242 .mps[USB_SPEED_HIGH] = 0xC8,
245 static const struct usb_temp_interval audio_isoc_interval = {
246 .bInterval[USB_SPEED_FULL] = 1, /* 1:1 */
247 .bInterval[USB_SPEED_HIGH] = 4, /* 1:8 */
250 static const struct usb_temp_endpoint_desc audio_isoc_out_ep = {
251 .ppRawDesc = audio_raw_ep_1_desc,
252 .pPacketSize = &audio_isoc_mps,
253 .pIntervals = &audio_isoc_interval,
254 .bEndpointAddress = UE_DIR_OUT,
255 .bmAttributes = UE_ISOCHRONOUS | UE_ISO_ADAPT,
258 static const struct usb_temp_endpoint_desc *audio_iface_1_ep[] = {
263 static const struct usb_temp_interface_desc audio_iface_1_alt_0 = {
264 .ppEndpoints = NULL, /* no endpoints */
265 .ppRawDesc = NULL, /* no raw descriptors */
266 .bInterfaceClass = 1,
267 .bInterfaceSubClass = 2,
268 .bInterfaceProtocol = 0,
269 .iInterface = INDEX_AUDIO_PLAYBACK,
272 static const struct usb_temp_interface_desc audio_iface_1_alt_1 = {
273 .ppEndpoints = audio_iface_1_ep,
274 .ppRawDesc = audio_raw_iface_1_desc,
275 .bInterfaceClass = 1,
276 .bInterfaceSubClass = 2,
277 .bInterfaceProtocol = 0,
278 .iInterface = INDEX_AUDIO_PLAYBACK,
279 .isAltInterface = 1, /* this is an alternate setting */
282 static const uint8_t audio_raw_desc_30[] = {
283 0x07, 0x24, 0x01, 0x07, 0x01, 0x01, 0x00
287 static const uint8_t audio_raw_desc_31[] = {
288 0x0b, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x01,
293 static const uint8_t audio_raw_desc_32[] = {
294 0x07, 0x25, 0x01, 0x01, 0x00, 0x00, 0x00
297 static const void *audio_raw_iface_2_desc[] = {
303 static const void *audio_raw_ep_2_desc[] = {
308 static const struct usb_temp_endpoint_desc audio_isoc_in_ep = {
309 .ppRawDesc = audio_raw_ep_2_desc,
310 .pPacketSize = &audio_isoc_mps,
311 .pIntervals = &audio_isoc_interval,
312 .bEndpointAddress = UE_DIR_IN,
313 .bmAttributes = UE_ISOCHRONOUS | UE_ISO_ADAPT,
316 static const struct usb_temp_endpoint_desc *audio_iface_2_ep[] = {
321 static const struct usb_temp_interface_desc audio_iface_2_alt_0 = {
322 .ppEndpoints = NULL, /* no endpoints */
323 .ppRawDesc = NULL, /* no raw descriptors */
324 .bInterfaceClass = 1,
325 .bInterfaceSubClass = 2,
326 .bInterfaceProtocol = 0,
327 .iInterface = INDEX_AUDIO_RECORD,
330 static const struct usb_temp_interface_desc audio_iface_2_alt_1 = {
331 .ppEndpoints = audio_iface_2_ep,
332 .ppRawDesc = audio_raw_iface_2_desc,
333 .bInterfaceClass = 1,
334 .bInterfaceSubClass = 2,
335 .bInterfaceProtocol = 0,
336 .iInterface = INDEX_AUDIO_RECORD,
337 .isAltInterface = 1, /* this is an alternate setting */
340 static const struct usb_temp_interface_desc *audio_interfaces[] = {
342 &audio_iface_1_alt_0,
343 &audio_iface_1_alt_1,
344 &audio_iface_2_alt_0,
345 &audio_iface_2_alt_1,
349 static const struct usb_temp_config_desc audio_config_desc = {
350 .ppIfaceDesc = audio_interfaces,
351 .bmAttributes = UC_BUS_POWERED,
352 .bMaxPower = 25, /* 50 mA */
353 .iConfiguration = INDEX_AUDIO_PRODUCT,
356 static const struct usb_temp_config_desc *audio_configs[] = {
361 static usb_temp_get_string_desc_t audio_get_string_desc;
363 const struct usb_temp_device_desc usb_template_audio = {
364 .getStringDesc = &audio_get_string_desc,
365 .ppConfigDesc = audio_configs,
366 .idVendor = USB_TEMPLATE_VENDOR,
369 .bDeviceClass = UDCLASS_COMM,
370 .bDeviceSubClass = 0,
371 .bDeviceProtocol = 0,
373 .iProduct = INDEX_AUDIO_PRODUCT,
377 /*------------------------------------------------------------------------*
378 * audio_get_string_desc
381 * NULL: Failure. No such string.
382 * Else: Success. Pointer to string descriptor is returned.
383 *------------------------------------------------------------------------*/
385 audio_get_string_desc(uint16_t lang_id, uint8_t string_index)
387 static const void *ptr[INDEX_AUDIO_MAX] = {
388 [INDEX_AUDIO_LANG] = &string_lang,
389 [INDEX_AUDIO_MIXER] = &string_audio_mixer,
390 [INDEX_AUDIO_RECORD] = &string_audio_record,
391 [INDEX_AUDIO_PLAYBACK] = &string_audio_playback,
392 [INDEX_AUDIO_PRODUCT] = &string_audio_product,
395 if (string_index == 0) {
396 return (&string_lang);
398 if (lang_id != 0x0409) {
401 if (string_index < INDEX_AUDIO_MAX) {
402 return (ptr[string_index]);