aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb')
-rw-r--r--sound/usb/Kconfig32
-rw-r--r--sound/usb/Makefile12
-rw-r--r--sound/usb/usbaudio.c3337
-rw-r--r--sound/usb/usbaudio.h251
-rw-r--r--sound/usb/usbmidi.c1564
-rw-r--r--sound/usb/usbmixer.c1545
-rw-r--r--sound/usb/usbmixer_maps.c135
-rw-r--r--sound/usb/usbquirks.h1202
-rw-r--r--sound/usb/usx2y/Makefile3
-rw-r--r--sound/usb/usx2y/usX2Yhwdep.c280
-rw-r--r--sound/usb/usx2y/usX2Yhwdep.h6
-rw-r--r--sound/usb/usx2y/usbus428ctldefs.h108
-rw-r--r--sound/usb/usx2y/usbusx2y.c461
-rw-r--r--sound/usb/usx2y/usbusx2y.h84
-rw-r--r--sound/usb/usx2y/usbusx2yaudio.c1026
-rw-r--r--sound/usb/usx2y/usx2y.h51
-rw-r--r--sound/usb/usx2y/usx2yhwdeppcm.c807
-rw-r--r--sound/usb/usx2y/usx2yhwdeppcm.h21
18 files changed, 10925 insertions, 0 deletions
diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig
new file mode 100644
index 000000000000..9329e992c841
--- /dev/null
+++ b/sound/usb/Kconfig
@@ -0,0 +1,32 @@
1# ALSA USB drivers
2
3menu "USB devices"
4 depends on SND!=n && USB!=n
5
6config SND_USB_AUDIO
7 tristate "USB Audio/MIDI driver"
8 depends on SND && USB
9 select SND_RAWMIDI
10 select SND_PCM
11 help
12 Say Y here to include support for USB audio and USB MIDI
13 devices.
14
15 To compile this driver as a module, choose M here: the module
16 will be called snd-usb-audio.
17
18config SND_USB_USX2Y
19 tristate "Tascam US-122, US-224 and US-428 USB driver"
20 depends on SND && USB && (X86 || PPC || ALPHA)
21 select SND_HWDEP
22 select SND_RAWMIDI
23 select SND_PCM
24 help
25 Say Y here to include support for Tascam USB Audio/MIDI
26 interfaces or controllers US-122, US-224 and US-428.
27
28 To compile this driver as a module, choose M here: the module
29 will be called snd-usb-usx2y.
30
31endmenu
32
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
new file mode 100644
index 000000000000..2c1dc11a72e2
--- /dev/null
+++ b/sound/usb/Makefile
@@ -0,0 +1,12 @@
1#
2# Makefile for ALSA
3#
4
5snd-usb-audio-objs := usbaudio.o usbmixer.o
6snd-usb-lib-objs := usbmidi.o
7
8# Toplevel Module Dependency
9obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usb-lib.o
10obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-lib.o
11
12obj-$(CONFIG_SND) += usx2y/
diff --git a/sound/usb/usbaudio.c b/sound/usb/usbaudio.c
new file mode 100644
index 000000000000..84b0bbddbd22
--- /dev/null
+++ b/sound/usb/usbaudio.c
@@ -0,0 +1,3337 @@
1/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Main and PCM part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 *
28 * NOTES:
29 *
30 * - async unlink should be used for avoiding the sleep inside lock.
31 * 2.4.22 usb-uhci seems buggy for async unlinking and results in
32 * oops. in such a cse, pass async_unlink=0 option.
33 * - the linked URBs would be preferred but not used so far because of
34 * the instability of unlinking.
35 * - type II is not supported properly. there is no device which supports
36 * this type *correctly*. SB extigy looks as if it supports, but it's
37 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
38 */
39
40
41#include <sound/driver.h>
42#include <linux/bitops.h>
43#include <linux/init.h>
44#include <linux/list.h>
45#include <linux/slab.h>
46#include <linux/string.h>
47#include <linux/usb.h>
48#include <linux/moduleparam.h>
49#include <sound/core.h>
50#include <sound/info.h>
51#include <sound/pcm.h>
52#include <sound/pcm_params.h>
53#include <sound/initval.h>
54
55#include "usbaudio.h"
56
57
58MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
59MODULE_DESCRIPTION("USB Audio");
60MODULE_LICENSE("GPL");
61MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
62
63
64static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
66static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
67static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
68static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
69static int nrpacks = 4; /* max. number of packets per urb */
70static int async_unlink = 1;
71
72module_param_array(index, int, NULL, 0444);
73MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
74module_param_array(id, charp, NULL, 0444);
75MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
76module_param_array(enable, bool, NULL, 0444);
77MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
78module_param_array(vid, int, NULL, 0444);
79MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
80module_param_array(pid, int, NULL, 0444);
81MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
82module_param(nrpacks, int, 0444);
83MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
84module_param(async_unlink, bool, 0444);
85MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
86
87
88/*
89 * debug the h/w constraints
90 */
91/* #define HW_CONST_DEBUG */
92
93
94/*
95 *
96 */
97
98#define MAX_PACKS 10
99#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
100#define MAX_URBS 5 /* max. 20ms long packets */
101#define SYNC_URBS 2 /* always two urbs for sync */
102#define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
103
104typedef struct snd_usb_substream snd_usb_substream_t;
105typedef struct snd_usb_stream snd_usb_stream_t;
106typedef struct snd_urb_ctx snd_urb_ctx_t;
107
108struct audioformat {
109 struct list_head list;
110 snd_pcm_format_t format; /* format type */
111 unsigned int channels; /* # channels */
112 unsigned int fmt_type; /* USB audio format type (1-3) */
113 unsigned int frame_size; /* samples per frame for non-audio */
114 int iface; /* interface number */
115 unsigned char altsetting; /* corresponding alternate setting */
116 unsigned char altset_idx; /* array index of altenate setting */
117 unsigned char attributes; /* corresponding attributes of cs endpoint */
118 unsigned char endpoint; /* endpoint */
119 unsigned char ep_attr; /* endpoint attributes */
120 unsigned int maxpacksize; /* max. packet size */
121 unsigned int rates; /* rate bitmasks */
122 unsigned int rate_min, rate_max; /* min/max rates */
123 unsigned int nr_rates; /* number of rate table entries */
124 unsigned int *rate_table; /* rate table */
125};
126
127struct snd_urb_ctx {
128 struct urb *urb;
129 snd_usb_substream_t *subs;
130 int index; /* index for urb array */
131 int packets; /* number of packets per urb */
132 int transfer; /* transferred size */
133 char *buf; /* buffer for capture */
134};
135
136struct snd_urb_ops {
137 int (*prepare)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
138 int (*retire)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
139 int (*prepare_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
140 int (*retire_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
141};
142
143struct snd_usb_substream {
144 snd_usb_stream_t *stream;
145 struct usb_device *dev;
146 snd_pcm_substream_t *pcm_substream;
147 int direction; /* playback or capture */
148 int interface; /* current interface */
149 int endpoint; /* assigned endpoint */
150 struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
151 unsigned int cur_rate; /* current rate (for hw_params callback) */
152 unsigned int period_bytes; /* current period bytes (for hw_params callback) */
153 unsigned int format; /* USB data format */
154 unsigned int datapipe; /* the data i/o pipe */
155 unsigned int syncpipe; /* 1 - async out or adaptive in */
156 unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
157 unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
158 unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
159 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
160 unsigned int phase; /* phase accumulator */
161 unsigned int maxpacksize; /* max packet size in bytes */
162 unsigned int maxframesize; /* max packet size in frames */
163 unsigned int curpacksize; /* current packet size in bytes (for capture) */
164 unsigned int curframesize; /* current packet size in frames (for capture) */
165 unsigned int fill_max: 1; /* fill max packet size always */
166 unsigned int fmt_type; /* USB audio format type (1-3) */
167
168 unsigned int running: 1; /* running status */
169
170 unsigned int hwptr; /* free frame position in the buffer (only for playback) */
171 unsigned int hwptr_done; /* processed frame position in the buffer */
172 unsigned int transfer_sched; /* scheduled frames since last period (for playback) */
173 unsigned int transfer_done; /* processed frames since last period update */
174 unsigned long active_mask; /* bitmask of active urbs */
175 unsigned long unlink_mask; /* bitmask of unlinked urbs */
176
177 unsigned int nurbs; /* # urbs */
178 snd_urb_ctx_t dataurb[MAX_URBS]; /* data urb table */
179 snd_urb_ctx_t syncurb[SYNC_URBS]; /* sync urb table */
180 char syncbuf[SYNC_URBS * MAX_PACKS * 4]; /* sync buffer; it's so small - let's get static */
181 char *tmpbuf; /* temporary buffer for playback */
182
183 u64 formats; /* format bitmasks (all or'ed) */
184 unsigned int num_formats; /* number of supported audio formats (list) */
185 struct list_head fmt_list; /* format list */
186 spinlock_t lock;
187
188 struct snd_urb_ops ops; /* callbacks (must be filled at init) */
189};
190
191
192struct snd_usb_stream {
193 snd_usb_audio_t *chip;
194 snd_pcm_t *pcm;
195 int pcm_index;
196 unsigned int fmt_type; /* USB audio format type (1-3) */
197 snd_usb_substream_t substream[2];
198 struct list_head list;
199};
200
201
202/*
203 * we keep the snd_usb_audio_t instances by ourselves for merging
204 * the all interfaces on the same card as one sound device.
205 */
206
207static DECLARE_MUTEX(register_mutex);
208static snd_usb_audio_t *usb_chip[SNDRV_CARDS];
209
210
211/*
212 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
213 * this will overflow at approx 524 kHz
214 */
215inline static unsigned get_usb_full_speed_rate(unsigned int rate)
216{
217 return ((rate << 13) + 62) / 125;
218}
219
220/*
221 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
222 * this will overflow at approx 4 MHz
223 */
224inline static unsigned get_usb_high_speed_rate(unsigned int rate)
225{
226 return ((rate << 10) + 62) / 125;
227}
228
229/* convert our full speed USB rate into sampling rate in Hz */
230inline static unsigned get_full_speed_hz(unsigned int usb_rate)
231{
232 return (usb_rate * 125 + (1 << 12)) >> 13;
233}
234
235/* convert our high speed USB rate into sampling rate in Hz */
236inline static unsigned get_high_speed_hz(unsigned int usb_rate)
237{
238 return (usb_rate * 125 + (1 << 9)) >> 10;
239}
240
241
242/*
243 * prepare urb for full speed capture sync pipe
244 *
245 * fill the length and offset of each urb descriptor.
246 * the fixed 10.14 frequency is passed through the pipe.
247 */
248static int prepare_capture_sync_urb(snd_usb_substream_t *subs,
249 snd_pcm_runtime_t *runtime,
250 struct urb *urb)
251{
252 unsigned char *cp = urb->transfer_buffer;
253 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
254 int i, offs;
255
256 urb->number_of_packets = ctx->packets;
257 urb->dev = ctx->subs->dev; /* we need to set this at each time */
258 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
259 urb->iso_frame_desc[i].length = 3;
260 urb->iso_frame_desc[i].offset = offs;
261 cp[0] = subs->freqn >> 2;
262 cp[1] = subs->freqn >> 10;
263 cp[2] = subs->freqn >> 18;
264 }
265 return 0;
266}
267
268/*
269 * prepare urb for high speed capture sync pipe
270 *
271 * fill the length and offset of each urb descriptor.
272 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
273 */
274static int prepare_capture_sync_urb_hs(snd_usb_substream_t *subs,
275 snd_pcm_runtime_t *runtime,
276 struct urb *urb)
277{
278 unsigned char *cp = urb->transfer_buffer;
279 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
280 int i, offs;
281
282 urb->number_of_packets = ctx->packets;
283 urb->dev = ctx->subs->dev; /* we need to set this at each time */
284 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4, cp += 4) {
285 urb->iso_frame_desc[i].length = 4;
286 urb->iso_frame_desc[i].offset = offs;
287 cp[0] = subs->freqn;
288 cp[1] = subs->freqn >> 8;
289 cp[2] = subs->freqn >> 16;
290 cp[3] = subs->freqn >> 24;
291 }
292 return 0;
293}
294
295/*
296 * process after capture sync complete
297 * - nothing to do
298 */
299static int retire_capture_sync_urb(snd_usb_substream_t *subs,
300 snd_pcm_runtime_t *runtime,
301 struct urb *urb)
302{
303 return 0;
304}
305
306/*
307 * prepare urb for capture data pipe
308 *
309 * fill the offset and length of each descriptor.
310 *
311 * we use a temporary buffer to write the captured data.
312 * since the length of written data is determined by host, we cannot
313 * write onto the pcm buffer directly... the data is thus copied
314 * later at complete callback to the global buffer.
315 */
316static int prepare_capture_urb(snd_usb_substream_t *subs,
317 snd_pcm_runtime_t *runtime,
318 struct urb *urb)
319{
320 int i, offs;
321 unsigned long flags;
322 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
323
324 offs = 0;
325 urb->dev = ctx->subs->dev; /* we need to set this at each time */
326 urb->number_of_packets = 0;
327 spin_lock_irqsave(&subs->lock, flags);
328 for (i = 0; i < ctx->packets; i++) {
329 urb->iso_frame_desc[i].offset = offs;
330 urb->iso_frame_desc[i].length = subs->curpacksize;
331 offs += subs->curpacksize;
332 urb->number_of_packets++;
333 subs->transfer_sched += subs->curframesize;
334 if (subs->transfer_sched >= runtime->period_size) {
335 subs->transfer_sched -= runtime->period_size;
336 break;
337 }
338 }
339 spin_unlock_irqrestore(&subs->lock, flags);
340 urb->transfer_buffer = ctx->buf;
341 urb->transfer_buffer_length = offs;
342#if 0 // for check
343 if (! urb->bandwidth) {
344 int bustime;
345 bustime = usb_check_bandwidth(urb->dev, urb);
346 if (bustime < 0)
347 return bustime;
348 printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
349 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
350 }
351#endif // for check
352 return 0;
353}
354
355/*
356 * process after capture complete
357 *
358 * copy the data from each desctiptor to the pcm buffer, and
359 * update the current position.
360 */
361static int retire_capture_urb(snd_usb_substream_t *subs,
362 snd_pcm_runtime_t *runtime,
363 struct urb *urb)
364{
365 unsigned long flags;
366 unsigned char *cp;
367 int i;
368 unsigned int stride, len, oldptr;
369
370 stride = runtime->frame_bits >> 3;
371
372 for (i = 0; i < urb->number_of_packets; i++) {
373 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
374 if (urb->iso_frame_desc[i].status) {
375 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
376 // continue;
377 }
378 len = urb->iso_frame_desc[i].actual_length / stride;
379 if (! len)
380 continue;
381 /* update the current pointer */
382 spin_lock_irqsave(&subs->lock, flags);
383 oldptr = subs->hwptr_done;
384 subs->hwptr_done += len;
385 if (subs->hwptr_done >= runtime->buffer_size)
386 subs->hwptr_done -= runtime->buffer_size;
387 subs->transfer_done += len;
388 spin_unlock_irqrestore(&subs->lock, flags);
389 /* copy a data chunk */
390 if (oldptr + len > runtime->buffer_size) {
391 unsigned int cnt = runtime->buffer_size - oldptr;
392 unsigned int blen = cnt * stride;
393 memcpy(runtime->dma_area + oldptr * stride, cp, blen);
394 memcpy(runtime->dma_area, cp + blen, len * stride - blen);
395 } else {
396 memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
397 }
398 /* update the pointer, call callback if necessary */
399 spin_lock_irqsave(&subs->lock, flags);
400 if (subs->transfer_done >= runtime->period_size) {
401 subs->transfer_done -= runtime->period_size;
402 spin_unlock_irqrestore(&subs->lock, flags);
403 snd_pcm_period_elapsed(subs->pcm_substream);
404 } else
405 spin_unlock_irqrestore(&subs->lock, flags);
406 }
407 return 0;
408}
409
410
411/*
412 * prepare urb for full speed playback sync pipe
413 *
414 * set up the offset and length to receive the current frequency.
415 */
416
417static int prepare_playback_sync_urb(snd_usb_substream_t *subs,
418 snd_pcm_runtime_t *runtime,
419 struct urb *urb)
420{
421 int i, offs;
422 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
423
424 urb->number_of_packets = ctx->packets;
425 urb->dev = ctx->subs->dev; /* we need to set this at each time */
426 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
427 urb->iso_frame_desc[i].length = 3;
428 urb->iso_frame_desc[i].offset = offs;
429 }
430 return 0;
431}
432
433/*
434 * prepare urb for high speed playback sync pipe
435 *
436 * set up the offset and length to receive the current frequency.
437 */
438
439static int prepare_playback_sync_urb_hs(snd_usb_substream_t *subs,
440 snd_pcm_runtime_t *runtime,
441 struct urb *urb)
442{
443 int i, offs;
444 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
445
446 urb->number_of_packets = ctx->packets;
447 urb->dev = ctx->subs->dev; /* we need to set this at each time */
448 for (i = offs = 0; i < urb->number_of_packets; i++, offs += 4) {
449 urb->iso_frame_desc[i].length = 4;
450 urb->iso_frame_desc[i].offset = offs;
451 }
452 return 0;
453}
454
455/*
456 * process after full speed playback sync complete
457 *
458 * retrieve the current 10.14 frequency from pipe, and set it.
459 * the value is referred in prepare_playback_urb().
460 */
461static int retire_playback_sync_urb(snd_usb_substream_t *subs,
462 snd_pcm_runtime_t *runtime,
463 struct urb *urb)
464{
465 int i;
466 unsigned int f, found;
467 unsigned char *cp = urb->transfer_buffer;
468 unsigned long flags;
469
470 found = 0;
471 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
472 if (urb->iso_frame_desc[i].status ||
473 urb->iso_frame_desc[i].actual_length < 3)
474 continue;
475 f = combine_triple(cp) << 2;
476#if 0
477 if (f < subs->freqn - (subs->freqn>>3) || f > subs->freqmax) {
478 snd_printd(KERN_WARNING "requested frequency %d (%u,%03uHz) out of range (current nominal %d (%u,%03uHz))\n",
479 f, f >> 14, (f & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1),
480 subs->freqn, subs->freqn >> 14, (subs->freqn & ((1 << 14) - 1) * 1000) / ((1 << 14) - 1));
481 continue;
482 }
483#endif
484 found = f;
485 }
486 if (found) {
487 spin_lock_irqsave(&subs->lock, flags);
488 subs->freqm = found;
489 spin_unlock_irqrestore(&subs->lock, flags);
490 }
491
492 return 0;
493}
494
495/*
496 * process after high speed playback sync complete
497 *
498 * retrieve the current 12.13 frequency from pipe, and set it.
499 * the value is referred in prepare_playback_urb().
500 */
501static int retire_playback_sync_urb_hs(snd_usb_substream_t *subs,
502 snd_pcm_runtime_t *runtime,
503 struct urb *urb)
504{
505 int i;
506 unsigned int found;
507 unsigned char *cp = urb->transfer_buffer;
508 unsigned long flags;
509
510 found = 0;
511 for (i = 0; i < urb->number_of_packets; i++, cp += 4) {
512 if (urb->iso_frame_desc[i].status ||
513 urb->iso_frame_desc[i].actual_length < 4)
514 continue;
515 found = combine_quad(cp) & 0x0fffffff;
516 }
517 if (found) {
518 spin_lock_irqsave(&subs->lock, flags);
519 subs->freqm = found;
520 spin_unlock_irqrestore(&subs->lock, flags);
521 }
522
523 return 0;
524}
525
526/*
527 * prepare urb for playback data pipe
528 *
529 * we copy the data directly from the pcm buffer.
530 * the current position to be copied is held in hwptr field.
531 * since a urb can handle only a single linear buffer, if the total
532 * transferred area overflows the buffer boundary, we cannot send
533 * it directly from the buffer. thus the data is once copied to
534 * a temporary buffer and urb points to that.
535 */
536static int prepare_playback_urb(snd_usb_substream_t *subs,
537 snd_pcm_runtime_t *runtime,
538 struct urb *urb)
539{
540 int i, stride, offs;
541 unsigned int counts;
542 unsigned long flags;
543 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
544
545 stride = runtime->frame_bits >> 3;
546
547 offs = 0;
548 urb->dev = ctx->subs->dev; /* we need to set this at each time */
549 urb->number_of_packets = 0;
550 spin_lock_irqsave(&subs->lock, flags);
551 for (i = 0; i < ctx->packets; i++) {
552 /* calculate the size of a packet */
553 if (subs->fill_max)
554 counts = subs->maxframesize; /* fixed */
555 else {
556 subs->phase = (subs->phase & 0xffff) + subs->freqm;
557 counts = subs->phase >> 16;
558 if (counts > subs->maxframesize)
559 counts = subs->maxframesize;
560 }
561 /* set up descriptor */
562 urb->iso_frame_desc[i].offset = offs * stride;
563 urb->iso_frame_desc[i].length = counts * stride;
564 offs += counts;
565 urb->number_of_packets++;
566 subs->transfer_sched += counts;
567 if (subs->transfer_sched >= runtime->period_size) {
568 subs->transfer_sched -= runtime->period_size;
569 if (subs->fmt_type == USB_FORMAT_TYPE_II) {
570 if (subs->transfer_sched > 0) {
571 /* FIXME: fill-max mode is not supported yet */
572 offs -= subs->transfer_sched;
573 counts -= subs->transfer_sched;
574 urb->iso_frame_desc[i].length = counts * stride;
575 subs->transfer_sched = 0;
576 }
577 i++;
578 if (i < ctx->packets) {
579 /* add a transfer delimiter */
580 urb->iso_frame_desc[i].offset = offs * stride;
581 urb->iso_frame_desc[i].length = 0;
582 urb->number_of_packets++;
583 }
584 }
585 break;
586 }
587 }
588 if (subs->hwptr + offs > runtime->buffer_size) {
589 /* err, the transferred area goes over buffer boundary.
590 * copy the data to the temp buffer.
591 */
592 int len;
593 len = runtime->buffer_size - subs->hwptr;
594 urb->transfer_buffer = subs->tmpbuf;
595 memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * stride, len * stride);
596 memcpy(subs->tmpbuf + len * stride, runtime->dma_area, (offs - len) * stride);
597 subs->hwptr += offs;
598 subs->hwptr -= runtime->buffer_size;
599 } else {
600 /* set the buffer pointer */
601 urb->transfer_buffer = runtime->dma_area + subs->hwptr * stride;
602 subs->hwptr += offs;
603 }
604 spin_unlock_irqrestore(&subs->lock, flags);
605 urb->transfer_buffer_length = offs * stride;
606 ctx->transfer = offs;
607
608 return 0;
609}
610
611/*
612 * process after playback data complete
613 *
614 * update the current position and call callback if a period is processed.
615 */
616static int retire_playback_urb(snd_usb_substream_t *subs,
617 snd_pcm_runtime_t *runtime,
618 struct urb *urb)
619{
620 unsigned long flags;
621 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
622
623 spin_lock_irqsave(&subs->lock, flags);
624 subs->transfer_done += ctx->transfer;
625 subs->hwptr_done += ctx->transfer;
626 ctx->transfer = 0;
627 if (subs->hwptr_done >= runtime->buffer_size)
628 subs->hwptr_done -= runtime->buffer_size;
629 if (subs->transfer_done >= runtime->period_size) {
630 subs->transfer_done -= runtime->period_size;
631 spin_unlock_irqrestore(&subs->lock, flags);
632 snd_pcm_period_elapsed(subs->pcm_substream);
633 } else
634 spin_unlock_irqrestore(&subs->lock, flags);
635 return 0;
636}
637
638
639/*
640 */
641static struct snd_urb_ops audio_urb_ops[2] = {
642 {
643 .prepare = prepare_playback_urb,
644 .retire = retire_playback_urb,
645 .prepare_sync = prepare_playback_sync_urb,
646 .retire_sync = retire_playback_sync_urb,
647 },
648 {
649 .prepare = prepare_capture_urb,
650 .retire = retire_capture_urb,
651 .prepare_sync = prepare_capture_sync_urb,
652 .retire_sync = retire_capture_sync_urb,
653 },
654};
655
656static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
657 {
658 .prepare = prepare_playback_urb,
659 .retire = retire_playback_urb,
660 .prepare_sync = prepare_playback_sync_urb_hs,
661 .retire_sync = retire_playback_sync_urb_hs,
662 },
663 {
664 .prepare = prepare_capture_urb,
665 .retire = retire_capture_urb,
666 .prepare_sync = prepare_capture_sync_urb_hs,
667 .retire_sync = retire_capture_sync_urb,
668 },
669};
670
671/*
672 * complete callback from data urb
673 */
674static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
675{
676 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
677 snd_usb_substream_t *subs = ctx->subs;
678 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
679 int err = 0;
680
681 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
682 ! subs->running || /* can be stopped during retire callback */
683 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
684 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
685 clear_bit(ctx->index, &subs->active_mask);
686 if (err < 0) {
687 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
688 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
689 }
690 }
691}
692
693
694/*
695 * complete callback from sync urb
696 */
697static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
698{
699 snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
700 snd_usb_substream_t *subs = ctx->subs;
701 snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
702 int err = 0;
703
704 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
705 ! subs->running || /* can be stopped during retire callback */
706 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
707 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
708 clear_bit(ctx->index + 16, &subs->active_mask);
709 if (err < 0) {
710 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
711 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
712 }
713 }
714}
715
716
717/*
718 * unlink active urbs.
719 */
720static int deactivate_urbs(snd_usb_substream_t *subs, int force, int can_sleep)
721{
722 unsigned int i;
723 int async;
724
725 subs->running = 0;
726
727 if (!force && subs->stream->chip->shutdown) /* to be sure... */
728 return -EBADFD;
729
730 async = !can_sleep && async_unlink;
731
732 if (! async && in_interrupt())
733 return 0;
734
735 for (i = 0; i < subs->nurbs; i++) {
736 if (test_bit(i, &subs->active_mask)) {
737 if (! test_and_set_bit(i, &subs->unlink_mask)) {
738 struct urb *u = subs->dataurb[i].urb;
739 if (async) {
740 u->transfer_flags |= URB_ASYNC_UNLINK;
741 usb_unlink_urb(u);
742 } else
743 usb_kill_urb(u);
744 }
745 }
746 }
747 if (subs->syncpipe) {
748 for (i = 0; i < SYNC_URBS; i++) {
749 if (test_bit(i+16, &subs->active_mask)) {
750 if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
751 struct urb *u = subs->syncurb[i].urb;
752 if (async) {
753 u->transfer_flags |= URB_ASYNC_UNLINK;
754 usb_unlink_urb(u);
755 } else
756 usb_kill_urb(u);
757 }
758 }
759 }
760 }
761 return 0;
762}
763
764
765/*
766 * set up and start data/sync urbs
767 */
768static int start_urbs(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)
769{
770 unsigned int i;
771 int err;
772
773 if (subs->stream->chip->shutdown)
774 return -EBADFD;
775
776 for (i = 0; i < subs->nurbs; i++) {
777 snd_assert(subs->dataurb[i].urb, return -EINVAL);
778 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
779 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
780 goto __error;
781 }
782 }
783 if (subs->syncpipe) {
784 for (i = 0; i < SYNC_URBS; i++) {
785 snd_assert(subs->syncurb[i].urb, return -EINVAL);
786 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
787 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
788 goto __error;
789 }
790 }
791 }
792
793 subs->active_mask = 0;
794 subs->unlink_mask = 0;
795 subs->running = 1;
796 for (i = 0; i < subs->nurbs; i++) {
797 if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
798 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
799 goto __error;
800 }
801 set_bit(i, &subs->active_mask);
802 }
803 if (subs->syncpipe) {
804 for (i = 0; i < SYNC_URBS; i++) {
805 if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
806 snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
807 goto __error;
808 }
809 set_bit(i + 16, &subs->active_mask);
810 }
811 }
812 return 0;
813
814 __error:
815 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
816 deactivate_urbs(subs, 0, 0);
817 return -EPIPE;
818}
819
820
821/*
822 * wait until all urbs are processed.
823 */
824static int wait_clear_urbs(snd_usb_substream_t *subs)
825{
826 int timeout = HZ;
827 unsigned int i;
828 int alive;
829
830 do {
831 alive = 0;
832 for (i = 0; i < subs->nurbs; i++) {
833 if (test_bit(i, &subs->active_mask))
834 alive++;
835 }
836 if (subs->syncpipe) {
837 for (i = 0; i < SYNC_URBS; i++) {
838 if (test_bit(i + 16, &subs->active_mask))
839 alive++;
840 }
841 }
842 if (! alive)
843 break;
844 set_current_state(TASK_UNINTERRUPTIBLE);
845 schedule_timeout(1);
846 } while (--timeout > 0);
847 if (alive)
848 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
849 return 0;
850}
851
852
853/*
854 * return the current pcm pointer. just return the hwptr_done value.
855 */
856static snd_pcm_uframes_t snd_usb_pcm_pointer(snd_pcm_substream_t *substream)
857{
858 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
859 return subs->hwptr_done;
860}
861
862
863/*
864 * start/stop substream
865 */
866static int snd_usb_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
867{
868 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
869 int err;
870
871 switch (cmd) {
872 case SNDRV_PCM_TRIGGER_START:
873 err = start_urbs(subs, substream->runtime);
874 break;
875 case SNDRV_PCM_TRIGGER_STOP:
876 err = deactivate_urbs(subs, 0, 0);
877 break;
878 default:
879 err = -EINVAL;
880 break;
881 }
882 return err < 0 ? err : 0;
883}
884
885
886/*
887 * release a urb data
888 */
889static void release_urb_ctx(snd_urb_ctx_t *u)
890{
891 if (u->urb) {
892 usb_free_urb(u->urb);
893 u->urb = NULL;
894 }
895 if (u->buf) {
896 kfree(u->buf);
897 u->buf = NULL;
898 }
899}
900
901/*
902 * release a substream
903 */
904static void release_substream_urbs(snd_usb_substream_t *subs, int force)
905{
906 int i;
907
908 /* stop urbs (to be sure) */
909 deactivate_urbs(subs, force, 1);
910 wait_clear_urbs(subs);
911
912 for (i = 0; i < MAX_URBS; i++)
913 release_urb_ctx(&subs->dataurb[i]);
914 for (i = 0; i < SYNC_URBS; i++)
915 release_urb_ctx(&subs->syncurb[i]);
916 if (subs->tmpbuf) {
917 kfree(subs->tmpbuf);
918 subs->tmpbuf = NULL;
919 }
920 subs->nurbs = 0;
921}
922
923/*
924 * initialize a substream for plaback/capture
925 */
926static int init_substream_urbs(snd_usb_substream_t *subs, unsigned int period_bytes,
927 unsigned int rate, unsigned int frame_bits)
928{
929 unsigned int maxsize, n, i;
930 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
931 unsigned int npacks[MAX_URBS], urb_packs, total_packs;
932
933 /* calculate the frequency in 16.16 format */
934 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
935 subs->freqn = get_usb_full_speed_rate(rate);
936 else
937 subs->freqn = get_usb_high_speed_rate(rate);
938 subs->freqm = subs->freqn;
939 subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */
940 subs->phase = 0;
941
942 /* calculate the max. size of packet */
943 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) >> 16;
944 if (subs->maxpacksize && maxsize > subs->maxpacksize) {
945 //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",
946 // maxsize, subs->maxpacksize);
947 maxsize = subs->maxpacksize;
948 }
949
950 if (subs->fill_max)
951 subs->curpacksize = subs->maxpacksize;
952 else
953 subs->curpacksize = maxsize;
954
955 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
956 urb_packs = nrpacks;
957 else
958 urb_packs = nrpacks * 8;
959
960 /* allocate a temporary buffer for playback */
961 if (is_playback) {
962 subs->tmpbuf = kmalloc(maxsize * urb_packs, GFP_KERNEL);
963 if (! subs->tmpbuf) {
964 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
965 return -ENOMEM;
966 }
967 }
968
969 /* decide how many packets to be used */
970 total_packs = (period_bytes + maxsize - 1) / maxsize;
971 if (total_packs < 2 * MIN_PACKS_URB)
972 total_packs = 2 * MIN_PACKS_URB;
973 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
974 if (subs->nurbs > MAX_URBS) {
975 /* too much... */
976 subs->nurbs = MAX_URBS;
977 total_packs = MAX_URBS * urb_packs;
978 }
979 n = total_packs;
980 for (i = 0; i < subs->nurbs; i++) {
981 npacks[i] = n > urb_packs ? urb_packs : n;
982 n -= urb_packs;
983 }
984 if (subs->nurbs <= 1) {
985 /* too little - we need at least two packets
986 * to ensure contiguous playback/capture
987 */
988 subs->nurbs = 2;
989 npacks[0] = (total_packs + 1) / 2;
990 npacks[1] = total_packs - npacks[0];
991 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB) {
992 /* the last packet is too small.. */
993 if (subs->nurbs > 2) {
994 /* merge to the first one */
995 npacks[0] += npacks[subs->nurbs - 1];
996 subs->nurbs--;
997 } else {
998 /* divide to two */
999 subs->nurbs = 2;
1000 npacks[0] = (total_packs + 1) / 2;
1001 npacks[1] = total_packs - npacks[0];
1002 }
1003 }
1004
1005 /* allocate and initialize data urbs */
1006 for (i = 0; i < subs->nurbs; i++) {
1007 snd_urb_ctx_t *u = &subs->dataurb[i];
1008 u->index = i;
1009 u->subs = subs;
1010 u->transfer = 0;
1011 u->packets = npacks[i];
1012 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1013 u->packets++; /* for transfer delimiter */
1014 if (! is_playback) {
1015 /* allocate a capture buffer per urb */
1016 u->buf = kmalloc(maxsize * u->packets, GFP_KERNEL);
1017 if (! u->buf) {
1018 release_substream_urbs(subs, 0);
1019 return -ENOMEM;
1020 }
1021 }
1022 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1023 if (! u->urb) {
1024 release_substream_urbs(subs, 0);
1025 return -ENOMEM;
1026 }
1027 u->urb->dev = subs->dev;
1028 u->urb->pipe = subs->datapipe;
1029 u->urb->transfer_flags = URB_ISO_ASAP;
1030 u->urb->number_of_packets = u->packets;
1031 u->urb->interval = 1;
1032 u->urb->context = u;
1033 u->urb->complete = snd_usb_complete_callback(snd_complete_urb);
1034 }
1035
1036 if (subs->syncpipe) {
1037 /* allocate and initialize sync urbs */
1038 for (i = 0; i < SYNC_URBS; i++) {
1039 snd_urb_ctx_t *u = &subs->syncurb[i];
1040 u->index = i;
1041 u->subs = subs;
1042 u->packets = nrpacks;
1043 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1044 if (! u->urb) {
1045 release_substream_urbs(subs, 0);
1046 return -ENOMEM;
1047 }
1048 u->urb->transfer_buffer = subs->syncbuf + i * nrpacks * 4;
1049 u->urb->transfer_buffer_length = nrpacks * 4;
1050 u->urb->dev = subs->dev;
1051 u->urb->pipe = subs->syncpipe;
1052 u->urb->transfer_flags = URB_ISO_ASAP;
1053 u->urb->number_of_packets = u->packets;
1054 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
1055 u->urb->interval = 8;
1056 else
1057 u->urb->interval = 1;
1058 u->urb->context = u;
1059 u->urb->complete = snd_usb_complete_callback(snd_complete_sync_urb);
1060 }
1061 }
1062 return 0;
1063}
1064
1065
1066/*
1067 * find a matching audio format
1068 */
1069static struct audioformat *find_format(snd_usb_substream_t *subs, unsigned int format,
1070 unsigned int rate, unsigned int channels)
1071{
1072 struct list_head *p;
1073 struct audioformat *found = NULL;
1074 int cur_attr = 0, attr;
1075
1076 list_for_each(p, &subs->fmt_list) {
1077 struct audioformat *fp;
1078 fp = list_entry(p, struct audioformat, list);
1079 if (fp->format != format || fp->channels != channels)
1080 continue;
1081 if (rate < fp->rate_min || rate > fp->rate_max)
1082 continue;
1083 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
1084 unsigned int i;
1085 for (i = 0; i < fp->nr_rates; i++)
1086 if (fp->rate_table[i] == rate)
1087 break;
1088 if (i >= fp->nr_rates)
1089 continue;
1090 }
1091 attr = fp->ep_attr & EP_ATTR_MASK;
1092 if (! found) {
1093 found = fp;
1094 cur_attr = attr;
1095 continue;
1096 }
1097 /* avoid async out and adaptive in if the other method
1098 * supports the same format.
1099 * this is a workaround for the case like
1100 * M-audio audiophile USB.
1101 */
1102 if (attr != cur_attr) {
1103 if ((attr == EP_ATTR_ASYNC &&
1104 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1105 (attr == EP_ATTR_ADAPTIVE &&
1106 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
1107 continue;
1108 if ((cur_attr == EP_ATTR_ASYNC &&
1109 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
1110 (cur_attr == EP_ATTR_ADAPTIVE &&
1111 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
1112 found = fp;
1113 cur_attr = attr;
1114 continue;
1115 }
1116 }
1117 /* find the format with the largest max. packet size */
1118 if (fp->maxpacksize > found->maxpacksize) {
1119 found = fp;
1120 cur_attr = attr;
1121 }
1122 }
1123 return found;
1124}
1125
1126
1127/*
1128 * initialize the picth control and sample rate
1129 */
1130static int init_usb_pitch(struct usb_device *dev, int iface,
1131 struct usb_host_interface *alts,
1132 struct audioformat *fmt)
1133{
1134 unsigned int ep;
1135 unsigned char data[1];
1136 int err;
1137
1138 ep = get_endpoint(alts, 0)->bEndpointAddress;
1139 /* if endpoint has pitch control, enable it */
1140 if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
1141 data[0] = 1;
1142 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1143 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1144 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1145 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
1146 dev->devnum, iface, ep);
1147 return err;
1148 }
1149 }
1150 return 0;
1151}
1152
1153static int init_usb_sample_rate(struct usb_device *dev, int iface,
1154 struct usb_host_interface *alts,
1155 struct audioformat *fmt, int rate)
1156{
1157 unsigned int ep;
1158 unsigned char data[3];
1159 int err;
1160
1161 ep = get_endpoint(alts, 0)->bEndpointAddress;
1162 /* if endpoint has sampling rate control, set it */
1163 if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
1164 int crate;
1165 data[0] = rate;
1166 data[1] = rate >> 8;
1167 data[2] = rate >> 16;
1168 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1169 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1170 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1171 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
1172 dev->devnum, iface, fmt->altsetting, rate, ep);
1173 return err;
1174 }
1175 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1176 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1177 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1178 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
1179 dev->devnum, iface, fmt->altsetting, ep);
1180 return 0; /* some devices don't support reading */
1181 }
1182 crate = data[0] | (data[1] << 8) | (data[2] << 16);
1183 if (crate != rate) {
1184 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
1185 // runtime->rate = crate;
1186 }
1187 }
1188 return 0;
1189}
1190
1191/*
1192 * find a matching format and set up the interface
1193 */
1194static int set_format(snd_usb_substream_t *subs, struct audioformat *fmt)
1195{
1196 struct usb_device *dev = subs->dev;
1197 struct usb_host_interface *alts;
1198 struct usb_interface_descriptor *altsd;
1199 struct usb_interface *iface;
1200 unsigned int ep, attr;
1201 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1202 int err;
1203
1204 iface = usb_ifnum_to_if(dev, fmt->iface);
1205 snd_assert(iface, return -EINVAL);
1206 alts = &iface->altsetting[fmt->altset_idx];
1207 altsd = get_iface_desc(alts);
1208 snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1209
1210 if (fmt == subs->cur_audiofmt)
1211 return 0;
1212
1213 /* close the old interface */
1214 if (subs->interface >= 0 && subs->interface != fmt->iface) {
1215 usb_set_interface(subs->dev, subs->interface, 0);
1216 subs->interface = -1;
1217 subs->format = 0;
1218 }
1219
1220 /* set interface */
1221 if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
1222 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
1223 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
1224 dev->devnum, fmt->iface, fmt->altsetting);
1225 return -EIO;
1226 }
1227 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
1228 subs->interface = fmt->iface;
1229 subs->format = fmt->altset_idx;
1230 }
1231
1232 /* create a data pipe */
1233 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
1234 if (is_playback)
1235 subs->datapipe = usb_sndisocpipe(dev, ep);
1236 else
1237 subs->datapipe = usb_rcvisocpipe(dev, ep);
1238 subs->syncpipe = subs->syncinterval = 0;
1239 subs->maxpacksize = fmt->maxpacksize;
1240 subs->fill_max = 0;
1241
1242 /* we need a sync pipe in async OUT or adaptive IN mode */
1243 /* check the number of EP, since some devices have broken
1244 * descriptors which fool us. if it has only one EP,
1245 * assume it as adaptive-out or sync-in.
1246 */
1247 attr = fmt->ep_attr & EP_ATTR_MASK;
1248 if (((is_playback && attr == EP_ATTR_ASYNC) ||
1249 (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
1250 altsd->bNumEndpoints >= 2) {
1251 /* check sync-pipe endpoint */
1252 /* ... and check descriptor size before accessing bSynchAddress
1253 because there is a version of the SB Audigy 2 NX firmware lacking
1254 the audio fields in the endpoint descriptors */
1255 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
1256 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1257 get_endpoint(alts, 1)->bSynchAddress != 0)) {
1258 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1259 dev->devnum, fmt->iface, fmt->altsetting);
1260 return -EINVAL;
1261 }
1262 ep = get_endpoint(alts, 1)->bEndpointAddress;
1263 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
1264 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
1265 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
1266 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
1267 dev->devnum, fmt->iface, fmt->altsetting);
1268 return -EINVAL;
1269 }
1270 ep &= USB_ENDPOINT_NUMBER_MASK;
1271 if (is_playback)
1272 subs->syncpipe = usb_rcvisocpipe(dev, ep);
1273 else
1274 subs->syncpipe = usb_sndisocpipe(dev, ep);
1275 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1276 }
1277
1278 /* always fill max packet size */
1279 if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1280 subs->fill_max = 1;
1281
1282 if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1283 return err;
1284
1285 subs->cur_audiofmt = fmt;
1286
1287#if 0
1288 printk("setting done: format = %d, rate = %d, channels = %d\n",
1289 fmt->format, fmt->rate, fmt->channels);
1290 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
1291 subs->datapipe, subs->syncpipe);
1292#endif
1293
1294 return 0;
1295}
1296
1297/*
1298 * hw_params callback
1299 *
1300 * allocate a buffer and set the given audio format.
1301 *
1302 * so far we use a physically linear buffer although packetize transfer
1303 * doesn't need a continuous area.
1304 * if sg buffer is supported on the later version of alsa, we'll follow
1305 * that.
1306 */
1307static int snd_usb_hw_params(snd_pcm_substream_t *substream,
1308 snd_pcm_hw_params_t *hw_params)
1309{
1310 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1311 struct audioformat *fmt;
1312 unsigned int channels, rate, format;
1313 int ret, changed;
1314
1315 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1316 if (ret < 0)
1317 return ret;
1318
1319 format = params_format(hw_params);
1320 rate = params_rate(hw_params);
1321 channels = params_channels(hw_params);
1322 fmt = find_format(subs, format, rate, channels);
1323 if (! fmt) {
1324 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1325 snd_pcm_format_name(format), rate, channels);
1326 return -EINVAL;
1327 }
1328
1329 changed = subs->cur_audiofmt != fmt ||
1330 subs->period_bytes != params_period_bytes(hw_params) ||
1331 subs->cur_rate != rate;
1332 if ((ret = set_format(subs, fmt)) < 0)
1333 return ret;
1334
1335 if (subs->cur_rate != rate) {
1336 struct usb_host_interface *alts;
1337 struct usb_interface *iface;
1338 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
1339 alts = &iface->altsetting[fmt->altset_idx];
1340 ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1341 if (ret < 0)
1342 return ret;
1343 subs->cur_rate = rate;
1344 }
1345
1346 if (changed) {
1347 /* format changed */
1348 release_substream_urbs(subs, 0);
1349 /* influenced: period_bytes, channels, rate, format, */
1350 ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1351 params_rate(hw_params),
1352 snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1353 }
1354
1355 return ret;
1356}
1357
1358/*
1359 * hw_free callback
1360 *
1361 * reset the audio format and release the buffer
1362 */
1363static int snd_usb_hw_free(snd_pcm_substream_t *substream)
1364{
1365 snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1366
1367 subs->cur_audiofmt = NULL;
1368 subs->cur_rate = 0;
1369 subs->period_bytes = 0;
1370 release_substream_urbs(subs, 0);
1371 return snd_pcm_lib_free_pages(substream);
1372}
1373
1374/*
1375 * prepare callback
1376 *
1377 * only a few subtle things...
1378 */
1379static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)
1380{
1381 snd_pcm_runtime_t *runtime = substream->runtime;
1382 snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;
1383
1384 if (! subs->cur_audiofmt) {
1385 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1386 return -ENXIO;
1387 }
1388
1389 /* some unit conversions in runtime */
1390 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1391 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1392
1393 /* reset the pointer */
1394 subs->hwptr = 0;
1395 subs->hwptr_done = 0;
1396 subs->transfer_sched = 0;
1397 subs->transfer_done = 0;
1398 subs->phase = 0;
1399
1400 /* clear urbs (to be sure) */
1401 deactivate_urbs(subs, 0, 1);
1402 wait_clear_urbs(subs);
1403
1404 return 0;
1405}
1406
1407static snd_pcm_hardware_t snd_usb_playback =
1408{
1409 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1410 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1411 SNDRV_PCM_INFO_MMAP_VALID),
1412 .buffer_bytes_max = (128*1024),
1413 .period_bytes_min = 64,
1414 .period_bytes_max = (128*1024),
1415 .periods_min = 2,
1416 .periods_max = 1024,
1417};
1418
1419static snd_pcm_hardware_t snd_usb_capture =
1420{
1421 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1422 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1423 SNDRV_PCM_INFO_MMAP_VALID),
1424 .buffer_bytes_max = (128*1024),
1425 .period_bytes_min = 64,
1426 .period_bytes_max = (128*1024),
1427 .periods_min = 2,
1428 .periods_max = 1024,
1429};
1430
1431/*
1432 * h/w constraints
1433 */
1434
1435#ifdef HW_CONST_DEBUG
1436#define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
1437#else
1438#define hwc_debug(fmt, args...) /**/
1439#endif
1440
1441static int hw_check_valid_format(snd_pcm_hw_params_t *params, struct audioformat *fp)
1442{
1443 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1444 snd_interval_t *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1445 snd_mask_t *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1446
1447 /* check the format */
1448 if (! snd_mask_test(fmts, fp->format)) {
1449 hwc_debug(" > check: no supported format %d\n", fp->format);
1450 return 0;
1451 }
1452 /* check the channels */
1453 if (fp->channels < ct->min || fp->channels > ct->max) {
1454 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1455 return 0;
1456 }
1457 /* check the rate is within the range */
1458 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1459 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1460 return 0;
1461 }
1462 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1463 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1464 return 0;
1465 }
1466 return 1;
1467}
1468
1469static int hw_rule_rate(snd_pcm_hw_params_t *params,
1470 snd_pcm_hw_rule_t *rule)
1471{
1472 snd_usb_substream_t *subs = rule->private;
1473 struct list_head *p;
1474 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1475 unsigned int rmin, rmax;
1476 int changed;
1477
1478 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1479 changed = 0;
1480 rmin = rmax = 0;
1481 list_for_each(p, &subs->fmt_list) {
1482 struct audioformat *fp;
1483 fp = list_entry(p, struct audioformat, list);
1484 if (! hw_check_valid_format(params, fp))
1485 continue;
1486 if (changed++) {
1487 if (rmin > fp->rate_min)
1488 rmin = fp->rate_min;
1489 if (rmax < fp->rate_max)
1490 rmax = fp->rate_max;
1491 } else {
1492 rmin = fp->rate_min;
1493 rmax = fp->rate_max;
1494 }
1495 }
1496
1497 if (! changed) {
1498 hwc_debug(" --> get empty\n");
1499 it->empty = 1;
1500 return -EINVAL;
1501 }
1502
1503 changed = 0;
1504 if (it->min < rmin) {
1505 it->min = rmin;
1506 it->openmin = 0;
1507 changed = 1;
1508 }
1509 if (it->max > rmax) {
1510 it->max = rmax;
1511 it->openmax = 0;
1512 changed = 1;
1513 }
1514 if (snd_interval_checkempty(it)) {
1515 it->empty = 1;
1516 return -EINVAL;
1517 }
1518 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1519 return changed;
1520}
1521
1522
1523static int hw_rule_channels(snd_pcm_hw_params_t *params,
1524 snd_pcm_hw_rule_t *rule)
1525{
1526 snd_usb_substream_t *subs = rule->private;
1527 struct list_head *p;
1528 snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1529 unsigned int rmin, rmax;
1530 int changed;
1531
1532 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1533 changed = 0;
1534 rmin = rmax = 0;
1535 list_for_each(p, &subs->fmt_list) {
1536 struct audioformat *fp;
1537 fp = list_entry(p, struct audioformat, list);
1538 if (! hw_check_valid_format(params, fp))
1539 continue;
1540 if (changed++) {
1541 if (rmin > fp->channels)
1542 rmin = fp->channels;
1543 if (rmax < fp->channels)
1544 rmax = fp->channels;
1545 } else {
1546 rmin = fp->channels;
1547 rmax = fp->channels;
1548 }
1549 }
1550
1551 if (! changed) {
1552 hwc_debug(" --> get empty\n");
1553 it->empty = 1;
1554 return -EINVAL;
1555 }
1556
1557 changed = 0;
1558 if (it->min < rmin) {
1559 it->min = rmin;
1560 it->openmin = 0;
1561 changed = 1;
1562 }
1563 if (it->max > rmax) {
1564 it->max = rmax;
1565 it->openmax = 0;
1566 changed = 1;
1567 }
1568 if (snd_interval_checkempty(it)) {
1569 it->empty = 1;
1570 return -EINVAL;
1571 }
1572 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1573 return changed;
1574}
1575
1576static int hw_rule_format(snd_pcm_hw_params_t *params,
1577 snd_pcm_hw_rule_t *rule)
1578{
1579 snd_usb_substream_t *subs = rule->private;
1580 struct list_head *p;
1581 snd_mask_t *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1582 u64 fbits;
1583 u32 oldbits[2];
1584 int changed;
1585
1586 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1587 fbits = 0;
1588 list_for_each(p, &subs->fmt_list) {
1589 struct audioformat *fp;
1590 fp = list_entry(p, struct audioformat, list);
1591 if (! hw_check_valid_format(params, fp))
1592 continue;
1593 fbits |= (1ULL << fp->format);
1594 }
1595
1596 oldbits[0] = fmt->bits[0];
1597 oldbits[1] = fmt->bits[1];
1598 fmt->bits[0] &= (u32)fbits;
1599 fmt->bits[1] &= (u32)(fbits >> 32);
1600 if (! fmt->bits[0] && ! fmt->bits[1]) {
1601 hwc_debug(" --> get empty\n");
1602 return -EINVAL;
1603 }
1604 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1605 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1606 return changed;
1607}
1608
1609#define MAX_MASK 64
1610
1611/*
1612 * check whether the registered audio formats need special hw-constraints
1613 */
1614static int check_hw_params_convention(snd_usb_substream_t *subs)
1615{
1616 int i;
1617 u32 *channels;
1618 u32 *rates;
1619 u32 cmaster, rmaster;
1620 u32 rate_min = 0, rate_max = 0;
1621 struct list_head *p;
1622 int err = 1;
1623
1624 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1625 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1626
1627 list_for_each(p, &subs->fmt_list) {
1628 struct audioformat *f;
1629 f = list_entry(p, struct audioformat, list);
1630 /* unconventional channels? */
1631 if (f->channels > 32)
1632 goto __out;
1633 /* continuous rate min/max matches? */
1634 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1635 if (rate_min && f->rate_min != rate_min)
1636 goto __out;
1637 if (rate_max && f->rate_max != rate_max)
1638 goto __out;
1639 rate_min = f->rate_min;
1640 rate_max = f->rate_max;
1641 }
1642 /* combination of continuous rates and fixed rates? */
1643 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1644 if (f->rates != rates[f->format])
1645 goto __out;
1646 }
1647 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1648 if (rates[f->format] && rates[f->format] != f->rates)
1649 goto __out;
1650 }
1651 channels[f->format] |= (1 << f->channels);
1652 rates[f->format] |= f->rates;
1653 }
1654 /* check whether channels and rates match for all formats */
1655 cmaster = rmaster = 0;
1656 for (i = 0; i < MAX_MASK; i++) {
1657 if (cmaster != channels[i] && cmaster && channels[i])
1658 goto __out;
1659 if (rmaster != rates[i] && rmaster && rates[i])
1660 goto __out;
1661 if (channels[i])
1662 cmaster = channels[i];
1663 if (rates[i])
1664 rmaster = rates[i];
1665 }
1666 /* check whether channels match for all distinct rates */
1667 memset(channels, 0, MAX_MASK * sizeof(u32));
1668 list_for_each(p, &subs->fmt_list) {
1669 struct audioformat *f;
1670 f = list_entry(p, struct audioformat, list);
1671 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1672 continue;
1673 for (i = 0; i < 32; i++) {
1674 if (f->rates & (1 << i))
1675 channels[i] |= (1 << f->channels);
1676 }
1677 }
1678 cmaster = 0;
1679 for (i = 0; i < 32; i++) {
1680 if (cmaster != channels[i] && cmaster && channels[i])
1681 goto __out;
1682 if (channels[i])
1683 cmaster = channels[i];
1684 }
1685 err = 0;
1686
1687 __out:
1688 kfree(channels);
1689 kfree(rates);
1690 return err;
1691}
1692
1693
1694/*
1695 * set up the runtime hardware information.
1696 */
1697
1698static int setup_hw_info(snd_pcm_runtime_t *runtime, snd_usb_substream_t *subs)
1699{
1700 struct list_head *p;
1701 int err;
1702
1703 runtime->hw.formats = subs->formats;
1704
1705 runtime->hw.rate_min = 0x7fffffff;
1706 runtime->hw.rate_max = 0;
1707 runtime->hw.channels_min = 256;
1708 runtime->hw.channels_max = 0;
1709 runtime->hw.rates = 0;
1710 /* check min/max rates and channels */
1711 list_for_each(p, &subs->fmt_list) {
1712 struct audioformat *fp;
1713 fp = list_entry(p, struct audioformat, list);
1714 runtime->hw.rates |= fp->rates;
1715 if (runtime->hw.rate_min > fp->rate_min)
1716 runtime->hw.rate_min = fp->rate_min;
1717 if (runtime->hw.rate_max < fp->rate_max)
1718 runtime->hw.rate_max = fp->rate_max;
1719 if (runtime->hw.channels_min > fp->channels)
1720 runtime->hw.channels_min = fp->channels;
1721 if (runtime->hw.channels_max < fp->channels)
1722 runtime->hw.channels_max = fp->channels;
1723 if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
1724 /* FIXME: there might be more than one audio formats... */
1725 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1726 fp->frame_size;
1727 }
1728 }
1729
1730 /* set the period time minimum 1ms */
1731 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1732 1000 * MIN_PACKS_URB,
1733 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1734
1735 if (check_hw_params_convention(subs)) {
1736 hwc_debug("setting extra hw constraints...\n");
1737 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1738 hw_rule_rate, subs,
1739 SNDRV_PCM_HW_PARAM_FORMAT,
1740 SNDRV_PCM_HW_PARAM_CHANNELS,
1741 -1)) < 0)
1742 return err;
1743 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1744 hw_rule_channels, subs,
1745 SNDRV_PCM_HW_PARAM_FORMAT,
1746 SNDRV_PCM_HW_PARAM_RATE,
1747 -1)) < 0)
1748 return err;
1749 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1750 hw_rule_format, subs,
1751 SNDRV_PCM_HW_PARAM_RATE,
1752 SNDRV_PCM_HW_PARAM_CHANNELS,
1753 -1)) < 0)
1754 return err;
1755 }
1756 return 0;
1757}
1758
1759static int snd_usb_pcm_open(snd_pcm_substream_t *substream, int direction,
1760 snd_pcm_hardware_t *hw)
1761{
1762 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1763 snd_pcm_runtime_t *runtime = substream->runtime;
1764 snd_usb_substream_t *subs = &as->substream[direction];
1765
1766 subs->interface = -1;
1767 subs->format = 0;
1768 runtime->hw = *hw;
1769 runtime->private_data = subs;
1770 subs->pcm_substream = substream;
1771 return setup_hw_info(runtime, subs);
1772}
1773
1774static int snd_usb_pcm_close(snd_pcm_substream_t *substream, int direction)
1775{
1776 snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1777 snd_usb_substream_t *subs = &as->substream[direction];
1778
1779 if (subs->interface >= 0) {
1780 usb_set_interface(subs->dev, subs->interface, 0);
1781 subs->interface = -1;
1782 }
1783 subs->pcm_substream = NULL;
1784 return 0;
1785}
1786
1787static int snd_usb_playback_open(snd_pcm_substream_t *substream)
1788{
1789 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
1790}
1791
1792static int snd_usb_playback_close(snd_pcm_substream_t *substream)
1793{
1794 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1795}
1796
1797static int snd_usb_capture_open(snd_pcm_substream_t *substream)
1798{
1799 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
1800}
1801
1802static int snd_usb_capture_close(snd_pcm_substream_t *substream)
1803{
1804 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1805}
1806
1807static snd_pcm_ops_t snd_usb_playback_ops = {
1808 .open = snd_usb_playback_open,
1809 .close = snd_usb_playback_close,
1810 .ioctl = snd_pcm_lib_ioctl,
1811 .hw_params = snd_usb_hw_params,
1812 .hw_free = snd_usb_hw_free,
1813 .prepare = snd_usb_pcm_prepare,
1814 .trigger = snd_usb_pcm_trigger,
1815 .pointer = snd_usb_pcm_pointer,
1816};
1817
1818static snd_pcm_ops_t snd_usb_capture_ops = {
1819 .open = snd_usb_capture_open,
1820 .close = snd_usb_capture_close,
1821 .ioctl = snd_pcm_lib_ioctl,
1822 .hw_params = snd_usb_hw_params,
1823 .hw_free = snd_usb_hw_free,
1824 .prepare = snd_usb_pcm_prepare,
1825 .trigger = snd_usb_pcm_trigger,
1826 .pointer = snd_usb_pcm_pointer,
1827};
1828
1829
1830
1831/*
1832 * helper functions
1833 */
1834
1835/*
1836 * combine bytes and get an integer value
1837 */
1838unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
1839{
1840 switch (size) {
1841 case 1: return *bytes;
1842 case 2: return combine_word(bytes);
1843 case 3: return combine_triple(bytes);
1844 case 4: return combine_quad(bytes);
1845 default: return 0;
1846 }
1847}
1848
1849/*
1850 * parse descriptor buffer and return the pointer starting the given
1851 * descriptor type.
1852 */
1853void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
1854{
1855 u8 *p, *end, *next;
1856
1857 p = descstart;
1858 end = p + desclen;
1859 for (; p < end;) {
1860 if (p[0] < 2)
1861 return NULL;
1862 next = p + p[0];
1863 if (next > end)
1864 return NULL;
1865 if (p[1] == dtype && (!after || (void *)p > after)) {
1866 return p;
1867 }
1868 p = next;
1869 }
1870 return NULL;
1871}
1872
1873/*
1874 * find a class-specified interface descriptor with the given subtype.
1875 */
1876void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
1877{
1878 unsigned char *p = after;
1879
1880 while ((p = snd_usb_find_desc(buffer, buflen, p,
1881 USB_DT_CS_INTERFACE)) != NULL) {
1882 if (p[0] >= 3 && p[2] == dsubtype)
1883 return p;
1884 }
1885 return NULL;
1886}
1887
1888/*
1889 * Wrapper for usb_control_msg().
1890 * Allocates a temp buffer to prevent dmaing from/to the stack.
1891 */
1892int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
1893 __u8 requesttype, __u16 value, __u16 index, void *data,
1894 __u16 size, int timeout)
1895{
1896 int err;
1897 void *buf = NULL;
1898
1899 if (size > 0) {
1900 buf = kmalloc(size, GFP_KERNEL);
1901 if (!buf)
1902 return -ENOMEM;
1903 memcpy(buf, data, size);
1904 }
1905 err = usb_control_msg(dev, pipe, request, requesttype,
1906 value, index, buf, size, timeout);
1907 if (size > 0) {
1908 memcpy(data, buf, size);
1909 kfree(buf);
1910 }
1911 return err;
1912}
1913
1914
1915/*
1916 * entry point for linux usb interface
1917 */
1918
1919static int usb_audio_probe(struct usb_interface *intf,
1920 const struct usb_device_id *id);
1921static void usb_audio_disconnect(struct usb_interface *intf);
1922
1923static struct usb_device_id usb_audio_ids [] = {
1924#include "usbquirks.h"
1925 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1926 .bInterfaceClass = USB_CLASS_AUDIO,
1927 .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
1928 { } /* Terminating entry */
1929};
1930
1931MODULE_DEVICE_TABLE (usb, usb_audio_ids);
1932
1933static struct usb_driver usb_audio_driver = {
1934 .owner = THIS_MODULE,
1935 .name = "snd-usb-audio",
1936 .probe = usb_audio_probe,
1937 .disconnect = usb_audio_disconnect,
1938 .id_table = usb_audio_ids,
1939};
1940
1941
1942/*
1943 * proc interface for list the supported pcm formats
1944 */
1945static void proc_dump_substream_formats(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1946{
1947 struct list_head *p;
1948 static char *sync_types[4] = {
1949 "NONE", "ASYNC", "ADAPTIVE", "SYNC"
1950 };
1951
1952 list_for_each(p, &subs->fmt_list) {
1953 struct audioformat *fp;
1954 fp = list_entry(p, struct audioformat, list);
1955 snd_iprintf(buffer, " Interface %d\n", fp->iface);
1956 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
1957 snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
1958 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
1959 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
1960 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
1961 fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
1962 sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
1963 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1964 snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
1965 fp->rate_min, fp->rate_max);
1966 } else {
1967 unsigned int i;
1968 snd_iprintf(buffer, " Rates: ");
1969 for (i = 0; i < fp->nr_rates; i++) {
1970 if (i > 0)
1971 snd_iprintf(buffer, ", ");
1972 snd_iprintf(buffer, "%d", fp->rate_table[i]);
1973 }
1974 snd_iprintf(buffer, "\n");
1975 }
1976 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
1977 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
1978 }
1979}
1980
1981static void proc_dump_substream_status(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
1982{
1983 if (subs->running) {
1984 unsigned int i;
1985 snd_iprintf(buffer, " Status: Running\n");
1986 snd_iprintf(buffer, " Interface = %d\n", subs->interface);
1987 snd_iprintf(buffer, " Altset = %d\n", subs->format);
1988 snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
1989 for (i = 0; i < subs->nurbs; i++)
1990 snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
1991 snd_iprintf(buffer, "]\n");
1992 snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
1993 snd_iprintf(buffer, " Momentary freq = %u Hz\n",
1994 snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
1995 ? get_full_speed_hz(subs->freqm)
1996 : get_high_speed_hz(subs->freqm));
1997 } else {
1998 snd_iprintf(buffer, " Status: Stop\n");
1999 }
2000}
2001
2002static void proc_pcm_format_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
2003{
2004 snd_usb_stream_t *stream = entry->private_data;
2005
2006 snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
2007
2008 if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
2009 snd_iprintf(buffer, "\nPlayback:\n");
2010 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2011 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
2012 }
2013 if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
2014 snd_iprintf(buffer, "\nCapture:\n");
2015 proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2016 proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
2017 }
2018}
2019
2020static void proc_pcm_format_add(snd_usb_stream_t *stream)
2021{
2022 snd_info_entry_t *entry;
2023 char name[32];
2024 snd_card_t *card = stream->chip->card;
2025
2026 sprintf(name, "stream%d", stream->pcm_index);
2027 if (! snd_card_proc_new(card, name, &entry))
2028 snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
2029}
2030
2031
2032/*
2033 * initialize the substream instance.
2034 */
2035
2036static void init_substream(snd_usb_stream_t *as, int stream, struct audioformat *fp)
2037{
2038 snd_usb_substream_t *subs = &as->substream[stream];
2039
2040 INIT_LIST_HEAD(&subs->fmt_list);
2041 spin_lock_init(&subs->lock);
2042
2043 subs->stream = as;
2044 subs->direction = stream;
2045 subs->dev = as->chip->dev;
2046 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
2047 subs->ops = audio_urb_ops[stream];
2048 else
2049 subs->ops = audio_urb_ops_high_speed[stream];
2050 snd_pcm_lib_preallocate_pages(as->pcm->streams[stream].substream,
2051 SNDRV_DMA_TYPE_CONTINUOUS,
2052 snd_dma_continuous_data(GFP_KERNEL),
2053 64 * 1024, 128 * 1024);
2054 snd_pcm_set_ops(as->pcm, stream,
2055 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2056 &snd_usb_playback_ops : &snd_usb_capture_ops);
2057
2058 list_add_tail(&fp->list, &subs->fmt_list);
2059 subs->formats |= 1ULL << fp->format;
2060 subs->endpoint = fp->endpoint;
2061 subs->num_formats++;
2062 subs->fmt_type = fp->fmt_type;
2063}
2064
2065
2066/*
2067 * free a substream
2068 */
2069static void free_substream(snd_usb_substream_t *subs)
2070{
2071 struct list_head *p, *n;
2072
2073 if (! subs->num_formats)
2074 return; /* not initialized */
2075 list_for_each_safe(p, n, &subs->fmt_list) {
2076 struct audioformat *fp = list_entry(p, struct audioformat, list);
2077 kfree(fp->rate_table);
2078 kfree(fp);
2079 }
2080}
2081
2082
2083/*
2084 * free a usb stream instance
2085 */
2086static void snd_usb_audio_stream_free(snd_usb_stream_t *stream)
2087{
2088 free_substream(&stream->substream[0]);
2089 free_substream(&stream->substream[1]);
2090 list_del(&stream->list);
2091 kfree(stream);
2092}
2093
2094static void snd_usb_audio_pcm_free(snd_pcm_t *pcm)
2095{
2096 snd_usb_stream_t *stream = pcm->private_data;
2097 if (stream) {
2098 stream->pcm = NULL;
2099 snd_pcm_lib_preallocate_free_for_all(pcm);
2100 snd_usb_audio_stream_free(stream);
2101 }
2102}
2103
2104
2105/*
2106 * add this endpoint to the chip instance.
2107 * if a stream with the same endpoint already exists, append to it.
2108 * if not, create a new pcm stream.
2109 */
2110static int add_audio_endpoint(snd_usb_audio_t *chip, int stream, struct audioformat *fp)
2111{
2112 struct list_head *p;
2113 snd_usb_stream_t *as;
2114 snd_usb_substream_t *subs;
2115 snd_pcm_t *pcm;
2116 int err;
2117
2118 list_for_each(p, &chip->pcm_list) {
2119 as = list_entry(p, snd_usb_stream_t, list);
2120 if (as->fmt_type != fp->fmt_type)
2121 continue;
2122 subs = &as->substream[stream];
2123 if (! subs->endpoint)
2124 continue;
2125 if (subs->endpoint == fp->endpoint) {
2126 list_add_tail(&fp->list, &subs->fmt_list);
2127 subs->num_formats++;
2128 subs->formats |= 1ULL << fp->format;
2129 return 0;
2130 }
2131 }
2132 /* look for an empty stream */
2133 list_for_each(p, &chip->pcm_list) {
2134 as = list_entry(p, snd_usb_stream_t, list);
2135 if (as->fmt_type != fp->fmt_type)
2136 continue;
2137 subs = &as->substream[stream];
2138 if (subs->endpoint)
2139 continue;
2140 err = snd_pcm_new_stream(as->pcm, stream, 1);
2141 if (err < 0)
2142 return err;
2143 init_substream(as, stream, fp);
2144 return 0;
2145 }
2146
2147 /* create a new pcm */
2148 as = kmalloc(sizeof(*as), GFP_KERNEL);
2149 if (! as)
2150 return -ENOMEM;
2151 memset(as, 0, sizeof(*as));
2152 as->pcm_index = chip->pcm_devs;
2153 as->chip = chip;
2154 as->fmt_type = fp->fmt_type;
2155 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
2156 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
2157 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
2158 &pcm);
2159 if (err < 0) {
2160 kfree(as);
2161 return err;
2162 }
2163 as->pcm = pcm;
2164 pcm->private_data = as;
2165 pcm->private_free = snd_usb_audio_pcm_free;
2166 pcm->info_flags = 0;
2167 if (chip->pcm_devs > 0)
2168 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
2169 else
2170 strcpy(pcm->name, "USB Audio");
2171
2172 init_substream(as, stream, fp);
2173
2174 list_add(&as->list, &chip->pcm_list);
2175 chip->pcm_devs++;
2176
2177 proc_pcm_format_add(as);
2178
2179 return 0;
2180}
2181
2182
2183/*
2184 * check if the device uses big-endian samples
2185 */
2186static int is_big_endian_format(struct usb_device *dev, struct audioformat *fp)
2187{
2188 /* M-Audio */
2189 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763) {
2190 /* Quattro: captured data only */
2191 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2001 &&
2192 fp->endpoint & USB_DIR_IN)
2193 return 1;
2194 /* Audiophile USB */
2195 if (le16_to_cpu(dev->descriptor.idProduct) == 0x2003)
2196 return 1;
2197 }
2198 return 0;
2199}
2200
2201/*
2202 * parse the audio format type I descriptor
2203 * and returns the corresponding pcm format
2204 *
2205 * @dev: usb device
2206 * @fp: audioformat record
2207 * @format: the format tag (wFormatTag)
2208 * @fmt: the format type descriptor
2209 */
2210static int parse_audio_format_i_type(struct usb_device *dev, struct audioformat *fp,
2211 int format, unsigned char *fmt)
2212{
2213 int pcm_format;
2214 int sample_width, sample_bytes;
2215
2216 /* FIXME: correct endianess and sign? */
2217 pcm_format = -1;
2218 sample_width = fmt[6];
2219 sample_bytes = fmt[5];
2220 switch (format) {
2221 case 0: /* some devices don't define this correctly... */
2222 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
2223 dev->devnum, fp->iface, fp->altsetting);
2224 /* fall-through */
2225 case USB_AUDIO_FORMAT_PCM:
2226 if (sample_width > sample_bytes * 8) {
2227 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
2228 dev->devnum, fp->iface, fp->altsetting,
2229 sample_width, sample_bytes);
2230 }
2231 /* check the format byte size */
2232 switch (fmt[5]) {
2233 case 1:
2234 pcm_format = SNDRV_PCM_FORMAT_S8;
2235 break;
2236 case 2:
2237 if (is_big_endian_format(dev, fp))
2238 pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
2239 else
2240 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2241 break;
2242 case 3:
2243 if (is_big_endian_format(dev, fp))
2244 pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
2245 else
2246 pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
2247 break;
2248 case 4:
2249 pcm_format = SNDRV_PCM_FORMAT_S32_LE;
2250 break;
2251 default:
2252 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
2253 dev->devnum, fp->iface, fp->altsetting, sample_width, sample_bytes);
2254 break;
2255 }
2256 break;
2257 case USB_AUDIO_FORMAT_PCM8:
2258 /* Dallas DS4201 workaround */
2259 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2260 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2261 pcm_format = SNDRV_PCM_FORMAT_S8;
2262 else
2263 pcm_format = SNDRV_PCM_FORMAT_U8;
2264 break;
2265 case USB_AUDIO_FORMAT_IEEE_FLOAT:
2266 pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
2267 break;
2268 case USB_AUDIO_FORMAT_ALAW:
2269 pcm_format = SNDRV_PCM_FORMAT_A_LAW;
2270 break;
2271 case USB_AUDIO_FORMAT_MU_LAW:
2272 pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
2273 break;
2274 default:
2275 snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
2276 dev->devnum, fp->iface, fp->altsetting, format);
2277 break;
2278 }
2279 return pcm_format;
2280}
2281
2282
2283/*
2284 * parse the format descriptor and stores the possible sample rates
2285 * on the audioformat table.
2286 *
2287 * @dev: usb device
2288 * @fp: audioformat record
2289 * @fmt: the format descriptor
2290 * @offset: the start offset of descriptor pointing the rate type
2291 * (7 for type I and II, 8 for type II)
2292 */
2293static int parse_audio_format_rates(struct usb_device *dev, struct audioformat *fp,
2294 unsigned char *fmt, int offset)
2295{
2296 int nr_rates = fmt[offset];
2297 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2298 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2299 dev->devnum, fp->iface, fp->altsetting);
2300 return -1;
2301 }
2302
2303 if (nr_rates) {
2304 /*
2305 * build the rate table and bitmap flags
2306 */
2307 int r, idx, c;
2308 /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
2309 static unsigned int conv_rates[] = {
2310 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
2311 64000, 88200, 96000, 176400, 192000
2312 };
2313 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2314 if (fp->rate_table == NULL) {
2315 snd_printk(KERN_ERR "cannot malloc\n");
2316 return -1;
2317 }
2318
2319 fp->nr_rates = nr_rates;
2320 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
2321 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2322 unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
2323 if (rate < fp->rate_min)
2324 fp->rate_min = rate;
2325 else if (rate > fp->rate_max)
2326 fp->rate_max = rate;
2327 for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
2328 if (rate == conv_rates[c]) {
2329 fp->rates |= (1 << c);
2330 break;
2331 }
2332 }
2333 }
2334 } else {
2335 /* continuous rates */
2336 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
2337 fp->rate_min = combine_triple(&fmt[offset + 1]);
2338 fp->rate_max = combine_triple(&fmt[offset + 4]);
2339 }
2340 return 0;
2341}
2342
2343/*
2344 * parse the format type I and III descriptors
2345 */
2346static int parse_audio_format_i(struct usb_device *dev, struct audioformat *fp,
2347 int format, unsigned char *fmt)
2348{
2349 int pcm_format;
2350
2351 if (fmt[3] == USB_FORMAT_TYPE_III) {
2352 /* FIXME: the format type is really IECxxx
2353 * but we give normal PCM format to get the existing
2354 * apps working...
2355 */
2356 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
2357 } else {
2358 pcm_format = parse_audio_format_i_type(dev, fp, format, fmt);
2359 if (pcm_format < 0)
2360 return -1;
2361 }
2362 fp->format = pcm_format;
2363 fp->channels = fmt[4];
2364 if (fp->channels < 1) {
2365 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
2366 dev->devnum, fp->iface, fp->altsetting, fp->channels);
2367 return -1;
2368 }
2369 return parse_audio_format_rates(dev, fp, fmt, 7);
2370}
2371
2372/*
2373 * prase the format type II descriptor
2374 */
2375static int parse_audio_format_ii(struct usb_device *dev, struct audioformat *fp,
2376 int format, unsigned char *fmt)
2377{
2378 int brate, framesize;
2379 switch (format) {
2380 case USB_AUDIO_FORMAT_AC3:
2381 /* FIXME: there is no AC3 format defined yet */
2382 // fp->format = SNDRV_PCM_FORMAT_AC3;
2383 fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
2384 break;
2385 case USB_AUDIO_FORMAT_MPEG:
2386 fp->format = SNDRV_PCM_FORMAT_MPEG;
2387 break;
2388 default:
2389 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
2390 dev->devnum, fp->iface, fp->altsetting, format);
2391 fp->format = SNDRV_PCM_FORMAT_MPEG;
2392 break;
2393 }
2394 fp->channels = 1;
2395 brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
2396 framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
2397 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
2398 fp->frame_size = framesize;
2399 return parse_audio_format_rates(dev, fp, fmt, 8); /* fmt[8..] sample rates */
2400}
2401
2402static int parse_audio_format(struct usb_device *dev, struct audioformat *fp,
2403 int format, unsigned char *fmt, int stream)
2404{
2405 int err;
2406
2407 switch (fmt[3]) {
2408 case USB_FORMAT_TYPE_I:
2409 case USB_FORMAT_TYPE_III:
2410 err = parse_audio_format_i(dev, fp, format, fmt);
2411 break;
2412 case USB_FORMAT_TYPE_II:
2413 err = parse_audio_format_ii(dev, fp, format, fmt);
2414 break;
2415 default:
2416 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
2417 dev->devnum, fp->iface, fp->altsetting, fmt[3]);
2418 return -1;
2419 }
2420 fp->fmt_type = fmt[3];
2421 if (err < 0)
2422 return err;
2423#if 1
2424 /* FIXME: temporary hack for extigy */
2425 /* extigy apparently supports sample rates other than 48k
2426 * but not in ordinary way. so we enable only 48k atm.
2427 */
2428 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
2429 le16_to_cpu(dev->descriptor.idProduct) == 0x3000) {
2430 if (fmt[3] == USB_FORMAT_TYPE_I &&
2431 stream == SNDRV_PCM_STREAM_PLAYBACK &&
2432 fp->rates != SNDRV_PCM_RATE_48000)
2433 return -1; /* use 48k only */
2434 }
2435#endif
2436 return 0;
2437}
2438
2439static int parse_audio_endpoints(snd_usb_audio_t *chip, int iface_no)
2440{
2441 struct usb_device *dev;
2442 struct usb_interface *iface;
2443 struct usb_host_interface *alts;
2444 struct usb_interface_descriptor *altsd;
2445 int i, altno, err, stream;
2446 int format;
2447 struct audioformat *fp;
2448 unsigned char *fmt, *csep;
2449
2450 dev = chip->dev;
2451
2452 /* parse the interface's altsettings */
2453 iface = usb_ifnum_to_if(dev, iface_no);
2454 for (i = 0; i < iface->num_altsetting; i++) {
2455 alts = &iface->altsetting[i];
2456 altsd = get_iface_desc(alts);
2457 /* skip invalid one */
2458 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2459 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2460 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2461 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2462 altsd->bNumEndpoints < 1 ||
2463 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
2464 continue;
2465 /* must be isochronous */
2466 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2467 USB_ENDPOINT_XFER_ISOC)
2468 continue;
2469 /* check direction */
2470 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
2471 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2472 altno = altsd->bAlternateSetting;
2473
2474 /* get audio formats */
2475 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
2476 if (!fmt) {
2477 snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
2478 dev->devnum, iface_no, altno);
2479 continue;
2480 }
2481
2482 if (fmt[0] < 7) {
2483 snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
2484 dev->devnum, iface_no, altno);
2485 continue;
2486 }
2487
2488 format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
2489
2490 /* get format type */
2491 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
2492 if (!fmt) {
2493 snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
2494 dev->devnum, iface_no, altno);
2495 continue;
2496 }
2497 if (fmt[0] < 8) {
2498 snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
2499 dev->devnum, iface_no, altno);
2500 continue;
2501 }
2502
2503 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
2504 /* Creamware Noah has this descriptor after the 2nd endpoint */
2505 if (!csep && altsd->bNumEndpoints >= 2)
2506 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
2507 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2508 snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
2509 dev->devnum, iface_no, altno);
2510 continue;
2511 }
2512
2513 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2514 if (! fp) {
2515 snd_printk(KERN_ERR "cannot malloc\n");
2516 return -ENOMEM;
2517 }
2518
2519 memset(fp, 0, sizeof(*fp));
2520 fp->iface = iface_no;
2521 fp->altsetting = altno;
2522 fp->altset_idx = i;
2523 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2524 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2525 /* FIXME: decode wMaxPacketSize of high bandwith endpoints */
2526 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2527 fp->attributes = csep[3];
2528
2529 /* some quirks for attributes here */
2530
2531 /* workaround for AudioTrak Optoplay */
2532 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0a92 &&
2533 le16_to_cpu(dev->descriptor.idProduct) == 0x0053) {
2534 /* Optoplay sets the sample rate attribute although
2535 * it seems not supporting it in fact.
2536 */
2537 fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
2538 }
2539
2540 /* workaround for M-Audio Audiophile USB */
2541 if (le16_to_cpu(dev->descriptor.idVendor) == 0x0763 &&
2542 le16_to_cpu(dev->descriptor.idProduct) == 0x2003) {
2543 /* doesn't set the sample rate attribute, but supports it */
2544 fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
2545 }
2546
2547 /*
2548 * plantronics headset and Griffin iMic have set adaptive-in
2549 * although it's really not...
2550 */
2551 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x047f &&
2552 le16_to_cpu(dev->descriptor.idProduct) == 0x0ca1) ||
2553 /* Griffin iMic (note that there is an older model 77d:223) */
2554 (le16_to_cpu(dev->descriptor.idVendor) == 0x077d &&
2555 le16_to_cpu(dev->descriptor.idProduct) == 0x07af)) {
2556 fp->ep_attr &= ~EP_ATTR_MASK;
2557 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2558 fp->ep_attr |= EP_ATTR_ADAPTIVE;
2559 else
2560 fp->ep_attr |= EP_ATTR_SYNC;
2561 }
2562
2563 /* ok, let's parse further... */
2564 if (parse_audio_format(dev, fp, format, fmt, stream) < 0) {
2565 kfree(fp->rate_table);
2566 kfree(fp);
2567 continue;
2568 }
2569
2570 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
2571 err = add_audio_endpoint(chip, stream, fp);
2572 if (err < 0) {
2573 kfree(fp->rate_table);
2574 kfree(fp);
2575 return err;
2576 }
2577 /* try to set the interface... */
2578 usb_set_interface(chip->dev, iface_no, altno);
2579 init_usb_pitch(chip->dev, iface_no, alts, fp);
2580 init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
2581 }
2582 return 0;
2583}
2584
2585
2586/*
2587 * disconnect streams
2588 * called from snd_usb_audio_disconnect()
2589 */
2590static void snd_usb_stream_disconnect(struct list_head *head, struct usb_driver *driver)
2591{
2592 int idx;
2593 snd_usb_stream_t *as;
2594 snd_usb_substream_t *subs;
2595
2596 as = list_entry(head, snd_usb_stream_t, list);
2597 for (idx = 0; idx < 2; idx++) {
2598 subs = &as->substream[idx];
2599 if (!subs->num_formats)
2600 return;
2601 release_substream_urbs(subs, 1);
2602 subs->interface = -1;
2603 }
2604}
2605
2606/*
2607 * parse audio control descriptor and create pcm/midi streams
2608 */
2609static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)
2610{
2611 struct usb_device *dev = chip->dev;
2612 struct usb_host_interface *host_iface;
2613 struct usb_interface *iface;
2614 unsigned char *p1;
2615 int i, j;
2616
2617 /* find audiocontrol interface */
2618 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2619 if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
2620 snd_printk(KERN_ERR "cannot find HEADER\n");
2621 return -EINVAL;
2622 }
2623 if (! p1[7] || p1[0] < 8 + p1[7]) {
2624 snd_printk(KERN_ERR "invalid HEADER\n");
2625 return -EINVAL;
2626 }
2627
2628 /*
2629 * parse all USB audio streaming interfaces
2630 */
2631 for (i = 0; i < p1[7]; i++) {
2632 struct usb_host_interface *alts;
2633 struct usb_interface_descriptor *altsd;
2634 j = p1[8 + i];
2635 iface = usb_ifnum_to_if(dev, j);
2636 if (!iface) {
2637 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
2638 dev->devnum, ctrlif, j);
2639 continue;
2640 }
2641 if (usb_interface_claimed(iface)) {
2642 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
2643 continue;
2644 }
2645 alts = &iface->altsetting[0];
2646 altsd = get_iface_desc(alts);
2647 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
2648 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
2649 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
2650 if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
2651 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
2652 continue;
2653 }
2654 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2655 continue;
2656 }
2657 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2658 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2659 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
2660 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
2661 /* skip non-supported classes */
2662 continue;
2663 }
2664 if (! parse_audio_endpoints(chip, j)) {
2665 usb_set_interface(dev, j, 0); /* reset the current interface */
2666 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2667 }
2668 }
2669
2670 return 0;
2671}
2672
2673/*
2674 * create a stream for an endpoint/altsetting without proper descriptors
2675 */
2676static int create_fixed_stream_quirk(snd_usb_audio_t *chip,
2677 struct usb_interface *iface,
2678 const snd_usb_audio_quirk_t *quirk)
2679{
2680 struct audioformat *fp;
2681 struct usb_host_interface *alts;
2682 int stream, err;
2683 int *rate_table = NULL;
2684
2685 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2686 if (! fp) {
2687 snd_printk(KERN_ERR "cannot malloc\n");
2688 return -ENOMEM;
2689 }
2690 memcpy(fp, quirk->data, sizeof(*fp));
2691 if (fp->nr_rates > 0) {
2692 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
2693 if (!rate_table) {
2694 kfree(fp);
2695 return -ENOMEM;
2696 }
2697 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
2698 fp->rate_table = rate_table;
2699 }
2700
2701 stream = (fp->endpoint & USB_DIR_IN)
2702 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2703 err = add_audio_endpoint(chip, stream, fp);
2704 if (err < 0) {
2705 kfree(fp);
2706 kfree(rate_table);
2707 return err;
2708 }
2709 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
2710 fp->altset_idx >= iface->num_altsetting) {
2711 kfree(fp);
2712 kfree(rate_table);
2713 return -EINVAL;
2714 }
2715 alts = &iface->altsetting[fp->altset_idx];
2716 usb_set_interface(chip->dev, fp->iface, 0);
2717 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2718 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
2719 return 0;
2720}
2721
2722/*
2723 * create a stream for an interface with proper descriptors
2724 */
2725static int create_standard_interface_quirk(snd_usb_audio_t *chip,
2726 struct usb_interface *iface,
2727 const snd_usb_audio_quirk_t *quirk)
2728{
2729 struct usb_host_interface *alts;
2730 struct usb_interface_descriptor *altsd;
2731 int err;
2732
2733 alts = &iface->altsetting[0];
2734 altsd = get_iface_desc(alts);
2735 switch (quirk->type) {
2736 case QUIRK_AUDIO_STANDARD_INTERFACE:
2737 err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
2738 if (!err)
2739 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */
2740 break;
2741 case QUIRK_MIDI_STANDARD_INTERFACE:
2742 err = snd_usb_create_midi_interface(chip, iface, NULL);
2743 break;
2744 default:
2745 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2746 return -ENXIO;
2747 }
2748 if (err < 0) {
2749 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
2750 altsd->bInterfaceNumber, err);
2751 return err;
2752 }
2753 return 0;
2754}
2755
2756/*
2757 * Create a stream for an Edirol UA-700/UA-25 interface. The only way
2758 * to detect the sample rate is by looking at wMaxPacketSize.
2759 */
2760static int create_ua700_ua25_quirk(snd_usb_audio_t *chip,
2761 struct usb_interface *iface)
2762{
2763 static const struct audioformat ua_format = {
2764 .format = SNDRV_PCM_FORMAT_S24_3LE,
2765 .channels = 2,
2766 .fmt_type = USB_FORMAT_TYPE_I,
2767 .altsetting = 1,
2768 .altset_idx = 1,
2769 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2770 };
2771 struct usb_host_interface *alts;
2772 struct usb_interface_descriptor *altsd;
2773 struct audioformat *fp;
2774 int stream, err;
2775
2776 /* both PCM and MIDI interfaces have 2 altsettings */
2777 if (iface->num_altsetting != 2)
2778 return -ENXIO;
2779 alts = &iface->altsetting[1];
2780 altsd = get_iface_desc(alts);
2781
2782 if (altsd->bNumEndpoints == 2) {
2783 static const snd_usb_midi_endpoint_info_t ua700_ep = {
2784 .out_cables = 0x0003,
2785 .in_cables = 0x0003
2786 };
2787 static const snd_usb_audio_quirk_t ua700_quirk = {
2788 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2789 .data = &ua700_ep
2790 };
2791 static const snd_usb_midi_endpoint_info_t ua25_ep = {
2792 .out_cables = 0x0001,
2793 .in_cables = 0x0001
2794 };
2795 static const snd_usb_audio_quirk_t ua25_quirk = {
2796 .type = QUIRK_MIDI_FIXED_ENDPOINT,
2797 .data = &ua25_ep
2798 };
2799 if (le16_to_cpu(chip->dev->descriptor.idProduct) == 0x002b)
2800 return snd_usb_create_midi_interface(chip, iface,
2801 &ua700_quirk);
2802 else
2803 return snd_usb_create_midi_interface(chip, iface,
2804 &ua25_quirk);
2805 }
2806
2807 if (altsd->bNumEndpoints != 1)
2808 return -ENXIO;
2809
2810 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2811 if (!fp)
2812 return -ENOMEM;
2813 memcpy(fp, &ua_format, sizeof(*fp));
2814
2815 fp->iface = altsd->bInterfaceNumber;
2816 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2817 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2818 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2819
2820 switch (fp->maxpacksize) {
2821 case 0x120:
2822 fp->rate_max = fp->rate_min = 44100;
2823 break;
2824 case 0x138:
2825 case 0x140:
2826 fp->rate_max = fp->rate_min = 48000;
2827 break;
2828 case 0x258:
2829 case 0x260:
2830 fp->rate_max = fp->rate_min = 96000;
2831 break;
2832 default:
2833 snd_printk(KERN_ERR "unknown sample rate\n");
2834 kfree(fp);
2835 return -ENXIO;
2836 }
2837
2838 stream = (fp->endpoint & USB_DIR_IN)
2839 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2840 err = add_audio_endpoint(chip, stream, fp);
2841 if (err < 0) {
2842 kfree(fp);
2843 return err;
2844 }
2845 usb_set_interface(chip->dev, fp->iface, 0);
2846 return 0;
2847}
2848
2849/*
2850 * Create a stream for an Edirol UA-1000 interface.
2851 */
2852static int create_ua1000_quirk(snd_usb_audio_t *chip, struct usb_interface *iface)
2853{
2854 static const struct audioformat ua1000_format = {
2855 .format = SNDRV_PCM_FORMAT_S32_LE,
2856 .fmt_type = USB_FORMAT_TYPE_I,
2857 .altsetting = 1,
2858 .altset_idx = 1,
2859 .attributes = 0,
2860 .rates = SNDRV_PCM_RATE_CONTINUOUS,
2861 };
2862 struct usb_host_interface *alts;
2863 struct usb_interface_descriptor *altsd;
2864 struct audioformat *fp;
2865 int stream, err;
2866
2867 if (iface->num_altsetting != 2)
2868 return -ENXIO;
2869 alts = &iface->altsetting[1];
2870 altsd = get_iface_desc(alts);
2871 if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
2872 altsd->bNumEndpoints != 1)
2873 return -ENXIO;
2874
2875 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
2876 if (!fp)
2877 return -ENOMEM;
2878 memcpy(fp, &ua1000_format, sizeof(*fp));
2879
2880 fp->channels = alts->extra[4];
2881 fp->iface = altsd->bInterfaceNumber;
2882 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2883 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2884 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2885 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
2886
2887 stream = (fp->endpoint & USB_DIR_IN)
2888 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2889 err = add_audio_endpoint(chip, stream, fp);
2890 if (err < 0) {
2891 kfree(fp);
2892 return err;
2893 }
2894 /* FIXME: playback must be synchronized to capture */
2895 usb_set_interface(chip->dev, fp->iface, 0);
2896 return 0;
2897}
2898
2899static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2900 struct usb_interface *iface,
2901 const snd_usb_audio_quirk_t *quirk);
2902
2903/*
2904 * handle the quirks for the contained interfaces
2905 */
2906static int create_composite_quirk(snd_usb_audio_t *chip,
2907 struct usb_interface *iface,
2908 const snd_usb_audio_quirk_t *quirk)
2909{
2910 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
2911 int err;
2912
2913 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
2914 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
2915 if (!iface)
2916 continue;
2917 if (quirk->ifnum != probed_ifnum &&
2918 usb_interface_claimed(iface))
2919 continue;
2920 err = snd_usb_create_quirk(chip, iface, quirk);
2921 if (err < 0)
2922 return err;
2923 if (quirk->ifnum != probed_ifnum)
2924 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
2925 }
2926 return 0;
2927}
2928
2929
2930/*
2931 * boot quirks
2932 */
2933
2934#define EXTIGY_FIRMWARE_SIZE_OLD 794
2935#define EXTIGY_FIRMWARE_SIZE_NEW 483
2936
2937static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
2938{
2939 struct usb_host_config *config = dev->actconfig;
2940 int err;
2941
2942 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
2943 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
2944 snd_printdd("sending Extigy boot sequence...\n");
2945 /* Send message to force it to reconnect with full interface. */
2946 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
2947 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
2948 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
2949 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
2950 &dev->descriptor, sizeof(dev->descriptor));
2951 config = dev->actconfig;
2952 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
2953 err = usb_reset_configuration(dev);
2954 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
2955 snd_printdd("extigy_boot: new boot length = %d\n",
2956 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
2957 return -ENODEV; /* quit this anyway */
2958 }
2959 return 0;
2960}
2961
2962
2963/*
2964 * audio-interface quirks
2965 *
2966 * returns zero if no standard audio/MIDI parsing is needed.
2967 * returns a postive value if standard audio/midi interfaces are parsed
2968 * after this.
2969 * returns a negative value at error.
2970 */
2971static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2972 struct usb_interface *iface,
2973 const snd_usb_audio_quirk_t *quirk)
2974{
2975 switch (quirk->type) {
2976 case QUIRK_MIDI_FIXED_ENDPOINT:
2977 case QUIRK_MIDI_YAMAHA:
2978 case QUIRK_MIDI_MIDIMAN:
2979 case QUIRK_MIDI_NOVATION:
2980 case QUIRK_MIDI_MOTU:
2981 case QUIRK_MIDI_EMAGIC:
2982 return snd_usb_create_midi_interface(chip, iface, quirk);
2983 case QUIRK_COMPOSITE:
2984 return create_composite_quirk(chip, iface, quirk);
2985 case QUIRK_AUDIO_FIXED_ENDPOINT:
2986 return create_fixed_stream_quirk(chip, iface, quirk);
2987 case QUIRK_AUDIO_STANDARD_INTERFACE:
2988 case QUIRK_MIDI_STANDARD_INTERFACE:
2989 return create_standard_interface_quirk(chip, iface, quirk);
2990 case QUIRK_AUDIO_EDIROL_UA700_UA25:
2991 return create_ua700_ua25_quirk(chip, iface);
2992 case QUIRK_AUDIO_EDIROL_UA1000:
2993 return create_ua1000_quirk(chip, iface);
2994 case QUIRK_IGNORE_INTERFACE:
2995 return 0;
2996 default:
2997 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2998 return -ENXIO;
2999 }
3000}
3001
3002
3003/*
3004 * common proc files to show the usb device info
3005 */
3006static void proc_audio_usbbus_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3007{
3008 snd_usb_audio_t *chip = entry->private_data;
3009 if (! chip->shutdown)
3010 snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
3011}
3012
3013static void proc_audio_usbid_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
3014{
3015 snd_usb_audio_t *chip = entry->private_data;
3016 if (! chip->shutdown)
3017 snd_iprintf(buffer, "%04x:%04x\n",
3018 le16_to_cpu(chip->dev->descriptor.idVendor),
3019 le16_to_cpu(chip->dev->descriptor.idProduct));
3020}
3021
3022static void snd_usb_audio_create_proc(snd_usb_audio_t *chip)
3023{
3024 snd_info_entry_t *entry;
3025 if (! snd_card_proc_new(chip->card, "usbbus", &entry))
3026 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
3027 if (! snd_card_proc_new(chip->card, "usbid", &entry))
3028 snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
3029}
3030
3031/*
3032 * free the chip instance
3033 *
3034 * here we have to do not much, since pcm and controls are already freed
3035 *
3036 */
3037
3038static int snd_usb_audio_free(snd_usb_audio_t *chip)
3039{
3040 kfree(chip);
3041 return 0;
3042}
3043
3044static int snd_usb_audio_dev_free(snd_device_t *device)
3045{
3046 snd_usb_audio_t *chip = device->device_data;
3047 return snd_usb_audio_free(chip);
3048}
3049
3050
3051/*
3052 * create a chip instance and set its names.
3053 */
3054static int snd_usb_audio_create(struct usb_device *dev, int idx,
3055 const snd_usb_audio_quirk_t *quirk,
3056 snd_usb_audio_t **rchip)
3057{
3058 snd_card_t *card;
3059 snd_usb_audio_t *chip;
3060 int err, len;
3061 char component[14];
3062 static snd_device_ops_t ops = {
3063 .dev_free = snd_usb_audio_dev_free,
3064 };
3065
3066 *rchip = NULL;
3067
3068 if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
3069 snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
3070 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
3071 return -ENXIO;
3072 }
3073
3074 card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
3075 if (card == NULL) {
3076 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
3077 return -ENOMEM;
3078 }
3079
3080 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
3081 if (! chip) {
3082 snd_card_free(card);
3083 return -ENOMEM;
3084 }
3085
3086 chip->index = idx;
3087 chip->dev = dev;
3088 chip->card = card;
3089 INIT_LIST_HEAD(&chip->pcm_list);
3090 INIT_LIST_HEAD(&chip->midi_list);
3091
3092 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
3093 snd_usb_audio_free(chip);
3094 snd_card_free(card);
3095 return err;
3096 }
3097
3098 strcpy(card->driver, "USB-Audio");
3099 sprintf(component, "USB%04x:%04x",
3100 le16_to_cpu(dev->descriptor.idVendor),
3101 le16_to_cpu(dev->descriptor.idProduct));
3102 snd_component_add(card, component);
3103
3104 /* retrieve the device string as shortname */
3105 if (quirk && quirk->product_name) {
3106 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
3107 } else {
3108 if (!dev->descriptor.iProduct ||
3109 usb_string(dev, dev->descriptor.iProduct,
3110 card->shortname, sizeof(card->shortname)) <= 0) {
3111 /* no name available from anywhere, so use ID */
3112 sprintf(card->shortname, "USB Device %#04x:%#04x",
3113 le16_to_cpu(dev->descriptor.idVendor),
3114 le16_to_cpu(dev->descriptor.idProduct));
3115 }
3116 }
3117
3118 /* retrieve the vendor and device strings as longname */
3119 if (quirk && quirk->vendor_name) {
3120 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
3121 } else {
3122 if (dev->descriptor.iManufacturer)
3123 len = usb_string(dev, dev->descriptor.iManufacturer,
3124 card->longname, sizeof(card->longname));
3125 else
3126 len = 0;
3127 /* we don't really care if there isn't any vendor string */
3128 }
3129 if (len > 0)
3130 strlcat(card->longname, " ", sizeof(card->longname));
3131
3132 strlcat(card->longname, card->shortname, sizeof(card->longname));
3133
3134 len = strlcat(card->longname, " at ", sizeof(card->longname));
3135
3136 if (len < sizeof(card->longname))
3137 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
3138
3139 strlcat(card->longname,
3140 snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
3141 sizeof(card->longname));
3142
3143 snd_usb_audio_create_proc(chip);
3144
3145 snd_card_set_dev(card, &dev->dev);
3146
3147 *rchip = chip;
3148 return 0;
3149}
3150
3151
3152/*
3153 * probe the active usb device
3154 *
3155 * note that this can be called multiple times per a device, when it
3156 * includes multiple audio control interfaces.
3157 *
3158 * thus we check the usb device pointer and creates the card instance
3159 * only at the first time. the successive calls of this function will
3160 * append the pcm interface to the corresponding card.
3161 */
3162static void *snd_usb_audio_probe(struct usb_device *dev,
3163 struct usb_interface *intf,
3164 const struct usb_device_id *usb_id)
3165{
3166 struct usb_host_config *config = dev->actconfig;
3167 const snd_usb_audio_quirk_t *quirk = (const snd_usb_audio_quirk_t *)usb_id->driver_info;
3168 int i, err;
3169 snd_usb_audio_t *chip;
3170 struct usb_host_interface *alts;
3171 int ifnum;
3172
3173 alts = &intf->altsetting[0];
3174 ifnum = get_iface_desc(alts)->bInterfaceNumber;
3175
3176 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
3177 goto __err_val;
3178
3179 /* SB Extigy needs special boot-up sequence */
3180 /* if more models come, this will go to the quirk list. */
3181 if (le16_to_cpu(dev->descriptor.idVendor) == 0x041e &&
3182 le16_to_cpu(dev->descriptor.idProduct) == 0x3000) {
3183 if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
3184 goto __err_val;
3185 config = dev->actconfig;
3186 }
3187
3188 /*
3189 * found a config. now register to ALSA
3190 */
3191
3192 /* check whether it's already registered */
3193 chip = NULL;
3194 down(&register_mutex);
3195 for (i = 0; i < SNDRV_CARDS; i++) {
3196 if (usb_chip[i] && usb_chip[i]->dev == dev) {
3197 if (usb_chip[i]->shutdown) {
3198 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
3199 goto __error;
3200 }
3201 chip = usb_chip[i];
3202 break;
3203 }
3204 }
3205 if (! chip) {
3206 /* it's a fresh one.
3207 * now look for an empty slot and create a new card instance
3208 */
3209 /* first, set the current configuration for this device */
3210 if (usb_reset_configuration(dev) < 0) {
3211 snd_printk(KERN_ERR "cannot reset configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);
3212 goto __error;
3213 }
3214 for (i = 0; i < SNDRV_CARDS; i++)
3215 if (enable[i] && ! usb_chip[i] &&
3216 (vid[i] == -1 || vid[i] == le16_to_cpu(dev->descriptor.idVendor)) &&
3217 (pid[i] == -1 || pid[i] == le16_to_cpu(dev->descriptor.idProduct))) {
3218 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
3219 goto __error;
3220 }
3221 break;
3222 }
3223 if (! chip) {
3224 snd_printk(KERN_ERR "no available usb audio device\n");
3225 goto __error;
3226 }
3227 }
3228
3229 err = 1; /* continue */
3230 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
3231 /* need some special handlings */
3232 if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
3233 goto __error;
3234 }
3235
3236 if (err > 0) {
3237 /* create normal USB audio interfaces */
3238 if (snd_usb_create_streams(chip, ifnum) < 0 ||
3239 snd_usb_create_mixer(chip, ifnum) < 0) {
3240 goto __error;
3241 }
3242 }
3243
3244 /* we are allowed to call snd_card_register() many times */
3245 if (snd_card_register(chip->card) < 0) {
3246 goto __error;
3247 }
3248
3249 usb_chip[chip->index] = chip;
3250 chip->num_interfaces++;
3251 up(&register_mutex);
3252 return chip;
3253
3254 __error:
3255 if (chip && !chip->num_interfaces)
3256 snd_card_free(chip->card);
3257 up(&register_mutex);
3258 __err_val:
3259 return NULL;
3260}
3261
3262/*
3263 * we need to take care of counter, since disconnection can be called also
3264 * many times as well as usb_audio_probe().
3265 */
3266static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
3267{
3268 snd_usb_audio_t *chip;
3269 snd_card_t *card;
3270 struct list_head *p;
3271
3272 if (ptr == (void *)-1L)
3273 return;
3274
3275 chip = ptr;
3276 card = chip->card;
3277 down(&register_mutex);
3278 chip->shutdown = 1;
3279 chip->num_interfaces--;
3280 if (chip->num_interfaces <= 0) {
3281 snd_card_disconnect(card);
3282 /* release the pcm resources */
3283 list_for_each(p, &chip->pcm_list) {
3284 snd_usb_stream_disconnect(p, &usb_audio_driver);
3285 }
3286 /* release the midi resources */
3287 list_for_each(p, &chip->midi_list) {
3288 snd_usbmidi_disconnect(p, &usb_audio_driver);
3289 }
3290 usb_chip[chip->index] = NULL;
3291 up(&register_mutex);
3292 snd_card_free_in_thread(card);
3293 } else {
3294 up(&register_mutex);
3295 }
3296}
3297
3298/*
3299 * new 2.5 USB kernel API
3300 */
3301static int usb_audio_probe(struct usb_interface *intf,
3302 const struct usb_device_id *id)
3303{
3304 void *chip;
3305 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
3306 if (chip) {
3307 dev_set_drvdata(&intf->dev, chip);
3308 return 0;
3309 } else
3310 return -EIO;
3311}
3312
3313static void usb_audio_disconnect(struct usb_interface *intf)
3314{
3315 snd_usb_audio_disconnect(interface_to_usbdev(intf),
3316 dev_get_drvdata(&intf->dev));
3317}
3318
3319
3320static int __init snd_usb_audio_init(void)
3321{
3322 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
3323 printk(KERN_WARNING "invalid nrpacks value.\n");
3324 return -EINVAL;
3325 }
3326 usb_register(&usb_audio_driver);
3327 return 0;
3328}
3329
3330
3331static void __exit snd_usb_audio_cleanup(void)
3332{
3333 usb_deregister(&usb_audio_driver);
3334}
3335
3336module_init(snd_usb_audio_init);
3337module_exit(snd_usb_audio_cleanup);
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
new file mode 100644
index 000000000000..eecbf19fcb6f
--- /dev/null
+++ b/sound/usb/usbaudio.h
@@ -0,0 +1,251 @@
1#ifndef __USBAUDIO_H
2#define __USBAUDIO_H
3/*
4 * (Tentative) USB Audio Driver for ALSA
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25/*
26 */
27
28#define USB_SUBCLASS_AUDIO_CONTROL 0x01
29#define USB_SUBCLASS_AUDIO_STREAMING 0x02
30#define USB_SUBCLASS_MIDI_STREAMING 0x03
31#define USB_SUBCLASS_VENDOR_SPEC 0xff
32
33#define CS_AUDIO_UNDEFINED 0x20
34#define CS_AUDIO_DEVICE 0x21
35#define CS_AUDIO_CONFIGURATION 0x22
36#define CS_AUDIO_STRING 0x23
37#define CS_AUDIO_INTERFACE 0x24
38#define CS_AUDIO_ENDPOINT 0x25
39
40#define HEADER 0x01
41#define INPUT_TERMINAL 0x02
42#define OUTPUT_TERMINAL 0x03
43#define MIXER_UNIT 0x04
44#define SELECTOR_UNIT 0x05
45#define FEATURE_UNIT 0x06
46#define PROCESSING_UNIT 0x07
47#define EXTENSION_UNIT 0x08
48
49#define AS_GENERAL 0x01
50#define FORMAT_TYPE 0x02
51#define FORMAT_SPECIFIC 0x03
52
53#define EP_GENERAL 0x01
54
55#define MS_GENERAL 0x01
56#define MIDI_IN_JACK 0x02
57#define MIDI_OUT_JACK 0x03
58
59/* endpoint attributes */
60#define EP_ATTR_MASK 0x0c
61#define EP_ATTR_ASYNC 0x04
62#define EP_ATTR_ADAPTIVE 0x08
63#define EP_ATTR_SYNC 0x0c
64
65/* cs endpoint attributes */
66#define EP_CS_ATTR_SAMPLE_RATE 0x01
67#define EP_CS_ATTR_PITCH_CONTROL 0x02
68#define EP_CS_ATTR_FILL_MAX 0x80
69
70/* Audio Class specific Request Codes */
71
72#define SET_CUR 0x01
73#define GET_CUR 0x81
74#define SET_MIN 0x02
75#define GET_MIN 0x82
76#define SET_MAX 0x03
77#define GET_MAX 0x83
78#define SET_RES 0x04
79#define GET_RES 0x84
80#define SET_MEM 0x05
81#define GET_MEM 0x85
82#define GET_STAT 0xff
83
84/* Terminal Control Selectors */
85
86#define COPY_PROTECT_CONTROL 0x01
87
88/* Endpoint Control Selectors */
89
90#define SAMPLING_FREQ_CONTROL 0x01
91#define PITCH_CONTROL 0x02
92
93/* Format Types */
94#define USB_FORMAT_TYPE_I 0x01
95#define USB_FORMAT_TYPE_II 0x02
96#define USB_FORMAT_TYPE_III 0x03
97
98/* type I */
99#define USB_AUDIO_FORMAT_PCM 0x01
100#define USB_AUDIO_FORMAT_PCM8 0x02
101#define USB_AUDIO_FORMAT_IEEE_FLOAT 0x03
102#define USB_AUDIO_FORMAT_ALAW 0x04
103#define USB_AUDIO_FORMAT_MU_LAW 0x05
104
105/* type II */
106#define USB_AUDIO_FORMAT_MPEG 0x1001
107#define USB_AUDIO_FORMAT_AC3 0x1002
108
109/* type III */
110#define USB_AUDIO_FORMAT_IEC1937_AC3 0x2001
111#define USB_AUDIO_FORMAT_IEC1937_MPEG1_LAYER1 0x2002
112#define USB_AUDIO_FORMAT_IEC1937_MPEG2_NOEXT 0x2003
113#define USB_AUDIO_FORMAT_IEC1937_MPEG2_EXT 0x2004
114#define USB_AUDIO_FORMAT_IEC1937_MPEG2_LAYER1_LS 0x2005
115#define USB_AUDIO_FORMAT_IEC1937_MPEG2_LAYER23_LS 0x2006
116
117
118/* maximum number of endpoints per interface */
119#define MIDI_MAX_ENDPOINTS 2
120
121/*
122 */
123
124typedef struct snd_usb_audio snd_usb_audio_t;
125
126struct snd_usb_audio {
127 int index;
128 struct usb_device *dev;
129 snd_card_t *card;
130 int shutdown;
131 int num_interfaces;
132
133 struct list_head pcm_list; /* list of pcm streams */
134 int pcm_devs;
135
136 struct list_head midi_list; /* list of midi interfaces */
137 int next_midi_device;
138
139 unsigned int ignore_ctl_error; /* for mixer */
140};
141
142/*
143 * Information about devices with broken descriptors
144 */
145
146/* special values for .ifnum */
147#define QUIRK_NO_INTERFACE -2
148#define QUIRK_ANY_INTERFACE -1
149
150/* quirk type */
151#define QUIRK_MIDI_FIXED_ENDPOINT 0
152#define QUIRK_MIDI_YAMAHA 1
153#define QUIRK_MIDI_MIDIMAN 2
154#define QUIRK_COMPOSITE 3
155#define QUIRK_AUDIO_FIXED_ENDPOINT 4
156#define QUIRK_AUDIO_STANDARD_INTERFACE 5
157#define QUIRK_MIDI_STANDARD_INTERFACE 6
158#define QUIRK_AUDIO_EDIROL_UA700_UA25 7
159#define QUIRK_AUDIO_EDIROL_UA1000 8
160#define QUIRK_IGNORE_INTERFACE 9
161#define QUIRK_MIDI_NOVATION 10
162#define QUIRK_MIDI_MOTU 11
163#define QUIRK_MIDI_EMAGIC 12
164
165typedef struct snd_usb_audio_quirk snd_usb_audio_quirk_t;
166typedef struct snd_usb_midi_endpoint_info snd_usb_midi_endpoint_info_t;
167
168struct snd_usb_audio_quirk {
169 const char *vendor_name;
170 const char *product_name;
171 int16_t ifnum;
172 int16_t type;
173 const void *data;
174};
175
176/* data for QUIRK_MIDI_FIXED_ENDPOINT */
177struct snd_usb_midi_endpoint_info {
178 int8_t out_ep; /* ep number, 0 autodetect */
179 uint8_t out_interval; /* interval for interrupt endpoints */
180 int8_t in_ep;
181 uint8_t in_interval;
182 uint16_t out_cables; /* bitmask */
183 uint16_t in_cables; /* bitmask */
184};
185
186/* for QUIRK_MIDI_YAMAHA, data is NULL */
187
188/* for QUIRK_MIDI_MIDIMAN, data points to a snd_usb_midi_endpoint_info
189 * structure (out_cables and in_cables only) */
190
191/* for QUIRK_COMPOSITE, data points to an array of snd_usb_audio_quirk
192 * structures, terminated with .ifnum = -1 */
193
194/* for QUIRK_AUDIO_FIXED_ENDPOINT, data points to an audioformat structure */
195
196/* for QUIRK_AUDIO/MIDI_STANDARD_INTERFACE, data is NULL */
197
198/* for QUIRK_AUDIO_EDIROL_UA700_UA25/UA1000, data is NULL */
199
200/* for QUIRK_IGNORE_INTERFACE, data is NULL */
201
202/* for QUIRK_MIDI_NOVATION and _MOTU, data is NULL */
203
204/* for QUIRK_MIDI_EMAGIC, data points to a snd_usb_midi_endpoint_info
205 * structure (out_cables and in_cables only) */
206
207/*
208 */
209
210#define combine_word(s) ((*s) | ((unsigned int)(s)[1] << 8))
211#define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16))
212#define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24))
213
214unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size);
215
216void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype);
217void *snd_usb_find_csint_desc(void *descstart, int desclen, void *after, u8 dsubtype);
218
219int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype, __u16 value, __u16 index, void *data, __u16 size, int timeout);
220
221int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif);
222
223int snd_usb_create_midi_interface(snd_usb_audio_t *chip, struct usb_interface *iface, const snd_usb_audio_quirk_t *quirk);
224void snd_usbmidi_input_stop(struct list_head* p);
225void snd_usbmidi_input_start(struct list_head* p);
226void snd_usbmidi_disconnect(struct list_head *p, struct usb_driver *driver);
227
228/*
229 * retrieve usb_interface descriptor from the host interface
230 * (conditional for compatibility with the older API)
231 */
232#ifndef get_iface_desc
233#define get_iface_desc(iface) (&(iface)->desc)
234#define get_endpoint(alt,ep) (&(alt)->endpoint[ep].desc)
235#define get_ep_desc(ep) (&(ep)->desc)
236#define get_cfg_desc(cfg) (&(cfg)->desc)
237#endif
238
239#ifndef usb_pipe_needs_resubmit
240#define usb_pipe_needs_resubmit(pipe) 1
241#endif
242
243#ifndef snd_usb_complete_callback
244#define snd_usb_complete_callback(x) (x)
245#endif
246
247#ifndef snd_usb_get_speed
248#define snd_usb_get_speed(dev) ((dev)->speed)
249#endif
250
251#endif /* __USBAUDIO_H */
diff --git a/sound/usb/usbmidi.c b/sound/usb/usbmidi.c
new file mode 100644
index 000000000000..5d32857ff955
--- /dev/null
+++ b/sound/usb/usbmidi.c
@@ -0,0 +1,1564 @@
1/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
4 * Copyright (c) 2002-2005 Clemens Ladisch
5 * All rights reserved.
6 *
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
23 * version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sound/driver.h>
39#include <linux/kernel.h>
40#include <linux/types.h>
41#include <linux/bitops.h>
42#include <linux/interrupt.h>
43#include <linux/spinlock.h>
44#include <linux/string.h>
45#include <linux/init.h>
46#include <linux/slab.h>
47#include <linux/usb.h>
48#include <sound/core.h>
49#include <sound/minors.h>
50#include <sound/rawmidi.h>
51#include "usbaudio.h"
52
53
54/*
55 * define this to log all USB packets
56 */
57/* #define DUMP_PACKETS */
58
59
60MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
61MODULE_DESCRIPTION("USB Audio/MIDI helper module");
62MODULE_LICENSE("Dual BSD/GPL");
63
64
65struct usb_ms_header_descriptor {
66 __u8 bLength;
67 __u8 bDescriptorType;
68 __u8 bDescriptorSubtype;
69 __u8 bcdMSC[2];
70 __le16 wTotalLength;
71} __attribute__ ((packed));
72
73struct usb_ms_endpoint_descriptor {
74 __u8 bLength;
75 __u8 bDescriptorType;
76 __u8 bDescriptorSubtype;
77 __u8 bNumEmbMIDIJack;
78 __u8 baAssocJackID[0];
79} __attribute__ ((packed));
80
81typedef struct snd_usb_midi snd_usb_midi_t;
82typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t;
83typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t;
84typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t;
85typedef struct usbmidi_out_port usbmidi_out_port_t;
86typedef struct usbmidi_in_port usbmidi_in_port_t;
87
88struct usb_protocol_ops {
89 void (*input)(snd_usb_midi_in_endpoint_t*, uint8_t*, int);
90 void (*output)(snd_usb_midi_out_endpoint_t*);
91 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
92 void (*init_out_endpoint)(snd_usb_midi_out_endpoint_t*);
93 void (*finish_out_endpoint)(snd_usb_midi_out_endpoint_t*);
94};
95
96struct snd_usb_midi {
97 snd_usb_audio_t *chip;
98 struct usb_interface *iface;
99 const snd_usb_audio_quirk_t *quirk;
100 snd_rawmidi_t* rmidi;
101 struct usb_protocol_ops* usb_protocol_ops;
102 struct list_head list;
103
104 struct snd_usb_midi_endpoint {
105 snd_usb_midi_out_endpoint_t *out;
106 snd_usb_midi_in_endpoint_t *in;
107 } endpoints[MIDI_MAX_ENDPOINTS];
108 unsigned long input_triggered;
109};
110
111struct snd_usb_midi_out_endpoint {
112 snd_usb_midi_t* umidi;
113 struct urb* urb;
114 int urb_active;
115 int max_transfer; /* size of urb buffer */
116 struct tasklet_struct tasklet;
117
118 spinlock_t buffer_lock;
119
120 struct usbmidi_out_port {
121 snd_usb_midi_out_endpoint_t* ep;
122 snd_rawmidi_substream_t* substream;
123 int active;
124 uint8_t cable; /* cable number << 4 */
125 uint8_t state;
126#define STATE_UNKNOWN 0
127#define STATE_1PARAM 1
128#define STATE_2PARAM_1 2
129#define STATE_2PARAM_2 3
130#define STATE_SYSEX_0 4
131#define STATE_SYSEX_1 5
132#define STATE_SYSEX_2 6
133 uint8_t data[2];
134 } ports[0x10];
135 int current_port;
136};
137
138struct snd_usb_midi_in_endpoint {
139 snd_usb_midi_t* umidi;
140 struct urb* urb;
141 struct usbmidi_in_port {
142 snd_rawmidi_substream_t* substream;
143 } ports[0x10];
144 int seen_f5;
145 int current_port;
146};
147
148static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep);
149
150static const uint8_t snd_usbmidi_cin_length[] = {
151 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
152};
153
154/*
155 * Submits the URB, with error handling.
156 */
157static int snd_usbmidi_submit_urb(struct urb* urb, int flags)
158{
159 int err = usb_submit_urb(urb, flags);
160 if (err < 0 && err != -ENODEV)
161 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
162 return err;
163}
164
165/*
166 * Error handling for URB completion functions.
167 */
168static int snd_usbmidi_urb_error(int status)
169{
170 if (status == -ENOENT)
171 return status; /* killed */
172 if (status == -EILSEQ ||
173 status == -ECONNRESET ||
174 status == -ETIMEDOUT)
175 return -ENODEV; /* device removed/shutdown */
176 snd_printk(KERN_ERR "urb status %d\n", status);
177 return 0; /* continue */
178}
179
180/*
181 * Receives a chunk of MIDI data.
182 */
183static void snd_usbmidi_input_data(snd_usb_midi_in_endpoint_t* ep, int portidx,
184 uint8_t* data, int length)
185{
186 usbmidi_in_port_t* port = &ep->ports[portidx];
187
188 if (!port->substream) {
189 snd_printd("unexpected port %d!\n", portidx);
190 return;
191 }
192 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
193 return;
194 snd_rawmidi_receive(port->substream, data, length);
195}
196
197#ifdef DUMP_PACKETS
198static void dump_urb(const char *type, const u8 *data, int length)
199{
200 snd_printk(KERN_DEBUG "%s packet: [", type);
201 for (; length > 0; ++data, --length)
202 printk(" %02x", *data);
203 printk(" ]\n");
204}
205#else
206#define dump_urb(type, data, length) /* nothing */
207#endif
208
209/*
210 * Processes the data read from the device.
211 */
212static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
213{
214 snd_usb_midi_in_endpoint_t* ep = urb->context;
215
216 if (urb->status == 0) {
217 dump_urb("received", urb->transfer_buffer, urb->actual_length);
218 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
219 urb->actual_length);
220 } else {
221 if (snd_usbmidi_urb_error(urb->status) < 0)
222 return;
223 }
224
225 if (usb_pipe_needs_resubmit(urb->pipe)) {
226 urb->dev = ep->umidi->chip->dev;
227 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
228 }
229}
230
231static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
232{
233 snd_usb_midi_out_endpoint_t* ep = urb->context;
234
235 spin_lock(&ep->buffer_lock);
236 ep->urb_active = 0;
237 spin_unlock(&ep->buffer_lock);
238 if (urb->status < 0) {
239 if (snd_usbmidi_urb_error(urb->status) < 0)
240 return;
241 }
242 snd_usbmidi_do_output(ep);
243}
244
245/*
246 * This is called when some data should be transferred to the device
247 * (from one or more substreams).
248 */
249static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep)
250{
251 struct urb* urb = ep->urb;
252 unsigned long flags;
253
254 spin_lock_irqsave(&ep->buffer_lock, flags);
255 if (ep->urb_active || ep->umidi->chip->shutdown) {
256 spin_unlock_irqrestore(&ep->buffer_lock, flags);
257 return;
258 }
259
260 urb->transfer_buffer_length = 0;
261 ep->umidi->usb_protocol_ops->output(ep);
262
263 if (urb->transfer_buffer_length > 0) {
264 dump_urb("sending", urb->transfer_buffer,
265 urb->transfer_buffer_length);
266 urb->dev = ep->umidi->chip->dev;
267 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
268 }
269 spin_unlock_irqrestore(&ep->buffer_lock, flags);
270}
271
272static void snd_usbmidi_out_tasklet(unsigned long data)
273{
274 snd_usb_midi_out_endpoint_t* ep = (snd_usb_midi_out_endpoint_t *) data;
275
276 snd_usbmidi_do_output(ep);
277}
278
279/* helper function to send static data that may not DMA-able */
280static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep,
281 const void *data, int len)
282{
283 int err;
284 void *buf = kmalloc(len, GFP_KERNEL);
285 if (!buf)
286 return -ENOMEM;
287 memcpy(buf, data, len);
288 dump_urb("sending", buf, len);
289 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
290 NULL, 250);
291 kfree(buf);
292 return err;
293}
294
295/*
296 * Standard USB MIDI protocol: see the spec.
297 * Midiman protocol: like the standard protocol, but the control byte is the
298 * fourth byte in each packet, and uses length instead of CIN.
299 */
300
301static void snd_usbmidi_standard_input(snd_usb_midi_in_endpoint_t* ep,
302 uint8_t* buffer, int buffer_length)
303{
304 int i;
305
306 for (i = 0; i + 3 < buffer_length; i += 4)
307 if (buffer[i] != 0) {
308 int cable = buffer[i] >> 4;
309 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
310 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
311 }
312}
313
314static void snd_usbmidi_midiman_input(snd_usb_midi_in_endpoint_t* ep,
315 uint8_t* buffer, int buffer_length)
316{
317 int i;
318
319 for (i = 0; i + 3 < buffer_length; i += 4)
320 if (buffer[i + 3] != 0) {
321 int port = buffer[i + 3] >> 4;
322 int length = buffer[i + 3] & 3;
323 snd_usbmidi_input_data(ep, port, &buffer[i], length);
324 }
325}
326
327/*
328 * Adds one USB MIDI packet to the output buffer.
329 */
330static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
331 uint8_t p1, uint8_t p2, uint8_t p3)
332{
333
334 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
335 buf[0] = p0;
336 buf[1] = p1;
337 buf[2] = p2;
338 buf[3] = p3;
339 urb->transfer_buffer_length += 4;
340}
341
342/*
343 * Adds one Midiman packet to the output buffer.
344 */
345static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
346 uint8_t p1, uint8_t p2, uint8_t p3)
347{
348
349 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
350 buf[0] = p1;
351 buf[1] = p2;
352 buf[2] = p3;
353 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
354 urb->transfer_buffer_length += 4;
355}
356
357/*
358 * Converts MIDI commands to USB MIDI packets.
359 */
360static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
361 uint8_t b, struct urb* urb)
362{
363 uint8_t p0 = port->cable;
364 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
365 port->ep->umidi->usb_protocol_ops->output_packet;
366
367 if (b >= 0xf8) {
368 output_packet(urb, p0 | 0x0f, b, 0, 0);
369 } else if (b >= 0xf0) {
370 switch (b) {
371 case 0xf0:
372 port->data[0] = b;
373 port->state = STATE_SYSEX_1;
374 break;
375 case 0xf1:
376 case 0xf3:
377 port->data[0] = b;
378 port->state = STATE_1PARAM;
379 break;
380 case 0xf2:
381 port->data[0] = b;
382 port->state = STATE_2PARAM_1;
383 break;
384 case 0xf4:
385 case 0xf5:
386 port->state = STATE_UNKNOWN;
387 break;
388 case 0xf6:
389 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
390 port->state = STATE_UNKNOWN;
391 break;
392 case 0xf7:
393 switch (port->state) {
394 case STATE_SYSEX_0:
395 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
396 break;
397 case STATE_SYSEX_1:
398 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
399 break;
400 case STATE_SYSEX_2:
401 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
402 break;
403 }
404 port->state = STATE_UNKNOWN;
405 break;
406 }
407 } else if (b >= 0x80) {
408 port->data[0] = b;
409 if (b >= 0xc0 && b <= 0xdf)
410 port->state = STATE_1PARAM;
411 else
412 port->state = STATE_2PARAM_1;
413 } else { /* b < 0x80 */
414 switch (port->state) {
415 case STATE_1PARAM:
416 if (port->data[0] < 0xf0) {
417 p0 |= port->data[0] >> 4;
418 } else {
419 p0 |= 0x02;
420 port->state = STATE_UNKNOWN;
421 }
422 output_packet(urb, p0, port->data[0], b, 0);
423 break;
424 case STATE_2PARAM_1:
425 port->data[1] = b;
426 port->state = STATE_2PARAM_2;
427 break;
428 case STATE_2PARAM_2:
429 if (port->data[0] < 0xf0) {
430 p0 |= port->data[0] >> 4;
431 port->state = STATE_2PARAM_1;
432 } else {
433 p0 |= 0x03;
434 port->state = STATE_UNKNOWN;
435 }
436 output_packet(urb, p0, port->data[0], port->data[1], b);
437 break;
438 case STATE_SYSEX_0:
439 port->data[0] = b;
440 port->state = STATE_SYSEX_1;
441 break;
442 case STATE_SYSEX_1:
443 port->data[1] = b;
444 port->state = STATE_SYSEX_2;
445 break;
446 case STATE_SYSEX_2:
447 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
448 port->state = STATE_SYSEX_0;
449 break;
450 }
451 }
452}
453
454static void snd_usbmidi_standard_output(snd_usb_midi_out_endpoint_t* ep)
455{
456 struct urb* urb = ep->urb;
457 int p;
458
459 /* FIXME: lower-numbered ports can starve higher-numbered ports */
460 for (p = 0; p < 0x10; ++p) {
461 usbmidi_out_port_t* port = &ep->ports[p];
462 if (!port->active)
463 continue;
464 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
465 uint8_t b;
466 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
467 port->active = 0;
468 break;
469 }
470 snd_usbmidi_transmit_byte(port, b, urb);
471 }
472 }
473}
474
475static struct usb_protocol_ops snd_usbmidi_standard_ops = {
476 .input = snd_usbmidi_standard_input,
477 .output = snd_usbmidi_standard_output,
478 .output_packet = snd_usbmidi_output_standard_packet,
479};
480
481static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
482 .input = snd_usbmidi_midiman_input,
483 .output = snd_usbmidi_standard_output,
484 .output_packet = snd_usbmidi_output_midiman_packet,
485};
486
487/*
488 * Novation USB MIDI protocol: number of data bytes is in the first byte
489 * (when receiving) (+1!) or in the second byte (when sending); data begins
490 * at the third byte.
491 */
492
493static void snd_usbmidi_novation_input(snd_usb_midi_in_endpoint_t* ep,
494 uint8_t* buffer, int buffer_length)
495{
496 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
497 return;
498 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
499}
500
501static void snd_usbmidi_novation_output(snd_usb_midi_out_endpoint_t* ep)
502{
503 uint8_t* transfer_buffer;
504 int count;
505
506 if (!ep->ports[0].active)
507 return;
508 transfer_buffer = ep->urb->transfer_buffer;
509 count = snd_rawmidi_transmit(ep->ports[0].substream,
510 &transfer_buffer[2],
511 ep->max_transfer - 2);
512 if (count < 1) {
513 ep->ports[0].active = 0;
514 return;
515 }
516 transfer_buffer[0] = 0;
517 transfer_buffer[1] = count;
518 ep->urb->transfer_buffer_length = 2 + count;
519}
520
521static struct usb_protocol_ops snd_usbmidi_novation_ops = {
522 .input = snd_usbmidi_novation_input,
523 .output = snd_usbmidi_novation_output,
524};
525
526/*
527 * Mark of the Unicorn USB MIDI protocol: raw MIDI.
528 */
529
530static void snd_usbmidi_motu_input(snd_usb_midi_in_endpoint_t* ep,
531 uint8_t* buffer, int buffer_length)
532{
533 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
534}
535
536static void snd_usbmidi_motu_output(snd_usb_midi_out_endpoint_t* ep)
537{
538 int count;
539
540 if (!ep->ports[0].active)
541 return;
542 count = snd_rawmidi_transmit(ep->ports[0].substream,
543 ep->urb->transfer_buffer,
544 ep->max_transfer);
545 if (count < 1) {
546 ep->ports[0].active = 0;
547 return;
548 }
549 ep->urb->transfer_buffer_length = count;
550}
551
552static struct usb_protocol_ops snd_usbmidi_motu_ops = {
553 .input = snd_usbmidi_motu_input,
554 .output = snd_usbmidi_motu_output,
555};
556
557/*
558 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
559 */
560
561static void snd_usbmidi_emagic_init_out(snd_usb_midi_out_endpoint_t* ep)
562{
563 static const u8 init_data[] = {
564 /* initialization magic: "get version" */
565 0xf0,
566 0x00, 0x20, 0x31, /* Emagic */
567 0x64, /* Unitor8 */
568 0x0b, /* version number request */
569 0x00, /* command version */
570 0x00, /* EEPROM, box 0 */
571 0xf7
572 };
573 send_bulk_static_data(ep, init_data, sizeof(init_data));
574 /* while we're at it, pour on more magic */
575 send_bulk_static_data(ep, init_data, sizeof(init_data));
576}
577
578static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t* ep)
579{
580 static const u8 finish_data[] = {
581 /* switch to patch mode with last preset */
582 0xf0,
583 0x00, 0x20, 0x31, /* Emagic */
584 0x64, /* Unitor8 */
585 0x10, /* patch switch command */
586 0x00, /* command version */
587 0x7f, /* to all boxes */
588 0x40, /* last preset in EEPROM */
589 0xf7
590 };
591 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
592}
593
594static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep,
595 uint8_t* buffer, int buffer_length)
596{
597 /* ignore padding bytes at end of buffer */
598 while (buffer_length > 0 && buffer[buffer_length - 1] == 0xff)
599 --buffer_length;
600
601 /* handle F5 at end of last buffer */
602 if (ep->seen_f5)
603 goto switch_port;
604
605 while (buffer_length > 0) {
606 int i;
607
608 /* determine size of data until next F5 */
609 for (i = 0; i < buffer_length; ++i)
610 if (buffer[i] == 0xf5)
611 break;
612 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
613 buffer += i;
614 buffer_length -= i;
615
616 if (buffer_length <= 0)
617 break;
618 /* assert(buffer[0] == 0xf5); */
619 ep->seen_f5 = 1;
620 ++buffer;
621 --buffer_length;
622
623 switch_port:
624 if (buffer_length <= 0)
625 break;
626 if (buffer[0] < 0x80) {
627 ep->current_port = (buffer[0] - 1) & 15;
628 ++buffer;
629 --buffer_length;
630 }
631 ep->seen_f5 = 0;
632 }
633}
634
635static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t* ep)
636{
637 int port0 = ep->current_port;
638 uint8_t* buf = ep->urb->transfer_buffer;
639 int buf_free = ep->max_transfer;
640 int length, i;
641
642 for (i = 0; i < 0x10; ++i) {
643 /* round-robin, starting at the last current port */
644 int portnum = (port0 + i) & 15;
645 usbmidi_out_port_t* port = &ep->ports[portnum];
646
647 if (!port->active)
648 continue;
649 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
650 port->active = 0;
651 continue;
652 }
653
654 if (portnum != ep->current_port) {
655 if (buf_free < 2)
656 break;
657 ep->current_port = portnum;
658 buf[0] = 0xf5;
659 buf[1] = (portnum + 1) & 15;
660 buf += 2;
661 buf_free -= 2;
662 }
663
664 if (buf_free < 1)
665 break;
666 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
667 if (length > 0) {
668 buf += length;
669 buf_free -= length;
670 if (buf_free < 1)
671 break;
672 }
673 }
674 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
675}
676
677static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
678 .input = snd_usbmidi_emagic_input,
679 .output = snd_usbmidi_emagic_output,
680 .init_out_endpoint = snd_usbmidi_emagic_init_out,
681 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
682};
683
684
685static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream)
686{
687 snd_usb_midi_t* umidi = substream->rmidi->private_data;
688 usbmidi_out_port_t* port = NULL;
689 int i, j;
690
691 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
692 if (umidi->endpoints[i].out)
693 for (j = 0; j < 0x10; ++j)
694 if (umidi->endpoints[i].out->ports[j].substream == substream) {
695 port = &umidi->endpoints[i].out->ports[j];
696 break;
697 }
698 if (!port) {
699 snd_BUG();
700 return -ENXIO;
701 }
702 substream->runtime->private_data = port;
703 port->state = STATE_UNKNOWN;
704 return 0;
705}
706
707static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream)
708{
709 return 0;
710}
711
712static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up)
713{
714 usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data;
715
716 port->active = up;
717 if (up) {
718 if (port->ep->umidi->chip->shutdown) {
719 /* gobble up remaining bytes to prevent wait in
720 * snd_rawmidi_drain_output */
721 while (!snd_rawmidi_transmit_empty(substream))
722 snd_rawmidi_transmit_ack(substream, 1);
723 return;
724 }
725 tasklet_hi_schedule(&port->ep->tasklet);
726 }
727}
728
729static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream)
730{
731 return 0;
732}
733
734static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream)
735{
736 return 0;
737}
738
739static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up)
740{
741 snd_usb_midi_t* umidi = substream->rmidi->private_data;
742
743 if (up)
744 set_bit(substream->number, &umidi->input_triggered);
745 else
746 clear_bit(substream->number, &umidi->input_triggered);
747}
748
749static snd_rawmidi_ops_t snd_usbmidi_output_ops = {
750 .open = snd_usbmidi_output_open,
751 .close = snd_usbmidi_output_close,
752 .trigger = snd_usbmidi_output_trigger,
753};
754
755static snd_rawmidi_ops_t snd_usbmidi_input_ops = {
756 .open = snd_usbmidi_input_open,
757 .close = snd_usbmidi_input_close,
758 .trigger = snd_usbmidi_input_trigger
759};
760
761/*
762 * Frees an input endpoint.
763 * May be called when ep hasn't been initialized completely.
764 */
765static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep)
766{
767 if (ep->urb) {
768 kfree(ep->urb->transfer_buffer);
769 usb_free_urb(ep->urb);
770 }
771 kfree(ep);
772}
773
774/*
775 * Creates an input endpoint.
776 */
777static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi,
778 snd_usb_midi_endpoint_info_t* ep_info,
779 snd_usb_midi_endpoint_t* rep)
780{
781 snd_usb_midi_in_endpoint_t* ep;
782 void* buffer;
783 unsigned int pipe;
784 int length;
785
786 rep->in = NULL;
787 ep = kcalloc(1, sizeof(*ep), GFP_KERNEL);
788 if (!ep)
789 return -ENOMEM;
790 ep->umidi = umidi;
791
792 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
793 if (!ep->urb) {
794 snd_usbmidi_in_endpoint_delete(ep);
795 return -ENOMEM;
796 }
797 if (ep_info->in_interval)
798 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
799 else
800 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
801 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
802 buffer = kmalloc(length, GFP_KERNEL);
803 if (!buffer) {
804 snd_usbmidi_in_endpoint_delete(ep);
805 return -ENOMEM;
806 }
807 if (ep_info->in_interval)
808 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
809 snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
810 ep, ep_info->in_interval);
811 else
812 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
813 snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
814 ep);
815
816 rep->in = ep;
817 return 0;
818}
819
820static unsigned int snd_usbmidi_count_bits(unsigned int x)
821{
822 unsigned int bits = 0;
823
824 for (; x; x >>= 1)
825 bits += x & 1;
826 return bits;
827}
828
829/*
830 * Frees an output endpoint.
831 * May be called when ep hasn't been initialized completely.
832 */
833static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep)
834{
835 if (ep->tasklet.func)
836 tasklet_kill(&ep->tasklet);
837 if (ep->urb) {
838 kfree(ep->urb->transfer_buffer);
839 usb_free_urb(ep->urb);
840 }
841 kfree(ep);
842}
843
844/*
845 * Creates an output endpoint, and initializes output ports.
846 */
847static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi,
848 snd_usb_midi_endpoint_info_t* ep_info,
849 snd_usb_midi_endpoint_t* rep)
850{
851 snd_usb_midi_out_endpoint_t* ep;
852 int i;
853 unsigned int pipe;
854 void* buffer;
855
856 rep->out = NULL;
857 ep = kcalloc(1, sizeof(*ep), GFP_KERNEL);
858 if (!ep)
859 return -ENOMEM;
860 ep->umidi = umidi;
861
862 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
863 if (!ep->urb) {
864 snd_usbmidi_out_endpoint_delete(ep);
865 return -ENOMEM;
866 }
867 /* we never use interrupt output pipes */
868 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
869 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
870 buffer = kmalloc(ep->max_transfer, GFP_KERNEL);
871 if (!buffer) {
872 snd_usbmidi_out_endpoint_delete(ep);
873 return -ENOMEM;
874 }
875 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
876 ep->max_transfer,
877 snd_usb_complete_callback(snd_usbmidi_out_urb_complete), ep);
878
879 spin_lock_init(&ep->buffer_lock);
880 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
881
882 for (i = 0; i < 0x10; ++i)
883 if (ep_info->out_cables & (1 << i)) {
884 ep->ports[i].ep = ep;
885 ep->ports[i].cable = i << 4;
886 }
887
888 if (umidi->usb_protocol_ops->init_out_endpoint)
889 umidi->usb_protocol_ops->init_out_endpoint(ep);
890
891 rep->out = ep;
892 return 0;
893}
894
895/*
896 * Frees everything.
897 */
898static void snd_usbmidi_free(snd_usb_midi_t* umidi)
899{
900 int i;
901
902 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
903 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
904 if (ep->out)
905 snd_usbmidi_out_endpoint_delete(ep->out);
906 if (ep->in)
907 snd_usbmidi_in_endpoint_delete(ep->in);
908 }
909 kfree(umidi);
910}
911
912/*
913 * Unlinks all URBs (must be done before the usb_device is deleted).
914 */
915void snd_usbmidi_disconnect(struct list_head* p, struct usb_driver *driver)
916{
917 snd_usb_midi_t* umidi;
918 int i;
919
920 umidi = list_entry(p, snd_usb_midi_t, list);
921 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
922 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
923 if (ep->out && ep->out->urb) {
924 usb_kill_urb(ep->out->urb);
925 if (umidi->usb_protocol_ops->finish_out_endpoint)
926 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
927 }
928 if (ep->in && ep->in->urb)
929 usb_kill_urb(ep->in->urb);
930 }
931}
932
933static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi)
934{
935 snd_usb_midi_t* umidi = rmidi->private_data;
936 snd_usbmidi_free(umidi);
937}
938
939static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi,
940 int stream, int number)
941{
942 struct list_head* list;
943
944 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
945 snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list);
946 if (substream->number == number)
947 return substream;
948 }
949 return NULL;
950}
951
952/*
953 * This list specifies names for ports that do not fit into the standard
954 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
955 * such as internal control or synthesizer ports.
956 */
957static struct {
958 __u16 vendor;
959 __u16 product;
960 int port;
961 const char *name_format;
962} snd_usbmidi_port_names[] = {
963 /* Roland UA-100 */
964 {0x0582, 0x0000, 2, "%s Control"},
965 /* Roland SC-8850 */
966 {0x0582, 0x0003, 0, "%s Part A"},
967 {0x0582, 0x0003, 1, "%s Part B"},
968 {0x0582, 0x0003, 2, "%s Part C"},
969 {0x0582, 0x0003, 3, "%s Part D"},
970 {0x0582, 0x0003, 4, "%s MIDI 1"},
971 {0x0582, 0x0003, 5, "%s MIDI 2"},
972 /* Roland U-8 */
973 {0x0582, 0x0004, 0, "%s MIDI"},
974 {0x0582, 0x0004, 1, "%s Control"},
975 /* Roland SC-8820 */
976 {0x0582, 0x0007, 0, "%s Part A"},
977 {0x0582, 0x0007, 1, "%s Part B"},
978 {0x0582, 0x0007, 2, "%s MIDI"},
979 /* Roland SK-500 */
980 {0x0582, 0x000b, 0, "%s Part A"},
981 {0x0582, 0x000b, 1, "%s Part B"},
982 {0x0582, 0x000b, 2, "%s MIDI"},
983 /* Roland SC-D70 */
984 {0x0582, 0x000c, 0, "%s Part A"},
985 {0x0582, 0x000c, 1, "%s Part B"},
986 {0x0582, 0x000c, 2, "%s MIDI"},
987 /* Edirol UM-880 */
988 {0x0582, 0x0014, 8, "%s Control"},
989 /* Edirol SD-90 */
990 {0x0582, 0x0016, 0, "%s Part A"},
991 {0x0582, 0x0016, 1, "%s Part B"},
992 {0x0582, 0x0016, 2, "%s MIDI 1"},
993 {0x0582, 0x0016, 3, "%s MIDI 2"},
994 /* Edirol UM-550 */
995 {0x0582, 0x0023, 5, "%s Control"},
996 /* Edirol SD-20 */
997 {0x0582, 0x0027, 0, "%s Part A"},
998 {0x0582, 0x0027, 1, "%s Part B"},
999 {0x0582, 0x0027, 2, "%s MIDI"},
1000 /* Edirol SD-80 */
1001 {0x0582, 0x0029, 0, "%s Part A"},
1002 {0x0582, 0x0029, 1, "%s Part B"},
1003 {0x0582, 0x0029, 2, "%s MIDI 1"},
1004 {0x0582, 0x0029, 3, "%s MIDI 2"},
1005 /* Edirol UA-700 */
1006 {0x0582, 0x002b, 0, "%s MIDI"},
1007 {0x0582, 0x002b, 1, "%s Control"},
1008 /* Roland VariOS */
1009 {0x0582, 0x002f, 0, "%s MIDI"},
1010 {0x0582, 0x002f, 1, "%s External MIDI"},
1011 {0x0582, 0x002f, 2, "%s Sync"},
1012 /* Edirol PCR */
1013 {0x0582, 0x0033, 0, "%s MIDI"},
1014 {0x0582, 0x0033, 1, "%s 1"},
1015 {0x0582, 0x0033, 2, "%s 2"},
1016 /* BOSS GS-10 */
1017 {0x0582, 0x003b, 0, "%s MIDI"},
1018 {0x0582, 0x003b, 1, "%s Control"},
1019 /* Edirol UA-1000 */
1020 {0x0582, 0x0044, 0, "%s MIDI"},
1021 {0x0582, 0x0044, 1, "%s Control"},
1022 /* Edirol UR-80 */
1023 {0x0582, 0x0048, 0, "%s MIDI"},
1024 {0x0582, 0x0048, 1, "%s 1"},
1025 {0x0582, 0x0048, 2, "%s 2"},
1026 /* Edirol PCR-A */
1027 {0x0582, 0x004d, 0, "%s MIDI"},
1028 {0x0582, 0x004d, 1, "%s 1"},
1029 {0x0582, 0x004d, 2, "%s 2"},
1030 /* M-Audio MidiSport 8x8 */
1031 {0x0763, 0x1031, 8, "%s Control"},
1032 {0x0763, 0x1033, 8, "%s Control"},
1033 /* MOTU Fastlane */
1034 {0x07fd, 0x0001, 0, "%s MIDI A"},
1035 {0x07fd, 0x0001, 1, "%s MIDI B"},
1036 /* Emagic Unitor8/AMT8/MT4 */
1037 {0x086a, 0x0001, 8, "%s Broadcast"},
1038 {0x086a, 0x0002, 8, "%s Broadcast"},
1039 {0x086a, 0x0003, 4, "%s Broadcast"},
1040};
1041
1042static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi,
1043 int stream, int number,
1044 snd_rawmidi_substream_t** rsubstream)
1045{
1046 int i;
1047 __u16 vendor, product;
1048 const char *name_format;
1049
1050 snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number);
1051 if (!substream) {
1052 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1053 return;
1054 }
1055
1056 /* TODO: read port name from jack descriptor */
1057 name_format = "%s MIDI %d";
1058 vendor = le16_to_cpu(umidi->chip->dev->descriptor.idVendor);
1059 product = le16_to_cpu(umidi->chip->dev->descriptor.idProduct);
1060 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
1061 if (snd_usbmidi_port_names[i].vendor == vendor &&
1062 snd_usbmidi_port_names[i].product == product &&
1063 snd_usbmidi_port_names[i].port == number) {
1064 name_format = snd_usbmidi_port_names[i].name_format;
1065 break;
1066 }
1067 }
1068 snprintf(substream->name, sizeof(substream->name),
1069 name_format, umidi->chip->card->shortname, number + 1);
1070
1071 *rsubstream = substream;
1072}
1073
1074/*
1075 * Creates the endpoints and their ports.
1076 */
1077static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi,
1078 snd_usb_midi_endpoint_info_t* endpoints)
1079{
1080 int i, j, err;
1081 int out_ports = 0, in_ports = 0;
1082
1083 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1084 if (endpoints[i].out_cables) {
1085 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1086 &umidi->endpoints[i]);
1087 if (err < 0)
1088 return err;
1089 }
1090 if (endpoints[i].in_cables) {
1091 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1092 &umidi->endpoints[i]);
1093 if (err < 0)
1094 return err;
1095 }
1096
1097 for (j = 0; j < 0x10; ++j) {
1098 if (endpoints[i].out_cables & (1 << j)) {
1099 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1100 &umidi->endpoints[i].out->ports[j].substream);
1101 ++out_ports;
1102 }
1103 if (endpoints[i].in_cables & (1 << j)) {
1104 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1105 &umidi->endpoints[i].in->ports[j].substream);
1106 ++in_ports;
1107 }
1108 }
1109 }
1110 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1111 out_ports, in_ports);
1112 return 0;
1113}
1114
1115/*
1116 * Returns MIDIStreaming device capabilities.
1117 */
1118static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi,
1119 snd_usb_midi_endpoint_info_t* endpoints)
1120{
1121 struct usb_interface* intf;
1122 struct usb_host_interface *hostif;
1123 struct usb_interface_descriptor* intfd;
1124 struct usb_ms_header_descriptor* ms_header;
1125 struct usb_host_endpoint *hostep;
1126 struct usb_endpoint_descriptor* ep;
1127 struct usb_ms_endpoint_descriptor* ms_ep;
1128 int i, epidx;
1129
1130 intf = umidi->iface;
1131 if (!intf)
1132 return -ENXIO;
1133 hostif = &intf->altsetting[0];
1134 intfd = get_iface_desc(hostif);
1135 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1136 if (hostif->extralen >= 7 &&
1137 ms_header->bLength >= 7 &&
1138 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1139 ms_header->bDescriptorSubtype == HEADER)
1140 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1141 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1142 else
1143 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1144
1145 epidx = 0;
1146 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1147 hostep = &hostif->endpoint[i];
1148 ep = get_ep_desc(hostep);
1149 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1150 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1151 continue;
1152 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1153 if (hostep->extralen < 4 ||
1154 ms_ep->bLength < 4 ||
1155 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1156 ms_ep->bDescriptorSubtype != MS_GENERAL)
1157 continue;
1158 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1159 if (endpoints[epidx].out_ep) {
1160 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1161 snd_printk(KERN_WARNING "too many endpoints\n");
1162 break;
1163 }
1164 }
1165 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1166 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1167 endpoints[epidx].out_interval = ep->bInterval;
1168 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1169 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1170 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1171 } else {
1172 if (endpoints[epidx].in_ep) {
1173 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1174 snd_printk(KERN_WARNING "too many endpoints\n");
1175 break;
1176 }
1177 }
1178 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1179 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1180 endpoints[epidx].in_interval = ep->bInterval;
1181 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1182 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1183 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1184 }
1185 }
1186 return 0;
1187}
1188
1189/*
1190 * On Roland devices, use the second alternate setting to be able to use
1191 * the interrupt input endpoint.
1192 */
1193static void snd_usbmidi_switch_roland_altsetting(snd_usb_midi_t* umidi)
1194{
1195 struct usb_interface* intf;
1196 struct usb_host_interface *hostif;
1197 struct usb_interface_descriptor* intfd;
1198
1199 intf = umidi->iface;
1200 if (!intf || intf->num_altsetting != 2)
1201 return;
1202
1203 hostif = &intf->altsetting[1];
1204 intfd = get_iface_desc(hostif);
1205 if (intfd->bNumEndpoints != 2 ||
1206 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1207 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1208 return;
1209
1210 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1211 intfd->bAlternateSetting);
1212 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1213 intfd->bAlternateSetting);
1214}
1215
1216/*
1217 * Try to find any usable endpoints in the interface.
1218 */
1219static int snd_usbmidi_detect_endpoints(snd_usb_midi_t* umidi,
1220 snd_usb_midi_endpoint_info_t* endpoint,
1221 int max_endpoints)
1222{
1223 struct usb_interface* intf;
1224 struct usb_host_interface *hostif;
1225 struct usb_interface_descriptor* intfd;
1226 struct usb_endpoint_descriptor* epd;
1227 int i, out_eps = 0, in_eps = 0;
1228
1229 if (le16_to_cpu(umidi->chip->dev->descriptor.idVendor) == 0x0582)
1230 snd_usbmidi_switch_roland_altsetting(umidi);
1231
1232 intf = umidi->iface;
1233 if (!intf || intf->num_altsetting < 1)
1234 return -ENOENT;
1235 hostif = intf->cur_altsetting;
1236 intfd = get_iface_desc(hostif);
1237
1238 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1239 epd = get_endpoint(hostif, i);
1240 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1241 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1242 continue;
1243 if (out_eps < max_endpoints &&
1244 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1245 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1246 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1247 endpoint[out_eps].out_interval = epd->bInterval;
1248 ++out_eps;
1249 }
1250 if (in_eps < max_endpoints &&
1251 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1252 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1253 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1254 endpoint[in_eps].in_interval = epd->bInterval;
1255 ++in_eps;
1256 }
1257 }
1258 return (out_eps || in_eps) ? 0 : -ENOENT;
1259}
1260
1261/*
1262 * Detects the endpoints for one-port-per-endpoint protocols.
1263 */
1264static int snd_usbmidi_detect_per_port_endpoints(snd_usb_midi_t* umidi,
1265 snd_usb_midi_endpoint_info_t* endpoints)
1266{
1267 int err, i;
1268
1269 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1270 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1271 if (endpoints[i].out_ep)
1272 endpoints[i].out_cables = 0x0001;
1273 if (endpoints[i].in_ep)
1274 endpoints[i].in_cables = 0x0001;
1275 }
1276 return err;
1277}
1278
1279/*
1280 * Detects the endpoints and ports of Yamaha devices.
1281 */
1282static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi,
1283 snd_usb_midi_endpoint_info_t* endpoint)
1284{
1285 struct usb_interface* intf;
1286 struct usb_host_interface *hostif;
1287 struct usb_interface_descriptor* intfd;
1288 uint8_t* cs_desc;
1289
1290 intf = umidi->iface;
1291 if (!intf)
1292 return -ENOENT;
1293 hostif = intf->altsetting;
1294 intfd = get_iface_desc(hostif);
1295 if (intfd->bNumEndpoints < 1)
1296 return -ENOENT;
1297
1298 /*
1299 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1300 * necessarily with any useful contents. So simply count 'em.
1301 */
1302 for (cs_desc = hostif->extra;
1303 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1304 cs_desc += cs_desc[0]) {
1305 if (cs_desc[1] == CS_AUDIO_INTERFACE) {
1306 if (cs_desc[2] == MIDI_IN_JACK)
1307 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1308 else if (cs_desc[2] == MIDI_OUT_JACK)
1309 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1310 }
1311 }
1312 if (!endpoint->in_cables && !endpoint->out_cables)
1313 return -ENOENT;
1314
1315 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1316}
1317
1318/*
1319 * Creates the endpoints and their ports for Midiman devices.
1320 */
1321static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi,
1322 snd_usb_midi_endpoint_info_t* endpoint)
1323{
1324 snd_usb_midi_endpoint_info_t ep_info;
1325 struct usb_interface* intf;
1326 struct usb_host_interface *hostif;
1327 struct usb_interface_descriptor* intfd;
1328 struct usb_endpoint_descriptor* epd;
1329 int cable, err;
1330
1331 intf = umidi->iface;
1332 if (!intf)
1333 return -ENOENT;
1334 hostif = intf->altsetting;
1335 intfd = get_iface_desc(hostif);
1336 /*
1337 * The various MidiSport devices have more or less random endpoint
1338 * numbers, so we have to identify the endpoints by their index in
1339 * the descriptor array, like the driver for that other OS does.
1340 *
1341 * There is one interrupt input endpoint for all input ports, one
1342 * bulk output endpoint for even-numbered ports, and one for odd-
1343 * numbered ports. Both bulk output endpoints have corresponding
1344 * input bulk endpoints (at indices 1 and 3) which aren't used.
1345 */
1346 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1347 snd_printdd(KERN_ERR "not enough endpoints\n");
1348 return -ENOENT;
1349 }
1350
1351 epd = get_endpoint(hostif, 0);
1352 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1353 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1354 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1355 return -ENXIO;
1356 }
1357 epd = get_endpoint(hostif, 2);
1358 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1359 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1360 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1361 return -ENXIO;
1362 }
1363 if (endpoint->out_cables > 0x0001) {
1364 epd = get_endpoint(hostif, 4);
1365 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1366 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1367 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1368 return -ENXIO;
1369 }
1370 }
1371
1372 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1373 ep_info.out_cables = endpoint->out_cables & 0x5555;
1374 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1375 if (err < 0)
1376 return err;
1377
1378 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1379 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1380 ep_info.in_cables = endpoint->in_cables;
1381 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1382 if (err < 0)
1383 return err;
1384
1385 if (endpoint->out_cables > 0x0001) {
1386 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1387 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1388 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1389 if (err < 0)
1390 return err;
1391 }
1392
1393 for (cable = 0; cable < 0x10; ++cable) {
1394 if (endpoint->out_cables & (1 << cable))
1395 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1396 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1397 if (endpoint->in_cables & (1 << cable))
1398 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1399 &umidi->endpoints[0].in->ports[cable].substream);
1400 }
1401 return 0;
1402}
1403
1404static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi,
1405 int out_ports, int in_ports)
1406{
1407 snd_rawmidi_t* rmidi;
1408 int err;
1409
1410 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1411 umidi->chip->next_midi_device++,
1412 out_ports, in_ports, &rmidi);
1413 if (err < 0)
1414 return err;
1415 strcpy(rmidi->name, umidi->chip->card->shortname);
1416 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1417 SNDRV_RAWMIDI_INFO_INPUT |
1418 SNDRV_RAWMIDI_INFO_DUPLEX;
1419 rmidi->private_data = umidi;
1420 rmidi->private_free = snd_usbmidi_rawmidi_free;
1421 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1422 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1423
1424 umidi->rmidi = rmidi;
1425 return 0;
1426}
1427
1428/*
1429 * Temporarily stop input.
1430 */
1431void snd_usbmidi_input_stop(struct list_head* p)
1432{
1433 snd_usb_midi_t* umidi;
1434 int i;
1435
1436 umidi = list_entry(p, snd_usb_midi_t, list);
1437 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1438 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
1439 if (ep->in)
1440 usb_kill_urb(ep->in->urb);
1441 }
1442}
1443
1444static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t* ep)
1445{
1446 if (ep) {
1447 struct urb* urb = ep->urb;
1448 urb->dev = ep->umidi->chip->dev;
1449 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1450 }
1451}
1452
1453/*
1454 * Resume input after a call to snd_usbmidi_input_stop().
1455 */
1456void snd_usbmidi_input_start(struct list_head* p)
1457{
1458 snd_usb_midi_t* umidi;
1459 int i;
1460
1461 umidi = list_entry(p, snd_usb_midi_t, list);
1462 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1463 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1464}
1465
1466/*
1467 * Creates and registers everything needed for a MIDI streaming interface.
1468 */
1469int snd_usb_create_midi_interface(snd_usb_audio_t* chip,
1470 struct usb_interface* iface,
1471 const snd_usb_audio_quirk_t* quirk)
1472{
1473 snd_usb_midi_t* umidi;
1474 snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS];
1475 int out_ports, in_ports;
1476 int i, err;
1477
1478 umidi = kcalloc(1, sizeof(*umidi), GFP_KERNEL);
1479 if (!umidi)
1480 return -ENOMEM;
1481 umidi->chip = chip;
1482 umidi->iface = iface;
1483 umidi->quirk = quirk;
1484 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1485
1486 /* detect the endpoint(s) to use */
1487 memset(endpoints, 0, sizeof(endpoints));
1488 if (!quirk) {
1489 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1490 } else {
1491 switch (quirk->type) {
1492 case QUIRK_MIDI_FIXED_ENDPOINT:
1493 memcpy(&endpoints[0], quirk->data,
1494 sizeof(snd_usb_midi_endpoint_info_t));
1495 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1496 break;
1497 case QUIRK_MIDI_YAMAHA:
1498 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1499 break;
1500 case QUIRK_MIDI_MIDIMAN:
1501 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1502 memcpy(&endpoints[0], quirk->data,
1503 sizeof(snd_usb_midi_endpoint_info_t));
1504 err = 0;
1505 break;
1506 case QUIRK_MIDI_NOVATION:
1507 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1508 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1509 break;
1510 case QUIRK_MIDI_MOTU:
1511 umidi->usb_protocol_ops = &snd_usbmidi_motu_ops;
1512 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1513 break;
1514 case QUIRK_MIDI_EMAGIC:
1515 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1516 memcpy(&endpoints[0], quirk->data,
1517 sizeof(snd_usb_midi_endpoint_info_t));
1518 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1519 break;
1520 default:
1521 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1522 err = -ENXIO;
1523 break;
1524 }
1525 }
1526 if (err < 0) {
1527 kfree(umidi);
1528 return err;
1529 }
1530
1531 /* create rawmidi device */
1532 out_ports = 0;
1533 in_ports = 0;
1534 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1535 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1536 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1537 }
1538 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1539 if (err < 0) {
1540 kfree(umidi);
1541 return err;
1542 }
1543
1544 /* create endpoint/port structures */
1545 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1546 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1547 else
1548 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1549 if (err < 0) {
1550 snd_usbmidi_free(umidi);
1551 return err;
1552 }
1553
1554 list_add(&umidi->list, &umidi->chip->midi_list);
1555
1556 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1557 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1558 return 0;
1559}
1560
1561EXPORT_SYMBOL(snd_usb_create_midi_interface);
1562EXPORT_SYMBOL(snd_usbmidi_input_stop);
1563EXPORT_SYMBOL(snd_usbmidi_input_start);
1564EXPORT_SYMBOL(snd_usbmidi_disconnect);
diff --git a/sound/usb/usbmixer.c b/sound/usb/usbmixer.c
new file mode 100644
index 000000000000..5f1906915aa6
--- /dev/null
+++ b/sound/usb/usbmixer.c
@@ -0,0 +1,1545 @@
1/*
2 * (Tentative) USB Audio Driver for ALSA
3 *
4 * Mixer control part
5 *
6 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
7 *
8 * Many codes borrowed from audio.c by
9 * Alan Cox (alan@lxorguk.ukuu.org.uk)
10 * Thomas Sailer (sailer@ife.ee.ethz.ch)
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
28
29#include <sound/driver.h>
30#include <linux/bitops.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/usb.h>
36#include <sound/core.h>
37#include <sound/control.h>
38
39#include "usbaudio.h"
40
41
42/*
43 */
44
45/* ignore error from controls - for debugging */
46/* #define IGNORE_CTL_ERROR */
47
48typedef struct usb_mixer_build mixer_build_t;
49typedef struct usb_audio_term usb_audio_term_t;
50typedef struct usb_mixer_elem_info usb_mixer_elem_info_t;
51
52
53struct usb_audio_term {
54 int id;
55 int type;
56 int channels;
57 unsigned int chconfig;
58 int name;
59};
60
61struct usbmix_name_map;
62
63struct usb_mixer_build {
64 snd_usb_audio_t *chip;
65 unsigned char *buffer;
66 unsigned int buflen;
67 unsigned int ctrlif;
68 unsigned short vendor;
69 unsigned short product;
70 DECLARE_BITMAP(unitbitmap, 32*32);
71 usb_audio_term_t oterm;
72 const struct usbmix_name_map *map;
73};
74
75struct usb_mixer_elem_info {
76 snd_usb_audio_t *chip;
77 unsigned int ctrlif;
78 unsigned int id;
79 unsigned int control; /* CS or ICN (high byte) */
80 unsigned int cmask; /* channel mask bitmap: 0 = master */
81 int channels;
82 int val_type;
83 int min, max, res;
84 unsigned int initialized: 1;
85};
86
87
88enum {
89 USB_FEATURE_NONE = 0,
90 USB_FEATURE_MUTE = 1,
91 USB_FEATURE_VOLUME,
92 USB_FEATURE_BASS,
93 USB_FEATURE_MID,
94 USB_FEATURE_TREBLE,
95 USB_FEATURE_GEQ,
96 USB_FEATURE_AGC,
97 USB_FEATURE_DELAY,
98 USB_FEATURE_BASSBOOST,
99 USB_FEATURE_LOUDNESS
100};
101
102enum {
103 USB_MIXER_BOOLEAN,
104 USB_MIXER_INV_BOOLEAN,
105 USB_MIXER_S8,
106 USB_MIXER_U8,
107 USB_MIXER_S16,
108 USB_MIXER_U16,
109};
110
111enum {
112 USB_PROC_UPDOWN = 1,
113 USB_PROC_UPDOWN_SWITCH = 1,
114 USB_PROC_UPDOWN_MODE_SEL = 2,
115
116 USB_PROC_PROLOGIC = 2,
117 USB_PROC_PROLOGIC_SWITCH = 1,
118 USB_PROC_PROLOGIC_MODE_SEL = 2,
119
120 USB_PROC_3DENH = 3,
121 USB_PROC_3DENH_SWITCH = 1,
122 USB_PROC_3DENH_SPACE = 2,
123
124 USB_PROC_REVERB = 4,
125 USB_PROC_REVERB_SWITCH = 1,
126 USB_PROC_REVERB_LEVEL = 2,
127 USB_PROC_REVERB_TIME = 3,
128 USB_PROC_REVERB_DELAY = 4,
129
130 USB_PROC_CHORUS = 5,
131 USB_PROC_CHORUS_SWITCH = 1,
132 USB_PROC_CHORUS_LEVEL = 2,
133 USB_PROC_CHORUS_RATE = 3,
134 USB_PROC_CHORUS_DEPTH = 4,
135
136 USB_PROC_DCR = 6,
137 USB_PROC_DCR_SWITCH = 1,
138 USB_PROC_DCR_RATIO = 2,
139 USB_PROC_DCR_MAX_AMP = 3,
140 USB_PROC_DCR_THRESHOLD = 4,
141 USB_PROC_DCR_ATTACK = 5,
142 USB_PROC_DCR_RELEASE = 6,
143};
144
145#define MAX_CHANNELS 10 /* max logical channels */
146
147
148/*
149 * manual mapping of mixer names
150 * if the mixer topology is too complicated and the parsed names are
151 * ambiguous, add the entries in usbmixer_maps.c.
152 */
153#include "usbmixer_maps.c"
154
155/* get the mapped name if the unit matches */
156static int check_mapped_name(mixer_build_t *state, int unitid, int control, char *buf, int buflen)
157{
158 const struct usbmix_name_map *p;
159
160 if (! state->map)
161 return 0;
162
163 for (p = state->map; p->id; p++) {
164 if (p->id == unitid && p->name &&
165 (! control || ! p->control || control == p->control)) {
166 buflen--;
167 return strlcpy(buf, p->name, buflen);
168 }
169 }
170 return 0;
171}
172
173/* check whether the control should be ignored */
174static int check_ignored_ctl(mixer_build_t *state, int unitid, int control)
175{
176 const struct usbmix_name_map *p;
177
178 if (! state->map)
179 return 0;
180 for (p = state->map; p->id; p++) {
181 if (p->id == unitid && ! p->name &&
182 (! control || ! p->control || control == p->control)) {
183 // printk("ignored control %d:%d\n", unitid, control);
184 return 1;
185 }
186 }
187 return 0;
188}
189
190/*
191 * find an audio control unit with the given unit id
192 */
193static void *find_audio_control_unit(mixer_build_t *state, unsigned char unit)
194{
195 unsigned char *p;
196
197 p = NULL;
198 while ((p = snd_usb_find_desc(state->buffer, state->buflen, p,
199 USB_DT_CS_INTERFACE)) != NULL) {
200 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
201 return p;
202 }
203 return NULL;
204}
205
206
207/*
208 * copy a string with the given id
209 */
210static int snd_usb_copy_string_desc(mixer_build_t *state, int index, char *buf, int maxlen)
211{
212 int len = usb_string(state->chip->dev, index, buf, maxlen - 1);
213 buf[len] = 0;
214 return len;
215}
216
217/*
218 * convert from the byte/word on usb descriptor to the zero-based integer
219 */
220static int convert_signed_value(usb_mixer_elem_info_t *cval, int val)
221{
222 switch (cval->val_type) {
223 case USB_MIXER_BOOLEAN:
224 return !!val;
225 case USB_MIXER_INV_BOOLEAN:
226 return !val;
227 case USB_MIXER_U8:
228 val &= 0xff;
229 break;
230 case USB_MIXER_S8:
231 val &= 0xff;
232 if (val >= 0x80)
233 val -= 0x100;
234 break;
235 case USB_MIXER_U16:
236 val &= 0xffff;
237 break;
238 case USB_MIXER_S16:
239 val &= 0xffff;
240 if (val >= 0x8000)
241 val -= 0x10000;
242 break;
243 }
244 return val;
245}
246
247/*
248 * convert from the zero-based int to the byte/word for usb descriptor
249 */
250static int convert_bytes_value(usb_mixer_elem_info_t *cval, int val)
251{
252 switch (cval->val_type) {
253 case USB_MIXER_BOOLEAN:
254 return !!val;
255 case USB_MIXER_INV_BOOLEAN:
256 return !val;
257 case USB_MIXER_S8:
258 case USB_MIXER_U8:
259 return val & 0xff;
260 case USB_MIXER_S16:
261 case USB_MIXER_U16:
262 return val & 0xffff;
263 }
264 return 0; /* not reached */
265}
266
267static int get_relative_value(usb_mixer_elem_info_t *cval, int val)
268{
269 if (! cval->res)
270 cval->res = 1;
271 if (val < cval->min)
272 return 0;
273 else if (val > cval->max)
274 return (cval->max - cval->min) / cval->res;
275 else
276 return (val - cval->min) / cval->res;
277}
278
279static int get_abs_value(usb_mixer_elem_info_t *cval, int val)
280{
281 if (val < 0)
282 return cval->min;
283 if (! cval->res)
284 cval->res = 1;
285 val *= cval->res;
286 val += cval->min;
287 if (val > cval->max)
288 return cval->max;
289 return val;
290}
291
292
293/*
294 * retrieve a mixer value
295 */
296
297static int get_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int *value_ret)
298{
299 unsigned char buf[2];
300 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
301 int timeout = 10;
302
303 while (timeout-- > 0) {
304 if (snd_usb_ctl_msg(cval->chip->dev, usb_rcvctrlpipe(cval->chip->dev, 0),
305 request,
306 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
307 validx, cval->ctrlif | (cval->id << 8),
308 buf, val_len, 100) >= 0) {
309 *value_ret = convert_signed_value(cval, snd_usb_combine_bytes(buf, val_len));
310 return 0;
311 }
312 }
313 snd_printdd(KERN_ERR "cannot get ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type);
314 return -EINVAL;
315}
316
317static int get_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int *value)
318{
319 return get_ctl_value(cval, GET_CUR, validx, value);
320}
321
322/* channel = 0: master, 1 = first channel */
323inline static int get_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int *value)
324{
325 return get_ctl_value(cval, GET_CUR, (cval->control << 8) | channel, value);
326}
327
328/*
329 * set a mixer value
330 */
331
332static int set_ctl_value(usb_mixer_elem_info_t *cval, int request, int validx, int value_set)
333{
334 unsigned char buf[2];
335 int val_len = cval->val_type >= USB_MIXER_S16 ? 2 : 1;
336 int timeout = 10;
337
338 value_set = convert_bytes_value(cval, value_set);
339 buf[0] = value_set & 0xff;
340 buf[1] = (value_set >> 8) & 0xff;
341 while (timeout -- > 0)
342 if (snd_usb_ctl_msg(cval->chip->dev, usb_sndctrlpipe(cval->chip->dev, 0),
343 request,
344 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
345 validx, cval->ctrlif | (cval->id << 8),
346 buf, val_len, 100) >= 0)
347 return 0;
348 snd_printdd(KERN_ERR "cannot set ctl value: req = 0x%x, wValue = 0x%x, wIndex = 0x%x, type = %d, data = 0x%x/0x%x\n", request, validx, cval->ctrlif | (cval->id << 8), cval->val_type, buf[0], buf[1]);
349 return -EINVAL;
350}
351
352static int set_cur_ctl_value(usb_mixer_elem_info_t *cval, int validx, int value)
353{
354 return set_ctl_value(cval, SET_CUR, validx, value);
355}
356
357inline static int set_cur_mix_value(usb_mixer_elem_info_t *cval, int channel, int value)
358{
359 return set_ctl_value(cval, SET_CUR, (cval->control << 8) | channel, value);
360}
361
362
363/*
364 * parser routines begin here...
365 */
366
367static int parse_audio_unit(mixer_build_t *state, int unitid);
368
369
370/*
371 * check if the input/output channel routing is enabled on the given bitmap.
372 * used for mixer unit parser
373 */
374static int check_matrix_bitmap(unsigned char *bmap, int ich, int och, int num_outs)
375{
376 int idx = ich * num_outs + och;
377 return bmap[idx >> 3] & (0x80 >> (idx & 7));
378}
379
380
381/*
382 * add an alsa control element
383 * search and increment the index until an empty slot is found.
384 *
385 * if failed, give up and free the control instance.
386 */
387
388static int add_control_to_empty(snd_card_t *card, snd_kcontrol_t *kctl)
389{
390 int err;
391 while (snd_ctl_find_id(card, &kctl->id))
392 kctl->id.index++;
393 if ((err = snd_ctl_add(card, kctl)) < 0) {
394 snd_printd(KERN_ERR "cannot add control (err = %d)\n", err);
395 snd_ctl_free_one(kctl);
396 }
397 return err;
398}
399
400
401/*
402 * get a terminal name string
403 */
404
405static struct iterm_name_combo {
406 int type;
407 char *name;
408} iterm_names[] = {
409 { 0x0300, "Output" },
410 { 0x0301, "Speaker" },
411 { 0x0302, "Headphone" },
412 { 0x0303, "HMD Audio" },
413 { 0x0304, "Desktop Speaker" },
414 { 0x0305, "Room Speaker" },
415 { 0x0306, "Com Speaker" },
416 { 0x0307, "LFE" },
417 { 0x0600, "External In" },
418 { 0x0601, "Analog In" },
419 { 0x0602, "Digital In" },
420 { 0x0603, "Line" },
421 { 0x0604, "Legacy In" },
422 { 0x0605, "IEC958 In" },
423 { 0x0606, "1394 DA Stream" },
424 { 0x0607, "1394 DV Stream" },
425 { 0x0700, "Embedded" },
426 { 0x0701, "Noise Source" },
427 { 0x0702, "Equalization Noise" },
428 { 0x0703, "CD" },
429 { 0x0704, "DAT" },
430 { 0x0705, "DCC" },
431 { 0x0706, "MiniDisk" },
432 { 0x0707, "Analog Tape" },
433 { 0x0708, "Phonograph" },
434 { 0x0709, "VCR Audio" },
435 { 0x070a, "Video Disk Audio" },
436 { 0x070b, "DVD Audio" },
437 { 0x070c, "TV Tuner Audio" },
438 { 0x070d, "Satellite Rec Audio" },
439 { 0x070e, "Cable Tuner Audio" },
440 { 0x070f, "DSS Audio" },
441 { 0x0710, "Radio Receiver" },
442 { 0x0711, "Radio Transmitter" },
443 { 0x0712, "Multi-Track Recorder" },
444 { 0x0713, "Synthesizer" },
445 { 0 },
446};
447
448static int get_term_name(mixer_build_t *state, usb_audio_term_t *iterm,
449 unsigned char *name, int maxlen, int term_only)
450{
451 struct iterm_name_combo *names;
452
453 if (iterm->name)
454 return snd_usb_copy_string_desc(state, iterm->name, name, maxlen);
455
456 /* virtual type - not a real terminal */
457 if (iterm->type >> 16) {
458 if (term_only)
459 return 0;
460 switch (iterm->type >> 16) {
461 case SELECTOR_UNIT:
462 strcpy(name, "Selector"); return 8;
463 case PROCESSING_UNIT:
464 strcpy(name, "Process Unit"); return 12;
465 case EXTENSION_UNIT:
466 strcpy(name, "Ext Unit"); return 8;
467 case MIXER_UNIT:
468 strcpy(name, "Mixer"); return 5;
469 default:
470 return sprintf(name, "Unit %d", iterm->id);
471 }
472 }
473
474 switch (iterm->type & 0xff00) {
475 case 0x0100:
476 strcpy(name, "PCM"); return 3;
477 case 0x0200:
478 strcpy(name, "Mic"); return 3;
479 case 0x0400:
480 strcpy(name, "Headset"); return 7;
481 case 0x0500:
482 strcpy(name, "Phone"); return 5;
483 }
484
485 for (names = iterm_names; names->type; names++)
486 if (names->type == iterm->type) {
487 strcpy(name, names->name);
488 return strlen(names->name);
489 }
490 return 0;
491}
492
493
494/*
495 * parse the source unit recursively until it reaches to a terminal
496 * or a branched unit.
497 */
498static int check_input_term(mixer_build_t *state, int id, usb_audio_term_t *term)
499{
500 unsigned char *p1;
501
502 memset(term, 0, sizeof(*term));
503 while ((p1 = find_audio_control_unit(state, id)) != NULL) {
504 term->id = id;
505 switch (p1[2]) {
506 case INPUT_TERMINAL:
507 term->type = combine_word(p1 + 4);
508 term->channels = p1[7];
509 term->chconfig = combine_word(p1 + 8);
510 term->name = p1[11];
511 return 0;
512 case FEATURE_UNIT:
513 id = p1[4];
514 break; /* continue to parse */
515 case MIXER_UNIT:
516 term->type = p1[2] << 16; /* virtual type */
517 term->channels = p1[5 + p1[4]];
518 term->chconfig = combine_word(p1 + 6 + p1[4]);
519 term->name = p1[p1[0] - 1];
520 return 0;
521 case SELECTOR_UNIT:
522 /* call recursively to retrieve the channel info */
523 if (check_input_term(state, p1[5], term) < 0)
524 return -ENODEV;
525 term->type = p1[2] << 16; /* virtual type */
526 term->id = id;
527 term->name = p1[9 + p1[0] - 1];
528 return 0;
529 case PROCESSING_UNIT:
530 case EXTENSION_UNIT:
531 if (p1[6] == 1) {
532 id = p1[7];
533 break; /* continue to parse */
534 }
535 term->type = p1[2] << 16; /* virtual type */
536 term->channels = p1[7 + p1[6]];
537 term->chconfig = combine_word(p1 + 8 + p1[6]);
538 term->name = p1[12 + p1[6] + p1[11 + p1[6]]];
539 return 0;
540 default:
541 return -ENODEV;
542 }
543 }
544 return -ENODEV;
545}
546
547
548/*
549 * Feature Unit
550 */
551
552/* feature unit control information */
553struct usb_feature_control_info {
554 const char *name;
555 unsigned int type; /* control type (mute, volume, etc.) */
556};
557
558static struct usb_feature_control_info audio_feature_info[] = {
559 { "Mute", USB_MIXER_INV_BOOLEAN },
560 { "Volume", USB_MIXER_S16 },
561 { "Tone Control - Bass", USB_MIXER_S8 },
562 { "Tone Control - Mid", USB_MIXER_S8 },
563 { "Tone Control - Treble", USB_MIXER_S8 },
564 { "Graphic Equalizer", USB_MIXER_S8 }, /* FIXME: not implemeted yet */
565 { "Auto Gain Control", USB_MIXER_BOOLEAN },
566 { "Delay Control", USB_MIXER_U16 },
567 { "Bass Boost", USB_MIXER_BOOLEAN },
568 { "Loudness", USB_MIXER_BOOLEAN },
569};
570
571
572/* private_free callback */
573static void usb_mixer_elem_free(snd_kcontrol_t *kctl)
574{
575 if (kctl->private_data) {
576 kfree(kctl->private_data);
577 kctl->private_data = NULL;
578 }
579}
580
581
582/*
583 * interface to ALSA control for feature/mixer units
584 */
585
586/*
587 * retrieve the minimum and maximum values for the specified control
588 */
589static int get_min_max(usb_mixer_elem_info_t *cval, int default_min)
590{
591 /* for failsafe */
592 cval->min = default_min;
593 cval->max = cval->min + 1;
594 cval->res = 1;
595
596 if (cval->val_type == USB_MIXER_BOOLEAN ||
597 cval->val_type == USB_MIXER_INV_BOOLEAN) {
598 cval->initialized = 1;
599 } else {
600 int minchn = 0;
601 if (cval->cmask) {
602 int i;
603 for (i = 0; i < MAX_CHANNELS; i++)
604 if (cval->cmask & (1 << i)) {
605 minchn = i + 1;
606 break;
607 }
608 }
609 if (get_ctl_value(cval, GET_MAX, (cval->control << 8) | minchn, &cval->max) < 0 ||
610 get_ctl_value(cval, GET_MIN, (cval->control << 8) | minchn, &cval->min) < 0) {
611 snd_printd(KERN_ERR "%d:%d: cannot get min/max values for control %d (id %d)\n", cval->id, cval->ctrlif, cval->control, cval->id);
612 return -EINVAL;
613 }
614 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0) {
615 cval->res = 1;
616 } else {
617 int last_valid_res = cval->res;
618
619 while (cval->res > 1) {
620 if (set_ctl_value(cval, SET_RES, (cval->control << 8) | minchn, cval->res / 2) < 0)
621 break;
622 cval->res /= 2;
623 }
624 if (get_ctl_value(cval, GET_RES, (cval->control << 8) | minchn, &cval->res) < 0)
625 cval->res = last_valid_res;
626 }
627 if (cval->res == 0)
628 cval->res = 1;
629 cval->initialized = 1;
630 }
631 return 0;
632}
633
634
635/* get a feature/mixer unit info */
636static int mixer_ctl_feature_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
637{
638 usb_mixer_elem_info_t *cval = kcontrol->private_data;
639
640 if (cval->val_type == USB_MIXER_BOOLEAN ||
641 cval->val_type == USB_MIXER_INV_BOOLEAN)
642 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
643 else
644 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
645 uinfo->count = cval->channels;
646 if (cval->val_type == USB_MIXER_BOOLEAN ||
647 cval->val_type == USB_MIXER_INV_BOOLEAN) {
648 uinfo->value.integer.min = 0;
649 uinfo->value.integer.max = 1;
650 } else {
651 if (! cval->initialized)
652 get_min_max(cval, 0);
653 uinfo->value.integer.min = 0;
654 uinfo->value.integer.max = (cval->max - cval->min) / cval->res;
655 }
656 return 0;
657}
658
659/* get the current value from feature/mixer unit */
660static int mixer_ctl_feature_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
661{
662 usb_mixer_elem_info_t *cval = kcontrol->private_data;
663 int c, cnt, val, err;
664
665 if (cval->cmask) {
666 cnt = 0;
667 for (c = 0; c < MAX_CHANNELS; c++) {
668 if (cval->cmask & (1 << c)) {
669 err = get_cur_mix_value(cval, c + 1, &val);
670 if (err < 0) {
671 if (cval->chip->ignore_ctl_error) {
672 ucontrol->value.integer.value[0] = cval->min;
673 return 0;
674 }
675 snd_printd(KERN_ERR "cannot get current value for control %d ch %d: err = %d\n", cval->control, c + 1, err);
676 return err;
677 }
678 val = get_relative_value(cval, val);
679 ucontrol->value.integer.value[cnt] = val;
680 cnt++;
681 }
682 }
683 } else {
684 /* master channel */
685 err = get_cur_mix_value(cval, 0, &val);
686 if (err < 0) {
687 if (cval->chip->ignore_ctl_error) {
688 ucontrol->value.integer.value[0] = cval->min;
689 return 0;
690 }
691 snd_printd(KERN_ERR "cannot get current value for control %d master ch: err = %d\n", cval->control, err);
692 return err;
693 }
694 val = get_relative_value(cval, val);
695 ucontrol->value.integer.value[0] = val;
696 }
697 return 0;
698}
699
700/* put the current value to feature/mixer unit */
701static int mixer_ctl_feature_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
702{
703 usb_mixer_elem_info_t *cval = kcontrol->private_data;
704 int c, cnt, val, oval, err;
705 int changed = 0;
706
707 if (cval->cmask) {
708 cnt = 0;
709 for (c = 0; c < MAX_CHANNELS; c++) {
710 if (cval->cmask & (1 << c)) {
711 err = get_cur_mix_value(cval, c + 1, &oval);
712 if (err < 0) {
713 if (cval->chip->ignore_ctl_error)
714 return 0;
715 return err;
716 }
717 val = ucontrol->value.integer.value[cnt];
718 val = get_abs_value(cval, val);
719 if (oval != val) {
720 set_cur_mix_value(cval, c + 1, val);
721 changed = 1;
722 }
723 get_cur_mix_value(cval, c + 1, &val);
724 cnt++;
725 }
726 }
727 } else {
728 /* master channel */
729 err = get_cur_mix_value(cval, 0, &oval);
730 if (err < 0 && cval->chip->ignore_ctl_error)
731 return 0;
732 if (err < 0)
733 return err;
734 val = ucontrol->value.integer.value[0];
735 val = get_abs_value(cval, val);
736 if (val != oval) {
737 set_cur_mix_value(cval, 0, val);
738 changed = 1;
739 }
740 }
741 return changed;
742}
743
744static snd_kcontrol_new_t usb_feature_unit_ctl = {
745 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
746 .name = "", /* will be filled later manually */
747 .info = mixer_ctl_feature_info,
748 .get = mixer_ctl_feature_get,
749 .put = mixer_ctl_feature_put,
750};
751
752
753/*
754 * build a feature control
755 */
756
757static void build_feature_ctl(mixer_build_t *state, unsigned char *desc,
758 unsigned int ctl_mask, int control,
759 usb_audio_term_t *iterm, int unitid)
760{
761 unsigned int len = 0;
762 int mapped_name = 0;
763 int nameid = desc[desc[0] - 1];
764 snd_kcontrol_t *kctl;
765 usb_mixer_elem_info_t *cval;
766
767 control++; /* change from zero-based to 1-based value */
768
769 if (control == USB_FEATURE_GEQ) {
770 /* FIXME: not supported yet */
771 return;
772 }
773
774 if (check_ignored_ctl(state, unitid, control))
775 return;
776
777 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
778 if (! cval) {
779 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
780 return;
781 }
782 cval->chip = state->chip;
783 cval->ctrlif = state->ctrlif;
784 cval->id = unitid;
785 cval->control = control;
786 cval->cmask = ctl_mask;
787 cval->val_type = audio_feature_info[control-1].type;
788 if (ctl_mask == 0)
789 cval->channels = 1; /* master channel */
790 else {
791 int i, c = 0;
792 for (i = 0; i < 16; i++)
793 if (ctl_mask & (1 << i))
794 c++;
795 cval->channels = c;
796 }
797
798 /* get min/max values */
799 get_min_max(cval, 0);
800
801 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
802 if (! kctl) {
803 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
804 kfree(cval);
805 return;
806 }
807 kctl->private_free = usb_mixer_elem_free;
808
809 len = check_mapped_name(state, unitid, control, kctl->id.name, sizeof(kctl->id.name));
810 mapped_name = len != 0;
811 if (! len && nameid)
812 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
813
814 switch (control) {
815 case USB_FEATURE_MUTE:
816 case USB_FEATURE_VOLUME:
817 /* determine the control name. the rule is:
818 * - if a name id is given in descriptor, use it.
819 * - if the connected input can be determined, then use the name
820 * of terminal type.
821 * - if the connected output can be determined, use it.
822 * - otherwise, anonymous name.
823 */
824 if (! len) {
825 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 1);
826 if (! len)
827 len = get_term_name(state, &state->oterm, kctl->id.name, sizeof(kctl->id.name), 1);
828 if (! len)
829 len = snprintf(kctl->id.name, sizeof(kctl->id.name),
830 "Feature %d", unitid);
831 }
832 /* determine the stream direction:
833 * if the connected output is USB stream, then it's likely a
834 * capture stream. otherwise it should be playback (hopefully :)
835 */
836 if (! mapped_name && ! (state->oterm.type >> 16)) {
837 if ((state->oterm.type & 0xff00) == 0x0100) {
838 len = strlcat(kctl->id.name, " Capture", sizeof(kctl->id.name));
839 } else {
840 len = strlcat(kctl->id.name + len, " Playback", sizeof(kctl->id.name));
841 }
842 }
843 strlcat(kctl->id.name + len, control == USB_FEATURE_MUTE ? " Switch" : " Volume",
844 sizeof(kctl->id.name));
845 break;
846
847 default:
848 if (! len)
849 strlcpy(kctl->id.name, audio_feature_info[control-1].name,
850 sizeof(kctl->id.name));
851 break;
852 }
853
854 /* quirk for UDA1321/N101 */
855 /* note that detection between firmware 2.1.1.7 (N101) and later 2.1.1.21 */
856 /* is not very clear from datasheets */
857 /* I hope that the min value is -15360 for newer firmware --jk */
858 if (((state->vendor == 0x471 && (state->product == 0x104 || state->product == 0x105 || state->product == 0x101)) ||
859 (state->vendor == 0x672 && state->product == 0x1041)) && !strcmp(kctl->id.name, "PCM Playback Volume") &&
860 cval->min == -15616) {
861 snd_printk("USB Audio: using volume control quirk for the UDA1321/N101 chip\n");
862 cval->max = -256;
863 }
864
865 snd_printdd(KERN_INFO "[%d] FU [%s] ch = %d, val = %d/%d/%d\n",
866 cval->id, kctl->id.name, cval->channels, cval->min, cval->max, cval->res);
867 add_control_to_empty(state->chip->card, kctl);
868}
869
870
871
872/*
873 * parse a feature unit
874 *
875 * most of controlls are defined here.
876 */
877static int parse_audio_feature_unit(mixer_build_t *state, int unitid, unsigned char *ftr)
878{
879 int channels, i, j;
880 usb_audio_term_t iterm;
881 unsigned int master_bits, first_ch_bits;
882 int err, csize;
883
884 if (ftr[0] < 7 || ! (csize = ftr[5]) || ftr[0] < 7 + csize) {
885 snd_printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
886 return -EINVAL;
887 }
888
889 /* parse the source unit */
890 if ((err = parse_audio_unit(state, ftr[4])) < 0)
891 return err;
892
893 /* determine the input source type and name */
894 if (check_input_term(state, ftr[4], &iterm) < 0)
895 return -EINVAL;
896
897 channels = (ftr[0] - 7) / csize - 1;
898
899 master_bits = snd_usb_combine_bytes(ftr + 6, csize);
900 if (channels > 0)
901 first_ch_bits = snd_usb_combine_bytes(ftr + 6 + csize, csize);
902 else
903 first_ch_bits = 0;
904 /* check all control types */
905 for (i = 0; i < 10; i++) {
906 unsigned int ch_bits = 0;
907 for (j = 0; j < channels; j++) {
908 unsigned int mask = snd_usb_combine_bytes(ftr + 6 + csize * (j+1), csize);
909 if (mask & (1 << i))
910 ch_bits |= (1 << j);
911 }
912 if (ch_bits & 1) /* the first channel must be set (for ease of programming) */
913 build_feature_ctl(state, ftr, ch_bits, i, &iterm, unitid);
914 if (master_bits & (1 << i))
915 build_feature_ctl(state, ftr, 0, i, &iterm, unitid);
916 }
917
918 return 0;
919}
920
921
922/*
923 * Mixer Unit
924 */
925
926/*
927 * build a mixer unit control
928 *
929 * the callbacks are identical with feature unit.
930 * input channel number (zero based) is given in control field instead.
931 */
932
933static void build_mixer_unit_ctl(mixer_build_t *state, unsigned char *desc,
934 int in_pin, int in_ch, int unitid,
935 usb_audio_term_t *iterm)
936{
937 usb_mixer_elem_info_t *cval;
938 unsigned int input_pins = desc[4];
939 unsigned int num_outs = desc[5 + input_pins];
940 unsigned int i, len;
941 snd_kcontrol_t *kctl;
942
943 if (check_ignored_ctl(state, unitid, 0))
944 return;
945
946 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
947 if (! cval)
948 return;
949
950 cval->chip = state->chip;
951 cval->ctrlif = state->ctrlif;
952 cval->id = unitid;
953 cval->control = in_ch + 1; /* based on 1 */
954 cval->val_type = USB_MIXER_S16;
955 for (i = 0; i < num_outs; i++) {
956 if (check_matrix_bitmap(desc + 9 + input_pins, in_ch, i, num_outs)) {
957 cval->cmask |= (1 << i);
958 cval->channels++;
959 }
960 }
961
962 /* get min/max values */
963 get_min_max(cval, 0);
964
965 kctl = snd_ctl_new1(&usb_feature_unit_ctl, cval);
966 if (! kctl) {
967 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
968 kfree(cval);
969 return;
970 }
971 kctl->private_free = usb_mixer_elem_free;
972
973 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
974 if (! len)
975 len = get_term_name(state, iterm, kctl->id.name, sizeof(kctl->id.name), 0);
976 if (! len)
977 len = sprintf(kctl->id.name, "Mixer Source %d", in_ch + 1);
978 strlcat(kctl->id.name + len, " Volume", sizeof(kctl->id.name));
979
980 snd_printdd(KERN_INFO "[%d] MU [%s] ch = %d, val = %d/%d\n",
981 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
982 add_control_to_empty(state->chip->card, kctl);
983}
984
985
986/*
987 * parse a mixer unit
988 */
989static int parse_audio_mixer_unit(mixer_build_t *state, int unitid, unsigned char *desc)
990{
991 usb_audio_term_t iterm;
992 int input_pins, num_ins, num_outs;
993 int pin, ich, err;
994
995 if (desc[0] < 11 || ! (input_pins = desc[4]) || ! (num_outs = desc[5 + input_pins])) {
996 snd_printk(KERN_ERR "invalid MIXER UNIT descriptor %d\n", unitid);
997 return -EINVAL;
998 }
999 /* no bmControls field (e.g. Maya44) -> ignore */
1000 if (desc[0] <= 10 + input_pins) {
1001 snd_printdd(KERN_INFO "MU %d has no bmControls field\n", unitid);
1002 return 0;
1003 }
1004
1005 num_ins = 0;
1006 ich = 0;
1007 for (pin = 0; pin < input_pins; pin++) {
1008 err = parse_audio_unit(state, desc[5 + pin]);
1009 if (err < 0)
1010 return err;
1011 err = check_input_term(state, desc[5 + pin], &iterm);
1012 if (err < 0)
1013 return err;
1014 num_ins += iterm.channels;
1015 for (; ich < num_ins; ++ich) {
1016 int och, ich_has_controls = 0;
1017
1018 for (och = 0; och < num_outs; ++och) {
1019 if (check_matrix_bitmap(desc + 9 + input_pins,
1020 ich, och, num_outs)) {
1021 ich_has_controls = 1;
1022 break;
1023 }
1024 }
1025 if (ich_has_controls)
1026 build_mixer_unit_ctl(state, desc, pin, ich,
1027 unitid, &iterm);
1028 }
1029 }
1030 return 0;
1031}
1032
1033
1034/*
1035 * Processing Unit / Extension Unit
1036 */
1037
1038/* get callback for processing/extension unit */
1039static int mixer_ctl_procunit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1040{
1041 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1042 int err, val;
1043
1044 err = get_cur_ctl_value(cval, cval->control << 8, &val);
1045 if (err < 0 && cval->chip->ignore_ctl_error) {
1046 ucontrol->value.integer.value[0] = cval->min;
1047 return 0;
1048 }
1049 if (err < 0)
1050 return err;
1051 val = get_relative_value(cval, val);
1052 ucontrol->value.integer.value[0] = val;
1053 return 0;
1054}
1055
1056/* put callback for processing/extension unit */
1057static int mixer_ctl_procunit_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1058{
1059 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1060 int val, oval, err;
1061
1062 err = get_cur_ctl_value(cval, cval->control << 8, &oval);
1063 if (err < 0) {
1064 if (cval->chip->ignore_ctl_error)
1065 return 0;
1066 return err;
1067 }
1068 val = ucontrol->value.integer.value[0];
1069 val = get_abs_value(cval, val);
1070 if (val != oval) {
1071 set_cur_ctl_value(cval, cval->control << 8, val);
1072 return 1;
1073 }
1074 return 0;
1075}
1076
1077/* alsa control interface for processing/extension unit */
1078static snd_kcontrol_new_t mixer_procunit_ctl = {
1079 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1080 .name = "", /* will be filled later */
1081 .info = mixer_ctl_feature_info,
1082 .get = mixer_ctl_procunit_get,
1083 .put = mixer_ctl_procunit_put,
1084};
1085
1086
1087/*
1088 * predefined data for processing units
1089 */
1090struct procunit_value_info {
1091 int control;
1092 char *suffix;
1093 int val_type;
1094 int min_value;
1095};
1096
1097struct procunit_info {
1098 int type;
1099 char *name;
1100 struct procunit_value_info *values;
1101};
1102
1103static struct procunit_value_info updown_proc_info[] = {
1104 { USB_PROC_UPDOWN_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1105 { USB_PROC_UPDOWN_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1106 { 0 }
1107};
1108static struct procunit_value_info prologic_proc_info[] = {
1109 { USB_PROC_PROLOGIC_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1110 { USB_PROC_PROLOGIC_MODE_SEL, "Mode Select", USB_MIXER_U8, 1 },
1111 { 0 }
1112};
1113static struct procunit_value_info threed_enh_proc_info[] = {
1114 { USB_PROC_3DENH_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1115 { USB_PROC_3DENH_SPACE, "Spaciousness", USB_MIXER_U8 },
1116 { 0 }
1117};
1118static struct procunit_value_info reverb_proc_info[] = {
1119 { USB_PROC_REVERB_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1120 { USB_PROC_REVERB_LEVEL, "Level", USB_MIXER_U8 },
1121 { USB_PROC_REVERB_TIME, "Time", USB_MIXER_U16 },
1122 { USB_PROC_REVERB_DELAY, "Delay", USB_MIXER_U8 },
1123 { 0 }
1124};
1125static struct procunit_value_info chorus_proc_info[] = {
1126 { USB_PROC_CHORUS_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1127 { USB_PROC_CHORUS_LEVEL, "Level", USB_MIXER_U8 },
1128 { USB_PROC_CHORUS_RATE, "Rate", USB_MIXER_U16 },
1129 { USB_PROC_CHORUS_DEPTH, "Depth", USB_MIXER_U16 },
1130 { 0 }
1131};
1132static struct procunit_value_info dcr_proc_info[] = {
1133 { USB_PROC_DCR_SWITCH, "Switch", USB_MIXER_BOOLEAN },
1134 { USB_PROC_DCR_RATIO, "Ratio", USB_MIXER_U16 },
1135 { USB_PROC_DCR_MAX_AMP, "Max Amp", USB_MIXER_S16 },
1136 { USB_PROC_DCR_THRESHOLD, "Threshold", USB_MIXER_S16 },
1137 { USB_PROC_DCR_ATTACK, "Attack Time", USB_MIXER_U16 },
1138 { USB_PROC_DCR_RELEASE, "Release Time", USB_MIXER_U16 },
1139 { 0 }
1140};
1141
1142static struct procunit_info procunits[] = {
1143 { USB_PROC_UPDOWN, "Up Down", updown_proc_info },
1144 { USB_PROC_PROLOGIC, "Dolby Prologic", prologic_proc_info },
1145 { USB_PROC_3DENH, "3D Stereo Extender", threed_enh_proc_info },
1146 { USB_PROC_REVERB, "Reverb", reverb_proc_info },
1147 { USB_PROC_CHORUS, "Chorus", chorus_proc_info },
1148 { USB_PROC_DCR, "DCR", dcr_proc_info },
1149 { 0 },
1150};
1151
1152/*
1153 * build a processing/extension unit
1154 */
1155static int build_audio_procunit(mixer_build_t *state, int unitid, unsigned char *dsc, struct procunit_info *list, char *name)
1156{
1157 int num_ins = dsc[6];
1158 usb_mixer_elem_info_t *cval;
1159 snd_kcontrol_t *kctl;
1160 int i, err, nameid, type, len;
1161 struct procunit_info *info;
1162 struct procunit_value_info *valinfo;
1163 static struct procunit_value_info default_value_info[] = {
1164 { 0x01, "Switch", USB_MIXER_BOOLEAN },
1165 { 0 }
1166 };
1167 static struct procunit_info default_info = {
1168 0, NULL, default_value_info
1169 };
1170
1171 if (dsc[0] < 13 || dsc[0] < 13 + num_ins || dsc[0] < num_ins + dsc[11 + num_ins]) {
1172 snd_printk(KERN_ERR "invalid %s descriptor (id %d)\n", name, unitid);
1173 return -EINVAL;
1174 }
1175
1176 for (i = 0; i < num_ins; i++) {
1177 if ((err = parse_audio_unit(state, dsc[7 + i])) < 0)
1178 return err;
1179 }
1180
1181 type = combine_word(&dsc[4]);
1182 if (! type)
1183 return 0; /* undefined? */
1184
1185 for (info = list; info && info->type; info++)
1186 if (info->type == type)
1187 break;
1188 if (! info || ! info->type)
1189 info = &default_info;
1190
1191 for (valinfo = info->values; valinfo->control; valinfo++) {
1192 /* FIXME: bitmap might be longer than 8bit */
1193 if (! (dsc[12 + num_ins] & (1 << (valinfo->control - 1))))
1194 continue;
1195 if (check_ignored_ctl(state, unitid, valinfo->control))
1196 continue;
1197 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1198 if (! cval) {
1199 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1200 return -ENOMEM;
1201 }
1202 cval->chip = state->chip;
1203 cval->ctrlif = state->ctrlif;
1204 cval->id = unitid;
1205 cval->control = valinfo->control;
1206 cval->val_type = valinfo->val_type;
1207 cval->channels = 1;
1208
1209 /* get min/max values */
1210 if (type == USB_PROC_UPDOWN && cval->control == USB_PROC_UPDOWN_MODE_SEL) {
1211 /* FIXME: hard-coded */
1212 cval->min = 1;
1213 cval->max = dsc[15];
1214 cval->res = 1;
1215 cval->initialized = 1;
1216 } else
1217 get_min_max(cval, valinfo->min_value);
1218
1219 kctl = snd_ctl_new1(&mixer_procunit_ctl, cval);
1220 if (! kctl) {
1221 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1222 kfree(cval);
1223 return -ENOMEM;
1224 }
1225 kctl->private_free = usb_mixer_elem_free;
1226
1227 if (check_mapped_name(state, unitid, cval->control, kctl->id.name, sizeof(kctl->id.name)))
1228 ;
1229 else if (info->name)
1230 strlcpy(kctl->id.name, info->name, sizeof(kctl->id.name));
1231 else {
1232 nameid = dsc[12 + num_ins + dsc[11 + num_ins]];
1233 len = 0;
1234 if (nameid)
1235 len = snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1236 if (! len)
1237 strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
1238 }
1239 strlcat(kctl->id.name, " ", sizeof(kctl->id.name));
1240 strlcat(kctl->id.name, valinfo->suffix, sizeof(kctl->id.name));
1241
1242 snd_printdd(KERN_INFO "[%d] PU [%s] ch = %d, val = %d/%d\n",
1243 cval->id, kctl->id.name, cval->channels, cval->min, cval->max);
1244 if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1245 return err;
1246 }
1247 return 0;
1248}
1249
1250
1251static int parse_audio_processing_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1252{
1253 return build_audio_procunit(state, unitid, desc, procunits, "Processing Unit");
1254}
1255
1256static int parse_audio_extension_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1257{
1258 return build_audio_procunit(state, unitid, desc, NULL, "Extension Unit");
1259}
1260
1261
1262/*
1263 * Selector Unit
1264 */
1265
1266/* info callback for selector unit
1267 * use an enumerator type for routing
1268 */
1269static int mixer_ctl_selector_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1270{
1271 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1272 char **itemlist = (char **)kcontrol->private_value;
1273
1274 snd_assert(itemlist, return -EINVAL);
1275 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1276 uinfo->count = 1;
1277 uinfo->value.enumerated.items = cval->max;
1278 if ((int)uinfo->value.enumerated.item >= cval->max)
1279 uinfo->value.enumerated.item = cval->max - 1;
1280 strcpy(uinfo->value.enumerated.name, itemlist[uinfo->value.enumerated.item]);
1281 return 0;
1282}
1283
1284/* get callback for selector unit */
1285static int mixer_ctl_selector_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1286{
1287 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1288 int val, err;
1289
1290 err = get_cur_ctl_value(cval, 0, &val);
1291 if (err < 0) {
1292 if (cval->chip->ignore_ctl_error) {
1293 ucontrol->value.enumerated.item[0] = 0;
1294 return 0;
1295 }
1296 return err;
1297 }
1298 val = get_relative_value(cval, val);
1299 ucontrol->value.enumerated.item[0] = val;
1300 return 0;
1301}
1302
1303/* put callback for selector unit */
1304static int mixer_ctl_selector_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1305{
1306 usb_mixer_elem_info_t *cval = kcontrol->private_data;
1307 int val, oval, err;
1308
1309 err = get_cur_ctl_value(cval, 0, &oval);
1310 if (err < 0) {
1311 if (cval->chip->ignore_ctl_error)
1312 return 0;
1313 return err;
1314 }
1315 val = ucontrol->value.enumerated.item[0];
1316 val = get_abs_value(cval, val);
1317 if (val != oval) {
1318 set_cur_ctl_value(cval, 0, val);
1319 return 1;
1320 }
1321 return 0;
1322}
1323
1324/* alsa control interface for selector unit */
1325static snd_kcontrol_new_t mixer_selectunit_ctl = {
1326 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1327 .name = "", /* will be filled later */
1328 .info = mixer_ctl_selector_info,
1329 .get = mixer_ctl_selector_get,
1330 .put = mixer_ctl_selector_put,
1331};
1332
1333
1334/* private free callback.
1335 * free both private_data and private_value
1336 */
1337static void usb_mixer_selector_elem_free(snd_kcontrol_t *kctl)
1338{
1339 int i, num_ins = 0;
1340
1341 if (kctl->private_data) {
1342 usb_mixer_elem_info_t *cval = kctl->private_data;
1343 num_ins = cval->max;
1344 kfree(cval);
1345 kctl->private_data = NULL;
1346 }
1347 if (kctl->private_value) {
1348 char **itemlist = (char **)kctl->private_value;
1349 for (i = 0; i < num_ins; i++)
1350 kfree(itemlist[i]);
1351 kfree(itemlist);
1352 kctl->private_value = 0;
1353 }
1354}
1355
1356/*
1357 * parse a selector unit
1358 */
1359static int parse_audio_selector_unit(mixer_build_t *state, int unitid, unsigned char *desc)
1360{
1361 unsigned int num_ins = desc[4];
1362 unsigned int i, nameid, len;
1363 int err;
1364 usb_mixer_elem_info_t *cval;
1365 snd_kcontrol_t *kctl;
1366 char **namelist;
1367
1368 if (! num_ins || desc[0] < 6 + num_ins) {
1369 snd_printk(KERN_ERR "invalid SELECTOR UNIT descriptor %d\n", unitid);
1370 return -EINVAL;
1371 }
1372
1373 for (i = 0; i < num_ins; i++) {
1374 if ((err = parse_audio_unit(state, desc[5 + i])) < 0)
1375 return err;
1376 }
1377
1378 if (num_ins == 1) /* only one ? nonsense! */
1379 return 0;
1380
1381 if (check_ignored_ctl(state, unitid, 0))
1382 return 0;
1383
1384 cval = kcalloc(1, sizeof(*cval), GFP_KERNEL);
1385 if (! cval) {
1386 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1387 return -ENOMEM;
1388 }
1389 cval->chip = state->chip;
1390 cval->ctrlif = state->ctrlif;
1391 cval->id = unitid;
1392 cval->val_type = USB_MIXER_U8;
1393 cval->channels = 1;
1394 cval->min = 1;
1395 cval->max = num_ins;
1396 cval->res = 1;
1397 cval->initialized = 1;
1398
1399 namelist = kmalloc(sizeof(char *) * num_ins, GFP_KERNEL);
1400 if (! namelist) {
1401 snd_printk(KERN_ERR "cannot malloc\n");
1402 kfree(cval);
1403 return -ENOMEM;
1404 }
1405#define MAX_ITEM_NAME_LEN 64
1406 for (i = 0; i < num_ins; i++) {
1407 usb_audio_term_t iterm;
1408 len = 0;
1409 namelist[i] = kmalloc(MAX_ITEM_NAME_LEN, GFP_KERNEL);
1410 if (! namelist[i]) {
1411 snd_printk(KERN_ERR "cannot malloc\n");
1412 while (--i > 0)
1413 kfree(namelist[i]);
1414 kfree(namelist);
1415 kfree(cval);
1416 return -ENOMEM;
1417 }
1418 if (check_input_term(state, desc[5 + i], &iterm) >= 0)
1419 len = get_term_name(state, &iterm, namelist[i], MAX_ITEM_NAME_LEN, 0);
1420 if (! len)
1421 sprintf(namelist[i], "Input %d", i);
1422 }
1423
1424 kctl = snd_ctl_new1(&mixer_selectunit_ctl, cval);
1425 if (! kctl) {
1426 snd_printk(KERN_ERR "cannot malloc kcontrol\n");
1427 kfree(cval);
1428 return -ENOMEM;
1429 }
1430 kctl->private_value = (unsigned long)namelist;
1431 kctl->private_free = usb_mixer_selector_elem_free;
1432
1433 nameid = desc[desc[0] - 1];
1434 len = check_mapped_name(state, unitid, 0, kctl->id.name, sizeof(kctl->id.name));
1435 if (len)
1436 ;
1437 else if (nameid)
1438 snd_usb_copy_string_desc(state, nameid, kctl->id.name, sizeof(kctl->id.name));
1439 else {
1440 len = get_term_name(state, &state->oterm,
1441 kctl->id.name, sizeof(kctl->id.name), 0);
1442 if (! len)
1443 strlcpy(kctl->id.name, "USB", sizeof(kctl->id.name));
1444
1445 if ((state->oterm.type & 0xff00) == 0x0100)
1446 strlcat(kctl->id.name, " Capture Source", sizeof(kctl->id.name));
1447 else
1448 strlcat(kctl->id.name, " Playback Source", sizeof(kctl->id.name));
1449 }
1450
1451 snd_printdd(KERN_INFO "[%d] SU [%s] items = %d\n",
1452 cval->id, kctl->id.name, num_ins);
1453 if ((err = add_control_to_empty(state->chip->card, kctl)) < 0)
1454 return err;
1455
1456 return 0;
1457}
1458
1459
1460/*
1461 * parse an audio unit recursively
1462 */
1463
1464static int parse_audio_unit(mixer_build_t *state, int unitid)
1465{
1466 unsigned char *p1;
1467
1468 if (test_and_set_bit(unitid, state->unitbitmap))
1469 return 0; /* the unit already visited */
1470
1471 p1 = find_audio_control_unit(state, unitid);
1472 if (!p1) {
1473 snd_printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
1474 return -EINVAL;
1475 }
1476
1477 switch (p1[2]) {
1478 case INPUT_TERMINAL:
1479 return 0; /* NOP */
1480 case MIXER_UNIT:
1481 return parse_audio_mixer_unit(state, unitid, p1);
1482 case SELECTOR_UNIT:
1483 return parse_audio_selector_unit(state, unitid, p1);
1484 case FEATURE_UNIT:
1485 return parse_audio_feature_unit(state, unitid, p1);
1486 case PROCESSING_UNIT:
1487 return parse_audio_processing_unit(state, unitid, p1);
1488 case EXTENSION_UNIT:
1489 return parse_audio_extension_unit(state, unitid, p1);
1490 default:
1491 snd_printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
1492 return -EINVAL;
1493 }
1494}
1495
1496/*
1497 * create mixer controls
1498 *
1499 * walk through all OUTPUT_TERMINAL descriptors to search for mixers
1500 */
1501int snd_usb_create_mixer(snd_usb_audio_t *chip, int ctrlif)
1502{
1503 unsigned char *desc;
1504 mixer_build_t state;
1505 int err;
1506 const struct usbmix_ctl_map *map;
1507 struct usb_device_descriptor *dev = &chip->dev->descriptor;
1508 struct usb_host_interface *hostif = &usb_ifnum_to_if(chip->dev, ctrlif)->altsetting[0];
1509
1510 strcpy(chip->card->mixername, "USB Mixer");
1511
1512 memset(&state, 0, sizeof(state));
1513 state.chip = chip;
1514 state.buffer = hostif->extra;
1515 state.buflen = hostif->extralen;
1516 state.ctrlif = ctrlif;
1517 state.vendor = le16_to_cpu(dev->idVendor);
1518 state.product = le16_to_cpu(dev->idProduct);
1519
1520 /* check the mapping table */
1521 for (map = usbmix_ctl_maps; map->vendor; map++) {
1522 if (map->vendor == state.vendor && map->product == state.product) {
1523 state.map = map->map;
1524 chip->ignore_ctl_error = map->ignore_ctl_error;
1525 break;
1526 }
1527 }
1528#ifdef IGNORE_CTL_ERROR
1529 chip->ignore_ctl_error = 1;
1530#endif
1531
1532 desc = NULL;
1533 while ((desc = snd_usb_find_csint_desc(hostif->extra, hostif->extralen, desc, OUTPUT_TERMINAL)) != NULL) {
1534 if (desc[0] < 9)
1535 continue; /* invalid descriptor? */
1536 set_bit(desc[3], state.unitbitmap); /* mark terminal ID as visited */
1537 state.oterm.id = desc[3];
1538 state.oterm.type = combine_word(&desc[4]);
1539 state.oterm.name = desc[8];
1540 err = parse_audio_unit(&state, desc[7]);
1541 if (err < 0)
1542 return err;
1543 }
1544 return 0;
1545}
diff --git a/sound/usb/usbmixer_maps.c b/sound/usb/usbmixer_maps.c
new file mode 100644
index 000000000000..c69b4b0875f8
--- /dev/null
+++ b/sound/usb/usbmixer_maps.c
@@ -0,0 +1,135 @@
1/*
2 * Additional mixer mapping
3 *
4 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22
23struct usbmix_name_map {
24 int id;
25 const char *name;
26 int control;
27};
28
29struct usbmix_ctl_map {
30 int vendor;
31 int product;
32 const struct usbmix_name_map *map;
33 int ignore_ctl_error;
34};
35
36/*
37 * USB control mappers for SB Exitigy
38 */
39
40/*
41 * Topology of SB Extigy (see on the wide screen :)
42
43USB_IN[1] --->FU[2]------------------------------+->MU[16]-->PU[17]-+->FU[18]--+->EU[27]--+->EU[21]-->FU[22]--+->FU[23] > Dig_OUT[24]
44 ^ | | | |
45USB_IN[3] -+->SU[5]-->FU[6]--+->MU[14] ->PU[15]->+ | | | +->FU[25] > Dig_OUT[26]
46 ^ ^ | | | |
47Dig_IN[4] -+ | | | | +->FU[28]---------------------> Spk_OUT[19]
48 | | | |
49Lin-IN[7] -+-->FU[8]---------+ | | +----------------------------------------> Hph_OUT[20]
50 | | |
51Mic-IN[9] --+->FU[10]----------------------------+ |
52 || |
53 || +----------------------------------------------------+
54 VV V
55 ++--+->SU[11]-->FU[12] --------------------------------------------------------------------------------------> USB_OUT[13]
56*/
57
58static struct usbmix_name_map extigy_map[] = {
59 /* 1: IT pcm */
60 { 2, "PCM Playback" }, /* FU */
61 /* 3: IT pcm */
62 /* 4: IT digital in */
63 { 5, NULL }, /* DISABLED: this seems to be bogus on some firmware */
64 { 6, "Digital In" }, /* FU */
65 /* 7: IT line */
66 { 8, "Line Playback" }, /* FU */
67 /* 9: IT mic */
68 { 10, "Mic Playback" }, /* FU */
69 { 11, "Capture Input Source" }, /* SU */
70 { 12, "Capture" }, /* FU */
71 /* 13: OT pcm capture */
72 /* 14: MU (w/o controls) */
73 /* 15: PU (3D enh) */
74 /* 16: MU (w/o controls) */
75 { 17, NULL, 1 }, /* DISABLED: PU-switch (any effect?) */
76 { 17, "Channel Routing", 2 }, /* PU: mode select */
77 { 18, "Tone Control - Bass", USB_FEATURE_BASS }, /* FU */
78 { 18, "Tone Control - Treble", USB_FEATURE_TREBLE }, /* FU */
79 { 18, "Master Playback" }, /* FU; others */
80 /* 19: OT speaker */
81 /* 20: OT headphone */
82 { 21, NULL }, /* DISABLED: EU (for what?) */
83 { 22, "Digital Out Playback" }, /* FU */
84 { 23, "Digital Out1 Playback" }, /* FU */ /* FIXME: corresponds to 24 */
85 /* 24: OT digital out */
86 { 25, "IEC958 Optical Playback" }, /* FU */
87 { 26, "IEC958 Optical Playback" }, /* OT */
88 { 27, NULL }, /* DISABLED: EU (for what?) */
89 /* 28: FU speaker (mute) */
90 { 29, NULL }, /* Digital Input Playback Source? */
91 { 0 } /* terminator */
92};
93
94/* LineX FM Transmitter entry - needed to bypass controls bug */
95static struct usbmix_name_map linex_map[] = {
96 /* 1: IT pcm */
97 /* 2: OT Speaker */
98 { 3, "Master" }, /* FU: master volume - left / right / mute */
99 { 0 } /* terminator */
100};
101
102/* Section "justlink_map" below added by James Courtier-Dutton <James@superbug.demon.co.uk>
103 * sourced from Maplin Electronics (http://www.maplin.co.uk), part number A56AK
104 * Part has 2 connectors that act as a single output. (TOSLINK Optical for digital out, and 3.5mm Jack for Analogue out.)
105 * The USB Mixer publishes a Microphone and extra Volume controls for it, but none exist on the device,
106 * so this map removes all unwanted sliders from alsamixer
107 */
108
109static struct usbmix_name_map justlink_map[] = {
110 /* 1: IT pcm playback */
111 /* 2: Not present */
112 { 3, NULL}, /* IT mic (No mic input on device) */
113 /* 4: Not present */
114 /* 5: OT speacker */
115 /* 6: OT pcm capture */
116 { 7, "Master Playback" }, /* Mute/volume for speaker */
117 { 8, NULL }, /* Capture Switch (No capture inputs on device) */
118 { 9, NULL }, /* Capture Mute/volume (No capture inputs on device */
119 /* 0xa: Not present */
120 /* 0xb: MU (w/o controls) */
121 { 0xc, NULL }, /* Mic feedback Mute/volume (No capture inputs on device) */
122 { 0 } /* terminator */
123};
124
125/*
126 * Control map entries
127 */
128
129static struct usbmix_ctl_map usbmix_ctl_maps[] = {
130 { 0x41e, 0x3000, extigy_map, 1 },
131 { 0x8bb, 0x2702, linex_map, 1 },
132 { 0xc45, 0x1158, justlink_map, 0 },
133 { 0 } /* terminator */
134};
135
diff --git a/sound/usb/usbquirks.h b/sound/usb/usbquirks.h
new file mode 100644
index 000000000000..88bbd944d4be
--- /dev/null
+++ b/sound/usb/usbquirks.h
@@ -0,0 +1,1202 @@
1/*
2 * ALSA USB Audio Driver
3 *
4 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>,
5 * Clemens Ladisch <clemens@ladisch.de>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/*
24 * The contents of this file are part of the driver's id_table.
25 *
26 * In a perfect world, this file would be empty.
27 */
28
29/*
30 * Use this for devices where other interfaces are standard compliant,
31 * to prevent the quirk being applied to those interfaces. (To work with
32 * hotplugging, bDeviceClass must be set to USB_CLASS_PER_INTERFACE.)
33 */
34#define USB_DEVICE_VENDOR_SPEC(vend, prod) \
35 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | \
36 USB_DEVICE_ID_MATCH_PRODUCT | \
37 USB_DEVICE_ID_MATCH_INT_CLASS, \
38 .idVendor = vend, \
39 .idProduct = prod, \
40 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
41
42/*
43 * Yamaha devices
44 */
45
46#define YAMAHA_DEVICE(id, name) { \
47 USB_DEVICE(0x0499, id), \
48 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) { \
49 .vendor_name = "Yamaha", \
50 .product_name = name, \
51 .ifnum = QUIRK_ANY_INTERFACE, \
52 .type = QUIRK_MIDI_YAMAHA \
53 } \
54}
55#define YAMAHA_INTERFACE(id, intf, name) { \
56 USB_DEVICE_VENDOR_SPEC(0x0499, id), \
57 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) { \
58 .vendor_name = "Yamaha", \
59 .product_name = name, \
60 .ifnum = intf, \
61 .type = QUIRK_MIDI_YAMAHA \
62 } \
63}
64YAMAHA_DEVICE(0x1000, "UX256"),
65YAMAHA_DEVICE(0x1001, "MU1000"),
66YAMAHA_DEVICE(0x1002, "MU2000"),
67YAMAHA_DEVICE(0x1003, "MU500"),
68YAMAHA_INTERFACE(0x1004, 3, "UW500"),
69YAMAHA_DEVICE(0x1005, "MOTIF6"),
70YAMAHA_DEVICE(0x1006, "MOTIF7"),
71YAMAHA_DEVICE(0x1007, "MOTIF8"),
72YAMAHA_DEVICE(0x1008, "UX96"),
73YAMAHA_DEVICE(0x1009, "UX16"),
74YAMAHA_INTERFACE(0x100a, 3, "EOS BX"),
75YAMAHA_DEVICE(0x100c, "UC-MX"),
76YAMAHA_DEVICE(0x100d, "UC-KX"),
77YAMAHA_DEVICE(0x100e, "S08"),
78YAMAHA_DEVICE(0x100f, "CLP-150"),
79YAMAHA_DEVICE(0x1010, "CLP-170"),
80YAMAHA_DEVICE(0x1011, "P-250"),
81YAMAHA_DEVICE(0x1012, "TYROS"),
82YAMAHA_DEVICE(0x1013, "PF-500"),
83YAMAHA_DEVICE(0x1014, "S90"),
84YAMAHA_DEVICE(0x1015, "MOTIF-R"),
85YAMAHA_DEVICE(0x1017, "CVP-204"),
86YAMAHA_DEVICE(0x1018, "CVP-206"),
87YAMAHA_DEVICE(0x1019, "CVP-208"),
88YAMAHA_DEVICE(0x101a, "CVP-210"),
89YAMAHA_DEVICE(0x101b, "PSR-1100"),
90YAMAHA_DEVICE(0x101c, "PSR-2100"),
91YAMAHA_DEVICE(0x101d, "CLP-175"),
92YAMAHA_DEVICE(0x101e, "PSR-K1"),
93YAMAHA_DEVICE(0x1020, "EZ-250i"),
94YAMAHA_DEVICE(0x1021, "MOTIF ES 6"),
95YAMAHA_DEVICE(0x1022, "MOTIF ES 7"),
96YAMAHA_DEVICE(0x1023, "MOTIF ES 8"),
97YAMAHA_DEVICE(0x1024, "CVP-301"),
98YAMAHA_DEVICE(0x1025, "CVP-303"),
99YAMAHA_DEVICE(0x1026, "CVP-305"),
100YAMAHA_DEVICE(0x1027, "CVP-307"),
101YAMAHA_DEVICE(0x1028, "CVP-309"),
102YAMAHA_DEVICE(0x1029, "CVP-309GP"),
103YAMAHA_DEVICE(0x102a, "PSR-1500"),
104YAMAHA_DEVICE(0x102b, "PSR-3000"),
105YAMAHA_DEVICE(0x102e, "ELS-01/01C"),
106YAMAHA_DEVICE(0x1030, "PSR-295/293"),
107YAMAHA_DEVICE(0x1031, "DGX-205/203"),
108YAMAHA_DEVICE(0x1032, "DGX-305"),
109YAMAHA_DEVICE(0x1033, "DGX-505"),
110YAMAHA_DEVICE(0x1034, NULL),
111YAMAHA_DEVICE(0x1035, NULL),
112YAMAHA_DEVICE(0x1036, NULL),
113YAMAHA_DEVICE(0x1037, NULL),
114YAMAHA_DEVICE(0x1038, NULL),
115YAMAHA_DEVICE(0x1039, NULL),
116YAMAHA_DEVICE(0x103a, NULL),
117YAMAHA_DEVICE(0x103b, NULL),
118YAMAHA_DEVICE(0x103c, NULL),
119YAMAHA_DEVICE(0x2000, "DGP-7"),
120YAMAHA_DEVICE(0x2001, "DGP-5"),
121YAMAHA_DEVICE(0x2002, NULL),
122YAMAHA_DEVICE(0x5000, "CS1D"),
123YAMAHA_DEVICE(0x5001, "DSP1D"),
124YAMAHA_DEVICE(0x5002, "DME32"),
125YAMAHA_DEVICE(0x5003, "DM2000"),
126YAMAHA_DEVICE(0x5004, "02R96"),
127YAMAHA_DEVICE(0x5005, "ACU16-C"),
128YAMAHA_DEVICE(0x5006, "NHB32-C"),
129YAMAHA_DEVICE(0x5007, "DM1000"),
130YAMAHA_DEVICE(0x5008, "01V96"),
131YAMAHA_DEVICE(0x5009, "SPX2000"),
132YAMAHA_DEVICE(0x500a, "PM5D"),
133YAMAHA_DEVICE(0x500b, "DME64N"),
134YAMAHA_DEVICE(0x500c, "DME24N"),
135YAMAHA_DEVICE(0x500d, NULL),
136YAMAHA_DEVICE(0x500e, NULL),
137YAMAHA_DEVICE(0x7000, "DTX"),
138YAMAHA_DEVICE(0x7010, "UB99"),
139#undef YAMAHA_DEVICE
140#undef YAMAHA_INTERFACE
141
142/*
143 * Roland/RolandED/Edirol/BOSS devices
144 */
145{
146 USB_DEVICE(0x0582, 0x0000),
147 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
148 .vendor_name = "Roland",
149 .product_name = "UA-100",
150 .ifnum = QUIRK_ANY_INTERFACE,
151 .type = QUIRK_COMPOSITE,
152 .data = (const snd_usb_audio_quirk_t[]) {
153 {
154 .ifnum = 0,
155 .type = QUIRK_AUDIO_FIXED_ENDPOINT,
156 .data = & (const struct audioformat) {
157 .format = SNDRV_PCM_FORMAT_S16_LE,
158 .channels = 4,
159 .iface = 0,
160 .altsetting = 1,
161 .altset_idx = 1,
162 .attributes = 0,
163 .endpoint = 0x01,
164 .ep_attr = 0x09,
165 .rates = SNDRV_PCM_RATE_CONTINUOUS,
166 .rate_min = 44100,
167 .rate_max = 44100,
168 }
169 },
170 {
171 .ifnum = 1,
172 .type = QUIRK_AUDIO_FIXED_ENDPOINT,
173 .data = & (const struct audioformat) {
174 .format = SNDRV_PCM_FORMAT_S16_LE,
175 .channels = 2,
176 .iface = 1,
177 .altsetting = 1,
178 .altset_idx = 1,
179 .attributes = EP_CS_ATTR_FILL_MAX,
180 .endpoint = 0x81,
181 .ep_attr = 0x05,
182 .rates = SNDRV_PCM_RATE_CONTINUOUS,
183 .rate_min = 44100,
184 .rate_max = 44100,
185 }
186 },
187 {
188 .ifnum = 2,
189 .type = QUIRK_MIDI_FIXED_ENDPOINT,
190 .data = & (const snd_usb_midi_endpoint_info_t) {
191 .out_cables = 0x0007,
192 .in_cables = 0x0007
193 }
194 },
195 {
196 .ifnum = -1
197 }
198 }
199 }
200},
201{
202 USB_DEVICE(0x0582, 0x0002),
203 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
204 .vendor_name = "EDIROL",
205 .product_name = "UM-4",
206 .ifnum = 2,
207 .type = QUIRK_MIDI_FIXED_ENDPOINT,
208 .data = & (const snd_usb_midi_endpoint_info_t) {
209 .out_cables = 0x000f,
210 .in_cables = 0x000f
211 }
212 }
213},
214{
215 USB_DEVICE(0x0582, 0x0003),
216 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
217 .vendor_name = "Roland",
218 .product_name = "SC-8850",
219 .ifnum = 2,
220 .type = QUIRK_MIDI_FIXED_ENDPOINT,
221 .data = & (const snd_usb_midi_endpoint_info_t) {
222 .out_cables = 0x003f,
223 .in_cables = 0x003f
224 }
225 }
226},
227{
228 USB_DEVICE(0x0582, 0x0004),
229 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
230 .vendor_name = "Roland",
231 .product_name = "U-8",
232 .ifnum = 2,
233 .type = QUIRK_MIDI_FIXED_ENDPOINT,
234 .data = & (const snd_usb_midi_endpoint_info_t) {
235 .out_cables = 0x0005,
236 .in_cables = 0x0005
237 }
238 }
239},
240{
241 USB_DEVICE(0x0582, 0x0005),
242 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
243 .vendor_name = "EDIROL",
244 .product_name = "UM-2",
245 .ifnum = 2,
246 .type = QUIRK_MIDI_FIXED_ENDPOINT,
247 .data = & (const snd_usb_midi_endpoint_info_t) {
248 .out_cables = 0x0003,
249 .in_cables = 0x0003
250 }
251 }
252},
253{
254 USB_DEVICE(0x0582, 0x0007),
255 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
256 .vendor_name = "Roland",
257 .product_name = "SC-8820",
258 .ifnum = 2,
259 .type = QUIRK_MIDI_FIXED_ENDPOINT,
260 .data = & (const snd_usb_midi_endpoint_info_t) {
261 .out_cables = 0x0013,
262 .in_cables = 0x0013
263 }
264 }
265},
266{
267 USB_DEVICE(0x0582, 0x0008),
268 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
269 .vendor_name = "Roland",
270 .product_name = "PC-300",
271 .ifnum = 2,
272 .type = QUIRK_MIDI_FIXED_ENDPOINT,
273 .data = & (const snd_usb_midi_endpoint_info_t) {
274 .out_cables = 0x0001,
275 .in_cables = 0x0001
276 }
277 }
278},
279{
280 USB_DEVICE(0x0582, 0x0009),
281 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
282 .vendor_name = "EDIROL",
283 .product_name = "UM-1",
284 .ifnum = 2,
285 .type = QUIRK_MIDI_FIXED_ENDPOINT,
286 .data = & (const snd_usb_midi_endpoint_info_t) {
287 .out_cables = 0x0001,
288 .in_cables = 0x0001
289 }
290 }
291},
292{
293 USB_DEVICE(0x0582, 0x000b),
294 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
295 .vendor_name = "Roland",
296 .product_name = "SK-500",
297 .ifnum = 2,
298 .type = QUIRK_MIDI_FIXED_ENDPOINT,
299 .data = & (const snd_usb_midi_endpoint_info_t) {
300 .out_cables = 0x0013,
301 .in_cables = 0x0013
302 }
303 }
304},
305{
306 /* thanks to Emiliano Grilli <emillo@libero.it>
307 * for helping researching this data */
308 USB_DEVICE(0x0582, 0x000c),
309 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
310 .vendor_name = "Roland",
311 .product_name = "SC-D70",
312 .ifnum = QUIRK_ANY_INTERFACE,
313 .type = QUIRK_COMPOSITE,
314 .data = (const snd_usb_audio_quirk_t[]) {
315 {
316 .ifnum = 0,
317 .type = QUIRK_AUDIO_FIXED_ENDPOINT,
318 .data = & (const struct audioformat) {
319 .format = SNDRV_PCM_FORMAT_S24_3LE,
320 .channels = 2,
321 .iface = 0,
322 .altsetting = 1,
323 .altset_idx = 1,
324 .attributes = 0,
325 .endpoint = 0x01,
326 .ep_attr = 0x01,
327 .rates = SNDRV_PCM_RATE_CONTINUOUS,
328 .rate_min = 44100,
329 .rate_max = 44100,
330 }
331 },
332 {
333 .ifnum = 1,
334 .type = QUIRK_AUDIO_FIXED_ENDPOINT,
335 .data = & (const struct audioformat) {
336 .format = SNDRV_PCM_FORMAT_S24_3LE,
337 .channels = 2,
338 .iface = 1,
339 .altsetting = 1,
340 .altset_idx = 1,
341 .attributes = 0,
342 .endpoint = 0x81,
343 .ep_attr = 0x01,
344 .rates = SNDRV_PCM_RATE_CONTINUOUS,
345 .rate_min = 44100,
346 .rate_max = 44100,
347 }
348 },
349 {
350 .ifnum = 2,
351 .type = QUIRK_MIDI_FIXED_ENDPOINT,
352 .data = & (const snd_usb_midi_endpoint_info_t) {
353 .out_cables = 0x0007,
354 .in_cables = 0x0007
355 }
356 },
357 {
358 .ifnum = -1
359 }
360 }
361 }
362},
363{ /*
364 * This quirk is for the "Advanced Driver" mode of the Edirol UA-5.
365 * If the advanced mode switch at the back of the unit is off, the
366 * UA-5 has ID 0x0582/0x0011 and is standard compliant (no quirks),
367 * but offers only 16-bit PCM.
368 * In advanced mode, the UA-5 will output S24_3LE samples (two
369 * channels) at the rate indicated on the front switch, including
370 * the 96kHz sample rate.
371 */
372 USB_DEVICE(0x0582, 0x0010),
373 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
374 .vendor_name = "EDIROL",
375 .product_name = "UA-5",
376 .ifnum = QUIRK_ANY_INTERFACE,
377 .type = QUIRK_COMPOSITE,
378 .data = (const snd_usb_audio_quirk_t[]) {
379 {
380 .ifnum = 1,
381 .type = QUIRK_AUDIO_STANDARD_INTERFACE
382 },
383 {
384 .ifnum = 2,
385 .type = QUIRK_AUDIO_STANDARD_INTERFACE
386 },
387 {
388 .ifnum = -1
389 }
390 }
391 }
392},
393{
394 USB_DEVICE(0x0582, 0x0012),
395 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
396 .vendor_name = "Roland",
397 .product_name = "XV-5050",
398 .ifnum = 0,
399 .type = QUIRK_MIDI_FIXED_ENDPOINT,
400 .data = & (const snd_usb_midi_endpoint_info_t) {
401 .out_cables = 0x0001,
402 .in_cables = 0x0001
403 }
404 }
405},
406{
407 USB_DEVICE(0x0582, 0x0014),
408 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
409 .vendor_name = "EDIROL",
410 .product_name = "UM-880",
411 .ifnum = 0,
412 .type = QUIRK_MIDI_FIXED_ENDPOINT,
413 .data = & (const snd_usb_midi_endpoint_info_t) {
414 .out_cables = 0x01ff,
415 .in_cables = 0x01ff
416 }
417 }
418},
419{
420 USB_DEVICE(0x0582, 0x0016),
421 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
422 .vendor_name = "EDIROL",
423 .product_name = "SD-90",
424 .ifnum = 2,
425 .type = QUIRK_MIDI_FIXED_ENDPOINT,
426 .data = & (const snd_usb_midi_endpoint_info_t) {
427 .out_cables = 0x000f,
428 .in_cables = 0x000f
429 }
430 }
431},
432{
433 USB_DEVICE(0x0582, 0x001b),
434 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
435 .vendor_name = "Roland",
436 .product_name = "MMP-2",
437 .ifnum = 2,
438 .type = QUIRK_MIDI_FIXED_ENDPOINT,
439 .data = & (const snd_usb_midi_endpoint_info_t) {
440 .out_cables = 0x0001,
441 .in_cables = 0x0001
442 }
443 }
444},
445{
446 USB_DEVICE(0x0582, 0x001d),
447 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
448 .vendor_name = "Roland",
449 .product_name = "V-SYNTH",
450 .ifnum = 0,
451 .type = QUIRK_MIDI_FIXED_ENDPOINT,
452 .data = & (const snd_usb_midi_endpoint_info_t) {
453 .out_cables = 0x0001,
454 .in_cables = 0x0001
455 }
456 }
457},
458{
459 USB_DEVICE(0x0582, 0x0023),
460 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
461 .vendor_name = "EDIROL",
462 .product_name = "UM-550",
463 .ifnum = 0,
464 .type = QUIRK_MIDI_FIXED_ENDPOINT,
465 .data = & (const snd_usb_midi_endpoint_info_t) {
466 .out_cables = 0x003f,
467 .in_cables = 0x003f
468 }
469 }
470},
471{
472 /*
473 * This quirk is for the "Advanced Driver" mode. If off, the UA-20
474 * has ID 0x0026 and is standard compliant, but has only 16-bit PCM
475 * and no MIDI.
476 */
477 USB_DEVICE(0x0582, 0x0025),
478 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
479 .vendor_name = "EDIROL",
480 .product_name = "UA-20",
481 .ifnum = QUIRK_ANY_INTERFACE,
482 .type = QUIRK_COMPOSITE,
483 .data = (const snd_usb_audio_quirk_t[]) {
484 {
485 .ifnum = 1,
486 .type = QUIRK_AUDIO_STANDARD_INTERFACE
487 },
488 {
489 .ifnum = 2,
490 .type = QUIRK_AUDIO_STANDARD_INTERFACE
491 },
492 {
493 .ifnum = 3,
494 .type = QUIRK_MIDI_STANDARD_INTERFACE
495 },
496 {
497 .ifnum = -1
498 }
499 }
500 }
501},
502{
503 USB_DEVICE(0x0582, 0x0027),
504 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
505 .vendor_name = "EDIROL",
506 .product_name = "SD-20",
507 .ifnum = 0,
508 .type = QUIRK_MIDI_FIXED_ENDPOINT,
509 .data = & (const snd_usb_midi_endpoint_info_t) {
510 .out_cables = 0x0003,
511 .in_cables = 0x0007
512 }
513 }
514},
515{
516 USB_DEVICE(0x0582, 0x0029),
517 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
518 .vendor_name = "EDIROL",
519 .product_name = "SD-80",
520 .ifnum = 0,
521 .type = QUIRK_MIDI_FIXED_ENDPOINT,
522 .data = & (const snd_usb_midi_endpoint_info_t) {
523 .out_cables = 0x000f,
524 .in_cables = 0x000f
525 }
526 }
527},
528{ /*
529 * This quirk is for the "Advanced" modes of the Edirol UA-700.
530 * If the sample format switch is not in an advanced setting, the
531 * UA-700 has ID 0x0582/0x002c and is standard compliant (no quirks),
532 * but offers only 16-bit PCM and no MIDI.
533 */
534 USB_DEVICE_VENDOR_SPEC(0x0582, 0x002b),
535 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
536 .vendor_name = "EDIROL",
537 .product_name = "UA-700",
538 .ifnum = QUIRK_ANY_INTERFACE,
539 .type = QUIRK_COMPOSITE,
540 .data = (const snd_usb_audio_quirk_t[]) {
541 {
542 .ifnum = 1,
543 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
544 },
545 {
546 .ifnum = 2,
547 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
548 },
549 {
550 .ifnum = 3,
551 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
552 },
553 {
554 .ifnum = -1
555 }
556 }
557 }
558},
559{
560 USB_DEVICE(0x0582, 0x002d),
561 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
562 .vendor_name = "Roland",
563 .product_name = "XV-2020",
564 .ifnum = 0,
565 .type = QUIRK_MIDI_FIXED_ENDPOINT,
566 .data = & (const snd_usb_midi_endpoint_info_t) {
567 .out_cables = 0x0001,
568 .in_cables = 0x0001
569 }
570 }
571},
572{
573 USB_DEVICE(0x0582, 0x002f),
574 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
575 .vendor_name = "Roland",
576 .product_name = "VariOS",
577 .ifnum = 0,
578 .type = QUIRK_MIDI_FIXED_ENDPOINT,
579 .data = & (const snd_usb_midi_endpoint_info_t) {
580 .out_cables = 0x0007,
581 .in_cables = 0x0007
582 }
583 }
584},
585{
586 USB_DEVICE(0x0582, 0x0033),
587 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
588 .vendor_name = "EDIROL",
589 .product_name = "PCR",
590 .ifnum = 0,
591 .type = QUIRK_MIDI_FIXED_ENDPOINT,
592 .data = & (const snd_usb_midi_endpoint_info_t) {
593 .out_cables = 0x0003,
594 .in_cables = 0x0007
595 }
596 }
597},
598{
599 USB_DEVICE(0x0582, 0x0037),
600 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
601 .vendor_name = "Roland",
602 .product_name = "Digital Piano",
603 .ifnum = 0,
604 .type = QUIRK_MIDI_FIXED_ENDPOINT,
605 .data = & (const snd_usb_midi_endpoint_info_t) {
606 .out_cables = 0x0001,
607 .in_cables = 0x0001
608 }
609 }
610},
611{
612 USB_DEVICE_VENDOR_SPEC(0x0582, 0x003b),
613 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
614 .vendor_name = "BOSS",
615 .product_name = "GS-10",
616 .ifnum = 3,
617 .type = QUIRK_MIDI_FIXED_ENDPOINT,
618 .data = & (const snd_usb_midi_endpoint_info_t) {
619 .out_cables = 0x0003,
620 .in_cables = 0x0003
621 }
622 }
623},
624{
625 USB_DEVICE(0x0582, 0x0040),
626 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
627 .vendor_name = "Roland",
628 .product_name = "GI-20",
629 .ifnum = 0,
630 .type = QUIRK_MIDI_FIXED_ENDPOINT,
631 .data = & (const snd_usb_midi_endpoint_info_t) {
632 .out_cables = 0x0001,
633 .in_cables = 0x0001
634 }
635 }
636},
637{
638 USB_DEVICE(0x0582, 0x0042),
639 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
640 .vendor_name = "Roland",
641 .product_name = "RS-70",
642 .ifnum = 0,
643 .type = QUIRK_MIDI_FIXED_ENDPOINT,
644 .data = & (const snd_usb_midi_endpoint_info_t) {
645 .out_cables = 0x0001,
646 .in_cables = 0x0001
647 }
648 }
649},
650{
651 USB_DEVICE(0x0582, 0x0044),
652 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
653 .vendor_name = "Roland",
654 .product_name = "UA-1000",
655 .ifnum = QUIRK_ANY_INTERFACE,
656 .type = QUIRK_COMPOSITE,
657 .data = (const snd_usb_audio_quirk_t[]) {
658 {
659 .ifnum = 1,
660 .type = QUIRK_AUDIO_EDIROL_UA1000
661 },
662 {
663 .ifnum = 2,
664 .type = QUIRK_AUDIO_EDIROL_UA1000
665 },
666 {
667 .ifnum = 3,
668 .type = QUIRK_MIDI_FIXED_ENDPOINT,
669 .data = & (const snd_usb_midi_endpoint_info_t) {
670 .out_cables = 0x0003,
671 .in_cables = 0x0003
672 }
673 },
674 {
675 .ifnum = -1
676 }
677 }
678 }
679},
680{
681 USB_DEVICE(0x0582, 0x0048),
682 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
683 .vendor_name = "EDIROL",
684 .product_name = "UR-80",
685 .ifnum = 0,
686 .type = QUIRK_MIDI_FIXED_ENDPOINT,
687 .data = & (const snd_usb_midi_endpoint_info_t) {
688 .out_cables = 0x0003,
689 .in_cables = 0x0007
690 }
691 }
692},
693{
694 USB_DEVICE(0x0582, 0x004d),
695 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
696 .vendor_name = "EDIROL",
697 .product_name = "PCR-A",
698 .ifnum = 0,
699 .type = QUIRK_MIDI_FIXED_ENDPOINT,
700 .data = & (const snd_usb_midi_endpoint_info_t) {
701 .out_cables = 0x0003,
702 .in_cables = 0x0007
703 }
704 }
705},
706{
707 /*
708 * This quirk is for the "Advanced Driver" mode. If off, the UA-3FX
709 * is standard compliant, but has only 16-bit PCM.
710 */
711 USB_DEVICE(0x0582, 0x0050),
712 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
713 .vendor_name = "EDIROL",
714 .product_name = "UA-3FX",
715 .ifnum = QUIRK_ANY_INTERFACE,
716 .type = QUIRK_COMPOSITE,
717 .data = (const snd_usb_audio_quirk_t[]) {
718 {
719 .ifnum = 1,
720 .type = QUIRK_AUDIO_STANDARD_INTERFACE
721 },
722 {
723 .ifnum = 2,
724 .type = QUIRK_AUDIO_STANDARD_INTERFACE
725 },
726 {
727 .ifnum = -1
728 }
729 }
730 }
731},
732{
733 USB_DEVICE(0x0582, 0x0052),
734 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
735 .vendor_name = "EDIROL",
736 .product_name = "UM-1SX",
737 .ifnum = 0,
738 .type = QUIRK_MIDI_STANDARD_INTERFACE
739 }
740},
741{
742 USB_DEVICE(0x0582, 0x0065),
743 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
744 .vendor_name = "EDIROL",
745 .product_name = "PCR-1",
746 .ifnum = 0,
747 .type = QUIRK_MIDI_FIXED_ENDPOINT,
748 .data = & (const snd_usb_midi_endpoint_info_t) {
749 .out_cables = 0x0001,
750 .in_cables = 0x0003
751 }
752 }
753},
754{
755 USB_DEVICE_VENDOR_SPEC(0x0582, 0x006a),
756 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
757 .vendor_name = "Roland",
758 .product_name = "SP-606",
759 .ifnum = 3,
760 .type = QUIRK_MIDI_FIXED_ENDPOINT,
761 .data = & (const snd_usb_midi_endpoint_info_t) {
762 .out_cables = 0x0001,
763 .in_cables = 0x0001
764 }
765 }
766},
767{
768 USB_DEVICE(0x0582, 0x006d),
769 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
770 .vendor_name = "Roland",
771 .product_name = "FANTOM-X",
772 .ifnum = 0,
773 .type = QUIRK_MIDI_FIXED_ENDPOINT,
774 .data = & (const snd_usb_midi_endpoint_info_t) {
775 .out_cables = 0x0001,
776 .in_cables = 0x0001
777 }
778 }
779},
780{ /*
781 * This quirk is for the "Advanced" modes of the Edirol UA-25.
782 * If the switch is not in an advanced setting, the UA-25 has
783 * ID 0x0582/0x0073 and is standard compliant (no quirks), but
784 * offers only 16-bit PCM at 44.1 kHz and no MIDI.
785 */
786 USB_DEVICE_VENDOR_SPEC(0x0582, 0x0074),
787 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
788 .vendor_name = "EDIROL",
789 .product_name = "UA-25",
790 .ifnum = QUIRK_ANY_INTERFACE,
791 .type = QUIRK_COMPOSITE,
792 .data = (const snd_usb_audio_quirk_t[]) {
793 {
794 .ifnum = 0,
795 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
796 },
797 {
798 .ifnum = 1,
799 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
800 },
801 {
802 .ifnum = 2,
803 .type = QUIRK_AUDIO_EDIROL_UA700_UA25
804 },
805 {
806 .ifnum = -1
807 }
808 }
809 }
810},
811{
812 USB_DEVICE(0x0582, 0x0075),
813 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
814 .vendor_name = "BOSS",
815 .product_name = "DR-880",
816 .ifnum = 0,
817 .type = QUIRK_MIDI_FIXED_ENDPOINT,
818 .data = & (const snd_usb_midi_endpoint_info_t) {
819 .out_cables = 0x0001,
820 .in_cables = 0x0001
821 }
822 }
823},
824
825/* Midiman/M-Audio devices */
826{
827 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1002),
828 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
829 .vendor_name = "M-Audio",
830 .product_name = "MidiSport 2x2",
831 .ifnum = QUIRK_ANY_INTERFACE,
832 .type = QUIRK_MIDI_MIDIMAN,
833 .data = & (const snd_usb_midi_endpoint_info_t) {
834 .out_cables = 0x0003,
835 .in_cables = 0x0003
836 }
837 }
838},
839{
840 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1011),
841 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
842 .vendor_name = "M-Audio",
843 .product_name = "MidiSport 1x1",
844 .ifnum = QUIRK_ANY_INTERFACE,
845 .type = QUIRK_MIDI_MIDIMAN,
846 .data = & (const snd_usb_midi_endpoint_info_t) {
847 .out_cables = 0x0001,
848 .in_cables = 0x0001
849 }
850 }
851},
852{
853 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1015),
854 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
855 .vendor_name = "M-Audio",
856 .product_name = "Keystation",
857 .ifnum = QUIRK_ANY_INTERFACE,
858 .type = QUIRK_MIDI_MIDIMAN,
859 .data = & (const snd_usb_midi_endpoint_info_t) {
860 .out_cables = 0x0001,
861 .in_cables = 0x0001
862 }
863 }
864},
865{
866 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1021),
867 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
868 .vendor_name = "M-Audio",
869 .product_name = "MidiSport 4x4",
870 .ifnum = QUIRK_ANY_INTERFACE,
871 .type = QUIRK_MIDI_MIDIMAN,
872 .data = & (const snd_usb_midi_endpoint_info_t) {
873 .out_cables = 0x000f,
874 .in_cables = 0x000f
875 }
876 }
877},
878{
879 /*
880 * For hardware revision 1.05; in the later revisions (1.10 and
881 * 1.21), 0x1031 is the ID for the device without firmware.
882 * Thanks to Olaf Giesbrecht <Olaf_Giesbrecht@yahoo.de>
883 */
884 USB_DEVICE_VER(0x0763, 0x1031, 0x0100, 0x0109),
885 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
886 .vendor_name = "M-Audio",
887 .product_name = "MidiSport 8x8",
888 .ifnum = QUIRK_ANY_INTERFACE,
889 .type = QUIRK_MIDI_MIDIMAN,
890 .data = & (const snd_usb_midi_endpoint_info_t) {
891 .out_cables = 0x01ff,
892 .in_cables = 0x01ff
893 }
894 }
895},
896{
897 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1033),
898 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
899 .vendor_name = "M-Audio",
900 .product_name = "MidiSport 8x8",
901 .ifnum = QUIRK_ANY_INTERFACE,
902 .type = QUIRK_MIDI_MIDIMAN,
903 .data = & (const snd_usb_midi_endpoint_info_t) {
904 .out_cables = 0x01ff,
905 .in_cables = 0x01ff
906 }
907 }
908},
909{
910 USB_DEVICE_VENDOR_SPEC(0x0763, 0x1041),
911 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
912 .vendor_name = "M-Audio",
913 .product_name = "MidiSport 2x4",
914 .ifnum = QUIRK_ANY_INTERFACE,
915 .type = QUIRK_MIDI_MIDIMAN,
916 .data = & (const snd_usb_midi_endpoint_info_t) {
917 .out_cables = 0x000f,
918 .in_cables = 0x0003
919 }
920 }
921},
922{
923 USB_DEVICE_VENDOR_SPEC(0x0763, 0x2001),
924 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
925 .vendor_name = "M-Audio",
926 .product_name = "Quattro",
927 .ifnum = QUIRK_ANY_INTERFACE,
928 .type = QUIRK_COMPOSITE,
929 .data = & (const snd_usb_audio_quirk_t[]) {
930 /*
931 * Interfaces 0-2 are "Windows-compatible", 16-bit only,
932 * and share endpoints with the other interfaces.
933 * Ignore them. The other interfaces can do 24 bits,
934 * but captured samples are big-endian (see usbaudio.c).
935 */
936 {
937 .ifnum = 0,
938 .type = QUIRK_IGNORE_INTERFACE
939 },
940 {
941 .ifnum = 1,
942 .type = QUIRK_IGNORE_INTERFACE
943 },
944 {
945 .ifnum = 2,
946 .type = QUIRK_IGNORE_INTERFACE
947 },
948 {
949 .ifnum = 3,
950 .type = QUIRK_IGNORE_INTERFACE
951 },
952 {
953 .ifnum = 4,
954 .type = QUIRK_AUDIO_STANDARD_INTERFACE
955 },
956 {
957 .ifnum = 5,
958 .type = QUIRK_AUDIO_STANDARD_INTERFACE
959 },
960 {
961 .ifnum = 6,
962 .type = QUIRK_IGNORE_INTERFACE
963 },
964 {
965 .ifnum = 7,
966 .type = QUIRK_AUDIO_STANDARD_INTERFACE
967 },
968 {
969 .ifnum = 8,
970 .type = QUIRK_AUDIO_STANDARD_INTERFACE
971 },
972 {
973 .ifnum = 9,
974 .type = QUIRK_MIDI_MIDIMAN,
975 .data = & (const snd_usb_midi_endpoint_info_t) {
976 .out_cables = 0x0001,
977 .in_cables = 0x0001
978 }
979 },
980 {
981 .ifnum = -1
982 }
983 }
984 }
985},
986{
987 USB_DEVICE_VENDOR_SPEC(0x0763, 0x2003),
988 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
989 .vendor_name = "M-Audio",
990 .product_name = "AudioPhile",
991 .ifnum = 6,
992 .type = QUIRK_MIDI_MIDIMAN,
993 .data = & (const snd_usb_midi_endpoint_info_t) {
994 .out_cables = 0x0001,
995 .in_cables = 0x0001
996 }
997 }
998},
999{
1000 USB_DEVICE_VENDOR_SPEC(0x0763, 0x2008),
1001 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1002 .vendor_name = "M-Audio",
1003 .product_name = "Ozone",
1004 .ifnum = 3,
1005 .type = QUIRK_MIDI_MIDIMAN,
1006 .data = & (const snd_usb_midi_endpoint_info_t) {
1007 .out_cables = 0x0001,
1008 .in_cables = 0x0001
1009 }
1010 }
1011},
1012{
1013 USB_DEVICE_VENDOR_SPEC(0x0763, 0x200d),
1014 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1015 .vendor_name = "M-Audio",
1016 .product_name = "OmniStudio",
1017 .ifnum = QUIRK_ANY_INTERFACE,
1018 .type = QUIRK_COMPOSITE,
1019 .data = & (const snd_usb_audio_quirk_t[]) {
1020 {
1021 .ifnum = 0,
1022 .type = QUIRK_IGNORE_INTERFACE
1023 },
1024 {
1025 .ifnum = 1,
1026 .type = QUIRK_IGNORE_INTERFACE
1027 },
1028 {
1029 .ifnum = 2,
1030 .type = QUIRK_IGNORE_INTERFACE
1031 },
1032 {
1033 .ifnum = 3,
1034 .type = QUIRK_IGNORE_INTERFACE
1035 },
1036 {
1037 .ifnum = 4,
1038 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1039 },
1040 {
1041 .ifnum = 5,
1042 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1043 },
1044 {
1045 .ifnum = 6,
1046 .type = QUIRK_IGNORE_INTERFACE
1047 },
1048 {
1049 .ifnum = 7,
1050 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1051 },
1052 {
1053 .ifnum = 8,
1054 .type = QUIRK_AUDIO_STANDARD_INTERFACE
1055 },
1056 {
1057 .ifnum = 9,
1058 .type = QUIRK_MIDI_MIDIMAN,
1059 .data = & (const snd_usb_midi_endpoint_info_t) {
1060 .out_cables = 0x0001,
1061 .in_cables = 0x0001
1062 }
1063 },
1064 {
1065 .ifnum = -1
1066 }
1067 }
1068 }
1069},
1070
1071/* Mark of the Unicorn devices */
1072{
1073 /* thanks to Robert A. Lerche <ral 'at' msbit.com> */
1074 USB_DEVICE(0x07fd, 0x0001),
1075 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1076 .vendor_name = "MOTU",
1077 .product_name = "Fastlane",
1078 .ifnum = QUIRK_ANY_INTERFACE,
1079 .type = QUIRK_COMPOSITE,
1080 .data = & (const snd_usb_audio_quirk_t[]) {
1081 {
1082 .ifnum = 0,
1083 .type = QUIRK_MIDI_MOTU
1084 },
1085 {
1086 .ifnum = 1,
1087 .type = QUIRK_IGNORE_INTERFACE
1088 },
1089 {
1090 .ifnum = -1
1091 }
1092 }
1093 }
1094},
1095
1096{
1097 /* Creative Sound Blaster MP3+ */
1098 USB_DEVICE(0x041e, 0x3010),
1099 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1100 .vendor_name = "Creative Labs",
1101 .product_name = "Sound Blaster MP3+",
1102 .ifnum = QUIRK_NO_INTERFACE
1103 }
1104
1105},
1106
1107/* Emagic devices */
1108{
1109 USB_DEVICE(0x086a, 0x0001),
1110 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1111 .vendor_name = "Emagic",
1112 /* .product_name = "Unitor8", */
1113 .ifnum = 2,
1114 .type = QUIRK_MIDI_EMAGIC,
1115 .data = & (const snd_usb_midi_endpoint_info_t) {
1116 .out_cables = 0x80ff,
1117 .in_cables = 0x80ff
1118 }
1119 }
1120},
1121{
1122 USB_DEVICE(0x086a, 0x0002),
1123 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1124 .vendor_name = "Emagic",
1125 /* .product_name = "AMT8", */
1126 .ifnum = 2,
1127 .type = QUIRK_MIDI_EMAGIC,
1128 .data = & (const snd_usb_midi_endpoint_info_t) {
1129 .out_cables = 0x80ff,
1130 .in_cables = 0x80ff
1131 }
1132 }
1133},
1134{
1135 USB_DEVICE(0x086a, 0x0003),
1136 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1137 .vendor_name = "Emagic",
1138 /* .product_name = "MT4", */
1139 .ifnum = 2,
1140 .type = QUIRK_MIDI_EMAGIC,
1141 .data = & (const snd_usb_midi_endpoint_info_t) {
1142 .out_cables = 0x800f,
1143 .in_cables = 0x8003
1144 }
1145 }
1146},
1147
1148{
1149 USB_DEVICE_VENDOR_SPEC(0x0ccd, 0x0013),
1150 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1151 .vendor_name = "Terratec",
1152 .product_name = "PHASE 26",
1153 .ifnum = 3,
1154 .type = QUIRK_MIDI_STANDARD_INTERFACE
1155 }
1156},
1157
1158/* Novation EMS devices */
1159{
1160 USB_DEVICE_VENDOR_SPEC(0x1235, 0x0001),
1161 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1162 .vendor_name = "Novation",
1163 .product_name = "ReMOTE Audio/XStation",
1164 .ifnum = 4,
1165 .type = QUIRK_MIDI_NOVATION
1166 }
1167},
1168{
1169 USB_DEVICE_VENDOR_SPEC(0x1235, 0x0002),
1170 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1171 .vendor_name = "Novation",
1172 .product_name = "Speedio",
1173 .ifnum = 3,
1174 .type = QUIRK_MIDI_NOVATION
1175 }
1176},
1177{
1178 USB_DEVICE_VENDOR_SPEC(0x1235, 0x4661),
1179 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1180 .vendor_name = "Novation",
1181 .product_name = "ReMOTE25",
1182 .ifnum = 0,
1183 .type = QUIRK_MIDI_NOVATION
1184 }
1185},
1186
1187{
1188 /*
1189 * Some USB MIDI devices don't have an audio control interface,
1190 * so we have to grab MIDI streaming interfaces here.
1191 */
1192 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
1193 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
1194 .bInterfaceClass = USB_CLASS_AUDIO,
1195 .bInterfaceSubClass = USB_SUBCLASS_MIDI_STREAMING,
1196 .driver_info = (unsigned long) & (const snd_usb_audio_quirk_t) {
1197 .ifnum = QUIRK_ANY_INTERFACE,
1198 .type = QUIRK_MIDI_STANDARD_INTERFACE
1199 }
1200},
1201
1202#undef USB_DEVICE_VENDOR_SPEC
diff --git a/sound/usb/usx2y/Makefile b/sound/usb/usx2y/Makefile
new file mode 100644
index 000000000000..9ac22bce1124
--- /dev/null
+++ b/sound/usb/usx2y/Makefile
@@ -0,0 +1,3 @@
1snd-usb-usx2y-objs := usbusx2y.o usX2Yhwdep.o usx2yhwdeppcm.o
2
3obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-usx2y.o
diff --git a/sound/usb/usx2y/usX2Yhwdep.c b/sound/usb/usx2y/usX2Yhwdep.c
new file mode 100644
index 000000000000..bef9b0c142c4
--- /dev/null
+++ b/sound/usb/usx2y/usX2Yhwdep.c
@@ -0,0 +1,280 @@
1/*
2 * Driver for Tascam US-X2Y USB soundcards
3 *
4 * FPGA Loader + ALSA Startup
5 *
6 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <sound/driver.h>
24#include <linux/interrupt.h>
25#include <linux/usb.h>
26#include <sound/core.h>
27#include <sound/memalloc.h>
28#include <sound/pcm.h>
29#include <sound/hwdep.h>
30#include "usx2y.h"
31#include "usbusx2y.h"
32#include "usX2Yhwdep.h"
33
34int usX2Y_hwdep_pcm_new(snd_card_t* card);
35
36
37static struct page * snd_us428ctls_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type)
38{
39 unsigned long offset;
40 struct page * page;
41 void *vaddr;
42
43 snd_printdd("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
44 area->vm_start,
45 address - area->vm_start,
46 (address - area->vm_start) >> PAGE_SHIFT,
47 address);
48
49 offset = area->vm_pgoff << PAGE_SHIFT;
50 offset += address - area->vm_start;
51 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
52 vaddr = (char*)((usX2Ydev_t*)area->vm_private_data)->us428ctls_sharedmem + offset;
53 page = virt_to_page(vaddr);
54 get_page(page);
55 snd_printdd( "vaddr=%p made us428ctls_vm_nopage() return %p; offset=%lX\n", vaddr, page, offset);
56
57 if (type)
58 *type = VM_FAULT_MINOR;
59
60 return page;
61}
62
63static struct vm_operations_struct us428ctls_vm_ops = {
64 .nopage = snd_us428ctls_vm_nopage,
65};
66
67static int snd_us428ctls_mmap(snd_hwdep_t * hw, struct file *filp, struct vm_area_struct *area)
68{
69 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
70 usX2Ydev_t *us428 = (usX2Ydev_t*)hw->private_data;
71
72 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
73 // so as long as the device isn't fully initialised yet we return -EBUSY here.
74 if (!(((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT))
75 return -EBUSY;
76
77 /* if userspace tries to mmap beyond end of our buffer, fail */
78 if (size > ((PAGE_SIZE - 1 + sizeof(us428ctls_sharedmem_t)) / PAGE_SIZE) * PAGE_SIZE) {
79 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(us428ctls_sharedmem_t));
80 return -EINVAL;
81 }
82
83 if (!us428->us428ctls_sharedmem) {
84 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
85 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(us428ctls_sharedmem_t), GFP_KERNEL)))
86 return -ENOMEM;
87 memset(us428->us428ctls_sharedmem, -1, sizeof(us428ctls_sharedmem_t));
88 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
89 }
90 area->vm_ops = &us428ctls_vm_ops;
91 area->vm_flags |= VM_RESERVED;
92 area->vm_private_data = hw->private_data;
93 return 0;
94}
95
96static unsigned int snd_us428ctls_poll(snd_hwdep_t *hw, struct file *file, poll_table *wait)
97{
98 unsigned int mask = 0;
99 usX2Ydev_t *us428 = (usX2Ydev_t*)hw->private_data;
100 us428ctls_sharedmem_t *shm = us428->us428ctls_sharedmem;
101 if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
102 return POLLHUP;
103
104 poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
105
106 if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
107 mask |= POLLIN;
108
109 return mask;
110}
111
112
113static int snd_usX2Y_hwdep_open(snd_hwdep_t *hw, struct file *file)
114{
115 return 0;
116}
117
118static int snd_usX2Y_hwdep_release(snd_hwdep_t *hw, struct file *file)
119{
120 return 0;
121}
122
123static int snd_usX2Y_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *info)
124{
125 static char *type_ids[USX2Y_TYPE_NUMS] = {
126 [USX2Y_TYPE_122] = "us122",
127 [USX2Y_TYPE_224] = "us224",
128 [USX2Y_TYPE_428] = "us428",
129 };
130 int id = -1;
131
132 switch (le16_to_cpu(((usX2Ydev_t*)hw->private_data)->chip.dev->descriptor.idProduct)) {
133 case USB_ID_US122:
134 id = USX2Y_TYPE_122;
135 break;
136 case USB_ID_US224:
137 id = USX2Y_TYPE_224;
138 break;
139 case USB_ID_US428:
140 id = USX2Y_TYPE_428;
141 break;
142 }
143 if (0 > id)
144 return -ENODEV;
145 strcpy(info->id, type_ids[id]);
146 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
147 if (((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT)
148 info->chip_ready = 1;
149 info->version = USX2Y_DRIVER_VERSION;
150 return 0;
151}
152
153
154static int usX2Y_create_usbmidi(snd_card_t* card )
155{
156 static snd_usb_midi_endpoint_info_t quirk_data_1 = {
157 .out_ep =0x06,
158 .in_ep = 0x06,
159 .out_cables = 0x001,
160 .in_cables = 0x001
161 };
162 static snd_usb_audio_quirk_t quirk_1 = {
163 .vendor_name = "TASCAM",
164 .product_name = NAME_ALLCAPS,
165 .ifnum = 0,
166 .type = QUIRK_MIDI_FIXED_ENDPOINT,
167 .data = &quirk_data_1
168 };
169 static snd_usb_midi_endpoint_info_t quirk_data_2 = {
170 .out_ep =0x06,
171 .in_ep = 0x06,
172 .out_cables = 0x003,
173 .in_cables = 0x003
174 };
175 static snd_usb_audio_quirk_t quirk_2 = {
176 .vendor_name = "TASCAM",
177 .product_name = "US428",
178 .ifnum = 0,
179 .type = QUIRK_MIDI_FIXED_ENDPOINT,
180 .data = &quirk_data_2
181 };
182 struct usb_device *dev = usX2Y(card)->chip.dev;
183 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
184 snd_usb_audio_quirk_t *quirk = le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ? &quirk_2 : &quirk_1;
185
186 snd_printdd("usX2Y_create_usbmidi \n");
187 return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
188}
189
190static int usX2Y_create_alsa_devices(snd_card_t* card)
191{
192 int err;
193
194 do {
195 if ((err = usX2Y_create_usbmidi(card)) < 0) {
196 snd_printk("usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
197 break;
198 }
199 if ((err = usX2Y_audio_create(card)) < 0)
200 break;
201 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
202 break;
203 if ((err = snd_card_register(card)) < 0)
204 break;
205 } while (0);
206
207 return err;
208}
209
210static int snd_usX2Y_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *dsp)
211{
212 usX2Ydev_t *priv = hw->private_data;
213 int lret, err = -EINVAL;
214 snd_printdd( "dsp_load %s\n", dsp->name);
215
216 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
217 struct usb_device* dev = priv->chip.dev;
218 char *buf = kmalloc(dsp->length, GFP_KERNEL);
219 if (!buf)
220 return -ENOMEM;
221 if (copy_from_user(buf, dsp->image, dsp->length)) {
222 kfree(buf);
223 return -EFAULT;
224 }
225 err = usb_set_interface(dev, 0, 1);
226 if (err)
227 snd_printk("usb_set_interface error \n");
228 else
229 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
230 kfree(buf);
231 }
232 if (err)
233 return err;
234 if (dsp->index == 1) {
235 set_current_state(TASK_UNINTERRUPTIBLE);
236 schedule_timeout(HZ/4); // give the device some time
237 err = usX2Y_AsyncSeq04_init(priv);
238 if (err) {
239 snd_printk("usX2Y_AsyncSeq04_init error \n");
240 return err;
241 }
242 err = usX2Y_In04_init(priv);
243 if (err) {
244 snd_printk("usX2Y_In04_init error \n");
245 return err;
246 }
247 err = usX2Y_create_alsa_devices(hw->card);
248 if (err) {
249 snd_printk("usX2Y_create_alsa_devices error %i \n", err);
250 snd_card_free(hw->card);
251 return err;
252 }
253 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
254 snd_printdd("%s: alsa all started\n", hw->name);
255 }
256 return err;
257}
258
259
260int usX2Y_hwdep_new(snd_card_t* card, struct usb_device* device)
261{
262 int err;
263 snd_hwdep_t *hw;
264
265 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
266 return err;
267
268 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
269 hw->private_data = usX2Y(card);
270 hw->ops.open = snd_usX2Y_hwdep_open;
271 hw->ops.release = snd_usX2Y_hwdep_release;
272 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
273 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
274 hw->ops.mmap = snd_us428ctls_mmap;
275 hw->ops.poll = snd_us428ctls_poll;
276 hw->exclusive = 1;
277 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
278 return 0;
279}
280
diff --git a/sound/usb/usx2y/usX2Yhwdep.h b/sound/usb/usx2y/usX2Yhwdep.h
new file mode 100644
index 000000000000..d612a26eb77c
--- /dev/null
+++ b/sound/usb/usx2y/usX2Yhwdep.h
@@ -0,0 +1,6 @@
1#ifndef USX2YHWDEP_H
2#define USX2YHWDEP_H
3
4int usX2Y_hwdep_new(snd_card_t* card, struct usb_device* device);
5
6#endif
diff --git a/sound/usb/usx2y/usbus428ctldefs.h b/sound/usb/usx2y/usbus428ctldefs.h
new file mode 100644
index 000000000000..6af16438d2c7
--- /dev/null
+++ b/sound/usb/usx2y/usbus428ctldefs.h
@@ -0,0 +1,108 @@
1/*
2 *
3 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20enum E_In84{
21 eFader0 = 0,
22 eFader1,
23 eFader2,
24 eFader3,
25 eFader4,
26 eFader5,
27 eFader6,
28 eFader7,
29 eFaderM,
30 eTransport,
31 eModifier = 10,
32 eFilterSelect,
33 eSelect,
34 eMute,
35
36 eSwitch = 15,
37 eWheelGain,
38 eWheelFreq,
39 eWheelQ,
40 eWheelPan,
41 eWheel = 20
42};
43
44#define T_RECORD 1
45#define T_PLAY 2
46#define T_STOP 4
47#define T_F_FWD 8
48#define T_REW 0x10
49#define T_SOLO 0x20
50#define T_REC 0x40
51#define T_NULL 0x80
52
53
54struct us428_ctls{
55 unsigned char Fader[9];
56 unsigned char Transport;
57 unsigned char Modifier;
58 unsigned char FilterSelect;
59 unsigned char Select;
60 unsigned char Mute;
61 unsigned char UNKNOWN;
62 unsigned char Switch;
63 unsigned char Wheel[5];
64};
65
66typedef struct us428_ctls us428_ctls_t;
67
68typedef struct us428_setByte{
69 unsigned char Offset,
70 Value;
71}us428_setByte_t;
72
73enum {
74 eLT_Volume = 0,
75 eLT_Light
76};
77
78typedef struct usX2Y_volume {
79 unsigned char Channel,
80 LH,
81 LL,
82 RH,
83 RL;
84} usX2Y_volume_t;
85
86struct us428_lights{
87 us428_setByte_t Light[7];
88};
89typedef struct us428_lights us428_lights_t;
90
91typedef struct {
92 char type;
93 union {
94 usX2Y_volume_t vol;
95 us428_lights_t lights;
96 } val;
97} us428_p4out_t;
98
99#define N_us428_ctl_BUFS 16
100#define N_us428_p4out_BUFS 16
101struct us428ctls_sharedmem{
102 us428_ctls_t CtlSnapShot[N_us428_ctl_BUFS];
103 int CtlSnapShotDiffersAt[N_us428_ctl_BUFS];
104 int CtlSnapShotLast, CtlSnapShotRed;
105 us428_p4out_t p4out[N_us428_p4out_BUFS];
106 int p4outLast, p4outSent;
107};
108typedef struct us428ctls_sharedmem us428ctls_sharedmem_t;
diff --git a/sound/usb/usx2y/usbusx2y.c b/sound/usb/usx2y/usbusx2y.c
new file mode 100644
index 000000000000..b06a267e5dac
--- /dev/null
+++ b/sound/usb/usx2y/usbusx2y.c
@@ -0,0 +1,461 @@
1/*
2 * usbusy2y.c - ALSA USB US-428 Driver
3 *
42004-12-14 Karsten Wiese
5 Version 0.8.7.1:
6 snd_pcm_open for rawusb pcm-devices now returns -EBUSY if called without rawusb's hwdep device being open.
7
82004-12-02 Karsten Wiese
9 Version 0.8.7:
10 Use macro usb_maxpacket() for portability.
11
122004-10-26 Karsten Wiese
13 Version 0.8.6:
14 wake_up() process waiting in usX2Y_urbs_start() on error.
15
162004-10-21 Karsten Wiese
17 Version 0.8.5:
18 nrpacks is runtime or compiletime configurable now with tested values from 1 to 4.
19
202004-10-03 Karsten Wiese
21 Version 0.8.2:
22 Avoid any possible racing while in prepare callback.
23
242004-09-30 Karsten Wiese
25 Version 0.8.0:
26 Simplified things and made ohci work again.
27
282004-09-20 Karsten Wiese
29 Version 0.7.3:
30 Use usb_kill_urb() instead of deprecated (kernel 2.6.9) usb_unlink_urb().
31
322004-07-13 Karsten Wiese
33 Version 0.7.1:
34 Don't sleep in START/STOP callbacks anymore.
35 us428 channels C/D not handled just for this version, sorry.
36
372004-06-21 Karsten Wiese
38 Version 0.6.4:
39 Temporarely suspend midi input
40 to sanely call usb_set_interface() when setting format.
41
422004-06-12 Karsten Wiese
43 Version 0.6.3:
44 Made it thus the following rule is enforced:
45 "All pcm substreams of one usX2Y have to operate at the same rate & format."
46
472004-04-06 Karsten Wiese
48 Version 0.6.0:
49 Runs on 2.6.5 kernel without any "--with-debug=" things.
50 us224 reported running.
51
522004-01-14 Karsten Wiese
53 Version 0.5.1:
54 Runs with 2.6.1 kernel.
55
562003-12-30 Karsten Wiese
57 Version 0.4.1:
58 Fix 24Bit 4Channel capturing for the us428.
59
602003-11-27 Karsten Wiese, Martin Langer
61 Version 0.4:
62 us122 support.
63 us224 could be tested by uncommenting the sections containing USB_ID_US224
64
652003-11-03 Karsten Wiese
66 Version 0.3:
67 24Bit support.
68 "arecord -D hw:1 -c 2 -r 48000 -M -f S24_3LE|aplay -D hw:1 -c 2 -r 48000 -M -f S24_3LE" works.
69
702003-08-22 Karsten Wiese
71 Version 0.0.8:
72 Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader.
73 See:
74 http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz
75
762003-06-18 Karsten Wiese
77 Version 0.0.5:
78 changed to compile with kernel 2.4.21 and alsa 0.9.4
79
802002-10-16 Karsten Wiese
81 Version 0.0.4:
82 compiles again with alsa-current.
83 USB_ISO_ASAP not used anymore (most of the time), instead
84 urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore.
85
86 To get the best out of this:
87 Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds.
88 This helped me much on my slowish PII 400 & PIII 500.
89 ACPI yet untested but might cause the same bad behaviour.
90 Use a kernel with lowlatency and preemptiv patches applied.
91 To autoload snd-usb-midi append a line
92 post-install snd-usb-us428 modprobe snd-usb-midi
93 to /etc/modules.conf.
94
95 known problems:
96 sliders, knobs, lights not yet handled except MASTER Volume slider.
97 "pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does.
98 KDE3: "Enable full duplex operation" deadlocks.
99
100
1012002-08-31 Karsten Wiese
102 Version 0.0.3: audio also simplex;
103 simplifying: iso urbs only 1 packet, melted structs.
104 ASYNC_UNLINK not used anymore: no more crashes so far.....
105 for alsa 0.9 rc3.
106
1072002-08-09 Karsten Wiese
108 Version 0.0.2: midi works with snd-usb-midi, audio (only fullduplex now) with i.e. bristol.
109 The firmware has been sniffed from win2k us-428 driver 3.09.
110
111 * Copyright (c) 2002 - 2004 Karsten Wiese
112 *
113 * This program is free software; you can redistribute it and/or modify
114 * it under the terms of the GNU General Public License as published by
115 * the Free Software Foundation; either version 2 of the License, or
116 * (at your option) any later version.
117 *
118 * This program is distributed in the hope that it will be useful,
119 * but WITHOUT ANY WARRANTY; without even the implied warranty of
120 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121 * GNU General Public License for more details.
122 *
123 * You should have received a copy of the GNU General Public License
124 * along with this program; if not, write to the Free Software
125 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
126*/
127
128#include <sound/driver.h>
129#include <linux/init.h>
130#include <linux/module.h>
131#include <linux/moduleparam.h>
132#include <linux/interrupt.h>
133#include <linux/usb.h>
134#include <sound/core.h>
135#include <sound/initval.h>
136#include <sound/pcm.h>
137
138#include <sound/rawmidi.h>
139#include "usx2y.h"
140#include "usbusx2y.h"
141#include "usX2Yhwdep.h"
142
143
144
145MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
146MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.8.7.1");
147MODULE_LICENSE("GPL");
148MODULE_SUPPORTED_DEVICE("{{TASCAM(0x1604), "NAME_ALLCAPS"(0x8001)(0x8005)(0x8007) }}");
149
150static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
151static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
152static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
153
154module_param_array(index, int, NULL, 0444);
155MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
156module_param_array(id, charp, NULL, 0444);
157MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
158module_param_array(enable, bool, NULL, 0444);
159MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
160
161
162static int snd_usX2Y_card_used[SNDRV_CARDS];
163
164static void usX2Y_usb_disconnect(struct usb_device* usb_device, void* ptr);
165static void snd_usX2Y_card_private_free(snd_card_t *card);
166
167/*
168 * pipe 4 is used for switching the lamps, setting samplerate, volumes ....
169 */
170static void i_usX2Y_Out04Int(struct urb* urb, struct pt_regs *regs)
171{
172#ifdef CONFIG_SND_DEBUG
173 if (urb->status) {
174 int i;
175 usX2Ydev_t* usX2Y = urb->context;
176 for (i = 0; i < 10 && usX2Y->AS04.urb[i] != urb; i++);
177 snd_printdd("i_usX2Y_Out04Int() urb %i status=%i\n", i, urb->status);
178 }
179#endif
180}
181
182static void i_usX2Y_In04Int(struct urb* urb, struct pt_regs *regs)
183{
184 int err = 0;
185 usX2Ydev_t *usX2Y = urb->context;
186 us428ctls_sharedmem_t *us428ctls = usX2Y->us428ctls_sharedmem;
187
188 usX2Y->In04IntCalls++;
189
190 if (urb->status) {
191 snd_printdd("Interrupt Pipe 4 came back with status=%i\n", urb->status);
192 return;
193 }
194
195 // printk("%i:0x%02X ", 8, (int)((unsigned char*)usX2Y->In04Buf)[8]); Master volume shows 0 here if fader is at max during boot ?!?
196 if (us428ctls) {
197 int diff = -1;
198 if (-2 == us428ctls->CtlSnapShotLast) {
199 diff = 0;
200 memcpy(usX2Y->In04Last, usX2Y->In04Buf, sizeof(usX2Y->In04Last));
201 us428ctls->CtlSnapShotLast = -1;
202 } else {
203 int i;
204 for (i = 0; i < 21; i++) {
205 if (usX2Y->In04Last[i] != ((char*)usX2Y->In04Buf)[i]) {
206 if (diff < 0)
207 diff = i;
208 usX2Y->In04Last[i] = ((char*)usX2Y->In04Buf)[i];
209 }
210 }
211 }
212 if (0 <= diff) {
213 int n = us428ctls->CtlSnapShotLast + 1;
214 if (n >= N_us428_ctl_BUFS || n < 0)
215 n = 0;
216 memcpy(us428ctls->CtlSnapShot + n, usX2Y->In04Buf, sizeof(us428ctls->CtlSnapShot[0]));
217 us428ctls->CtlSnapShotDiffersAt[n] = diff;
218 us428ctls->CtlSnapShotLast = n;
219 wake_up(&usX2Y->us428ctls_wait_queue_head);
220 }
221 }
222
223
224 if (usX2Y->US04) {
225 if (0 == usX2Y->US04->submitted)
226 do
227 err = usb_submit_urb(usX2Y->US04->urb[usX2Y->US04->submitted++], GFP_ATOMIC);
228 while (!err && usX2Y->US04->submitted < usX2Y->US04->len);
229 } else
230 if (us428ctls && us428ctls->p4outLast >= 0 && us428ctls->p4outLast < N_us428_p4out_BUFS) {
231 if (us428ctls->p4outLast != us428ctls->p4outSent) {
232 int j, send = us428ctls->p4outSent + 1;
233 if (send >= N_us428_p4out_BUFS)
234 send = 0;
235 for (j = 0; j < URBS_AsyncSeq && !err; ++j)
236 if (0 == usX2Y->AS04.urb[j]->status) {
237 us428_p4out_t *p4out = us428ctls->p4out + send; // FIXME if more then 1 p4out is new, 1 gets lost.
238 usb_fill_bulk_urb(usX2Y->AS04.urb[j], usX2Y->chip.dev,
239 usb_sndbulkpipe(usX2Y->chip.dev, 0x04), &p4out->val.vol,
240 p4out->type == eLT_Light ? sizeof(us428_lights_t) : 5,
241 i_usX2Y_Out04Int, usX2Y);
242 err = usb_submit_urb(usX2Y->AS04.urb[j], GFP_ATOMIC);
243 us428ctls->p4outSent = send;
244 break;
245 }
246 }
247 }
248
249 if (err) {
250 snd_printk("In04Int() usb_submit_urb err=%i\n", err);
251 }
252
253 urb->dev = usX2Y->chip.dev;
254 usb_submit_urb(urb, GFP_ATOMIC);
255}
256
257/*
258 * Prepare some urbs
259 */
260int usX2Y_AsyncSeq04_init(usX2Ydev_t* usX2Y)
261{
262 int err = 0,
263 i;
264
265 if (NULL == (usX2Y->AS04.buffer = kmalloc(URB_DataLen_AsyncSeq*URBS_AsyncSeq, GFP_KERNEL))) {
266 err = -ENOMEM;
267 } else
268 for (i = 0; i < URBS_AsyncSeq; ++i) {
269 if (NULL == (usX2Y->AS04.urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
270 err = -ENOMEM;
271 break;
272 }
273 usb_fill_bulk_urb( usX2Y->AS04.urb[i], usX2Y->chip.dev,
274 usb_sndbulkpipe(usX2Y->chip.dev, 0x04),
275 usX2Y->AS04.buffer + URB_DataLen_AsyncSeq*i, 0,
276 i_usX2Y_Out04Int, usX2Y
277 );
278 }
279 return err;
280}
281
282int usX2Y_In04_init(usX2Ydev_t* usX2Y)
283{
284 int err = 0;
285 if (! (usX2Y->In04urb = usb_alloc_urb(0, GFP_KERNEL)))
286 return -ENOMEM;
287
288 if (! (usX2Y->In04Buf = kmalloc(21, GFP_KERNEL))) {
289 usb_free_urb(usX2Y->In04urb);
290 return -ENOMEM;
291 }
292
293 init_waitqueue_head(&usX2Y->In04WaitQueue);
294 usb_fill_int_urb(usX2Y->In04urb, usX2Y->chip.dev, usb_rcvintpipe(usX2Y->chip.dev, 0x4),
295 usX2Y->In04Buf, 21,
296 i_usX2Y_In04Int, usX2Y,
297 10);
298 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
299 return err;
300}
301
302static void usX2Y_unlinkSeq(snd_usX2Y_AsyncSeq_t* S)
303{
304 int i;
305 for (i = 0; i < URBS_AsyncSeq; ++i) {
306 if (S[i].urb) {
307 usb_kill_urb(S->urb[i]);
308 usb_free_urb(S->urb[i]);
309 S->urb[i] = NULL;
310 }
311 }
312 kfree(S->buffer);
313}
314
315
316static struct usb_device_id snd_usX2Y_usb_id_table[] = {
317 {
318 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
319 .idVendor = 0x1604,
320 .idProduct = USB_ID_US428
321 },
322 {
323 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
324 .idVendor = 0x1604,
325 .idProduct = USB_ID_US122
326 },
327 {
328 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
329 .idVendor = 0x1604,
330 .idProduct = USB_ID_US224
331 },
332 { /* terminator */ }
333};
334
335static snd_card_t* usX2Y_create_card(struct usb_device* device)
336{
337 int dev;
338 snd_card_t* card;
339 for (dev = 0; dev < SNDRV_CARDS; ++dev)
340 if (enable[dev] && !snd_usX2Y_card_used[dev])
341 break;
342 if (dev >= SNDRV_CARDS)
343 return NULL;
344 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(usX2Ydev_t));
345 if (!card)
346 return NULL;
347 snd_usX2Y_card_used[usX2Y(card)->chip.index = dev] = 1;
348 card->private_free = snd_usX2Y_card_private_free;
349 usX2Y(card)->chip.dev = device;
350 usX2Y(card)->chip.card = card;
351 init_waitqueue_head(&usX2Y(card)->prepare_wait_queue);
352 init_MUTEX (&usX2Y(card)->prepare_mutex);
353 INIT_LIST_HEAD(&usX2Y(card)->chip.midi_list);
354 strcpy(card->driver, "USB "NAME_ALLCAPS"");
355 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
356 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
357 card->shortname,
358 le16_to_cpu(device->descriptor.idVendor),
359 le16_to_cpu(device->descriptor.idProduct),
360 0,//us428(card)->usbmidi.ifnum,
361 usX2Y(card)->chip.dev->bus->busnum, usX2Y(card)->chip.dev->devnum
362 );
363 snd_card_set_dev(card, &device->dev);
364 return card;
365}
366
367
368static void* usX2Y_usb_probe(struct usb_device* device, struct usb_interface *intf, const struct usb_device_id* device_id)
369{
370 int err;
371 snd_card_t* card;
372 if (le16_to_cpu(device->descriptor.idVendor) != 0x1604 ||
373 (le16_to_cpu(device->descriptor.idProduct) != USB_ID_US122 &&
374 le16_to_cpu(device->descriptor.idProduct) != USB_ID_US224 &&
375 le16_to_cpu(device->descriptor.idProduct) != USB_ID_US428) ||
376 !(card = usX2Y_create_card(device)))
377 return NULL;
378 if ((err = usX2Y_hwdep_new(card, device)) < 0 ||
379 (err = snd_card_register(card)) < 0) {
380 snd_card_free(card);
381 return NULL;
382 }
383 return card;
384}
385
386/*
387 * new 2.5 USB kernel API
388 */
389static int snd_usX2Y_probe(struct usb_interface *intf, const struct usb_device_id *id)
390{
391 void *chip;
392 chip = usX2Y_usb_probe(interface_to_usbdev(intf), intf, id);
393 if (chip) {
394 dev_set_drvdata(&intf->dev, chip);
395 return 0;
396 } else
397 return -EIO;
398}
399
400static void snd_usX2Y_disconnect(struct usb_interface *intf)
401{
402 usX2Y_usb_disconnect(interface_to_usbdev(intf),
403 dev_get_drvdata(&intf->dev));
404}
405
406MODULE_DEVICE_TABLE(usb, snd_usX2Y_usb_id_table);
407static struct usb_driver snd_usX2Y_usb_driver = {
408 .owner = THIS_MODULE,
409 .name = "snd-usb-usx2y",
410 .probe = snd_usX2Y_probe,
411 .disconnect = snd_usX2Y_disconnect,
412 .id_table = snd_usX2Y_usb_id_table,
413};
414
415static void snd_usX2Y_card_private_free(snd_card_t *card)
416{
417 kfree(usX2Y(card)->In04Buf);
418 usb_free_urb(usX2Y(card)->In04urb);
419 if (usX2Y(card)->us428ctls_sharedmem)
420 snd_free_pages(usX2Y(card)->us428ctls_sharedmem, sizeof(*usX2Y(card)->us428ctls_sharedmem));
421 if (usX2Y(card)->chip.index >= 0 && usX2Y(card)->chip.index < SNDRV_CARDS)
422 snd_usX2Y_card_used[usX2Y(card)->chip.index] = 0;
423}
424
425/*
426 * Frees the device.
427 */
428static void usX2Y_usb_disconnect(struct usb_device* device, void* ptr)
429{
430 if (ptr) {
431 usX2Ydev_t* usX2Y = usX2Y((snd_card_t*)ptr);
432 struct list_head* p;
433 if (usX2Y->chip_status == USX2Y_STAT_CHIP_HUP) // on 2.6.1 kernel snd_usbmidi_disconnect()
434 return; // calls us back. better leave :-) .
435 usX2Y->chip.shutdown = 1;
436 usX2Y->chip_status = USX2Y_STAT_CHIP_HUP;
437 usX2Y_unlinkSeq(&usX2Y->AS04);
438 usb_kill_urb(usX2Y->In04urb);
439 snd_card_disconnect((snd_card_t*)ptr);
440 /* release the midi resources */
441 list_for_each(p, &usX2Y->chip.midi_list) {
442 snd_usbmidi_disconnect(p, &snd_usX2Y_usb_driver);
443 }
444 if (usX2Y->us428ctls_sharedmem)
445 wake_up(&usX2Y->us428ctls_wait_queue_head);
446 snd_card_free_in_thread((snd_card_t*)ptr);
447 }
448}
449
450static int __init snd_usX2Y_module_init(void)
451{
452 return usb_register(&snd_usX2Y_usb_driver);
453}
454
455static void __exit snd_usX2Y_module_exit(void)
456{
457 usb_deregister(&snd_usX2Y_usb_driver);
458}
459
460module_init(snd_usX2Y_module_init)
461module_exit(snd_usX2Y_module_exit)
diff --git a/sound/usb/usx2y/usbusx2y.h b/sound/usb/usx2y/usbusx2y.h
new file mode 100644
index 000000000000..f65f3a7194ca
--- /dev/null
+++ b/sound/usb/usx2y/usbusx2y.h
@@ -0,0 +1,84 @@
1#ifndef USBUSX2Y_H
2#define USBUSX2Y_H
3#include "../usbaudio.h"
4#include "usbus428ctldefs.h"
5
6#define NRURBS 2
7
8
9#define URBS_AsyncSeq 10
10#define URB_DataLen_AsyncSeq 32
11typedef struct {
12 struct urb* urb[URBS_AsyncSeq];
13 char* buffer;
14} snd_usX2Y_AsyncSeq_t;
15
16typedef struct {
17 int submitted;
18 int len;
19 struct urb* urb[0];
20} snd_usX2Y_urbSeq_t;
21
22typedef struct snd_usX2Y_substream snd_usX2Y_substream_t;
23#include "usx2yhwdeppcm.h"
24
25typedef struct {
26 snd_usb_audio_t chip;
27 int stride;
28 struct urb *In04urb;
29 void *In04Buf;
30 char In04Last[24];
31 unsigned In04IntCalls;
32 snd_usX2Y_urbSeq_t *US04;
33 wait_queue_head_t In04WaitQueue;
34 snd_usX2Y_AsyncSeq_t AS04;
35 unsigned int rate,
36 format;
37 int chip_status;
38 struct semaphore prepare_mutex;
39 us428ctls_sharedmem_t *us428ctls_sharedmem;
40 int wait_iso_frame;
41 wait_queue_head_t us428ctls_wait_queue_head;
42 snd_usX2Y_hwdep_pcm_shm_t *hwdep_pcm_shm;
43 snd_usX2Y_substream_t *subs[4];
44 snd_usX2Y_substream_t * volatile prepare_subs;
45 wait_queue_head_t prepare_wait_queue;
46} usX2Ydev_t;
47
48
49struct snd_usX2Y_substream {
50 usX2Ydev_t *usX2Y;
51 snd_pcm_substream_t *pcm_substream;
52
53 int endpoint;
54 unsigned int maxpacksize; /* max packet size in bytes */
55
56 atomic_t state;
57#define state_STOPPED 0
58#define state_STARTING1 1
59#define state_STARTING2 2
60#define state_STARTING3 3
61#define state_PREPARED 4
62#define state_PRERUNNING 6
63#define state_RUNNING 8
64
65 int hwptr; /* free frame position in the buffer (only for playback) */
66 int hwptr_done; /* processed frame position in the buffer */
67 int transfer_done; /* processed frames since last period update */
68
69 struct urb *urb[NRURBS]; /* data urb table */
70 struct urb *completed_urb;
71 char *tmpbuf; /* temporary buffer for playback */
72};
73
74
75#define usX2Y(c) ((usX2Ydev_t*)(c)->private_data)
76
77int usX2Y_audio_create(snd_card_t* card);
78
79int usX2Y_AsyncSeq04_init(usX2Ydev_t* usX2Y);
80int usX2Y_In04_init(usX2Ydev_t* usX2Y);
81
82#define NAME_ALLCAPS "US-X2Y"
83
84#endif
diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
new file mode 100644
index 000000000000..4c292e090069
--- /dev/null
+++ b/sound/usb/usx2y/usbusx2yaudio.c
@@ -0,0 +1,1026 @@
1/*
2 * US-X2Y AUDIO
3 * Copyright (c) 2002-2004 by Karsten Wiese
4 *
5 * based on
6 *
7 * (Tentative) USB Audio Driver for ALSA
8 *
9 * Main and PCM part
10 *
11 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
12 *
13 * Many codes borrowed from audio.c by
14 * Alan Cox (alan@lxorguk.ukuu.org.uk)
15 * Thomas Sailer (sailer@ife.ee.ethz.ch)
16 *
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 */
32
33
34#include <sound/driver.h>
35#include <linux/interrupt.h>
36#include <linux/usb.h>
37#include <sound/core.h>
38#include <sound/info.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include "usx2y.h"
42#include "usbusx2y.h"
43
44#define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
45 1 to 4 have been tested ok on uhci.
46 To use 3 on ohci, you'd need a patch:
47 look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
48 "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
49 .
50 1, 2 and 4 work out of the box on ohci, if I recall correctly.
51 Bigger is safer operation,
52 smaller gives lower latencies.
53 */
54#define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
55 nrpacks set to 1, you might as well comment
56 this #define out, and thereby produce smaller, faster code.
57 You'd also set USX2Y_NRPACKS to 1 then.
58 */
59
60#ifdef USX2Y_NRPACKS_VARIABLE
61 static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
62 #define nr_of_packs() nrpacks
63 module_param(nrpacks, int, 0444);
64 MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
65#else
66 #define nr_of_packs() USX2Y_NRPACKS
67#endif
68
69
70static int usX2Y_urb_capt_retire(snd_usX2Y_substream_t *subs)
71{
72 struct urb *urb = subs->completed_urb;
73 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
74 unsigned char *cp;
75 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
76 usX2Ydev_t *usX2Y = subs->usX2Y;
77
78 for (i = 0; i < nr_of_packs(); i++) {
79 cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
80 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
81 snd_printk("activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status);
82 return urb->iso_frame_desc[i].status;
83 }
84 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
85 if (! len) {
86 snd_printd("0 == len ERROR!\n");
87 continue;
88 }
89
90 /* copy a data chunk */
91 if ((hwptr_done + len) > runtime->buffer_size) {
92 int cnt = runtime->buffer_size - hwptr_done;
93 int blen = cnt * usX2Y->stride;
94 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
95 memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
96 } else {
97 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, len * usX2Y->stride);
98 }
99 lens += len;
100 if ((hwptr_done += len) >= runtime->buffer_size)
101 hwptr_done -= runtime->buffer_size;
102 }
103
104 subs->hwptr_done = hwptr_done;
105 subs->transfer_done += lens;
106 /* update the pointer, call callback if necessary */
107 if (subs->transfer_done >= runtime->period_size) {
108 subs->transfer_done -= runtime->period_size;
109 snd_pcm_period_elapsed(subs->pcm_substream);
110 }
111 return 0;
112}
113/*
114 * prepare urb for playback data pipe
115 *
116 * we copy the data directly from the pcm buffer.
117 * the current position to be copied is held in hwptr field.
118 * since a urb can handle only a single linear buffer, if the total
119 * transferred area overflows the buffer boundary, we cannot send
120 * it directly from the buffer. thus the data is once copied to
121 * a temporary buffer and urb points to that.
122 */
123static int usX2Y_urb_play_prepare(snd_usX2Y_substream_t *subs,
124 struct urb *cap_urb,
125 struct urb *urb)
126{
127 int count, counts, pack;
128 usX2Ydev_t* usX2Y = subs->usX2Y;
129 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
130
131 count = 0;
132 for (pack = 0; pack < nr_of_packs(); pack++) {
133 /* calculate the size of a packet */
134 counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
135 count += counts;
136 if (counts < 43 || counts > 50) {
137 snd_printk("should not be here with counts=%i\n", counts);
138 return -EPIPE;
139 }
140 /* set up descriptor */
141 urb->iso_frame_desc[pack].offset = pack ?
142 urb->iso_frame_desc[pack - 1].offset + urb->iso_frame_desc[pack - 1].length :
143 0;
144 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
145 }
146 if (atomic_read(&subs->state) >= state_PRERUNNING)
147 if (subs->hwptr + count > runtime->buffer_size) {
148 /* err, the transferred area goes over buffer boundary.
149 * copy the data to the temp buffer.
150 */
151 int len;
152 len = runtime->buffer_size - subs->hwptr;
153 urb->transfer_buffer = subs->tmpbuf;
154 memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * usX2Y->stride, len * usX2Y->stride);
155 memcpy(subs->tmpbuf + len * usX2Y->stride, runtime->dma_area, (count - len) * usX2Y->stride);
156 subs->hwptr += count;
157 subs->hwptr -= runtime->buffer_size;
158 } else {
159 /* set the buffer pointer */
160 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
161 if ((subs->hwptr += count) >= runtime->buffer_size)
162 subs->hwptr -= runtime->buffer_size;
163 }
164 else
165 urb->transfer_buffer = subs->tmpbuf;
166 urb->transfer_buffer_length = count * usX2Y->stride;
167 return 0;
168}
169
170/*
171 * process after playback data complete
172 *
173 * update the current position and call callback if a period is processed.
174 */
175static void usX2Y_urb_play_retire(snd_usX2Y_substream_t *subs, struct urb *urb)
176{
177 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
178 int len = urb->actual_length / subs->usX2Y->stride;
179
180 subs->transfer_done += len;
181 subs->hwptr_done += len;
182 if (subs->hwptr_done >= runtime->buffer_size)
183 subs->hwptr_done -= runtime->buffer_size;
184 if (subs->transfer_done >= runtime->period_size) {
185 subs->transfer_done -= runtime->period_size;
186 snd_pcm_period_elapsed(subs->pcm_substream);
187 }
188}
189
190static int usX2Y_urb_submit(snd_usX2Y_substream_t *subs, struct urb *urb, int frame)
191{
192 int err;
193 if (!urb)
194 return -ENODEV;
195 urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
196 urb->hcpriv = NULL;
197 urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */
198 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
199 snd_printk("usb_submit_urb() returned %i\n", err);
200 return err;
201 }
202 return 0;
203}
204
205static inline int usX2Y_usbframe_complete(snd_usX2Y_substream_t *capsubs, snd_usX2Y_substream_t *playbacksubs, int frame)
206{
207 int err, state;
208 {
209 struct urb *urb = playbacksubs->completed_urb;
210
211 state = atomic_read(&playbacksubs->state);
212 if (NULL != urb) {
213 if (state == state_RUNNING)
214 usX2Y_urb_play_retire(playbacksubs, urb);
215 else
216 if (state >= state_PRERUNNING) {
217 atomic_inc(&playbacksubs->state);
218 }
219 } else {
220 switch (state) {
221 case state_STARTING1:
222 urb = playbacksubs->urb[0];
223 atomic_inc(&playbacksubs->state);
224 break;
225 case state_STARTING2:
226 urb = playbacksubs->urb[1];
227 atomic_inc(&playbacksubs->state);
228 break;
229 }
230 }
231 if (urb) {
232 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
233 (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
234 return err;
235 }
236 }
237
238 playbacksubs->completed_urb = NULL;
239 }
240 state = atomic_read(&capsubs->state);
241 if (state >= state_PREPARED) {
242 if (state == state_RUNNING) {
243 if ((err = usX2Y_urb_capt_retire(capsubs)))
244 return err;
245 } else
246 if (state >= state_PRERUNNING) {
247 atomic_inc(&capsubs->state);
248 }
249 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
250 return err;
251 }
252 capsubs->completed_urb = NULL;
253 return 0;
254}
255
256
257static void usX2Y_clients_stop(usX2Ydev_t *usX2Y)
258{
259 int s, u;
260 for (s = 0; s < 4; s++) {
261 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
262 if (subs) {
263 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
264 atomic_set(&subs->state, state_STOPPED);
265 }
266 }
267 for (s = 0; s < 4; s++) {
268 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
269 if (subs) {
270 if (atomic_read(&subs->state) >= state_PRERUNNING) {
271 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
272 }
273 for (u = 0; u < NRURBS; u++) {
274 struct urb *urb = subs->urb[u];
275 if (NULL != urb)
276 snd_printdd("%i status=%i start_frame=%i\n", u, urb->status, urb->start_frame);
277 }
278 }
279 }
280 usX2Y->prepare_subs = NULL;
281 wake_up(&usX2Y->prepare_wait_queue);
282}
283
284static void usX2Y_error_urb_status(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
285{
286 snd_printk("ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
287 urb->status = 0;
288 usX2Y_clients_stop(usX2Y);
289}
290
291static void usX2Y_error_sequence(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
292{
293 snd_printk("Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
294 "Most propably some urb of usb-frame %i is still missing.\n"
295 "Cause could be too long delays in usb-hcd interrupt handling.\n",
296 usb_get_current_frame_number(usX2Y->chip.dev),
297 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
298 usX2Y_clients_stop(usX2Y);
299}
300
301static void i_usX2Y_urb_complete(struct urb *urb, struct pt_regs *regs)
302{
303 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
304 usX2Ydev_t *usX2Y = subs->usX2Y;
305
306 if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
307 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame);
308 return;
309 }
310 if (unlikely(urb->status)) {
311 usX2Y_error_urb_status(usX2Y, subs, urb);
312 return;
313 }
314 if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame))
315 subs->completed_urb = urb;
316 else {
317 usX2Y_error_sequence(usX2Y, subs, urb);
318 return;
319 }
320 {
321 snd_usX2Y_substream_t *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
322 *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
323 if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED &&
324 (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) {
325 if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
326 if (nr_of_packs() <= urb->start_frame &&
327 urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci
328 usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs();
329 else
330 usX2Y->wait_iso_frame += nr_of_packs();
331 } else {
332 snd_printdd("\n");
333 usX2Y_clients_stop(usX2Y);
334 }
335 }
336 }
337}
338
339static void usX2Y_urbs_set_complete(usX2Ydev_t * usX2Y, void (*complete)(struct urb *, struct pt_regs *))
340{
341 int s, u;
342 for (s = 0; s < 4; s++) {
343 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
344 if (NULL != subs)
345 for (u = 0; u < NRURBS; u++) {
346 struct urb * urb = subs->urb[u];
347 if (NULL != urb)
348 urb->complete = complete;
349 }
350 }
351}
352
353static void usX2Y_subs_startup_finish(usX2Ydev_t * usX2Y)
354{
355 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
356 usX2Y->prepare_subs = NULL;
357}
358
359static void i_usX2Y_subs_startup(struct urb *urb, struct pt_regs *regs)
360{
361 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
362 usX2Ydev_t *usX2Y = subs->usX2Y;
363 snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs;
364 if (NULL != prepare_subs)
365 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
366 usX2Y_subs_startup_finish(usX2Y);
367 atomic_inc(&prepare_subs->state);
368 wake_up(&usX2Y->prepare_wait_queue);
369 }
370
371 i_usX2Y_urb_complete(urb, regs);
372}
373
374static void usX2Y_subs_prepare(snd_usX2Y_substream_t *subs)
375{
376 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n", subs, subs->endpoint, subs->urb[0], subs->urb[1]);
377 /* reset the pointer */
378 subs->hwptr = 0;
379 subs->hwptr_done = 0;
380 subs->transfer_done = 0;
381}
382
383
384static void usX2Y_urb_release(struct urb** urb, int free_tb)
385{
386 if (*urb) {
387 usb_kill_urb(*urb);
388 if (free_tb)
389 kfree((*urb)->transfer_buffer);
390 usb_free_urb(*urb);
391 *urb = NULL;
392 }
393}
394/*
395 * release a substreams urbs
396 */
397static void usX2Y_urbs_release(snd_usX2Y_substream_t *subs)
398{
399 int i;
400 snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
401 for (i = 0; i < NRURBS; i++)
402 usX2Y_urb_release(subs->urb + i, subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
403
404 if (subs->tmpbuf) {
405 kfree(subs->tmpbuf);
406 subs->tmpbuf = NULL;
407 }
408}
409/*
410 * initialize a substream's urbs
411 */
412static int usX2Y_urbs_allocate(snd_usX2Y_substream_t *subs)
413{
414 int i;
415 unsigned int pipe;
416 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
417 struct usb_device *dev = subs->usX2Y->chip.dev;
418
419 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
420 usb_rcvisocpipe(dev, subs->endpoint);
421 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
422 if (!subs->maxpacksize)
423 return -EINVAL;
424
425 if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
426 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
427 if (NULL == subs->tmpbuf) {
428 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
429 return -ENOMEM;
430 }
431 }
432 /* allocate and initialize data urbs */
433 for (i = 0; i < NRURBS; i++) {
434 struct urb** purb = subs->urb + i;
435 if (*purb) {
436 usb_kill_urb(*purb);
437 continue;
438 }
439 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
440 if (NULL == *purb) {
441 usX2Y_urbs_release(subs);
442 return -ENOMEM;
443 }
444 if (!is_playback && !(*purb)->transfer_buffer) {
445 /* allocate a capture buffer per urb */
446 (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
447 if (NULL == (*purb)->transfer_buffer) {
448 usX2Y_urbs_release(subs);
449 return -ENOMEM;
450 }
451 }
452 (*purb)->dev = dev;
453 (*purb)->pipe = pipe;
454 (*purb)->number_of_packets = nr_of_packs();
455 (*purb)->context = subs;
456 (*purb)->interval = 1;
457 (*purb)->complete = i_usX2Y_subs_startup;
458 }
459 return 0;
460}
461
462static void usX2Y_subs_startup(snd_usX2Y_substream_t *subs)
463{
464 usX2Ydev_t *usX2Y = subs->usX2Y;
465 usX2Y->prepare_subs = subs;
466 subs->urb[0]->start_frame = -1;
467 wmb();
468 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
469}
470
471static int usX2Y_urbs_start(snd_usX2Y_substream_t *subs)
472{
473 int i, err;
474 usX2Ydev_t *usX2Y = subs->usX2Y;
475
476 if ((err = usX2Y_urbs_allocate(subs)) < 0)
477 return err;
478 subs->completed_urb = NULL;
479 for (i = 0; i < 4; i++) {
480 snd_usX2Y_substream_t *subs = usX2Y->subs[i];
481 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
482 goto start;
483 }
484 usX2Y->wait_iso_frame = -1;
485 start:
486 {
487 usX2Y_subs_startup(subs);
488 for (i = 0; i < NRURBS; i++) {
489 struct urb *urb = subs->urb[i];
490 if (usb_pipein(urb->pipe)) {
491 unsigned long pack;
492 if (0 == i)
493 atomic_set(&subs->state, state_STARTING3);
494 urb->dev = usX2Y->chip.dev;
495 urb->transfer_flags = URB_ISO_ASAP;
496 for (pack = 0; pack < nr_of_packs(); pack++) {
497 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
498 urb->iso_frame_desc[pack].length = subs->maxpacksize;
499 }
500 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
501 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
502 snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
503 err = -EPIPE;
504 goto cleanup;
505 } else {
506 if (0 > usX2Y->wait_iso_frame)
507 usX2Y->wait_iso_frame = urb->start_frame;
508 }
509 urb->transfer_flags = 0;
510 } else {
511 atomic_set(&subs->state, state_STARTING1);
512 break;
513 }
514 }
515 err = 0;
516 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
517 if (atomic_read(&subs->state) != state_PREPARED) {
518 err = -EPIPE;
519 }
520
521 cleanup:
522 if (err) {
523 usX2Y_subs_startup_finish(usX2Y);
524 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
525 }
526 }
527 return err;
528}
529
530/*
531 * return the current pcm pointer. just return the hwptr_done value.
532 */
533static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(snd_pcm_substream_t *substream)
534{
535 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
536 return subs->hwptr_done;
537}
538/*
539 * start/stop substream
540 */
541static int snd_usX2Y_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
542{
543 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
544
545 switch (cmd) {
546 case SNDRV_PCM_TRIGGER_START:
547 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
548 if (atomic_read(&subs->state) == state_PREPARED &&
549 atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
550 atomic_set(&subs->state, state_PRERUNNING);
551 } else {
552 snd_printdd("\n");
553 return -EPIPE;
554 }
555 break;
556 case SNDRV_PCM_TRIGGER_STOP:
557 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
558 if (atomic_read(&subs->state) >= state_PRERUNNING)
559 atomic_set(&subs->state, state_PREPARED);
560 break;
561 default:
562 return -EINVAL;
563 }
564 return 0;
565}
566
567
568/*
569 * allocate a buffer, setup samplerate
570 *
571 * so far we use a physically linear buffer although packetize transfer
572 * doesn't need a continuous area.
573 * if sg buffer is supported on the later version of alsa, we'll follow
574 * that.
575 */
576static struct s_c2
577{
578 char c1, c2;
579}
580 SetRate44100[] =
581{
582 { 0x14, 0x08}, // this line sets 44100, well actually a little less
583 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
584 { 0x18, 0x42},
585 { 0x18, 0x45},
586 { 0x18, 0x46},
587 { 0x18, 0x48},
588 { 0x18, 0x4A},
589 { 0x18, 0x4C},
590 { 0x18, 0x4E},
591 { 0x18, 0x50},
592 { 0x18, 0x52},
593 { 0x18, 0x54},
594 { 0x18, 0x56},
595 { 0x18, 0x58},
596 { 0x18, 0x5A},
597 { 0x18, 0x5C},
598 { 0x18, 0x5E},
599 { 0x18, 0x60},
600 { 0x18, 0x62},
601 { 0x18, 0x64},
602 { 0x18, 0x66},
603 { 0x18, 0x68},
604 { 0x18, 0x6A},
605 { 0x18, 0x6C},
606 { 0x18, 0x6E},
607 { 0x18, 0x70},
608 { 0x18, 0x72},
609 { 0x18, 0x74},
610 { 0x18, 0x76},
611 { 0x18, 0x78},
612 { 0x18, 0x7A},
613 { 0x18, 0x7C},
614 { 0x18, 0x7E}
615};
616static struct s_c2 SetRate48000[] =
617{
618 { 0x14, 0x09}, // this line sets 48000, well actually a little less
619 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
620 { 0x18, 0x42},
621 { 0x18, 0x45},
622 { 0x18, 0x46},
623 { 0x18, 0x48},
624 { 0x18, 0x4A},
625 { 0x18, 0x4C},
626 { 0x18, 0x4E},
627 { 0x18, 0x50},
628 { 0x18, 0x52},
629 { 0x18, 0x54},
630 { 0x18, 0x56},
631 { 0x18, 0x58},
632 { 0x18, 0x5A},
633 { 0x18, 0x5C},
634 { 0x18, 0x5E},
635 { 0x18, 0x60},
636 { 0x18, 0x62},
637 { 0x18, 0x64},
638 { 0x18, 0x66},
639 { 0x18, 0x68},
640 { 0x18, 0x6A},
641 { 0x18, 0x6C},
642 { 0x18, 0x6E},
643 { 0x18, 0x70},
644 { 0x18, 0x73},
645 { 0x18, 0x74},
646 { 0x18, 0x76},
647 { 0x18, 0x78},
648 { 0x18, 0x7A},
649 { 0x18, 0x7C},
650 { 0x18, 0x7E}
651};
652#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
653
654static void i_usX2Y_04Int(struct urb* urb, struct pt_regs *regs)
655{
656 usX2Ydev_t* usX2Y = urb->context;
657
658 if (urb->status) {
659 snd_printk("snd_usX2Y_04Int() urb->status=%i\n", urb->status);
660 }
661 if (0 == --usX2Y->US04->len)
662 wake_up(&usX2Y->In04WaitQueue);
663}
664
665static int usX2Y_rate_set(usX2Ydev_t *usX2Y, int rate)
666{
667 int err = 0, i;
668 snd_usX2Y_urbSeq_t *us = NULL;
669 int *usbdata = NULL;
670 struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
671
672 if (usX2Y->rate != rate) {
673 us = kmalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
674 if (NULL == us) {
675 err = -ENOMEM;
676 goto cleanup;
677 }
678 memset(us, 0, sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS);
679 usbdata = kmalloc(sizeof(int)*NOOF_SETRATE_URBS, GFP_KERNEL);
680 if (NULL == usbdata) {
681 err = -ENOMEM;
682 goto cleanup;
683 }
684 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
685 if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
686 err = -ENOMEM;
687 goto cleanup;
688 }
689 ((char*)(usbdata + i))[0] = ra[i].c1;
690 ((char*)(usbdata + i))[1] = ra[i].c2;
691 usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4),
692 usbdata + i, 2, i_usX2Y_04Int, usX2Y);
693#ifdef OLD_USB
694 us->urb[i]->transfer_flags = USB_QUEUE_BULK;
695#endif
696 }
697 us->submitted = 0;
698 us->len = NOOF_SETRATE_URBS;
699 usX2Y->US04 = us;
700 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
701 usX2Y->US04 = NULL;
702 if (us->len)
703 err = -ENODEV;
704 cleanup:
705 if (us) {
706 us->submitted = 2*NOOF_SETRATE_URBS;
707 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
708 struct urb *urb = us->urb[i];
709 if (urb->status) {
710 if (!err)
711 err = -ENODEV;
712 usb_kill_urb(urb);
713 }
714 usb_free_urb(urb);
715 }
716 usX2Y->US04 = NULL;
717 kfree(usbdata);
718 kfree(us);
719 if (!err) {
720 usX2Y->rate = rate;
721 }
722 }
723 }
724
725 return err;
726}
727
728
729static int usX2Y_format_set(usX2Ydev_t *usX2Y, snd_pcm_format_t format)
730{
731 int alternate, err;
732 struct list_head* p;
733 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
734 alternate = 2;
735 usX2Y->stride = 6;
736 } else {
737 alternate = 1;
738 usX2Y->stride = 4;
739 }
740 list_for_each(p, &usX2Y->chip.midi_list) {
741 snd_usbmidi_input_stop(p);
742 }
743 usb_kill_urb(usX2Y->In04urb);
744 if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) {
745 snd_printk("usb_set_interface error \n");
746 return err;
747 }
748 usX2Y->In04urb->dev = usX2Y->chip.dev;
749 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
750 list_for_each(p, &usX2Y->chip.midi_list) {
751 snd_usbmidi_input_start(p);
752 }
753 usX2Y->format = format;
754 usX2Y->rate = 0;
755 return err;
756}
757
758
759static int snd_usX2Y_pcm_hw_params(snd_pcm_substream_t *substream,
760 snd_pcm_hw_params_t *hw_params)
761{
762 int err = 0;
763 unsigned int rate = params_rate(hw_params);
764 snd_pcm_format_t format = params_format(hw_params);
765 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
766
767 { // all pcm substreams off one usX2Y have to operate at the same rate & format
768 snd_card_t *card = substream->pstr->pcm->card;
769 struct list_head *list;
770 list_for_each(list, &card->devices) {
771 snd_device_t *dev;
772 snd_pcm_t *pcm;
773 int s;
774 dev = snd_device(list);
775 if (dev->type != SNDRV_DEV_PCM)
776 continue;
777 pcm = dev->device_data;
778 for (s = 0; s < 2; ++s) {
779 snd_pcm_substream_t *test_substream;
780 test_substream = pcm->streams[s].substream;
781 if (test_substream && test_substream != substream &&
782 test_substream->runtime &&
783 ((test_substream->runtime->format &&
784 test_substream->runtime->format != format) ||
785 (test_substream->runtime->rate &&
786 test_substream->runtime->rate != rate)))
787 return -EINVAL;
788 }
789 }
790 }
791 if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
792 snd_printk("snd_pcm_lib_malloc_pages(%p, %i) returned %i\n", substream, params_buffer_bytes(hw_params), err);
793 return err;
794 }
795 return 0;
796}
797
798/*
799 * free the buffer
800 */
801static int snd_usX2Y_pcm_hw_free(snd_pcm_substream_t *substream)
802{
803 snd_pcm_runtime_t *runtime = substream->runtime;
804 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
805 down(&subs->usX2Y->prepare_mutex);
806 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
807
808 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
809 snd_usX2Y_substream_t *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
810 atomic_set(&subs->state, state_STOPPED);
811 usX2Y_urbs_release(subs);
812 if (!cap_subs->pcm_substream ||
813 !cap_subs->pcm_substream->runtime ||
814 !cap_subs->pcm_substream->runtime->status ||
815 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
816 atomic_set(&cap_subs->state, state_STOPPED);
817 usX2Y_urbs_release(cap_subs);
818 }
819 } else {
820 snd_usX2Y_substream_t *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
821 if (atomic_read(&playback_subs->state) < state_PREPARED) {
822 atomic_set(&subs->state, state_STOPPED);
823 usX2Y_urbs_release(subs);
824 }
825 }
826 up(&subs->usX2Y->prepare_mutex);
827 return snd_pcm_lib_free_pages(substream);
828}
829/*
830 * prepare callback
831 *
832 * set format and initialize urbs
833 */
834static int snd_usX2Y_pcm_prepare(snd_pcm_substream_t *substream)
835{
836 snd_pcm_runtime_t *runtime = substream->runtime;
837 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
838 usX2Ydev_t *usX2Y = subs->usX2Y;
839 snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
840 int err = 0;
841 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
842
843 down(&usX2Y->prepare_mutex);
844 usX2Y_subs_prepare(subs);
845// Start hardware streams
846// SyncStream first....
847 if (atomic_read(&capsubs->state) < state_PREPARED) {
848 if (usX2Y->format != runtime->format)
849 if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
850 goto up_prepare_mutex;
851 if (usX2Y->rate != runtime->rate)
852 if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
853 goto up_prepare_mutex;
854 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
855 if (0 > (err = usX2Y_urbs_start(capsubs)))
856 goto up_prepare_mutex;
857 }
858
859 if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
860 err = usX2Y_urbs_start(subs);
861
862 up_prepare_mutex:
863 up(&usX2Y->prepare_mutex);
864 return err;
865}
866
867static snd_pcm_hardware_t snd_usX2Y_2c =
868{
869 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
870 SNDRV_PCM_INFO_BLOCK_TRANSFER |
871 SNDRV_PCM_INFO_MMAP_VALID),
872 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
873 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
874 .rate_min = 44100,
875 .rate_max = 48000,
876 .channels_min = 2,
877 .channels_max = 2,
878 .buffer_bytes_max = (2*128*1024),
879 .period_bytes_min = 64,
880 .period_bytes_max = (128*1024),
881 .periods_min = 2,
882 .periods_max = 1024,
883 .fifo_size = 0
884};
885
886
887
888static int snd_usX2Y_pcm_open(snd_pcm_substream_t *substream)
889{
890 snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **)
891 snd_pcm_substream_chip(substream))[substream->stream];
892 snd_pcm_runtime_t *runtime = substream->runtime;
893
894 if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
895 return -EBUSY;
896
897 runtime->hw = snd_usX2Y_2c;
898 runtime->private_data = subs;
899 subs->pcm_substream = substream;
900 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
901 return 0;
902}
903
904
905
906static int snd_usX2Y_pcm_close(snd_pcm_substream_t *substream)
907{
908 snd_pcm_runtime_t *runtime = substream->runtime;
909 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
910 int err = 0;
911
912 subs->pcm_substream = NULL;
913
914 return err;
915}
916
917
918static snd_pcm_ops_t snd_usX2Y_pcm_ops =
919{
920 .open = snd_usX2Y_pcm_open,
921 .close = snd_usX2Y_pcm_close,
922 .ioctl = snd_pcm_lib_ioctl,
923 .hw_params = snd_usX2Y_pcm_hw_params,
924 .hw_free = snd_usX2Y_pcm_hw_free,
925 .prepare = snd_usX2Y_pcm_prepare,
926 .trigger = snd_usX2Y_pcm_trigger,
927 .pointer = snd_usX2Y_pcm_pointer,
928};
929
930
931/*
932 * free a usb stream instance
933 */
934static void usX2Y_audio_stream_free(snd_usX2Y_substream_t **usX2Y_substream)
935{
936 if (NULL != usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]) {
937 kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
938 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
939 }
940 kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
941 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
942}
943
944static void snd_usX2Y_pcm_private_free(snd_pcm_t *pcm)
945{
946 snd_usX2Y_substream_t **usX2Y_stream = pcm->private_data;
947 if (usX2Y_stream) {
948 snd_pcm_lib_preallocate_free_for_all(pcm);
949 usX2Y_audio_stream_free(usX2Y_stream);
950 }
951}
952
953static int usX2Y_audio_stream_new(snd_card_t *card, int playback_endpoint, int capture_endpoint)
954{
955 snd_pcm_t *pcm;
956 int err, i;
957 snd_usX2Y_substream_t **usX2Y_substream =
958 usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs;
959
960 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
961 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
962 usX2Y_substream[i] = kcalloc(1, sizeof(snd_usX2Y_substream_t), GFP_KERNEL);
963 if (NULL == usX2Y_substream[i]) {
964 snd_printk(KERN_ERR "cannot malloc\n");
965 return -ENOMEM;
966 }
967 usX2Y_substream[i]->usX2Y = usX2Y(card);
968 }
969
970 if (playback_endpoint)
971 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
972 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
973
974 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs,
975 playback_endpoint ? 1 : 0, 1,
976 &pcm);
977 if (err < 0) {
978 usX2Y_audio_stream_free(usX2Y_substream);
979 return err;
980 }
981
982 if (playback_endpoint)
983 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
984 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
985
986 pcm->private_data = usX2Y_substream;
987 pcm->private_free = snd_usX2Y_pcm_private_free;
988 pcm->info_flags = 0;
989
990 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs);
991
992 if ((playback_endpoint &&
993 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
994 SNDRV_DMA_TYPE_CONTINUOUS,
995 snd_dma_continuous_data(GFP_KERNEL),
996 64*1024, 128*1024))) ||
997 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
998 SNDRV_DMA_TYPE_CONTINUOUS,
999 snd_dma_continuous_data(GFP_KERNEL),
1000 64*1024, 128*1024))) {
1001 snd_usX2Y_pcm_private_free(pcm);
1002 return err;
1003 }
1004 usX2Y(card)->chip.pcm_devs++;
1005
1006 return 0;
1007}
1008
1009/*
1010 * create a chip instance and set its names.
1011 */
1012int usX2Y_audio_create(snd_card_t* card)
1013{
1014 int err = 0;
1015
1016 INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list);
1017
1018 if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1019 return err;
1020 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428)
1021 if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1022 return err;
1023 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122)
1024 err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
1025 return err;
1026}
diff --git a/sound/usb/usx2y/usx2y.h b/sound/usb/usx2y/usx2y.h
new file mode 100644
index 000000000000..7e59263dd895
--- /dev/null
+++ b/sound/usb/usx2y/usx2y.h
@@ -0,0 +1,51 @@
1/*
2 * Driver for Tascam US-X2Y USB soundcards
3 *
4 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef __SOUND_USX2Y_COMMON_H
22#define __SOUND_USX2Y_COMMON_H
23
24
25#define USX2Y_DRIVER_VERSION 0x0100 /* 0.1.0 */
26
27
28/* hwdep id string */
29#define SND_USX2Y_LOADER_ID "USX2Y Loader"
30#define SND_USX2Y_USBPCM_ID "USX2Y USBPCM"
31
32/* hardware type */
33enum {
34 USX2Y_TYPE_122,
35 USX2Y_TYPE_224,
36 USX2Y_TYPE_428,
37 USX2Y_TYPE_NUMS
38};
39
40#define USB_ID_US122 0x8007
41#define USB_ID_US224 0x8005
42#define USB_ID_US428 0x8001
43
44/* chip status */
45enum {
46 USX2Y_STAT_CHIP_INIT = (1 << 0), /* all operational */
47 USX2Y_STAT_CHIP_MMAP_PCM_URBS = (1 << 1), /* pcm transport over mmaped urbs */
48 USX2Y_STAT_CHIP_HUP = (1 << 31), /* all operational */
49};
50
51#endif /* __SOUND_USX2Y_COMMON_H */
diff --git a/sound/usb/usx2y/usx2yhwdeppcm.c b/sound/usb/usx2y/usx2yhwdeppcm.c
new file mode 100644
index 000000000000..bb2c8e9000c6
--- /dev/null
+++ b/sound/usb/usx2y/usx2yhwdeppcm.c
@@ -0,0 +1,807 @@
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/* USX2Y "rawusb" aka hwdep_pcm implementation
18
19 Its usb's unableness to atomically handle power of 2 period sized data chuncs
20 at standard samplerates,
21 what led to this part of the usx2y module:
22 It provides the alsa kernel half of the usx2y-alsa-jack driver pair.
23 The pair uses a hardware dependant alsa-device for mmaped pcm transport.
24 Advantage achieved:
25 The usb_hc moves pcm data from/into memory via DMA.
26 That memory is mmaped by jack's usx2y driver.
27 Jack's usx2y driver is the first/last to read/write pcm data.
28 Read/write is a combination of power of 2 period shaping and
29 float/int conversation.
30 Compared to mainline alsa/jack we leave out power of 2 period shaping inside
31 snd-usb-usx2y which needs memcpy() and additional buffers.
32 As a side effect possible unwanted pcm-data coruption resulting of
33 standard alsa's snd-usb-usx2y period shaping scheme falls away.
34 Result is sane jack operation at buffering schemes down to 128frames,
35 2 periods.
36 plain usx2y alsa mode is able to achieve 64frames, 4periods, but only at the
37 cost of easier triggered i.e. aeolus xruns (128 or 256frames,
38 2periods works but is useless cause of crackling).
39
40 This is a first "proof of concept" implementation.
41 Later, funcionalities should migrate to more apropriate places:
42 Userland:
43 - The jackd could mmap its float-pcm buffers directly from alsa-lib.
44 - alsa-lib could provide power of 2 period sized shaping combined with int/float
45 conversation.
46 Currently the usx2y jack driver provides above 2 services.
47 Kernel:
48 - rawusb dma pcm buffer transport should go to snd-usb-lib, so also snd-usb-audio
49 devices can use it.
50 Currently rawusb dma pcm buffer transport (this file) is only available to snd-usb-usx2y.
51*/
52
53#include "usbusx2yaudio.c"
54
55#if defined(USX2Y_NRPACKS_VARIABLE) || (!defined(USX2Y_NRPACKS_VARIABLE) && USX2Y_NRPACKS == 1)
56
57#include <sound/hwdep.h>
58
59
60static int usX2Y_usbpcm_urb_capt_retire(snd_usX2Y_substream_t *subs)
61{
62 struct urb *urb = subs->completed_urb;
63 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
64 int i, lens = 0, hwptr_done = subs->hwptr_done;
65 usX2Ydev_t *usX2Y = subs->usX2Y;
66 if (0 > usX2Y->hwdep_pcm_shm->capture_iso_start) { //FIXME
67 int head = usX2Y->hwdep_pcm_shm->captured_iso_head + 1;
68 if (head >= ARRAY_SIZE(usX2Y->hwdep_pcm_shm->captured_iso))
69 head = 0;
70 usX2Y->hwdep_pcm_shm->capture_iso_start = head;
71 snd_printdd("cap start %i\n", head);
72 }
73 for (i = 0; i < nr_of_packs(); i++) {
74 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
75 snd_printk("activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status);
76 return urb->iso_frame_desc[i].status;
77 }
78 lens += urb->iso_frame_desc[i].actual_length / usX2Y->stride;
79 }
80 if ((hwptr_done += lens) >= runtime->buffer_size)
81 hwptr_done -= runtime->buffer_size;
82 subs->hwptr_done = hwptr_done;
83 subs->transfer_done += lens;
84 /* update the pointer, call callback if necessary */
85 if (subs->transfer_done >= runtime->period_size) {
86 subs->transfer_done -= runtime->period_size;
87 snd_pcm_period_elapsed(subs->pcm_substream);
88 }
89 return 0;
90}
91
92static inline int usX2Y_iso_frames_per_buffer(snd_pcm_runtime_t *runtime, usX2Ydev_t * usX2Y)
93{
94 return (runtime->buffer_size * 1000) / usX2Y->rate + 1; //FIXME: so far only correct period_size == 2^x ?
95}
96
97/*
98 * prepare urb for playback data pipe
99 *
100 * we copy the data directly from the pcm buffer.
101 * the current position to be copied is held in hwptr field.
102 * since a urb can handle only a single linear buffer, if the total
103 * transferred area overflows the buffer boundary, we cannot send
104 * it directly from the buffer. thus the data is once copied to
105 * a temporary buffer and urb points to that.
106 */
107static int usX2Y_hwdep_urb_play_prepare(snd_usX2Y_substream_t *subs,
108 struct urb *urb)
109{
110 int count, counts, pack;
111 usX2Ydev_t *usX2Y = subs->usX2Y;
112 struct snd_usX2Y_hwdep_pcm_shm *shm = usX2Y->hwdep_pcm_shm;
113 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
114
115 if (0 > shm->playback_iso_start) {
116 shm->playback_iso_start = shm->captured_iso_head -
117 usX2Y_iso_frames_per_buffer(runtime, usX2Y);
118 if (0 > shm->playback_iso_start)
119 shm->playback_iso_start += ARRAY_SIZE(shm->captured_iso);
120 shm->playback_iso_head = shm->playback_iso_start;
121 }
122
123 count = 0;
124 for (pack = 0; pack < nr_of_packs(); pack++) {
125 /* calculate the size of a packet */
126 counts = shm->captured_iso[shm->playback_iso_head].length / usX2Y->stride;
127 if (counts < 43 || counts > 50) {
128 snd_printk("should not be here with counts=%i\n", counts);
129 return -EPIPE;
130 }
131 /* set up descriptor */
132 urb->iso_frame_desc[pack].offset = shm->captured_iso[shm->playback_iso_head].offset;
133 urb->iso_frame_desc[pack].length = shm->captured_iso[shm->playback_iso_head].length;
134 if (atomic_read(&subs->state) != state_RUNNING)
135 memset((char *)urb->transfer_buffer + urb->iso_frame_desc[pack].offset, 0,
136 urb->iso_frame_desc[pack].length);
137 if (++shm->playback_iso_head >= ARRAY_SIZE(shm->captured_iso))
138 shm->playback_iso_head = 0;
139 count += counts;
140 }
141 urb->transfer_buffer_length = count * usX2Y->stride;
142 return 0;
143}
144
145
146static inline void usX2Y_usbpcm_urb_capt_iso_advance(snd_usX2Y_substream_t *subs, struct urb *urb)
147{
148 int pack;
149 for (pack = 0; pack < nr_of_packs(); ++pack) {
150 struct usb_iso_packet_descriptor *desc = urb->iso_frame_desc + pack;
151 if (NULL != subs) {
152 snd_usX2Y_hwdep_pcm_shm_t *shm = subs->usX2Y->hwdep_pcm_shm;
153 int head = shm->captured_iso_head + 1;
154 if (head >= ARRAY_SIZE(shm->captured_iso))
155 head = 0;
156 shm->captured_iso[head].frame = urb->start_frame + pack;
157 shm->captured_iso[head].offset = desc->offset;
158 shm->captured_iso[head].length = desc->actual_length;
159 shm->captured_iso_head = head;
160 shm->captured_iso_frames++;
161 }
162 if ((desc->offset += desc->length * NRURBS*nr_of_packs()) +
163 desc->length >= SSS)
164 desc->offset -= (SSS - desc->length);
165 }
166}
167
168static inline int usX2Y_usbpcm_usbframe_complete(snd_usX2Y_substream_t *capsubs,
169 snd_usX2Y_substream_t *capsubs2,
170 snd_usX2Y_substream_t *playbacksubs, int frame)
171{
172 int err, state;
173 struct urb *urb = playbacksubs->completed_urb;
174
175 state = atomic_read(&playbacksubs->state);
176 if (NULL != urb) {
177 if (state == state_RUNNING)
178 usX2Y_urb_play_retire(playbacksubs, urb);
179 else
180 if (state >= state_PRERUNNING) {
181 atomic_inc(&playbacksubs->state);
182 }
183 } else {
184 switch (state) {
185 case state_STARTING1:
186 urb = playbacksubs->urb[0];
187 atomic_inc(&playbacksubs->state);
188 break;
189 case state_STARTING2:
190 urb = playbacksubs->urb[1];
191 atomic_inc(&playbacksubs->state);
192 break;
193 }
194 }
195 if (urb) {
196 if ((err = usX2Y_hwdep_urb_play_prepare(playbacksubs, urb)) ||
197 (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
198 return err;
199 }
200 }
201
202 playbacksubs->completed_urb = NULL;
203
204 state = atomic_read(&capsubs->state);
205 if (state >= state_PREPARED) {
206 if (state == state_RUNNING) {
207 if ((err = usX2Y_usbpcm_urb_capt_retire(capsubs)))
208 return err;
209 } else {
210 if (state >= state_PRERUNNING)
211 atomic_inc(&capsubs->state);
212 }
213 usX2Y_usbpcm_urb_capt_iso_advance(capsubs, capsubs->completed_urb);
214 if (NULL != capsubs2)
215 usX2Y_usbpcm_urb_capt_iso_advance(NULL, capsubs2->completed_urb);
216 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
217 return err;
218 if (NULL != capsubs2)
219 if ((err = usX2Y_urb_submit(capsubs2, capsubs2->completed_urb, frame)))
220 return err;
221 }
222 capsubs->completed_urb = NULL;
223 if (NULL != capsubs2)
224 capsubs2->completed_urb = NULL;
225 return 0;
226}
227
228
229static void i_usX2Y_usbpcm_urb_complete(struct urb *urb, struct pt_regs *regs)
230{
231 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
232 usX2Ydev_t *usX2Y = subs->usX2Y;
233 snd_usX2Y_substream_t *capsubs, *capsubs2, *playbacksubs;
234
235 if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
236 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame);
237 return;
238 }
239 if (unlikely(urb->status)) {
240 usX2Y_error_urb_status(usX2Y, subs, urb);
241 return;
242 }
243 if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame))
244 subs->completed_urb = urb;
245 else {
246 usX2Y_error_sequence(usX2Y, subs, urb);
247 return;
248 }
249
250 capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
251 capsubs2 = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
252 playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
253 if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED &&
254 (NULL == capsubs2 || capsubs2->completed_urb) &&
255 (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) {
256 if (!usX2Y_usbpcm_usbframe_complete(capsubs, capsubs2, playbacksubs, urb->start_frame)) {
257 if (nr_of_packs() <= urb->start_frame &&
258 urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci
259 usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs();
260 else
261 usX2Y->wait_iso_frame += nr_of_packs();
262 } else {
263 snd_printdd("\n");
264 usX2Y_clients_stop(usX2Y);
265 }
266 }
267}
268
269
270static void usX2Y_hwdep_urb_release(struct urb** urb)
271{
272 usb_kill_urb(*urb);
273 usb_free_urb(*urb);
274 *urb = NULL;
275}
276
277/*
278 * release a substream
279 */
280static void usX2Y_usbpcm_urbs_release(snd_usX2Y_substream_t *subs)
281{
282 int i;
283 snd_printdd("snd_usX2Y_urbs_release() %i\n", subs->endpoint);
284 for (i = 0; i < NRURBS; i++)
285 usX2Y_hwdep_urb_release(subs->urb + i);
286}
287
288static void usX2Y_usbpcm_subs_startup_finish(usX2Ydev_t * usX2Y)
289{
290 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_usbpcm_urb_complete);
291 usX2Y->prepare_subs = NULL;
292}
293
294static void i_usX2Y_usbpcm_subs_startup(struct urb *urb, struct pt_regs *regs)
295{
296 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
297 usX2Ydev_t *usX2Y = subs->usX2Y;
298 snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs;
299 if (NULL != prepare_subs &&
300 urb->start_frame == prepare_subs->urb[0]->start_frame) {
301 atomic_inc(&prepare_subs->state);
302 if (prepare_subs == usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]) {
303 snd_usX2Y_substream_t *cap_subs2 = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
304 if (cap_subs2 != NULL)
305 atomic_inc(&cap_subs2->state);
306 }
307 usX2Y_usbpcm_subs_startup_finish(usX2Y);
308 wake_up(&usX2Y->prepare_wait_queue);
309 }
310
311 i_usX2Y_usbpcm_urb_complete(urb, regs);
312}
313
314/*
315 * initialize a substream's urbs
316 */
317static int usX2Y_usbpcm_urbs_allocate(snd_usX2Y_substream_t *subs)
318{
319 int i;
320 unsigned int pipe;
321 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
322 struct usb_device *dev = subs->usX2Y->chip.dev;
323
324 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
325 usb_rcvisocpipe(dev, subs->endpoint);
326 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
327 if (!subs->maxpacksize)
328 return -EINVAL;
329
330 /* allocate and initialize data urbs */
331 for (i = 0; i < NRURBS; i++) {
332 struct urb** purb = subs->urb + i;
333 if (*purb) {
334 usb_kill_urb(*purb);
335 continue;
336 }
337 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
338 if (NULL == *purb) {
339 usX2Y_usbpcm_urbs_release(subs);
340 return -ENOMEM;
341 }
342 (*purb)->transfer_buffer = is_playback ?
343 subs->usX2Y->hwdep_pcm_shm->playback : (
344 subs->endpoint == 0x8 ?
345 subs->usX2Y->hwdep_pcm_shm->capture0x8 :
346 subs->usX2Y->hwdep_pcm_shm->capture0xA);
347
348 (*purb)->dev = dev;
349 (*purb)->pipe = pipe;
350 (*purb)->number_of_packets = nr_of_packs();
351 (*purb)->context = subs;
352 (*purb)->interval = 1;
353 (*purb)->complete = i_usX2Y_usbpcm_subs_startup;
354 }
355 return 0;
356}
357
358/*
359 * free the buffer
360 */
361static int snd_usX2Y_usbpcm_hw_free(snd_pcm_substream_t *substream)
362{
363 snd_pcm_runtime_t *runtime = substream->runtime;
364 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data,
365 *cap_subs2 = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
366 down(&subs->usX2Y->prepare_mutex);
367 snd_printdd("snd_usX2Y_usbpcm_hw_free(%p)\n", substream);
368
369 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
370 snd_usX2Y_substream_t *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
371 atomic_set(&subs->state, state_STOPPED);
372 usX2Y_usbpcm_urbs_release(subs);
373 if (!cap_subs->pcm_substream ||
374 !cap_subs->pcm_substream->runtime ||
375 !cap_subs->pcm_substream->runtime->status ||
376 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
377 atomic_set(&cap_subs->state, state_STOPPED);
378 if (NULL != cap_subs2)
379 atomic_set(&cap_subs2->state, state_STOPPED);
380 usX2Y_usbpcm_urbs_release(cap_subs);
381 if (NULL != cap_subs2)
382 usX2Y_usbpcm_urbs_release(cap_subs2);
383 }
384 } else {
385 snd_usX2Y_substream_t *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
386 if (atomic_read(&playback_subs->state) < state_PREPARED) {
387 atomic_set(&subs->state, state_STOPPED);
388 if (NULL != cap_subs2)
389 atomic_set(&cap_subs2->state, state_STOPPED);
390 usX2Y_usbpcm_urbs_release(subs);
391 if (NULL != cap_subs2)
392 usX2Y_usbpcm_urbs_release(cap_subs2);
393 }
394 }
395 up(&subs->usX2Y->prepare_mutex);
396 return snd_pcm_lib_free_pages(substream);
397}
398
399static void usX2Y_usbpcm_subs_startup(snd_usX2Y_substream_t *subs)
400{
401 usX2Ydev_t * usX2Y = subs->usX2Y;
402 usX2Y->prepare_subs = subs;
403 subs->urb[0]->start_frame = -1;
404 smp_wmb(); // Make shure above modifications are seen by i_usX2Y_subs_startup()
405 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_usbpcm_subs_startup);
406}
407
408static int usX2Y_usbpcm_urbs_start(snd_usX2Y_substream_t *subs)
409{
410 int p, u, err,
411 stream = subs->pcm_substream->stream;
412 usX2Ydev_t *usX2Y = subs->usX2Y;
413
414 if (SNDRV_PCM_STREAM_CAPTURE == stream) {
415 usX2Y->hwdep_pcm_shm->captured_iso_head = -1;
416 usX2Y->hwdep_pcm_shm->captured_iso_frames = 0;
417 }
418
419 for (p = 0; 3 >= (stream + p); p += 2) {
420 snd_usX2Y_substream_t *subs = usX2Y->subs[stream + p];
421 if (subs != NULL) {
422 if ((err = usX2Y_usbpcm_urbs_allocate(subs)) < 0)
423 return err;
424 subs->completed_urb = NULL;
425 }
426 }
427
428 for (p = 0; p < 4; p++) {
429 snd_usX2Y_substream_t *subs = usX2Y->subs[p];
430 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
431 goto start;
432 }
433 usX2Y->wait_iso_frame = -1;
434
435 start:
436 usX2Y_usbpcm_subs_startup(subs);
437 for (u = 0; u < NRURBS; u++) {
438 for (p = 0; 3 >= (stream + p); p += 2) {
439 snd_usX2Y_substream_t *subs = usX2Y->subs[stream + p];
440 if (subs != NULL) {
441 struct urb *urb = subs->urb[u];
442 if (usb_pipein(urb->pipe)) {
443 unsigned long pack;
444 if (0 == u)
445 atomic_set(&subs->state, state_STARTING3);
446 urb->dev = usX2Y->chip.dev;
447 urb->transfer_flags = URB_ISO_ASAP;
448 for (pack = 0; pack < nr_of_packs(); pack++) {
449 urb->iso_frame_desc[pack].offset = subs->maxpacksize * (pack + u * nr_of_packs());
450 urb->iso_frame_desc[pack].length = subs->maxpacksize;
451 }
452 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
453 if ((err = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
454 snd_printk (KERN_ERR "cannot usb_submit_urb() for urb %d, err = %d\n", u, err);
455 err = -EPIPE;
456 goto cleanup;
457 } else {
458 snd_printdd("%i\n", urb->start_frame);
459 if (0 > usX2Y->wait_iso_frame)
460 usX2Y->wait_iso_frame = urb->start_frame;
461 }
462 urb->transfer_flags = 0;
463 } else {
464 atomic_set(&subs->state, state_STARTING1);
465 break;
466 }
467 }
468 }
469 }
470 err = 0;
471 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
472 if (atomic_read(&subs->state) != state_PREPARED)
473 err = -EPIPE;
474
475 cleanup:
476 if (err) {
477 usX2Y_subs_startup_finish(usX2Y); // Call it now
478 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
479 }
480 return err;
481}
482
483/*
484 * prepare callback
485 *
486 * set format and initialize urbs
487 */
488static int snd_usX2Y_usbpcm_prepare(snd_pcm_substream_t *substream)
489{
490 snd_pcm_runtime_t *runtime = substream->runtime;
491 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
492 usX2Ydev_t *usX2Y = subs->usX2Y;
493 snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
494 int err = 0;
495 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
496
497 if (NULL == usX2Y->hwdep_pcm_shm) {
498 if (NULL == (usX2Y->hwdep_pcm_shm = snd_malloc_pages(sizeof(snd_usX2Y_hwdep_pcm_shm_t), GFP_KERNEL)))
499 return -ENOMEM;
500 memset(usX2Y->hwdep_pcm_shm, 0, sizeof(snd_usX2Y_hwdep_pcm_shm_t));
501 }
502
503 down(&usX2Y->prepare_mutex);
504 usX2Y_subs_prepare(subs);
505// Start hardware streams
506// SyncStream first....
507 if (atomic_read(&capsubs->state) < state_PREPARED) {
508 if (usX2Y->format != runtime->format)
509 if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
510 goto up_prepare_mutex;
511 if (usX2Y->rate != runtime->rate)
512 if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
513 goto up_prepare_mutex;
514 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
515 if (0 > (err = usX2Y_usbpcm_urbs_start(capsubs)))
516 goto up_prepare_mutex;
517 }
518
519 if (subs != capsubs) {
520 usX2Y->hwdep_pcm_shm->playback_iso_start = -1;
521 if (atomic_read(&subs->state) < state_PREPARED) {
522 while (usX2Y_iso_frames_per_buffer(runtime, usX2Y) > usX2Y->hwdep_pcm_shm->captured_iso_frames) {
523 signed long timeout;
524 snd_printd("Wait: iso_frames_per_buffer=%i,captured_iso_frames=%i\n", usX2Y_iso_frames_per_buffer(runtime, usX2Y), usX2Y->hwdep_pcm_shm->captured_iso_frames);
525 set_current_state(TASK_INTERRUPTIBLE);
526 timeout = schedule_timeout(HZ/100 + 1);
527 if (signal_pending(current)) {
528 err = -ERESTARTSYS;
529 goto up_prepare_mutex;
530 }
531 }
532 if (0 > (err = usX2Y_usbpcm_urbs_start(subs)))
533 goto up_prepare_mutex;
534 }
535 snd_printd("Ready: iso_frames_per_buffer=%i,captured_iso_frames=%i\n", usX2Y_iso_frames_per_buffer(runtime, usX2Y), usX2Y->hwdep_pcm_shm->captured_iso_frames);
536 } else
537 usX2Y->hwdep_pcm_shm->capture_iso_start = -1;
538
539 up_prepare_mutex:
540 up(&usX2Y->prepare_mutex);
541 return err;
542}
543
544static snd_pcm_hardware_t snd_usX2Y_4c =
545{
546 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
547 SNDRV_PCM_INFO_BLOCK_TRANSFER |
548 SNDRV_PCM_INFO_MMAP_VALID),
549 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
550 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
551 .rate_min = 44100,
552 .rate_max = 48000,
553 .channels_min = 2,
554 .channels_max = 4,
555 .buffer_bytes_max = (2*128*1024),
556 .period_bytes_min = 64,
557 .period_bytes_max = (128*1024),
558 .periods_min = 2,
559 .periods_max = 1024,
560 .fifo_size = 0
561};
562
563
564
565static int snd_usX2Y_usbpcm_open(snd_pcm_substream_t *substream)
566{
567 snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **)
568 snd_pcm_substream_chip(substream))[substream->stream];
569 snd_pcm_runtime_t *runtime = substream->runtime;
570
571 if (!(subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS))
572 return -EBUSY;
573
574 runtime->hw = SNDRV_PCM_STREAM_PLAYBACK == substream->stream ? snd_usX2Y_2c :
575 (subs->usX2Y->subs[3] ? snd_usX2Y_4c : snd_usX2Y_2c);
576 runtime->private_data = subs;
577 subs->pcm_substream = substream;
578 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
579 return 0;
580}
581
582
583static int snd_usX2Y_usbpcm_close(snd_pcm_substream_t *substream)
584{
585 snd_pcm_runtime_t *runtime = substream->runtime;
586 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
587 int err = 0;
588 snd_printd("\n");
589 subs->pcm_substream = NULL;
590 return err;
591}
592
593
594static snd_pcm_ops_t snd_usX2Y_usbpcm_ops =
595{
596 .open = snd_usX2Y_usbpcm_open,
597 .close = snd_usX2Y_usbpcm_close,
598 .ioctl = snd_pcm_lib_ioctl,
599 .hw_params = snd_usX2Y_pcm_hw_params,
600 .hw_free = snd_usX2Y_usbpcm_hw_free,
601 .prepare = snd_usX2Y_usbpcm_prepare,
602 .trigger = snd_usX2Y_pcm_trigger,
603 .pointer = snd_usX2Y_pcm_pointer,
604};
605
606
607static int usX2Y_pcms_lock_check(snd_card_t *card)
608{
609 struct list_head *list;
610 snd_device_t *dev;
611 snd_pcm_t *pcm;
612 int err = 0;
613 list_for_each(list, &card->devices) {
614 dev = snd_device(list);
615 if (dev->type != SNDRV_DEV_PCM)
616 continue;
617 pcm = dev->device_data;
618 down(&pcm->open_mutex);
619 }
620 list_for_each(list, &card->devices) {
621 int s;
622 dev = snd_device(list);
623 if (dev->type != SNDRV_DEV_PCM)
624 continue;
625 pcm = dev->device_data;
626 for (s = 0; s < 2; ++s) {
627 snd_pcm_substream_t *substream;
628 substream = pcm->streams[s].substream;
629 if (substream && substream->open_flag)
630 err = -EBUSY;
631 }
632 }
633 return err;
634}
635
636
637static void usX2Y_pcms_unlock(snd_card_t *card)
638{
639 struct list_head *list;
640 snd_device_t *dev;
641 snd_pcm_t *pcm;
642 list_for_each(list, &card->devices) {
643 dev = snd_device(list);
644 if (dev->type != SNDRV_DEV_PCM)
645 continue;
646 pcm = dev->device_data;
647 up(&pcm->open_mutex);
648 }
649}
650
651
652static int snd_usX2Y_hwdep_pcm_open(snd_hwdep_t *hw, struct file *file)
653{
654 // we need to be the first
655 snd_card_t *card = hw->card;
656 int err = usX2Y_pcms_lock_check(card);
657 if (0 == err)
658 usX2Y(card)->chip_status |= USX2Y_STAT_CHIP_MMAP_PCM_URBS;
659 usX2Y_pcms_unlock(card);
660 return err;
661}
662
663
664static int snd_usX2Y_hwdep_pcm_release(snd_hwdep_t *hw, struct file *file)
665{
666 snd_card_t *card = hw->card;
667 int err = usX2Y_pcms_lock_check(card);
668 if (0 == err)
669 usX2Y(hw->card)->chip_status &= ~USX2Y_STAT_CHIP_MMAP_PCM_URBS;
670 usX2Y_pcms_unlock(card);
671 return err;
672}
673
674
675static void snd_usX2Y_hwdep_pcm_vm_open(struct vm_area_struct *area)
676{
677}
678
679
680static void snd_usX2Y_hwdep_pcm_vm_close(struct vm_area_struct *area)
681{
682}
683
684
685static struct page * snd_usX2Y_hwdep_pcm_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type)
686{
687 unsigned long offset;
688 struct page *page;
689 void *vaddr;
690
691 offset = area->vm_pgoff << PAGE_SHIFT;
692 offset += address - area->vm_start;
693 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
694 vaddr = (char*)((usX2Ydev_t*)area->vm_private_data)->hwdep_pcm_shm + offset;
695 page = virt_to_page(vaddr);
696
697 if (type)
698 *type = VM_FAULT_MINOR;
699
700 return page;
701}
702
703
704static struct vm_operations_struct snd_usX2Y_hwdep_pcm_vm_ops = {
705 .open = snd_usX2Y_hwdep_pcm_vm_open,
706 .close = snd_usX2Y_hwdep_pcm_vm_close,
707 .nopage = snd_usX2Y_hwdep_pcm_vm_nopage,
708};
709
710
711static int snd_usX2Y_hwdep_pcm_mmap(snd_hwdep_t * hw, struct file *filp, struct vm_area_struct *area)
712{
713 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
714 usX2Ydev_t *usX2Y = (usX2Ydev_t*)hw->private_data;
715
716 if (!(((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT))
717 return -EBUSY;
718
719 /* if userspace tries to mmap beyond end of our buffer, fail */
720 if (size > PAGE_ALIGN(sizeof(snd_usX2Y_hwdep_pcm_shm_t))) {
721 snd_printd("%lu > %lu\n", size, (unsigned long)sizeof(snd_usX2Y_hwdep_pcm_shm_t));
722 return -EINVAL;
723 }
724
725 if (!usX2Y->hwdep_pcm_shm) {
726 return -ENODEV;
727 }
728 area->vm_ops = &snd_usX2Y_hwdep_pcm_vm_ops;
729 area->vm_flags |= VM_RESERVED;
730 snd_printd("vm_flags=0x%lX\n", area->vm_flags);
731 area->vm_private_data = hw->private_data;
732 return 0;
733}
734
735
736static void snd_usX2Y_hwdep_pcm_private_free(snd_hwdep_t *hwdep)
737{
738 usX2Ydev_t *usX2Y = (usX2Ydev_t *)hwdep->private_data;
739 if (NULL != usX2Y->hwdep_pcm_shm)
740 snd_free_pages(usX2Y->hwdep_pcm_shm, sizeof(snd_usX2Y_hwdep_pcm_shm_t));
741}
742
743
744static void snd_usX2Y_usbpcm_private_free(snd_pcm_t *pcm)
745{
746 snd_pcm_lib_preallocate_free_for_all(pcm);
747}
748
749
750int usX2Y_hwdep_pcm_new(snd_card_t* card)
751{
752 int err;
753 snd_hwdep_t *hw;
754 snd_pcm_t *pcm;
755 struct usb_device *dev = usX2Y(card)->chip.dev;
756 if (1 != nr_of_packs())
757 return 0;
758
759 if ((err = snd_hwdep_new(card, SND_USX2Y_USBPCM_ID, 1, &hw)) < 0) {
760 snd_printd("\n");
761 return err;
762 }
763 hw->iface = SNDRV_HWDEP_IFACE_USX2Y_PCM;
764 hw->private_data = usX2Y(card);
765 hw->private_free = snd_usX2Y_hwdep_pcm_private_free;
766 hw->ops.open = snd_usX2Y_hwdep_pcm_open;
767 hw->ops.release = snd_usX2Y_hwdep_pcm_release;
768 hw->ops.mmap = snd_usX2Y_hwdep_pcm_mmap;
769 hw->exclusive = 1;
770 sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm", dev->bus->busnum, dev->devnum);
771
772 err = snd_pcm_new(card, NAME_ALLCAPS" hwdep Audio", 2, 1, 1, &pcm);
773 if (err < 0) {
774 return err;
775 }
776 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_usbpcm_ops);
777 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_usbpcm_ops);
778
779 pcm->private_data = usX2Y(card)->subs;
780 pcm->private_free = snd_usX2Y_usbpcm_private_free;
781 pcm->info_flags = 0;
782
783 sprintf(pcm->name, NAME_ALLCAPS" hwdep Audio");
784 if (0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
785 SNDRV_DMA_TYPE_CONTINUOUS,
786 snd_dma_continuous_data(GFP_KERNEL),
787 64*1024, 128*1024)) ||
788 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
789 SNDRV_DMA_TYPE_CONTINUOUS,
790 snd_dma_continuous_data(GFP_KERNEL),
791 64*1024, 128*1024))) {
792 snd_usX2Y_usbpcm_private_free(pcm);
793 return err;
794 }
795
796
797 return 0;
798}
799
800#else
801
802int usX2Y_hwdep_pcm_new(snd_card_t* card)
803{
804 return 0;
805}
806
807#endif
diff --git a/sound/usb/usx2y/usx2yhwdeppcm.h b/sound/usb/usx2y/usx2yhwdeppcm.h
new file mode 100644
index 000000000000..d68f0cbdbbe2
--- /dev/null
+++ b/sound/usb/usx2y/usx2yhwdeppcm.h
@@ -0,0 +1,21 @@
1#define MAXPACK 50
2#define MAXBUFFERMS 100
3#define MAXSTRIDE 3
4
5#define SSS (((MAXPACK*MAXBUFFERMS*MAXSTRIDE + 4096) / 4096) * 4096)
6struct snd_usX2Y_hwdep_pcm_shm {
7 char playback[SSS];
8 char capture0x8[SSS];
9 char capture0xA[SSS];
10 volatile int playback_iso_head;
11 int playback_iso_start;
12 struct {
13 int frame,
14 offset,
15 length;
16 } captured_iso[128];
17 volatile int captured_iso_head;
18 volatile unsigned captured_iso_frames;
19 int capture_iso_start;
20};
21typedef struct snd_usX2Y_hwdep_pcm_shm snd_usX2Y_hwdep_pcm_shm_t;