1 /*****************************************************************************/
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.
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
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
55 * "Let's make things better" -> but please Philips start with your
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
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.
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
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>
198 #include <linux/usb.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
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))
237 * - Synchronisation behaviour
238 * Don't touch this if you don't understand all of the above.
241 #define SYNCFRAMES DESCFRAMES
243 #define MIXFLG_STEREOIN 1
244 #define MIXFLG_STEREOOUT 2
246 struct mixerchannel
{
248 __u16 osschannel
; /* number of the OSS channel */
249 __s16 minval
, maxval
;
259 unsigned int sratelo
;
260 unsigned int sratehi
;
261 unsigned char altsetting
;
262 unsigned char attributes
;
266 /* buffer data format */
269 /* physical buffer */
270 unsigned char *sgbuf
[NRSGBUF
];
274 unsigned wrptr
, rdptr
;
275 unsigned total_bytes
;
277 unsigned error
; /* over/underrun */
278 wait_queue_head_t wait
;
279 /* redundant, but makes calculations easier */
285 unsigned ossfragshift
;
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
308 struct usb_audiodev
{
309 struct list_head list
;
310 struct usb_audio_state
*state
;
312 /* soundcore stuff */
317 spinlock_t lock
; /* DMA buffer access spinlock */
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 */
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 */
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 */
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
;
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
)
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
)
440 for(nr
= 0; nr
< NRSGBUF
; nr
++) {
441 if (!(p
= db
->sgbuf
[nr
]))
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
;
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);
464 db
->fragshift
= db
->ossfragshift
;
466 db
->fragshift
= ld2(bytepersec
/100/(db
->subdivision
? db
->subdivision
: 1));
467 if (db
->fragshift
< 3)
470 db
->numfrag
= bufs
>> db
->fragshift
;
471 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
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
);
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
)
491 db
->bufsize
= nr
<< PAGE_SHIFT
;
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
));
500 static int dmabuf_mmap(struct vm_area_struct
*vma
, struct dmabuf
*db
, unsigned long start
, unsigned long size
, pgprot_t prot
)
504 if (!db
->ready
|| db
->mapped
|| (start
| size
) & (PAGE_SIZE
-1) || size
> db
->bufsize
)
507 for(nr
= 0; nr
< size
; nr
++)
511 for(nr
= 0; nr
< size
; nr
++) {
514 pfn
= virt_to_phys(db
->sgbuf
[nr
]) >> PAGE_SHIFT
;
515 if (remap_pfn_range(vma
, start
, pfn
, PAGE_SIZE
, prot
))
522 static void dmabuf_copyin(struct dmabuf
*db
, const void *buffer
, unsigned int size
)
524 unsigned int pgrem
, rem
;
526 db
->total_bytes
+= size
;
530 pgrem
= ((~db
->wrptr
) & (PAGE_SIZE
-1)) + 1;
533 rem
= db
->dmasize
- db
->wrptr
;
536 memcpy((db
->sgbuf
[db
->wrptr
>> PAGE_SHIFT
]) + (db
->wrptr
& (PAGE_SIZE
-1)), buffer
, pgrem
);
540 if (db
->wrptr
>= db
->dmasize
)
545 static void dmabuf_copyout(struct dmabuf
*db
, void *buffer
, unsigned int size
)
547 unsigned int pgrem
, rem
;
549 db
->total_bytes
+= size
;
553 pgrem
= ((~db
->rdptr
) & (PAGE_SIZE
-1)) + 1;
556 rem
= db
->dmasize
- db
->rdptr
;
559 memcpy(buffer
, (db
->sgbuf
[db
->rdptr
>> PAGE_SHIFT
]) + (db
->rdptr
& (PAGE_SIZE
-1)), pgrem
);
563 if (db
->rdptr
>= db
->dmasize
)
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
)
577 pgrem
= ((~ptr
) & (PAGE_SIZE
-1)) + 1;
580 rem
= db
->dmasize
- ptr
;
583 if (copy_from_user((db
->sgbuf
[ptr
>> PAGE_SHIFT
]) + (ptr
& (PAGE_SIZE
-1)), buffer
, pgrem
))
588 if (ptr
>= db
->dmasize
)
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
)
602 pgrem
= ((~ptr
) & (PAGE_SIZE
-1)) + 1;
605 rem
= db
->dmasize
- ptr
;
608 if (copy_to_user(buffer
, (db
->sgbuf
[ptr
>> PAGE_SHIFT
]) + (ptr
& (PAGE_SIZE
-1)), pgrem
))
613 if (ptr
>= db
->dmasize
)
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
;
627 unsigned int i
, notkilled
= 1;
629 spin_lock_irqsave(&as
->lock
, flags
);
630 u
->flags
&= ~FLG_RUNNING
;
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
);
636 spin_lock_irqsave(&as
->lock
, 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
);
651 set_current_state(TASK_RUNNING
);
652 if (u
->durb
[0].urb
->transfer_buffer
)
653 kfree(u
->durb
[0].urb
->transfer_buffer
);
654 if (u
->durb
[1].urb
->transfer_buffer
)
655 kfree(u
->durb
[1].urb
->transfer_buffer
);
656 if (u
->surb
[0].urb
->transfer_buffer
)
657 kfree(u
->surb
[0].urb
->transfer_buffer
);
658 if (u
->surb
[1].urb
->transfer_buffer
)
659 kfree(u
->surb
[1].urb
->transfer_buffer
);
660 u
->durb
[0].urb
->transfer_buffer
= u
->durb
[1].urb
->transfer_buffer
=
661 u
->surb
[0].urb
->transfer_buffer
= u
->surb
[1].urb
->transfer_buffer
= NULL
;
664 static inline void usbin_release(struct usb_audiodev
*as
)
669 static void usbin_disc(struct usb_audiodev
*as
)
671 struct usbin
*u
= &as
->usbin
;
675 spin_lock_irqsave(&as
->lock
, flags
);
676 u
->flags
&= ~(FLG_RUNNING
| FLG_CONNECTED
);
677 spin_unlock_irqrestore(&as
->lock
, flags
);
681 static void conversion(const void *ibuf
, unsigned int ifmt
, void *obuf
, unsigned int ofmt
, void *tmp
, unsigned int scnt
)
688 if (AFMT_ISSTEREO(ifmt
))
690 sp
= ((__s16
*)tmp
) + cnt
;
691 switch (ifmt
& ~AFMT_STEREO
) {
693 for (bp
= ((unsigned char *)ibuf
)+cnt
, i
= 0; i
< cnt
; i
++) {
696 *sp
= (*bp
^ 0x80) << 8;
701 for (bp
= ((unsigned char *)ibuf
)+cnt
, i
= 0; i
< cnt
; i
++) {
709 for (bp
= ((unsigned char *)ibuf
)+2*cnt
, i
= 0; i
< cnt
; i
++) {
712 *sp
= (bp
[0] | (bp
[1] << 8)) ^ 0x8000;
717 for (bp
= ((unsigned char *)ibuf
)+2*cnt
, i
= 0; i
< cnt
; i
++) {
720 *sp
= (bp
[1] | (bp
[0] << 8)) ^ 0x8000;
725 for (bp
= ((unsigned char *)ibuf
)+2*cnt
, i
= 0; i
< cnt
; i
++) {
728 *sp
= bp
[0] | (bp
[1] << 8);
733 for (bp
= ((unsigned char *)ibuf
)+2*cnt
, i
= 0; i
< cnt
; i
++) {
736 *sp
= bp
[1] | (bp
[0] << 8);
740 if (!AFMT_ISSTEREO(ifmt
) && AFMT_ISSTEREO(ofmt
)) {
741 /* expand from mono to stereo */
742 for (sp
= ((__s16
*)tmp
)+scnt
, sp2
= ((__s16
*)tmp
)+2*scnt
, i
= 0; i
< scnt
; i
++) {
745 sp2
[0] = sp2
[1] = sp
[0];
748 if (AFMT_ISSTEREO(ifmt
) && !AFMT_ISSTEREO(ofmt
)) {
749 /* contract from stereo to mono */
750 for (sp
= sp2
= ((__s16
*)tmp
), i
= 0; i
< scnt
; i
++, sp
++, sp2
+= 2)
751 sp
[0] = (sp2
[0] + sp2
[1]) >> 1;
754 if (AFMT_ISSTEREO(ofmt
))
757 bp
= ((unsigned char *)obuf
);
758 switch (ofmt
& ~AFMT_STEREO
) {
760 for (i
= 0; i
< cnt
; i
++, sp
++, bp
++)
761 *bp
= (*sp
>> 8) ^ 0x80;
765 for (i
= 0; i
< cnt
; i
++, sp
++, bp
++)
770 for (i
= 0; i
< cnt
; i
++, sp
++, bp
+= 2) {
773 bp
[1] = (s
>> 8) ^ 0x80;
778 for (i
= 0; i
< cnt
; i
++, sp
++, bp
+= 2) {
781 bp
[0] = (s
>> 8) ^ 0x80;
786 for (i
= 0; i
< cnt
; i
++, sp
++, bp
+= 2) {
794 for (i
= 0; i
< cnt
; i
++, sp
++, bp
+= 2) {
804 static void usbin_convert(struct usbin
*u
, unsigned char *buffer
, unsigned int samples
)
810 unsigned int scnt
, maxs
, ufmtsh
, dfmtsh
;
812 ufmtsh
= AFMT_BYTESSHIFT(u
->format
);
813 dfmtsh
= AFMT_BYTESSHIFT(u
->dma
.format
);
814 maxs
= (AFMT_ISSTEREO(u
->dma
.format
| u
->format
)) ? 32 : 64;
815 while (samples
> 0) {
819 conversion(buffer
, u
->format
, tmp
.b
, u
->dma
.format
, tmp
.b
, scnt
);
820 dmabuf_copyin(&u
->dma
, tmp
.b
, scnt
<< dfmtsh
);
821 buffer
+= scnt
<< ufmtsh
;
826 static int usbin_prepare_desc(struct usbin
*u
, struct urb
*urb
)
828 unsigned int i
, maxsize
, offs
;
830 maxsize
= (u
->freqmax
+ 0x3fff) >> (14 - AFMT_BYTESSHIFT(u
->format
));
831 //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format);
832 for (i
= offs
= 0; i
< DESCFRAMES
; i
++, offs
+= maxsize
) {
833 urb
->iso_frame_desc
[i
].length
= maxsize
;
834 urb
->iso_frame_desc
[i
].offset
= offs
;
841 * return value: 0 if descriptor should be restarted, -1 otherwise
842 * convert sample format on the fly if necessary
844 static int usbin_retire_desc(struct usbin
*u
, struct urb
*urb
)
846 unsigned int i
, ufmtsh
, dfmtsh
, err
= 0, cnt
, scnt
, dmafree
;
849 ufmtsh
= AFMT_BYTESSHIFT(u
->format
);
850 dfmtsh
= AFMT_BYTESSHIFT(u
->dma
.format
);
851 for (i
= 0; i
< DESCFRAMES
; i
++) {
852 cp
= ((unsigned char *)urb
->transfer_buffer
) + urb
->iso_frame_desc
[i
].offset
;
853 if (urb
->iso_frame_desc
[i
].status
) {
854 dprintk((KERN_DEBUG
"usbin_retire_desc: frame %u status %d\n", i
, urb
->iso_frame_desc
[i
].status
));
857 scnt
= urb
->iso_frame_desc
[i
].actual_length
>> ufmtsh
;
860 cnt
= scnt
<< dfmtsh
;
861 if (!u
->dma
.mapped
) {
862 dmafree
= u
->dma
.dmasize
- u
->dma
.count
;
864 scnt
= dmafree
>> dfmtsh
;
865 cnt
= scnt
<< dfmtsh
;
870 if (u
->format
== u
->dma
.format
) {
871 /* we do not need format conversion */
872 dprintk((KERN_DEBUG
"usbaudio: no sample format conversion\n"));
873 dmabuf_copyin(&u
->dma
, cp
, cnt
);
875 /* we need sampling format conversion */
876 dprintk((KERN_DEBUG
"usbaudio: sample format conversion %x != %x\n", u
->format
, u
->dma
.format
));
877 usbin_convert(u
, cp
, scnt
);
882 if (u
->dma
.count
>= (signed)u
->dma
.fragsize
)
883 wake_up(&u
->dma
.wait
);
887 static void usbin_completed(struct urb
*urb
, struct pt_regs
*regs
)
889 struct usb_audiodev
*as
= (struct usb_audiodev
*)urb
->context
;
890 struct usbin
*u
= &as
->usbin
;
896 printk(KERN_DEBUG
"usbin_completed: status %d errcnt %d flags 0x%x\n", urb
->status
, urb
->error_count
, u
->flags
);
898 if (urb
== u
->durb
[0].urb
)
899 mask
= FLG_URB0RUNNING
;
900 else if (urb
== u
->durb
[1].urb
)
901 mask
= FLG_URB1RUNNING
;
904 printk(KERN_ERR
"usbin_completed: panic: unknown URB\n");
906 urb
->dev
= as
->state
->usbdev
;
907 spin_lock_irqsave(&as
->lock
, flags
);
908 if (!usbin_retire_desc(u
, urb
) &&
909 u
->flags
& FLG_RUNNING
&&
910 !usbin_prepare_desc(u
, urb
) &&
911 (suret
= usb_submit_urb(urb
, GFP_ATOMIC
)) == 0) {
914 u
->flags
&= ~(mask
| FLG_RUNNING
);
915 wake_up(&u
->dma
.wait
);
916 printk(KERN_DEBUG
"usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret
);
918 spin_unlock_irqrestore(&as
->lock
, flags
);
922 * we output sync data
924 static int usbin_sync_prepare_desc(struct usbin
*u
, struct urb
*urb
)
926 unsigned char *cp
= urb
->transfer_buffer
;
927 unsigned int i
, offs
;
929 for (i
= offs
= 0; i
< SYNCFRAMES
; i
++, offs
+= 3, cp
+= 3) {
930 urb
->iso_frame_desc
[i
].length
= 3;
931 urb
->iso_frame_desc
[i
].offset
= offs
;
933 cp
[1] = u
->freqn
>> 8;
934 cp
[2] = u
->freqn
>> 16;
941 * return value: 0 if descriptor should be restarted, -1 otherwise
943 static int usbin_sync_retire_desc(struct usbin
*u
, struct urb
*urb
)
947 for (i
= 0; i
< SYNCFRAMES
; i
++)
948 if (urb
->iso_frame_desc
[0].status
)
949 dprintk((KERN_DEBUG
"usbin_sync_retire_desc: frame %u status %d\n", i
, urb
->iso_frame_desc
[i
].status
));
953 static void usbin_sync_completed(struct urb
*urb
, struct pt_regs
*regs
)
955 struct usb_audiodev
*as
= (struct usb_audiodev
*)urb
->context
;
956 struct usbin
*u
= &as
->usbin
;
962 printk(KERN_DEBUG
"usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb
->status
, urb
->error_count
, u
->flags
);
964 if (urb
== u
->surb
[0].urb
)
965 mask
= FLG_SYNC0RUNNING
;
966 else if (urb
== u
->surb
[1].urb
)
967 mask
= FLG_SYNC1RUNNING
;
970 printk(KERN_ERR
"usbin_sync_completed: panic: unknown URB\n");
972 urb
->dev
= as
->state
->usbdev
;
973 spin_lock_irqsave(&as
->lock
, flags
);
974 if (!usbin_sync_retire_desc(u
, urb
) &&
975 u
->flags
& FLG_RUNNING
&&
976 !usbin_sync_prepare_desc(u
, urb
) &&
977 (suret
= usb_submit_urb(urb
, GFP_ATOMIC
)) == 0) {
980 u
->flags
&= ~(mask
| FLG_RUNNING
);
981 wake_up(&u
->dma
.wait
);
982 dprintk((KERN_DEBUG
"usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret
));
984 spin_unlock_irqrestore(&as
->lock
, flags
);
987 static int usbin_start(struct usb_audiodev
*as
)
989 struct usb_device
*dev
= as
->state
->usbdev
;
990 struct usbin
*u
= &as
->usbin
;
993 unsigned int maxsze
, bufsz
;
996 printk(KERN_DEBUG
"usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
997 dev
->devnum
, u
->format
, u
->dma
.format
, u
->dma
.srate
);
999 /* allocate USB storage if not already done */
1000 spin_lock_irqsave(&as
->lock
, flags
);
1001 if (!(u
->flags
& FLG_CONNECTED
)) {
1002 spin_unlock_irqrestore(&as
->lock
, flags
);
1005 if (!(u
->flags
& FLG_RUNNING
)) {
1006 spin_unlock_irqrestore(&as
->lock
, flags
);
1007 u
->freqn
= ((u
->dma
.srate
<< 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1008 u
->freqmax
= u
->freqn
+ (u
->freqn
>> 2);
1010 maxsze
= (u
->freqmax
+ 0x3fff) >> (14 - AFMT_BYTESSHIFT(u
->format
));
1011 bufsz
= DESCFRAMES
* maxsze
;
1012 if (u
->durb
[0].urb
->transfer_buffer
)
1013 kfree(u
->durb
[0].urb
->transfer_buffer
);
1014 u
->durb
[0].urb
->transfer_buffer
= kmalloc(bufsz
, GFP_KERNEL
);
1015 u
->durb
[0].urb
->transfer_buffer_length
= bufsz
;
1016 if (u
->durb
[1].urb
->transfer_buffer
)
1017 kfree(u
->durb
[1].urb
->transfer_buffer
);
1018 u
->durb
[1].urb
->transfer_buffer
= kmalloc(bufsz
, GFP_KERNEL
);
1019 u
->durb
[1].urb
->transfer_buffer_length
= bufsz
;
1021 if (u
->surb
[0].urb
->transfer_buffer
)
1022 kfree(u
->surb
[0].urb
->transfer_buffer
);
1023 u
->surb
[0].urb
->transfer_buffer
= kmalloc(3*SYNCFRAMES
, GFP_KERNEL
);
1024 u
->surb
[0].urb
->transfer_buffer_length
= 3*SYNCFRAMES
;
1025 if (u
->surb
[1].urb
->transfer_buffer
)
1026 kfree(u
->surb
[1].urb
->transfer_buffer
);
1027 u
->surb
[1].urb
->transfer_buffer
= kmalloc(3*SYNCFRAMES
, GFP_KERNEL
);
1028 u
->surb
[1].urb
->transfer_buffer_length
= 3*SYNCFRAMES
;
1030 if (!u
->durb
[0].urb
->transfer_buffer
|| !u
->durb
[1].urb
->transfer_buffer
||
1031 (u
->syncpipe
&& (!u
->surb
[0].urb
->transfer_buffer
|| !u
->surb
[1].urb
->transfer_buffer
))) {
1032 printk(KERN_ERR
"usbaudio: cannot start playback device %d\n", dev
->devnum
);
1035 spin_lock_irqsave(&as
->lock
, flags
);
1037 if (u
->dma
.count
>= u
->dma
.dmasize
&& !u
->dma
.mapped
) {
1038 spin_unlock_irqrestore(&as
->lock
, flags
);
1041 u
->flags
|= FLG_RUNNING
;
1042 if (!(u
->flags
& FLG_URB0RUNNING
)) {
1043 urb
= u
->durb
[0].urb
;
1045 urb
->pipe
= u
->datapipe
;
1046 urb
->transfer_flags
= URB_ISO_ASAP
;
1047 urb
->number_of_packets
= DESCFRAMES
;
1049 urb
->complete
= usbin_completed
;
1050 if (!usbin_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_KERNEL
))
1051 u
->flags
|= FLG_URB0RUNNING
;
1053 u
->flags
&= ~FLG_RUNNING
;
1055 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_URB1RUNNING
)) {
1056 urb
= u
->durb
[1].urb
;
1058 urb
->pipe
= u
->datapipe
;
1059 urb
->transfer_flags
= URB_ISO_ASAP
;
1060 urb
->number_of_packets
= DESCFRAMES
;
1062 urb
->complete
= usbin_completed
;
1063 if (!usbin_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_KERNEL
))
1064 u
->flags
|= FLG_URB1RUNNING
;
1066 u
->flags
&= ~FLG_RUNNING
;
1069 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_SYNC0RUNNING
)) {
1070 urb
= u
->surb
[0].urb
;
1072 urb
->pipe
= u
->syncpipe
;
1073 urb
->transfer_flags
= URB_ISO_ASAP
;
1074 urb
->number_of_packets
= SYNCFRAMES
;
1076 urb
->complete
= usbin_sync_completed
;
1077 /* stride: u->syncinterval */
1078 if (!usbin_sync_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_KERNEL
))
1079 u
->flags
|= FLG_SYNC0RUNNING
;
1081 u
->flags
&= ~FLG_RUNNING
;
1083 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_SYNC1RUNNING
)) {
1084 urb
= u
->surb
[1].urb
;
1086 urb
->pipe
= u
->syncpipe
;
1087 urb
->transfer_flags
= URB_ISO_ASAP
;
1088 urb
->number_of_packets
= SYNCFRAMES
;
1090 urb
->complete
= usbin_sync_completed
;
1091 /* stride: u->syncinterval */
1092 if (!usbin_sync_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_KERNEL
))
1093 u
->flags
|= FLG_SYNC1RUNNING
;
1095 u
->flags
&= ~FLG_RUNNING
;
1098 spin_unlock_irqrestore(&as
->lock
, flags
);
1102 static void usbout_stop(struct usb_audiodev
*as
)
1104 struct usbout
*u
= &as
->usbout
;
1105 unsigned long flags
;
1106 unsigned int i
, notkilled
= 1;
1108 spin_lock_irqsave(&as
->lock
, flags
);
1109 u
->flags
&= ~FLG_RUNNING
;
1111 spin_unlock_irqrestore(&as
->lock
, flags
);
1112 while (i
& (FLG_URB0RUNNING
|FLG_URB1RUNNING
|FLG_SYNC0RUNNING
|FLG_SYNC1RUNNING
)) {
1113 set_current_state(notkilled
? TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
1114 schedule_timeout(1);
1115 spin_lock_irqsave(&as
->lock
, flags
);
1117 spin_unlock_irqrestore(&as
->lock
, flags
);
1118 if (notkilled
&& signal_pending(current
)) {
1119 if (i
& FLG_URB0RUNNING
)
1120 usb_kill_urb(u
->durb
[0].urb
);
1121 if (i
& FLG_URB1RUNNING
)
1122 usb_kill_urb(u
->durb
[1].urb
);
1123 if (i
& FLG_SYNC0RUNNING
)
1124 usb_kill_urb(u
->surb
[0].urb
);
1125 if (i
& FLG_SYNC1RUNNING
)
1126 usb_kill_urb(u
->surb
[1].urb
);
1130 set_current_state(TASK_RUNNING
);
1131 if (u
->durb
[0].urb
->transfer_buffer
)
1132 kfree(u
->durb
[0].urb
->transfer_buffer
);
1133 if (u
->durb
[1].urb
->transfer_buffer
)
1134 kfree(u
->durb
[1].urb
->transfer_buffer
);
1135 if (u
->surb
[0].urb
->transfer_buffer
)
1136 kfree(u
->surb
[0].urb
->transfer_buffer
);
1137 if (u
->surb
[1].urb
->transfer_buffer
)
1138 kfree(u
->surb
[1].urb
->transfer_buffer
);
1139 u
->durb
[0].urb
->transfer_buffer
= u
->durb
[1].urb
->transfer_buffer
=
1140 u
->surb
[0].urb
->transfer_buffer
= u
->surb
[1].urb
->transfer_buffer
= NULL
;
1143 static inline void usbout_release(struct usb_audiodev
*as
)
1148 static void usbout_disc(struct usb_audiodev
*as
)
1150 struct usbout
*u
= &as
->usbout
;
1151 unsigned long flags
;
1153 spin_lock_irqsave(&as
->lock
, flags
);
1154 u
->flags
&= ~(FLG_RUNNING
| FLG_CONNECTED
);
1155 spin_unlock_irqrestore(&as
->lock
, flags
);
1159 static void usbout_convert(struct usbout
*u
, unsigned char *buffer
, unsigned int samples
)
1165 unsigned int scnt
, maxs
, ufmtsh
, dfmtsh
;
1167 ufmtsh
= AFMT_BYTESSHIFT(u
->format
);
1168 dfmtsh
= AFMT_BYTESSHIFT(u
->dma
.format
);
1169 maxs
= (AFMT_ISSTEREO(u
->dma
.format
| u
->format
)) ? 32 : 64;
1170 while (samples
> 0) {
1174 dmabuf_copyout(&u
->dma
, tmp
.b
, scnt
<< dfmtsh
);
1175 conversion(tmp
.b
, u
->dma
.format
, buffer
, u
->format
, tmp
.b
, scnt
);
1176 buffer
+= scnt
<< ufmtsh
;
1181 static int usbout_prepare_desc(struct usbout
*u
, struct urb
*urb
)
1183 unsigned int i
, ufmtsh
, dfmtsh
, err
= 0, cnt
, scnt
, offs
;
1184 unsigned char *cp
= urb
->transfer_buffer
;
1186 ufmtsh
= AFMT_BYTESSHIFT(u
->format
);
1187 dfmtsh
= AFMT_BYTESSHIFT(u
->dma
.format
);
1188 for (i
= offs
= 0; i
< DESCFRAMES
; i
++) {
1189 urb
->iso_frame_desc
[i
].offset
= offs
;
1190 u
->phase
= (u
->phase
& 0x3fff) + u
->freqm
;
1191 scnt
= u
->phase
>> 14;
1193 urb
->iso_frame_desc
[i
].length
= 0;
1196 cnt
= scnt
<< dfmtsh
;
1197 if (!u
->dma
.mapped
) {
1198 if (cnt
> u
->dma
.count
) {
1199 scnt
= u
->dma
.count
>> dfmtsh
;
1200 cnt
= scnt
<< dfmtsh
;
1203 u
->dma
.count
-= cnt
;
1205 u
->dma
.count
+= cnt
;
1206 if (u
->format
== u
->dma
.format
) {
1207 /* we do not need format conversion */
1208 dmabuf_copyout(&u
->dma
, cp
, cnt
);
1210 /* we need sampling format conversion */
1211 usbout_convert(u
, cp
, scnt
);
1213 cnt
= scnt
<< ufmtsh
;
1214 urb
->iso_frame_desc
[i
].length
= cnt
;
1221 if (u
->dma
.mapped
) {
1222 if (u
->dma
.count
>= (signed)u
->dma
.fragsize
)
1223 wake_up(&u
->dma
.wait
);
1225 if ((signed)u
->dma
.dmasize
>= u
->dma
.count
+ (signed)u
->dma
.fragsize
)
1226 wake_up(&u
->dma
.wait
);
1228 return err
? -1 : 0;
1232 * return value: 0 if descriptor should be restarted, -1 otherwise
1234 static int usbout_retire_desc(struct usbout
*u
, struct urb
*urb
)
1238 for (i
= 0; i
< DESCFRAMES
; i
++) {
1239 if (urb
->iso_frame_desc
[i
].status
) {
1240 dprintk((KERN_DEBUG
"usbout_retire_desc: frame %u status %d\n", i
, urb
->iso_frame_desc
[i
].status
));
1247 static void usbout_completed(struct urb
*urb
, struct pt_regs
*regs
)
1249 struct usb_audiodev
*as
= (struct usb_audiodev
*)urb
->context
;
1250 struct usbout
*u
= &as
->usbout
;
1251 unsigned long flags
;
1256 printk(KERN_DEBUG
"usbout_completed: status %d errcnt %d flags 0x%x\n", urb
->status
, urb
->error_count
, u
->flags
);
1258 if (urb
== u
->durb
[0].urb
)
1259 mask
= FLG_URB0RUNNING
;
1260 else if (urb
== u
->durb
[1].urb
)
1261 mask
= FLG_URB1RUNNING
;
1264 printk(KERN_ERR
"usbout_completed: panic: unknown URB\n");
1266 urb
->dev
= as
->state
->usbdev
;
1267 spin_lock_irqsave(&as
->lock
, flags
);
1268 if (!usbout_retire_desc(u
, urb
) &&
1269 u
->flags
& FLG_RUNNING
&&
1270 !usbout_prepare_desc(u
, urb
) &&
1271 (suret
= usb_submit_urb(urb
, GFP_ATOMIC
)) == 0) {
1274 u
->flags
&= ~(mask
| FLG_RUNNING
);
1275 wake_up(&u
->dma
.wait
);
1276 dprintk((KERN_DEBUG
"usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret
));
1278 spin_unlock_irqrestore(&as
->lock
, flags
);
1281 static int usbout_sync_prepare_desc(struct usbout
*u
, struct urb
*urb
)
1283 unsigned int i
, offs
;
1285 for (i
= offs
= 0; i
< SYNCFRAMES
; i
++, offs
+= 3) {
1286 urb
->iso_frame_desc
[i
].length
= 3;
1287 urb
->iso_frame_desc
[i
].offset
= offs
;
1294 * return value: 0 if descriptor should be restarted, -1 otherwise
1296 static int usbout_sync_retire_desc(struct usbout
*u
, struct urb
*urb
)
1298 unsigned char *cp
= urb
->transfer_buffer
;
1301 for (i
= 0; i
< SYNCFRAMES
; i
++, cp
+= 3) {
1302 if (urb
->iso_frame_desc
[i
].status
) {
1303 dprintk((KERN_DEBUG
"usbout_sync_retire_desc: frame %u status %d\n", i
, urb
->iso_frame_desc
[i
].status
));
1306 if (urb
->iso_frame_desc
[i
].actual_length
< 3) {
1307 dprintk((KERN_DEBUG
"usbout_sync_retire_desc: frame %u length %d\n", i
, urb
->iso_frame_desc
[i
].actual_length
));
1310 f
= cp
[0] | (cp
[1] << 8) | (cp
[2] << 16);
1311 if (abs(f
- u
->freqn
) > (u
->freqn
>> 3) || f
> u
->freqmax
) {
1312 printk(KERN_WARNING
"usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f
, u
->freqn
);
1320 static void usbout_sync_completed(struct urb
*urb
, struct pt_regs
*regs
)
1322 struct usb_audiodev
*as
= (struct usb_audiodev
*)urb
->context
;
1323 struct usbout
*u
= &as
->usbout
;
1324 unsigned long flags
;
1329 printk(KERN_DEBUG
"usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb
->status
, urb
->error_count
, u
->flags
);
1331 if (urb
== u
->surb
[0].urb
)
1332 mask
= FLG_SYNC0RUNNING
;
1333 else if (urb
== u
->surb
[1].urb
)
1334 mask
= FLG_SYNC1RUNNING
;
1337 printk(KERN_ERR
"usbout_sync_completed: panic: unknown URB\n");
1339 urb
->dev
= as
->state
->usbdev
;
1340 spin_lock_irqsave(&as
->lock
, flags
);
1341 if (!usbout_sync_retire_desc(u
, urb
) &&
1342 u
->flags
& FLG_RUNNING
&&
1343 !usbout_sync_prepare_desc(u
, urb
) &&
1344 (suret
= usb_submit_urb(urb
, GFP_ATOMIC
)) == 0) {
1347 u
->flags
&= ~(mask
| FLG_RUNNING
);
1348 wake_up(&u
->dma
.wait
);
1349 dprintk((KERN_DEBUG
"usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret
));
1351 spin_unlock_irqrestore(&as
->lock
, flags
);
1354 static int usbout_start(struct usb_audiodev
*as
)
1356 struct usb_device
*dev
= as
->state
->usbdev
;
1357 struct usbout
*u
= &as
->usbout
;
1359 unsigned long flags
;
1360 unsigned int maxsze
, bufsz
;
1363 printk(KERN_DEBUG
"usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
1364 dev
->devnum
, u
->format
, u
->dma
.format
, u
->dma
.srate
);
1366 /* allocate USB storage if not already done */
1367 spin_lock_irqsave(&as
->lock
, flags
);
1368 if (!(u
->flags
& FLG_CONNECTED
)) {
1369 spin_unlock_irqrestore(&as
->lock
, flags
);
1372 if (!(u
->flags
& FLG_RUNNING
)) {
1373 spin_unlock_irqrestore(&as
->lock
, flags
);
1374 u
->freqn
= u
->freqm
= ((u
->dma
.srate
<< 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1375 u
->freqmax
= u
->freqn
+ (u
->freqn
>> 2);
1377 maxsze
= (u
->freqmax
+ 0x3fff) >> (14 - AFMT_BYTESSHIFT(u
->format
));
1378 bufsz
= DESCFRAMES
* maxsze
;
1379 if (u
->durb
[0].urb
->transfer_buffer
)
1380 kfree(u
->durb
[0].urb
->transfer_buffer
);
1381 u
->durb
[0].urb
->transfer_buffer
= kmalloc(bufsz
, GFP_KERNEL
);
1382 u
->durb
[0].urb
->transfer_buffer_length
= bufsz
;
1383 if (u
->durb
[1].urb
->transfer_buffer
)
1384 kfree(u
->durb
[1].urb
->transfer_buffer
);
1385 u
->durb
[1].urb
->transfer_buffer
= kmalloc(bufsz
, GFP_KERNEL
);
1386 u
->durb
[1].urb
->transfer_buffer_length
= bufsz
;
1388 if (u
->surb
[0].urb
->transfer_buffer
)
1389 kfree(u
->surb
[0].urb
->transfer_buffer
);
1390 u
->surb
[0].urb
->transfer_buffer
= kmalloc(3*SYNCFRAMES
, GFP_KERNEL
);
1391 u
->surb
[0].urb
->transfer_buffer_length
= 3*SYNCFRAMES
;
1392 if (u
->surb
[1].urb
->transfer_buffer
)
1393 kfree(u
->surb
[1].urb
->transfer_buffer
);
1394 u
->surb
[1].urb
->transfer_buffer
= kmalloc(3*SYNCFRAMES
, GFP_KERNEL
);
1395 u
->surb
[1].urb
->transfer_buffer_length
= 3*SYNCFRAMES
;
1397 if (!u
->durb
[0].urb
->transfer_buffer
|| !u
->durb
[1].urb
->transfer_buffer
||
1398 (u
->syncpipe
&& (!u
->surb
[0].urb
->transfer_buffer
|| !u
->surb
[1].urb
->transfer_buffer
))) {
1399 printk(KERN_ERR
"usbaudio: cannot start playback device %d\n", dev
->devnum
);
1402 spin_lock_irqsave(&as
->lock
, flags
);
1404 if (u
->dma
.count
<= 0 && !u
->dma
.mapped
) {
1405 spin_unlock_irqrestore(&as
->lock
, flags
);
1408 u
->flags
|= FLG_RUNNING
;
1409 if (!(u
->flags
& FLG_URB0RUNNING
)) {
1410 urb
= u
->durb
[0].urb
;
1412 urb
->pipe
= u
->datapipe
;
1413 urb
->transfer_flags
= URB_ISO_ASAP
;
1414 urb
->number_of_packets
= DESCFRAMES
;
1416 urb
->complete
= usbout_completed
;
1417 if (!usbout_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_ATOMIC
))
1418 u
->flags
|= FLG_URB0RUNNING
;
1420 u
->flags
&= ~FLG_RUNNING
;
1422 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_URB1RUNNING
)) {
1423 urb
= u
->durb
[1].urb
;
1425 urb
->pipe
= u
->datapipe
;
1426 urb
->transfer_flags
= URB_ISO_ASAP
;
1427 urb
->number_of_packets
= DESCFRAMES
;
1429 urb
->complete
= usbout_completed
;
1430 if (!usbout_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_ATOMIC
))
1431 u
->flags
|= FLG_URB1RUNNING
;
1433 u
->flags
&= ~FLG_RUNNING
;
1436 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_SYNC0RUNNING
)) {
1437 urb
= u
->surb
[0].urb
;
1439 urb
->pipe
= u
->syncpipe
;
1440 urb
->transfer_flags
= URB_ISO_ASAP
;
1441 urb
->number_of_packets
= SYNCFRAMES
;
1443 urb
->complete
= usbout_sync_completed
;
1444 /* stride: u->syncinterval */
1445 if (!usbout_sync_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_ATOMIC
))
1446 u
->flags
|= FLG_SYNC0RUNNING
;
1448 u
->flags
&= ~FLG_RUNNING
;
1450 if (u
->flags
& FLG_RUNNING
&& !(u
->flags
& FLG_SYNC1RUNNING
)) {
1451 urb
= u
->surb
[1].urb
;
1453 urb
->pipe
= u
->syncpipe
;
1454 urb
->transfer_flags
= URB_ISO_ASAP
;
1455 urb
->number_of_packets
= SYNCFRAMES
;
1457 urb
->complete
= usbout_sync_completed
;
1458 /* stride: u->syncinterval */
1459 if (!usbout_sync_prepare_desc(u
, urb
) && !usb_submit_urb(urb
, GFP_ATOMIC
))
1460 u
->flags
|= FLG_SYNC1RUNNING
;
1462 u
->flags
&= ~FLG_RUNNING
;
1465 spin_unlock_irqrestore(&as
->lock
, flags
);
1469 /* --------------------------------------------------------------------- */
1471 static unsigned int format_goodness(struct audioformat
*afp
, unsigned int fmt
, unsigned int srate
)
1475 if (srate
< afp
->sratelo
)
1476 g
+= afp
->sratelo
- srate
;
1477 if (srate
> afp
->sratehi
)
1478 g
+= srate
- afp
->sratehi
;
1479 if (AFMT_ISSTEREO(afp
->format
) && !AFMT_ISSTEREO(fmt
))
1481 if (!AFMT_ISSTEREO(afp
->format
) && AFMT_ISSTEREO(fmt
))
1483 if (AFMT_IS16BIT(afp
->format
) && !AFMT_IS16BIT(fmt
))
1485 if (!AFMT_IS16BIT(afp
->format
) && AFMT_IS16BIT(fmt
))
1490 static int find_format(struct audioformat
*afp
, unsigned int nr
, unsigned int fmt
, unsigned int srate
)
1492 unsigned int i
, g
, gb
= ~0;
1493 int j
= -1; /* default to failure */
1495 /* find "best" format (according to format_goodness) */
1496 for (i
= 0; i
< nr
; i
++) {
1497 g
= format_goodness(&afp
[i
], fmt
, srate
);
1506 static int set_format_in(struct usb_audiodev
*as
)
1508 struct usb_device
*dev
= as
->state
->usbdev
;
1509 struct usb_host_interface
*alts
;
1510 struct usb_interface
*iface
;
1511 struct usbin
*u
= &as
->usbin
;
1512 struct dmabuf
*d
= &u
->dma
;
1513 struct audioformat
*fmt
;
1515 unsigned char data
[3];
1518 iface
= usb_ifnum_to_if(dev
, u
->interface
);
1522 fmtnr
= find_format(as
->fmtin
, as
->numfmtin
, d
->format
, d
->srate
);
1524 printk(KERN_ERR
"usbaudio: set_format_in(): failed to find desired format/speed combination.\n");
1528 fmt
= as
->fmtin
+ fmtnr
;
1529 alts
= usb_altnum_to_altsetting(iface
, fmt
->altsetting
);
1530 u
->format
= fmt
->format
;
1531 u
->datapipe
= usb_rcvisocpipe(dev
, alts
->endpoint
[0].desc
.bEndpointAddress
& 0xf);
1532 u
->syncpipe
= u
->syncinterval
= 0;
1533 if ((alts
->endpoint
[0].desc
.bmAttributes
& 0x0c) == 0x08) {
1534 if (alts
->desc
.bNumEndpoints
< 2 ||
1535 alts
->endpoint
[1].desc
.bmAttributes
!= 0x01 ||
1536 alts
->endpoint
[1].desc
.bSynchAddress
!= 0 ||
1537 alts
->endpoint
[1].desc
.bEndpointAddress
!= (alts
->endpoint
[0].desc
.bSynchAddress
& 0x7f)) {
1538 printk(KERN_WARNING
"usbaudio: device %d interface %d altsetting %d claims adaptive in "
1539 "but has invalid synch pipe; treating as asynchronous in\n",
1540 dev
->devnum
, u
->interface
, fmt
->altsetting
);
1542 u
->syncpipe
= usb_sndisocpipe(dev
, alts
->endpoint
[1].desc
.bEndpointAddress
& 0xf);
1543 u
->syncinterval
= alts
->endpoint
[1].desc
.bRefresh
;
1546 if (d
->srate
< fmt
->sratelo
)
1547 d
->srate
= fmt
->sratelo
;
1548 if (d
->srate
> fmt
->sratehi
)
1549 d
->srate
= fmt
->sratehi
;
1550 dprintk((KERN_DEBUG
"usbaudio: set_format_in: usb_set_interface %u %u\n",
1551 u
->interface
, fmt
->altsetting
));
1552 if (usb_set_interface(dev
, alts
->desc
.bInterfaceNumber
, fmt
->altsetting
) < 0) {
1553 printk(KERN_WARNING
"usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1554 dev
->devnum
, u
->interface
, fmt
->altsetting
);
1557 if (fmt
->sratelo
== fmt
->sratehi
)
1559 ep
= usb_pipeendpoint(u
->datapipe
) | (u
->datapipe
& USB_DIR_IN
);
1560 /* if endpoint has pitch control, enable it */
1561 if (fmt
->attributes
& 0x02) {
1563 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1564 PITCH_CONTROL
<< 8, ep
, data
, 1, 1000)) < 0) {
1565 printk(KERN_ERR
"usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1566 ret
, dev
->devnum
, u
->interface
, ep
, d
->srate
);
1570 /* if endpoint has sampling rate control, set it */
1571 if (fmt
->attributes
& 0x01) {
1573 data
[1] = d
->srate
>> 8;
1574 data
[2] = d
->srate
>> 16;
1575 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1576 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1577 printk(KERN_ERR
"usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1578 ret
, dev
->devnum
, u
->interface
, ep
, d
->srate
);
1581 if ((ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_IN
,
1582 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1583 printk(KERN_ERR
"usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n",
1584 ret
, dev
->devnum
, u
->interface
, ep
);
1587 dprintk((KERN_DEBUG
"usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n",
1588 dev
->devnum
, u
->interface
, fmt
->altsetting
, d
->srate
, data
[0] | (data
[1] << 8) | (data
[2] << 16)));
1589 d
->srate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
1591 dprintk((KERN_DEBUG
"usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u
->format
, d
->format
, d
->srate
));
1595 static int set_format_out(struct usb_audiodev
*as
)
1597 struct usb_device
*dev
= as
->state
->usbdev
;
1598 struct usb_host_interface
*alts
;
1599 struct usb_interface
*iface
;
1600 struct usbout
*u
= &as
->usbout
;
1601 struct dmabuf
*d
= &u
->dma
;
1602 struct audioformat
*fmt
;
1604 unsigned char data
[3];
1607 iface
= usb_ifnum_to_if(dev
, u
->interface
);
1611 fmtnr
= find_format(as
->fmtout
, as
->numfmtout
, d
->format
, d
->srate
);
1613 printk(KERN_ERR
"usbaudio: set_format_out(): failed to find desired format/speed combination.\n");
1617 fmt
= as
->fmtout
+ fmtnr
;
1618 u
->format
= fmt
->format
;
1619 alts
= usb_altnum_to_altsetting(iface
, fmt
->altsetting
);
1620 u
->datapipe
= usb_sndisocpipe(dev
, alts
->endpoint
[0].desc
.bEndpointAddress
& 0xf);
1621 u
->syncpipe
= u
->syncinterval
= 0;
1622 if ((alts
->endpoint
[0].desc
.bmAttributes
& 0x0c) == 0x04) {
1624 printk(KERN_DEBUG
"bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n"
1625 KERN_DEBUG
"endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n"
1626 KERN_DEBUG
"endpoint[0].bSynchAddress 0x%02x\n", alts
->bNumEndpoints
,
1627 alts
->endpoint
[1].bmAttributes
, alts
->endpoint
[1].bSynchAddress
,
1628 alts
->endpoint
[1].bEndpointAddress
, alts
->endpoint
[0].bSynchAddress
);
1630 if (alts
->desc
.bNumEndpoints
< 2 ||
1631 alts
->endpoint
[1].desc
.bmAttributes
!= 0x01 ||
1632 alts
->endpoint
[1].desc
.bSynchAddress
!= 0 ||
1633 alts
->endpoint
[1].desc
.bEndpointAddress
!= (alts
->endpoint
[0].desc
.bSynchAddress
| 0x80)) {
1634 printk(KERN_WARNING
"usbaudio: device %d interface %d altsetting %d claims asynch out "
1635 "but has invalid synch pipe; treating as adaptive out\n",
1636 dev
->devnum
, u
->interface
, fmt
->altsetting
);
1638 u
->syncpipe
= usb_rcvisocpipe(dev
, alts
->endpoint
[1].desc
.bEndpointAddress
& 0xf);
1639 u
->syncinterval
= alts
->endpoint
[1].desc
.bRefresh
;
1642 if (d
->srate
< fmt
->sratelo
)
1643 d
->srate
= fmt
->sratelo
;
1644 if (d
->srate
> fmt
->sratehi
)
1645 d
->srate
= fmt
->sratehi
;
1646 dprintk((KERN_DEBUG
"usbaudio: set_format_out: usb_set_interface %u %u\n",
1647 u
->interface
, fmt
->altsetting
));
1648 if (usb_set_interface(dev
, u
->interface
, fmt
->altsetting
) < 0) {
1649 printk(KERN_WARNING
"usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1650 dev
->devnum
, u
->interface
, fmt
->altsetting
);
1653 if (fmt
->sratelo
== fmt
->sratehi
)
1655 ep
= usb_pipeendpoint(u
->datapipe
) | (u
->datapipe
& USB_DIR_IN
);
1656 /* if endpoint has pitch control, enable it */
1657 if (fmt
->attributes
& 0x02) {
1659 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1660 PITCH_CONTROL
<< 8, ep
, data
, 1, 1000)) < 0) {
1661 printk(KERN_ERR
"usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1662 ret
, dev
->devnum
, u
->interface
, ep
, d
->srate
);
1666 /* if endpoint has sampling rate control, set it */
1667 if (fmt
->attributes
& 0x01) {
1669 data
[1] = d
->srate
>> 8;
1670 data
[2] = d
->srate
>> 16;
1671 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
1672 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1673 printk(KERN_ERR
"usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1674 ret
, dev
->devnum
, u
->interface
, ep
, d
->srate
);
1677 if ((ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_IN
,
1678 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000)) < 0) {
1679 printk(KERN_ERR
"usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n",
1680 ret
, dev
->devnum
, u
->interface
, ep
);
1683 dprintk((KERN_DEBUG
"usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n",
1684 dev
->devnum
, u
->interface
, fmt
->altsetting
, d
->srate
, data
[0] | (data
[1] << 8) | (data
[2] << 16)));
1685 d
->srate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
1687 dprintk((KERN_DEBUG
"usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u
->format
, d
->format
, d
->srate
));
1691 static int set_format(struct usb_audiodev
*s
, unsigned int fmode
, unsigned int fmt
, unsigned int srate
)
1693 int ret1
= 0, ret2
= 0;
1695 if (!(fmode
& (FMODE_READ
|FMODE_WRITE
)))
1697 if (fmode
& FMODE_READ
) {
1699 s
->usbin
.dma
.ready
= 0;
1700 if (fmt
== AFMT_QUERY
)
1701 fmt
= s
->usbin
.dma
.format
;
1703 s
->usbin
.dma
.format
= fmt
;
1705 srate
= s
->usbin
.dma
.srate
;
1707 s
->usbin
.dma
.srate
= srate
;
1709 if (fmode
& FMODE_WRITE
) {
1711 s
->usbout
.dma
.ready
= 0;
1712 if (fmt
== AFMT_QUERY
)
1713 fmt
= s
->usbout
.dma
.format
;
1715 s
->usbout
.dma
.format
= fmt
;
1717 srate
= s
->usbout
.dma
.srate
;
1719 s
->usbout
.dma
.srate
= srate
;
1721 if (fmode
& FMODE_READ
)
1722 ret1
= set_format_in(s
);
1723 if (fmode
& FMODE_WRITE
)
1724 ret2
= set_format_out(s
);
1725 return ret1
? ret1
: ret2
;
1728 /* --------------------------------------------------------------------- */
1730 static int wrmixer(struct usb_mixerdev
*ms
, unsigned mixch
, unsigned value
)
1732 struct usb_device
*dev
= ms
->state
->usbdev
;
1733 unsigned char data
[2];
1734 struct mixerchannel
*ch
;
1737 if (mixch
>= ms
->numch
)
1739 ch
= &ms
->ch
[mixch
];
1740 v3
= ch
->maxval
- ch
->minval
;
1742 v2
= (value
>> 8) & 0xff;
1747 if (!(ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)))
1749 ch
->value
= v1
| (v2
<< 8);
1750 v1
= (v1
* v3
) / 100 + ch
->minval
;
1751 v2
= (v2
* v3
) / 100 + ch
->minval
;
1752 switch (ch
->selector
) {
1753 case 0: /* mixer unit request */
1756 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1757 (ch
->chnum
<< 8) | 1, ms
->iface
| (ch
->unitid
<< 8), data
, 2, 1000) < 0)
1759 if (!(ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)))
1763 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1764 ((ch
->chnum
+ !!(ch
->flags
& MIXFLG_STEREOIN
)) << 8) | (1 + !!(ch
->flags
& MIXFLG_STEREOOUT
)),
1765 ms
->iface
| (ch
->unitid
<< 8), data
, 2, 1000) < 0)
1769 /* various feature unit controls */
1770 case VOLUME_CONTROL
:
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
, 2, 1000) < 0)
1776 if (!(ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)))
1780 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1781 (ch
->selector
<< 8) | (ch
->chnum
+ 1), ms
->iface
| (ch
->unitid
<< 8), data
, 2, 1000) < 0)
1787 case TREBLE_CONTROL
:
1789 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1790 (ch
->selector
<< 8) | ch
->chnum
, ms
->iface
| (ch
->unitid
<< 8), data
, 1, 1000) < 0)
1792 if (!(ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)))
1795 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1796 (ch
->selector
<< 8) | (ch
->chnum
+ 1), ms
->iface
| (ch
->unitid
<< 8), data
, 1, 1000) < 0)
1806 printk(KERN_ERR
"usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
1807 dev
->devnum
, ms
->iface
, ch
->unitid
, ch
->chnum
, ch
->selector
);
1811 static int get_rec_src(struct usb_mixerdev
*ms
)
1813 struct usb_device
*dev
= ms
->state
->usbdev
;
1814 unsigned int mask
= 0, retmask
= 0;
1819 for (i
= 0; i
< ms
->numch
; i
++) {
1820 if (!ms
->ch
[i
].slctunitid
|| (mask
& (1 << i
)))
1822 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
1823 0, ms
->iface
| (ms
->ch
[i
].slctunitid
<< 8), &buf
, 1, 1000) < 0) {
1825 printk(KERN_ERR
"usbaudio: selector read request device %u if %u unit %u failed\n",
1826 dev
->devnum
, ms
->iface
, ms
->ch
[i
].slctunitid
& 0xff);
1829 for (j
= i
; j
< ms
->numch
; j
++) {
1830 if ((ms
->ch
[i
].slctunitid
^ ms
->ch
[j
].slctunitid
) & 0xff)
1833 if (buf
== (ms
->ch
[j
].slctunitid
>> 8))
1834 retmask
|= 1 << ms
->ch
[j
].osschannel
;
1842 static int set_rec_src(struct usb_mixerdev
*ms
, int srcmask
)
1844 struct usb_device
*dev
= ms
->state
->usbdev
;
1845 unsigned int mask
= 0, smask
, bmask
;
1850 for (i
= 0; i
< ms
->numch
; i
++) {
1851 if (!ms
->ch
[i
].slctunitid
|| (mask
& (1 << i
)))
1853 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
1854 0, ms
->iface
| (ms
->ch
[i
].slctunitid
<< 8), &buf
, 1, 1000) < 0) {
1856 printk(KERN_ERR
"usbaudio: selector read request device %u if %u unit %u failed\n",
1857 dev
->devnum
, ms
->iface
, ms
->ch
[i
].slctunitid
& 0xff);
1860 /* first generate smask */
1862 for (j
= i
; j
< ms
->numch
; j
++) {
1863 if ((ms
->ch
[i
].slctunitid
^ ms
->ch
[j
].slctunitid
) & 0xff)
1865 smask
|= 1 << ms
->ch
[j
].osschannel
;
1866 if (buf
== (ms
->ch
[j
].slctunitid
>> 8))
1867 bmask
|= 1 << ms
->ch
[j
].osschannel
;
1870 /* check for multiple set sources */
1871 j
= hweight32(srcmask
& smask
);
1876 for (j
= i
; j
< ms
->numch
; j
++) {
1877 if ((ms
->ch
[i
].slctunitid
^ ms
->ch
[j
].slctunitid
) & 0xff)
1879 if (!(srcmask
& (1 << ms
->ch
[j
].osschannel
)))
1881 buf
= ms
->ch
[j
].slctunitid
>> 8;
1882 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
1883 0, ms
->iface
| (ms
->ch
[j
].slctunitid
<< 8), &buf
, 1, 1000) < 0) {
1885 printk(KERN_ERR
"usbaudio: selector write request device %u if %u unit %u failed\n",
1886 dev
->devnum
, ms
->iface
, ms
->ch
[j
].slctunitid
& 0xff);
1891 return err
? -EIO
: 0;
1894 /* --------------------------------------------------------------------- */
1897 * should be called with open_sem hold, so that no new processes
1898 * look at the audio device to be destroyed
1901 static void release(struct usb_audio_state
*s
)
1903 struct usb_audiodev
*as
;
1904 struct usb_mixerdev
*ms
;
1912 wake_up(&open_wait
);
1913 while (!list_empty(&s
->audiolist
)) {
1914 as
= list_entry(s
->audiolist
.next
, struct usb_audiodev
, list
);
1915 list_del(&as
->list
);
1918 dmabuf_release(&as
->usbin
.dma
);
1919 dmabuf_release(&as
->usbout
.dma
);
1920 usb_free_urb(as
->usbin
.durb
[0].urb
);
1921 usb_free_urb(as
->usbin
.durb
[1].urb
);
1922 usb_free_urb(as
->usbin
.surb
[0].urb
);
1923 usb_free_urb(as
->usbin
.surb
[1].urb
);
1924 usb_free_urb(as
->usbout
.durb
[0].urb
);
1925 usb_free_urb(as
->usbout
.durb
[1].urb
);
1926 usb_free_urb(as
->usbout
.surb
[0].urb
);
1927 usb_free_urb(as
->usbout
.surb
[1].urb
);
1930 while (!list_empty(&s
->mixerlist
)) {
1931 ms
= list_entry(s
->mixerlist
.next
, struct usb_mixerdev
, list
);
1932 list_del(&ms
->list
);
1938 static inline int prog_dmabuf_in(struct usb_audiodev
*as
)
1941 return dmabuf_init(&as
->usbin
.dma
);
1944 static inline int prog_dmabuf_out(struct usb_audiodev
*as
)
1947 return dmabuf_init(&as
->usbout
.dma
);
1950 /* --------------------------------------------------------------------- */
1952 static int usb_audio_open_mixdev(struct inode
*inode
, struct file
*file
)
1954 unsigned int minor
= iminor(inode
);
1955 struct usb_mixerdev
*ms
;
1956 struct usb_audio_state
*s
;
1959 list_for_each_entry(s
, &audiodevs
, audiodev
) {
1960 list_for_each_entry(ms
, &s
->mixerlist
, list
) {
1961 if (ms
->dev_mixer
== minor
)
1973 file
->private_data
= ms
;
1977 return nonseekable_open(inode
, file
);
1980 static int usb_audio_release_mixdev(struct inode
*inode
, struct file
*file
)
1982 struct usb_mixerdev
*ms
= (struct usb_mixerdev
*)file
->private_data
;
1983 struct usb_audio_state
*s
;
1993 static int usb_audio_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1995 struct usb_mixerdev
*ms
= (struct usb_mixerdev
*)file
->private_data
;
1997 int __user
*user_arg
= (int __user
*)arg
;
1999 if (!ms
->state
->usbdev
)
2002 if (cmd
== SOUND_MIXER_INFO
) {
2005 memset(&info
, 0, sizeof(info
));
2006 strncpy(info
.id
, "USB_AUDIO", sizeof(info
.id
));
2007 strncpy(info
.name
, "USB Audio Class Driver", sizeof(info
.name
));
2008 info
.modify_counter
= ms
->modcnt
;
2009 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
2013 if (cmd
== SOUND_OLD_MIXER_INFO
) {
2014 _old_mixer_info info
;
2016 memset(&info
, 0, sizeof(info
));
2017 strncpy(info
.id
, "USB_AUDIO", sizeof(info
.id
));
2018 strncpy(info
.name
, "USB Audio Class Driver", sizeof(info
.name
));
2019 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
2023 if (cmd
== OSS_GETVERSION
)
2024 return put_user(SOUND_VERSION
, user_arg
);
2025 if (_IOC_TYPE(cmd
) != 'M' || _IOC_SIZE(cmd
) != sizeof(int))
2027 if (_IOC_DIR(cmd
) == _IOC_READ
) {
2028 switch (_IOC_NR(cmd
)) {
2029 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
2030 val
= get_rec_src(ms
);
2033 return put_user(val
, user_arg
);
2035 case SOUND_MIXER_DEVMASK
: /* Arg contains a bit for each supported device */
2036 for (val
= i
= 0; i
< ms
->numch
; i
++)
2037 val
|= 1 << ms
->ch
[i
].osschannel
;
2038 return put_user(val
, user_arg
);
2040 case SOUND_MIXER_RECMASK
: /* Arg contains a bit for each supported recording source */
2041 for (val
= i
= 0; i
< ms
->numch
; i
++)
2042 if (ms
->ch
[i
].slctunitid
)
2043 val
|= 1 << ms
->ch
[i
].osschannel
;
2044 return put_user(val
, user_arg
);
2046 case SOUND_MIXER_STEREODEVS
: /* Mixer channels supporting stereo */
2047 for (val
= i
= 0; i
< ms
->numch
; i
++)
2048 if (ms
->ch
[i
].flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
))
2049 val
|= 1 << ms
->ch
[i
].osschannel
;
2050 return put_user(val
, user_arg
);
2052 case SOUND_MIXER_CAPS
:
2053 return put_user(SOUND_CAP_EXCL_INPUT
, user_arg
);
2057 if (i
>= SOUND_MIXER_NRDEVICES
)
2059 for (j
= 0; j
< ms
->numch
; j
++) {
2060 if (ms
->ch
[j
].osschannel
== i
) {
2061 return put_user(ms
->ch
[j
].value
, user_arg
);
2067 if (_IOC_DIR(cmd
) != (_IOC_READ
|_IOC_WRITE
))
2070 switch (_IOC_NR(cmd
)) {
2071 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
2072 if (get_user(val
, user_arg
))
2074 return set_rec_src(ms
, val
);
2078 if (i
>= SOUND_MIXER_NRDEVICES
)
2080 for (j
= 0; j
< ms
->numch
&& ms
->ch
[j
].osschannel
!= i
; j
++);
2083 if (get_user(val
, user_arg
))
2085 if (wrmixer(ms
, j
, val
))
2087 return put_user(ms
->ch
[j
].value
, user_arg
);
2091 static /*const*/ struct file_operations usb_mixer_fops
= {
2092 .owner
= THIS_MODULE
,
2093 .llseek
= no_llseek
,
2094 .ioctl
= usb_audio_ioctl_mixdev
,
2095 .open
= usb_audio_open_mixdev
,
2096 .release
= usb_audio_release_mixdev
,
2099 /* --------------------------------------------------------------------- */
2101 static int drain_out(struct usb_audiodev
*as
, int nonblock
)
2103 DECLARE_WAITQUEUE(wait
, current
);
2104 unsigned long flags
;
2107 if (as
->usbout
.dma
.mapped
|| !as
->usbout
.dma
.ready
)
2110 add_wait_queue(&as
->usbout
.dma
.wait
, &wait
);
2112 __set_current_state(TASK_INTERRUPTIBLE
);
2113 spin_lock_irqsave(&as
->lock
, flags
);
2114 count
= as
->usbout
.dma
.count
;
2115 spin_unlock_irqrestore(&as
->lock
, flags
);
2118 if (signal_pending(current
))
2121 remove_wait_queue(&as
->usbout
.dma
.wait
, &wait
);
2122 set_current_state(TASK_RUNNING
);
2125 tmo
= 3 * HZ
* count
/ as
->usbout
.dma
.srate
;
2126 tmo
>>= AFMT_BYTESSHIFT(as
->usbout
.dma
.format
);
2127 if (!schedule_timeout(tmo
+ 1)) {
2128 printk(KERN_DEBUG
"usbaudio: dma timed out??\n");
2132 remove_wait_queue(&as
->usbout
.dma
.wait
, &wait
);
2133 set_current_state(TASK_RUNNING
);
2134 if (signal_pending(current
))
2135 return -ERESTARTSYS
;
2139 /* --------------------------------------------------------------------- */
2141 static ssize_t
usb_audio_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2143 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2144 DECLARE_WAITQUEUE(wait
, current
);
2146 unsigned long flags
;
2150 if (as
->usbin
.dma
.mapped
)
2152 if (!as
->usbin
.dma
.ready
&& (ret
= prog_dmabuf_in(as
)))
2154 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2156 add_wait_queue(&as
->usbin
.dma
.wait
, &wait
);
2158 spin_lock_irqsave(&as
->lock
, flags
);
2159 ptr
= as
->usbin
.dma
.rdptr
;
2160 cnt
= as
->usbin
.dma
.count
;
2161 /* set task state early to avoid wakeup races */
2163 __set_current_state(TASK_INTERRUPTIBLE
);
2164 spin_unlock_irqrestore(&as
->lock
, flags
);
2168 if (usbin_start(as
)) {
2173 if (file
->f_flags
& O_NONBLOCK
) {
2179 if (signal_pending(current
)) {
2186 if ((err
= dmabuf_copyout_user(&as
->usbin
.dma
, ptr
, buffer
, cnt
))) {
2192 if (ptr
>= as
->usbin
.dma
.dmasize
)
2193 ptr
-= as
->usbin
.dma
.dmasize
;
2194 spin_lock_irqsave(&as
->lock
, flags
);
2195 as
->usbin
.dma
.rdptr
= ptr
;
2196 as
->usbin
.dma
.count
-= cnt
;
2197 spin_unlock_irqrestore(&as
->lock
, flags
);
2202 __set_current_state(TASK_RUNNING
);
2203 remove_wait_queue(&as
->usbin
.dma
.wait
, &wait
);
2207 static ssize_t
usb_audio_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2209 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2210 DECLARE_WAITQUEUE(wait
, current
);
2212 unsigned long flags
;
2214 unsigned int start_thr
;
2217 if (as
->usbout
.dma
.mapped
)
2219 if (!as
->usbout
.dma
.ready
&& (ret
= prog_dmabuf_out(as
)))
2221 if (!access_ok(VERIFY_READ
, buffer
, count
))
2223 start_thr
= (as
->usbout
.dma
.srate
<< AFMT_BYTESSHIFT(as
->usbout
.dma
.format
)) / (1000 / (3 * DESCFRAMES
));
2224 add_wait_queue(&as
->usbout
.dma
.wait
, &wait
);
2227 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",
2228 count
, as
->usbout
.dma
.count
, as
->usbout
.dma
.rdptr
, as
->usbout
.dma
.wrptr
, as
->usbout
.dma
.dmasize
, as
->usbout
.dma
.fragsize
,
2229 as
->usbout
.flags
, current
->state
);
2231 spin_lock_irqsave(&as
->lock
, flags
);
2232 if (as
->usbout
.dma
.count
< 0) {
2233 as
->usbout
.dma
.count
= 0;
2234 as
->usbout
.dma
.rdptr
= as
->usbout
.dma
.wrptr
;
2236 ptr
= as
->usbout
.dma
.wrptr
;
2237 cnt
= as
->usbout
.dma
.dmasize
- as
->usbout
.dma
.count
;
2238 /* set task state early to avoid wakeup races */
2240 __set_current_state(TASK_INTERRUPTIBLE
);
2241 spin_unlock_irqrestore(&as
->lock
, flags
);
2245 if (usbout_start(as
)) {
2250 if (file
->f_flags
& O_NONBLOCK
) {
2256 if (signal_pending(current
)) {
2263 if ((err
= dmabuf_copyin_user(&as
->usbout
.dma
, ptr
, buffer
, cnt
))) {
2269 if (ptr
>= as
->usbout
.dma
.dmasize
)
2270 ptr
-= as
->usbout
.dma
.dmasize
;
2271 spin_lock_irqsave(&as
->lock
, flags
);
2272 as
->usbout
.dma
.wrptr
= ptr
;
2273 as
->usbout
.dma
.count
+= cnt
;
2274 spin_unlock_irqrestore(&as
->lock
, flags
);
2278 if (as
->usbout
.dma
.count
>= start_thr
&& usbout_start(as
)) {
2284 __set_current_state(TASK_RUNNING
);
2285 remove_wait_queue(&as
->usbout
.dma
.wait
, &wait
);
2289 /* Called without the kernel lock - fine */
2290 static unsigned int usb_audio_poll(struct file
*file
, struct poll_table_struct
*wait
)
2292 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2293 unsigned long flags
;
2294 unsigned int mask
= 0;
2296 if (file
->f_mode
& FMODE_WRITE
) {
2297 if (!as
->usbout
.dma
.ready
)
2298 prog_dmabuf_out(as
);
2299 poll_wait(file
, &as
->usbout
.dma
.wait
, wait
);
2301 if (file
->f_mode
& FMODE_READ
) {
2302 if (!as
->usbin
.dma
.ready
)
2304 poll_wait(file
, &as
->usbin
.dma
.wait
, wait
);
2306 spin_lock_irqsave(&as
->lock
, flags
);
2307 if (file
->f_mode
& FMODE_READ
) {
2308 if (as
->usbin
.dma
.count
>= (signed)as
->usbin
.dma
.fragsize
)
2309 mask
|= POLLIN
| POLLRDNORM
;
2311 if (file
->f_mode
& FMODE_WRITE
) {
2312 if (as
->usbout
.dma
.mapped
) {
2313 if (as
->usbout
.dma
.count
>= (signed)as
->usbout
.dma
.fragsize
)
2314 mask
|= POLLOUT
| POLLWRNORM
;
2316 if ((signed)as
->usbout
.dma
.dmasize
>= as
->usbout
.dma
.count
+ (signed)as
->usbout
.dma
.fragsize
)
2317 mask
|= POLLOUT
| POLLWRNORM
;
2320 spin_unlock_irqrestore(&as
->lock
, flags
);
2324 static int usb_audio_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2326 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2331 if (vma
->vm_flags
& VM_WRITE
) {
2332 if ((ret
= prog_dmabuf_out(as
)) != 0)
2334 db
= &as
->usbout
.dma
;
2335 } else if (vma
->vm_flags
& VM_READ
) {
2336 if ((ret
= prog_dmabuf_in(as
)) != 0)
2338 db
= &as
->usbin
.dma
;
2343 if (vma
->vm_pgoff
!= 0)
2346 ret
= dmabuf_mmap(vma
, db
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
2352 static int usb_audio_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2354 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2355 struct usb_audio_state
*s
= as
->state
;
2356 int __user
*user_arg
= (int __user
*)arg
;
2357 unsigned long flags
;
2358 audio_buf_info abinfo
;
2361 int val2
, mapped
, ret
;
2365 mapped
= ((file
->f_mode
& FMODE_WRITE
) && as
->usbout
.dma
.mapped
) ||
2366 ((file
->f_mode
& FMODE_READ
) && as
->usbin
.dma
.mapped
);
2369 get_user(val
, (int *)arg
);
2370 printk(KERN_DEBUG
"usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd
, arg
, val
)
2373 case OSS_GETVERSION
:
2374 return put_user(SOUND_VERSION
, user_arg
);
2376 case SNDCTL_DSP_SYNC
:
2377 if (file
->f_mode
& FMODE_WRITE
)
2378 return drain_out(as
, 0/*file->f_flags & O_NONBLOCK*/);
2381 case SNDCTL_DSP_SETDUPLEX
:
2384 case SNDCTL_DSP_GETCAPS
:
2385 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
|
2386 DSP_CAP_MMAP
| DSP_CAP_BATCH
, user_arg
);
2388 case SNDCTL_DSP_RESET
:
2389 if (file
->f_mode
& FMODE_WRITE
) {
2391 as
->usbout
.dma
.rdptr
= as
->usbout
.dma
.wrptr
= as
->usbout
.dma
.count
= as
->usbout
.dma
.total_bytes
= 0;
2393 if (file
->f_mode
& FMODE_READ
) {
2395 as
->usbin
.dma
.rdptr
= as
->usbin
.dma
.wrptr
= as
->usbin
.dma
.count
= as
->usbin
.dma
.total_bytes
= 0;
2399 case SNDCTL_DSP_SPEED
:
2400 if (get_user(val
, user_arg
))
2407 if (set_format(as
, file
->f_mode
, AFMT_QUERY
, val
))
2410 return put_user((file
->f_mode
& FMODE_READ
) ?
2411 as
->usbin
.dma
.srate
: as
->usbout
.dma
.srate
,
2414 case SNDCTL_DSP_STEREO
:
2415 if (get_user(val
, user_arg
))
2417 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2419 val2
|= AFMT_STEREO
;
2421 val2
&= ~AFMT_STEREO
;
2422 if (set_format(as
, file
->f_mode
, val2
, 0))
2426 case SNDCTL_DSP_CHANNELS
:
2427 if (get_user(val
, user_arg
))
2430 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2432 val2
&= ~AFMT_STEREO
;
2434 val2
|= AFMT_STEREO
;
2435 if (set_format(as
, file
->f_mode
, val2
, 0))
2438 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2439 return put_user(AFMT_ISSTEREO(val2
) ? 2 : 1, user_arg
);
2441 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
2442 return put_user(AFMT_U8
| AFMT_U16_LE
| AFMT_U16_BE
|
2443 AFMT_S8
| AFMT_S16_LE
| AFMT_S16_BE
, user_arg
);
2445 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
2446 if (get_user(val
, user_arg
))
2448 if (val
!= AFMT_QUERY
) {
2449 if (hweight32(val
) != 1)
2451 if (!(val
& (AFMT_U8
| AFMT_U16_LE
| AFMT_U16_BE
|
2452 AFMT_S8
| AFMT_S16_LE
| AFMT_S16_BE
)))
2454 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2455 val
|= val2
& AFMT_STEREO
;
2456 if (set_format(as
, file
->f_mode
, val
, 0))
2459 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2460 return put_user(val2
& ~AFMT_STEREO
, user_arg
);
2462 case SNDCTL_DSP_POST
:
2465 case SNDCTL_DSP_GETTRIGGER
:
2467 if (file
->f_mode
& FMODE_READ
&& as
->usbin
.flags
& FLG_RUNNING
)
2468 val
|= PCM_ENABLE_INPUT
;
2469 if (file
->f_mode
& FMODE_WRITE
&& as
->usbout
.flags
& FLG_RUNNING
)
2470 val
|= PCM_ENABLE_OUTPUT
;
2471 return put_user(val
, user_arg
);
2473 case SNDCTL_DSP_SETTRIGGER
:
2474 if (get_user(val
, user_arg
))
2476 if (file
->f_mode
& FMODE_READ
) {
2477 if (val
& PCM_ENABLE_INPUT
) {
2478 if (!as
->usbin
.dma
.ready
&& (ret
= prog_dmabuf_in(as
)))
2480 if (usbin_start(as
))
2485 if (file
->f_mode
& FMODE_WRITE
) {
2486 if (val
& PCM_ENABLE_OUTPUT
) {
2487 if (!as
->usbout
.dma
.ready
&& (ret
= prog_dmabuf_out(as
)))
2489 if (usbout_start(as
))
2496 case SNDCTL_DSP_GETOSPACE
:
2497 if (!(file
->f_mode
& FMODE_WRITE
))
2499 if (!(as
->usbout
.flags
& FLG_RUNNING
) && (val
= prog_dmabuf_out(as
)) != 0)
2501 spin_lock_irqsave(&as
->lock
, flags
);
2502 abinfo
.fragsize
= as
->usbout
.dma
.fragsize
;
2503 abinfo
.bytes
= as
->usbout
.dma
.dmasize
- as
->usbout
.dma
.count
;
2504 abinfo
.fragstotal
= as
->usbout
.dma
.numfrag
;
2505 abinfo
.fragments
= abinfo
.bytes
>> as
->usbout
.dma
.fragshift
;
2506 spin_unlock_irqrestore(&as
->lock
, flags
);
2507 return copy_to_user((void __user
*)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2509 case SNDCTL_DSP_GETISPACE
:
2510 if (!(file
->f_mode
& FMODE_READ
))
2512 if (!(as
->usbin
.flags
& FLG_RUNNING
) && (val
= prog_dmabuf_in(as
)) != 0)
2514 spin_lock_irqsave(&as
->lock
, flags
);
2515 abinfo
.fragsize
= as
->usbin
.dma
.fragsize
;
2516 abinfo
.bytes
= as
->usbin
.dma
.count
;
2517 abinfo
.fragstotal
= as
->usbin
.dma
.numfrag
;
2518 abinfo
.fragments
= abinfo
.bytes
>> as
->usbin
.dma
.fragshift
;
2519 spin_unlock_irqrestore(&as
->lock
, flags
);
2520 return copy_to_user((void __user
*)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2522 case SNDCTL_DSP_NONBLOCK
:
2523 file
->f_flags
|= O_NONBLOCK
;
2526 case SNDCTL_DSP_GETODELAY
:
2527 if (!(file
->f_mode
& FMODE_WRITE
))
2529 spin_lock_irqsave(&as
->lock
, flags
);
2530 val
= as
->usbout
.dma
.count
;
2531 spin_unlock_irqrestore(&as
->lock
, flags
);
2532 return put_user(val
, user_arg
);
2534 case SNDCTL_DSP_GETIPTR
:
2535 if (!(file
->f_mode
& FMODE_READ
))
2537 spin_lock_irqsave(&as
->lock
, flags
);
2538 cinfo
.bytes
= as
->usbin
.dma
.total_bytes
;
2539 cinfo
.blocks
= as
->usbin
.dma
.count
>> as
->usbin
.dma
.fragshift
;
2540 cinfo
.ptr
= as
->usbin
.dma
.wrptr
;
2541 if (as
->usbin
.dma
.mapped
)
2542 as
->usbin
.dma
.count
&= as
->usbin
.dma
.fragsize
-1;
2543 spin_unlock_irqrestore(&as
->lock
, flags
);
2544 if (copy_to_user((void __user
*)arg
, &cinfo
, sizeof(cinfo
)))
2548 case SNDCTL_DSP_GETOPTR
:
2549 if (!(file
->f_mode
& FMODE_WRITE
))
2551 spin_lock_irqsave(&as
->lock
, flags
);
2552 cinfo
.bytes
= as
->usbout
.dma
.total_bytes
;
2553 cinfo
.blocks
= as
->usbout
.dma
.count
>> as
->usbout
.dma
.fragshift
;
2554 cinfo
.ptr
= as
->usbout
.dma
.rdptr
;
2555 if (as
->usbout
.dma
.mapped
)
2556 as
->usbout
.dma
.count
&= as
->usbout
.dma
.fragsize
-1;
2557 spin_unlock_irqrestore(&as
->lock
, flags
);
2558 if (copy_to_user((void __user
*)arg
, &cinfo
, sizeof(cinfo
)))
2562 case SNDCTL_DSP_GETBLKSIZE
:
2563 if (file
->f_mode
& FMODE_WRITE
) {
2564 if ((val
= prog_dmabuf_out(as
)))
2566 return put_user(as
->usbout
.dma
.fragsize
, user_arg
);
2568 if ((val
= prog_dmabuf_in(as
)))
2570 return put_user(as
->usbin
.dma
.fragsize
, user_arg
);
2572 case SNDCTL_DSP_SETFRAGMENT
:
2573 if (get_user(val
, user_arg
))
2575 if (file
->f_mode
& FMODE_READ
) {
2576 as
->usbin
.dma
.ossfragshift
= val
& 0xffff;
2577 as
->usbin
.dma
.ossmaxfrags
= (val
>> 16) & 0xffff;
2578 if (as
->usbin
.dma
.ossfragshift
< 4)
2579 as
->usbin
.dma
.ossfragshift
= 4;
2580 if (as
->usbin
.dma
.ossfragshift
> 15)
2581 as
->usbin
.dma
.ossfragshift
= 15;
2582 if (as
->usbin
.dma
.ossmaxfrags
< 4)
2583 as
->usbin
.dma
.ossmaxfrags
= 4;
2585 if (file
->f_mode
& FMODE_WRITE
) {
2586 as
->usbout
.dma
.ossfragshift
= val
& 0xffff;
2587 as
->usbout
.dma
.ossmaxfrags
= (val
>> 16) & 0xffff;
2588 if (as
->usbout
.dma
.ossfragshift
< 4)
2589 as
->usbout
.dma
.ossfragshift
= 4;
2590 if (as
->usbout
.dma
.ossfragshift
> 15)
2591 as
->usbout
.dma
.ossfragshift
= 15;
2592 if (as
->usbout
.dma
.ossmaxfrags
< 4)
2593 as
->usbout
.dma
.ossmaxfrags
= 4;
2597 case SNDCTL_DSP_SUBDIVIDE
:
2598 if ((file
->f_mode
& FMODE_READ
&& as
->usbin
.dma
.subdivision
) ||
2599 (file
->f_mode
& FMODE_WRITE
&& as
->usbout
.dma
.subdivision
))
2601 if (get_user(val
, user_arg
))
2603 if (val
!= 1 && val
!= 2 && val
!= 4)
2605 if (file
->f_mode
& FMODE_READ
)
2606 as
->usbin
.dma
.subdivision
= val
;
2607 if (file
->f_mode
& FMODE_WRITE
)
2608 as
->usbout
.dma
.subdivision
= val
;
2611 case SOUND_PCM_READ_RATE
:
2612 return put_user((file
->f_mode
& FMODE_READ
) ?
2613 as
->usbin
.dma
.srate
: as
->usbout
.dma
.srate
,
2616 case SOUND_PCM_READ_CHANNELS
:
2617 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2618 return put_user(AFMT_ISSTEREO(val2
) ? 2 : 1, user_arg
);
2620 case SOUND_PCM_READ_BITS
:
2621 val2
= (file
->f_mode
& FMODE_READ
) ? as
->usbin
.dma
.format
: as
->usbout
.dma
.format
;
2622 return put_user(AFMT_IS16BIT(val2
) ? 16 : 8, user_arg
);
2624 case SOUND_PCM_WRITE_FILTER
:
2625 case SNDCTL_DSP_SETSYNCRO
:
2626 case SOUND_PCM_READ_FILTER
:
2629 dprintk((KERN_DEBUG
"usbaudio: usb_audio_ioctl - no command found\n"));
2630 return -ENOIOCTLCMD
;
2633 static int usb_audio_open(struct inode
*inode
, struct file
*file
)
2635 unsigned int minor
= iminor(inode
);
2636 DECLARE_WAITQUEUE(wait
, current
);
2637 struct usb_audiodev
*as
;
2638 struct usb_audio_state
*s
;
2642 list_for_each_entry(s
, &audiodevs
, audiodev
) {
2643 list_for_each_entry(as
, &s
->audiolist
, list
) {
2644 if (!((as
->dev_audio
^ minor
) & ~0xf))
2656 /* wait for device to become free */
2657 if (!(as
->open_mode
& file
->f_mode
))
2659 if (file
->f_flags
& O_NONBLOCK
) {
2663 __set_current_state(TASK_INTERRUPTIBLE
);
2664 add_wait_queue(&open_wait
, &wait
);
2667 __set_current_state(TASK_RUNNING
);
2668 remove_wait_queue(&open_wait
, &wait
);
2669 if (signal_pending(current
))
2670 return -ERESTARTSYS
;
2672 if (file
->f_mode
& FMODE_READ
)
2673 as
->usbin
.dma
.ossfragshift
= as
->usbin
.dma
.ossmaxfrags
= as
->usbin
.dma
.subdivision
= 0;
2674 if (file
->f_mode
& FMODE_WRITE
)
2675 as
->usbout
.dma
.ossfragshift
= as
->usbout
.dma
.ossmaxfrags
= as
->usbout
.dma
.subdivision
= 0;
2676 if (set_format(as
, file
->f_mode
, ((minor
& 0xf) == SND_DEV_DSP16
) ? AFMT_S16_LE
: AFMT_U8
/* AFMT_ULAW */, 8000)) {
2680 file
->private_data
= as
;
2681 as
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
2684 return nonseekable_open(inode
, file
);
2687 static int usb_audio_release(struct inode
*inode
, struct file
*file
)
2689 struct usb_audiodev
*as
= (struct usb_audiodev
*)file
->private_data
;
2690 struct usb_audio_state
*s
;
2691 struct usb_device
*dev
;
2696 if (file
->f_mode
& FMODE_WRITE
)
2697 drain_out(as
, file
->f_flags
& O_NONBLOCK
);
2699 if (file
->f_mode
& FMODE_WRITE
) {
2701 if (dev
&& as
->usbout
.interface
>= 0)
2702 usb_set_interface(dev
, as
->usbout
.interface
, 0);
2703 dmabuf_release(&as
->usbout
.dma
);
2706 if (file
->f_mode
& FMODE_READ
) {
2708 if (dev
&& as
->usbin
.interface
>= 0)
2709 usb_set_interface(dev
, as
->usbin
.interface
, 0);
2710 dmabuf_release(&as
->usbin
.dma
);
2713 as
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
2715 wake_up(&open_wait
);
2720 static /*const*/ struct file_operations usb_audio_fops
= {
2721 .owner
= THIS_MODULE
,
2722 .llseek
= no_llseek
,
2723 .read
= usb_audio_read
,
2724 .write
= usb_audio_write
,
2725 .poll
= usb_audio_poll
,
2726 .ioctl
= usb_audio_ioctl
,
2727 .mmap
= usb_audio_mmap
,
2728 .open
= usb_audio_open
,
2729 .release
= usb_audio_release
,
2732 /* --------------------------------------------------------------------- */
2734 static int usb_audio_probe(struct usb_interface
*iface
,
2735 const struct usb_device_id
*id
);
2736 static void usb_audio_disconnect(struct usb_interface
*iface
);
2738 static struct usb_device_id usb_audio_ids
[] = {
2739 { .match_flags
= (USB_DEVICE_ID_MATCH_INT_CLASS
| USB_DEVICE_ID_MATCH_INT_SUBCLASS
),
2740 .bInterfaceClass
= USB_CLASS_AUDIO
, .bInterfaceSubClass
= 1},
2741 { } /* Terminating entry */
2744 MODULE_DEVICE_TABLE (usb
, usb_audio_ids
);
2746 static struct usb_driver usb_audio_driver
= {
2747 .owner
= THIS_MODULE
,
2749 .probe
= usb_audio_probe
,
2750 .disconnect
= usb_audio_disconnect
,
2751 .id_table
= usb_audio_ids
,
2754 static void *find_descriptor(void *descstart
, unsigned int desclen
, void *after
,
2755 u8 dtype
, int iface
, int altsetting
)
2758 int ifc
= -1, as
= -1;
2768 if (p
[1] == USB_DT_INTERFACE
) {
2769 /* minimum length of interface descriptor */
2775 if (p
[1] == dtype
&& (!after
|| (void *)p
> after
) &&
2776 (iface
== -1 || iface
== ifc
) && (altsetting
== -1 || altsetting
== as
)) {
2784 static void *find_csinterface_descriptor(void *descstart
, unsigned int desclen
, void *after
, u8 dsubtype
, int iface
, int altsetting
)
2788 p
= find_descriptor(descstart
, desclen
, after
, USB_DT_CS_INTERFACE
, iface
, altsetting
);
2790 if (p
[0] >= 3 && p
[2] == dsubtype
)
2792 p
= find_descriptor(descstart
, desclen
, p
, USB_DT_CS_INTERFACE
, iface
, altsetting
);
2797 static void *find_audiocontrol_unit(void *descstart
, unsigned int desclen
, void *after
, u8 unit
, int iface
)
2801 p
= find_descriptor(descstart
, desclen
, after
, USB_DT_CS_INTERFACE
, iface
, -1);
2803 if (p
[0] >= 4 && p
[2] >= INPUT_TERMINAL
&& p
[2] <= EXTENSION_UNIT
&& p
[3] == unit
)
2805 p
= find_descriptor(descstart
, desclen
, p
, USB_DT_CS_INTERFACE
, iface
, -1);
2810 static void usb_audio_parsestreaming(struct usb_audio_state
*s
, unsigned char *buffer
, unsigned int buflen
, int asifin
, int asifout
)
2812 struct usb_device
*dev
= s
->usbdev
;
2813 struct usb_audiodev
*as
;
2814 struct usb_host_interface
*alts
;
2815 struct usb_interface
*iface
;
2816 struct audioformat
*fp
;
2817 unsigned char *fmt
, *csep
;
2818 unsigned int i
, j
, k
, format
, idx
;
2820 if (!(as
= kmalloc(sizeof(struct usb_audiodev
), GFP_KERNEL
)))
2822 memset(as
, 0, sizeof(struct usb_audiodev
));
2823 init_waitqueue_head(&as
->usbin
.dma
.wait
);
2824 init_waitqueue_head(&as
->usbout
.dma
.wait
);
2825 spin_lock_init(&as
->lock
);
2826 as
->usbin
.durb
[0].urb
= usb_alloc_urb (DESCFRAMES
, GFP_KERNEL
);
2827 as
->usbin
.durb
[1].urb
= usb_alloc_urb (DESCFRAMES
, GFP_KERNEL
);
2828 as
->usbin
.surb
[0].urb
= usb_alloc_urb (SYNCFRAMES
, GFP_KERNEL
);
2829 as
->usbin
.surb
[1].urb
= usb_alloc_urb (SYNCFRAMES
, GFP_KERNEL
);
2830 as
->usbout
.durb
[0].urb
= usb_alloc_urb (DESCFRAMES
, GFP_KERNEL
);
2831 as
->usbout
.durb
[1].urb
= usb_alloc_urb (DESCFRAMES
, GFP_KERNEL
);
2832 as
->usbout
.surb
[0].urb
= usb_alloc_urb (SYNCFRAMES
, GFP_KERNEL
);
2833 as
->usbout
.surb
[1].urb
= usb_alloc_urb (SYNCFRAMES
, GFP_KERNEL
);
2834 if ((!as
->usbin
.durb
[0].urb
) ||
2835 (!as
->usbin
.durb
[1].urb
) ||
2836 (!as
->usbin
.surb
[0].urb
) ||
2837 (!as
->usbin
.surb
[1].urb
) ||
2838 (!as
->usbout
.durb
[0].urb
) ||
2839 (!as
->usbout
.durb
[1].urb
) ||
2840 (!as
->usbout
.surb
[0].urb
) ||
2841 (!as
->usbout
.surb
[1].urb
)) {
2842 usb_free_urb(as
->usbin
.durb
[0].urb
);
2843 usb_free_urb(as
->usbin
.durb
[1].urb
);
2844 usb_free_urb(as
->usbin
.surb
[0].urb
);
2845 usb_free_urb(as
->usbin
.surb
[1].urb
);
2846 usb_free_urb(as
->usbout
.durb
[0].urb
);
2847 usb_free_urb(as
->usbout
.durb
[1].urb
);
2848 usb_free_urb(as
->usbout
.surb
[0].urb
);
2849 usb_free_urb(as
->usbout
.surb
[1].urb
);
2854 as
->usbin
.interface
= asifin
;
2855 as
->usbout
.interface
= asifout
;
2856 /* search for input formats */
2858 as
->usbin
.flags
= FLG_CONNECTED
;
2859 iface
= usb_ifnum_to_if(dev
, asifin
);
2860 for (idx
= 0; idx
< iface
->num_altsetting
; idx
++) {
2861 alts
= &iface
->altsetting
[idx
];
2862 i
= alts
->desc
.bAlternateSetting
;
2863 if (alts
->desc
.bInterfaceClass
!= USB_CLASS_AUDIO
|| alts
->desc
.bInterfaceSubClass
!= 2)
2865 if (alts
->desc
.bNumEndpoints
< 1) {
2866 if (i
!= 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */
2867 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2868 dev
->devnum
, asifin
, i
);
2872 if ((alts
->endpoint
[0].desc
.bmAttributes
& 0x03) != 0x01 ||
2873 !(alts
->endpoint
[0].desc
.bEndpointAddress
& 0x80)) {
2874 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n",
2875 dev
->devnum
, asifin
, i
);
2878 fmt
= find_csinterface_descriptor(buffer
, buflen
, NULL
, AS_GENERAL
, asifin
, i
);
2880 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2881 dev
->devnum
, asifin
, i
);
2884 if (fmt
[0] < 7 || fmt
[6] != 0 || (fmt
[5] != 1 && fmt
[5] != 2)) {
2885 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u format not supported\n",
2886 dev
->devnum
, asifin
, i
);
2889 format
= (fmt
[5] == 2) ? (AFMT_U16_LE
| AFMT_U8
) : (AFMT_S16_LE
| AFMT_S8
);
2890 fmt
= find_csinterface_descriptor(buffer
, buflen
, NULL
, FORMAT_TYPE
, asifin
, i
);
2892 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2893 dev
->devnum
, asifin
, i
);
2896 if (fmt
[0] < 8+3*(fmt
[7] ? fmt
[7] : 2) || fmt
[3] != 1) {
2897 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2898 dev
->devnum
, asifin
, i
);
2901 if (fmt
[4] < 1 || fmt
[4] > 2 || fmt
[5] < 1 || fmt
[5] > 2) {
2902 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2903 dev
->devnum
, asifin
, i
, fmt
[4], fmt
[5]);
2906 csep
= find_descriptor(buffer
, buflen
, NULL
, USB_DT_CS_ENDPOINT
, asifin
, i
);
2907 if (!csep
|| csep
[0] < 7 || csep
[2] != EP_GENERAL
) {
2908 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2909 dev
->devnum
, asifin
, i
);
2912 if (as
->numfmtin
>= MAXFORMATS
)
2914 fp
= &as
->fmtin
[as
->numfmtin
++];
2916 format
&= (AFMT_U16_LE
| AFMT_S16_LE
);
2918 format
&= (AFMT_U8
| AFMT_S8
);
2920 format
|= AFMT_STEREO
;
2921 fp
->format
= format
;
2923 fp
->sratelo
= fp
->sratehi
= fmt
[8] | (fmt
[9] << 8) | (fmt
[10] << 16);
2924 printk(KERN_INFO
"usbaudio: valid input sample rate %u\n", fp
->sratelo
);
2925 for (j
= fmt
[7] ? (fmt
[7]-1) : 1; j
> 0; j
--) {
2926 k
= fmt
[8+3*j
] | (fmt
[9+3*j
] << 8) | (fmt
[10+3*j
] << 16);
2927 printk(KERN_INFO
"usbaudio: valid input sample rate %u\n", k
);
2928 if (k
> fp
->sratehi
)
2930 if (k
< fp
->sratelo
)
2933 fp
->attributes
= csep
[3];
2934 printk(KERN_INFO
"usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
2935 dev
->devnum
, asifin
, i
, fp
->format
, fp
->sratelo
, fp
->sratehi
, fp
->attributes
);
2938 /* search for output formats */
2940 as
->usbout
.flags
= FLG_CONNECTED
;
2941 iface
= usb_ifnum_to_if(dev
, asifout
);
2942 for (idx
= 0; idx
< iface
->num_altsetting
; idx
++) {
2943 alts
= &iface
->altsetting
[idx
];
2944 i
= alts
->desc
.bAlternateSetting
;
2945 if (alts
->desc
.bInterfaceClass
!= USB_CLASS_AUDIO
|| alts
->desc
.bInterfaceSubClass
!= 2)
2947 if (alts
->desc
.bNumEndpoints
< 1) {
2948 /* altsetting 0 should never have iso EPs */
2950 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2951 dev
->devnum
, asifout
, i
);
2954 if ((alts
->endpoint
[0].desc
.bmAttributes
& 0x03) != 0x01 ||
2955 (alts
->endpoint
[0].desc
.bEndpointAddress
& 0x80)) {
2956 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n",
2957 dev
->devnum
, asifout
, i
);
2960 /* See USB audio formats manual, section 2 */
2961 fmt
= find_csinterface_descriptor(buffer
, buflen
, NULL
, AS_GENERAL
, asifout
, i
);
2963 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2964 dev
->devnum
, asifout
, i
);
2967 if (fmt
[0] < 7 || fmt
[6] != 0 || (fmt
[5] != 1 && fmt
[5] != 2)) {
2968 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u format not supported\n",
2969 dev
->devnum
, asifout
, i
);
2972 format
= (fmt
[5] == 2) ? (AFMT_U16_LE
| AFMT_U8
) : (AFMT_S16_LE
| AFMT_S8
);
2973 /* Dallas DS4201 workaround */
2974 if (le16_to_cpu(dev
->descriptor
.idVendor
) == 0x04fa &&
2975 le16_to_cpu(dev
->descriptor
.idProduct
) == 0x4201)
2976 format
= (AFMT_S16_LE
| AFMT_S8
);
2977 fmt
= find_csinterface_descriptor(buffer
, buflen
, NULL
, FORMAT_TYPE
, asifout
, i
);
2979 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2980 dev
->devnum
, asifout
, i
);
2983 if (fmt
[0] < 8+3*(fmt
[7] ? fmt
[7] : 2) || fmt
[3] != 1) {
2984 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2985 dev
->devnum
, asifout
, i
);
2988 if (fmt
[4] < 1 || fmt
[4] > 2 || fmt
[5] < 1 || fmt
[5] > 2) {
2989 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2990 dev
->devnum
, asifout
, i
, fmt
[4], fmt
[5]);
2993 csep
= find_descriptor(buffer
, buflen
, NULL
, USB_DT_CS_ENDPOINT
, asifout
, i
);
2994 if (!csep
|| csep
[0] < 7 || csep
[2] != EP_GENERAL
) {
2995 printk(KERN_ERR
"usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2996 dev
->devnum
, asifout
, i
);
2999 if (as
->numfmtout
>= MAXFORMATS
)
3001 fp
= &as
->fmtout
[as
->numfmtout
++];
3003 format
&= (AFMT_U16_LE
| AFMT_S16_LE
);
3005 format
&= (AFMT_U8
| AFMT_S8
);
3007 format
|= AFMT_STEREO
;
3008 fp
->format
= format
;
3010 fp
->sratelo
= fp
->sratehi
= fmt
[8] | (fmt
[9] << 8) | (fmt
[10] << 16);
3011 printk(KERN_INFO
"usbaudio: valid output sample rate %u\n", fp
->sratelo
);
3012 for (j
= fmt
[7] ? (fmt
[7]-1) : 1; j
> 0; j
--) {
3013 k
= fmt
[8+3*j
] | (fmt
[9+3*j
] << 8) | (fmt
[10+3*j
] << 16);
3014 printk(KERN_INFO
"usbaudio: valid output sample rate %u\n", k
);
3015 if (k
> fp
->sratehi
)
3017 if (k
< fp
->sratelo
)
3020 fp
->attributes
= csep
[3];
3021 printk(KERN_INFO
"usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
3022 dev
->devnum
, asifout
, i
, fp
->format
, fp
->sratelo
, fp
->sratehi
, fp
->attributes
);
3025 if (as
->numfmtin
== 0 && as
->numfmtout
== 0) {
3026 usb_free_urb(as
->usbin
.durb
[0].urb
);
3027 usb_free_urb(as
->usbin
.durb
[1].urb
);
3028 usb_free_urb(as
->usbin
.surb
[0].urb
);
3029 usb_free_urb(as
->usbin
.surb
[1].urb
);
3030 usb_free_urb(as
->usbout
.durb
[0].urb
);
3031 usb_free_urb(as
->usbout
.durb
[1].urb
);
3032 usb_free_urb(as
->usbout
.surb
[0].urb
);
3033 usb_free_urb(as
->usbout
.surb
[1].urb
);
3037 if ((as
->dev_audio
= register_sound_dsp(&usb_audio_fops
, -1)) < 0) {
3038 printk(KERN_ERR
"usbaudio: cannot register dsp\n");
3039 usb_free_urb(as
->usbin
.durb
[0].urb
);
3040 usb_free_urb(as
->usbin
.durb
[1].urb
);
3041 usb_free_urb(as
->usbin
.surb
[0].urb
);
3042 usb_free_urb(as
->usbin
.surb
[1].urb
);
3043 usb_free_urb(as
->usbout
.durb
[0].urb
);
3044 usb_free_urb(as
->usbout
.durb
[1].urb
);
3045 usb_free_urb(as
->usbout
.surb
[0].urb
);
3046 usb_free_urb(as
->usbout
.surb
[1].urb
);
3050 printk(KERN_INFO
"usbaudio: registered dsp 14,%d\n", as
->dev_audio
);
3051 /* everything successful */
3052 list_add_tail(&as
->list
, &s
->audiolist
);
3055 struct consmixstate
{
3056 struct usb_audio_state
*s
;
3057 unsigned char *buffer
;
3058 unsigned int buflen
;
3059 unsigned int ctrlif
;
3060 struct mixerchannel mixch
[SOUND_MIXER_NRDEVICES
];
3061 unsigned int nrmixch
;
3062 unsigned int mixchmask
;
3063 unsigned long unitbitmap
[32/sizeof(unsigned long)];
3065 unsigned int nrchannels
;
3066 unsigned int termtype
;
3067 unsigned int chconfig
;
3070 static struct mixerchannel
*getmixchannel(struct consmixstate
*state
, unsigned int nr
)
3072 struct mixerchannel
*c
;
3074 if (nr
>= SOUND_MIXER_NRDEVICES
) {
3075 printk(KERN_ERR
"usbaudio: invalid OSS mixer channel %u\n", nr
);
3078 if (!(state
->mixchmask
& (1 << nr
))) {
3079 printk(KERN_WARNING
"usbaudio: OSS mixer channel %u already in use\n", nr
);
3082 c
= &state
->mixch
[state
->nrmixch
++];
3084 state
->mixchmask
&= ~(1 << nr
);
3088 static unsigned int getvolchannel(struct consmixstate
*state
)
3092 if ((state
->termtype
& 0xff00) == 0x0000 && (state
->mixchmask
& SOUND_MASK_VOLUME
))
3093 return SOUND_MIXER_VOLUME
;
3094 if ((state
->termtype
& 0xff00) == 0x0100) {
3095 if (state
->mixchmask
& SOUND_MASK_PCM
)
3096 return SOUND_MIXER_PCM
;
3097 if (state
->mixchmask
& SOUND_MASK_ALTPCM
)
3098 return SOUND_MIXER_ALTPCM
;
3100 if ((state
->termtype
& 0xff00) == 0x0200 && (state
->mixchmask
& SOUND_MASK_MIC
))
3101 return SOUND_MIXER_MIC
;
3102 if ((state
->termtype
& 0xff00) == 0x0300 && (state
->mixchmask
& SOUND_MASK_SPEAKER
))
3103 return SOUND_MIXER_SPEAKER
;
3104 if ((state
->termtype
& 0xff00) == 0x0500) {
3105 if (state
->mixchmask
& SOUND_MASK_PHONEIN
)
3106 return SOUND_MIXER_PHONEIN
;
3107 if (state
->mixchmask
& SOUND_MASK_PHONEOUT
)
3108 return SOUND_MIXER_PHONEOUT
;
3110 if (state
->termtype
>= 0x710 && state
->termtype
<= 0x711 && (state
->mixchmask
& SOUND_MASK_RADIO
))
3111 return SOUND_MIXER_RADIO
;
3112 if (state
->termtype
>= 0x709 && state
->termtype
<= 0x70f && (state
->mixchmask
& SOUND_MASK_VIDEO
))
3113 return SOUND_MIXER_VIDEO
;
3114 u
= ffs(state
->mixchmask
& (SOUND_MASK_LINE
| SOUND_MASK_CD
| SOUND_MASK_LINE1
| SOUND_MASK_LINE2
| SOUND_MASK_LINE3
|
3115 SOUND_MASK_DIGITAL1
| SOUND_MASK_DIGITAL2
| SOUND_MASK_DIGITAL3
));
3119 static void prepmixch(struct consmixstate
*state
)
3121 struct usb_device
*dev
= state
->s
->usbdev
;
3122 struct mixerchannel
*ch
;
3125 unsigned int v2
, v3
;
3127 if (!state
->nrmixch
|| state
->nrmixch
> SOUND_MIXER_NRDEVICES
)
3129 buf
= kmalloc(sizeof(*buf
) * 2, GFP_KERNEL
);
3131 printk(KERN_ERR
"prepmixch: out of memory\n") ;
3135 ch
= &state
->mixch
[state
->nrmixch
-1];
3136 switch (ch
->selector
) {
3137 case 0: /* mixer unit request */
3138 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MIN
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3139 (ch
->chnum
<< 8) | 1, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3141 ch
->minval
= buf
[0] | (buf
[1] << 8);
3142 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MAX
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3143 (ch
->chnum
<< 8) | 1, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3145 ch
->maxval
= buf
[0] | (buf
[1] << 8);
3146 v2
= ch
->maxval
- ch
->minval
;
3149 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3150 (ch
->chnum
<< 8) | 1, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3152 v1
= buf
[0] | (buf
[1] << 8);
3153 v3
= v1
- ch
->minval
;
3158 if (ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)) {
3159 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3160 ((ch
->chnum
+ !!(ch
->flags
& MIXFLG_STEREOIN
)) << 8) | (1 + !!(ch
->flags
& MIXFLG_STEREOOUT
)),
3161 state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3163 v1
= buf
[0] | (buf
[1] << 8);
3164 v3
= v1
- ch
->minval
;
3169 ch
->value
|= v3
<< 8;
3172 /* various feature unit controls */
3173 case VOLUME_CONTROL
:
3174 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MIN
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3175 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3177 ch
->minval
= buf
[0] | (buf
[1] << 8);
3178 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MAX
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3179 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3181 ch
->maxval
= buf
[0] | (buf
[1] << 8);
3182 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3183 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3185 v1
= buf
[0] | (buf
[1] << 8);
3186 v2
= ch
->maxval
- ch
->minval
;
3187 v3
= v1
- ch
->minval
;
3194 if (ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)) {
3195 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3196 (ch
->selector
<< 8) | (ch
->chnum
+ 1), state
->ctrlif
| (ch
->unitid
<< 8), buf
, 2, 1000) < 0)
3198 v1
= buf
[0] | (buf
[1] << 8);
3199 v3
= v1
- ch
->minval
;
3204 ch
->value
|= v3
<< 8;
3209 case TREBLE_CONTROL
:
3210 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MIN
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3211 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 1, 1000) < 0)
3213 ch
->minval
= buf
[0] << 8;
3214 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_MAX
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3215 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 1, 1000) < 0)
3217 ch
->maxval
= buf
[0] << 8;
3218 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3219 (ch
->selector
<< 8) | ch
->chnum
, state
->ctrlif
| (ch
->unitid
<< 8), buf
, 1, 1000) < 0)
3222 v2
= ch
->maxval
- ch
->minval
;
3223 v3
= v1
- ch
->minval
;
3230 if (ch
->flags
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)) {
3231 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), GET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_IN
,
3232 (ch
->selector
<< 8) | (ch
->chnum
+ 1), state
->ctrlif
| (ch
->unitid
<< 8), buf
, 1, 1000) < 0)
3235 v3
= v1
- ch
->minval
;
3240 ch
->value
|= v3
<< 8;
3251 printk(KERN_ERR
"usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
3252 dev
->devnum
, state
->ctrlif
, ch
->unitid
, ch
->chnum
, ch
->selector
);
3259 static void usb_audio_recurseunit(struct consmixstate
*state
, unsigned char unitid
);
3261 static inline int checkmixbmap(unsigned char *bmap
, unsigned char flg
, unsigned int inidx
, unsigned int numoch
)
3266 if (!(bmap
[-(idx
>> 3)] & (0x80 >> (idx
& 7))))
3268 if (!(flg
& (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
)))
3270 idx
= (inidx
+!!(flg
& MIXFLG_STEREOIN
))*numoch
+!!(flg
& MIXFLG_STEREOOUT
);
3271 if (!(bmap
[-(idx
>> 3)] & (0x80 >> (idx
& 7))))
3276 static void usb_audio_mixerunit(struct consmixstate
*state
, unsigned char *mixer
)
3278 unsigned int nroutch
= mixer
[5+mixer
[4]];
3279 unsigned int chidx
[SOUND_MIXER_NRDEVICES
+1];
3280 unsigned int termt
[SOUND_MIXER_NRDEVICES
];
3281 unsigned char flg
= (nroutch
>= 2) ? MIXFLG_STEREOOUT
: 0;
3282 unsigned char *bmap
= &mixer
[9+mixer
[4]];
3283 unsigned int bmapsize
;
3284 struct mixerchannel
*ch
;
3288 printk(KERN_ERR
"usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer
[3]);
3291 if (mixer
[4] > SOUND_MIXER_NRDEVICES
) {
3292 printk(KERN_ERR
"usbaudio: mixer unit %u: too many input pins\n", mixer
[3]);
3296 for (i
= 0; i
< mixer
[4]; i
++) {
3297 usb_audio_recurseunit(state
, mixer
[5+i
]);
3298 chidx
[i
+1] = chidx
[i
] + state
->nrchannels
;
3299 termt
[i
] = state
->termtype
;
3301 state
->termtype
= 0;
3302 state
->chconfig
= mixer
[6+mixer
[4]] | (mixer
[7+mixer
[4]] << 8);
3303 bmapsize
= (nroutch
* chidx
[mixer
[4]] + 7) >> 3;
3304 bmap
+= bmapsize
- 1;
3305 if (mixer
[0] < 10+mixer
[4]+bmapsize
) {
3306 printk(KERN_ERR
"usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer
[3]);
3309 for (i
= 0; i
< mixer
[4]; i
++) {
3310 state
->termtype
= termt
[i
];
3311 if (chidx
[i
+1]-chidx
[i
] >= 2) {
3312 flg
|= MIXFLG_STEREOIN
;
3313 if (checkmixbmap(bmap
, flg
, chidx
[i
], nroutch
)) {
3314 ch
= getmixchannel(state
, getvolchannel(state
));
3316 ch
->unitid
= mixer
[3];
3318 ch
->chnum
= chidx
[i
]+1;
3325 flg
&= ~MIXFLG_STEREOIN
;
3326 if (checkmixbmap(bmap
, flg
, chidx
[i
], nroutch
)) {
3327 ch
= getmixchannel(state
, getvolchannel(state
));
3329 ch
->unitid
= mixer
[3];
3331 ch
->chnum
= chidx
[i
]+1;
3337 state
->termtype
= 0;
3340 static struct mixerchannel
*slctsrc_findunit(struct consmixstate
*state
, __u8 unitid
)
3344 for (i
= 0; i
< state
->nrmixch
; i
++)
3345 if (state
->mixch
[i
].unitid
== unitid
)
3346 return &state
->mixch
[i
];
3350 static void usb_audio_selectorunit(struct consmixstate
*state
, unsigned char *selector
)
3352 unsigned int chnum
, i
, mixch
;
3353 struct mixerchannel
*mch
;
3356 printk(KERN_ERR
"usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector
[3]);
3359 mixch
= state
->nrmixch
;
3360 usb_audio_recurseunit(state
, selector
[5]);
3361 if (state
->nrmixch
!= mixch
) {
3362 mch
= &state
->mixch
[state
->nrmixch
-1];
3363 mch
->slctunitid
= selector
[3] | (1 << 8);
3364 } else if ((mch
= slctsrc_findunit(state
, selector
[5]))) {
3365 mch
->slctunitid
= selector
[3] | (1 << 8);
3367 printk(KERN_INFO
"usbaudio: selector unit %u: ignoring channel 1\n", selector
[3]);
3369 chnum
= state
->nrchannels
;
3370 for (i
= 1; i
< selector
[4]; i
++) {
3371 mixch
= state
->nrmixch
;
3372 usb_audio_recurseunit(state
, selector
[5+i
]);
3373 if (chnum
!= state
->nrchannels
) {
3374 printk(KERN_ERR
"usbaudio: selector unit %u: input pins with varying channel numbers\n", selector
[3]);
3375 state
->termtype
= 0;
3376 state
->chconfig
= 0;
3377 state
->nrchannels
= 0;
3380 if (state
->nrmixch
!= mixch
) {
3381 mch
= &state
->mixch
[state
->nrmixch
-1];
3382 mch
->slctunitid
= selector
[3] | ((i
+ 1) << 8);
3383 } else if ((mch
= slctsrc_findunit(state
, selector
[5+i
]))) {
3384 mch
->slctunitid
= selector
[3] | ((i
+ 1) << 8);
3386 printk(KERN_INFO
"usbaudio: selector unit %u: ignoring channel %u\n", selector
[3], i
+1);
3389 state
->termtype
= 0;
3390 state
->chconfig
= 0;
3393 /* in the future we might try to handle 3D etc. effect units */
3395 static void usb_audio_processingunit(struct consmixstate
*state
, unsigned char *proc
)
3399 for (i
= 0; i
< proc
[6]; i
++)
3400 usb_audio_recurseunit(state
, proc
[7+i
]);
3401 state
->nrchannels
= proc
[7+proc
[6]];
3402 state
->termtype
= 0;
3403 state
->chconfig
= proc
[8+proc
[6]] | (proc
[9+proc
[6]] << 8);
3407 /* See Audio Class Spec, section 4.3.2.5 */
3408 static void usb_audio_featureunit(struct consmixstate
*state
, unsigned char *ftr
)
3410 struct mixerchannel
*ch
;
3411 unsigned short chftr
, mchftr
;
3413 struct usb_device
*dev
= state
->s
->usbdev
;
3414 unsigned char data
[1];
3416 unsigned char nr_logical_channels
, i
;
3418 usb_audio_recurseunit(state
, ftr
[4]);
3421 printk(KERN_ERR
"usbaudio: wrong controls size in feature unit %u\n",ftr
[3]);
3425 if (state
->nrchannels
== 0) {
3426 printk(KERN_ERR
"usbaudio: feature unit %u source has no channels\n", ftr
[3]);
3429 if (state
->nrchannels
> 2)
3430 printk(KERN_WARNING
"usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr
[3]);
3432 nr_logical_channels
=(ftr
[0]-7)/ftr
[5]-1;
3434 if (nr_logical_channels
!= state
->nrchannels
) {
3435 printk(KERN_WARNING
"usbaudio: warning: found %d of %d logical channels.\n", state
->nrchannels
,nr_logical_channels
);
3437 if (state
->nrchannels
== 1 && nr_logical_channels
==0) {
3438 printk(KERN_INFO
"usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n");
3439 } else if (state
->nrchannels
== 1 && nr_logical_channels
==2) {
3440 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");
3441 state
->nrchannels
=nr_logical_channels
;
3443 printk(KERN_WARNING
"usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n");
3447 /* There is always a master channel */
3449 /* Binary AND over logical channels if they exist */
3450 if (nr_logical_channels
) {
3451 chftr
= ftr
[6+ftr
[5]];
3452 for (i
= 2; i
<= nr_logical_channels
; i
++)
3453 chftr
&= ftr
[6+i
*ftr
[5]];
3458 /* volume control */
3460 ch
= getmixchannel(state
, getvolchannel(state
));
3462 ch
->unitid
= ftr
[3];
3463 ch
->selector
= VOLUME_CONTROL
;
3465 ch
->flags
= (state
->nrchannels
> 1) ? (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
) : 0;
3468 } else if (mchftr
& 2) {
3469 ch
= getmixchannel(state
, getvolchannel(state
));
3471 ch
->unitid
= ftr
[3];
3472 ch
->selector
= VOLUME_CONTROL
;
3480 ch
= getmixchannel(state
, SOUND_MIXER_BASS
);
3482 ch
->unitid
= ftr
[3];
3483 ch
->selector
= BASS_CONTROL
;
3485 ch
->flags
= (state
->nrchannels
> 1) ? (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
) : 0;
3488 } else if (mchftr
& 4) {
3489 ch
= getmixchannel(state
, SOUND_MIXER_BASS
);
3491 ch
->unitid
= ftr
[3];
3492 ch
->selector
= BASS_CONTROL
;
3498 /* treble control */
3500 ch
= getmixchannel(state
, SOUND_MIXER_TREBLE
);
3502 ch
->unitid
= ftr
[3];
3503 ch
->selector
= TREBLE_CONTROL
;
3505 ch
->flags
= (state
->nrchannels
> 1) ? (MIXFLG_STEREOIN
| MIXFLG_STEREOOUT
) : 0;
3508 } else if (mchftr
& 16) {
3509 ch
= getmixchannel(state
, SOUND_MIXER_TREBLE
);
3511 ch
->unitid
= ftr
[3];
3512 ch
->selector
= TREBLE_CONTROL
;
3519 /* if there are mute controls, unmute them */
3520 /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */
3521 if ((chftr
& 1) || (mchftr
& 1)) {
3522 printk(KERN_DEBUG
"usbaudio: unmuting feature unit %u interface %u\n", ftr
[3], state
->ctrlif
);
3524 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
, USB_RECIP_INTERFACE
| USB_TYPE_CLASS
| USB_DIR_OUT
,
3525 (MUTE_CONTROL
<< 8) | 0xff, state
->ctrlif
| (ftr
[3] << 8), data
, 1, 1000) < 0)
3526 printk(KERN_WARNING
"usbaudio: failure to unmute feature unit %u interface %u\n", ftr
[3], state
->ctrlif
);
3531 static void usb_audio_recurseunit(struct consmixstate
*state
, unsigned char unitid
)
3536 if (test_and_set_bit(unitid
, state
->unitbitmap
)) {
3537 printk(KERN_INFO
"usbaudio: mixer path revisits unit %d\n", unitid
);
3540 p1
= find_audiocontrol_unit(state
->buffer
, state
->buflen
, NULL
, unitid
, state
->ctrlif
);
3542 printk(KERN_ERR
"usbaudio: unit %d not found!\n", unitid
);
3545 state
->nrchannels
= 0;
3546 state
->termtype
= 0;
3547 state
->chconfig
= 0;
3549 case INPUT_TERMINAL
:
3551 printk(KERN_ERR
"usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid
);
3554 state
->nrchannels
= p1
[7];
3555 state
->termtype
= p1
[4] | (p1
[5] << 8);
3556 state
->chconfig
= p1
[8] | (p1
[9] << 8);
3560 if (p1
[0] < 10 || p1
[0] < 10+p1
[4]) {
3561 printk(KERN_ERR
"usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid
);
3564 usb_audio_mixerunit(state
, p1
);
3568 if (p1
[0] < 6 || p1
[0] < 6+p1
[4]) {
3569 printk(KERN_ERR
"usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid
);
3572 usb_audio_selectorunit(state
, p1
);
3575 case FEATURE_UNIT
: /* See USB Audio Class Spec 4.3.2.5 */
3576 if (p1
[0] < 7 || p1
[0] < 7+p1
[5]) {
3577 printk(KERN_ERR
"usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid
);
3580 usb_audio_featureunit(state
, p1
);
3583 case PROCESSING_UNIT
:
3584 if (p1
[0] < 13 || p1
[0] < 13+p1
[6] || p1
[0] < 13+p1
[6]+p1
[11+p1
[6]]) {
3585 printk(KERN_ERR
"usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid
);
3588 usb_audio_processingunit(state
, p1
);
3591 case EXTENSION_UNIT
:
3592 if (p1
[0] < 13 || p1
[0] < 13+p1
[6] || p1
[0] < 13+p1
[6]+p1
[11+p1
[6]]) {
3593 printk(KERN_ERR
"usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid
);
3596 for (j
= i
= 0; i
< p1
[6]; i
++) {
3597 usb_audio_recurseunit(state
, p1
[7+i
]);
3599 j
= state
->termtype
;
3600 else if (j
!= state
->termtype
)
3603 state
->nrchannels
= p1
[7+p1
[6]];
3604 state
->chconfig
= p1
[8+p1
[6]] | (p1
[9+p1
[6]] << 8);
3605 state
->termtype
= j
;
3609 printk(KERN_ERR
"usbaudio: unit %u: unexpected type 0x%02x\n", unitid
, p1
[2]);
3614 static void usb_audio_constructmixer(struct usb_audio_state
*s
, unsigned char *buffer
, unsigned int buflen
, unsigned int ctrlif
, unsigned char *oterm
)
3616 struct usb_mixerdev
*ms
;
3617 struct consmixstate state
;
3619 memset(&state
, 0, sizeof(state
));
3622 state
.mixchmask
= ~0;
3623 state
.buffer
= buffer
;
3624 state
.buflen
= buflen
;
3625 state
.ctrlif
= ctrlif
;
3626 set_bit(oterm
[3], state
.unitbitmap
); /* mark terminal ID as visited */
3627 printk(KERN_DEBUG
"usbaudio: constructing mixer for Terminal %u type 0x%04x\n",
3628 oterm
[3], oterm
[4] | (oterm
[5] << 8));
3629 usb_audio_recurseunit(&state
, oterm
[7]);
3630 if (!state
.nrmixch
) {
3631 printk(KERN_INFO
"usbaudio: no mixer controls found for Terminal %u\n", oterm
[3]);
3634 if (!(ms
= kmalloc(sizeof(struct usb_mixerdev
)+state
.nrmixch
*sizeof(struct mixerchannel
), GFP_KERNEL
)))
3636 memset(ms
, 0, sizeof(struct usb_mixerdev
));
3637 memcpy(&ms
->ch
, &state
.mixch
, state
.nrmixch
*sizeof(struct mixerchannel
));
3640 ms
->numch
= state
.nrmixch
;
3641 if ((ms
->dev_mixer
= register_sound_mixer(&usb_mixer_fops
, -1)) < 0) {
3642 printk(KERN_ERR
"usbaudio: cannot register mixer\n");
3646 printk(KERN_INFO
"usbaudio: registered mixer 14,%d\n", ms
->dev_mixer
);
3647 list_add_tail(&ms
->list
, &s
->mixerlist
);
3650 /* arbitrary limit, we won't check more interfaces than this */
3651 #define USB_MAXINTERFACES 32
3653 static struct usb_audio_state
*usb_audio_parsecontrol(struct usb_device
*dev
, unsigned char *buffer
, unsigned int buflen
, unsigned int ctrlif
)
3655 struct usb_audio_state
*s
;
3656 struct usb_interface
*iface
;
3657 struct usb_host_interface
*alt
;
3658 unsigned char ifin
[USB_MAXINTERFACES
], ifout
[USB_MAXINTERFACES
];
3660 unsigned int i
, j
, k
, numifin
= 0, numifout
= 0;
3662 if (!(s
= kmalloc(sizeof(struct usb_audio_state
), GFP_KERNEL
)))
3664 memset(s
, 0, sizeof(struct usb_audio_state
));
3665 INIT_LIST_HEAD(&s
->audiolist
);
3666 INIT_LIST_HEAD(&s
->mixerlist
);
3670 /* find audiocontrol interface */
3671 if (!(p1
= find_csinterface_descriptor(buffer
, buflen
, NULL
, HEADER
, ctrlif
, -1))) {
3672 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u no HEADER found\n",
3673 dev
->devnum
, ctrlif
);
3676 if (p1
[0] < 8 + p1
[7]) {
3677 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u HEADER error\n",
3678 dev
->devnum
, ctrlif
);
3682 printk(KERN_INFO
"usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n",
3683 dev
->devnum
, ctrlif
);
3684 for (i
= 0; i
< p1
[7]; i
++) {
3686 iface
= usb_ifnum_to_if(dev
, j
);
3688 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u does not exist\n",
3689 dev
->devnum
, ctrlif
, j
);
3692 if (iface
->num_altsetting
== 1) {
3693 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev
->devnum
, ctrlif
);
3696 alt
= usb_altnum_to_altsetting(iface
, 0);
3698 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n",
3699 dev
->devnum
, ctrlif
, j
);
3702 if (alt
->desc
.bInterfaceClass
!= USB_CLASS_AUDIO
) {
3703 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n",
3704 dev
->devnum
, ctrlif
, j
);
3707 if (alt
->desc
.bInterfaceSubClass
== 3) {
3708 printk(KERN_INFO
"usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n",
3709 dev
->devnum
, ctrlif
, j
);
3712 if (alt
->desc
.bInterfaceSubClass
!= 2) {
3713 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n",
3714 dev
->devnum
, ctrlif
, j
);
3717 if (alt
->desc
.bNumEndpoints
> 0) {
3718 /* Check all endpoints; should they all have a bandwidth of 0 ? */
3719 for (k
= 0; k
< alt
->desc
.bNumEndpoints
; k
++) {
3720 if (le16_to_cpu(alt
->endpoint
[k
].desc
.wMaxPacketSize
) > 0) {
3721 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev
->devnum
, ctrlif
, k
);
3725 if (k
< alt
->desc
.bNumEndpoints
)
3729 alt
= usb_altnum_to_altsetting(iface
, 1);
3731 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n",
3732 dev
->devnum
, ctrlif
, j
);
3735 if (alt
->desc
.bNumEndpoints
< 1) {
3736 printk(KERN_ERR
"usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n",
3737 dev
->devnum
, ctrlif
, j
);
3740 /* note: this requires the data endpoint to be ep0 and the optional sync
3741 ep to be ep1, which seems to be the case */
3742 if (alt
->endpoint
[0].desc
.bEndpointAddress
& USB_DIR_IN
) {
3743 if (numifin
< USB_MAXINTERFACES
) {
3744 ifin
[numifin
++] = j
;
3745 usb_driver_claim_interface(&usb_audio_driver
, iface
, (void *)-1);
3748 if (numifout
< USB_MAXINTERFACES
) {
3749 ifout
[numifout
++] = j
;
3750 usb_driver_claim_interface(&usb_audio_driver
, iface
, (void *)-1);
3754 printk(KERN_INFO
"usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n",
3755 dev
->devnum
, ctrlif
, numifin
, numifout
);
3756 for (i
= 0; i
< numifin
&& i
< numifout
; i
++)
3757 usb_audio_parsestreaming(s
, buffer
, buflen
, ifin
[i
], ifout
[i
]);
3758 for (j
= i
; j
< numifin
; j
++)
3759 usb_audio_parsestreaming(s
, buffer
, buflen
, ifin
[i
], -1);
3760 for (j
= i
; j
< numifout
; j
++)
3761 usb_audio_parsestreaming(s
, buffer
, buflen
, -1, ifout
[i
]);
3762 /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */
3763 p1
= find_csinterface_descriptor(buffer
, buflen
, NULL
, OUTPUT_TERMINAL
, ctrlif
, -1);
3766 usb_audio_constructmixer(s
, buffer
, buflen
, ctrlif
, p1
);
3767 p1
= find_csinterface_descriptor(buffer
, buflen
, p1
, OUTPUT_TERMINAL
, ctrlif
, -1);
3771 if (list_empty(&s
->audiolist
) && list_empty(&s
->mixerlist
)) {
3775 /* everything successful */
3777 list_add_tail(&s
->audiodev
, &audiodevs
);
3779 printk(KERN_DEBUG
"usb_audio_parsecontrol: usb_audio_state at %p\n", s
);
3783 /* we only care for the currently active configuration */
3785 static int usb_audio_probe(struct usb_interface
*intf
,
3786 const struct usb_device_id
*id
)
3788 struct usb_device
*dev
= interface_to_usbdev (intf
);
3789 struct usb_audio_state
*s
;
3790 unsigned char *buffer
;
3791 unsigned int buflen
;
3794 printk(KERN_DEBUG
"usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum
,
3795 config
->interface
[ifnum
].altsetting
[0].desc
.bInterfaceClass
,
3796 config
->interface
[ifnum
].altsetting
[0].desc
.bInterfaceSubClass
);
3800 * audiocontrol interface found
3801 * find which configuration number is active
3803 buffer
= dev
->rawdescriptors
[dev
->actconfig
- dev
->config
];
3804 buflen
= le16_to_cpu(dev
->actconfig
->desc
.wTotalLength
);
3805 s
= usb_audio_parsecontrol(dev
, buffer
, buflen
, intf
->altsetting
->desc
.bInterfaceNumber
);
3807 usb_set_intfdata (intf
, s
);
3814 /* a revoke facility would make things simpler */
3816 static void usb_audio_disconnect(struct usb_interface
*intf
)
3818 struct usb_audio_state
*s
= usb_get_intfdata (intf
);
3819 struct usb_audiodev
*as
;
3820 struct usb_mixerdev
*ms
;
3825 /* we get called with -1 for every audiostreaming interface registered */
3826 if (s
== (struct usb_audio_state
*)-1) {
3827 dprintk((KERN_DEBUG
"usbaudio: note, usb_audio_disconnect called with -1\n"));
3831 dprintk((KERN_DEBUG
"usbaudio: error, usb_audio_disconnect already called for %p!\n", s
));
3835 list_del_init(&s
->audiodev
);
3837 usb_set_intfdata (intf
, NULL
);
3839 /* deregister all audio and mixer devices, so no new processes can open this device */
3840 list_for_each_entry(as
, &s
->audiolist
, list
) {
3843 wake_up(&as
->usbin
.dma
.wait
);
3844 wake_up(&as
->usbout
.dma
.wait
);
3845 if (as
->dev_audio
>= 0) {
3846 unregister_sound_dsp(as
->dev_audio
);
3847 printk(KERN_INFO
"usbaudio: unregister dsp 14,%d\n", as
->dev_audio
);
3851 list_for_each_entry(ms
, &s
->mixerlist
, list
) {
3852 if (ms
->dev_mixer
>= 0) {
3853 unregister_sound_mixer(ms
->dev_mixer
);
3854 printk(KERN_INFO
"usbaudio: unregister mixer 14,%d\n", ms
->dev_mixer
);
3859 wake_up(&open_wait
);
3862 static int __init
usb_audio_init(void)
3864 int result
= usb_register(&usb_audio_driver
);
3866 info(DRIVER_VERSION
":" DRIVER_DESC
);
3871 static void __exit
usb_audio_cleanup(void)
3873 usb_deregister(&usb_audio_driver
);
3876 module_init(usb_audio_init
);
3877 module_exit(usb_audio_cleanup
);
3879 MODULE_AUTHOR( DRIVER_AUTHOR
);
3880 MODULE_DESCRIPTION( DRIVER_DESC
);
3881 MODULE_LICENSE("GPL");