Linux v2.6.13
[linux-2.6/next.git] / drivers / usb / class / audio.c
blobf8f21567cc22489c505d104851c80ab03fe8e47f
1 /*****************************************************************************/
3 /*
4 * audio.c -- USB Audio Class driver
6 * Copyright (C) 1999, 2000, 2001, 2003, 2004
7 * Alan Cox (alan@lxorguk.ukuu.org.uk)
8 * Thomas Sailer (sailer@ife.ee.ethz.ch)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * Debugging:
16 * Use the 'lsusb' utility to dump the descriptors.
18 * 1999-09-07: Alan Cox
19 * Parsing Audio descriptor patch
20 * 1999-09-08: Thomas Sailer
21 * Added OSS compatible data io functions; both parts of the
22 * driver remain to be glued together
23 * 1999-09-10: Thomas Sailer
24 * Beautified the driver. Added sample format conversions.
25 * Still not properly glued with the parsing code.
26 * The parsing code seems to have its problems btw,
27 * Since it parses all available configs but doesn't
28 * store which iface/altsetting belongs to which config.
29 * 1999-09-20: Thomas Sailer
30 * Threw out Alan's parsing code and implemented my own one.
31 * You cannot reasonnably linearly parse audio descriptors,
32 * especially the AudioClass descriptors have to be considered
33 * pointer lists. Mixer parsing untested, due to lack of device.
34 * First stab at synch pipe implementation, the Dallas USB DAC
35 * wants to use an Asynch out pipe. usb_audio_state now basically
36 * only contains lists of mixer and wave devices. We can therefore
37 * now have multiple mixer/wave devices per USB device.
38 * 1999-10-28: Thomas Sailer
39 * Converted to URB API. Fixed a taskstate/wakeup semantics mistake
40 * that made the driver consume all available CPU cycles.
41 * Now runs stable on UHCI-Acher/Fliegl/Sailer.
42 * 1999-10-31: Thomas Sailer
43 * Audio can now be unloaded if it is not in use by any mixer
44 * or dsp client (formerly you had to disconnect the audio devices
45 * from the USB port)
46 * Finally, about three months after ordering, my "Maxxtro SPK222"
47 * speakers arrived, isn't disdata a great mail order company 8-)
48 * Parse class specific endpoint descriptor of the audiostreaming
49 * interfaces and take the endpoint attributes from there.
50 * Unbelievably, the Philips USB DAC has a sampling rate range
51 * of over a decade, yet does not support the sampling rate control!
52 * No wonder it sounds so bad, has very audible sampling rate
53 * conversion distortion. Don't try to listen to it using
54 * decent headphones!
55 * "Let's make things better" -> but please Philips start with your
56 * own stuff!!!!
57 * 1999-11-02: Thomas Sailer
58 * It takes the Philips boxes several seconds to acquire synchronisation
59 * that means they won't play short sounds. Should probably maintain
60 * the ISO datastream even if there's nothing to play.
61 * Fix counting the total_bytes counter, RealPlayer G2 depends on it.
62 * 1999-12-20: Thomas Sailer
63 * Fix bad bug in conversion to per interface probing.
64 * disconnect was called multiple times for the audio device,
65 * leading to a premature freeing of the audio structures
66 * 2000-05-13: Thomas Sailer
67 * I don't remember who changed the find_format routine,
68 * but the change was completely broken for the Dallas
69 * chip. Anyway taking sampling rate into account in find_format
70 * is bad and should not be done unless there are devices with
71 * completely broken audio descriptors. Unless someone shows
72 * me such a descriptor, I will not allow find_format to
73 * take the sampling rate into account.
74 * Also, the former find_format made:
75 * - mpg123 play mono instead of stereo
76 * - sox completely fail for wav's with sample rates < 44.1kHz
77 * for the Dallas chip.
78 * Also fix a rather long standing problem with applications that
79 * use "small" writes producing no sound at all.
80 * 2000-05-15: Thomas Sailer
81 * My fears came true, the Philips camera indeed has pretty stupid
82 * audio descriptors.
83 * 2000-05-17: Thomas Sailer
84 * Nemsoft spotted my stupid last minute change, thanks
85 * 2000-05-19: Thomas Sailer
86 * Fixed FEATURE_UNIT thinkos found thanks to the KC Technology
87 * Xtend device. Basically the driver treated FEATURE_UNIT's sourced
88 * by mono terminals as stereo.
89 * 2000-05-20: Thomas Sailer
90 * SELECTOR support (and thus selecting record channels from the mixer).
91 * Somewhat peculiar due to OSS interface limitations. Only works
92 * for channels where a "slider" is already in front of it (i.e.
93 * a MIXER unit or a FEATURE unit with volume capability).
94 * 2000-11-26: Thomas Sailer
95 * Workaround for Dallas DS4201. The DS4201 uses PCM8 as format tag for
96 * its 8 bit modes, but expects signed data (and should therefore have used PCM).
97 * 2001-03-10: Thomas Sailer
98 * provide abs function, prevent picking up a bogus kernel macro
99 * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au>
100 * 2001-06-16: Bryce Nesbitt <bryce@obviously.com>
101 * Fix SNDCTL_DSP_STEREO API violation
102 * 2003-04-08: Oliver Neukum (oliver@neukum.name):
103 * Setting a configuration is done by usbcore and must not be overridden
104 * 2004-02-27: Workaround for broken synch descriptors
105 * 2004-03-07: Alan Stern <stern@rowland.harvard.edu>
106 * Add usb_ifnum_to_if() and usb_altnum_to_altsetting() support.
107 * Use the in-memory descriptors instead of reading them from the device.
112 * Strategy:
114 * Alan Cox and Thomas Sailer are starting to dig at opposite ends and
115 * are hoping to meet in the middle, just like tunnel diggers :)
116 * Alan tackles the descriptor parsing, Thomas the actual data IO and the
117 * OSS compatible interface.
119 * Data IO implementation issues
121 * A mmap'able ring buffer per direction is implemented, because
122 * almost every OSS app expects it. It is however impractical to
123 * transmit/receive USB data directly into and out of the ring buffer,
124 * due to alignment and synchronisation issues. Instead, the ring buffer
125 * feeds a constant time delay line that handles the USB issues.
127 * Now we first try to find an alternate setting that exactly matches
128 * the sample format requested by the user. If we find one, we do not
129 * need to perform any sample rate conversions. If there is no matching
130 * altsetting, we choose the closest one and perform sample format
131 * conversions. We never do sample rate conversion; these are too
132 * expensive to be performed in the kernel.
134 * Current status: no known HCD-specific issues.
136 * Generally: Due to the brokenness of the Audio Class spec
137 * it seems generally impossible to write a generic Audio Class driver,
138 * so a reasonable driver should implement the features that are actually
139 * used.
141 * Parsing implementation issues
143 * One cannot reasonably parse the AudioClass descriptors linearly.
144 * Therefore the current implementation features routines to look
145 * for a specific descriptor in the descriptor list.
147 * How does the parsing work? First, all interfaces are searched
148 * for an AudioControl class interface. If found, the config descriptor
149 * that belongs to the current configuration is searched and
150 * the HEADER descriptor is found. It contains a list of
151 * all AudioStreaming and MIDIStreaming devices. This list is then walked,
152 * and all AudioStreaming interfaces are classified into input and output
153 * interfaces (according to the endpoint0 direction in altsetting1) (MIDIStreaming
154 * is currently not supported). The input & output list is then used
155 * to group inputs and outputs together and issued pairwise to the
156 * AudioStreaming class parser. Finally, all OUTPUT_TERMINAL descriptors
157 * are walked and issued to the mixer construction routine.
159 * The AudioStreaming parser simply enumerates all altsettings belonging
160 * to the specified interface. It looks for AS_GENERAL and FORMAT_TYPE
161 * class specific descriptors to extract the sample format/sample rate
162 * data. Only sample format types PCM and PCM8 are supported right now, and
163 * only FORMAT_TYPE_I is handled. The isochronous data endpoint needs to
164 * be the first endpoint of the interface, and the optional synchronisation
165 * isochronous endpoint the second one.
167 * Mixer construction works as follows: The various TERMINAL and UNIT
168 * descriptors span a tree from the root (OUTPUT_TERMINAL) through the
169 * intermediate nodes (UNITs) to the leaves (INPUT_TERMINAL). We walk
170 * that tree in a depth first manner. FEATURE_UNITs may contribute volume,
171 * bass and treble sliders to the mixer, MIXER_UNITs volume sliders.
172 * The terminal type encoded in the INPUT_TERMINALs feeds a heuristic
173 * to determine "meaningful" OSS slider numbers, however we will see
174 * how well this works in practice. Other features are not used at the
175 * moment, they seem less often used. Also, it seems difficult at least
176 * to construct recording source switches from SELECTOR_UNITs, but
177 * since there are not many USB ADC's available, we leave that for later.
180 /*****************************************************************************/
182 #include <linux/kernel.h>
183 #include <linux/slab.h>
184 #include <linux/string.h>
185 #include <linux/timer.h>
186 #include <linux/sched.h>
187 #include <linux/smp_lock.h>
188 #include <linux/module.h>
189 #include <linux/sound.h>
190 #include <linux/soundcard.h>
191 #include <linux/list.h>
192 #include <linux/vmalloc.h>
193 #include <linux/init.h>
194 #include <linux/poll.h>
195 #include <linux/bitops.h>
196 #include <asm/uaccess.h>
197 #include <asm/io.h>
198 #include <linux/usb.h>
200 #include "audio.h"
203 * Version Information
205 #define DRIVER_VERSION "v1.0.0"
206 #define DRIVER_AUTHOR "Alan Cox <alan@lxorguk.ukuu.org.uk>, Thomas Sailer (sailer@ife.ee.ethz.ch)"
207 #define DRIVER_DESC "USB Audio Class driver"
209 #define AUDIO_DEBUG 1
211 #define SND_DEV_DSP16 5
213 #define dprintk(x)
215 /* --------------------------------------------------------------------- */
218 * Linked list of all audio devices...
220 static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs);
221 static DECLARE_MUTEX(open_sem);
224 * wait queue for processes wanting to open an USB audio device
226 static DECLARE_WAIT_QUEUE_HEAD(open_wait);
229 #define MAXFORMATS MAX_ALT
230 #define DMABUFSHIFT 17 /* 128k worth of DMA buffer */
231 #define NRSGBUF (1U<<(DMABUFSHIFT-PAGE_SHIFT))
234 * This influences:
235 * - Latency
236 * - Interrupt rate
237 * - Synchronisation behaviour
238 * Don't touch this if you don't understand all of the above.
240 #define DESCFRAMES 5
241 #define SYNCFRAMES DESCFRAMES
243 #define MIXFLG_STEREOIN 1
244 #define MIXFLG_STEREOOUT 2
246 struct mixerchannel {
247 __u16 value;
248 __u16 osschannel; /* number of the OSS channel */
249 __s16 minval, maxval;
250 __u16 slctunitid;
251 __u8 unitid;
252 __u8 selector;
253 __u8 chnum;
254 __u8 flags;
257 struct audioformat {
258 unsigned int format;
259 unsigned int sratelo;
260 unsigned int sratehi;
261 unsigned char altsetting;
262 unsigned char attributes;
265 struct dmabuf {
266 /* buffer data format */
267 unsigned int format;
268 unsigned int srate;
269 /* physical buffer */
270 unsigned char *sgbuf[NRSGBUF];
271 unsigned bufsize;
272 unsigned numfrag;
273 unsigned fragshift;
274 unsigned wrptr, rdptr;
275 unsigned total_bytes;
276 int count;
277 unsigned error; /* over/underrun */
278 wait_queue_head_t wait;
279 /* redundant, but makes calculations easier */
280 unsigned fragsize;
281 unsigned dmasize;
282 /* OSS stuff */
283 unsigned mapped:1;
284 unsigned ready:1;
285 unsigned ossfragshift;
286 int ossmaxfrags;
287 unsigned subdivision;
290 struct usb_audio_state;
292 #define FLG_URB0RUNNING 1
293 #define FLG_URB1RUNNING 2
294 #define FLG_SYNC0RUNNING 4
295 #define FLG_SYNC1RUNNING 8
296 #define FLG_RUNNING 16
297 #define FLG_CONNECTED 32
299 struct my_data_urb {
300 struct urb *urb;
303 struct my_sync_urb {
304 struct urb *urb;
308 struct usb_audiodev {
309 struct list_head list;
310 struct usb_audio_state *state;
312 /* soundcore stuff */
313 int dev_audio;
315 /* wave stuff */
316 mode_t open_mode;
317 spinlock_t lock; /* DMA buffer access spinlock */
319 struct usbin {
320 int interface; /* Interface number, -1 means not used */
321 unsigned int format; /* USB data format */
322 unsigned int datapipe; /* the data input pipe */
323 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but adaptive IN mode */
324 unsigned int syncinterval; /* P for adaptive IN mode, 0 otherwise */
325 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
326 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
327 unsigned int phase; /* phase accumulator */
328 unsigned int flags; /* see FLG_ defines */
330 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
331 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
333 struct dmabuf dma;
334 } usbin;
336 struct usbout {
337 int interface; /* Interface number, -1 means not used */
338 unsigned int format; /* USB data format */
339 unsigned int datapipe; /* the data input pipe */
340 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but asynchronous OUT mode */
341 unsigned int syncinterval; /* P for asynchronous OUT mode, 0 otherwise */
342 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
343 unsigned int freqm; /* momentary sampling rate in USB format, i.e. fs/1000 in Q10.14 */
344 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
345 unsigned int phase; /* phase accumulator */
346 unsigned int flags; /* see FLG_ defines */
348 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
349 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
351 struct dmabuf dma;
352 } usbout;
355 unsigned int numfmtin, numfmtout;
356 struct audioformat fmtin[MAXFORMATS];
357 struct audioformat fmtout[MAXFORMATS];
360 struct usb_mixerdev {
361 struct list_head list;
362 struct usb_audio_state *state;
364 /* soundcore stuff */
365 int dev_mixer;
367 unsigned char iface; /* interface number of the AudioControl interface */
369 /* USB format descriptions */
370 unsigned int numch, modcnt;
372 /* mixch is last and gets allocated dynamically */
373 struct mixerchannel ch[0];
376 struct usb_audio_state {
377 struct list_head audiodev;
379 /* USB device */
380 struct usb_device *usbdev;
382 struct list_head audiolist;
383 struct list_head mixerlist;
385 unsigned count; /* usage counter; NOTE: the usb stack is also considered a user */
388 /* private audio format extensions */
389 #define AFMT_STEREO 0x80000000
390 #define AFMT_ISSTEREO(x) ((x) & AFMT_STEREO)
391 #define AFMT_IS16BIT(x) ((x) & (AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE))
392 #define AFMT_ISUNSIGNED(x) ((x) & (AFMT_U8|AFMT_U16_LE|AFMT_U16_BE))
393 #define AFMT_BYTESSHIFT(x) ((AFMT_ISSTEREO(x) ? 1 : 0) + (AFMT_IS16BIT(x) ? 1 : 0))
394 #define AFMT_BYTES(x) (1<<AFMT_BYTESSHFIT(x))
396 /* --------------------------------------------------------------------- */
398 static inline unsigned ld2(unsigned int x)
400 unsigned r = 0;
402 if (x >= 0x10000) {
403 x >>= 16;
404 r += 16;
406 if (x >= 0x100) {
407 x >>= 8;
408 r += 8;
410 if (x >= 0x10) {
411 x >>= 4;
412 r += 4;
414 if (x >= 4) {
415 x >>= 2;
416 r += 2;
418 if (x >= 2)
419 r++;
420 return r;
423 /* --------------------------------------------------------------------- */
426 * OSS compatible ring buffer management. The ring buffer may be mmap'ed into
427 * an application address space.
429 * I first used the rvmalloc stuff copied from bttv. Alan Cox did not like it, so
430 * we now use an array of pointers to a single page each. This saves us the
431 * kernel page table manipulations, but we have to do a page table alike mechanism
432 * (though only one indirection) in software.
435 static void dmabuf_release(struct dmabuf *db)
437 unsigned int nr;
438 void *p;
440 for(nr = 0; nr < NRSGBUF; nr++) {
441 if (!(p = db->sgbuf[nr]))
442 continue;
443 ClearPageReserved(virt_to_page(p));
444 free_page((unsigned long)p);
445 db->sgbuf[nr] = NULL;
447 db->mapped = db->ready = 0;
450 static int dmabuf_init(struct dmabuf *db)
452 unsigned int nr, bytepersec, bufs;
453 void *p;
455 /* initialize some fields */
456 db->rdptr = db->wrptr = db->total_bytes = db->count = db->error = 0;
457 /* calculate required buffer size */
458 bytepersec = db->srate << AFMT_BYTESSHIFT(db->format);
459 bufs = 1U << DMABUFSHIFT;
460 if (db->ossfragshift) {
461 if ((1000 << db->ossfragshift) < bytepersec)
462 db->fragshift = ld2(bytepersec/1000);
463 else
464 db->fragshift = db->ossfragshift;
465 } else {
466 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
467 if (db->fragshift < 3)
468 db->fragshift = 3;
470 db->numfrag = bufs >> db->fragshift;
471 while (db->numfrag < 4 && db->fragshift > 3) {
472 db->fragshift--;
473 db->numfrag = bufs >> db->fragshift;
475 db->fragsize = 1 << db->fragshift;
476 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
477 db->numfrag = db->ossmaxfrags;
478 db->dmasize = db->numfrag << db->fragshift;
479 for(nr = 0; nr < NRSGBUF; nr++) {
480 if (!db->sgbuf[nr]) {
481 p = (void *)get_zeroed_page(GFP_KERNEL);
482 if (!p)
483 return -ENOMEM;
484 db->sgbuf[nr] = p;
485 SetPageReserved(virt_to_page(p));
487 memset(db->sgbuf[nr], AFMT_ISUNSIGNED(db->format) ? 0x80 : 0, PAGE_SIZE);
488 if ((nr << PAGE_SHIFT) >= db->dmasize)
489 break;
491 db->bufsize = nr << PAGE_SHIFT;
492 db->ready = 1;
493 dprintk((KERN_DEBUG "usbaudio: dmabuf_init bytepersec %d bufs %d ossfragshift %d ossmaxfrags %d "
494 "fragshift %d fragsize %d numfrag %d dmasize %d bufsize %d fmt 0x%x srate %d\n",
495 bytepersec, bufs, db->ossfragshift, db->ossmaxfrags, db->fragshift, db->fragsize,
496 db->numfrag, db->dmasize, db->bufsize, db->format, db->srate));
497 return 0;
500 static int dmabuf_mmap(struct vm_area_struct *vma, struct dmabuf *db, unsigned long start, unsigned long size, pgprot_t prot)
502 unsigned int nr;
504 if (!db->ready || db->mapped || (start | size) & (PAGE_SIZE-1) || size > db->bufsize)
505 return -EINVAL;
506 size >>= PAGE_SHIFT;
507 for(nr = 0; nr < size; nr++)
508 if (!db->sgbuf[nr])
509 return -EINVAL;
510 db->mapped = 1;
511 for(nr = 0; nr < size; nr++) {
512 unsigned long pfn;
514 pfn = virt_to_phys(db->sgbuf[nr]) >> PAGE_SHIFT;
515 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, prot))
516 return -EAGAIN;
517 start += PAGE_SIZE;
519 return 0;
522 static void dmabuf_copyin(struct dmabuf *db, const void *buffer, unsigned int size)
524 unsigned int pgrem, rem;
526 db->total_bytes += size;
527 for (;;) {
528 if (size <= 0)
529 return;
530 pgrem = ((~db->wrptr) & (PAGE_SIZE-1)) + 1;
531 if (pgrem > size)
532 pgrem = size;
533 rem = db->dmasize - db->wrptr;
534 if (pgrem > rem)
535 pgrem = rem;
536 memcpy((db->sgbuf[db->wrptr >> PAGE_SHIFT]) + (db->wrptr & (PAGE_SIZE-1)), buffer, pgrem);
537 size -= pgrem;
538 buffer += pgrem;
539 db->wrptr += pgrem;
540 if (db->wrptr >= db->dmasize)
541 db->wrptr = 0;
545 static void dmabuf_copyout(struct dmabuf *db, void *buffer, unsigned int size)
547 unsigned int pgrem, rem;
549 db->total_bytes += size;
550 for (;;) {
551 if (size <= 0)
552 return;
553 pgrem = ((~db->rdptr) & (PAGE_SIZE-1)) + 1;
554 if (pgrem > size)
555 pgrem = size;
556 rem = db->dmasize - db->rdptr;
557 if (pgrem > rem)
558 pgrem = rem;
559 memcpy(buffer, (db->sgbuf[db->rdptr >> PAGE_SHIFT]) + (db->rdptr & (PAGE_SIZE-1)), pgrem);
560 size -= pgrem;
561 buffer += pgrem;
562 db->rdptr += pgrem;
563 if (db->rdptr >= db->dmasize)
564 db->rdptr = 0;
568 static int dmabuf_copyin_user(struct dmabuf *db, unsigned int ptr, const void __user *buffer, unsigned int size)
570 unsigned int pgrem, rem;
572 if (!db->ready || db->mapped)
573 return -EINVAL;
574 for (;;) {
575 if (size <= 0)
576 return 0;
577 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
578 if (pgrem > size)
579 pgrem = size;
580 rem = db->dmasize - ptr;
581 if (pgrem > rem)
582 pgrem = rem;
583 if (copy_from_user((db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), buffer, pgrem))
584 return -EFAULT;
585 size -= pgrem;
586 buffer += pgrem;
587 ptr += pgrem;
588 if (ptr >= db->dmasize)
589 ptr = 0;
593 static int dmabuf_copyout_user(struct dmabuf *db, unsigned int ptr, void __user *buffer, unsigned int size)
595 unsigned int pgrem, rem;
597 if (!db->ready || db->mapped)
598 return -EINVAL;
599 for (;;) {
600 if (size <= 0)
601 return 0;
602 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
603 if (pgrem > size)
604 pgrem = size;
605 rem = db->dmasize - ptr;
606 if (pgrem > rem)
607 pgrem = rem;
608 if (copy_to_user(buffer, (db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), pgrem))
609 return -EFAULT;
610 size -= pgrem;
611 buffer += pgrem;
612 ptr += pgrem;
613 if (ptr >= db->dmasize)
614 ptr = 0;
618 /* --------------------------------------------------------------------- */
620 * USB I/O code. We do sample format conversion if necessary
623 static void usbin_stop(struct usb_audiodev *as)
625 struct usbin *u = &as->usbin;
626 unsigned long flags;
627 unsigned int i, notkilled = 1;
629 spin_lock_irqsave(&as->lock, flags);
630 u->flags &= ~FLG_RUNNING;
631 i = u->flags;
632 spin_unlock_irqrestore(&as->lock, flags);
633 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
634 set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
635 schedule_timeout(1);
636 spin_lock_irqsave(&as->lock, flags);
637 i = u->flags;
638 spin_unlock_irqrestore(&as->lock, flags);
639 if (notkilled && signal_pending(current)) {
640 if (i & FLG_URB0RUNNING)
641 usb_kill_urb(u->durb[0].urb);
642 if (i & FLG_URB1RUNNING)
643 usb_kill_urb(u->durb[1].urb);
644 if (i & FLG_SYNC0RUNNING)
645 usb_kill_urb(u->surb[0].urb);
646 if (i & FLG_SYNC1RUNNING)
647 usb_kill_urb(u->surb[1].urb);
648 notkilled = 0;
651 set_current_state(TASK_RUNNING);
652 kfree(u->durb[0].urb->transfer_buffer);
653 kfree(u->durb[1].urb->transfer_buffer);
654 kfree(u->surb[0].urb->transfer_buffer);
655 kfree(u->surb[1].urb->transfer_buffer);
656 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
657 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
660 static inline void usbin_release(struct usb_audiodev *as)
662 usbin_stop(as);
665 static void usbin_disc(struct usb_audiodev *as)
667 struct usbin *u = &as->usbin;
669 unsigned long flags;
671 spin_lock_irqsave(&as->lock, flags);
672 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
673 spin_unlock_irqrestore(&as->lock, flags);
674 usbin_stop(as);
677 static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt)
679 unsigned int cnt, i;
680 __s16 *sp, *sp2, s;
681 unsigned char *bp;
683 cnt = scnt;
684 if (AFMT_ISSTEREO(ifmt))
685 cnt <<= 1;
686 sp = ((__s16 *)tmp) + cnt;
687 switch (ifmt & ~AFMT_STEREO) {
688 case AFMT_U8:
689 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
690 bp--;
691 sp--;
692 *sp = (*bp ^ 0x80) << 8;
694 break;
696 case AFMT_S8:
697 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
698 bp--;
699 sp--;
700 *sp = *bp << 8;
702 break;
704 case AFMT_U16_LE:
705 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
706 bp -= 2;
707 sp--;
708 *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000;
710 break;
712 case AFMT_U16_BE:
713 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
714 bp -= 2;
715 sp--;
716 *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000;
718 break;
720 case AFMT_S16_LE:
721 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
722 bp -= 2;
723 sp--;
724 *sp = bp[0] | (bp[1] << 8);
726 break;
728 case AFMT_S16_BE:
729 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
730 bp -= 2;
731 sp--;
732 *sp = bp[1] | (bp[0] << 8);
734 break;
736 if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) {
737 /* expand from mono to stereo */
738 for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) {
739 sp--;
740 sp2 -= 2;
741 sp2[0] = sp2[1] = sp[0];
744 if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) {
745 /* contract from stereo to mono */
746 for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2)
747 sp[0] = (sp2[0] + sp2[1]) >> 1;
749 cnt = scnt;
750 if (AFMT_ISSTEREO(ofmt))
751 cnt <<= 1;
752 sp = ((__s16 *)tmp);
753 bp = ((unsigned char *)obuf);
754 switch (ofmt & ~AFMT_STEREO) {
755 case AFMT_U8:
756 for (i = 0; i < cnt; i++, sp++, bp++)
757 *bp = (*sp >> 8) ^ 0x80;
758 break;
760 case AFMT_S8:
761 for (i = 0; i < cnt; i++, sp++, bp++)
762 *bp = *sp >> 8;
763 break;
765 case AFMT_U16_LE:
766 for (i = 0; i < cnt; i++, sp++, bp += 2) {
767 s = *sp;
768 bp[0] = s;
769 bp[1] = (s >> 8) ^ 0x80;
771 break;
773 case AFMT_U16_BE:
774 for (i = 0; i < cnt; i++, sp++, bp += 2) {
775 s = *sp;
776 bp[1] = s;
777 bp[0] = (s >> 8) ^ 0x80;
779 break;
781 case AFMT_S16_LE:
782 for (i = 0; i < cnt; i++, sp++, bp += 2) {
783 s = *sp;
784 bp[0] = s;
785 bp[1] = s >> 8;
787 break;
789 case AFMT_S16_BE:
790 for (i = 0; i < cnt; i++, sp++, bp += 2) {
791 s = *sp;
792 bp[1] = s;
793 bp[0] = s >> 8;
795 break;
800 static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples)
802 union {
803 __s16 s[64];
804 unsigned char b[0];
805 } tmp;
806 unsigned int scnt, maxs, ufmtsh, dfmtsh;
808 ufmtsh = AFMT_BYTESSHIFT(u->format);
809 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
810 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
811 while (samples > 0) {
812 scnt = samples;
813 if (scnt > maxs)
814 scnt = maxs;
815 conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt);
816 dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh);
817 buffer += scnt << ufmtsh;
818 samples -= scnt;
822 static int usbin_prepare_desc(struct usbin *u, struct urb *urb)
824 unsigned int i, maxsize, offs;
826 maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
827 //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format);
828 for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) {
829 urb->iso_frame_desc[i].length = maxsize;
830 urb->iso_frame_desc[i].offset = offs;
832 urb->interval = 1;
833 return 0;
837 * return value: 0 if descriptor should be restarted, -1 otherwise
838 * convert sample format on the fly if necessary
840 static int usbin_retire_desc(struct usbin *u, struct urb *urb)
842 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree;
843 unsigned char *cp;
845 ufmtsh = AFMT_BYTESSHIFT(u->format);
846 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
847 for (i = 0; i < DESCFRAMES; i++) {
848 cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset;
849 if (urb->iso_frame_desc[i].status) {
850 dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
851 continue;
853 scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh;
854 if (!scnt)
855 continue;
856 cnt = scnt << dfmtsh;
857 if (!u->dma.mapped) {
858 dmafree = u->dma.dmasize - u->dma.count;
859 if (cnt > dmafree) {
860 scnt = dmafree >> dfmtsh;
861 cnt = scnt << dfmtsh;
862 err++;
865 u->dma.count += cnt;
866 if (u->format == u->dma.format) {
867 /* we do not need format conversion */
868 dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n"));
869 dmabuf_copyin(&u->dma, cp, cnt);
870 } else {
871 /* we need sampling format conversion */
872 dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format));
873 usbin_convert(u, cp, scnt);
876 if (err)
877 u->dma.error++;
878 if (u->dma.count >= (signed)u->dma.fragsize)
879 wake_up(&u->dma.wait);
880 return err ? -1 : 0;
883 static void usbin_completed(struct urb *urb, struct pt_regs *regs)
885 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
886 struct usbin *u = &as->usbin;
887 unsigned long flags;
888 unsigned int mask;
889 int suret = 0;
891 #if 0
892 printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
893 #endif
894 if (urb == u->durb[0].urb)
895 mask = FLG_URB0RUNNING;
896 else if (urb == u->durb[1].urb)
897 mask = FLG_URB1RUNNING;
898 else {
899 mask = 0;
900 printk(KERN_ERR "usbin_completed: panic: unknown URB\n");
902 urb->dev = as->state->usbdev;
903 spin_lock_irqsave(&as->lock, flags);
904 if (!usbin_retire_desc(u, urb) &&
905 u->flags & FLG_RUNNING &&
906 !usbin_prepare_desc(u, urb) &&
907 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
908 u->flags |= mask;
909 } else {
910 u->flags &= ~(mask | FLG_RUNNING);
911 wake_up(&u->dma.wait);
912 printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret);
914 spin_unlock_irqrestore(&as->lock, flags);
918 * we output sync data
920 static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb)
922 unsigned char *cp = urb->transfer_buffer;
923 unsigned int i, offs;
925 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) {
926 urb->iso_frame_desc[i].length = 3;
927 urb->iso_frame_desc[i].offset = offs;
928 cp[0] = u->freqn;
929 cp[1] = u->freqn >> 8;
930 cp[2] = u->freqn >> 16;
932 urb->interval = 1;
933 return 0;
937 * return value: 0 if descriptor should be restarted, -1 otherwise
939 static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb)
941 unsigned int i;
943 for (i = 0; i < SYNCFRAMES; i++)
944 if (urb->iso_frame_desc[0].status)
945 dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
946 return 0;
949 static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs)
951 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
952 struct usbin *u = &as->usbin;
953 unsigned long flags;
954 unsigned int mask;
955 int suret = 0;
957 #if 0
958 printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
959 #endif
960 if (urb == u->surb[0].urb)
961 mask = FLG_SYNC0RUNNING;
962 else if (urb == u->surb[1].urb)
963 mask = FLG_SYNC1RUNNING;
964 else {
965 mask = 0;
966 printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n");
968 urb->dev = as->state->usbdev;
969 spin_lock_irqsave(&as->lock, flags);
970 if (!usbin_sync_retire_desc(u, urb) &&
971 u->flags & FLG_RUNNING &&
972 !usbin_sync_prepare_desc(u, urb) &&
973 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
974 u->flags |= mask;
975 } else {
976 u->flags &= ~(mask | FLG_RUNNING);
977 wake_up(&u->dma.wait);
978 dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
980 spin_unlock_irqrestore(&as->lock, flags);
983 static int usbin_start(struct usb_audiodev *as)
985 struct usb_device *dev = as->state->usbdev;
986 struct usbin *u = &as->usbin;
987 struct urb *urb;
988 unsigned long flags;
989 unsigned int maxsze, bufsz;
991 #if 0
992 printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
993 dev->devnum, u->format, u->dma.format, u->dma.srate);
994 #endif
995 /* allocate USB storage if not already done */
996 spin_lock_irqsave(&as->lock, flags);
997 if (!(u->flags & FLG_CONNECTED)) {
998 spin_unlock_irqrestore(&as->lock, flags);
999 return -EIO;
1001 if (!(u->flags & FLG_RUNNING)) {
1002 spin_unlock_irqrestore(&as->lock, flags);
1003 u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1004 u->freqmax = u->freqn + (u->freqn >> 2);
1005 u->phase = 0;
1006 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1007 bufsz = DESCFRAMES * maxsze;
1008 kfree(u->durb[0].urb->transfer_buffer);
1009 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1010 u->durb[0].urb->transfer_buffer_length = bufsz;
1011 kfree(u->durb[1].urb->transfer_buffer);
1012 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1013 u->durb[1].urb->transfer_buffer_length = bufsz;
1014 if (u->syncpipe) {
1015 kfree(u->surb[0].urb->transfer_buffer);
1016 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1017 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1018 kfree(u->surb[1].urb->transfer_buffer);
1019 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1020 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1022 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1023 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1024 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1025 return 0;
1027 spin_lock_irqsave(&as->lock, flags);
1029 if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) {
1030 spin_unlock_irqrestore(&as->lock, flags);
1031 return 0;
1033 u->flags |= FLG_RUNNING;
1034 if (!(u->flags & FLG_URB0RUNNING)) {
1035 urb = u->durb[0].urb;
1036 urb->dev = dev;
1037 urb->pipe = u->datapipe;
1038 urb->transfer_flags = URB_ISO_ASAP;
1039 urb->number_of_packets = DESCFRAMES;
1040 urb->context = as;
1041 urb->complete = usbin_completed;
1042 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1043 u->flags |= FLG_URB0RUNNING;
1044 else
1045 u->flags &= ~FLG_RUNNING;
1047 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1048 urb = u->durb[1].urb;
1049 urb->dev = dev;
1050 urb->pipe = u->datapipe;
1051 urb->transfer_flags = URB_ISO_ASAP;
1052 urb->number_of_packets = DESCFRAMES;
1053 urb->context = as;
1054 urb->complete = usbin_completed;
1055 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1056 u->flags |= FLG_URB1RUNNING;
1057 else
1058 u->flags &= ~FLG_RUNNING;
1060 if (u->syncpipe) {
1061 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1062 urb = u->surb[0].urb;
1063 urb->dev = dev;
1064 urb->pipe = u->syncpipe;
1065 urb->transfer_flags = URB_ISO_ASAP;
1066 urb->number_of_packets = SYNCFRAMES;
1067 urb->context = as;
1068 urb->complete = usbin_sync_completed;
1069 /* stride: u->syncinterval */
1070 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1071 u->flags |= FLG_SYNC0RUNNING;
1072 else
1073 u->flags &= ~FLG_RUNNING;
1075 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1076 urb = u->surb[1].urb;
1077 urb->dev = dev;
1078 urb->pipe = u->syncpipe;
1079 urb->transfer_flags = URB_ISO_ASAP;
1080 urb->number_of_packets = SYNCFRAMES;
1081 urb->context = as;
1082 urb->complete = usbin_sync_completed;
1083 /* stride: u->syncinterval */
1084 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1085 u->flags |= FLG_SYNC1RUNNING;
1086 else
1087 u->flags &= ~FLG_RUNNING;
1090 spin_unlock_irqrestore(&as->lock, flags);
1091 return 0;
1094 static void usbout_stop(struct usb_audiodev *as)
1096 struct usbout *u = &as->usbout;
1097 unsigned long flags;
1098 unsigned int i, notkilled = 1;
1100 spin_lock_irqsave(&as->lock, flags);
1101 u->flags &= ~FLG_RUNNING;
1102 i = u->flags;
1103 spin_unlock_irqrestore(&as->lock, flags);
1104 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
1105 set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
1106 schedule_timeout(1);
1107 spin_lock_irqsave(&as->lock, flags);
1108 i = u->flags;
1109 spin_unlock_irqrestore(&as->lock, flags);
1110 if (notkilled && signal_pending(current)) {
1111 if (i & FLG_URB0RUNNING)
1112 usb_kill_urb(u->durb[0].urb);
1113 if (i & FLG_URB1RUNNING)
1114 usb_kill_urb(u->durb[1].urb);
1115 if (i & FLG_SYNC0RUNNING)
1116 usb_kill_urb(u->surb[0].urb);
1117 if (i & FLG_SYNC1RUNNING)
1118 usb_kill_urb(u->surb[1].urb);
1119 notkilled = 0;
1122 set_current_state(TASK_RUNNING);
1123 kfree(u->durb[0].urb->transfer_buffer);
1124 kfree(u->durb[1].urb->transfer_buffer);
1125 kfree(u->surb[0].urb->transfer_buffer);
1126 kfree(u->surb[1].urb->transfer_buffer);
1127 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
1128 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
1131 static inline void usbout_release(struct usb_audiodev *as)
1133 usbout_stop(as);
1136 static void usbout_disc(struct usb_audiodev *as)
1138 struct usbout *u = &as->usbout;
1139 unsigned long flags;
1141 spin_lock_irqsave(&as->lock, flags);
1142 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
1143 spin_unlock_irqrestore(&as->lock, flags);
1144 usbout_stop(as);
1147 static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples)
1149 union {
1150 __s16 s[64];
1151 unsigned char b[0];
1152 } tmp;
1153 unsigned int scnt, maxs, ufmtsh, dfmtsh;
1155 ufmtsh = AFMT_BYTESSHIFT(u->format);
1156 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1157 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
1158 while (samples > 0) {
1159 scnt = samples;
1160 if (scnt > maxs)
1161 scnt = maxs;
1162 dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh);
1163 conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt);
1164 buffer += scnt << ufmtsh;
1165 samples -= scnt;
1169 static int usbout_prepare_desc(struct usbout *u, struct urb *urb)
1171 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs;
1172 unsigned char *cp = urb->transfer_buffer;
1174 ufmtsh = AFMT_BYTESSHIFT(u->format);
1175 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1176 for (i = offs = 0; i < DESCFRAMES; i++) {
1177 urb->iso_frame_desc[i].offset = offs;
1178 u->phase = (u->phase & 0x3fff) + u->freqm;
1179 scnt = u->phase >> 14;
1180 if (!scnt) {
1181 urb->iso_frame_desc[i].length = 0;
1182 continue;
1184 cnt = scnt << dfmtsh;
1185 if (!u->dma.mapped) {
1186 if (cnt > u->dma.count) {
1187 scnt = u->dma.count >> dfmtsh;
1188 cnt = scnt << dfmtsh;
1189 err++;
1191 u->dma.count -= cnt;
1192 } else
1193 u->dma.count += cnt;
1194 if (u->format == u->dma.format) {
1195 /* we do not need format conversion */
1196 dmabuf_copyout(&u->dma, cp, cnt);
1197 } else {
1198 /* we need sampling format conversion */
1199 usbout_convert(u, cp, scnt);
1201 cnt = scnt << ufmtsh;
1202 urb->iso_frame_desc[i].length = cnt;
1203 offs += cnt;
1204 cp += cnt;
1206 urb->interval = 1;
1207 if (err)
1208 u->dma.error++;
1209 if (u->dma.mapped) {
1210 if (u->dma.count >= (signed)u->dma.fragsize)
1211 wake_up(&u->dma.wait);
1212 } else {
1213 if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize)
1214 wake_up(&u->dma.wait);
1216 return err ? -1 : 0;
1220 * return value: 0 if descriptor should be restarted, -1 otherwise
1222 static int usbout_retire_desc(struct usbout *u, struct urb *urb)
1224 unsigned int i;
1226 for (i = 0; i < DESCFRAMES; i++) {
1227 if (urb->iso_frame_desc[i].status) {
1228 dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1229 continue;
1232 return 0;
1235 static void usbout_completed(struct urb *urb, struct pt_regs *regs)
1237 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1238 struct usbout *u = &as->usbout;
1239 unsigned long flags;
1240 unsigned int mask;
1241 int suret = 0;
1243 #if 0
1244 printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1245 #endif
1246 if (urb == u->durb[0].urb)
1247 mask = FLG_URB0RUNNING;
1248 else if (urb == u->durb[1].urb)
1249 mask = FLG_URB1RUNNING;
1250 else {
1251 mask = 0;
1252 printk(KERN_ERR "usbout_completed: panic: unknown URB\n");
1254 urb->dev = as->state->usbdev;
1255 spin_lock_irqsave(&as->lock, flags);
1256 if (!usbout_retire_desc(u, urb) &&
1257 u->flags & FLG_RUNNING &&
1258 !usbout_prepare_desc(u, urb) &&
1259 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1260 u->flags |= mask;
1261 } else {
1262 u->flags &= ~(mask | FLG_RUNNING);
1263 wake_up(&u->dma.wait);
1264 dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1266 spin_unlock_irqrestore(&as->lock, flags);
1269 static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb)
1271 unsigned int i, offs;
1273 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) {
1274 urb->iso_frame_desc[i].length = 3;
1275 urb->iso_frame_desc[i].offset = offs;
1277 urb->interval = 1;
1278 return 0;
1282 * return value: 0 if descriptor should be restarted, -1 otherwise
1284 static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb)
1286 unsigned char *cp = urb->transfer_buffer;
1287 unsigned int f, i;
1289 for (i = 0; i < SYNCFRAMES; i++, cp += 3) {
1290 if (urb->iso_frame_desc[i].status) {
1291 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1292 continue;
1294 if (urb->iso_frame_desc[i].actual_length < 3) {
1295 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length));
1296 continue;
1298 f = cp[0] | (cp[1] << 8) | (cp[2] << 16);
1299 if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) {
1300 printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn);
1301 continue;
1303 u->freqm = f;
1305 return 0;
1308 static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs)
1310 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1311 struct usbout *u = &as->usbout;
1312 unsigned long flags;
1313 unsigned int mask;
1314 int suret = 0;
1316 #if 0
1317 printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1318 #endif
1319 if (urb == u->surb[0].urb)
1320 mask = FLG_SYNC0RUNNING;
1321 else if (urb == u->surb[1].urb)
1322 mask = FLG_SYNC1RUNNING;
1323 else {
1324 mask = 0;
1325 printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n");
1327 urb->dev = as->state->usbdev;
1328 spin_lock_irqsave(&as->lock, flags);
1329 if (!usbout_sync_retire_desc(u, urb) &&
1330 u->flags & FLG_RUNNING &&
1331 !usbout_sync_prepare_desc(u, urb) &&
1332 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1333 u->flags |= mask;
1334 } else {
1335 u->flags &= ~(mask | FLG_RUNNING);
1336 wake_up(&u->dma.wait);
1337 dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1339 spin_unlock_irqrestore(&as->lock, flags);
1342 static int usbout_start(struct usb_audiodev *as)
1344 struct usb_device *dev = as->state->usbdev;
1345 struct usbout *u = &as->usbout;
1346 struct urb *urb;
1347 unsigned long flags;
1348 unsigned int maxsze, bufsz;
1350 #if 0
1351 printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
1352 dev->devnum, u->format, u->dma.format, u->dma.srate);
1353 #endif
1354 /* allocate USB storage if not already done */
1355 spin_lock_irqsave(&as->lock, flags);
1356 if (!(u->flags & FLG_CONNECTED)) {
1357 spin_unlock_irqrestore(&as->lock, flags);
1358 return -EIO;
1360 if (!(u->flags & FLG_RUNNING)) {
1361 spin_unlock_irqrestore(&as->lock, flags);
1362 u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1363 u->freqmax = u->freqn + (u->freqn >> 2);
1364 u->phase = 0;
1365 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1366 bufsz = DESCFRAMES * maxsze;
1367 kfree(u->durb[0].urb->transfer_buffer);
1368 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1369 u->durb[0].urb->transfer_buffer_length = bufsz;
1370 kfree(u->durb[1].urb->transfer_buffer);
1371 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1372 u->durb[1].urb->transfer_buffer_length = bufsz;
1373 if (u->syncpipe) {
1374 kfree(u->surb[0].urb->transfer_buffer);
1375 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1376 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1377 kfree(u->surb[1].urb->transfer_buffer);
1378 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1379 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1381 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1382 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1383 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1384 return 0;
1386 spin_lock_irqsave(&as->lock, flags);
1388 if (u->dma.count <= 0 && !u->dma.mapped) {
1389 spin_unlock_irqrestore(&as->lock, flags);
1390 return 0;
1392 u->flags |= FLG_RUNNING;
1393 if (!(u->flags & FLG_URB0RUNNING)) {
1394 urb = u->durb[0].urb;
1395 urb->dev = dev;
1396 urb->pipe = u->datapipe;
1397 urb->transfer_flags = URB_ISO_ASAP;
1398 urb->number_of_packets = DESCFRAMES;
1399 urb->context = as;
1400 urb->complete = usbout_completed;
1401 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1402 u->flags |= FLG_URB0RUNNING;
1403 else
1404 u->flags &= ~FLG_RUNNING;
1406 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1407 urb = u->durb[1].urb;
1408 urb->dev = dev;
1409 urb->pipe = u->datapipe;
1410 urb->transfer_flags = URB_ISO_ASAP;
1411 urb->number_of_packets = DESCFRAMES;
1412 urb->context = as;
1413 urb->complete = usbout_completed;
1414 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1415 u->flags |= FLG_URB1RUNNING;
1416 else
1417 u->flags &= ~FLG_RUNNING;
1419 if (u->syncpipe) {
1420 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1421 urb = u->surb[0].urb;
1422 urb->dev = dev;
1423 urb->pipe = u->syncpipe;
1424 urb->transfer_flags = URB_ISO_ASAP;
1425 urb->number_of_packets = SYNCFRAMES;
1426 urb->context = as;
1427 urb->complete = usbout_sync_completed;
1428 /* stride: u->syncinterval */
1429 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1430 u->flags |= FLG_SYNC0RUNNING;
1431 else
1432 u->flags &= ~FLG_RUNNING;
1434 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1435 urb = u->surb[1].urb;
1436 urb->dev = dev;
1437 urb->pipe = u->syncpipe;
1438 urb->transfer_flags = URB_ISO_ASAP;
1439 urb->number_of_packets = SYNCFRAMES;
1440 urb->context = as;
1441 urb->complete = usbout_sync_completed;
1442 /* stride: u->syncinterval */
1443 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1444 u->flags |= FLG_SYNC1RUNNING;
1445 else
1446 u->flags &= ~FLG_RUNNING;
1449 spin_unlock_irqrestore(&as->lock, flags);
1450 return 0;
1453 /* --------------------------------------------------------------------- */
1455 static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate)
1457 unsigned int g = 0;
1459 if (srate < afp->sratelo)
1460 g += afp->sratelo - srate;
1461 if (srate > afp->sratehi)
1462 g += srate - afp->sratehi;
1463 if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt))
1464 g += 0x100000;
1465 if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt))
1466 g += 0x400000;
1467 if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt))
1468 g += 0x100000;
1469 if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt))
1470 g += 0x400000;
1471 return g;
1474 static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate)
1476 unsigned int i, g, gb = ~0;
1477 int j = -1; /* default to failure */
1479 /* find "best" format (according to format_goodness) */
1480 for (i = 0; i < nr; i++) {
1481 g = format_goodness(&afp[i], fmt, srate);
1482 if (g >= gb)
1483 continue;
1484 j = i;
1485 gb = g;
1487 return j;
1490 static int set_format_in(struct usb_audiodev *as)
1492 struct usb_device *dev = as->state->usbdev;
1493 struct usb_host_interface *alts;
1494 struct usb_interface *iface;
1495 struct usbin *u = &as->usbin;
1496 struct dmabuf *d = &u->dma;
1497 struct audioformat *fmt;
1498 unsigned int ep;
1499 unsigned char data[3];
1500 int fmtnr, ret;
1502 iface = usb_ifnum_to_if(dev, u->interface);
1503 if (!iface)
1504 return 0;
1506 fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate);
1507 if (fmtnr < 0) {
1508 printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n");
1509 return -1;
1512 fmt = as->fmtin + fmtnr;
1513 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1514 u->format = fmt->format;
1515 u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1516 u->syncpipe = u->syncinterval = 0;
1517 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) {
1518 if (alts->desc.bNumEndpoints < 2 ||
1519 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1520 alts->endpoint[1].desc.bSynchAddress != 0 ||
1521 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) {
1522 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in "
1523 "but has invalid synch pipe; treating as asynchronous in\n",
1524 dev->devnum, u->interface, fmt->altsetting);
1525 } else {
1526 u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1527 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1530 if (d->srate < fmt->sratelo)
1531 d->srate = fmt->sratelo;
1532 if (d->srate > fmt->sratehi)
1533 d->srate = fmt->sratehi;
1534 dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n",
1535 u->interface, fmt->altsetting));
1536 if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) {
1537 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1538 dev->devnum, u->interface, fmt->altsetting);
1539 return -1;
1541 if (fmt->sratelo == fmt->sratehi)
1542 return 0;
1543 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1544 /* if endpoint has pitch control, enable it */
1545 if (fmt->attributes & 0x02) {
1546 data[0] = 1;
1547 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1548 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1549 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1550 ret, dev->devnum, u->interface, ep, d->srate);
1551 return -1;
1554 /* if endpoint has sampling rate control, set it */
1555 if (fmt->attributes & 0x01) {
1556 data[0] = d->srate;
1557 data[1] = d->srate >> 8;
1558 data[2] = d->srate >> 16;
1559 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1560 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1561 printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1562 ret, dev->devnum, u->interface, ep, d->srate);
1563 return -1;
1565 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1566 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1567 printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n",
1568 ret, dev->devnum, u->interface, ep);
1569 return -1;
1571 dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n",
1572 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1573 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1575 dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1576 return 0;
1579 static int set_format_out(struct usb_audiodev *as)
1581 struct usb_device *dev = as->state->usbdev;
1582 struct usb_host_interface *alts;
1583 struct usb_interface *iface;
1584 struct usbout *u = &as->usbout;
1585 struct dmabuf *d = &u->dma;
1586 struct audioformat *fmt;
1587 unsigned int ep;
1588 unsigned char data[3];
1589 int fmtnr, ret;
1591 iface = usb_ifnum_to_if(dev, u->interface);
1592 if (!iface)
1593 return 0;
1595 fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate);
1596 if (fmtnr < 0) {
1597 printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n");
1598 return -1;
1601 fmt = as->fmtout + fmtnr;
1602 u->format = fmt->format;
1603 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1604 u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1605 u->syncpipe = u->syncinterval = 0;
1606 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) {
1607 #if 0
1608 printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n"
1609 KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n"
1610 KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints,
1611 alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress,
1612 alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress);
1613 #endif
1614 if (alts->desc.bNumEndpoints < 2 ||
1615 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1616 alts->endpoint[1].desc.bSynchAddress != 0 ||
1617 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) {
1618 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out "
1619 "but has invalid synch pipe; treating as adaptive out\n",
1620 dev->devnum, u->interface, fmt->altsetting);
1621 } else {
1622 u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1623 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1626 if (d->srate < fmt->sratelo)
1627 d->srate = fmt->sratelo;
1628 if (d->srate > fmt->sratehi)
1629 d->srate = fmt->sratehi;
1630 dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n",
1631 u->interface, fmt->altsetting));
1632 if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) {
1633 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1634 dev->devnum, u->interface, fmt->altsetting);
1635 return -1;
1637 if (fmt->sratelo == fmt->sratehi)
1638 return 0;
1639 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1640 /* if endpoint has pitch control, enable it */
1641 if (fmt->attributes & 0x02) {
1642 data[0] = 1;
1643 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1644 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1645 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1646 ret, dev->devnum, u->interface, ep, d->srate);
1647 return -1;
1650 /* if endpoint has sampling rate control, set it */
1651 if (fmt->attributes & 0x01) {
1652 data[0] = d->srate;
1653 data[1] = d->srate >> 8;
1654 data[2] = d->srate >> 16;
1655 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1656 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1657 printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1658 ret, dev->devnum, u->interface, ep, d->srate);
1659 return -1;
1661 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1662 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1663 printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n",
1664 ret, dev->devnum, u->interface, ep);
1665 return -1;
1667 dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n",
1668 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1669 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1671 dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1672 return 0;
1675 static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate)
1677 int ret1 = 0, ret2 = 0;
1679 if (!(fmode & (FMODE_READ|FMODE_WRITE)))
1680 return -EINVAL;
1681 if (fmode & FMODE_READ) {
1682 usbin_stop(s);
1683 s->usbin.dma.ready = 0;
1684 if (fmt == AFMT_QUERY)
1685 fmt = s->usbin.dma.format;
1686 else
1687 s->usbin.dma.format = fmt;
1688 if (!srate)
1689 srate = s->usbin.dma.srate;
1690 else
1691 s->usbin.dma.srate = srate;
1693 if (fmode & FMODE_WRITE) {
1694 usbout_stop(s);
1695 s->usbout.dma.ready = 0;
1696 if (fmt == AFMT_QUERY)
1697 fmt = s->usbout.dma.format;
1698 else
1699 s->usbout.dma.format = fmt;
1700 if (!srate)
1701 srate = s->usbout.dma.srate;
1702 else
1703 s->usbout.dma.srate = srate;
1705 if (fmode & FMODE_READ)
1706 ret1 = set_format_in(s);
1707 if (fmode & FMODE_WRITE)
1708 ret2 = set_format_out(s);
1709 return ret1 ? ret1 : ret2;
1712 /* --------------------------------------------------------------------- */
1714 static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value)
1716 struct usb_device *dev = ms->state->usbdev;
1717 unsigned char data[2];
1718 struct mixerchannel *ch;
1719 int v1, v2, v3;
1721 if (mixch >= ms->numch)
1722 return -1;
1723 ch = &ms->ch[mixch];
1724 v3 = ch->maxval - ch->minval;
1725 v1 = value & 0xff;
1726 v2 = (value >> 8) & 0xff;
1727 if (v1 > 100)
1728 v1 = 100;
1729 if (v2 > 100)
1730 v2 = 100;
1731 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1732 v2 = v1;
1733 ch->value = v1 | (v2 << 8);
1734 v1 = (v1 * v3) / 100 + ch->minval;
1735 v2 = (v2 * v3) / 100 + ch->minval;
1736 switch (ch->selector) {
1737 case 0: /* mixer unit request */
1738 data[0] = v1;
1739 data[1] = v1 >> 8;
1740 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1741 (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1742 goto err;
1743 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1744 return 0;
1745 data[0] = v2;
1746 data[1] = v2 >> 8;
1747 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1748 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
1749 ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1750 goto err;
1751 return 0;
1753 /* various feature unit controls */
1754 case VOLUME_CONTROL:
1755 data[0] = v1;
1756 data[1] = v1 >> 8;
1757 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1758 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1759 goto err;
1760 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1761 return 0;
1762 data[0] = v2;
1763 data[1] = v2 >> 8;
1764 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1765 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1766 goto err;
1767 return 0;
1769 case BASS_CONTROL:
1770 case MID_CONTROL:
1771 case TREBLE_CONTROL:
1772 data[0] = v1 >> 8;
1773 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1774 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1775 goto err;
1776 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1777 return 0;
1778 data[0] = v2 >> 8;
1779 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1780 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1781 goto err;
1782 return 0;
1784 default:
1785 return -1;
1787 return 0;
1789 err:
1790 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
1791 dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector);
1792 return -1;
1795 static int get_rec_src(struct usb_mixerdev *ms)
1797 struct usb_device *dev = ms->state->usbdev;
1798 unsigned int mask = 0, retmask = 0;
1799 unsigned int i, j;
1800 unsigned char buf;
1801 int err = 0;
1803 for (i = 0; i < ms->numch; i++) {
1804 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1805 continue;
1806 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1807 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1808 err = -EIO;
1809 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1810 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1811 continue;
1813 for (j = i; j < ms->numch; j++) {
1814 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1815 continue;
1816 mask |= 1 << j;
1817 if (buf == (ms->ch[j].slctunitid >> 8))
1818 retmask |= 1 << ms->ch[j].osschannel;
1821 if (err)
1822 return -EIO;
1823 return retmask;
1826 static int set_rec_src(struct usb_mixerdev *ms, int srcmask)
1828 struct usb_device *dev = ms->state->usbdev;
1829 unsigned int mask = 0, smask, bmask;
1830 unsigned int i, j;
1831 unsigned char buf;
1832 int err = 0;
1834 for (i = 0; i < ms->numch; i++) {
1835 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1836 continue;
1837 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1838 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1839 err = -EIO;
1840 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1841 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1842 continue;
1844 /* first generate smask */
1845 smask = bmask = 0;
1846 for (j = i; j < ms->numch; j++) {
1847 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1848 continue;
1849 smask |= 1 << ms->ch[j].osschannel;
1850 if (buf == (ms->ch[j].slctunitid >> 8))
1851 bmask |= 1 << ms->ch[j].osschannel;
1852 mask |= 1 << j;
1854 /* check for multiple set sources */
1855 j = hweight32(srcmask & smask);
1856 if (j == 0)
1857 continue;
1858 if (j > 1)
1859 srcmask &= ~bmask;
1860 for (j = i; j < ms->numch; j++) {
1861 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1862 continue;
1863 if (!(srcmask & (1 << ms->ch[j].osschannel)))
1864 continue;
1865 buf = ms->ch[j].slctunitid >> 8;
1866 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1867 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, 1000) < 0) {
1868 err = -EIO;
1869 printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n",
1870 dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff);
1871 continue;
1875 return err ? -EIO : 0;
1878 /* --------------------------------------------------------------------- */
1881 * should be called with open_sem hold, so that no new processes
1882 * look at the audio device to be destroyed
1885 static void release(struct usb_audio_state *s)
1887 struct usb_audiodev *as;
1888 struct usb_mixerdev *ms;
1890 s->count--;
1891 if (s->count) {
1892 up(&open_sem);
1893 return;
1895 up(&open_sem);
1896 wake_up(&open_wait);
1897 while (!list_empty(&s->audiolist)) {
1898 as = list_entry(s->audiolist.next, struct usb_audiodev, list);
1899 list_del(&as->list);
1900 usbin_release(as);
1901 usbout_release(as);
1902 dmabuf_release(&as->usbin.dma);
1903 dmabuf_release(&as->usbout.dma);
1904 usb_free_urb(as->usbin.durb[0].urb);
1905 usb_free_urb(as->usbin.durb[1].urb);
1906 usb_free_urb(as->usbin.surb[0].urb);
1907 usb_free_urb(as->usbin.surb[1].urb);
1908 usb_free_urb(as->usbout.durb[0].urb);
1909 usb_free_urb(as->usbout.durb[1].urb);
1910 usb_free_urb(as->usbout.surb[0].urb);
1911 usb_free_urb(as->usbout.surb[1].urb);
1912 kfree(as);
1914 while (!list_empty(&s->mixerlist)) {
1915 ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list);
1916 list_del(&ms->list);
1917 kfree(ms);
1919 kfree(s);
1922 static inline int prog_dmabuf_in(struct usb_audiodev *as)
1924 usbin_stop(as);
1925 return dmabuf_init(&as->usbin.dma);
1928 static inline int prog_dmabuf_out(struct usb_audiodev *as)
1930 usbout_stop(as);
1931 return dmabuf_init(&as->usbout.dma);
1934 /* --------------------------------------------------------------------- */
1936 static int usb_audio_open_mixdev(struct inode *inode, struct file *file)
1938 unsigned int minor = iminor(inode);
1939 struct usb_mixerdev *ms;
1940 struct usb_audio_state *s;
1942 down(&open_sem);
1943 list_for_each_entry(s, &audiodevs, audiodev) {
1944 list_for_each_entry(ms, &s->mixerlist, list) {
1945 if (ms->dev_mixer == minor)
1946 goto mixer_found;
1949 up(&open_sem);
1950 return -ENODEV;
1952 mixer_found:
1953 if (!s->usbdev) {
1954 up(&open_sem);
1955 return -EIO;
1957 file->private_data = ms;
1958 s->count++;
1960 up(&open_sem);
1961 return nonseekable_open(inode, file);
1964 static int usb_audio_release_mixdev(struct inode *inode, struct file *file)
1966 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1967 struct usb_audio_state *s;
1969 lock_kernel();
1970 s = ms->state;
1971 down(&open_sem);
1972 release(s);
1973 unlock_kernel();
1974 return 0;
1977 static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1979 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1980 int i, j, val;
1981 int __user *user_arg = (int __user *)arg;
1983 if (!ms->state->usbdev)
1984 return -ENODEV;
1986 if (cmd == SOUND_MIXER_INFO) {
1987 mixer_info info;
1989 memset(&info, 0, sizeof(info));
1990 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
1991 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
1992 info.modify_counter = ms->modcnt;
1993 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1994 return -EFAULT;
1995 return 0;
1997 if (cmd == SOUND_OLD_MIXER_INFO) {
1998 _old_mixer_info info;
2000 memset(&info, 0, sizeof(info));
2001 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
2002 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
2003 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
2004 return -EFAULT;
2005 return 0;
2007 if (cmd == OSS_GETVERSION)
2008 return put_user(SOUND_VERSION, user_arg);
2009 if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
2010 return -EINVAL;
2011 if (_IOC_DIR(cmd) == _IOC_READ) {
2012 switch (_IOC_NR(cmd)) {
2013 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2014 val = get_rec_src(ms);
2015 if (val < 0)
2016 return val;
2017 return put_user(val, user_arg);
2019 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
2020 for (val = i = 0; i < ms->numch; i++)
2021 val |= 1 << ms->ch[i].osschannel;
2022 return put_user(val, user_arg);
2024 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
2025 for (val = i = 0; i < ms->numch; i++)
2026 if (ms->ch[i].slctunitid)
2027 val |= 1 << ms->ch[i].osschannel;
2028 return put_user(val, user_arg);
2030 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
2031 for (val = i = 0; i < ms->numch; i++)
2032 if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))
2033 val |= 1 << ms->ch[i].osschannel;
2034 return put_user(val, user_arg);
2036 case SOUND_MIXER_CAPS:
2037 return put_user(SOUND_CAP_EXCL_INPUT, user_arg);
2039 default:
2040 i = _IOC_NR(cmd);
2041 if (i >= SOUND_MIXER_NRDEVICES)
2042 return -EINVAL;
2043 for (j = 0; j < ms->numch; j++) {
2044 if (ms->ch[j].osschannel == i) {
2045 return put_user(ms->ch[j].value, user_arg);
2048 return -EINVAL;
2051 if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
2052 return -EINVAL;
2053 ms->modcnt++;
2054 switch (_IOC_NR(cmd)) {
2055 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2056 if (get_user(val, user_arg))
2057 return -EFAULT;
2058 return set_rec_src(ms, val);
2060 default:
2061 i = _IOC_NR(cmd);
2062 if (i >= SOUND_MIXER_NRDEVICES)
2063 return -EINVAL;
2064 for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++);
2065 if (j >= ms->numch)
2066 return -EINVAL;
2067 if (get_user(val, user_arg))
2068 return -EFAULT;
2069 if (wrmixer(ms, j, val))
2070 return -EIO;
2071 return put_user(ms->ch[j].value, user_arg);
2075 static /*const*/ struct file_operations usb_mixer_fops = {
2076 .owner = THIS_MODULE,
2077 .llseek = no_llseek,
2078 .ioctl = usb_audio_ioctl_mixdev,
2079 .open = usb_audio_open_mixdev,
2080 .release = usb_audio_release_mixdev,
2083 /* --------------------------------------------------------------------- */
2085 static int drain_out(struct usb_audiodev *as, int nonblock)
2087 DECLARE_WAITQUEUE(wait, current);
2088 unsigned long flags;
2089 int count, tmo;
2091 if (as->usbout.dma.mapped || !as->usbout.dma.ready)
2092 return 0;
2093 usbout_start(as);
2094 add_wait_queue(&as->usbout.dma.wait, &wait);
2095 for (;;) {
2096 __set_current_state(TASK_INTERRUPTIBLE);
2097 spin_lock_irqsave(&as->lock, flags);
2098 count = as->usbout.dma.count;
2099 spin_unlock_irqrestore(&as->lock, flags);
2100 if (count <= 0)
2101 break;
2102 if (signal_pending(current))
2103 break;
2104 if (nonblock) {
2105 remove_wait_queue(&as->usbout.dma.wait, &wait);
2106 set_current_state(TASK_RUNNING);
2107 return -EBUSY;
2109 tmo = 3 * HZ * count / as->usbout.dma.srate;
2110 tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format);
2111 if (!schedule_timeout(tmo + 1)) {
2112 printk(KERN_DEBUG "usbaudio: dma timed out??\n");
2113 break;
2116 remove_wait_queue(&as->usbout.dma.wait, &wait);
2117 set_current_state(TASK_RUNNING);
2118 if (signal_pending(current))
2119 return -ERESTARTSYS;
2120 return 0;
2123 /* --------------------------------------------------------------------- */
2125 static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2127 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2128 DECLARE_WAITQUEUE(wait, current);
2129 ssize_t ret = 0;
2130 unsigned long flags;
2131 unsigned int ptr;
2132 int cnt, err;
2134 if (as->usbin.dma.mapped)
2135 return -ENXIO;
2136 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2137 return ret;
2138 if (!access_ok(VERIFY_WRITE, buffer, count))
2139 return -EFAULT;
2140 add_wait_queue(&as->usbin.dma.wait, &wait);
2141 while (count > 0) {
2142 spin_lock_irqsave(&as->lock, flags);
2143 ptr = as->usbin.dma.rdptr;
2144 cnt = as->usbin.dma.count;
2145 /* set task state early to avoid wakeup races */
2146 if (cnt <= 0)
2147 __set_current_state(TASK_INTERRUPTIBLE);
2148 spin_unlock_irqrestore(&as->lock, flags);
2149 if (cnt > count)
2150 cnt = count;
2151 if (cnt <= 0) {
2152 if (usbin_start(as)) {
2153 if (!ret)
2154 ret = -ENODEV;
2155 break;
2157 if (file->f_flags & O_NONBLOCK) {
2158 if (!ret)
2159 ret = -EAGAIN;
2160 break;
2162 schedule();
2163 if (signal_pending(current)) {
2164 if (!ret)
2165 ret = -ERESTARTSYS;
2166 break;
2168 continue;
2170 if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) {
2171 if (!ret)
2172 ret = err;
2173 break;
2175 ptr += cnt;
2176 if (ptr >= as->usbin.dma.dmasize)
2177 ptr -= as->usbin.dma.dmasize;
2178 spin_lock_irqsave(&as->lock, flags);
2179 as->usbin.dma.rdptr = ptr;
2180 as->usbin.dma.count -= cnt;
2181 spin_unlock_irqrestore(&as->lock, flags);
2182 count -= cnt;
2183 buffer += cnt;
2184 ret += cnt;
2186 __set_current_state(TASK_RUNNING);
2187 remove_wait_queue(&as->usbin.dma.wait, &wait);
2188 return ret;
2191 static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2193 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2194 DECLARE_WAITQUEUE(wait, current);
2195 ssize_t ret = 0;
2196 unsigned long flags;
2197 unsigned int ptr;
2198 unsigned int start_thr;
2199 int cnt, err;
2201 if (as->usbout.dma.mapped)
2202 return -ENXIO;
2203 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2204 return ret;
2205 if (!access_ok(VERIFY_READ, buffer, count))
2206 return -EFAULT;
2207 start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES));
2208 add_wait_queue(&as->usbout.dma.wait, &wait);
2209 while (count > 0) {
2210 #if 0
2211 printk(KERN_DEBUG "usb_audio_write: count %u dma: count %u rdptr %u wrptr %u dmasize %u fragsize %u flags 0x%02x taskst 0x%lx\n",
2212 count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize,
2213 as->usbout.flags, current->state);
2214 #endif
2215 spin_lock_irqsave(&as->lock, flags);
2216 if (as->usbout.dma.count < 0) {
2217 as->usbout.dma.count = 0;
2218 as->usbout.dma.rdptr = as->usbout.dma.wrptr;
2220 ptr = as->usbout.dma.wrptr;
2221 cnt = as->usbout.dma.dmasize - as->usbout.dma.count;
2222 /* set task state early to avoid wakeup races */
2223 if (cnt <= 0)
2224 __set_current_state(TASK_INTERRUPTIBLE);
2225 spin_unlock_irqrestore(&as->lock, flags);
2226 if (cnt > count)
2227 cnt = count;
2228 if (cnt <= 0) {
2229 if (usbout_start(as)) {
2230 if (!ret)
2231 ret = -ENODEV;
2232 break;
2234 if (file->f_flags & O_NONBLOCK) {
2235 if (!ret)
2236 ret = -EAGAIN;
2237 break;
2239 schedule();
2240 if (signal_pending(current)) {
2241 if (!ret)
2242 ret = -ERESTARTSYS;
2243 break;
2245 continue;
2247 if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) {
2248 if (!ret)
2249 ret = err;
2250 break;
2252 ptr += cnt;
2253 if (ptr >= as->usbout.dma.dmasize)
2254 ptr -= as->usbout.dma.dmasize;
2255 spin_lock_irqsave(&as->lock, flags);
2256 as->usbout.dma.wrptr = ptr;
2257 as->usbout.dma.count += cnt;
2258 spin_unlock_irqrestore(&as->lock, flags);
2259 count -= cnt;
2260 buffer += cnt;
2261 ret += cnt;
2262 if (as->usbout.dma.count >= start_thr && usbout_start(as)) {
2263 if (!ret)
2264 ret = -ENODEV;
2265 break;
2268 __set_current_state(TASK_RUNNING);
2269 remove_wait_queue(&as->usbout.dma.wait, &wait);
2270 return ret;
2273 /* Called without the kernel lock - fine */
2274 static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait)
2276 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2277 unsigned long flags;
2278 unsigned int mask = 0;
2280 if (file->f_mode & FMODE_WRITE) {
2281 if (!as->usbout.dma.ready)
2282 prog_dmabuf_out(as);
2283 poll_wait(file, &as->usbout.dma.wait, wait);
2285 if (file->f_mode & FMODE_READ) {
2286 if (!as->usbin.dma.ready)
2287 prog_dmabuf_in(as);
2288 poll_wait(file, &as->usbin.dma.wait, wait);
2290 spin_lock_irqsave(&as->lock, flags);
2291 if (file->f_mode & FMODE_READ) {
2292 if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize)
2293 mask |= POLLIN | POLLRDNORM;
2295 if (file->f_mode & FMODE_WRITE) {
2296 if (as->usbout.dma.mapped) {
2297 if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize)
2298 mask |= POLLOUT | POLLWRNORM;
2299 } else {
2300 if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize)
2301 mask |= POLLOUT | POLLWRNORM;
2304 spin_unlock_irqrestore(&as->lock, flags);
2305 return mask;
2308 static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma)
2310 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2311 struct dmabuf *db;
2312 int ret = -EINVAL;
2314 lock_kernel();
2315 if (vma->vm_flags & VM_WRITE) {
2316 if ((ret = prog_dmabuf_out(as)) != 0)
2317 goto out;
2318 db = &as->usbout.dma;
2319 } else if (vma->vm_flags & VM_READ) {
2320 if ((ret = prog_dmabuf_in(as)) != 0)
2321 goto out;
2322 db = &as->usbin.dma;
2323 } else
2324 goto out;
2326 ret = -EINVAL;
2327 if (vma->vm_pgoff != 0)
2328 goto out;
2330 ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot);
2331 out:
2332 unlock_kernel();
2333 return ret;
2336 static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2338 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2339 struct usb_audio_state *s = as->state;
2340 int __user *user_arg = (int __user *)arg;
2341 unsigned long flags;
2342 audio_buf_info abinfo;
2343 count_info cinfo;
2344 int val = 0;
2345 int val2, mapped, ret;
2347 if (!s->usbdev)
2348 return -EIO;
2349 mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) ||
2350 ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped);
2351 #if 0
2352 if (arg)
2353 get_user(val, (int *)arg);
2354 printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val)
2355 #endif
2356 switch (cmd) {
2357 case OSS_GETVERSION:
2358 return put_user(SOUND_VERSION, user_arg);
2360 case SNDCTL_DSP_SYNC:
2361 if (file->f_mode & FMODE_WRITE)
2362 return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/);
2363 return 0;
2365 case SNDCTL_DSP_SETDUPLEX:
2366 return 0;
2368 case SNDCTL_DSP_GETCAPS:
2369 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2370 DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg);
2372 case SNDCTL_DSP_RESET:
2373 if (file->f_mode & FMODE_WRITE) {
2374 usbout_stop(as);
2375 as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0;
2377 if (file->f_mode & FMODE_READ) {
2378 usbin_stop(as);
2379 as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0;
2381 return 0;
2383 case SNDCTL_DSP_SPEED:
2384 if (get_user(val, user_arg))
2385 return -EFAULT;
2386 if (val >= 0) {
2387 if (val < 4000)
2388 val = 4000;
2389 if (val > 100000)
2390 val = 100000;
2391 if (set_format(as, file->f_mode, AFMT_QUERY, val))
2392 return -EIO;
2394 return put_user((file->f_mode & FMODE_READ) ?
2395 as->usbin.dma.srate : as->usbout.dma.srate,
2396 user_arg);
2398 case SNDCTL_DSP_STEREO:
2399 if (get_user(val, user_arg))
2400 return -EFAULT;
2401 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2402 if (val)
2403 val2 |= AFMT_STEREO;
2404 else
2405 val2 &= ~AFMT_STEREO;
2406 if (set_format(as, file->f_mode, val2, 0))
2407 return -EIO;
2408 return 0;
2410 case SNDCTL_DSP_CHANNELS:
2411 if (get_user(val, user_arg))
2412 return -EFAULT;
2413 if (val != 0) {
2414 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2415 if (val == 1)
2416 val2 &= ~AFMT_STEREO;
2417 else
2418 val2 |= AFMT_STEREO;
2419 if (set_format(as, file->f_mode, val2, 0))
2420 return -EIO;
2422 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2423 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2425 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2426 return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2427 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg);
2429 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2430 if (get_user(val, user_arg))
2431 return -EFAULT;
2432 if (val != AFMT_QUERY) {
2433 if (hweight32(val) != 1)
2434 return -EINVAL;
2435 if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2436 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE)))
2437 return -EINVAL;
2438 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2439 val |= val2 & AFMT_STEREO;
2440 if (set_format(as, file->f_mode, val, 0))
2441 return -EIO;
2443 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2444 return put_user(val2 & ~AFMT_STEREO, user_arg);
2446 case SNDCTL_DSP_POST:
2447 return 0;
2449 case SNDCTL_DSP_GETTRIGGER:
2450 val = 0;
2451 if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING)
2452 val |= PCM_ENABLE_INPUT;
2453 if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING)
2454 val |= PCM_ENABLE_OUTPUT;
2455 return put_user(val, user_arg);
2457 case SNDCTL_DSP_SETTRIGGER:
2458 if (get_user(val, user_arg))
2459 return -EFAULT;
2460 if (file->f_mode & FMODE_READ) {
2461 if (val & PCM_ENABLE_INPUT) {
2462 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2463 return ret;
2464 if (usbin_start(as))
2465 return -ENODEV;
2466 } else
2467 usbin_stop(as);
2469 if (file->f_mode & FMODE_WRITE) {
2470 if (val & PCM_ENABLE_OUTPUT) {
2471 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2472 return ret;
2473 if (usbout_start(as))
2474 return -ENODEV;
2475 } else
2476 usbout_stop(as);
2478 return 0;
2480 case SNDCTL_DSP_GETOSPACE:
2481 if (!(file->f_mode & FMODE_WRITE))
2482 return -EINVAL;
2483 if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0)
2484 return val;
2485 spin_lock_irqsave(&as->lock, flags);
2486 abinfo.fragsize = as->usbout.dma.fragsize;
2487 abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count;
2488 abinfo.fragstotal = as->usbout.dma.numfrag;
2489 abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift;
2490 spin_unlock_irqrestore(&as->lock, flags);
2491 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2493 case SNDCTL_DSP_GETISPACE:
2494 if (!(file->f_mode & FMODE_READ))
2495 return -EINVAL;
2496 if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0)
2497 return val;
2498 spin_lock_irqsave(&as->lock, flags);
2499 abinfo.fragsize = as->usbin.dma.fragsize;
2500 abinfo.bytes = as->usbin.dma.count;
2501 abinfo.fragstotal = as->usbin.dma.numfrag;
2502 abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift;
2503 spin_unlock_irqrestore(&as->lock, flags);
2504 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2506 case SNDCTL_DSP_NONBLOCK:
2507 file->f_flags |= O_NONBLOCK;
2508 return 0;
2510 case SNDCTL_DSP_GETODELAY:
2511 if (!(file->f_mode & FMODE_WRITE))
2512 return -EINVAL;
2513 spin_lock_irqsave(&as->lock, flags);
2514 val = as->usbout.dma.count;
2515 spin_unlock_irqrestore(&as->lock, flags);
2516 return put_user(val, user_arg);
2518 case SNDCTL_DSP_GETIPTR:
2519 if (!(file->f_mode & FMODE_READ))
2520 return -EINVAL;
2521 spin_lock_irqsave(&as->lock, flags);
2522 cinfo.bytes = as->usbin.dma.total_bytes;
2523 cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift;
2524 cinfo.ptr = as->usbin.dma.wrptr;
2525 if (as->usbin.dma.mapped)
2526 as->usbin.dma.count &= as->usbin.dma.fragsize-1;
2527 spin_unlock_irqrestore(&as->lock, flags);
2528 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2529 return -EFAULT;
2530 return 0;
2532 case SNDCTL_DSP_GETOPTR:
2533 if (!(file->f_mode & FMODE_WRITE))
2534 return -EINVAL;
2535 spin_lock_irqsave(&as->lock, flags);
2536 cinfo.bytes = as->usbout.dma.total_bytes;
2537 cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift;
2538 cinfo.ptr = as->usbout.dma.rdptr;
2539 if (as->usbout.dma.mapped)
2540 as->usbout.dma.count &= as->usbout.dma.fragsize-1;
2541 spin_unlock_irqrestore(&as->lock, flags);
2542 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2543 return -EFAULT;
2544 return 0;
2546 case SNDCTL_DSP_GETBLKSIZE:
2547 if (file->f_mode & FMODE_WRITE) {
2548 if ((val = prog_dmabuf_out(as)))
2549 return val;
2550 return put_user(as->usbout.dma.fragsize, user_arg);
2552 if ((val = prog_dmabuf_in(as)))
2553 return val;
2554 return put_user(as->usbin.dma.fragsize, user_arg);
2556 case SNDCTL_DSP_SETFRAGMENT:
2557 if (get_user(val, user_arg))
2558 return -EFAULT;
2559 if (file->f_mode & FMODE_READ) {
2560 as->usbin.dma.ossfragshift = val & 0xffff;
2561 as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff;
2562 if (as->usbin.dma.ossfragshift < 4)
2563 as->usbin.dma.ossfragshift = 4;
2564 if (as->usbin.dma.ossfragshift > 15)
2565 as->usbin.dma.ossfragshift = 15;
2566 if (as->usbin.dma.ossmaxfrags < 4)
2567 as->usbin.dma.ossmaxfrags = 4;
2569 if (file->f_mode & FMODE_WRITE) {
2570 as->usbout.dma.ossfragshift = val & 0xffff;
2571 as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff;
2572 if (as->usbout.dma.ossfragshift < 4)
2573 as->usbout.dma.ossfragshift = 4;
2574 if (as->usbout.dma.ossfragshift > 15)
2575 as->usbout.dma.ossfragshift = 15;
2576 if (as->usbout.dma.ossmaxfrags < 4)
2577 as->usbout.dma.ossmaxfrags = 4;
2579 return 0;
2581 case SNDCTL_DSP_SUBDIVIDE:
2582 if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) ||
2583 (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision))
2584 return -EINVAL;
2585 if (get_user(val, user_arg))
2586 return -EFAULT;
2587 if (val != 1 && val != 2 && val != 4)
2588 return -EINVAL;
2589 if (file->f_mode & FMODE_READ)
2590 as->usbin.dma.subdivision = val;
2591 if (file->f_mode & FMODE_WRITE)
2592 as->usbout.dma.subdivision = val;
2593 return 0;
2595 case SOUND_PCM_READ_RATE:
2596 return put_user((file->f_mode & FMODE_READ) ?
2597 as->usbin.dma.srate : as->usbout.dma.srate,
2598 user_arg);
2600 case SOUND_PCM_READ_CHANNELS:
2601 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2602 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2604 case SOUND_PCM_READ_BITS:
2605 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2606 return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg);
2608 case SOUND_PCM_WRITE_FILTER:
2609 case SNDCTL_DSP_SETSYNCRO:
2610 case SOUND_PCM_READ_FILTER:
2611 return -EINVAL;
2613 dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n"));
2614 return -ENOIOCTLCMD;
2617 static int usb_audio_open(struct inode *inode, struct file *file)
2619 unsigned int minor = iminor(inode);
2620 DECLARE_WAITQUEUE(wait, current);
2621 struct usb_audiodev *as;
2622 struct usb_audio_state *s;
2624 for (;;) {
2625 down(&open_sem);
2626 list_for_each_entry(s, &audiodevs, audiodev) {
2627 list_for_each_entry(as, &s->audiolist, list) {
2628 if (!((as->dev_audio ^ minor) & ~0xf))
2629 goto device_found;
2632 up(&open_sem);
2633 return -ENODEV;
2635 device_found:
2636 if (!s->usbdev) {
2637 up(&open_sem);
2638 return -EIO;
2640 /* wait for device to become free */
2641 if (!(as->open_mode & file->f_mode))
2642 break;
2643 if (file->f_flags & O_NONBLOCK) {
2644 up(&open_sem);
2645 return -EBUSY;
2647 __set_current_state(TASK_INTERRUPTIBLE);
2648 add_wait_queue(&open_wait, &wait);
2649 up(&open_sem);
2650 schedule();
2651 __set_current_state(TASK_RUNNING);
2652 remove_wait_queue(&open_wait, &wait);
2653 if (signal_pending(current))
2654 return -ERESTARTSYS;
2656 if (file->f_mode & FMODE_READ)
2657 as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0;
2658 if (file->f_mode & FMODE_WRITE)
2659 as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0;
2660 if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) {
2661 up(&open_sem);
2662 return -EIO;
2664 file->private_data = as;
2665 as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2666 s->count++;
2667 up(&open_sem);
2668 return nonseekable_open(inode, file);
2671 static int usb_audio_release(struct inode *inode, struct file *file)
2673 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2674 struct usb_audio_state *s;
2675 struct usb_device *dev;
2677 lock_kernel();
2678 s = as->state;
2679 dev = s->usbdev;
2680 if (file->f_mode & FMODE_WRITE)
2681 drain_out(as, file->f_flags & O_NONBLOCK);
2682 down(&open_sem);
2683 if (file->f_mode & FMODE_WRITE) {
2684 usbout_stop(as);
2685 if (dev && as->usbout.interface >= 0)
2686 usb_set_interface(dev, as->usbout.interface, 0);
2687 dmabuf_release(&as->usbout.dma);
2688 usbout_release(as);
2690 if (file->f_mode & FMODE_READ) {
2691 usbin_stop(as);
2692 if (dev && as->usbin.interface >= 0)
2693 usb_set_interface(dev, as->usbin.interface, 0);
2694 dmabuf_release(&as->usbin.dma);
2695 usbin_release(as);
2697 as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
2698 release(s);
2699 wake_up(&open_wait);
2700 unlock_kernel();
2701 return 0;
2704 static /*const*/ struct file_operations usb_audio_fops = {
2705 .owner = THIS_MODULE,
2706 .llseek = no_llseek,
2707 .read = usb_audio_read,
2708 .write = usb_audio_write,
2709 .poll = usb_audio_poll,
2710 .ioctl = usb_audio_ioctl,
2711 .mmap = usb_audio_mmap,
2712 .open = usb_audio_open,
2713 .release = usb_audio_release,
2716 /* --------------------------------------------------------------------- */
2718 static int usb_audio_probe(struct usb_interface *iface,
2719 const struct usb_device_id *id);
2720 static void usb_audio_disconnect(struct usb_interface *iface);
2722 static struct usb_device_id usb_audio_ids [] = {
2723 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2724 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1},
2725 { } /* Terminating entry */
2728 MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2730 static struct usb_driver usb_audio_driver = {
2731 .owner = THIS_MODULE,
2732 .name = "audio",
2733 .probe = usb_audio_probe,
2734 .disconnect = usb_audio_disconnect,
2735 .id_table = usb_audio_ids,
2738 static void *find_descriptor(void *descstart, unsigned int desclen, void *after,
2739 u8 dtype, int iface, int altsetting)
2741 u8 *p, *end, *next;
2742 int ifc = -1, as = -1;
2744 p = descstart;
2745 end = p + desclen;
2746 for (; p < end;) {
2747 if (p[0] < 2)
2748 return NULL;
2749 next = p + p[0];
2750 if (next > end)
2751 return NULL;
2752 if (p[1] == USB_DT_INTERFACE) {
2753 /* minimum length of interface descriptor */
2754 if (p[0] < 9)
2755 return NULL;
2756 ifc = p[2];
2757 as = p[3];
2759 if (p[1] == dtype && (!after || (void *)p > after) &&
2760 (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) {
2761 return p;
2763 p = next;
2765 return NULL;
2768 static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting)
2770 unsigned char *p;
2772 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting);
2773 while (p) {
2774 if (p[0] >= 3 && p[2] == dsubtype)
2775 return p;
2776 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting);
2778 return NULL;
2781 static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface)
2783 unsigned char *p;
2785 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1);
2786 while (p) {
2787 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
2788 return p;
2789 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1);
2791 return NULL;
2794 static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout)
2796 struct usb_device *dev = s->usbdev;
2797 struct usb_audiodev *as;
2798 struct usb_host_interface *alts;
2799 struct usb_interface *iface;
2800 struct audioformat *fp;
2801 unsigned char *fmt, *csep;
2802 unsigned int i, j, k, format, idx;
2804 if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL)))
2805 return;
2806 memset(as, 0, sizeof(struct usb_audiodev));
2807 init_waitqueue_head(&as->usbin.dma.wait);
2808 init_waitqueue_head(&as->usbout.dma.wait);
2809 spin_lock_init(&as->lock);
2810 as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2811 as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2812 as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2813 as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2814 as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2815 as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2816 as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2817 as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2818 if ((!as->usbin.durb[0].urb) ||
2819 (!as->usbin.durb[1].urb) ||
2820 (!as->usbin.surb[0].urb) ||
2821 (!as->usbin.surb[1].urb) ||
2822 (!as->usbout.durb[0].urb) ||
2823 (!as->usbout.durb[1].urb) ||
2824 (!as->usbout.surb[0].urb) ||
2825 (!as->usbout.surb[1].urb)) {
2826 usb_free_urb(as->usbin.durb[0].urb);
2827 usb_free_urb(as->usbin.durb[1].urb);
2828 usb_free_urb(as->usbin.surb[0].urb);
2829 usb_free_urb(as->usbin.surb[1].urb);
2830 usb_free_urb(as->usbout.durb[0].urb);
2831 usb_free_urb(as->usbout.durb[1].urb);
2832 usb_free_urb(as->usbout.surb[0].urb);
2833 usb_free_urb(as->usbout.surb[1].urb);
2834 kfree(as);
2835 return;
2837 as->state = s;
2838 as->usbin.interface = asifin;
2839 as->usbout.interface = asifout;
2840 /* search for input formats */
2841 if (asifin >= 0) {
2842 as->usbin.flags = FLG_CONNECTED;
2843 iface = usb_ifnum_to_if(dev, asifin);
2844 for (idx = 0; idx < iface->num_altsetting; idx++) {
2845 alts = &iface->altsetting[idx];
2846 i = alts->desc.bAlternateSetting;
2847 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2848 continue;
2849 if (alts->desc.bNumEndpoints < 1) {
2850 if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */
2851 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2852 dev->devnum, asifin, i);
2854 continue;
2856 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2857 !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2858 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n",
2859 dev->devnum, asifin, i);
2860 continue;
2862 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i);
2863 if (!fmt) {
2864 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2865 dev->devnum, asifin, i);
2866 continue;
2868 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2869 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2870 dev->devnum, asifin, i);
2871 continue;
2873 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2874 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i);
2875 if (!fmt) {
2876 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2877 dev->devnum, asifin, i);
2878 continue;
2880 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2881 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2882 dev->devnum, asifin, i);
2883 continue;
2885 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2886 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2887 dev->devnum, asifin, i, fmt[4], fmt[5]);
2888 continue;
2890 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i);
2891 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2892 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2893 dev->devnum, asifin, i);
2894 continue;
2896 if (as->numfmtin >= MAXFORMATS)
2897 continue;
2898 fp = &as->fmtin[as->numfmtin++];
2899 if (fmt[5] == 2)
2900 format &= (AFMT_U16_LE | AFMT_S16_LE);
2901 else
2902 format &= (AFMT_U8 | AFMT_S8);
2903 if (fmt[4] == 2)
2904 format |= AFMT_STEREO;
2905 fp->format = format;
2906 fp->altsetting = i;
2907 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2908 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo);
2909 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
2910 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
2911 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k);
2912 if (k > fp->sratehi)
2913 fp->sratehi = k;
2914 if (k < fp->sratelo)
2915 fp->sratelo = k;
2917 fp->attributes = csep[3];
2918 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
2919 dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
2922 /* search for output formats */
2923 if (asifout >= 0) {
2924 as->usbout.flags = FLG_CONNECTED;
2925 iface = usb_ifnum_to_if(dev, asifout);
2926 for (idx = 0; idx < iface->num_altsetting; idx++) {
2927 alts = &iface->altsetting[idx];
2928 i = alts->desc.bAlternateSetting;
2929 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2930 continue;
2931 if (alts->desc.bNumEndpoints < 1) {
2932 /* altsetting 0 should never have iso EPs */
2933 if (i != 0)
2934 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2935 dev->devnum, asifout, i);
2936 continue;
2938 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2939 (alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2940 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n",
2941 dev->devnum, asifout, i);
2942 continue;
2944 /* See USB audio formats manual, section 2 */
2945 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i);
2946 if (!fmt) {
2947 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2948 dev->devnum, asifout, i);
2949 continue;
2951 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2952 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2953 dev->devnum, asifout, i);
2954 continue;
2956 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2957 /* Dallas DS4201 workaround */
2958 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2959 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2960 format = (AFMT_S16_LE | AFMT_S8);
2961 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i);
2962 if (!fmt) {
2963 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2964 dev->devnum, asifout, i);
2965 continue;
2967 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2968 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2969 dev->devnum, asifout, i);
2970 continue;
2972 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2973 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2974 dev->devnum, asifout, i, fmt[4], fmt[5]);
2975 continue;
2977 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i);
2978 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2979 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2980 dev->devnum, asifout, i);
2981 continue;
2983 if (as->numfmtout >= MAXFORMATS)
2984 continue;
2985 fp = &as->fmtout[as->numfmtout++];
2986 if (fmt[5] == 2)
2987 format &= (AFMT_U16_LE | AFMT_S16_LE);
2988 else
2989 format &= (AFMT_U8 | AFMT_S8);
2990 if (fmt[4] == 2)
2991 format |= AFMT_STEREO;
2992 fp->format = format;
2993 fp->altsetting = i;
2994 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2995 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo);
2996 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
2997 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
2998 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k);
2999 if (k > fp->sratehi)
3000 fp->sratehi = k;
3001 if (k < fp->sratelo)
3002 fp->sratelo = k;
3004 fp->attributes = csep[3];
3005 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
3006 dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
3009 if (as->numfmtin == 0 && as->numfmtout == 0) {
3010 usb_free_urb(as->usbin.durb[0].urb);
3011 usb_free_urb(as->usbin.durb[1].urb);
3012 usb_free_urb(as->usbin.surb[0].urb);
3013 usb_free_urb(as->usbin.surb[1].urb);
3014 usb_free_urb(as->usbout.durb[0].urb);
3015 usb_free_urb(as->usbout.durb[1].urb);
3016 usb_free_urb(as->usbout.surb[0].urb);
3017 usb_free_urb(as->usbout.surb[1].urb);
3018 kfree(as);
3019 return;
3021 if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) {
3022 printk(KERN_ERR "usbaudio: cannot register dsp\n");
3023 usb_free_urb(as->usbin.durb[0].urb);
3024 usb_free_urb(as->usbin.durb[1].urb);
3025 usb_free_urb(as->usbin.surb[0].urb);
3026 usb_free_urb(as->usbin.surb[1].urb);
3027 usb_free_urb(as->usbout.durb[0].urb);
3028 usb_free_urb(as->usbout.durb[1].urb);
3029 usb_free_urb(as->usbout.surb[0].urb);
3030 usb_free_urb(as->usbout.surb[1].urb);
3031 kfree(as);
3032 return;
3034 printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio);
3035 /* everything successful */
3036 list_add_tail(&as->list, &s->audiolist);
3039 struct consmixstate {
3040 struct usb_audio_state *s;
3041 unsigned char *buffer;
3042 unsigned int buflen;
3043 unsigned int ctrlif;
3044 struct mixerchannel mixch[SOUND_MIXER_NRDEVICES];
3045 unsigned int nrmixch;
3046 unsigned int mixchmask;
3047 unsigned long unitbitmap[32/sizeof(unsigned long)];
3048 /* return values */
3049 unsigned int nrchannels;
3050 unsigned int termtype;
3051 unsigned int chconfig;
3054 static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr)
3056 struct mixerchannel *c;
3058 if (nr >= SOUND_MIXER_NRDEVICES) {
3059 printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr);
3060 return NULL;
3062 if (!(state->mixchmask & (1 << nr))) {
3063 printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr);
3064 return NULL;
3066 c = &state->mixch[state->nrmixch++];
3067 c->osschannel = nr;
3068 state->mixchmask &= ~(1 << nr);
3069 return c;
3072 static unsigned int getvolchannel(struct consmixstate *state)
3074 unsigned int u;
3076 if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME))
3077 return SOUND_MIXER_VOLUME;
3078 if ((state->termtype & 0xff00) == 0x0100) {
3079 if (state->mixchmask & SOUND_MASK_PCM)
3080 return SOUND_MIXER_PCM;
3081 if (state->mixchmask & SOUND_MASK_ALTPCM)
3082 return SOUND_MIXER_ALTPCM;
3084 if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC))
3085 return SOUND_MIXER_MIC;
3086 if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER))
3087 return SOUND_MIXER_SPEAKER;
3088 if ((state->termtype & 0xff00) == 0x0500) {
3089 if (state->mixchmask & SOUND_MASK_PHONEIN)
3090 return SOUND_MIXER_PHONEIN;
3091 if (state->mixchmask & SOUND_MASK_PHONEOUT)
3092 return SOUND_MIXER_PHONEOUT;
3094 if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO))
3095 return SOUND_MIXER_RADIO;
3096 if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO))
3097 return SOUND_MIXER_VIDEO;
3098 u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 |
3099 SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3));
3100 return u-1;
3103 static void prepmixch(struct consmixstate *state)
3105 struct usb_device *dev = state->s->usbdev;
3106 struct mixerchannel *ch;
3107 unsigned char *buf;
3108 __s16 v1;
3109 unsigned int v2, v3;
3111 if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES)
3112 return;
3113 buf = kmalloc(sizeof(*buf) * 2, GFP_KERNEL);
3114 if (!buf) {
3115 printk(KERN_ERR "prepmixch: out of memory\n") ;
3116 return;
3119 ch = &state->mixch[state->nrmixch-1];
3120 switch (ch->selector) {
3121 case 0: /* mixer unit request */
3122 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3123 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3124 goto err;
3125 ch->minval = buf[0] | (buf[1] << 8);
3126 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3127 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3128 goto err;
3129 ch->maxval = buf[0] | (buf[1] << 8);
3130 v2 = ch->maxval - ch->minval;
3131 if (!v2)
3132 v2 = 1;
3133 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3134 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3135 goto err;
3136 v1 = buf[0] | (buf[1] << 8);
3137 v3 = v1 - ch->minval;
3138 v3 = 100 * v3 / v2;
3139 if (v3 > 100)
3140 v3 = 100;
3141 ch->value = v3;
3142 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3143 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3144 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
3145 state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3146 goto err;
3147 v1 = buf[0] | (buf[1] << 8);
3148 v3 = v1 - ch->minval;
3149 v3 = 100 * v3 / v2;
3150 if (v3 > 100)
3151 v3 = 100;
3153 ch->value |= v3 << 8;
3154 break;
3156 /* various feature unit controls */
3157 case VOLUME_CONTROL:
3158 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3159 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3160 goto err;
3161 ch->minval = buf[0] | (buf[1] << 8);
3162 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3163 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3164 goto err;
3165 ch->maxval = buf[0] | (buf[1] << 8);
3166 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3167 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3168 goto err;
3169 v1 = buf[0] | (buf[1] << 8);
3170 v2 = ch->maxval - ch->minval;
3171 v3 = v1 - ch->minval;
3172 if (!v2)
3173 v2 = 1;
3174 v3 = 100 * v3 / v2;
3175 if (v3 > 100)
3176 v3 = 100;
3177 ch->value = v3;
3178 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3179 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3180 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3181 goto err;
3182 v1 = buf[0] | (buf[1] << 8);
3183 v3 = v1 - ch->minval;
3184 v3 = 100 * v3 / v2;
3185 if (v3 > 100)
3186 v3 = 100;
3188 ch->value |= v3 << 8;
3189 break;
3191 case BASS_CONTROL:
3192 case MID_CONTROL:
3193 case TREBLE_CONTROL:
3194 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3195 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3196 goto err;
3197 ch->minval = buf[0] << 8;
3198 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3199 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3200 goto err;
3201 ch->maxval = buf[0] << 8;
3202 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3203 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3204 goto err;
3205 v1 = buf[0] << 8;
3206 v2 = ch->maxval - ch->minval;
3207 v3 = v1 - ch->minval;
3208 if (!v2)
3209 v2 = 1;
3210 v3 = 100 * v3 / v2;
3211 if (v3 > 100)
3212 v3 = 100;
3213 ch->value = v3;
3214 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3215 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3216 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3217 goto err;
3218 v1 = buf[0] << 8;
3219 v3 = v1 - ch->minval;
3220 v3 = 100 * v3 / v2;
3221 if (v3 > 100)
3222 v3 = 100;
3224 ch->value |= v3 << 8;
3225 break;
3227 default:
3228 goto err;
3231 freebuf:
3232 kfree(buf);
3233 return;
3234 err:
3235 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
3236 dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector);
3237 if (state->nrmixch)
3238 state->nrmixch--;
3239 goto freebuf;
3243 static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid);
3245 static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch)
3247 unsigned int idx;
3249 idx = inidx*numoch;
3250 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3251 return 0;
3252 if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
3253 return 1;
3254 idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT);
3255 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3256 return 0;
3257 return 1;
3260 static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer)
3262 unsigned int nroutch = mixer[5+mixer[4]];
3263 unsigned int chidx[SOUND_MIXER_NRDEVICES+1];
3264 unsigned int termt[SOUND_MIXER_NRDEVICES];
3265 unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0;
3266 unsigned char *bmap = &mixer[9+mixer[4]];
3267 unsigned int bmapsize;
3268 struct mixerchannel *ch;
3269 unsigned int i;
3271 if (!mixer[4]) {
3272 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]);
3273 return;
3275 if (mixer[4] > SOUND_MIXER_NRDEVICES) {
3276 printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]);
3277 return;
3279 chidx[0] = 0;
3280 for (i = 0; i < mixer[4]; i++) {
3281 usb_audio_recurseunit(state, mixer[5+i]);
3282 chidx[i+1] = chidx[i] + state->nrchannels;
3283 termt[i] = state->termtype;
3285 state->termtype = 0;
3286 state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8);
3287 bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3;
3288 bmap += bmapsize - 1;
3289 if (mixer[0] < 10+mixer[4]+bmapsize) {
3290 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]);
3291 return;
3293 for (i = 0; i < mixer[4]; i++) {
3294 state->termtype = termt[i];
3295 if (chidx[i+1]-chidx[i] >= 2) {
3296 flg |= MIXFLG_STEREOIN;
3297 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3298 ch = getmixchannel(state, getvolchannel(state));
3299 if (ch) {
3300 ch->unitid = mixer[3];
3301 ch->selector = 0;
3302 ch->chnum = chidx[i]+1;
3303 ch->flags = flg;
3304 prepmixch(state);
3306 continue;
3309 flg &= ~MIXFLG_STEREOIN;
3310 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3311 ch = getmixchannel(state, getvolchannel(state));
3312 if (ch) {
3313 ch->unitid = mixer[3];
3314 ch->selector = 0;
3315 ch->chnum = chidx[i]+1;
3316 ch->flags = flg;
3317 prepmixch(state);
3321 state->termtype = 0;
3324 static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid)
3326 unsigned int i;
3328 for (i = 0; i < state->nrmixch; i++)
3329 if (state->mixch[i].unitid == unitid)
3330 return &state->mixch[i];
3331 return NULL;
3334 static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector)
3336 unsigned int chnum, i, mixch;
3337 struct mixerchannel *mch;
3339 if (!selector[4]) {
3340 printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]);
3341 return;
3343 mixch = state->nrmixch;
3344 usb_audio_recurseunit(state, selector[5]);
3345 if (state->nrmixch != mixch) {
3346 mch = &state->mixch[state->nrmixch-1];
3347 mch->slctunitid = selector[3] | (1 << 8);
3348 } else if ((mch = slctsrc_findunit(state, selector[5]))) {
3349 mch->slctunitid = selector[3] | (1 << 8);
3350 } else {
3351 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]);
3353 chnum = state->nrchannels;
3354 for (i = 1; i < selector[4]; i++) {
3355 mixch = state->nrmixch;
3356 usb_audio_recurseunit(state, selector[5+i]);
3357 if (chnum != state->nrchannels) {
3358 printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]);
3359 state->termtype = 0;
3360 state->chconfig = 0;
3361 state->nrchannels = 0;
3362 return;
3364 if (state->nrmixch != mixch) {
3365 mch = &state->mixch[state->nrmixch-1];
3366 mch->slctunitid = selector[3] | ((i + 1) << 8);
3367 } else if ((mch = slctsrc_findunit(state, selector[5+i]))) {
3368 mch->slctunitid = selector[3] | ((i + 1) << 8);
3369 } else {
3370 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1);
3373 state->termtype = 0;
3374 state->chconfig = 0;
3377 /* in the future we might try to handle 3D etc. effect units */
3379 static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc)
3381 unsigned int i;
3383 for (i = 0; i < proc[6]; i++)
3384 usb_audio_recurseunit(state, proc[7+i]);
3385 state->nrchannels = proc[7+proc[6]];
3386 state->termtype = 0;
3387 state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8);
3391 /* See Audio Class Spec, section 4.3.2.5 */
3392 static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr)
3394 struct mixerchannel *ch;
3395 unsigned short chftr, mchftr;
3396 #if 0
3397 struct usb_device *dev = state->s->usbdev;
3398 unsigned char data[1];
3399 #endif
3400 unsigned char nr_logical_channels, i;
3402 usb_audio_recurseunit(state, ftr[4]);
3404 if (ftr[5] == 0 ) {
3405 printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]);
3406 return;
3409 if (state->nrchannels == 0) {
3410 printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]);
3411 return;
3413 if (state->nrchannels > 2)
3414 printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]);
3416 nr_logical_channels=(ftr[0]-7)/ftr[5]-1;
3418 if (nr_logical_channels != state->nrchannels) {
3419 printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels);
3421 if (state->nrchannels == 1 && nr_logical_channels==0) {
3422 printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n");
3423 } else if (state->nrchannels == 1 && nr_logical_channels==2) {
3424 printk(KERN_INFO "usbaudio: assuming that a stereo channel connected directly to a mixer is missing in search (got Labtec headset?). Should be fine.\n");
3425 state->nrchannels=nr_logical_channels;
3426 } else {
3427 printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n");
3431 /* There is always a master channel */
3432 mchftr = ftr[6];
3433 /* Binary AND over logical channels if they exist */
3434 if (nr_logical_channels) {
3435 chftr = ftr[6+ftr[5]];
3436 for (i = 2; i <= nr_logical_channels; i++)
3437 chftr &= ftr[6+i*ftr[5]];
3438 } else {
3439 chftr = 0;
3442 /* volume control */
3443 if (chftr & 2) {
3444 ch = getmixchannel(state, getvolchannel(state));
3445 if (ch) {
3446 ch->unitid = ftr[3];
3447 ch->selector = VOLUME_CONTROL;
3448 ch->chnum = 1;
3449 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3450 prepmixch(state);
3452 } else if (mchftr & 2) {
3453 ch = getmixchannel(state, getvolchannel(state));
3454 if (ch) {
3455 ch->unitid = ftr[3];
3456 ch->selector = VOLUME_CONTROL;
3457 ch->chnum = 0;
3458 ch->flags = 0;
3459 prepmixch(state);
3462 /* bass control */
3463 if (chftr & 4) {
3464 ch = getmixchannel(state, SOUND_MIXER_BASS);
3465 if (ch) {
3466 ch->unitid = ftr[3];
3467 ch->selector = BASS_CONTROL;
3468 ch->chnum = 1;
3469 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3470 prepmixch(state);
3472 } else if (mchftr & 4) {
3473 ch = getmixchannel(state, SOUND_MIXER_BASS);
3474 if (ch) {
3475 ch->unitid = ftr[3];
3476 ch->selector = BASS_CONTROL;
3477 ch->chnum = 0;
3478 ch->flags = 0;
3479 prepmixch(state);
3482 /* treble control */
3483 if (chftr & 16) {
3484 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3485 if (ch) {
3486 ch->unitid = ftr[3];
3487 ch->selector = TREBLE_CONTROL;
3488 ch->chnum = 1;
3489 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3490 prepmixch(state);
3492 } else if (mchftr & 16) {
3493 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3494 if (ch) {
3495 ch->unitid = ftr[3];
3496 ch->selector = TREBLE_CONTROL;
3497 ch->chnum = 0;
3498 ch->flags = 0;
3499 prepmixch(state);
3502 #if 0
3503 /* if there are mute controls, unmute them */
3504 /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */
3505 if ((chftr & 1) || (mchftr & 1)) {
3506 printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif);
3507 data[0] = 0;
3508 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
3509 (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, 1000) < 0)
3510 printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif);
3512 #endif
3515 static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid)
3517 unsigned char *p1;
3518 unsigned int i, j;
3520 if (test_and_set_bit(unitid, state->unitbitmap)) {
3521 printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid);
3522 return;
3524 p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif);
3525 if (!p1) {
3526 printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
3527 return;
3529 state->nrchannels = 0;
3530 state->termtype = 0;
3531 state->chconfig = 0;
3532 switch (p1[2]) {
3533 case INPUT_TERMINAL:
3534 if (p1[0] < 12) {
3535 printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid);
3536 return;
3538 state->nrchannels = p1[7];
3539 state->termtype = p1[4] | (p1[5] << 8);
3540 state->chconfig = p1[8] | (p1[9] << 8);
3541 return;
3543 case MIXER_UNIT:
3544 if (p1[0] < 10 || p1[0] < 10+p1[4]) {
3545 printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid);
3546 return;
3548 usb_audio_mixerunit(state, p1);
3549 return;
3551 case SELECTOR_UNIT:
3552 if (p1[0] < 6 || p1[0] < 6+p1[4]) {
3553 printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid);
3554 return;
3556 usb_audio_selectorunit(state, p1);
3557 return;
3559 case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */
3560 if (p1[0] < 7 || p1[0] < 7+p1[5]) {
3561 printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
3562 return;
3564 usb_audio_featureunit(state, p1);
3565 return;
3567 case PROCESSING_UNIT:
3568 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3569 printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid);
3570 return;
3572 usb_audio_processingunit(state, p1);
3573 return;
3575 case EXTENSION_UNIT:
3576 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3577 printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid);
3578 return;
3580 for (j = i = 0; i < p1[6]; i++) {
3581 usb_audio_recurseunit(state, p1[7+i]);
3582 if (!i)
3583 j = state->termtype;
3584 else if (j != state->termtype)
3585 j = 0;
3587 state->nrchannels = p1[7+p1[6]];
3588 state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8);
3589 state->termtype = j;
3590 return;
3592 default:
3593 printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
3594 return;
3598 static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm)
3600 struct usb_mixerdev *ms;
3601 struct consmixstate state;
3603 memset(&state, 0, sizeof(state));
3604 state.s = s;
3605 state.nrmixch = 0;
3606 state.mixchmask = ~0;
3607 state.buffer = buffer;
3608 state.buflen = buflen;
3609 state.ctrlif = ctrlif;
3610 set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */
3611 printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n",
3612 oterm[3], oterm[4] | (oterm[5] << 8));
3613 usb_audio_recurseunit(&state, oterm[7]);
3614 if (!state.nrmixch) {
3615 printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]);
3616 return;
3618 if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL)))
3619 return;
3620 memset(ms, 0, sizeof(struct usb_mixerdev));
3621 memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel));
3622 ms->state = s;
3623 ms->iface = ctrlif;
3624 ms->numch = state.nrmixch;
3625 if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) {
3626 printk(KERN_ERR "usbaudio: cannot register mixer\n");
3627 kfree(ms);
3628 return;
3630 printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer);
3631 list_add_tail(&ms->list, &s->mixerlist);
3634 /* arbitrary limit, we won't check more interfaces than this */
3635 #define USB_MAXINTERFACES 32
3637 static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif)
3639 struct usb_audio_state *s;
3640 struct usb_interface *iface;
3641 struct usb_host_interface *alt;
3642 unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES];
3643 unsigned char *p1;
3644 unsigned int i, j, k, numifin = 0, numifout = 0;
3646 if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL)))
3647 return NULL;
3648 memset(s, 0, sizeof(struct usb_audio_state));
3649 INIT_LIST_HEAD(&s->audiolist);
3650 INIT_LIST_HEAD(&s->mixerlist);
3651 s->usbdev = dev;
3652 s->count = 1;
3654 /* find audiocontrol interface */
3655 if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) {
3656 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n",
3657 dev->devnum, ctrlif);
3658 goto ret;
3660 if (p1[0] < 8 + p1[7]) {
3661 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n",
3662 dev->devnum, ctrlif);
3663 goto ret;
3665 if (!p1[7])
3666 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n",
3667 dev->devnum, ctrlif);
3668 for (i = 0; i < p1[7]; i++) {
3669 j = p1[8+i];
3670 iface = usb_ifnum_to_if(dev, j);
3671 if (!iface) {
3672 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n",
3673 dev->devnum, ctrlif, j);
3674 continue;
3676 if (iface->num_altsetting == 1) {
3677 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif);
3678 continue;
3680 alt = usb_altnum_to_altsetting(iface, 0);
3681 if (!alt) {
3682 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n",
3683 dev->devnum, ctrlif, j);
3684 continue;
3686 if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) {
3687 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n",
3688 dev->devnum, ctrlif, j);
3689 continue;
3691 if (alt->desc.bInterfaceSubClass == 3) {
3692 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n",
3693 dev->devnum, ctrlif, j);
3694 continue;
3696 if (alt->desc.bInterfaceSubClass != 2) {
3697 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n",
3698 dev->devnum, ctrlif, j);
3699 continue;
3701 if (alt->desc.bNumEndpoints > 0) {
3702 /* Check all endpoints; should they all have a bandwidth of 0 ? */
3703 for (k = 0; k < alt->desc.bNumEndpoints; k++) {
3704 if (le16_to_cpu(alt->endpoint[k].desc.wMaxPacketSize) > 0) {
3705 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k);
3706 break;
3709 if (k < alt->desc.bNumEndpoints)
3710 continue;
3713 alt = usb_altnum_to_altsetting(iface, 1);
3714 if (!alt) {
3715 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n",
3716 dev->devnum, ctrlif, j);
3717 continue;
3719 if (alt->desc.bNumEndpoints < 1) {
3720 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n",
3721 dev->devnum, ctrlif, j);
3722 continue;
3724 /* note: this requires the data endpoint to be ep0 and the optional sync
3725 ep to be ep1, which seems to be the case */
3726 if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) {
3727 if (numifin < USB_MAXINTERFACES) {
3728 ifin[numifin++] = j;
3729 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3731 } else {
3732 if (numifout < USB_MAXINTERFACES) {
3733 ifout[numifout++] = j;
3734 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3738 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n",
3739 dev->devnum, ctrlif, numifin, numifout);
3740 for (i = 0; i < numifin && i < numifout; i++)
3741 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]);
3742 for (j = i; j < numifin; j++)
3743 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1);
3744 for (j = i; j < numifout; j++)
3745 usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]);
3746 /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */
3747 p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1);
3748 while (p1) {
3749 if (p1[0] >= 9)
3750 usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1);
3751 p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1);
3754 ret:
3755 if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) {
3756 kfree(s);
3757 return NULL;
3759 /* everything successful */
3760 down(&open_sem);
3761 list_add_tail(&s->audiodev, &audiodevs);
3762 up(&open_sem);
3763 printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s);
3764 return s;
3767 /* we only care for the currently active configuration */
3769 static int usb_audio_probe(struct usb_interface *intf,
3770 const struct usb_device_id *id)
3772 struct usb_device *dev = interface_to_usbdev (intf);
3773 struct usb_audio_state *s;
3774 unsigned char *buffer;
3775 unsigned int buflen;
3777 #if 0
3778 printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum,
3779 config->interface[ifnum].altsetting[0].desc.bInterfaceClass,
3780 config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass);
3781 #endif
3784 * audiocontrol interface found
3785 * find which configuration number is active
3787 buffer = dev->rawdescriptors[dev->actconfig - dev->config];
3788 buflen = le16_to_cpu(dev->actconfig->desc.wTotalLength);
3789 s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber);
3790 if (s) {
3791 usb_set_intfdata (intf, s);
3792 return 0;
3794 return -ENODEV;
3798 /* a revoke facility would make things simpler */
3800 static void usb_audio_disconnect(struct usb_interface *intf)
3802 struct usb_audio_state *s = usb_get_intfdata (intf);
3803 struct usb_audiodev *as;
3804 struct usb_mixerdev *ms;
3806 if (!s)
3807 return;
3809 /* we get called with -1 for every audiostreaming interface registered */
3810 if (s == (struct usb_audio_state *)-1) {
3811 dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n"));
3812 return;
3814 if (!s->usbdev) {
3815 dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s));
3816 return;
3818 down(&open_sem);
3819 list_del_init(&s->audiodev);
3820 s->usbdev = NULL;
3821 usb_set_intfdata (intf, NULL);
3823 /* deregister all audio and mixer devices, so no new processes can open this device */
3824 list_for_each_entry(as, &s->audiolist, list) {
3825 usbin_disc(as);
3826 usbout_disc(as);
3827 wake_up(&as->usbin.dma.wait);
3828 wake_up(&as->usbout.dma.wait);
3829 if (as->dev_audio >= 0) {
3830 unregister_sound_dsp(as->dev_audio);
3831 printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio);
3833 as->dev_audio = -1;
3835 list_for_each_entry(ms, &s->mixerlist, list) {
3836 if (ms->dev_mixer >= 0) {
3837 unregister_sound_mixer(ms->dev_mixer);
3838 printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer);
3840 ms->dev_mixer = -1;
3842 release(s);
3843 wake_up(&open_wait);
3846 static int __init usb_audio_init(void)
3848 int result = usb_register(&usb_audio_driver);
3849 if (result == 0)
3850 info(DRIVER_VERSION ":" DRIVER_DESC);
3851 return result;
3855 static void __exit usb_audio_cleanup(void)
3857 usb_deregister(&usb_audio_driver);
3860 module_init(usb_audio_init);
3861 module_exit(usb_audio_cleanup);
3863 MODULE_AUTHOR( DRIVER_AUTHOR );
3864 MODULE_DESCRIPTION( DRIVER_DESC );
3865 MODULE_LICENSE("GPL");