aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Bunk <bunk@stusta.de>2006-02-04 18:03:28 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2006-03-20 17:49:54 -0500
commit9628416a542488230b53df48d90d4a967dea41c8 (patch)
treeb356d4c1ea8065263eda375284b87ba64d735d5f
parent29da7937a11a47bad64558c3be4d998c4ea5e823 (diff)
[PATCH] USB: remove OBSOLETE_OSS_USB_DRIVER drivers
This patch removes the obsolete USB_MIDI and USB_AUDIO drivers. Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r--drivers/usb/Makefile2
-rw-r--r--drivers/usb/class/Kconfig47
-rw-r--r--drivers/usb/class/Makefile2
-rw-r--r--drivers/usb/class/audio.c3869
-rw-r--r--drivers/usb/class/audio.h110
-rw-r--r--drivers/usb/class/usb-midi.c2153
-rw-r--r--drivers/usb/class/usb-midi.h164
7 files changed, 0 insertions, 6347 deletions
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index bade87b62cf9..7796ecfc0765 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -17,8 +17,6 @@ obj-$(CONFIG_USB_SL811_HCD) += host/
17obj-$(CONFIG_ETRAX_USB_HOST) += host/ 17obj-$(CONFIG_ETRAX_USB_HOST) += host/
18 18
19obj-$(CONFIG_USB_ACM) += class/ 19obj-$(CONFIG_USB_ACM) += class/
20obj-$(CONFIG_USB_AUDIO) += class/
21obj-$(CONFIG_USB_MIDI) += class/
22obj-$(CONFIG_USB_PRINTER) += class/ 20obj-$(CONFIG_USB_PRINTER) += class/
23 21
24obj-$(CONFIG_USB_STORAGE) += storage/ 22obj-$(CONFIG_USB_STORAGE) += storage/
diff --git a/drivers/usb/class/Kconfig b/drivers/usb/class/Kconfig
index ef105a92a7bd..3a9102d2591b 100644
--- a/drivers/usb/class/Kconfig
+++ b/drivers/usb/class/Kconfig
@@ -4,53 +4,6 @@
4comment "USB Device Class drivers" 4comment "USB Device Class drivers"
5 depends on USB 5 depends on USB
6 6
7config OBSOLETE_OSS_USB_DRIVER
8 bool "Obsolete OSS USB drivers"
9 depends on USB && SOUND
10 help
11 This option enables support for the obsolete USB Audio and Midi
12 drivers that are scheduled for removal in the near future since
13 there are ALSA drivers for the same hardware.
14
15 Please contact Adrian Bunk <bunk@stusta.de> if you had to
16 say Y here because of missing support in the ALSA drivers.
17
18 If unsure, say N.
19
20config USB_AUDIO
21 tristate "USB Audio support"
22 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
23 help
24 Say Y here if you want to connect USB audio equipment such as
25 speakers to your computer's USB port. You only need this if you use
26 the OSS sound driver; ALSA has its own option for usb audio support.
27
28 To compile this driver as a module, choose M here: the
29 module will be called audio.
30
31config USB_MIDI
32 tristate "USB MIDI support"
33 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
34 ---help---
35 Say Y here if you want to connect a USB MIDI device to your
36 computer's USB port. You only need this if you use the OSS
37 sound system; USB MIDI devices are supported by ALSA's USB
38 audio driver. This driver is for devices that comply with
39 'Universal Serial Bus Device Class Definition for MIDI Device'.
40
41 The following devices are known to work:
42 * Steinberg USB2MIDI
43 * Roland MPU64
44 * Roland PC-300
45 * Roland SC8850
46 * Roland UM-1
47 * Roland UM-2
48 * Roland UA-100
49 * Yamaha MU1000
50
51 To compile this driver as a module, choose M here: the
52 module will be called usb-midi.
53
54config USB_ACM 7config USB_ACM
55 tristate "USB Modem (CDC ACM) support" 8 tristate "USB Modem (CDC ACM) support"
56 depends on USB 9 depends on USB
diff --git a/drivers/usb/class/Makefile b/drivers/usb/class/Makefile
index 229471247751..cc391e6c2af8 100644
--- a/drivers/usb/class/Makefile
+++ b/drivers/usb/class/Makefile
@@ -4,6 +4,4 @@
4# 4#
5 5
6obj-$(CONFIG_USB_ACM) += cdc-acm.o 6obj-$(CONFIG_USB_ACM) += cdc-acm.o
7obj-$(CONFIG_USB_AUDIO) += audio.o
8obj-$(CONFIG_USB_MIDI) += usb-midi.o
9obj-$(CONFIG_USB_PRINTER) += usblp.o 7obj-$(CONFIG_USB_PRINTER) += usblp.o
diff --git a/drivers/usb/class/audio.c b/drivers/usb/class/audio.c
deleted file mode 100644
index 3ad9ee8b84a9..000000000000
--- a/drivers/usb/class/audio.c
+++ /dev/null
@@ -1,3869 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * audio.c -- USB Audio Class driver
5 *
6 * Copyright (C) 1999, 2000, 2001, 2003, 2004
7 * Alan Cox (alan@lxorguk.ukuu.org.uk)
8 * Thomas Sailer (sailer@ife.ee.ethz.ch)
9 *
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.
14 *
15 * Debugging:
16 * Use the 'lsusb' utility to dump the descriptors.
17 *
18 * 1999-09-07: Alan Cox
19 * Parsing Audio descriptor patch
20 * 1999-09-08: Thomas Sailer
21 * Added OSS compatible data io functions; both parts of the
22 * driver remain to be glued together
23 * 1999-09-10: Thomas Sailer
24 * Beautified the driver. Added sample format conversions.
25 * Still not properly glued with the parsing code.
26 * The parsing code seems to have its problems btw,
27 * Since it parses all available configs but doesn't
28 * store which iface/altsetting belongs to which config.
29 * 1999-09-20: Thomas Sailer
30 * Threw out Alan's parsing code and implemented my own one.
31 * You cannot reasonnably linearly parse audio descriptors,
32 * especially the AudioClass descriptors have to be considered
33 * pointer lists. Mixer parsing untested, due to lack of device.
34 * First stab at synch pipe implementation, the Dallas USB DAC
35 * wants to use an Asynch out pipe. usb_audio_state now basically
36 * only contains lists of mixer and wave devices. We can therefore
37 * now have multiple mixer/wave devices per USB device.
38 * 1999-10-28: Thomas Sailer
39 * Converted to URB API. Fixed a taskstate/wakeup semantics mistake
40 * that made the driver consume all available CPU cycles.
41 * Now runs stable on UHCI-Acher/Fliegl/Sailer.
42 * 1999-10-31: Thomas Sailer
43 * Audio can now be unloaded if it is not in use by any mixer
44 * or dsp client (formerly you had to disconnect the audio devices
45 * from the USB port)
46 * Finally, about three months after ordering, my "Maxxtro SPK222"
47 * speakers arrived, isn't disdata a great mail order company 8-)
48 * Parse class specific endpoint descriptor of the audiostreaming
49 * interfaces and take the endpoint attributes from there.
50 * Unbelievably, the Philips USB DAC has a sampling rate range
51 * of over a decade, yet does not support the sampling rate control!
52 * No wonder it sounds so bad, has very audible sampling rate
53 * conversion distortion. Don't try to listen to it using
54 * decent headphones!
55 * "Let's make things better" -> but please Philips start with your
56 * own stuff!!!!
57 * 1999-11-02: Thomas Sailer
58 * It takes the Philips boxes several seconds to acquire synchronisation
59 * that means they won't play short sounds. Should probably maintain
60 * the ISO datastream even if there's nothing to play.
61 * Fix counting the total_bytes counter, RealPlayer G2 depends on it.
62 * 1999-12-20: Thomas Sailer
63 * Fix bad bug in conversion to per interface probing.
64 * disconnect was called multiple times for the audio device,
65 * leading to a premature freeing of the audio structures
66 * 2000-05-13: Thomas Sailer
67 * I don't remember who changed the find_format routine,
68 * but the change was completely broken for the Dallas
69 * chip. Anyway taking sampling rate into account in find_format
70 * is bad and should not be done unless there are devices with
71 * completely broken audio descriptors. Unless someone shows
72 * me such a descriptor, I will not allow find_format to
73 * take the sampling rate into account.
74 * Also, the former find_format made:
75 * - mpg123 play mono instead of stereo
76 * - sox completely fail for wav's with sample rates < 44.1kHz
77 * for the Dallas chip.
78 * Also fix a rather long standing problem with applications that
79 * use "small" writes producing no sound at all.
80 * 2000-05-15: Thomas Sailer
81 * My fears came true, the Philips camera indeed has pretty stupid
82 * audio descriptors.
83 * 2000-05-17: Thomas Sailer
84 * Nemsoft spotted my stupid last minute change, thanks
85 * 2000-05-19: Thomas Sailer
86 * Fixed FEATURE_UNIT thinkos found thanks to the KC Technology
87 * Xtend device. Basically the driver treated FEATURE_UNIT's sourced
88 * by mono terminals as stereo.
89 * 2000-05-20: Thomas Sailer
90 * SELECTOR support (and thus selecting record channels from the mixer).
91 * Somewhat peculiar due to OSS interface limitations. Only works
92 * for channels where a "slider" is already in front of it (i.e.
93 * a MIXER unit or a FEATURE unit with volume capability).
94 * 2000-11-26: Thomas Sailer
95 * Workaround for Dallas DS4201. The DS4201 uses PCM8 as format tag for
96 * its 8 bit modes, but expects signed data (and should therefore have used PCM).
97 * 2001-03-10: Thomas Sailer
98 * provide abs function, prevent picking up a bogus kernel macro
99 * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au>
100 * 2001-06-16: Bryce Nesbitt <bryce@obviously.com>
101 * Fix SNDCTL_DSP_STEREO API violation
102 * 2003-04-08: Oliver Neukum (oliver@neukum.name):
103 * Setting a configuration is done by usbcore and must not be overridden
104 * 2004-02-27: Workaround for broken synch descriptors
105 * 2004-03-07: Alan Stern <stern@rowland.harvard.edu>
106 * Add usb_ifnum_to_if() and usb_altnum_to_altsetting() support.
107 * Use the in-memory descriptors instead of reading them from the device.
108 *
109 */
110
111/*
112 * Strategy:
113 *
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.
118 *
119 * Data IO implementation issues
120 *
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.
126 *
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.
133 *
134 * Current status: no known HCD-specific issues.
135 *
136 * Generally: Due to the brokenness of the Audio Class spec
137 * it seems generally impossible to write a generic Audio Class driver,
138 * so a reasonable driver should implement the features that are actually
139 * used.
140 *
141 * Parsing implementation issues
142 *
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.
146 *
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.
158 *
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.
166 *
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.
178 */
179
180/*****************************************************************************/
181
182#include <linux/kernel.h>
183#include <linux/slab.h>
184#include <linux/string.h>
185#include <linux/timer.h>
186#include <linux/sched.h>
187#include <linux/smp_lock.h>
188#include <linux/module.h>
189#include <linux/sound.h>
190#include <linux/soundcard.h>
191#include <linux/list.h>
192#include <linux/vmalloc.h>
193#include <linux/init.h>
194#include <linux/poll.h>
195#include <linux/bitops.h>
196#include <asm/uaccess.h>
197#include <asm/io.h>
198#include <linux/usb.h>
199
200#include "audio.h"
201
202/*
203 * Version Information
204 */
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"
208
209#define AUDIO_DEBUG 1
210
211#define SND_DEV_DSP16 5
212
213#define dprintk(x)
214
215/* --------------------------------------------------------------------- */
216
217/*
218 * Linked list of all audio devices...
219 */
220static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs);
221static DECLARE_MUTEX(open_sem);
222
223/*
224 * wait queue for processes wanting to open an USB audio device
225 */
226static DECLARE_WAIT_QUEUE_HEAD(open_wait);
227
228
229#define MAXFORMATS MAX_ALT
230#define DMABUFSHIFT 17 /* 128k worth of DMA buffer */
231#define NRSGBUF (1U<<(DMABUFSHIFT-PAGE_SHIFT))
232
233/*
234 * This influences:
235 * - Latency
236 * - Interrupt rate
237 * - Synchronisation behaviour
238 * Don't touch this if you don't understand all of the above.
239 */
240#define DESCFRAMES 5
241#define SYNCFRAMES DESCFRAMES
242
243#define MIXFLG_STEREOIN 1
244#define MIXFLG_STEREOOUT 2
245
246struct mixerchannel {
247 __u16 value;
248 __u16 osschannel; /* number of the OSS channel */
249 __s16 minval, maxval;
250 __u16 slctunitid;
251 __u8 unitid;
252 __u8 selector;
253 __u8 chnum;
254 __u8 flags;
255};
256
257struct audioformat {
258 unsigned int format;
259 unsigned int sratelo;
260 unsigned int sratehi;
261 unsigned char altsetting;
262 unsigned char attributes;
263};
264
265struct dmabuf {
266 /* buffer data format */
267 unsigned int format;
268 unsigned int srate;
269 /* physical buffer */
270 unsigned char *sgbuf[NRSGBUF];
271 unsigned bufsize;
272 unsigned numfrag;
273 unsigned fragshift;
274 unsigned wrptr, rdptr;
275 unsigned total_bytes;
276 int count;
277 unsigned error; /* over/underrun */
278 wait_queue_head_t wait;
279 /* redundant, but makes calculations easier */
280 unsigned fragsize;
281 unsigned dmasize;
282 /* OSS stuff */
283 unsigned mapped:1;
284 unsigned ready:1;
285 unsigned ossfragshift;
286 int ossmaxfrags;
287 unsigned subdivision;
288};
289
290struct usb_audio_state;
291
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
298
299struct my_data_urb {
300 struct urb *urb;
301};
302
303struct my_sync_urb {
304 struct urb *urb;
305};
306
307
308struct usb_audiodev {
309 struct list_head list;
310 struct usb_audio_state *state;
311
312 /* soundcore stuff */
313 int dev_audio;
314
315 /* wave stuff */
316 mode_t open_mode;
317 spinlock_t lock; /* DMA buffer access spinlock */
318
319 struct usbin {
320 int interface; /* Interface number, -1 means not used */
321 unsigned int format; /* USB data format */
322 unsigned int datapipe; /* the data input pipe */
323 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but adaptive IN mode */
324 unsigned int syncinterval; /* P for adaptive IN mode, 0 otherwise */
325 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
326 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
327 unsigned int phase; /* phase accumulator */
328 unsigned int flags; /* see FLG_ defines */
329
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 */
332
333 struct dmabuf dma;
334 } usbin;
335
336 struct usbout {
337 int interface; /* Interface number, -1 means not used */
338 unsigned int format; /* USB data format */
339 unsigned int datapipe; /* the data input pipe */
340 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but asynchronous OUT mode */
341 unsigned int syncinterval; /* P for asynchronous OUT mode, 0 otherwise */
342 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
343 unsigned int freqm; /* momentary sampling rate in USB format, i.e. fs/1000 in Q10.14 */
344 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
345 unsigned int phase; /* phase accumulator */
346 unsigned int flags; /* see FLG_ defines */
347
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 */
350
351 struct dmabuf dma;
352 } usbout;
353
354
355 unsigned int numfmtin, numfmtout;
356 struct audioformat fmtin[MAXFORMATS];
357 struct audioformat fmtout[MAXFORMATS];
358};
359
360struct usb_mixerdev {
361 struct list_head list;
362 struct usb_audio_state *state;
363
364 /* soundcore stuff */
365 int dev_mixer;
366
367 unsigned char iface; /* interface number of the AudioControl interface */
368
369 /* USB format descriptions */
370 unsigned int numch, modcnt;
371
372 /* mixch is last and gets allocated dynamically */
373 struct mixerchannel ch[0];
374};
375
376struct usb_audio_state {
377 struct list_head audiodev;
378
379 /* USB device */
380 struct usb_device *usbdev;
381
382 struct list_head audiolist;
383 struct list_head mixerlist;
384
385 unsigned count; /* usage counter; NOTE: the usb stack is also considered a user */
386};
387
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))
395
396/* --------------------------------------------------------------------- */
397
398static inline unsigned ld2(unsigned int x)
399{
400 unsigned r = 0;
401
402 if (x >= 0x10000) {
403 x >>= 16;
404 r += 16;
405 }
406 if (x >= 0x100) {
407 x >>= 8;
408 r += 8;
409 }
410 if (x >= 0x10) {
411 x >>= 4;
412 r += 4;
413 }
414 if (x >= 4) {
415 x >>= 2;
416 r += 2;
417 }
418 if (x >= 2)
419 r++;
420 return r;
421}
422
423/* --------------------------------------------------------------------- */
424
425/*
426 * OSS compatible ring buffer management. The ring buffer may be mmap'ed into
427 * an application address space.
428 *
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.
433 */
434
435static void dmabuf_release(struct dmabuf *db)
436{
437 unsigned int nr;
438 void *p;
439
440 for(nr = 0; nr < NRSGBUF; nr++) {
441 if (!(p = db->sgbuf[nr]))
442 continue;
443 ClearPageReserved(virt_to_page(p));
444 free_page((unsigned long)p);
445 db->sgbuf[nr] = NULL;
446 }
447 db->mapped = db->ready = 0;
448}
449
450static int dmabuf_init(struct dmabuf *db)
451{
452 unsigned int nr, bytepersec, bufs;
453 void *p;
454
455 /* initialize some fields */
456 db->rdptr = db->wrptr = db->total_bytes = db->count = db->error = 0;
457 /* calculate required buffer size */
458 bytepersec = db->srate << AFMT_BYTESSHIFT(db->format);
459 bufs = 1U << DMABUFSHIFT;
460 if (db->ossfragshift) {
461 if ((1000 << db->ossfragshift) < bytepersec)
462 db->fragshift = ld2(bytepersec/1000);
463 else
464 db->fragshift = db->ossfragshift;
465 } else {
466 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
467 if (db->fragshift < 3)
468 db->fragshift = 3;
469 }
470 db->numfrag = bufs >> db->fragshift;
471 while (db->numfrag < 4 && db->fragshift > 3) {
472 db->fragshift--;
473 db->numfrag = bufs >> db->fragshift;
474 }
475 db->fragsize = 1 << db->fragshift;
476 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
477 db->numfrag = db->ossmaxfrags;
478 db->dmasize = db->numfrag << db->fragshift;
479 for(nr = 0; nr < NRSGBUF; nr++) {
480 if (!db->sgbuf[nr]) {
481 p = (void *)get_zeroed_page(GFP_KERNEL);
482 if (!p)
483 return -ENOMEM;
484 db->sgbuf[nr] = p;
485 SetPageReserved(virt_to_page(p));
486 }
487 memset(db->sgbuf[nr], AFMT_ISUNSIGNED(db->format) ? 0x80 : 0, PAGE_SIZE);
488 if ((nr << PAGE_SHIFT) >= db->dmasize)
489 break;
490 }
491 db->bufsize = nr << PAGE_SHIFT;
492 db->ready = 1;
493 dprintk((KERN_DEBUG "usbaudio: dmabuf_init bytepersec %d bufs %d ossfragshift %d ossmaxfrags %d "
494 "fragshift %d fragsize %d numfrag %d dmasize %d bufsize %d fmt 0x%x srate %d\n",
495 bytepersec, bufs, db->ossfragshift, db->ossmaxfrags, db->fragshift, db->fragsize,
496 db->numfrag, db->dmasize, db->bufsize, db->format, db->srate));
497 return 0;
498}
499
500static int dmabuf_mmap(struct vm_area_struct *vma, struct dmabuf *db, unsigned long start, unsigned long size, pgprot_t prot)
501{
502 unsigned int nr;
503
504 if (!db->ready || db->mapped || (start | size) & (PAGE_SIZE-1) || size > db->bufsize)
505 return -EINVAL;
506 size >>= PAGE_SHIFT;
507 for(nr = 0; nr < size; nr++)
508 if (!db->sgbuf[nr])
509 return -EINVAL;
510 db->mapped = 1;
511 for(nr = 0; nr < size; nr++) {
512 unsigned long pfn;
513
514 pfn = virt_to_phys(db->sgbuf[nr]) >> PAGE_SHIFT;
515 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, prot))
516 return -EAGAIN;
517 start += PAGE_SIZE;
518 }
519 return 0;
520}
521
522static void dmabuf_copyin(struct dmabuf *db, const void *buffer, unsigned int size)
523{
524 unsigned int pgrem, rem;
525
526 db->total_bytes += size;
527 for (;;) {
528 if (size <= 0)
529 return;
530 pgrem = ((~db->wrptr) & (PAGE_SIZE-1)) + 1;
531 if (pgrem > size)
532 pgrem = size;
533 rem = db->dmasize - db->wrptr;
534 if (pgrem > rem)
535 pgrem = rem;
536 memcpy((db->sgbuf[db->wrptr >> PAGE_SHIFT]) + (db->wrptr & (PAGE_SIZE-1)), buffer, pgrem);
537 size -= pgrem;
538 buffer += pgrem;
539 db->wrptr += pgrem;
540 if (db->wrptr >= db->dmasize)
541 db->wrptr = 0;
542 }
543}
544
545static void dmabuf_copyout(struct dmabuf *db, void *buffer, unsigned int size)
546{
547 unsigned int pgrem, rem;
548
549 db->total_bytes += size;
550 for (;;) {
551 if (size <= 0)
552 return;
553 pgrem = ((~db->rdptr) & (PAGE_SIZE-1)) + 1;
554 if (pgrem > size)
555 pgrem = size;
556 rem = db->dmasize - db->rdptr;
557 if (pgrem > rem)
558 pgrem = rem;
559 memcpy(buffer, (db->sgbuf[db->rdptr >> PAGE_SHIFT]) + (db->rdptr & (PAGE_SIZE-1)), pgrem);
560 size -= pgrem;
561 buffer += pgrem;
562 db->rdptr += pgrem;
563 if (db->rdptr >= db->dmasize)
564 db->rdptr = 0;
565 }
566}
567
568static int dmabuf_copyin_user(struct dmabuf *db, unsigned int ptr, const void __user *buffer, unsigned int size)
569{
570 unsigned int pgrem, rem;
571
572 if (!db->ready || db->mapped)
573 return -EINVAL;
574 for (;;) {
575 if (size <= 0)
576 return 0;
577 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
578 if (pgrem > size)
579 pgrem = size;
580 rem = db->dmasize - ptr;
581 if (pgrem > rem)
582 pgrem = rem;
583 if (copy_from_user((db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), buffer, pgrem))
584 return -EFAULT;
585 size -= pgrem;
586 buffer += pgrem;
587 ptr += pgrem;
588 if (ptr >= db->dmasize)
589 ptr = 0;
590 }
591}
592
593static int dmabuf_copyout_user(struct dmabuf *db, unsigned int ptr, void __user *buffer, unsigned int size)
594{
595 unsigned int pgrem, rem;
596
597 if (!db->ready || db->mapped)
598 return -EINVAL;
599 for (;;) {
600 if (size <= 0)
601 return 0;
602 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
603 if (pgrem > size)
604 pgrem = size;
605 rem = db->dmasize - ptr;
606 if (pgrem > rem)
607 pgrem = rem;
608 if (copy_to_user(buffer, (db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), pgrem))
609 return -EFAULT;
610 size -= pgrem;
611 buffer += pgrem;
612 ptr += pgrem;
613 if (ptr >= db->dmasize)
614 ptr = 0;
615 }
616}
617
618/* --------------------------------------------------------------------- */
619/*
620 * USB I/O code. We do sample format conversion if necessary
621 */
622
623static void usbin_stop(struct usb_audiodev *as)
624{
625 struct usbin *u = &as->usbin;
626 unsigned long flags;
627 unsigned int i, notkilled = 1;
628
629 spin_lock_irqsave(&as->lock, flags);
630 u->flags &= ~FLG_RUNNING;
631 i = u->flags;
632 spin_unlock_irqrestore(&as->lock, flags);
633 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
634 if (notkilled)
635 schedule_timeout_interruptible(1);
636 else
637 schedule_timeout_uninterruptible(1);
638 spin_lock_irqsave(&as->lock, flags);
639 i = u->flags;
640 spin_unlock_irqrestore(&as->lock, flags);
641 if (notkilled && signal_pending(current)) {
642 if (i & FLG_URB0RUNNING)
643 usb_kill_urb(u->durb[0].urb);
644 if (i & FLG_URB1RUNNING)
645 usb_kill_urb(u->durb[1].urb);
646 if (i & FLG_SYNC0RUNNING)
647 usb_kill_urb(u->surb[0].urb);
648 if (i & FLG_SYNC1RUNNING)
649 usb_kill_urb(u->surb[1].urb);
650 notkilled = 0;
651 }
652 }
653 set_current_state(TASK_RUNNING);
654 kfree(u->durb[0].urb->transfer_buffer);
655 kfree(u->durb[1].urb->transfer_buffer);
656 kfree(u->surb[0].urb->transfer_buffer);
657 kfree(u->surb[1].urb->transfer_buffer);
658 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
659 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
660}
661
662static inline void usbin_release(struct usb_audiodev *as)
663{
664 usbin_stop(as);
665}
666
667static void usbin_disc(struct usb_audiodev *as)
668{
669 struct usbin *u = &as->usbin;
670
671 unsigned long flags;
672
673 spin_lock_irqsave(&as->lock, flags);
674 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
675 spin_unlock_irqrestore(&as->lock, flags);
676 usbin_stop(as);
677}
678
679static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt)
680{
681 unsigned int cnt, i;
682 __s16 *sp, *sp2, s;
683 unsigned char *bp;
684
685 cnt = scnt;
686 if (AFMT_ISSTEREO(ifmt))
687 cnt <<= 1;
688 sp = ((__s16 *)tmp) + cnt;
689 switch (ifmt & ~AFMT_STEREO) {
690 case AFMT_U8:
691 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
692 bp--;
693 sp--;
694 *sp = (*bp ^ 0x80) << 8;
695 }
696 break;
697
698 case AFMT_S8:
699 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
700 bp--;
701 sp--;
702 *sp = *bp << 8;
703 }
704 break;
705
706 case AFMT_U16_LE:
707 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
708 bp -= 2;
709 sp--;
710 *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000;
711 }
712 break;
713
714 case AFMT_U16_BE:
715 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
716 bp -= 2;
717 sp--;
718 *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000;
719 }
720 break;
721
722 case AFMT_S16_LE:
723 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
724 bp -= 2;
725 sp--;
726 *sp = bp[0] | (bp[1] << 8);
727 }
728 break;
729
730 case AFMT_S16_BE:
731 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
732 bp -= 2;
733 sp--;
734 *sp = bp[1] | (bp[0] << 8);
735 }
736 break;
737 }
738 if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) {
739 /* expand from mono to stereo */
740 for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) {
741 sp--;
742 sp2 -= 2;
743 sp2[0] = sp2[1] = sp[0];
744 }
745 }
746 if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) {
747 /* contract from stereo to mono */
748 for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2)
749 sp[0] = (sp2[0] + sp2[1]) >> 1;
750 }
751 cnt = scnt;
752 if (AFMT_ISSTEREO(ofmt))
753 cnt <<= 1;
754 sp = ((__s16 *)tmp);
755 bp = ((unsigned char *)obuf);
756 switch (ofmt & ~AFMT_STEREO) {
757 case AFMT_U8:
758 for (i = 0; i < cnt; i++, sp++, bp++)
759 *bp = (*sp >> 8) ^ 0x80;
760 break;
761
762 case AFMT_S8:
763 for (i = 0; i < cnt; i++, sp++, bp++)
764 *bp = *sp >> 8;
765 break;
766
767 case AFMT_U16_LE:
768 for (i = 0; i < cnt; i++, sp++, bp += 2) {
769 s = *sp;
770 bp[0] = s;
771 bp[1] = (s >> 8) ^ 0x80;
772 }
773 break;
774
775 case AFMT_U16_BE:
776 for (i = 0; i < cnt; i++, sp++, bp += 2) {
777 s = *sp;
778 bp[1] = s;
779 bp[0] = (s >> 8) ^ 0x80;
780 }
781 break;
782
783 case AFMT_S16_LE:
784 for (i = 0; i < cnt; i++, sp++, bp += 2) {
785 s = *sp;
786 bp[0] = s;
787 bp[1] = s >> 8;
788 }
789 break;
790
791 case AFMT_S16_BE:
792 for (i = 0; i < cnt; i++, sp++, bp += 2) {
793 s = *sp;
794 bp[1] = s;
795 bp[0] = s >> 8;
796 }
797 break;
798 }
799
800}
801
802static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples)
803{
804 union {
805 __s16 s[64];
806 unsigned char b[0];
807 } tmp;
808 unsigned int scnt, maxs, ufmtsh, dfmtsh;
809
810 ufmtsh = AFMT_BYTESSHIFT(u->format);
811 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
812 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
813 while (samples > 0) {
814 scnt = samples;
815 if (scnt > maxs)
816 scnt = maxs;
817 conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt);
818 dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh);
819 buffer += scnt << ufmtsh;
820 samples -= scnt;
821 }
822}
823
824static int usbin_prepare_desc(struct usbin *u, struct urb *urb)
825{
826 unsigned int i, maxsize, offs;
827
828 maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
829 //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format);
830 for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) {
831 urb->iso_frame_desc[i].length = maxsize;
832 urb->iso_frame_desc[i].offset = offs;
833 }
834 urb->interval = 1;
835 return 0;
836}
837
838/*
839 * return value: 0 if descriptor should be restarted, -1 otherwise
840 * convert sample format on the fly if necessary
841 */
842static int usbin_retire_desc(struct usbin *u, struct urb *urb)
843{
844 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree;
845 unsigned char *cp;
846
847 ufmtsh = AFMT_BYTESSHIFT(u->format);
848 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
849 for (i = 0; i < DESCFRAMES; i++) {
850 cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset;
851 if (urb->iso_frame_desc[i].status) {
852 dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
853 continue;
854 }
855 scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh;
856 if (!scnt)
857 continue;
858 cnt = scnt << dfmtsh;
859 if (!u->dma.mapped) {
860 dmafree = u->dma.dmasize - u->dma.count;
861 if (cnt > dmafree) {
862 scnt = dmafree >> dfmtsh;
863 cnt = scnt << dfmtsh;
864 err++;
865 }
866 }
867 u->dma.count += cnt;
868 if (u->format == u->dma.format) {
869 /* we do not need format conversion */
870 dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n"));
871 dmabuf_copyin(&u->dma, cp, cnt);
872 } else {
873 /* we need sampling format conversion */
874 dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format));
875 usbin_convert(u, cp, scnt);
876 }
877 }
878 if (err)
879 u->dma.error++;
880 if (u->dma.count >= (signed)u->dma.fragsize)
881 wake_up(&u->dma.wait);
882 return err ? -1 : 0;
883}
884
885static void usbin_completed(struct urb *urb, struct pt_regs *regs)
886{
887 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
888 struct usbin *u = &as->usbin;
889 unsigned long flags;
890 unsigned int mask;
891 int suret = 0;
892
893#if 0
894 printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
895#endif
896 if (urb == u->durb[0].urb)
897 mask = FLG_URB0RUNNING;
898 else if (urb == u->durb[1].urb)
899 mask = FLG_URB1RUNNING;
900 else {
901 mask = 0;
902 printk(KERN_ERR "usbin_completed: panic: unknown URB\n");
903 }
904 urb->dev = as->state->usbdev;
905 spin_lock_irqsave(&as->lock, flags);
906 if (!usbin_retire_desc(u, urb) &&
907 u->flags & FLG_RUNNING &&
908 !usbin_prepare_desc(u, urb) &&
909 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
910 u->flags |= mask;
911 } else {
912 u->flags &= ~(mask | FLG_RUNNING);
913 wake_up(&u->dma.wait);
914 printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret);
915 }
916 spin_unlock_irqrestore(&as->lock, flags);
917}
918
919/*
920 * we output sync data
921 */
922static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb)
923{
924 unsigned char *cp = urb->transfer_buffer;
925 unsigned int i, offs;
926
927 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) {
928 urb->iso_frame_desc[i].length = 3;
929 urb->iso_frame_desc[i].offset = offs;
930 cp[0] = u->freqn;
931 cp[1] = u->freqn >> 8;
932 cp[2] = u->freqn >> 16;
933 }
934 urb->interval = 1;
935 return 0;
936}
937
938/*
939 * return value: 0 if descriptor should be restarted, -1 otherwise
940 */
941static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb)
942{
943 unsigned int i;
944
945 for (i = 0; i < SYNCFRAMES; i++)
946 if (urb->iso_frame_desc[0].status)
947 dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
948 return 0;
949}
950
951static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs)
952{
953 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
954 struct usbin *u = &as->usbin;
955 unsigned long flags;
956 unsigned int mask;
957 int suret = 0;
958
959#if 0
960 printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
961#endif
962 if (urb == u->surb[0].urb)
963 mask = FLG_SYNC0RUNNING;
964 else if (urb == u->surb[1].urb)
965 mask = FLG_SYNC1RUNNING;
966 else {
967 mask = 0;
968 printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n");
969 }
970 urb->dev = as->state->usbdev;
971 spin_lock_irqsave(&as->lock, flags);
972 if (!usbin_sync_retire_desc(u, urb) &&
973 u->flags & FLG_RUNNING &&
974 !usbin_sync_prepare_desc(u, urb) &&
975 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
976 u->flags |= mask;
977 } else {
978 u->flags &= ~(mask | FLG_RUNNING);
979 wake_up(&u->dma.wait);
980 dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
981 }
982 spin_unlock_irqrestore(&as->lock, flags);
983}
984
985static int usbin_start(struct usb_audiodev *as)
986{
987 struct usb_device *dev = as->state->usbdev;
988 struct usbin *u = &as->usbin;
989 struct urb *urb;
990 unsigned long flags;
991 unsigned int maxsze, bufsz;
992
993#if 0
994 printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
995 dev->devnum, u->format, u->dma.format, u->dma.srate);
996#endif
997 /* allocate USB storage if not already done */
998 spin_lock_irqsave(&as->lock, flags);
999 if (!(u->flags & FLG_CONNECTED)) {
1000 spin_unlock_irqrestore(&as->lock, flags);
1001 return -EIO;
1002 }
1003 if (!(u->flags & FLG_RUNNING)) {
1004 spin_unlock_irqrestore(&as->lock, flags);
1005 u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1006 u->freqmax = u->freqn + (u->freqn >> 2);
1007 u->phase = 0;
1008 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1009 bufsz = DESCFRAMES * maxsze;
1010 kfree(u->durb[0].urb->transfer_buffer);
1011 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1012 u->durb[0].urb->transfer_buffer_length = bufsz;
1013 kfree(u->durb[1].urb->transfer_buffer);
1014 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1015 u->durb[1].urb->transfer_buffer_length = bufsz;
1016 if (u->syncpipe) {
1017 kfree(u->surb[0].urb->transfer_buffer);
1018 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1019 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1020 kfree(u->surb[1].urb->transfer_buffer);
1021 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1022 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1023 }
1024 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1025 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1026 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1027 return 0;
1028 }
1029 spin_lock_irqsave(&as->lock, flags);
1030 }
1031 if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) {
1032 spin_unlock_irqrestore(&as->lock, flags);
1033 return 0;
1034 }
1035 u->flags |= FLG_RUNNING;
1036 if (!(u->flags & FLG_URB0RUNNING)) {
1037 urb = u->durb[0].urb;
1038 urb->dev = dev;
1039 urb->pipe = u->datapipe;
1040 urb->transfer_flags = URB_ISO_ASAP;
1041 urb->number_of_packets = DESCFRAMES;
1042 urb->context = as;
1043 urb->complete = usbin_completed;
1044 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1045 u->flags |= FLG_URB0RUNNING;
1046 else
1047 u->flags &= ~FLG_RUNNING;
1048 }
1049 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1050 urb = u->durb[1].urb;
1051 urb->dev = dev;
1052 urb->pipe = u->datapipe;
1053 urb->transfer_flags = URB_ISO_ASAP;
1054 urb->number_of_packets = DESCFRAMES;
1055 urb->context = as;
1056 urb->complete = usbin_completed;
1057 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1058 u->flags |= FLG_URB1RUNNING;
1059 else
1060 u->flags &= ~FLG_RUNNING;
1061 }
1062 if (u->syncpipe) {
1063 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1064 urb = u->surb[0].urb;
1065 urb->dev = dev;
1066 urb->pipe = u->syncpipe;
1067 urb->transfer_flags = URB_ISO_ASAP;
1068 urb->number_of_packets = SYNCFRAMES;
1069 urb->context = as;
1070 urb->complete = usbin_sync_completed;
1071 /* stride: u->syncinterval */
1072 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1073 u->flags |= FLG_SYNC0RUNNING;
1074 else
1075 u->flags &= ~FLG_RUNNING;
1076 }
1077 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1078 urb = u->surb[1].urb;
1079 urb->dev = dev;
1080 urb->pipe = u->syncpipe;
1081 urb->transfer_flags = URB_ISO_ASAP;
1082 urb->number_of_packets = SYNCFRAMES;
1083 urb->context = as;
1084 urb->complete = usbin_sync_completed;
1085 /* stride: u->syncinterval */
1086 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1087 u->flags |= FLG_SYNC1RUNNING;
1088 else
1089 u->flags &= ~FLG_RUNNING;
1090 }
1091 }
1092 spin_unlock_irqrestore(&as->lock, flags);
1093 return 0;
1094}
1095
1096static void usbout_stop(struct usb_audiodev *as)
1097{
1098 struct usbout *u = &as->usbout;
1099 unsigned long flags;
1100 unsigned int i, notkilled = 1;
1101
1102 spin_lock_irqsave(&as->lock, flags);
1103 u->flags &= ~FLG_RUNNING;
1104 i = u->flags;
1105 spin_unlock_irqrestore(&as->lock, flags);
1106 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
1107 if (notkilled)
1108 schedule_timeout_interruptible(1);
1109 else
1110 schedule_timeout_uninterruptible(1);
1111 spin_lock_irqsave(&as->lock, flags);
1112 i = u->flags;
1113 spin_unlock_irqrestore(&as->lock, flags);
1114 if (notkilled && signal_pending(current)) {
1115 if (i & FLG_URB0RUNNING)
1116 usb_kill_urb(u->durb[0].urb);
1117 if (i & FLG_URB1RUNNING)
1118 usb_kill_urb(u->durb[1].urb);
1119 if (i & FLG_SYNC0RUNNING)
1120 usb_kill_urb(u->surb[0].urb);
1121 if (i & FLG_SYNC1RUNNING)
1122 usb_kill_urb(u->surb[1].urb);
1123 notkilled = 0;
1124 }
1125 }
1126 set_current_state(TASK_RUNNING);
1127 kfree(u->durb[0].urb->transfer_buffer);
1128 kfree(u->durb[1].urb->transfer_buffer);
1129 kfree(u->surb[0].urb->transfer_buffer);
1130 kfree(u->surb[1].urb->transfer_buffer);
1131 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
1132 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
1133}
1134
1135static inline void usbout_release(struct usb_audiodev *as)
1136{
1137 usbout_stop(as);
1138}
1139
1140static void usbout_disc(struct usb_audiodev *as)
1141{
1142 struct usbout *u = &as->usbout;
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&as->lock, flags);
1146 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
1147 spin_unlock_irqrestore(&as->lock, flags);
1148 usbout_stop(as);
1149}
1150
1151static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples)
1152{
1153 union {
1154 __s16 s[64];
1155 unsigned char b[0];
1156 } tmp;
1157 unsigned int scnt, maxs, ufmtsh, dfmtsh;
1158
1159 ufmtsh = AFMT_BYTESSHIFT(u->format);
1160 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1161 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
1162 while (samples > 0) {
1163 scnt = samples;
1164 if (scnt > maxs)
1165 scnt = maxs;
1166 dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh);
1167 conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt);
1168 buffer += scnt << ufmtsh;
1169 samples -= scnt;
1170 }
1171}
1172
1173static int usbout_prepare_desc(struct usbout *u, struct urb *urb)
1174{
1175 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs;
1176 unsigned char *cp = urb->transfer_buffer;
1177
1178 ufmtsh = AFMT_BYTESSHIFT(u->format);
1179 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1180 for (i = offs = 0; i < DESCFRAMES; i++) {
1181 urb->iso_frame_desc[i].offset = offs;
1182 u->phase = (u->phase & 0x3fff) + u->freqm;
1183 scnt = u->phase >> 14;
1184 if (!scnt) {
1185 urb->iso_frame_desc[i].length = 0;
1186 continue;
1187 }
1188 cnt = scnt << dfmtsh;
1189 if (!u->dma.mapped) {
1190 if (cnt > u->dma.count) {
1191 scnt = u->dma.count >> dfmtsh;
1192 cnt = scnt << dfmtsh;
1193 err++;
1194 }
1195 u->dma.count -= cnt;
1196 } else
1197 u->dma.count += cnt;
1198 if (u->format == u->dma.format) {
1199 /* we do not need format conversion */
1200 dmabuf_copyout(&u->dma, cp, cnt);
1201 } else {
1202 /* we need sampling format conversion */
1203 usbout_convert(u, cp, scnt);
1204 }
1205 cnt = scnt << ufmtsh;
1206 urb->iso_frame_desc[i].length = cnt;
1207 offs += cnt;
1208 cp += cnt;
1209 }
1210 urb->interval = 1;
1211 if (err)
1212 u->dma.error++;
1213 if (u->dma.mapped) {
1214 if (u->dma.count >= (signed)u->dma.fragsize)
1215 wake_up(&u->dma.wait);
1216 } else {
1217 if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize)
1218 wake_up(&u->dma.wait);
1219 }
1220 return err ? -1 : 0;
1221}
1222
1223/*
1224 * return value: 0 if descriptor should be restarted, -1 otherwise
1225 */
1226static int usbout_retire_desc(struct usbout *u, struct urb *urb)
1227{
1228 unsigned int i;
1229
1230 for (i = 0; i < DESCFRAMES; i++) {
1231 if (urb->iso_frame_desc[i].status) {
1232 dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1233 continue;
1234 }
1235 }
1236 return 0;
1237}
1238
1239static void usbout_completed(struct urb *urb, struct pt_regs *regs)
1240{
1241 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1242 struct usbout *u = &as->usbout;
1243 unsigned long flags;
1244 unsigned int mask;
1245 int suret = 0;
1246
1247#if 0
1248 printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1249#endif
1250 if (urb == u->durb[0].urb)
1251 mask = FLG_URB0RUNNING;
1252 else if (urb == u->durb[1].urb)
1253 mask = FLG_URB1RUNNING;
1254 else {
1255 mask = 0;
1256 printk(KERN_ERR "usbout_completed: panic: unknown URB\n");
1257 }
1258 urb->dev = as->state->usbdev;
1259 spin_lock_irqsave(&as->lock, flags);
1260 if (!usbout_retire_desc(u, urb) &&
1261 u->flags & FLG_RUNNING &&
1262 !usbout_prepare_desc(u, urb) &&
1263 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1264 u->flags |= mask;
1265 } else {
1266 u->flags &= ~(mask | FLG_RUNNING);
1267 wake_up(&u->dma.wait);
1268 dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1269 }
1270 spin_unlock_irqrestore(&as->lock, flags);
1271}
1272
1273static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb)
1274{
1275 unsigned int i, offs;
1276
1277 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) {
1278 urb->iso_frame_desc[i].length = 3;
1279 urb->iso_frame_desc[i].offset = offs;
1280 }
1281 urb->interval = 1;
1282 return 0;
1283}
1284
1285/*
1286 * return value: 0 if descriptor should be restarted, -1 otherwise
1287 */
1288static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb)
1289{
1290 unsigned char *cp = urb->transfer_buffer;
1291 unsigned int f, i;
1292
1293 for (i = 0; i < SYNCFRAMES; i++, cp += 3) {
1294 if (urb->iso_frame_desc[i].status) {
1295 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1296 continue;
1297 }
1298 if (urb->iso_frame_desc[i].actual_length < 3) {
1299 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length));
1300 continue;
1301 }
1302 f = cp[0] | (cp[1] << 8) | (cp[2] << 16);
1303 if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) {
1304 printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn);
1305 continue;
1306 }
1307 u->freqm = f;
1308 }
1309 return 0;
1310}
1311
1312static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs)
1313{
1314 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1315 struct usbout *u = &as->usbout;
1316 unsigned long flags;
1317 unsigned int mask;
1318 int suret = 0;
1319
1320#if 0
1321 printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1322#endif
1323 if (urb == u->surb[0].urb)
1324 mask = FLG_SYNC0RUNNING;
1325 else if (urb == u->surb[1].urb)
1326 mask = FLG_SYNC1RUNNING;
1327 else {
1328 mask = 0;
1329 printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n");
1330 }
1331 urb->dev = as->state->usbdev;
1332 spin_lock_irqsave(&as->lock, flags);
1333 if (!usbout_sync_retire_desc(u, urb) &&
1334 u->flags & FLG_RUNNING &&
1335 !usbout_sync_prepare_desc(u, urb) &&
1336 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1337 u->flags |= mask;
1338 } else {
1339 u->flags &= ~(mask | FLG_RUNNING);
1340 wake_up(&u->dma.wait);
1341 dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1342 }
1343 spin_unlock_irqrestore(&as->lock, flags);
1344}
1345
1346static int usbout_start(struct usb_audiodev *as)
1347{
1348 struct usb_device *dev = as->state->usbdev;
1349 struct usbout *u = &as->usbout;
1350 struct urb *urb;
1351 unsigned long flags;
1352 unsigned int maxsze, bufsz;
1353
1354#if 0
1355 printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
1356 dev->devnum, u->format, u->dma.format, u->dma.srate);
1357#endif
1358 /* allocate USB storage if not already done */
1359 spin_lock_irqsave(&as->lock, flags);
1360 if (!(u->flags & FLG_CONNECTED)) {
1361 spin_unlock_irqrestore(&as->lock, flags);
1362 return -EIO;
1363 }
1364 if (!(u->flags & FLG_RUNNING)) {
1365 spin_unlock_irqrestore(&as->lock, flags);
1366 u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1367 u->freqmax = u->freqn + (u->freqn >> 2);
1368 u->phase = 0;
1369 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1370 bufsz = DESCFRAMES * maxsze;
1371 kfree(u->durb[0].urb->transfer_buffer);
1372 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1373 u->durb[0].urb->transfer_buffer_length = bufsz;
1374 kfree(u->durb[1].urb->transfer_buffer);
1375 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1376 u->durb[1].urb->transfer_buffer_length = bufsz;
1377 if (u->syncpipe) {
1378 kfree(u->surb[0].urb->transfer_buffer);
1379 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1380 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1381 kfree(u->surb[1].urb->transfer_buffer);
1382 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1383 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1384 }
1385 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1386 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1387 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1388 return 0;
1389 }
1390 spin_lock_irqsave(&as->lock, flags);
1391 }
1392 if (u->dma.count <= 0 && !u->dma.mapped) {
1393 spin_unlock_irqrestore(&as->lock, flags);
1394 return 0;
1395 }
1396 u->flags |= FLG_RUNNING;
1397 if (!(u->flags & FLG_URB0RUNNING)) {
1398 urb = u->durb[0].urb;
1399 urb->dev = dev;
1400 urb->pipe = u->datapipe;
1401 urb->transfer_flags = URB_ISO_ASAP;
1402 urb->number_of_packets = DESCFRAMES;
1403 urb->context = as;
1404 urb->complete = usbout_completed;
1405 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1406 u->flags |= FLG_URB0RUNNING;
1407 else
1408 u->flags &= ~FLG_RUNNING;
1409 }
1410 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1411 urb = u->durb[1].urb;
1412 urb->dev = dev;
1413 urb->pipe = u->datapipe;
1414 urb->transfer_flags = URB_ISO_ASAP;
1415 urb->number_of_packets = DESCFRAMES;
1416 urb->context = as;
1417 urb->complete = usbout_completed;
1418 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1419 u->flags |= FLG_URB1RUNNING;
1420 else
1421 u->flags &= ~FLG_RUNNING;
1422 }
1423 if (u->syncpipe) {
1424 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1425 urb = u->surb[0].urb;
1426 urb->dev = dev;
1427 urb->pipe = u->syncpipe;
1428 urb->transfer_flags = URB_ISO_ASAP;
1429 urb->number_of_packets = SYNCFRAMES;
1430 urb->context = as;
1431 urb->complete = usbout_sync_completed;
1432 /* stride: u->syncinterval */
1433 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1434 u->flags |= FLG_SYNC0RUNNING;
1435 else
1436 u->flags &= ~FLG_RUNNING;
1437 }
1438 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1439 urb = u->surb[1].urb;
1440 urb->dev = dev;
1441 urb->pipe = u->syncpipe;
1442 urb->transfer_flags = URB_ISO_ASAP;
1443 urb->number_of_packets = SYNCFRAMES;
1444 urb->context = as;
1445 urb->complete = usbout_sync_completed;
1446 /* stride: u->syncinterval */
1447 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1448 u->flags |= FLG_SYNC1RUNNING;
1449 else
1450 u->flags &= ~FLG_RUNNING;
1451 }
1452 }
1453 spin_unlock_irqrestore(&as->lock, flags);
1454 return 0;
1455}
1456
1457/* --------------------------------------------------------------------- */
1458
1459static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate)
1460{
1461 unsigned int g = 0;
1462
1463 if (srate < afp->sratelo)
1464 g += afp->sratelo - srate;
1465 if (srate > afp->sratehi)
1466 g += srate - afp->sratehi;
1467 if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt))
1468 g += 0x100000;
1469 if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt))
1470 g += 0x400000;
1471 if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt))
1472 g += 0x100000;
1473 if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt))
1474 g += 0x400000;
1475 return g;
1476}
1477
1478static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate)
1479{
1480 unsigned int i, g, gb = ~0;
1481 int j = -1; /* default to failure */
1482
1483 /* find "best" format (according to format_goodness) */
1484 for (i = 0; i < nr; i++) {
1485 g = format_goodness(&afp[i], fmt, srate);
1486 if (g >= gb)
1487 continue;
1488 j = i;
1489 gb = g;
1490 }
1491 return j;
1492}
1493
1494static int set_format_in(struct usb_audiodev *as)
1495{
1496 struct usb_device *dev = as->state->usbdev;
1497 struct usb_host_interface *alts;
1498 struct usb_interface *iface;
1499 struct usbin *u = &as->usbin;
1500 struct dmabuf *d = &u->dma;
1501 struct audioformat *fmt;
1502 unsigned int ep;
1503 unsigned char data[3];
1504 int fmtnr, ret;
1505
1506 iface = usb_ifnum_to_if(dev, u->interface);
1507 if (!iface)
1508 return 0;
1509
1510 fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate);
1511 if (fmtnr < 0) {
1512 printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n");
1513 return -1;
1514 }
1515
1516 fmt = as->fmtin + fmtnr;
1517 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1518 u->format = fmt->format;
1519 u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1520 u->syncpipe = u->syncinterval = 0;
1521 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) {
1522 if (alts->desc.bNumEndpoints < 2 ||
1523 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1524 alts->endpoint[1].desc.bSynchAddress != 0 ||
1525 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) {
1526 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in "
1527 "but has invalid synch pipe; treating as asynchronous in\n",
1528 dev->devnum, u->interface, fmt->altsetting);
1529 } else {
1530 u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1531 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1532 }
1533 }
1534 if (d->srate < fmt->sratelo)
1535 d->srate = fmt->sratelo;
1536 if (d->srate > fmt->sratehi)
1537 d->srate = fmt->sratehi;
1538 dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n",
1539 u->interface, fmt->altsetting));
1540 if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) {
1541 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1542 dev->devnum, u->interface, fmt->altsetting);
1543 return -1;
1544 }
1545 if (fmt->sratelo == fmt->sratehi)
1546 return 0;
1547 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1548 /* if endpoint has pitch control, enable it */
1549 if (fmt->attributes & 0x02) {
1550 data[0] = 1;
1551 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1552 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1553 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1554 ret, dev->devnum, u->interface, ep, d->srate);
1555 return -1;
1556 }
1557 }
1558 /* if endpoint has sampling rate control, set it */
1559 if (fmt->attributes & 0x01) {
1560 data[0] = d->srate;
1561 data[1] = d->srate >> 8;
1562 data[2] = d->srate >> 16;
1563 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1564 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1565 printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1566 ret, dev->devnum, u->interface, ep, d->srate);
1567 return -1;
1568 }
1569 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1570 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1571 printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n",
1572 ret, dev->devnum, u->interface, ep);
1573 return -1;
1574 }
1575 dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n",
1576 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1577 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1578 }
1579 dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1580 return 0;
1581}
1582
1583static int set_format_out(struct usb_audiodev *as)
1584{
1585 struct usb_device *dev = as->state->usbdev;
1586 struct usb_host_interface *alts;
1587 struct usb_interface *iface;
1588 struct usbout *u = &as->usbout;
1589 struct dmabuf *d = &u->dma;
1590 struct audioformat *fmt;
1591 unsigned int ep;
1592 unsigned char data[3];
1593 int fmtnr, ret;
1594
1595 iface = usb_ifnum_to_if(dev, u->interface);
1596 if (!iface)
1597 return 0;
1598
1599 fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate);
1600 if (fmtnr < 0) {
1601 printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n");
1602 return -1;
1603 }
1604
1605 fmt = as->fmtout + fmtnr;
1606 u->format = fmt->format;
1607 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1608 u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1609 u->syncpipe = u->syncinterval = 0;
1610 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) {
1611#if 0
1612 printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n"
1613 KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n"
1614 KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints,
1615 alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress,
1616 alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress);
1617#endif
1618 if (alts->desc.bNumEndpoints < 2 ||
1619 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1620 alts->endpoint[1].desc.bSynchAddress != 0 ||
1621 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) {
1622 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out "
1623 "but has invalid synch pipe; treating as adaptive out\n",
1624 dev->devnum, u->interface, fmt->altsetting);
1625 } else {
1626 u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1627 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1628 }
1629 }
1630 if (d->srate < fmt->sratelo)
1631 d->srate = fmt->sratelo;
1632 if (d->srate > fmt->sratehi)
1633 d->srate = fmt->sratehi;
1634 dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n",
1635 u->interface, fmt->altsetting));
1636 if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) {
1637 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1638 dev->devnum, u->interface, fmt->altsetting);
1639 return -1;
1640 }
1641 if (fmt->sratelo == fmt->sratehi)
1642 return 0;
1643 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1644 /* if endpoint has pitch control, enable it */
1645 if (fmt->attributes & 0x02) {
1646 data[0] = 1;
1647 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1648 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1649 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1650 ret, dev->devnum, u->interface, ep, d->srate);
1651 return -1;
1652 }
1653 }
1654 /* if endpoint has sampling rate control, set it */
1655 if (fmt->attributes & 0x01) {
1656 data[0] = d->srate;
1657 data[1] = d->srate >> 8;
1658 data[2] = d->srate >> 16;
1659 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1660 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1661 printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1662 ret, dev->devnum, u->interface, ep, d->srate);
1663 return -1;
1664 }
1665 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1666 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1667 printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n",
1668 ret, dev->devnum, u->interface, ep);
1669 return -1;
1670 }
1671 dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n",
1672 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1673 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1674 }
1675 dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1676 return 0;
1677}
1678
1679static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate)
1680{
1681 int ret1 = 0, ret2 = 0;
1682
1683 if (!(fmode & (FMODE_READ|FMODE_WRITE)))
1684 return -EINVAL;
1685 if (fmode & FMODE_READ) {
1686 usbin_stop(s);
1687 s->usbin.dma.ready = 0;
1688 if (fmt == AFMT_QUERY)
1689 fmt = s->usbin.dma.format;
1690 else
1691 s->usbin.dma.format = fmt;
1692 if (!srate)
1693 srate = s->usbin.dma.srate;
1694 else
1695 s->usbin.dma.srate = srate;
1696 }
1697 if (fmode & FMODE_WRITE) {
1698 usbout_stop(s);
1699 s->usbout.dma.ready = 0;
1700 if (fmt == AFMT_QUERY)
1701 fmt = s->usbout.dma.format;
1702 else
1703 s->usbout.dma.format = fmt;
1704 if (!srate)
1705 srate = s->usbout.dma.srate;
1706 else
1707 s->usbout.dma.srate = srate;
1708 }
1709 if (fmode & FMODE_READ)
1710 ret1 = set_format_in(s);
1711 if (fmode & FMODE_WRITE)
1712 ret2 = set_format_out(s);
1713 return ret1 ? ret1 : ret2;
1714}
1715
1716/* --------------------------------------------------------------------- */
1717
1718static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value)
1719{
1720 struct usb_device *dev = ms->state->usbdev;
1721 unsigned char data[2];
1722 struct mixerchannel *ch;
1723 int v1, v2, v3;
1724
1725 if (mixch >= ms->numch)
1726 return -1;
1727 ch = &ms->ch[mixch];
1728 v3 = ch->maxval - ch->minval;
1729 v1 = value & 0xff;
1730 v2 = (value >> 8) & 0xff;
1731 if (v1 > 100)
1732 v1 = 100;
1733 if (v2 > 100)
1734 v2 = 100;
1735 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1736 v2 = v1;
1737 ch->value = v1 | (v2 << 8);
1738 v1 = (v1 * v3) / 100 + ch->minval;
1739 v2 = (v2 * v3) / 100 + ch->minval;
1740 switch (ch->selector) {
1741 case 0: /* mixer unit request */
1742 data[0] = v1;
1743 data[1] = v1 >> 8;
1744 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1745 (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1746 goto err;
1747 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1748 return 0;
1749 data[0] = v2;
1750 data[1] = v2 >> 8;
1751 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1752 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
1753 ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1754 goto err;
1755 return 0;
1756
1757 /* various feature unit controls */
1758 case VOLUME_CONTROL:
1759 data[0] = v1;
1760 data[1] = v1 >> 8;
1761 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1762 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1763 goto err;
1764 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1765 return 0;
1766 data[0] = v2;
1767 data[1] = v2 >> 8;
1768 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1769 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1770 goto err;
1771 return 0;
1772
1773 case BASS_CONTROL:
1774 case MID_CONTROL:
1775 case TREBLE_CONTROL:
1776 data[0] = v1 >> 8;
1777 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1778 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1779 goto err;
1780 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1781 return 0;
1782 data[0] = v2 >> 8;
1783 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1784 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1785 goto err;
1786 return 0;
1787
1788 default:
1789 return -1;
1790 }
1791 return 0;
1792
1793 err:
1794 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
1795 dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector);
1796 return -1;
1797}
1798
1799static int get_rec_src(struct usb_mixerdev *ms)
1800{
1801 struct usb_device *dev = ms->state->usbdev;
1802 unsigned int mask = 0, retmask = 0;
1803 unsigned int i, j;
1804 unsigned char buf;
1805 int err = 0;
1806
1807 for (i = 0; i < ms->numch; i++) {
1808 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1809 continue;
1810 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1811 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1812 err = -EIO;
1813 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1814 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1815 continue;
1816 }
1817 for (j = i; j < ms->numch; j++) {
1818 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1819 continue;
1820 mask |= 1 << j;
1821 if (buf == (ms->ch[j].slctunitid >> 8))
1822 retmask |= 1 << ms->ch[j].osschannel;
1823 }
1824 }
1825 if (err)
1826 return -EIO;
1827 return retmask;
1828}
1829
1830static int set_rec_src(struct usb_mixerdev *ms, int srcmask)
1831{
1832 struct usb_device *dev = ms->state->usbdev;
1833 unsigned int mask = 0, smask, bmask;
1834 unsigned int i, j;
1835 unsigned char buf;
1836 int err = 0;
1837
1838 for (i = 0; i < ms->numch; i++) {
1839 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1840 continue;
1841 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1842 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1843 err = -EIO;
1844 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1845 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1846 continue;
1847 }
1848 /* first generate smask */
1849 smask = bmask = 0;
1850 for (j = i; j < ms->numch; j++) {
1851 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1852 continue;
1853 smask |= 1 << ms->ch[j].osschannel;
1854 if (buf == (ms->ch[j].slctunitid >> 8))
1855 bmask |= 1 << ms->ch[j].osschannel;
1856 mask |= 1 << j;
1857 }
1858 /* check for multiple set sources */
1859 j = hweight32(srcmask & smask);
1860 if (j == 0)
1861 continue;
1862 if (j > 1)
1863 srcmask &= ~bmask;
1864 for (j = i; j < ms->numch; j++) {
1865 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1866 continue;
1867 if (!(srcmask & (1 << ms->ch[j].osschannel)))
1868 continue;
1869 buf = ms->ch[j].slctunitid >> 8;
1870 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1871 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, 1000) < 0) {
1872 err = -EIO;
1873 printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n",
1874 dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff);
1875 continue;
1876 }
1877 }
1878 }
1879 return err ? -EIO : 0;
1880}
1881
1882/* --------------------------------------------------------------------- */
1883
1884/*
1885 * should be called with open_sem hold, so that no new processes
1886 * look at the audio device to be destroyed
1887 */
1888
1889static void release(struct usb_audio_state *s)
1890{
1891 struct usb_audiodev *as;
1892 struct usb_mixerdev *ms;
1893
1894 s->count--;
1895 if (s->count) {
1896 up(&open_sem);
1897 return;
1898 }
1899 up(&open_sem);
1900 wake_up(&open_wait);
1901 while (!list_empty(&s->audiolist)) {
1902 as = list_entry(s->audiolist.next, struct usb_audiodev, list);
1903 list_del(&as->list);
1904 usbin_release(as);
1905 usbout_release(as);
1906 dmabuf_release(&as->usbin.dma);
1907 dmabuf_release(&as->usbout.dma);
1908 usb_free_urb(as->usbin.durb[0].urb);
1909 usb_free_urb(as->usbin.durb[1].urb);
1910 usb_free_urb(as->usbin.surb[0].urb);
1911 usb_free_urb(as->usbin.surb[1].urb);
1912 usb_free_urb(as->usbout.durb[0].urb);
1913 usb_free_urb(as->usbout.durb[1].urb);
1914 usb_free_urb(as->usbout.surb[0].urb);
1915 usb_free_urb(as->usbout.surb[1].urb);
1916 kfree(as);
1917 }
1918 while (!list_empty(&s->mixerlist)) {
1919 ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list);
1920 list_del(&ms->list);
1921 kfree(ms);
1922 }
1923 kfree(s);
1924}
1925
1926static inline int prog_dmabuf_in(struct usb_audiodev *as)
1927{
1928 usbin_stop(as);
1929 return dmabuf_init(&as->usbin.dma);
1930}
1931
1932static inline int prog_dmabuf_out(struct usb_audiodev *as)
1933{
1934 usbout_stop(as);
1935 return dmabuf_init(&as->usbout.dma);
1936}
1937
1938/* --------------------------------------------------------------------- */
1939
1940static int usb_audio_open_mixdev(struct inode *inode, struct file *file)
1941{
1942 unsigned int minor = iminor(inode);
1943 struct usb_mixerdev *ms;
1944 struct usb_audio_state *s;
1945
1946 down(&open_sem);
1947 list_for_each_entry(s, &audiodevs, audiodev) {
1948 list_for_each_entry(ms, &s->mixerlist, list) {
1949 if (ms->dev_mixer == minor)
1950 goto mixer_found;
1951 }
1952 }
1953 up(&open_sem);
1954 return -ENODEV;
1955
1956 mixer_found:
1957 if (!s->usbdev) {
1958 up(&open_sem);
1959 return -EIO;
1960 }
1961 file->private_data = ms;
1962 s->count++;
1963
1964 up(&open_sem);
1965 return nonseekable_open(inode, file);
1966}
1967
1968static int usb_audio_release_mixdev(struct inode *inode, struct file *file)
1969{
1970 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1971 struct usb_audio_state *s;
1972
1973 lock_kernel();
1974 s = ms->state;
1975 down(&open_sem);
1976 release(s);
1977 unlock_kernel();
1978 return 0;
1979}
1980
1981static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1982{
1983 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1984 int i, j, val;
1985 int __user *user_arg = (int __user *)arg;
1986
1987 if (!ms->state->usbdev)
1988 return -ENODEV;
1989
1990 if (cmd == SOUND_MIXER_INFO) {
1991 mixer_info info;
1992
1993 memset(&info, 0, sizeof(info));
1994 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
1995 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
1996 info.modify_counter = ms->modcnt;
1997 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1998 return -EFAULT;
1999 return 0;
2000 }
2001 if (cmd == SOUND_OLD_MIXER_INFO) {
2002 _old_mixer_info info;
2003
2004 memset(&info, 0, sizeof(info));
2005 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
2006 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
2007 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
2008 return -EFAULT;
2009 return 0;
2010 }
2011 if (cmd == OSS_GETVERSION)
2012 return put_user(SOUND_VERSION, user_arg);
2013 if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
2014 return -EINVAL;
2015 if (_IOC_DIR(cmd) == _IOC_READ) {
2016 switch (_IOC_NR(cmd)) {
2017 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2018 val = get_rec_src(ms);
2019 if (val < 0)
2020 return val;
2021 return put_user(val, user_arg);
2022
2023 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
2024 for (val = i = 0; i < ms->numch; i++)
2025 val |= 1 << ms->ch[i].osschannel;
2026 return put_user(val, user_arg);
2027
2028 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
2029 for (val = i = 0; i < ms->numch; i++)
2030 if (ms->ch[i].slctunitid)
2031 val |= 1 << ms->ch[i].osschannel;
2032 return put_user(val, user_arg);
2033
2034 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
2035 for (val = i = 0; i < ms->numch; i++)
2036 if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))
2037 val |= 1 << ms->ch[i].osschannel;
2038 return put_user(val, user_arg);
2039
2040 case SOUND_MIXER_CAPS:
2041 return put_user(SOUND_CAP_EXCL_INPUT, user_arg);
2042
2043 default:
2044 i = _IOC_NR(cmd);
2045 if (i >= SOUND_MIXER_NRDEVICES)
2046 return -EINVAL;
2047 for (j = 0; j < ms->numch; j++) {
2048 if (ms->ch[j].osschannel == i) {
2049 return put_user(ms->ch[j].value, user_arg);
2050 }
2051 }
2052 return -EINVAL;
2053 }
2054 }
2055 if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
2056 return -EINVAL;
2057 ms->modcnt++;
2058 switch (_IOC_NR(cmd)) {
2059 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2060 if (get_user(val, user_arg))
2061 return -EFAULT;
2062 return set_rec_src(ms, val);
2063
2064 default:
2065 i = _IOC_NR(cmd);
2066 if (i >= SOUND_MIXER_NRDEVICES)
2067 return -EINVAL;
2068 for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++);
2069 if (j >= ms->numch)
2070 return -EINVAL;
2071 if (get_user(val, user_arg))
2072 return -EFAULT;
2073 if (wrmixer(ms, j, val))
2074 return -EIO;
2075 return put_user(ms->ch[j].value, user_arg);
2076 }
2077}
2078
2079static /*const*/ struct file_operations usb_mixer_fops = {
2080 .owner = THIS_MODULE,
2081 .llseek = no_llseek,
2082 .ioctl = usb_audio_ioctl_mixdev,
2083 .open = usb_audio_open_mixdev,
2084 .release = usb_audio_release_mixdev,
2085};
2086
2087/* --------------------------------------------------------------------- */
2088
2089static int drain_out(struct usb_audiodev *as, int nonblock)
2090{
2091 DECLARE_WAITQUEUE(wait, current);
2092 unsigned long flags;
2093 int count, tmo;
2094
2095 if (as->usbout.dma.mapped || !as->usbout.dma.ready)
2096 return 0;
2097 usbout_start(as);
2098 add_wait_queue(&as->usbout.dma.wait, &wait);
2099 for (;;) {
2100 __set_current_state(TASK_INTERRUPTIBLE);
2101 spin_lock_irqsave(&as->lock, flags);
2102 count = as->usbout.dma.count;
2103 spin_unlock_irqrestore(&as->lock, flags);
2104 if (count <= 0)
2105 break;
2106 if (signal_pending(current))
2107 break;
2108 if (nonblock) {
2109 remove_wait_queue(&as->usbout.dma.wait, &wait);
2110 set_current_state(TASK_RUNNING);
2111 return -EBUSY;
2112 }
2113 tmo = 3 * HZ * count / as->usbout.dma.srate;
2114 tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format);
2115 if (!schedule_timeout(tmo + 1)) {
2116 printk(KERN_DEBUG "usbaudio: dma timed out??\n");
2117 break;
2118 }
2119 }
2120 remove_wait_queue(&as->usbout.dma.wait, &wait);
2121 set_current_state(TASK_RUNNING);
2122 if (signal_pending(current))
2123 return -ERESTARTSYS;
2124 return 0;
2125}
2126
2127/* --------------------------------------------------------------------- */
2128
2129static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2130{
2131 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2132 DECLARE_WAITQUEUE(wait, current);
2133 ssize_t ret = 0;
2134 unsigned long flags;
2135 unsigned int ptr;
2136 int cnt, err;
2137
2138 if (as->usbin.dma.mapped)
2139 return -ENXIO;
2140 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2141 return ret;
2142 if (!access_ok(VERIFY_WRITE, buffer, count))
2143 return -EFAULT;
2144 add_wait_queue(&as->usbin.dma.wait, &wait);
2145 while (count > 0) {
2146 spin_lock_irqsave(&as->lock, flags);
2147 ptr = as->usbin.dma.rdptr;
2148 cnt = as->usbin.dma.count;
2149 /* set task state early to avoid wakeup races */
2150 if (cnt <= 0)
2151 __set_current_state(TASK_INTERRUPTIBLE);
2152 spin_unlock_irqrestore(&as->lock, flags);
2153 if (cnt > count)
2154 cnt = count;
2155 if (cnt <= 0) {
2156 if (usbin_start(as)) {
2157 if (!ret)
2158 ret = -ENODEV;
2159 break;
2160 }
2161 if (file->f_flags & O_NONBLOCK) {
2162 if (!ret)
2163 ret = -EAGAIN;
2164 break;
2165 }
2166 schedule();
2167 if (signal_pending(current)) {
2168 if (!ret)
2169 ret = -ERESTARTSYS;
2170 break;
2171 }
2172 continue;
2173 }
2174 if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) {
2175 if (!ret)
2176 ret = err;
2177 break;
2178 }
2179 ptr += cnt;
2180 if (ptr >= as->usbin.dma.dmasize)
2181 ptr -= as->usbin.dma.dmasize;
2182 spin_lock_irqsave(&as->lock, flags);
2183 as->usbin.dma.rdptr = ptr;
2184 as->usbin.dma.count -= cnt;
2185 spin_unlock_irqrestore(&as->lock, flags);
2186 count -= cnt;
2187 buffer += cnt;
2188 ret += cnt;
2189 }
2190 __set_current_state(TASK_RUNNING);
2191 remove_wait_queue(&as->usbin.dma.wait, &wait);
2192 return ret;
2193}
2194
2195static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2196{
2197 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2198 DECLARE_WAITQUEUE(wait, current);
2199 ssize_t ret = 0;
2200 unsigned long flags;
2201 unsigned int ptr;
2202 unsigned int start_thr;
2203 int cnt, err;
2204
2205 if (as->usbout.dma.mapped)
2206 return -ENXIO;
2207 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2208 return ret;
2209 if (!access_ok(VERIFY_READ, buffer, count))
2210 return -EFAULT;
2211 start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES));
2212 add_wait_queue(&as->usbout.dma.wait, &wait);
2213 while (count > 0) {
2214#if 0
2215 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",
2216 count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize,
2217 as->usbout.flags, current->state);
2218#endif
2219 spin_lock_irqsave(&as->lock, flags);
2220 if (as->usbout.dma.count < 0) {
2221 as->usbout.dma.count = 0;
2222 as->usbout.dma.rdptr = as->usbout.dma.wrptr;
2223 }
2224 ptr = as->usbout.dma.wrptr;
2225 cnt = as->usbout.dma.dmasize - as->usbout.dma.count;
2226 /* set task state early to avoid wakeup races */
2227 if (cnt <= 0)
2228 __set_current_state(TASK_INTERRUPTIBLE);
2229 spin_unlock_irqrestore(&as->lock, flags);
2230 if (cnt > count)
2231 cnt = count;
2232 if (cnt <= 0) {
2233 if (usbout_start(as)) {
2234 if (!ret)
2235 ret = -ENODEV;
2236 break;
2237 }
2238 if (file->f_flags & O_NONBLOCK) {
2239 if (!ret)
2240 ret = -EAGAIN;
2241 break;
2242 }
2243 schedule();
2244 if (signal_pending(current)) {
2245 if (!ret)
2246 ret = -ERESTARTSYS;
2247 break;
2248 }
2249 continue;
2250 }
2251 if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) {
2252 if (!ret)
2253 ret = err;
2254 break;
2255 }
2256 ptr += cnt;
2257 if (ptr >= as->usbout.dma.dmasize)
2258 ptr -= as->usbout.dma.dmasize;
2259 spin_lock_irqsave(&as->lock, flags);
2260 as->usbout.dma.wrptr = ptr;
2261 as->usbout.dma.count += cnt;
2262 spin_unlock_irqrestore(&as->lock, flags);
2263 count -= cnt;
2264 buffer += cnt;
2265 ret += cnt;
2266 if (as->usbout.dma.count >= start_thr && usbout_start(as)) {
2267 if (!ret)
2268 ret = -ENODEV;
2269 break;
2270 }
2271 }
2272 __set_current_state(TASK_RUNNING);
2273 remove_wait_queue(&as->usbout.dma.wait, &wait);
2274 return ret;
2275}
2276
2277/* Called without the kernel lock - fine */
2278static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait)
2279{
2280 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2281 unsigned long flags;
2282 unsigned int mask = 0;
2283
2284 if (file->f_mode & FMODE_WRITE) {
2285 if (!as->usbout.dma.ready)
2286 prog_dmabuf_out(as);
2287 poll_wait(file, &as->usbout.dma.wait, wait);
2288 }
2289 if (file->f_mode & FMODE_READ) {
2290 if (!as->usbin.dma.ready)
2291 prog_dmabuf_in(as);
2292 poll_wait(file, &as->usbin.dma.wait, wait);
2293 }
2294 spin_lock_irqsave(&as->lock, flags);
2295 if (file->f_mode & FMODE_READ) {
2296 if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize)
2297 mask |= POLLIN | POLLRDNORM;
2298 }
2299 if (file->f_mode & FMODE_WRITE) {
2300 if (as->usbout.dma.mapped) {
2301 if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize)
2302 mask |= POLLOUT | POLLWRNORM;
2303 } else {
2304 if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize)
2305 mask |= POLLOUT | POLLWRNORM;
2306 }
2307 }
2308 spin_unlock_irqrestore(&as->lock, flags);
2309 return mask;
2310}
2311
2312static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma)
2313{
2314 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2315 struct dmabuf *db;
2316 int ret = -EINVAL;
2317
2318 lock_kernel();
2319 if (vma->vm_flags & VM_WRITE) {
2320 if ((ret = prog_dmabuf_out(as)) != 0)
2321 goto out;
2322 db = &as->usbout.dma;
2323 } else if (vma->vm_flags & VM_READ) {
2324 if ((ret = prog_dmabuf_in(as)) != 0)
2325 goto out;
2326 db = &as->usbin.dma;
2327 } else
2328 goto out;
2329
2330 ret = -EINVAL;
2331 if (vma->vm_pgoff != 0)
2332 goto out;
2333
2334 ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot);
2335out:
2336 unlock_kernel();
2337 return ret;
2338}
2339
2340static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2341{
2342 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2343 struct usb_audio_state *s = as->state;
2344 int __user *user_arg = (int __user *)arg;
2345 unsigned long flags;
2346 audio_buf_info abinfo;
2347 count_info cinfo;
2348 int val = 0;
2349 int val2, mapped, ret;
2350
2351 if (!s->usbdev)
2352 return -EIO;
2353 mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) ||
2354 ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped);
2355#if 0
2356 if (arg)
2357 get_user(val, (int *)arg);
2358 printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val)
2359#endif
2360 switch (cmd) {
2361 case OSS_GETVERSION:
2362 return put_user(SOUND_VERSION, user_arg);
2363
2364 case SNDCTL_DSP_SYNC:
2365 if (file->f_mode & FMODE_WRITE)
2366 return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/);
2367 return 0;
2368
2369 case SNDCTL_DSP_SETDUPLEX:
2370 return 0;
2371
2372 case SNDCTL_DSP_GETCAPS:
2373 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2374 DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg);
2375
2376 case SNDCTL_DSP_RESET:
2377 if (file->f_mode & FMODE_WRITE) {
2378 usbout_stop(as);
2379 as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0;
2380 }
2381 if (file->f_mode & FMODE_READ) {
2382 usbin_stop(as);
2383 as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0;
2384 }
2385 return 0;
2386
2387 case SNDCTL_DSP_SPEED:
2388 if (get_user(val, user_arg))
2389 return -EFAULT;
2390 if (val >= 0) {
2391 if (val < 4000)
2392 val = 4000;
2393 if (val > 100000)
2394 val = 100000;
2395 if (set_format(as, file->f_mode, AFMT_QUERY, val))
2396 return -EIO;
2397 }
2398 return put_user((file->f_mode & FMODE_READ) ?
2399 as->usbin.dma.srate : as->usbout.dma.srate,
2400 user_arg);
2401
2402 case SNDCTL_DSP_STEREO:
2403 if (get_user(val, user_arg))
2404 return -EFAULT;
2405 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2406 if (val)
2407 val2 |= AFMT_STEREO;
2408 else
2409 val2 &= ~AFMT_STEREO;
2410 if (set_format(as, file->f_mode, val2, 0))
2411 return -EIO;
2412 return 0;
2413
2414 case SNDCTL_DSP_CHANNELS:
2415 if (get_user(val, user_arg))
2416 return -EFAULT;
2417 if (val != 0) {
2418 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2419 if (val == 1)
2420 val2 &= ~AFMT_STEREO;
2421 else
2422 val2 |= AFMT_STEREO;
2423 if (set_format(as, file->f_mode, val2, 0))
2424 return -EIO;
2425 }
2426 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2427 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2428
2429 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2430 return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2431 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg);
2432
2433 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2434 if (get_user(val, user_arg))
2435 return -EFAULT;
2436 if (val != AFMT_QUERY) {
2437 if (hweight32(val) != 1)
2438 return -EINVAL;
2439 if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2440 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE)))
2441 return -EINVAL;
2442 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2443 val |= val2 & AFMT_STEREO;
2444 if (set_format(as, file->f_mode, val, 0))
2445 return -EIO;
2446 }
2447 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2448 return put_user(val2 & ~AFMT_STEREO, user_arg);
2449
2450 case SNDCTL_DSP_POST:
2451 return 0;
2452
2453 case SNDCTL_DSP_GETTRIGGER:
2454 val = 0;
2455 if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING)
2456 val |= PCM_ENABLE_INPUT;
2457 if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING)
2458 val |= PCM_ENABLE_OUTPUT;
2459 return put_user(val, user_arg);
2460
2461 case SNDCTL_DSP_SETTRIGGER:
2462 if (get_user(val, user_arg))
2463 return -EFAULT;
2464 if (file->f_mode & FMODE_READ) {
2465 if (val & PCM_ENABLE_INPUT) {
2466 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2467 return ret;
2468 if (usbin_start(as))
2469 return -ENODEV;
2470 } else
2471 usbin_stop(as);
2472 }
2473 if (file->f_mode & FMODE_WRITE) {
2474 if (val & PCM_ENABLE_OUTPUT) {
2475 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2476 return ret;
2477 if (usbout_start(as))
2478 return -ENODEV;
2479 } else
2480 usbout_stop(as);
2481 }
2482 return 0;
2483
2484 case SNDCTL_DSP_GETOSPACE:
2485 if (!(file->f_mode & FMODE_WRITE))
2486 return -EINVAL;
2487 if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0)
2488 return val;
2489 spin_lock_irqsave(&as->lock, flags);
2490 abinfo.fragsize = as->usbout.dma.fragsize;
2491 abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count;
2492 abinfo.fragstotal = as->usbout.dma.numfrag;
2493 abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift;
2494 spin_unlock_irqrestore(&as->lock, flags);
2495 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2496
2497 case SNDCTL_DSP_GETISPACE:
2498 if (!(file->f_mode & FMODE_READ))
2499 return -EINVAL;
2500 if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0)
2501 return val;
2502 spin_lock_irqsave(&as->lock, flags);
2503 abinfo.fragsize = as->usbin.dma.fragsize;
2504 abinfo.bytes = as->usbin.dma.count;
2505 abinfo.fragstotal = as->usbin.dma.numfrag;
2506 abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift;
2507 spin_unlock_irqrestore(&as->lock, flags);
2508 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2509
2510 case SNDCTL_DSP_NONBLOCK:
2511 file->f_flags |= O_NONBLOCK;
2512 return 0;
2513
2514 case SNDCTL_DSP_GETODELAY:
2515 if (!(file->f_mode & FMODE_WRITE))
2516 return -EINVAL;
2517 spin_lock_irqsave(&as->lock, flags);
2518 val = as->usbout.dma.count;
2519 spin_unlock_irqrestore(&as->lock, flags);
2520 return put_user(val, user_arg);
2521
2522 case SNDCTL_DSP_GETIPTR:
2523 if (!(file->f_mode & FMODE_READ))
2524 return -EINVAL;
2525 spin_lock_irqsave(&as->lock, flags);
2526 cinfo.bytes = as->usbin.dma.total_bytes;
2527 cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift;
2528 cinfo.ptr = as->usbin.dma.wrptr;
2529 if (as->usbin.dma.mapped)
2530 as->usbin.dma.count &= as->usbin.dma.fragsize-1;
2531 spin_unlock_irqrestore(&as->lock, flags);
2532 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2533 return -EFAULT;
2534 return 0;
2535
2536 case SNDCTL_DSP_GETOPTR:
2537 if (!(file->f_mode & FMODE_WRITE))
2538 return -EINVAL;
2539 spin_lock_irqsave(&as->lock, flags);
2540 cinfo.bytes = as->usbout.dma.total_bytes;
2541 cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift;
2542 cinfo.ptr = as->usbout.dma.rdptr;
2543 if (as->usbout.dma.mapped)
2544 as->usbout.dma.count &= as->usbout.dma.fragsize-1;
2545 spin_unlock_irqrestore(&as->lock, flags);
2546 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2547 return -EFAULT;
2548 return 0;
2549
2550 case SNDCTL_DSP_GETBLKSIZE:
2551 if (file->f_mode & FMODE_WRITE) {
2552 if ((val = prog_dmabuf_out(as)))
2553 return val;
2554 return put_user(as->usbout.dma.fragsize, user_arg);
2555 }
2556 if ((val = prog_dmabuf_in(as)))
2557 return val;
2558 return put_user(as->usbin.dma.fragsize, user_arg);
2559
2560 case SNDCTL_DSP_SETFRAGMENT:
2561 if (get_user(val, user_arg))
2562 return -EFAULT;
2563 if (file->f_mode & FMODE_READ) {
2564 as->usbin.dma.ossfragshift = val & 0xffff;
2565 as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff;
2566 if (as->usbin.dma.ossfragshift < 4)
2567 as->usbin.dma.ossfragshift = 4;
2568 if (as->usbin.dma.ossfragshift > 15)
2569 as->usbin.dma.ossfragshift = 15;
2570 if (as->usbin.dma.ossmaxfrags < 4)
2571 as->usbin.dma.ossmaxfrags = 4;
2572 }
2573 if (file->f_mode & FMODE_WRITE) {
2574 as->usbout.dma.ossfragshift = val & 0xffff;
2575 as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff;
2576 if (as->usbout.dma.ossfragshift < 4)
2577 as->usbout.dma.ossfragshift = 4;
2578 if (as->usbout.dma.ossfragshift > 15)
2579 as->usbout.dma.ossfragshift = 15;
2580 if (as->usbout.dma.ossmaxfrags < 4)
2581 as->usbout.dma.ossmaxfrags = 4;
2582 }
2583 return 0;
2584
2585 case SNDCTL_DSP_SUBDIVIDE:
2586 if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) ||
2587 (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision))
2588 return -EINVAL;
2589 if (get_user(val, user_arg))
2590 return -EFAULT;
2591 if (val != 1 && val != 2 && val != 4)
2592 return -EINVAL;
2593 if (file->f_mode & FMODE_READ)
2594 as->usbin.dma.subdivision = val;
2595 if (file->f_mode & FMODE_WRITE)
2596 as->usbout.dma.subdivision = val;
2597 return 0;
2598
2599 case SOUND_PCM_READ_RATE:
2600 return put_user((file->f_mode & FMODE_READ) ?
2601 as->usbin.dma.srate : as->usbout.dma.srate,
2602 user_arg);
2603
2604 case SOUND_PCM_READ_CHANNELS:
2605 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2606 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2607
2608 case SOUND_PCM_READ_BITS:
2609 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2610 return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg);
2611
2612 case SOUND_PCM_WRITE_FILTER:
2613 case SNDCTL_DSP_SETSYNCRO:
2614 case SOUND_PCM_READ_FILTER:
2615 return -EINVAL;
2616 }
2617 dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n"));
2618 return -ENOIOCTLCMD;
2619}
2620
2621static int usb_audio_open(struct inode *inode, struct file *file)
2622{
2623 unsigned int minor = iminor(inode);
2624 DECLARE_WAITQUEUE(wait, current);
2625 struct usb_audiodev *as;
2626 struct usb_audio_state *s;
2627
2628 for (;;) {
2629 down(&open_sem);
2630 list_for_each_entry(s, &audiodevs, audiodev) {
2631 list_for_each_entry(as, &s->audiolist, list) {
2632 if (!((as->dev_audio ^ minor) & ~0xf))
2633 goto device_found;
2634 }
2635 }
2636 up(&open_sem);
2637 return -ENODEV;
2638
2639 device_found:
2640 if (!s->usbdev) {
2641 up(&open_sem);
2642 return -EIO;
2643 }
2644 /* wait for device to become free */
2645 if (!(as->open_mode & file->f_mode))
2646 break;
2647 if (file->f_flags & O_NONBLOCK) {
2648 up(&open_sem);
2649 return -EBUSY;
2650 }
2651 __set_current_state(TASK_INTERRUPTIBLE);
2652 add_wait_queue(&open_wait, &wait);
2653 up(&open_sem);
2654 schedule();
2655 __set_current_state(TASK_RUNNING);
2656 remove_wait_queue(&open_wait, &wait);
2657 if (signal_pending(current))
2658 return -ERESTARTSYS;
2659 }
2660 if (file->f_mode & FMODE_READ)
2661 as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0;
2662 if (file->f_mode & FMODE_WRITE)
2663 as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0;
2664 if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) {
2665 up(&open_sem);
2666 return -EIO;
2667 }
2668 file->private_data = as;
2669 as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2670 s->count++;
2671 up(&open_sem);
2672 return nonseekable_open(inode, file);
2673}
2674
2675static int usb_audio_release(struct inode *inode, struct file *file)
2676{
2677 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2678 struct usb_audio_state *s;
2679 struct usb_device *dev;
2680
2681 lock_kernel();
2682 s = as->state;
2683 dev = s->usbdev;
2684 if (file->f_mode & FMODE_WRITE)
2685 drain_out(as, file->f_flags & O_NONBLOCK);
2686 down(&open_sem);
2687 if (file->f_mode & FMODE_WRITE) {
2688 usbout_stop(as);
2689 if (dev && as->usbout.interface >= 0)
2690 usb_set_interface(dev, as->usbout.interface, 0);
2691 dmabuf_release(&as->usbout.dma);
2692 usbout_release(as);
2693 }
2694 if (file->f_mode & FMODE_READ) {
2695 usbin_stop(as);
2696 if (dev && as->usbin.interface >= 0)
2697 usb_set_interface(dev, as->usbin.interface, 0);
2698 dmabuf_release(&as->usbin.dma);
2699 usbin_release(as);
2700 }
2701 as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
2702 release(s);
2703 wake_up(&open_wait);
2704 unlock_kernel();
2705 return 0;
2706}
2707
2708static /*const*/ struct file_operations usb_audio_fops = {
2709 .owner = THIS_MODULE,
2710 .llseek = no_llseek,
2711 .read = usb_audio_read,
2712 .write = usb_audio_write,
2713 .poll = usb_audio_poll,
2714 .ioctl = usb_audio_ioctl,
2715 .mmap = usb_audio_mmap,
2716 .open = usb_audio_open,
2717 .release = usb_audio_release,
2718};
2719
2720/* --------------------------------------------------------------------- */
2721
2722static int usb_audio_probe(struct usb_interface *iface,
2723 const struct usb_device_id *id);
2724static void usb_audio_disconnect(struct usb_interface *iface);
2725
2726static struct usb_device_id usb_audio_ids [] = {
2727 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2728 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1},
2729 { } /* Terminating entry */
2730};
2731
2732MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2733
2734static struct usb_driver usb_audio_driver = {
2735 .name = "audio",
2736 .probe = usb_audio_probe,
2737 .disconnect = usb_audio_disconnect,
2738 .id_table = usb_audio_ids,
2739};
2740
2741static void *find_descriptor(void *descstart, unsigned int desclen, void *after,
2742 u8 dtype, int iface, int altsetting)
2743{
2744 u8 *p, *end, *next;
2745 int ifc = -1, as = -1;
2746
2747 p = descstart;
2748 end = p + desclen;
2749 for (; p < end;) {
2750 if (p[0] < 2)
2751 return NULL;
2752 next = p + p[0];
2753 if (next > end)
2754 return NULL;
2755 if (p[1] == USB_DT_INTERFACE) {
2756 /* minimum length of interface descriptor */
2757 if (p[0] < 9)
2758 return NULL;
2759 ifc = p[2];
2760 as = p[3];
2761 }
2762 if (p[1] == dtype && (!after || (void *)p > after) &&
2763 (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) {
2764 return p;
2765 }
2766 p = next;
2767 }
2768 return NULL;
2769}
2770
2771static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting)
2772{
2773 unsigned char *p;
2774
2775 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting);
2776 while (p) {
2777 if (p[0] >= 3 && p[2] == dsubtype)
2778 return p;
2779 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting);
2780 }
2781 return NULL;
2782}
2783
2784static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface)
2785{
2786 unsigned char *p;
2787
2788 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1);
2789 while (p) {
2790 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
2791 return p;
2792 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1);
2793 }
2794 return NULL;
2795}
2796
2797static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout)
2798{
2799 struct usb_device *dev = s->usbdev;
2800 struct usb_audiodev *as;
2801 struct usb_host_interface *alts;
2802 struct usb_interface *iface;
2803 struct audioformat *fp;
2804 unsigned char *fmt, *csep;
2805 unsigned int i, j, k, format, idx;
2806
2807 if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL)))
2808 return;
2809 memset(as, 0, sizeof(struct usb_audiodev));
2810 init_waitqueue_head(&as->usbin.dma.wait);
2811 init_waitqueue_head(&as->usbout.dma.wait);
2812 spin_lock_init(&as->lock);
2813 as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2814 as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2815 as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2816 as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2817 as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2818 as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2819 as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2820 as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2821 if ((!as->usbin.durb[0].urb) ||
2822 (!as->usbin.durb[1].urb) ||
2823 (!as->usbin.surb[0].urb) ||
2824 (!as->usbin.surb[1].urb) ||
2825 (!as->usbout.durb[0].urb) ||
2826 (!as->usbout.durb[1].urb) ||
2827 (!as->usbout.surb[0].urb) ||
2828 (!as->usbout.surb[1].urb)) {
2829 usb_free_urb(as->usbin.durb[0].urb);
2830 usb_free_urb(as->usbin.durb[1].urb);
2831 usb_free_urb(as->usbin.surb[0].urb);
2832 usb_free_urb(as->usbin.surb[1].urb);
2833 usb_free_urb(as->usbout.durb[0].urb);
2834 usb_free_urb(as->usbout.durb[1].urb);
2835 usb_free_urb(as->usbout.surb[0].urb);
2836 usb_free_urb(as->usbout.surb[1].urb);
2837 kfree(as);
2838 return;
2839 }
2840 as->state = s;
2841 as->usbin.interface = asifin;
2842 as->usbout.interface = asifout;
2843 /* search for input formats */
2844 if (asifin >= 0) {
2845 as->usbin.flags = FLG_CONNECTED;
2846 iface = usb_ifnum_to_if(dev, asifin);
2847 for (idx = 0; idx < iface->num_altsetting; idx++) {
2848 alts = &iface->altsetting[idx];
2849 i = alts->desc.bAlternateSetting;
2850 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2851 continue;
2852 if (alts->desc.bNumEndpoints < 1) {
2853 if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */
2854 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2855 dev->devnum, asifin, i);
2856 }
2857 continue;
2858 }
2859 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2860 !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2861 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n",
2862 dev->devnum, asifin, i);
2863 continue;
2864 }
2865 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i);
2866 if (!fmt) {
2867 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2868 dev->devnum, asifin, i);
2869 continue;
2870 }
2871 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2872 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2873 dev->devnum, asifin, i);
2874 continue;
2875 }
2876 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2877 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i);
2878 if (!fmt) {
2879 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2880 dev->devnum, asifin, i);
2881 continue;
2882 }
2883 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2884 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2885 dev->devnum, asifin, i);
2886 continue;
2887 }
2888 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2889 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2890 dev->devnum, asifin, i, fmt[4], fmt[5]);
2891 continue;
2892 }
2893 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i);
2894 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2895 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2896 dev->devnum, asifin, i);
2897 continue;
2898 }
2899 if (as->numfmtin >= MAXFORMATS)
2900 continue;
2901 fp = &as->fmtin[as->numfmtin++];
2902 if (fmt[5] == 2)
2903 format &= (AFMT_U16_LE | AFMT_S16_LE);
2904 else
2905 format &= (AFMT_U8 | AFMT_S8);
2906 if (fmt[4] == 2)
2907 format |= AFMT_STEREO;
2908 fp->format = format;
2909 fp->altsetting = i;
2910 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2911 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo);
2912 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
2913 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
2914 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k);
2915 if (k > fp->sratehi)
2916 fp->sratehi = k;
2917 if (k < fp->sratelo)
2918 fp->sratelo = k;
2919 }
2920 fp->attributes = csep[3];
2921 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
2922 dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
2923 }
2924 }
2925 /* search for output formats */
2926 if (asifout >= 0) {
2927 as->usbout.flags = FLG_CONNECTED;
2928 iface = usb_ifnum_to_if(dev, asifout);
2929 for (idx = 0; idx < iface->num_altsetting; idx++) {
2930 alts = &iface->altsetting[idx];
2931 i = alts->desc.bAlternateSetting;
2932 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2933 continue;
2934 if (alts->desc.bNumEndpoints < 1) {
2935 /* altsetting 0 should never have iso EPs */
2936 if (i != 0)
2937 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2938 dev->devnum, asifout, i);
2939 continue;
2940 }
2941 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2942 (alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2943 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n",
2944 dev->devnum, asifout, i);
2945 continue;
2946 }
2947 /* See USB audio formats manual, section 2 */
2948 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i);
2949 if (!fmt) {
2950 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2951 dev->devnum, asifout, i);
2952 continue;
2953 }
2954 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2955 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2956 dev->devnum, asifout, i);
2957 continue;
2958 }
2959 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2960 /* Dallas DS4201 workaround */
2961 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2962 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2963 format = (AFMT_S16_LE | AFMT_S8);
2964 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i);
2965 if (!fmt) {
2966 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2967 dev->devnum, asifout, i);
2968 continue;
2969 }
2970 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2971 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2972 dev->devnum, asifout, i);
2973 continue;
2974 }
2975 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2976 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2977 dev->devnum, asifout, i, fmt[4], fmt[5]);
2978 continue;
2979 }
2980 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i);
2981 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2982 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2983 dev->devnum, asifout, i);
2984 continue;
2985 }
2986 if (as->numfmtout >= MAXFORMATS)
2987 continue;
2988 fp = &as->fmtout[as->numfmtout++];
2989 if (fmt[5] == 2)
2990 format &= (AFMT_U16_LE | AFMT_S16_LE);
2991 else
2992 format &= (AFMT_U8 | AFMT_S8);
2993 if (fmt[4] == 2)
2994 format |= AFMT_STEREO;
2995 fp->format = format;
2996 fp->altsetting = i;
2997 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2998 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo);
2999 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
3000 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
3001 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k);
3002 if (k > fp->sratehi)
3003 fp->sratehi = k;
3004 if (k < fp->sratelo)
3005 fp->sratelo = k;
3006 }
3007 fp->attributes = csep[3];
3008 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
3009 dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
3010 }
3011 }
3012 if (as->numfmtin == 0 && as->numfmtout == 0) {
3013 usb_free_urb(as->usbin.durb[0].urb);
3014 usb_free_urb(as->usbin.durb[1].urb);
3015 usb_free_urb(as->usbin.surb[0].urb);
3016 usb_free_urb(as->usbin.surb[1].urb);
3017 usb_free_urb(as->usbout.durb[0].urb);
3018 usb_free_urb(as->usbout.durb[1].urb);
3019 usb_free_urb(as->usbout.surb[0].urb);
3020 usb_free_urb(as->usbout.surb[1].urb);
3021 kfree(as);
3022 return;
3023 }
3024 if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) {
3025 printk(KERN_ERR "usbaudio: cannot register dsp\n");
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);
3034 kfree(as);
3035 return;
3036 }
3037 printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio);
3038 /* everything successful */
3039 list_add_tail(&as->list, &s->audiolist);
3040}
3041
3042struct consmixstate {
3043 struct usb_audio_state *s;
3044 unsigned char *buffer;
3045 unsigned int buflen;
3046 unsigned int ctrlif;
3047 struct mixerchannel mixch[SOUND_MIXER_NRDEVICES];
3048 unsigned int nrmixch;
3049 unsigned int mixchmask;
3050 unsigned long unitbitmap[32/sizeof(unsigned long)];
3051 /* return values */
3052 unsigned int nrchannels;
3053 unsigned int termtype;
3054 unsigned int chconfig;
3055};
3056
3057static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr)
3058{
3059 struct mixerchannel *c;
3060
3061 if (nr >= SOUND_MIXER_NRDEVICES) {
3062 printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr);
3063 return NULL;
3064 }
3065 if (!(state->mixchmask & (1 << nr))) {
3066 printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr);
3067 return NULL;
3068 }
3069 c = &state->mixch[state->nrmixch++];
3070 c->osschannel = nr;
3071 state->mixchmask &= ~(1 << nr);
3072 return c;
3073}
3074
3075static unsigned int getvolchannel(struct consmixstate *state)
3076{
3077 unsigned int u;
3078
3079 if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME))
3080 return SOUND_MIXER_VOLUME;
3081 if ((state->termtype & 0xff00) == 0x0100) {
3082 if (state->mixchmask & SOUND_MASK_PCM)
3083 return SOUND_MIXER_PCM;
3084 if (state->mixchmask & SOUND_MASK_ALTPCM)
3085 return SOUND_MIXER_ALTPCM;
3086 }
3087 if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC))
3088 return SOUND_MIXER_MIC;
3089 if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER))
3090 return SOUND_MIXER_SPEAKER;
3091 if ((state->termtype & 0xff00) == 0x0500) {
3092 if (state->mixchmask & SOUND_MASK_PHONEIN)
3093 return SOUND_MIXER_PHONEIN;
3094 if (state->mixchmask & SOUND_MASK_PHONEOUT)
3095 return SOUND_MIXER_PHONEOUT;
3096 }
3097 if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO))
3098 return SOUND_MIXER_RADIO;
3099 if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO))
3100 return SOUND_MIXER_VIDEO;
3101 u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 |
3102 SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3));
3103 return u-1;
3104}
3105
3106static void prepmixch(struct consmixstate *state)
3107{
3108 struct usb_device *dev = state->s->usbdev;
3109 struct mixerchannel *ch;
3110 unsigned char *buf;
3111 __s16 v1;
3112 unsigned int v2, v3;
3113
3114 if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES)
3115 return;
3116 buf = kmalloc(sizeof(*buf) * 2, GFP_KERNEL);
3117 if (!buf) {
3118 printk(KERN_ERR "prepmixch: out of memory\n") ;
3119 return;
3120 }
3121
3122 ch = &state->mixch[state->nrmixch-1];
3123 switch (ch->selector) {
3124 case 0: /* mixer unit request */
3125 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3126 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3127 goto err;
3128 ch->minval = buf[0] | (buf[1] << 8);
3129 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3130 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3131 goto err;
3132 ch->maxval = buf[0] | (buf[1] << 8);
3133 v2 = ch->maxval - ch->minval;
3134 if (!v2)
3135 v2 = 1;
3136 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3137 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3138 goto err;
3139 v1 = buf[0] | (buf[1] << 8);
3140 v3 = v1 - ch->minval;
3141 v3 = 100 * v3 / v2;
3142 if (v3 > 100)
3143 v3 = 100;
3144 ch->value = v3;
3145 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3146 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3147 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
3148 state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3149 goto err;
3150 v1 = buf[0] | (buf[1] << 8);
3151 v3 = v1 - ch->minval;
3152 v3 = 100 * v3 / v2;
3153 if (v3 > 100)
3154 v3 = 100;
3155 }
3156 ch->value |= v3 << 8;
3157 break;
3158
3159 /* various feature unit controls */
3160 case VOLUME_CONTROL:
3161 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3162 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3163 goto err;
3164 ch->minval = buf[0] | (buf[1] << 8);
3165 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3166 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3167 goto err;
3168 ch->maxval = buf[0] | (buf[1] << 8);
3169 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3170 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3171 goto err;
3172 v1 = buf[0] | (buf[1] << 8);
3173 v2 = ch->maxval - ch->minval;
3174 v3 = v1 - ch->minval;
3175 if (!v2)
3176 v2 = 1;
3177 v3 = 100 * v3 / v2;
3178 if (v3 > 100)
3179 v3 = 100;
3180 ch->value = v3;
3181 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
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 + 1), state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3184 goto err;
3185 v1 = buf[0] | (buf[1] << 8);
3186 v3 = v1 - ch->minval;
3187 v3 = 100 * v3 / v2;
3188 if (v3 > 100)
3189 v3 = 100;
3190 }
3191 ch->value |= v3 << 8;
3192 break;
3193
3194 case BASS_CONTROL:
3195 case MID_CONTROL:
3196 case TREBLE_CONTROL:
3197 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3198 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3199 goto err;
3200 ch->minval = buf[0] << 8;
3201 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3202 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3203 goto err;
3204 ch->maxval = buf[0] << 8;
3205 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3206 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3207 goto err;
3208 v1 = buf[0] << 8;
3209 v2 = ch->maxval - ch->minval;
3210 v3 = v1 - ch->minval;
3211 if (!v2)
3212 v2 = 1;
3213 v3 = 100 * v3 / v2;
3214 if (v3 > 100)
3215 v3 = 100;
3216 ch->value = v3;
3217 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
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 + 1), state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3220 goto err;
3221 v1 = buf[0] << 8;
3222 v3 = v1 - ch->minval;
3223 v3 = 100 * v3 / v2;
3224 if (v3 > 100)
3225 v3 = 100;
3226 }
3227 ch->value |= v3 << 8;
3228 break;
3229
3230 default:
3231 goto err;
3232 }
3233
3234 freebuf:
3235 kfree(buf);
3236 return;
3237 err:
3238 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
3239 dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector);
3240 if (state->nrmixch)
3241 state->nrmixch--;
3242 goto freebuf;
3243}
3244
3245
3246static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid);
3247
3248static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch)
3249{
3250 unsigned int idx;
3251
3252 idx = inidx*numoch;
3253 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3254 return 0;
3255 if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
3256 return 1;
3257 idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT);
3258 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3259 return 0;
3260 return 1;
3261}
3262
3263static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer)
3264{
3265 unsigned int nroutch = mixer[5+mixer[4]];
3266 unsigned int chidx[SOUND_MIXER_NRDEVICES+1];
3267 unsigned int termt[SOUND_MIXER_NRDEVICES];
3268 unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0;
3269 unsigned char *bmap = &mixer[9+mixer[4]];
3270 unsigned int bmapsize;
3271 struct mixerchannel *ch;
3272 unsigned int i;
3273
3274 if (!mixer[4]) {
3275 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]);
3276 return;
3277 }
3278 if (mixer[4] > SOUND_MIXER_NRDEVICES) {
3279 printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]);
3280 return;
3281 }
3282 chidx[0] = 0;
3283 for (i = 0; i < mixer[4]; i++) {
3284 usb_audio_recurseunit(state, mixer[5+i]);
3285 chidx[i+1] = chidx[i] + state->nrchannels;
3286 termt[i] = state->termtype;
3287 }
3288 state->termtype = 0;
3289 state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8);
3290 bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3;
3291 bmap += bmapsize - 1;
3292 if (mixer[0] < 10+mixer[4]+bmapsize) {
3293 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]);
3294 return;
3295 }
3296 for (i = 0; i < mixer[4]; i++) {
3297 state->termtype = termt[i];
3298 if (chidx[i+1]-chidx[i] >= 2) {
3299 flg |= MIXFLG_STEREOIN;
3300 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3301 ch = getmixchannel(state, getvolchannel(state));
3302 if (ch) {
3303 ch->unitid = mixer[3];
3304 ch->selector = 0;
3305 ch->chnum = chidx[i]+1;
3306 ch->flags = flg;
3307 prepmixch(state);
3308 }
3309 continue;
3310 }
3311 }
3312 flg &= ~MIXFLG_STEREOIN;
3313 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3314 ch = getmixchannel(state, getvolchannel(state));
3315 if (ch) {
3316 ch->unitid = mixer[3];
3317 ch->selector = 0;
3318 ch->chnum = chidx[i]+1;
3319 ch->flags = flg;
3320 prepmixch(state);
3321 }
3322 }
3323 }
3324 state->termtype = 0;
3325}
3326
3327static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid)
3328{
3329 unsigned int i;
3330
3331 for (i = 0; i < state->nrmixch; i++)
3332 if (state->mixch[i].unitid == unitid)
3333 return &state->mixch[i];
3334 return NULL;
3335}
3336
3337static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector)
3338{
3339 unsigned int chnum, i, mixch;
3340 struct mixerchannel *mch;
3341
3342 if (!selector[4]) {
3343 printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]);
3344 return;
3345 }
3346 mixch = state->nrmixch;
3347 usb_audio_recurseunit(state, selector[5]);
3348 if (state->nrmixch != mixch) {
3349 mch = &state->mixch[state->nrmixch-1];
3350 mch->slctunitid = selector[3] | (1 << 8);
3351 } else if ((mch = slctsrc_findunit(state, selector[5]))) {
3352 mch->slctunitid = selector[3] | (1 << 8);
3353 } else {
3354 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]);
3355 }
3356 chnum = state->nrchannels;
3357 for (i = 1; i < selector[4]; i++) {
3358 mixch = state->nrmixch;
3359 usb_audio_recurseunit(state, selector[5+i]);
3360 if (chnum != state->nrchannels) {
3361 printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]);
3362 state->termtype = 0;
3363 state->chconfig = 0;
3364 state->nrchannels = 0;
3365 return;
3366 }
3367 if (state->nrmixch != mixch) {
3368 mch = &state->mixch[state->nrmixch-1];
3369 mch->slctunitid = selector[3] | ((i + 1) << 8);
3370 } else if ((mch = slctsrc_findunit(state, selector[5+i]))) {
3371 mch->slctunitid = selector[3] | ((i + 1) << 8);
3372 } else {
3373 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1);
3374 }
3375 }
3376 state->termtype = 0;
3377 state->chconfig = 0;
3378}
3379
3380/* in the future we might try to handle 3D etc. effect units */
3381
3382static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc)
3383{
3384 unsigned int i;
3385
3386 for (i = 0; i < proc[6]; i++)
3387 usb_audio_recurseunit(state, proc[7+i]);
3388 state->nrchannels = proc[7+proc[6]];
3389 state->termtype = 0;
3390 state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8);
3391}
3392
3393
3394/* See Audio Class Spec, section 4.3.2.5 */
3395static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr)
3396{
3397 struct mixerchannel *ch;
3398 unsigned short chftr, mchftr;
3399#if 0
3400 struct usb_device *dev = state->s->usbdev;
3401 unsigned char data[1];
3402#endif
3403 unsigned char nr_logical_channels, i;
3404
3405 usb_audio_recurseunit(state, ftr[4]);
3406
3407 if (ftr[5] == 0 ) {
3408 printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]);
3409 return;
3410 }
3411
3412 if (state->nrchannels == 0) {
3413 printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]);
3414 return;
3415 }
3416 if (state->nrchannels > 2)
3417 printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]);
3418
3419 nr_logical_channels=(ftr[0]-7)/ftr[5]-1;
3420
3421 if (nr_logical_channels != state->nrchannels) {
3422 printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels);
3423
3424 if (state->nrchannels == 1 && nr_logical_channels==0) {
3425 printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n");
3426 } else if (state->nrchannels == 1 && nr_logical_channels==2) {
3427 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");
3428 state->nrchannels=nr_logical_channels;
3429 } else {
3430 printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n");
3431 }
3432 }
3433
3434 /* There is always a master channel */
3435 mchftr = ftr[6];
3436 /* Binary AND over logical channels if they exist */
3437 if (nr_logical_channels) {
3438 chftr = ftr[6+ftr[5]];
3439 for (i = 2; i <= nr_logical_channels; i++)
3440 chftr &= ftr[6+i*ftr[5]];
3441 } else {
3442 chftr = 0;
3443 }
3444
3445 /* volume control */
3446 if (chftr & 2) {
3447 ch = getmixchannel(state, getvolchannel(state));
3448 if (ch) {
3449 ch->unitid = ftr[3];
3450 ch->selector = VOLUME_CONTROL;
3451 ch->chnum = 1;
3452 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3453 prepmixch(state);
3454 }
3455 } else if (mchftr & 2) {
3456 ch = getmixchannel(state, getvolchannel(state));
3457 if (ch) {
3458 ch->unitid = ftr[3];
3459 ch->selector = VOLUME_CONTROL;
3460 ch->chnum = 0;
3461 ch->flags = 0;
3462 prepmixch(state);
3463 }
3464 }
3465 /* bass control */
3466 if (chftr & 4) {
3467 ch = getmixchannel(state, SOUND_MIXER_BASS);
3468 if (ch) {
3469 ch->unitid = ftr[3];
3470 ch->selector = BASS_CONTROL;
3471 ch->chnum = 1;
3472 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3473 prepmixch(state);
3474 }
3475 } else if (mchftr & 4) {
3476 ch = getmixchannel(state, SOUND_MIXER_BASS);
3477 if (ch) {
3478 ch->unitid = ftr[3];
3479 ch->selector = BASS_CONTROL;
3480 ch->chnum = 0;
3481 ch->flags = 0;
3482 prepmixch(state);
3483 }
3484 }
3485 /* treble control */
3486 if (chftr & 16) {
3487 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3488 if (ch) {
3489 ch->unitid = ftr[3];
3490 ch->selector = TREBLE_CONTROL;
3491 ch->chnum = 1;
3492 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3493 prepmixch(state);
3494 }
3495 } else if (mchftr & 16) {
3496 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3497 if (ch) {
3498 ch->unitid = ftr[3];
3499 ch->selector = TREBLE_CONTROL;
3500 ch->chnum = 0;
3501 ch->flags = 0;
3502 prepmixch(state);
3503 }
3504 }
3505#if 0
3506 /* if there are mute controls, unmute them */
3507 /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */
3508 if ((chftr & 1) || (mchftr & 1)) {
3509 printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif);
3510 data[0] = 0;
3511 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
3512 (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, 1000) < 0)
3513 printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif);
3514 }
3515#endif
3516}
3517
3518static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid)
3519{
3520 unsigned char *p1;
3521 unsigned int i, j;
3522
3523 if (test_and_set_bit(unitid, state->unitbitmap)) {
3524 printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid);
3525 return;
3526 }
3527 p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif);
3528 if (!p1) {
3529 printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
3530 return;
3531 }
3532 state->nrchannels = 0;
3533 state->termtype = 0;
3534 state->chconfig = 0;
3535 switch (p1[2]) {
3536 case INPUT_TERMINAL:
3537 if (p1[0] < 12) {
3538 printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid);
3539 return;
3540 }
3541 state->nrchannels = p1[7];
3542 state->termtype = p1[4] | (p1[5] << 8);
3543 state->chconfig = p1[8] | (p1[9] << 8);
3544 return;
3545
3546 case MIXER_UNIT:
3547 if (p1[0] < 10 || p1[0] < 10+p1[4]) {
3548 printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid);
3549 return;
3550 }
3551 usb_audio_mixerunit(state, p1);
3552 return;
3553
3554 case SELECTOR_UNIT:
3555 if (p1[0] < 6 || p1[0] < 6+p1[4]) {
3556 printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid);
3557 return;
3558 }
3559 usb_audio_selectorunit(state, p1);
3560 return;
3561
3562 case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */
3563 if (p1[0] < 7 || p1[0] < 7+p1[5]) {
3564 printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
3565 return;
3566 }
3567 usb_audio_featureunit(state, p1);
3568 return;
3569
3570 case PROCESSING_UNIT:
3571 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3572 printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid);
3573 return;
3574 }
3575 usb_audio_processingunit(state, p1);
3576 return;
3577
3578 case EXTENSION_UNIT:
3579 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3580 printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid);
3581 return;
3582 }
3583 for (j = i = 0; i < p1[6]; i++) {
3584 usb_audio_recurseunit(state, p1[7+i]);
3585 if (!i)
3586 j = state->termtype;
3587 else if (j != state->termtype)
3588 j = 0;
3589 }
3590 state->nrchannels = p1[7+p1[6]];
3591 state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8);
3592 state->termtype = j;
3593 return;
3594
3595 default:
3596 printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
3597 return;
3598 }
3599}
3600
3601static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm)
3602{
3603 struct usb_mixerdev *ms;
3604 struct consmixstate state;
3605
3606 memset(&state, 0, sizeof(state));
3607 state.s = s;
3608 state.nrmixch = 0;
3609 state.mixchmask = ~0;
3610 state.buffer = buffer;
3611 state.buflen = buflen;
3612 state.ctrlif = ctrlif;
3613 set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */
3614 printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n",
3615 oterm[3], oterm[4] | (oterm[5] << 8));
3616 usb_audio_recurseunit(&state, oterm[7]);
3617 if (!state.nrmixch) {
3618 printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]);
3619 return;
3620 }
3621 if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL)))
3622 return;
3623 memset(ms, 0, sizeof(struct usb_mixerdev));
3624 memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel));
3625 ms->state = s;
3626 ms->iface = ctrlif;
3627 ms->numch = state.nrmixch;
3628 if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) {
3629 printk(KERN_ERR "usbaudio: cannot register mixer\n");
3630 kfree(ms);
3631 return;
3632 }
3633 printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer);
3634 list_add_tail(&ms->list, &s->mixerlist);
3635}
3636
3637/* arbitrary limit, we won't check more interfaces than this */
3638#define USB_MAXINTERFACES 32
3639
3640static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif)
3641{
3642 struct usb_audio_state *s;
3643 struct usb_interface *iface;
3644 struct usb_host_interface *alt;
3645 unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES];
3646 unsigned char *p1;
3647 unsigned int i, j, k, numifin = 0, numifout = 0;
3648
3649 if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL)))
3650 return NULL;
3651 memset(s, 0, sizeof(struct usb_audio_state));
3652 INIT_LIST_HEAD(&s->audiolist);
3653 INIT_LIST_HEAD(&s->mixerlist);
3654 s->usbdev = dev;
3655 s->count = 1;
3656
3657 /* find audiocontrol interface */
3658 if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) {
3659 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n",
3660 dev->devnum, ctrlif);
3661 goto ret;
3662 }
3663 if (p1[0] < 8 + p1[7]) {
3664 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n",
3665 dev->devnum, ctrlif);
3666 goto ret;
3667 }
3668 if (!p1[7])
3669 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n",
3670 dev->devnum, ctrlif);
3671 for (i = 0; i < p1[7]; i++) {
3672 j = p1[8+i];
3673 iface = usb_ifnum_to_if(dev, j);
3674 if (!iface) {
3675 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n",
3676 dev->devnum, ctrlif, j);
3677 continue;
3678 }
3679 if (iface->num_altsetting == 1) {
3680 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif);
3681 continue;
3682 }
3683 alt = usb_altnum_to_altsetting(iface, 0);
3684 if (!alt) {
3685 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n",
3686 dev->devnum, ctrlif, j);
3687 continue;
3688 }
3689 if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) {
3690 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n",
3691 dev->devnum, ctrlif, j);
3692 continue;
3693 }
3694 if (alt->desc.bInterfaceSubClass == 3) {
3695 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n",
3696 dev->devnum, ctrlif, j);
3697 continue;
3698 }
3699 if (alt->desc.bInterfaceSubClass != 2) {
3700 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n",
3701 dev->devnum, ctrlif, j);
3702 continue;
3703 }
3704 if (alt->desc.bNumEndpoints > 0) {
3705 /* Check all endpoints; should they all have a bandwidth of 0 ? */
3706 for (k = 0; k < alt->desc.bNumEndpoints; k++) {
3707 if (le16_to_cpu(alt->endpoint[k].desc.wMaxPacketSize) > 0) {
3708 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k);
3709 break;
3710 }
3711 }
3712 if (k < alt->desc.bNumEndpoints)
3713 continue;
3714 }
3715
3716 alt = usb_altnum_to_altsetting(iface, 1);
3717 if (!alt) {
3718 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n",
3719 dev->devnum, ctrlif, j);
3720 continue;
3721 }
3722 if (alt->desc.bNumEndpoints < 1) {
3723 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n",
3724 dev->devnum, ctrlif, j);
3725 continue;
3726 }
3727 /* note: this requires the data endpoint to be ep0 and the optional sync
3728 ep to be ep1, which seems to be the case */
3729 if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) {
3730 if (numifin < USB_MAXINTERFACES) {
3731 ifin[numifin++] = j;
3732 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3733 }
3734 } else {
3735 if (numifout < USB_MAXINTERFACES) {
3736 ifout[numifout++] = j;
3737 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3738 }
3739 }
3740 }
3741 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n",
3742 dev->devnum, ctrlif, numifin, numifout);
3743 for (i = 0; i < numifin && i < numifout; i++)
3744 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]);
3745 for (j = i; j < numifin; j++)
3746 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1);
3747 for (j = i; j < numifout; j++)
3748 usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]);
3749 /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */
3750 p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1);
3751 while (p1) {
3752 if (p1[0] >= 9)
3753 usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1);
3754 p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1);
3755 }
3756
3757ret:
3758 if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) {
3759 kfree(s);
3760 return NULL;
3761 }
3762 /* everything successful */
3763 down(&open_sem);
3764 list_add_tail(&s->audiodev, &audiodevs);
3765 up(&open_sem);
3766 printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s);
3767 return s;
3768}
3769
3770/* we only care for the currently active configuration */
3771
3772static int usb_audio_probe(struct usb_interface *intf,
3773 const struct usb_device_id *id)
3774{
3775 struct usb_device *dev = interface_to_usbdev (intf);
3776 struct usb_audio_state *s;
3777 unsigned char *buffer;
3778 unsigned int buflen;
3779
3780#if 0
3781 printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum,
3782 config->interface[ifnum].altsetting[0].desc.bInterfaceClass,
3783 config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass);
3784#endif
3785
3786 /*
3787 * audiocontrol interface found
3788 * find which configuration number is active
3789 */
3790 buffer = dev->rawdescriptors[dev->actconfig - dev->config];
3791 buflen = le16_to_cpu(dev->actconfig->desc.wTotalLength);
3792 s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber);
3793 if (s) {
3794 usb_set_intfdata (intf, s);
3795 return 0;
3796 }
3797 return -ENODEV;
3798}
3799
3800
3801/* a revoke facility would make things simpler */
3802
3803static void usb_audio_disconnect(struct usb_interface *intf)
3804{
3805 struct usb_audio_state *s = usb_get_intfdata (intf);
3806 struct usb_audiodev *as;
3807 struct usb_mixerdev *ms;
3808
3809 if (!s)
3810 return;
3811
3812 /* we get called with -1 for every audiostreaming interface registered */
3813 if (s == (struct usb_audio_state *)-1) {
3814 dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n"));
3815 return;
3816 }
3817 if (!s->usbdev) {
3818 dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s));
3819 return;
3820 }
3821 down(&open_sem);
3822 list_del_init(&s->audiodev);
3823 s->usbdev = NULL;
3824 usb_set_intfdata (intf, NULL);
3825
3826 /* deregister all audio and mixer devices, so no new processes can open this device */
3827 list_for_each_entry(as, &s->audiolist, list) {
3828 usbin_disc(as);
3829 usbout_disc(as);
3830 wake_up(&as->usbin.dma.wait);
3831 wake_up(&as->usbout.dma.wait);
3832 if (as->dev_audio >= 0) {
3833 unregister_sound_dsp(as->dev_audio);
3834 printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio);
3835 }
3836 as->dev_audio = -1;
3837 }
3838 list_for_each_entry(ms, &s->mixerlist, list) {
3839 if (ms->dev_mixer >= 0) {
3840 unregister_sound_mixer(ms->dev_mixer);
3841 printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer);
3842 }
3843 ms->dev_mixer = -1;
3844 }
3845 release(s);
3846 wake_up(&open_wait);
3847}
3848
3849static int __init usb_audio_init(void)
3850{
3851 int result = usb_register(&usb_audio_driver);
3852 if (result == 0)
3853 info(DRIVER_VERSION ":" DRIVER_DESC);
3854 return result;
3855}
3856
3857
3858static void __exit usb_audio_cleanup(void)
3859{
3860 usb_deregister(&usb_audio_driver);
3861}
3862
3863module_init(usb_audio_init);
3864module_exit(usb_audio_cleanup);
3865
3866MODULE_AUTHOR( DRIVER_AUTHOR );
3867MODULE_DESCRIPTION( DRIVER_DESC );
3868MODULE_LICENSE("GPL");
3869
diff --git a/drivers/usb/class/audio.h b/drivers/usb/class/audio.h
deleted file mode 100644
index 45916eb12103..000000000000
--- a/drivers/usb/class/audio.h
+++ /dev/null
@@ -1,110 +0,0 @@
1#define CS_AUDIO_UNDEFINED 0x20
2#define CS_AUDIO_DEVICE 0x21
3#define CS_AUDIO_CONFIGURATION 0x22
4#define CS_AUDIO_STRING 0x23
5#define CS_AUDIO_INTERFACE 0x24
6#define CS_AUDIO_ENDPOINT 0x25
7
8#define HEADER 0x01
9#define INPUT_TERMINAL 0x02
10#define OUTPUT_TERMINAL 0x03
11#define MIXER_UNIT 0x04
12#define SELECTOR_UNIT 0x05
13#define FEATURE_UNIT 0x06
14#define PROCESSING_UNIT 0x07
15#define EXTENSION_UNIT 0x08
16
17#define AS_GENERAL 0x01
18#define FORMAT_TYPE 0x02
19#define FORMAT_SPECIFIC 0x03
20
21#define EP_GENERAL 0x01
22
23#define MAX_CHAN 9
24#define MAX_FREQ 16
25#define MAX_IFACE 8
26#define MAX_FORMAT 8
27#define MAX_ALT 32 /* Sorry, we need quite a few for the Philips webcams */
28
29struct usb_audio_terminal
30{
31 u8 flags;
32 u8 assoc;
33 u16 type; /* Mic etc */
34 u8 channels;
35 u8 source;
36 u16 chancfg;
37};
38
39struct usb_audio_format
40{
41 u8 type;
42 u8 channels;
43 u8 num_freq;
44 u8 sfz;
45 u8 bits;
46 u16 freq[MAX_FREQ];
47};
48
49struct usb_audio_interface
50{
51 u8 terminal;
52 u8 delay;
53 u16 num_formats;
54 u16 format_type;
55 u8 flags;
56 u8 idleconf; /* Idle config */
57#define AU_IFACE_FOUND 1
58 struct usb_audio_format format[MAX_FORMAT];
59};
60
61struct usb_audio_device
62{
63 struct list_head list;
64 u8 mixer;
65 u8 selector;
66 void *irq_handle;
67 u8 num_channels;
68 u8 num_dsp_iface;
69 u8 channel_map[MAX_CHAN];
70 struct usb_audio_terminal terminal[MAX_CHAN];
71 struct usb_audio_interface interface[MAX_IFACE][MAX_ALT];
72};
73
74
75
76/* Audio Class specific Request Codes */
77
78#define SET_CUR 0x01
79#define GET_CUR 0x81
80#define SET_MIN 0x02
81#define GET_MIN 0x82
82#define SET_MAX 0x03
83#define GET_MAX 0x83
84#define SET_RES 0x04
85#define GET_RES 0x84
86#define SET_MEM 0x05
87#define GET_MEM 0x85
88#define GET_STAT 0xff
89
90/* Terminal Control Selectors */
91
92#define COPY_PROTECT_CONTROL 0x01
93
94/* Feature Unit Control Selectors */
95
96#define MUTE_CONTROL 0x01
97#define VOLUME_CONTROL 0x02
98#define BASS_CONTROL 0x03
99#define MID_CONTROL 0x04
100#define TREBLE_CONTROL 0x05
101#define GRAPHIC_EQUALIZER_CONTROL 0x06
102#define AUTOMATIC_GAIN_CONTROL 0x07
103#define DELAY_CONTROL 0x08
104#define BASS_BOOST_CONTROL 0x09
105#define LOUDNESS_CONTROL 0x0a
106
107/* Endpoint Control Selectors */
108
109#define SAMPLING_FREQ_CONTROL 0x01
110#define PITCH_CONTROL 0x02
diff --git a/drivers/usb/class/usb-midi.c b/drivers/usb/class/usb-midi.c
deleted file mode 100644
index f13f004d311f..000000000000
--- a/drivers/usb/class/usb-midi.c
+++ /dev/null
@@ -1,2153 +0,0 @@
1/*
2 usb-midi.c -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 This driver is based on:
22 - 'Universal Serial Bus Device Class Definition for MIDI Device'
23 - linux/drivers/sound/es1371.c, linux/drivers/usb/audio.c
24 - alsa/lowlevel/pci/cs64xx.c
25 - umidi.c for NetBSD
26 */
27
28/* ------------------------------------------------------------------------- */
29
30
31#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/list.h>
35#include <linux/slab.h>
36#include <linux/usb.h>
37#include <linux/poll.h>
38#include <linux/sound.h>
39#include <linux/init.h>
40#include <asm/semaphore.h>
41
42#include "usb-midi.h"
43
44/* ------------------------------------------------------------------------- */
45
46/* More verbose on syslog */
47#undef MIDI_DEBUG
48
49#define MIDI_IN_BUFSIZ 1024
50
51#define HAVE_SUPPORT_USB_MIDI_CLASS
52
53#undef HAVE_SUPPORT_ALSA
54
55/* ------------------------------------------------------------------------- */
56
57static int singlebyte = 0;
58module_param(singlebyte, int, 0);
59MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
60
61static int maxdevices = 4;
62module_param(maxdevices, int, 0);
63MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
64
65static int uvendor = -1;
66module_param(uvendor, int, 0);
67MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
68
69static int uproduct = -1;
70module_param(uproduct, int, 0);
71MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
72
73static int uinterface = -1;
74module_param(uinterface, int, 0);
75MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
76
77static int ualt = -1;
78module_param(ualt, int, 0);
79MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
80
81static int umin = -1;
82module_param(umin, int, 0);
83MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
84
85static int umout = -1;
86module_param(umout, int, 0);
87MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
88
89static int ucable = -1;
90module_param(ucable, int, 0);
91MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
92
93/** Note -- the usb_string() returns only Latin-1 characters.
94 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
95 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
96 **/
97static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/
98module_param(ulangid, ushort, 0);
99MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
100
101MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
102MODULE_DESCRIPTION("USB-MIDI driver");
103MODULE_LICENSE("GPL");
104
105/* ------------------------------------------------------------------------- */
106
107/** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
108
109#define MS_DESCRIPTOR_UNDEFINED 0
110#define MS_HEADER 1
111#define MIDI_IN_JACK 2
112#define MIDI_OUT_JACK 3
113/* Spec reads: ELEMENT */
114#define ELEMENT_DESCRIPTOR 4
115
116#define MS_HEADER_LENGTH 7
117
118/** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
119
120#define DESCRIPTOR_UNDEFINED 0
121/* Spec reads: MS_GENERAL */
122#define MS_GENERAL_ENDPOINT 1
123
124/** MIDIStreaming MIDI IN and OUT Jack Types **/
125
126#define JACK_TYPE_UNDEFINED 0
127/* Spec reads: EMBEDDED */
128#define EMBEDDED_JACK 1
129/* Spec reads: EXTERNAL */
130#define EXTERNAL_JACK 2
131
132
133/* structure summary
134
135 usb_midi_state usb_device
136 | |
137 *| *| per ep
138 in_ep out_ep
139 | |
140 *| *| per cable
141 min mout
142 | | (cable to device pairing magic)
143 | |
144 usb_midi_dev dev_id (major,minor) == file->private_data
145
146*/
147
148/* usb_midi_state: corresponds to a USB-MIDI module */
149struct usb_midi_state {
150 struct list_head mididev;
151
152 struct usb_device *usbdev;
153
154 struct list_head midiDevList;
155 struct list_head inEndpointList;
156 struct list_head outEndpointList;
157
158 spinlock_t lock;
159
160 unsigned int count; /* usage counter */
161};
162
163/* midi_out_endpoint: corresponds to an output endpoint */
164struct midi_out_endpoint {
165 struct list_head list;
166
167 struct usb_device *usbdev;
168 int endpoint;
169 spinlock_t lock;
170 wait_queue_head_t wait;
171
172 unsigned char *buf;
173 int bufWrPtr;
174 int bufSize;
175
176 struct urb *urb;
177};
178
179/* midi_in_endpoint: corresponds to an input endpoint */
180struct midi_in_endpoint {
181 struct list_head list;
182
183 struct usb_device *usbdev;
184 int endpoint;
185 spinlock_t lock;
186 wait_queue_head_t wait;
187
188 struct usb_mididev *cables[16]; // cables open for read
189 int readers; // number of cables open for read
190
191 struct urb *urb;
192 unsigned char *recvBuf;
193 int recvBufSize;
194 int urbSubmitted; //FIXME: == readers > 0
195};
196
197/* usb_mididev: corresponds to a logical device */
198struct usb_mididev {
199 struct list_head list;
200
201 struct usb_midi_state *midi;
202 int dev_midi;
203 mode_t open_mode;
204
205 struct {
206 struct midi_in_endpoint *ep;
207 int cableId;
208
209// as we are pushing data from usb_bulk_read to usb_midi_read,
210// we need a larger, cyclic buffer here.
211 unsigned char buf[MIDI_IN_BUFSIZ];
212 int bufRdPtr;
213 int bufWrPtr;
214 int bufRemains;
215 } min;
216
217 struct {
218 struct midi_out_endpoint *ep;
219 int cableId;
220
221 unsigned char buf[3];
222 int bufPtr;
223 int bufRemains;
224
225 int isInExclusive;
226 unsigned char lastEvent;
227 } mout;
228
229 int singlebyte;
230};
231
232/** Map the high nybble of MIDI voice messages to number of Message bytes.
233 * High nyble ranges from 0x8 to 0xe
234 */
235
236static int remains_80e0[] = {
237 3, /** 0x8X Note Off **/
238 3, /** 0x9X Note On **/
239 3, /** 0xAX Poly-key pressure **/
240 3, /** 0xBX Control Change **/
241 2, /** 0xCX Program Change **/
242 2, /** 0xDX Channel pressure **/
243 3 /** 0xEX PitchBend Change **/
244};
245
246/** Map the messages to a number of Message bytes.
247 *
248 **/
249static int remains_f0f6[] = {
250 0, /** 0xF0 **/
251 2, /** 0XF1 **/
252 3, /** 0XF2 **/
253 2, /** 0XF3 **/
254 2, /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
255 2, /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
256 1 /** 0XF6 **/
257};
258
259/** Map the messages to a CIN (Code Index Number).
260 *
261 **/
262static int cin_f0ff[] = {
263 4, /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
264 2, /** 0xF1 **/
265 3, /** 0xF2 **/
266 2, /** 0xF3 **/
267 2, /** 0xF4 **/
268 2, /** 0xF5 **/
269 5, /** 0xF6 **/
270 5, /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
271 5, /** 0xF8 **/
272 5, /** 0xF9 **/
273 5, /** 0xFA **/
274 5, /** 0xFB **/
275 5, /** 0xFC **/
276 5, /** 0xFD **/
277 5, /** 0xFE **/
278 5 /** 0xFF **/
279};
280
281/** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
282 * to the number of bytes of valid MIDI data.
283 *
284 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
285 *
286 **/
287static int cin_to_len[] = {
288 0, 0, 2, 3,
289 3, 1, 2, 3,
290 3, 3, 3, 3,
291 2, 2, 3, 1
292};
293
294
295/* ------------------------------------------------------------------------- */
296
297static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
298
299static DECLARE_MUTEX(open_sem);
300static DECLARE_WAIT_QUEUE_HEAD(open_wait);
301
302
303/* ------------------------------------------------------------------------- */
304
305static void usb_write_callback(struct urb *urb, struct pt_regs *regs)
306{
307 struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
308
309 if ( waitqueue_active( &ep->wait ) )
310 wake_up_interruptible( &ep->wait );
311}
312
313
314static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
315{
316 struct usb_device *d;
317 int pipe;
318 int ret = 0;
319 int status;
320 int maxretry = 50;
321
322 DECLARE_WAITQUEUE(wait,current);
323 init_waitqueue_head(&ep->wait);
324
325 d = ep->usbdev;
326 pipe = usb_sndbulkpipe(d, ep->endpoint);
327 usb_fill_bulk_urb( ep->urb, d, pipe, (unsigned char*)buf, len,
328 usb_write_callback, ep );
329
330 status = usb_submit_urb(ep->urb, GFP_KERNEL);
331
332 if (status) {
333 printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
334 ret = -EIO;
335 goto error;
336 }
337
338 add_wait_queue( &ep->wait, &wait );
339 set_current_state( TASK_INTERRUPTIBLE );
340
341 while( ep->urb->status == -EINPROGRESS ) {
342 if ( maxretry-- < 0 ) {
343 printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
344 ret = -ETIME;
345 break;
346 }
347 interruptible_sleep_on_timeout( &ep->wait, 10 );
348 }
349 set_current_state( TASK_RUNNING );
350 remove_wait_queue( &ep->wait, &wait );
351
352error:
353 return ret;
354}
355
356
357/** Copy data from URB to In endpoint buf.
358 * Discard if CIN == 0 or CIN = 1.
359 *
360 *
361 **/
362
363static void usb_bulk_read(struct urb *urb, struct pt_regs *regs)
364{
365 struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
366 unsigned char *data = urb->transfer_buffer;
367 int i, j, wake;
368
369 if ( !ep->urbSubmitted ) {
370 return;
371 }
372
373 if ( (urb->status == 0) && (urb->actual_length > 0) ) {
374 wake = 0;
375 spin_lock( &ep->lock );
376
377 for(j = 0; j < urb->actual_length; j += 4) {
378 int cin = (data[j]>>0)&0xf;
379 int cab = (data[j]>>4)&0xf;
380 struct usb_mididev *cable = ep->cables[cab];
381 if ( cable ) {
382 int len = cin_to_len[cin]; /** length of MIDI data **/
383 for (i = 0; i < len; i++) {
384 cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
385 cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
386 if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
387 cable->min.bufRemains += 1;
388 else /** need to drop data **/
389 cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
390 wake = 1;
391 }
392 }
393 }
394
395 spin_unlock ( &ep->lock );
396 if ( wake ) {
397 wake_up( &ep->wait );
398 }
399 }
400
401 /* urb->dev must be reinitialized on 2.4.x kernels */
402 urb->dev = ep->usbdev;
403
404 urb->actual_length = 0;
405 usb_submit_urb(urb, GFP_ATOMIC);
406}
407
408
409
410/* ------------------------------------------------------------------------- */
411
412/* This routine must be called with spin_lock */
413
414/** Wrapper around usb_write().
415 * This routine must be called with spin_lock held on ep.
416 * Called by midiWrite(), putOneMidiEvent(), and usb_midi_write();
417 **/
418static int flush_midi_buffer( struct midi_out_endpoint *ep )
419{
420 int ret=0;
421
422 if ( ep->bufWrPtr > 0 ) {
423 ret = usb_write( ep, ep->buf, ep->bufWrPtr );
424 ep->bufWrPtr = 0;
425 }
426
427 return ret;
428}
429
430
431/* ------------------------------------------------------------------------- */
432
433
434/** Given a MIDI Event, determine size of data to be attached to
435 * USB-MIDI packet.
436 * Returns 1, 2 or 3.
437 * Called by midiWrite();
438 * Uses remains_80e0 and remains_f0f6;
439 **/
440static int get_remains(int event)
441{
442 int ret;
443
444 if ( event < 0x80 ) {
445 ret = 1;
446 } else if ( event < 0xf0 ) {
447 ret = remains_80e0[((event-0x80)>>4)&0x0f];
448 } else if ( event < 0xf7 ) {
449 ret = remains_f0f6[event-0xf0];
450 } else {
451 ret = 1;
452 }
453
454 return ret;
455}
456
457/** Given the output MIDI data in the output buffer, computes a reasonable
458 * CIN.
459 * Called by putOneMidiEvent().
460 **/
461static int get_CIN( struct usb_mididev *m )
462{
463 int cin;
464
465 if ( m->mout.buf[0] == 0xf7 ) {
466 cin = 5;
467 }
468 else if ( m->mout.buf[1] == 0xf7 ) {
469 cin = 6;
470 }
471 else if ( m->mout.buf[2] == 0xf7 ) {
472 cin = 7;
473 }
474 else {
475 if ( m->mout.isInExclusive == 1 ) {
476 cin = 4;
477 } else if ( m->mout.buf[0] < 0x80 ) {
478 /** One byte that we know nothing about. **/
479 cin = 0xF;
480 } else if ( m->mout.buf[0] < 0xf0 ) {
481 /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
482 cin = (m->mout.buf[0]>>4)&0x0f;
483 }
484 else {
485 /** Special lookup table exists for real-time events. **/
486 cin = cin_f0ff[m->mout.buf[0]-0xf0];
487 }
488 }
489
490 return cin;
491}
492
493
494/* ------------------------------------------------------------------------- */
495
496
497
498/** Move data to USB endpoint buffer.
499 *
500 **/
501static int put_one_midi_event(struct usb_mididev *m)
502{
503 int cin;
504 unsigned long flags;
505 struct midi_out_endpoint *ep = m->mout.ep;
506 int ret=0;
507
508 cin = get_CIN( m );
509 if ( cin > 0x0f || cin < 0 ) {
510 return -EINVAL;
511 }
512
513 spin_lock_irqsave( &ep->lock, flags );
514 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
515 ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
516 ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
517 ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
518 if ( ep->bufWrPtr >= ep->bufSize ) {
519 ret = flush_midi_buffer( ep );
520 }
521 spin_unlock_irqrestore( &ep->lock, flags);
522
523 m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
524 m->mout.bufPtr = 0;
525
526 return ret;
527}
528
529/** Write the MIDI message v on the midi device.
530 * Called by usb_midi_write();
531 * Responsible for packaging a MIDI data stream into USB-MIDI packets.
532 **/
533
534static int midi_write( struct usb_mididev *m, int v )
535{
536 unsigned long flags;
537 struct midi_out_endpoint *ep = m->mout.ep;
538 int ret=0;
539 unsigned char c = (unsigned char)v;
540 unsigned char sysrt_buf[4];
541
542 if ( m->singlebyte != 0 ) {
543 /** Simple code to handle the single-byte USB-MIDI protocol. */
544 spin_lock_irqsave( &ep->lock, flags );
545 if ( ep->bufWrPtr+4 > ep->bufSize ) {
546 ret = flush_midi_buffer( ep );
547 if ( !ret ) {
548 spin_unlock_irqrestore( &ep->lock, flags );
549 return ret;
550 }
551 }
552 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | 0x0f; /* single byte */
553 ep->buf[ep->bufWrPtr++] = c;
554 ep->buf[ep->bufWrPtr++] = 0;
555 ep->buf[ep->bufWrPtr++] = 0;
556 if ( ep->bufWrPtr >= ep->bufSize ) {
557 ret = flush_midi_buffer( ep );
558 }
559 spin_unlock_irqrestore( &ep->lock, flags );
560
561 return ret;
562 }
563 /** Normal USB-MIDI protocol begins here. */
564
565 if ( c > 0xf7 ) { /* system: Realtime messages */
566 /** Realtime messages are written IMMEDIATELY. */
567 sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
568 sysrt_buf[1] = c;
569 sysrt_buf[2] = 0;
570 sysrt_buf[3] = 0;
571 spin_lock_irqsave( &ep->lock, flags );
572 ret = usb_write( ep, sysrt_buf, 4 );
573 spin_unlock_irqrestore( &ep->lock, flags );
574 /* m->mout.lastEvent = 0; */
575
576 return ret;
577 }
578
579 if ( c >= 0x80 ) {
580 if ( c < 0xf0 ) {
581 m->mout.lastEvent = c;
582 m->mout.isInExclusive = 0;
583 m->mout.bufRemains = get_remains(c);
584 } else if ( c == 0xf0 ) {
585 /* m->mout.lastEvent = 0; */
586 m->mout.isInExclusive = 1;
587 m->mout.bufRemains = get_remains(c);
588 } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
589 /* m->mout.lastEvent = 0; */
590 m->mout.isInExclusive = 0;
591 m->mout.bufRemains = 1;
592 } else if ( c > 0xf0 ) {
593 /* m->mout.lastEvent = 0; */
594 m->mout.isInExclusive = 0;
595 m->mout.bufRemains = get_remains(c);
596 }
597
598 } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
599 if ( m->mout.lastEvent == 0 ) {
600 return 0; /* discard, waiting for the first event */
601 }
602 /** track status **/
603 m->mout.buf[0] = m->mout.lastEvent;
604 m->mout.bufPtr = 1;
605 m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
606 }
607
608 m->mout.buf[m->mout.bufPtr++] = c;
609 m->mout.bufRemains--;
610 if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
611 ret = put_one_midi_event(m);
612 }
613
614 return ret;
615}
616
617
618/* ------------------------------------------------------------------------- */
619
620/** Basic operation on /dev/midiXX as registered through struct file_operations.
621 *
622 * Basic contract: Used to change the current read/write position in a file.
623 * On success, the non-negative position is reported.
624 * On failure, the negative of an error code is reported.
625 *
626 * Because a MIDIStream is not a file, all seek operations are doomed to fail.
627 *
628 **/
629static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
630{
631 /** Tell user you cannot seek on a PIPE-like device. **/
632 return -ESPIPE;
633}
634
635
636/** Basic operation on /dev/midiXX as registered through struct file_operations.
637 *
638 * Basic contract: Block until count bytes have been read or an error occurs.
639 *
640 **/
641
642static ssize_t usb_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
643{
644 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
645 struct midi_in_endpoint *ep = m->min.ep;
646 ssize_t ret;
647 DECLARE_WAITQUEUE(wait, current);
648
649 if ( !access_ok(VERIFY_READ, buffer, count) ) {
650 return -EFAULT;
651 }
652 if ( count == 0 ) {
653 return 0;
654 }
655
656 add_wait_queue( &ep->wait, &wait );
657 ret = 0;
658 while( count > 0 ) {
659 int cnt;
660 int d = (int)count;
661
662 cnt = m->min.bufRemains;
663 if ( cnt > d ) {
664 cnt = d;
665 }
666
667 if ( cnt <= 0 ) {
668 if ( file->f_flags & O_NONBLOCK ) {
669 if (!ret)
670 ret = -EAGAIN;
671 break;
672 }
673 __set_current_state(TASK_INTERRUPTIBLE);
674 schedule();
675 if (signal_pending(current)) {
676 if(!ret)
677 ret=-ERESTARTSYS;
678 break;
679 }
680 continue;
681 }
682
683 {
684 int i;
685 unsigned long flags; /* used to synchronize access to the endpoint */
686 spin_lock_irqsave( &ep->lock, flags );
687 for (i = 0; i < cnt; i++) {
688 if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
689 if ( !ret )
690 ret = -EFAULT;
691 break;
692 }
693 m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
694 m->min.bufRemains -= 1;
695 }
696 spin_unlock_irqrestore( &ep->lock, flags );
697 }
698
699 count-=cnt;
700 buffer+=cnt;
701 ret+=cnt;
702
703 break;
704 }
705
706 remove_wait_queue( &ep->wait, &wait );
707 set_current_state(TASK_RUNNING);
708
709 return ret;
710}
711
712
713/** Basic operation on /dev/midiXX as registered through struct file_operations.
714 *
715 * Basic Contract: Take MIDI data byte-by-byte and pass it to
716 * writeMidi() which packages MIDI data into USB-MIDI stream.
717 * Then flushMidiData() is called to ensure all bytes have been written
718 * in a timely fashion.
719 *
720 **/
721
722static ssize_t usb_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
723{
724 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
725 ssize_t ret;
726 unsigned long int flags;
727
728 if ( !access_ok(VERIFY_READ, buffer, count) ) {
729 return -EFAULT;
730 }
731 if ( count == 0 ) {
732 return 0;
733 }
734
735 ret = 0;
736 while( count > 0 ) {
737 unsigned char c;
738
739 if (copy_from_user((unsigned char *)&c, buffer, 1)) {
740 if ( ret == 0 )
741 ret = -EFAULT;
742 break;
743 }
744 if( midi_write(m, (int)c) ) {
745 if ( ret == 0 )
746 ret = -EFAULT;
747 break;
748 }
749 count--;
750 buffer++;
751 ret++;
752 }
753
754 spin_lock_irqsave( &m->mout.ep->lock, flags );
755 if ( flush_midi_buffer(m->mout.ep) < 0 ) {
756 ret = -EFAULT;
757 }
758 spin_unlock_irqrestore( &m->mout.ep->lock, flags );
759
760 return ret;
761}
762
763/** Basic operation on /dev/midiXX as registered through struct file_operations.
764 *
765 * Basic contract: Wait (spin) until ready to read or write on the file.
766 *
767 **/
768static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
769{
770 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
771 struct midi_in_endpoint *iep = m->min.ep;
772 struct midi_out_endpoint *oep = m->mout.ep;
773 unsigned long flags;
774 unsigned int mask = 0;
775
776 if ( file->f_mode & FMODE_READ ) {
777 poll_wait( file, &iep->wait, wait );
778 spin_lock_irqsave( &iep->lock, flags );
779 if ( m->min.bufRemains > 0 )
780 mask |= POLLIN | POLLRDNORM;
781 spin_unlock_irqrestore( &iep->lock, flags );
782 }
783
784 if ( file->f_mode & FMODE_WRITE ) {
785 poll_wait( file, &oep->wait, wait );
786 spin_lock_irqsave( &oep->lock, flags );
787 if ( oep->bufWrPtr < oep->bufSize )
788 mask |= POLLOUT | POLLWRNORM;
789 spin_unlock_irqrestore( &oep->lock, flags );
790 }
791
792 return mask;
793}
794
795
796/** Basic operation on /dev/midiXX as registered through struct file_operations.
797 *
798 * Basic contract: This is always the first operation performed on the
799 * device node. If no method is defined, the open succeeds without any
800 * notification given to the module.
801 *
802 **/
803
804static int usb_midi_open(struct inode *inode, struct file *file)
805{
806 int minor = iminor(inode);
807 DECLARE_WAITQUEUE(wait, current);
808 struct usb_midi_state *s;
809 struct usb_mididev *m;
810 unsigned long flags;
811 int succeed = 0;
812
813#if 0
814 printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
815#endif
816
817 for(;;) {
818 down(&open_sem);
819 list_for_each_entry(s, &mididevs, mididev) {
820 list_for_each_entry(m, &s->midiDevList, list) {
821 if ( !((m->dev_midi ^ minor) & ~0xf) )
822 goto device_found;
823 }
824 }
825 up(&open_sem);
826 return -ENODEV;
827
828 device_found:
829 if ( !s->usbdev ) {
830 up(&open_sem);
831 return -EIO;
832 }
833 if ( !(m->open_mode & file->f_mode) ) {
834 break;
835 }
836 if ( file->f_flags & O_NONBLOCK ) {
837 up(&open_sem);
838 return -EBUSY;
839 }
840 __set_current_state(TASK_INTERRUPTIBLE);
841 add_wait_queue( &open_wait, &wait );
842 up(&open_sem);
843 schedule();
844 remove_wait_queue( &open_wait, &wait );
845 if ( signal_pending(current) ) {
846 return -ERESTARTSYS;
847 }
848 }
849
850 file->private_data = m;
851 spin_lock_irqsave( &s->lock, flags );
852
853 if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
854 //FIXME: intented semantics unclear here
855 m->min.bufRdPtr = 0;
856 m->min.bufWrPtr = 0;
857 m->min.bufRemains = 0;
858 spin_lock_init(&m->min.ep->lock);
859
860 m->mout.bufPtr = 0;
861 m->mout.bufRemains = 0;
862 m->mout.isInExclusive = 0;
863 m->mout.lastEvent = 0;
864 spin_lock_init(&m->mout.ep->lock);
865 }
866
867 if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
868 unsigned long int flagsep;
869 spin_lock_irqsave( &m->min.ep->lock, flagsep );
870 m->min.ep->cables[m->min.cableId] = m;
871 m->min.ep->readers += 1;
872 m->min.bufRdPtr = 0;
873 m->min.bufWrPtr = 0;
874 m->min.bufRemains = 0;
875 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
876
877 if ( !(m->min.ep->urbSubmitted)) {
878
879 /* urb->dev must be reinitialized on 2.4.x kernels */
880 m->min.ep->urb->dev = m->min.ep->usbdev;
881
882 if ( usb_submit_urb(m->min.ep->urb, GFP_ATOMIC) ) {
883 printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
884 }
885 m->min.ep->urbSubmitted = 1;
886 }
887 m->open_mode |= FMODE_READ;
888 succeed = 1;
889 }
890
891 if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
892 m->mout.bufPtr = 0;
893 m->mout.bufRemains = 0;
894 m->mout.isInExclusive = 0;
895 m->mout.lastEvent = 0;
896 m->open_mode |= FMODE_WRITE;
897 succeed = 1;
898 }
899
900 spin_unlock_irqrestore( &s->lock, flags );
901
902 s->count++;
903 up(&open_sem);
904
905 /** Changed to prevent extra increments to USE_COUNT. **/
906 if (!succeed) {
907 return -EBUSY;
908 }
909
910#if 0
911 printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
912#endif
913
914 return nonseekable_open(inode, file); /** Success. **/
915}
916
917
918/** Basic operation on /dev/midiXX as registered through struct file_operations.
919 *
920 * Basic contract: Close an opened file and deallocate anything we allocated.
921 * Like open(), this can be missing. If open set file->private_data,
922 * release() must clear it.
923 *
924 **/
925
926static int usb_midi_release(struct inode *inode, struct file *file)
927{
928 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
929 struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
930
931#if 0
932 printk(KERN_INFO "usb-midi: Close.\n");
933#endif
934
935 down(&open_sem);
936
937 if ( m->open_mode & FMODE_WRITE ) {
938 m->open_mode &= ~FMODE_WRITE;
939 usb_kill_urb( m->mout.ep->urb );
940 }
941
942 if ( m->open_mode & FMODE_READ ) {
943 unsigned long int flagsep;
944 spin_lock_irqsave( &m->min.ep->lock, flagsep );
945 m->min.ep->cables[m->min.cableId] = NULL; // discard cable
946 m->min.ep->readers -= 1;
947 m->open_mode &= ~FMODE_READ;
948 if ( m->min.ep->readers == 0 &&
949 m->min.ep->urbSubmitted ) {
950 m->min.ep->urbSubmitted = 0;
951 usb_kill_urb(m->min.ep->urb);
952 }
953 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
954 }
955
956 s->count--;
957
958 up(&open_sem);
959 wake_up(&open_wait);
960
961 file->private_data = NULL;
962 return 0;
963}
964
965static struct file_operations usb_midi_fops = {
966 .owner = THIS_MODULE,
967 .llseek = usb_midi_llseek,
968 .read = usb_midi_read,
969 .write = usb_midi_write,
970 .poll = usb_midi_poll,
971 .open = usb_midi_open,
972 .release = usb_midi_release,
973};
974
975/* ------------------------------------------------------------------------- */
976
977/** Returns filled midi_in_endpoint structure or null on failure.
978 *
979 * Parameters:
980 * d - a usb_device
981 * endPoint - An usb endpoint in the range 0 to 15.
982 * Called by allocUsbMidiDev();
983 *
984 **/
985
986static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
987{
988 struct midi_in_endpoint *ep;
989 int bufSize;
990 int pipe;
991
992 endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
993
994 pipe = usb_rcvbulkpipe( d, endPoint );
995 bufSize = usb_maxpacket( d, pipe, 0 );
996 /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
997
998 ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
999 if ( !ep ) {
1000 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1001 return NULL;
1002 }
1003 memset( ep, 0, sizeof(struct midi_in_endpoint) );
1004// this sets cables[] and readers to 0, too.
1005// for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1006// ep->readers = 0;
1007
1008 ep->endpoint = endPoint;
1009
1010 ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1011 if ( !ep->recvBuf ) {
1012 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1013 kfree(ep);
1014 return NULL;
1015 }
1016
1017 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1018 if ( !ep->urb ) {
1019 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1020 kfree(ep->recvBuf);
1021 kfree(ep);
1022 return NULL;
1023 }
1024 usb_fill_bulk_urb( ep->urb, d,
1025 usb_rcvbulkpipe(d, endPoint),
1026 (unsigned char *)ep->recvBuf, bufSize,
1027 usb_bulk_read, ep );
1028
1029 /* ep->bufRdPtr = 0; */
1030 /* ep->bufWrPtr = 0; */
1031 /* ep->bufRemains = 0; */
1032 /* ep->urbSubmitted = 0; */
1033 ep->recvBufSize = bufSize;
1034
1035 init_waitqueue_head(&ep->wait);
1036
1037 return ep;
1038}
1039
1040static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1041{
1042 usb_kill_urb( min->urb );
1043 usb_free_urb( min->urb );
1044 kfree( min->recvBuf );
1045 kfree( min );
1046
1047 return 0;
1048}
1049
1050/** Returns filled midi_out_endpoint structure or null on failure.
1051 *
1052 * Parameters:
1053 * d - a usb_device
1054 * endPoint - An usb endpoint in the range 0 to 15.
1055 * Called by allocUsbMidiDev();
1056 *
1057 **/
1058static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1059{
1060 struct midi_out_endpoint *ep = NULL;
1061 int pipe;
1062 int bufSize;
1063
1064 endPoint &= 0x0f;
1065 pipe = usb_sndbulkpipe( d, endPoint );
1066 bufSize = usb_maxpacket( d, pipe, 1 );
1067
1068 ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1069 if ( !ep ) {
1070 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1071 return NULL;
1072 }
1073 memset( ep, 0, sizeof(struct midi_out_endpoint) );
1074
1075 ep->endpoint = endPoint;
1076 ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1077 if ( !ep->buf ) {
1078 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1079 kfree(ep);
1080 return NULL;
1081 }
1082
1083 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1084 if ( !ep->urb ) {
1085 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1086 kfree(ep->buf);
1087 kfree(ep);
1088 return NULL;
1089 }
1090
1091 ep->bufSize = bufSize;
1092 /* ep->bufWrPtr = 0; */
1093
1094 init_waitqueue_head(&ep->wait);
1095
1096 return ep;
1097}
1098
1099
1100static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1101{
1102 usb_kill_urb( mout->urb );
1103 usb_free_urb( mout->urb );
1104 kfree( mout->buf );
1105 kfree( mout );
1106
1107 return 0;
1108}
1109
1110
1111/** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1112 *
1113 * Returns null if memory is not available or the device cannot be registered.
1114 * Called by allocUsbMidiDev();
1115 *
1116 **/
1117static struct usb_mididev *allocMidiDev(
1118 struct usb_midi_state *s,
1119 struct midi_in_endpoint *min,
1120 struct midi_out_endpoint *mout,
1121 int inCableId,
1122 int outCableId )
1123{
1124 struct usb_mididev *m;
1125
1126 m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1127 if (!m) {
1128 printk(KERN_ERR "usbmidi: no memory for midi device\n");
1129 return NULL;
1130 }
1131
1132 memset(m, 0, sizeof(struct usb_mididev));
1133
1134 if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1135 printk(KERN_ERR "usbmidi: cannot register midi device\n");
1136 kfree(m);
1137 return NULL;
1138 }
1139
1140 m->midi = s;
1141 /* m->open_mode = 0; */
1142
1143 if ( min ) {
1144 m->min.ep = min;
1145 m->min.ep->usbdev = s->usbdev;
1146 m->min.cableId = inCableId;
1147 }
1148 /* m->min.bufPtr = 0; */
1149 /* m->min.bufRemains = 0; */
1150
1151 if ( mout ) {
1152 m->mout.ep = mout;
1153 m->mout.ep->usbdev = s->usbdev;
1154 m->mout.cableId = outCableId;
1155 }
1156 /* m->mout.bufPtr = 0; */
1157 /* m->mout.bufRemains = 0; */
1158 /* m->mout.isInExclusive = 0; */
1159 /* m->mout.lastEvent = 0; */
1160
1161 m->singlebyte = singlebyte;
1162
1163 return m;
1164}
1165
1166
1167static void release_midi_device( struct usb_midi_state *s )
1168{
1169 struct usb_mididev *m;
1170 struct midi_in_endpoint *min;
1171 struct midi_out_endpoint *mout;
1172
1173 if ( s->count > 0 ) {
1174 up(&open_sem);
1175 return;
1176 }
1177 up( &open_sem );
1178 wake_up( &open_wait );
1179
1180 while(!list_empty(&s->inEndpointList)) {
1181 min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1182 list_del(&min->list);
1183 remove_midi_in_endpoint(min);
1184 }
1185
1186 while(!list_empty(&s->outEndpointList)) {
1187 mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1188 list_del(&mout->list);
1189 remove_midi_out_endpoint(mout);
1190 }
1191
1192 while(!list_empty(&s->midiDevList)) {
1193 m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1194 list_del(&m->list);
1195 kfree(m);
1196 }
1197
1198 kfree(s);
1199
1200 return;
1201}
1202
1203
1204/* ------------------------------------------------------------------------- */
1205
1206/** Utility routine to find a descriptor in a dump of many descriptors.
1207 * Returns start of descriptor or NULL if not found.
1208 * descStart pointer to list of interfaces.
1209 * descLength length (in bytes) of dump
1210 * after (ignored if NULL) this routine returns only descriptors after "after"
1211 * dtype (mandatory) The descriptor type.
1212 * iface (ignored if -1) returns descriptor at/following given interface
1213 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1214 *
1215 *
1216 * Called by parseDescriptor(), find_csinterface_descriptor();
1217 *
1218 */
1219static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1220{
1221 unsigned char *p, *end, *next;
1222 int interfaceNumber = -1, altSet = -1;
1223
1224 p = descStart;
1225 end = p + descLength;
1226 for( ; p < end; ) {
1227 if ( p[0] < 2 )
1228 return NULL;
1229 next = p + p[0];
1230 if ( next > end )
1231 return NULL;
1232 if ( p[1] == USB_DT_INTERFACE ) {
1233 if ( p[0] < USB_DT_INTERFACE_SIZE )
1234 return NULL;
1235 interfaceNumber = p[2];
1236 altSet = p[3];
1237 }
1238 if ( p[1] == dtype &&
1239 ( !after || ( p > (unsigned char *)after) ) &&
1240 ( ( iface == -1) || (iface == interfaceNumber) ) &&
1241 ( (altSetting == -1) || (altSetting == altSet) )) {
1242 return p;
1243 }
1244 p = next;
1245 }
1246 return NULL;
1247}
1248
1249/** Utility to find a class-specific interface descriptor.
1250 * dsubtype is a descriptor subtype
1251 * Called by parseDescriptor();
1252 **/
1253static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1254{
1255 unsigned char *p;
1256
1257 p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1258 while ( p ) {
1259 if ( p[0] >= 3 && p[2] == dsubtype )
1260 return p;
1261 p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1262 iface, altSetting );
1263 }
1264 return NULL;
1265}
1266
1267
1268/** The magic of making a new usb_midi_device from config happens here.
1269 *
1270 * The caller is responsible for free-ing this return value (if not NULL).
1271 *
1272 **/
1273static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1274{
1275 struct usb_midi_device *u;
1276 unsigned char *p1;
1277 unsigned char *p2;
1278 unsigned char *next;
1279 int iep, oep;
1280 int length;
1281 unsigned long longBits;
1282 int pins, nbytes, offset, shift, jack;
1283#ifdef HAVE_JACK_STRINGS
1284 /** Jacks can have associated names. **/
1285 unsigned char jack2string[256];
1286#endif
1287
1288 u = NULL;
1289 /* find audiocontrol interface */
1290 p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1291 MS_HEADER, ifnum, altSetting);
1292
1293 if ( !p1 ) {
1294 goto error_end;
1295 }
1296
1297 if ( p1[0] < MS_HEADER_LENGTH ) {
1298 goto error_end;
1299 }
1300
1301 /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1302 that the rest of the USB 2.0 spec is obeyed. */
1303
1304 u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1305 if ( !u ) {
1306 return NULL;
1307 }
1308 u->deviceName = NULL;
1309 u->idVendor = le16_to_cpu(d->descriptor.idVendor);
1310 u->idProduct = le16_to_cpu(d->descriptor.idProduct);
1311 u->interface = ifnum;
1312 u->altSetting = altSetting;
1313 u->in[0].endpoint = -1;
1314 u->in[0].cableId = -1;
1315 u->out[0].endpoint = -1;
1316 u->out[0].cableId = -1;
1317
1318
1319 printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1320 (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1321 (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1322 );
1323
1324 length = p1[5] | (p1[6] << 8);
1325
1326#ifdef HAVE_JACK_STRINGS
1327 memset(jack2string, 0, sizeof(unsigned char) * 256);
1328#endif
1329
1330 length -= p1[0];
1331 for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1332 next = p2 + p2[0];
1333 length -= p2[0];
1334
1335 if (p2[0] < 2 )
1336 break;
1337 if (p2[1] != USB_DT_CS_INTERFACE)
1338 break;
1339 if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1340 jack = p2[4];
1341#ifdef HAVE_JACK_STRINGS
1342 jack2string[jack] = p2[5];
1343#endif
1344 printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1345 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1346 } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1347 pins = p2[5];
1348 if ( p2[0] < (6 + 2 * pins) )
1349 continue;
1350 jack = p2[4];
1351#ifdef HAVE_JACK_STRINGS
1352 jack2string[jack] = p2[5 + 2 * pins];
1353#endif
1354 printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1355 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1356 } else if ( p2[2] == ELEMENT_DESCRIPTOR && p2[0] >= 10) {
1357 pins = p2[4];
1358 if ( p2[0] < (9 + 2 * pins ) )
1359 continue;
1360 nbytes = p2[8 + 2 * pins ];
1361 if ( p2[0] < (10 + 2 * pins + nbytes) )
1362 continue;
1363 longBits = 0L;
1364 for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1365 longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1366 }
1367 jack = p2[3];
1368#ifdef HAVE_JACK_STRINGS
1369 jack2string[jack] = p2[9 + 2 * pins + nbytes];
1370#endif
1371 printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1372 jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1373 } else {
1374 }
1375 }
1376
1377 iep=0;
1378 oep=0;
1379
1380 if (quirks==0) {
1381 /* MIDISTREAM */
1382 p2 = NULL;
1383 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1384 ifnum, altSetting ); p1; p1 = next ) {
1385 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1386 ifnum, altSetting );
1387 p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1388 ifnum, altSetting );
1389
1390 if ( p2 && next && ( p2 > next ) )
1391 p2 = NULL;
1392
1393 if ( p1[0] < 9 || !p2 || p2[0] < 4 )
1394 continue;
1395
1396 if ( (p1[2] & 0x80) == 0x80 ) {
1397 if ( iep < 15 ) {
1398 pins = p2[3]; /* not pins -- actually "cables" */
1399 if ( pins > 16 )
1400 pins = 16;
1401 u->in[iep].endpoint = p1[2];
1402 u->in[iep].cableId = ( 1 << pins ) - 1;
1403 if ( u->in[iep].cableId )
1404 iep ++;
1405 if ( iep < 15 ) {
1406 u->in[iep].endpoint = -1;
1407 u->in[iep].cableId = -1;
1408 }
1409 }
1410 } else {
1411 if ( oep < 15 ) {
1412 pins = p2[3]; /* not pins -- actually "cables" */
1413 if ( pins > 16 )
1414 pins = 16;
1415 u->out[oep].endpoint = p1[2];
1416 u->out[oep].cableId = ( 1 << pins ) - 1;
1417 if ( u->out[oep].cableId )
1418 oep ++;
1419 if ( oep < 15 ) {
1420 u->out[oep].endpoint = -1;
1421 u->out[oep].cableId = -1;
1422 }
1423 }
1424 }
1425
1426 }
1427 } else if (quirks==1) {
1428 /* YAMAHA quirks */
1429 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1430 ifnum, altSetting ); p1; p1 = next ) {
1431 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1432 ifnum, altSetting );
1433
1434 if ( p1[0] < 7 )
1435 continue;
1436
1437 if ( (p1[2] & 0x80) == 0x80 ) {
1438 if ( iep < 15 ) {
1439 pins = iep+1;
1440 if ( pins > 16 )
1441 pins = 16;
1442 u->in[iep].endpoint = p1[2];
1443 u->in[iep].cableId = ( 1 << pins ) - 1;
1444 if ( u->in[iep].cableId )
1445 iep ++;
1446 if ( iep < 15 ) {
1447 u->in[iep].endpoint = -1;
1448 u->in[iep].cableId = -1;
1449 }
1450 }
1451 } else {
1452 if ( oep < 15 ) {
1453 pins = oep+1;
1454 u->out[oep].endpoint = p1[2];
1455 u->out[oep].cableId = ( 1 << pins ) - 1;
1456 if ( u->out[oep].cableId )
1457 oep ++;
1458 if ( oep < 15 ) {
1459 u->out[oep].endpoint = -1;
1460 u->out[oep].cableId = -1;
1461 }
1462 }
1463 }
1464
1465 }
1466 }
1467
1468 if ( !iep && ! oep ) {
1469 goto error_end;
1470 }
1471
1472 return u;
1473
1474error_end:
1475 kfree(u);
1476 return NULL;
1477}
1478
1479/* ------------------------------------------------------------------------- */
1480
1481/** Returns number between 0 and 16.
1482 *
1483 **/
1484static int on_bits( unsigned short v )
1485{
1486 int i;
1487 int ret=0;
1488
1489 for ( i=0 ; i<16 ; i++ ) {
1490 if ( v & (1<<i) )
1491 ret++;
1492 }
1493
1494 return ret;
1495}
1496
1497
1498/** USB-device will be interrogated for altSetting.
1499 *
1500 * Returns negative on error.
1501 * Called by allocUsbMidiDev();
1502 *
1503 **/
1504
1505static int get_alt_setting( struct usb_device *d, int ifnum )
1506{
1507 int alts, alt=0;
1508 struct usb_interface *iface;
1509 struct usb_host_interface *interface;
1510 struct usb_endpoint_descriptor *ep;
1511 int epin, epout;
1512 int i;
1513
1514 iface = usb_ifnum_to_if( d, ifnum );
1515 alts = iface->num_altsetting;
1516
1517 for ( alt=0 ; alt<alts ; alt++ ) {
1518 interface = &iface->altsetting[alt];
1519 epin = -1;
1520 epout = -1;
1521
1522 for ( i=0 ; i<interface->desc.bNumEndpoints ; i++ ) {
1523 ep = &interface->endpoint[i].desc;
1524 if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1525 continue;
1526 }
1527 if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1528 epin = i;
1529 } else if ( epout < 0 ) {
1530 epout = i;
1531 }
1532 if ( epin >= 0 && epout >= 0 ) {
1533 return interface->desc.bAlternateSetting;
1534 }
1535 }
1536 }
1537
1538 return -ENODEV;
1539}
1540
1541
1542/* ------------------------------------------------------------------------- */
1543
1544
1545/** Returns 0 if successful in allocating and registering internal structures.
1546 * Returns negative on failure.
1547 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1548 * Writes messages on success to indicate which /dev/midiXX is which physical
1549 * endpoint.
1550 *
1551 **/
1552static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1553{
1554 struct usb_mididev **mdevs=NULL;
1555 struct midi_in_endpoint *mins[15], *min;
1556 struct midi_out_endpoint *mouts[15], *mout;
1557 int inDevs=0, outDevs=0;
1558 int inEndpoints=0, outEndpoints=0;
1559 int inEndpoint, outEndpoint;
1560 int inCableId, outCableId;
1561 int i;
1562 int devices = 0;
1563 int alt = 0;
1564
1565 /* Obtain altSetting or die.. */
1566 alt = u->altSetting;
1567 if ( alt < 0 ) {
1568 alt = get_alt_setting( d, u->interface );
1569 }
1570 if ( alt < 0 )
1571 return -ENXIO;
1572
1573 /* Configure interface */
1574 if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1575 return -ENXIO;
1576 }
1577
1578 for ( i = 0 ; i < 15 ; i++ ) {
1579 mins[i] = NULL;
1580 mouts[i] = NULL;
1581 }
1582
1583 /* Begin Allocation */
1584 while( inEndpoints < 15
1585 && inDevs < maxdevices
1586 && u->in[inEndpoints].cableId >= 0 ) {
1587 inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1588 mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1589 if ( mins[inEndpoints] == NULL )
1590 goto error_end;
1591 inEndpoints++;
1592 }
1593
1594 while( outEndpoints < 15
1595 && outDevs < maxdevices
1596 && u->out[outEndpoints].cableId >= 0 ) {
1597 outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1598 mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1599 if ( mouts[outEndpoints] == NULL )
1600 goto error_end;
1601 outEndpoints++;
1602 }
1603
1604 devices = inDevs > outDevs ? inDevs : outDevs;
1605 devices = maxdevices > devices ? devices : maxdevices;
1606
1607 /* obtain space for device name (iProduct) if not known. */
1608 if ( ! u->deviceName ) {
1609 mdevs = (struct usb_mididev **)
1610 kmalloc(sizeof(struct usb_mididevs *)*devices
1611 + sizeof(char) * 256, GFP_KERNEL);
1612 } else {
1613 mdevs = (struct usb_mididev **)
1614 kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1615 }
1616
1617 if ( !mdevs ) {
1618 /* devices = 0; */
1619 /* mdevs = NULL; */
1620 goto error_end;
1621 }
1622 for ( i=0 ; i<devices ; i++ ) {
1623 mdevs[i] = NULL;
1624 }
1625
1626 /* obtain device name (iProduct) if not known. */
1627 if ( ! u->deviceName ) {
1628 u->deviceName = (char *) (mdevs + devices);
1629 if ( ! d->have_langid && d->descriptor.iProduct) {
1630 alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1631 if (alt < 0) {
1632 printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1633 } else if (u->deviceName[0] < 4) {
1634 printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1635 } else {
1636 printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1637 for(; alt >= 4; alt -= 2) {
1638 i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1639 printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1640 (alt-4) >> 1, i);
1641 if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1642 d->have_langid = 1;
1643 d->string_langid = i;
1644 printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1645 if ( i == ulangid )
1646 break;
1647 }
1648 }
1649 }
1650 }
1651 u->deviceName[0] = (char) 0;
1652 if (d->descriptor.iProduct) {
1653 printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1654 alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1655 if( alt < 0 ) {
1656 u->deviceName[0] = (char) 0;
1657 }
1658 printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1659 }
1660 /* Failsafe */
1661 if ( !u->deviceName[0] ) {
1662 if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_ROLAND ) {
1663 strcpy(u->deviceName, "Unknown Roland");
1664 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_STEINBERG ) {
1665 strcpy(u->deviceName, "Unknown Steinberg");
1666 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_YAMAHA ) {
1667 strcpy(u->deviceName, "Unknown Yamaha");
1668 } else {
1669 strcpy(u->deviceName, "Unknown");
1670 }
1671 }
1672 }
1673
1674 inEndpoint = 0; inCableId = -1;
1675 outEndpoint = 0; outCableId = -1;
1676
1677 for ( i=0 ; i<devices ; i++ ) {
1678 for ( inCableId ++ ;
1679 inEndpoint <15
1680 && mins[inEndpoint]
1681 && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1682 inCableId++ ) {
1683 if ( inCableId >= 16 ) {
1684 inEndpoint ++;
1685 inCableId = 0;
1686 }
1687 }
1688 min = mins[inEndpoint];
1689 for ( outCableId ++ ;
1690 outEndpoint <15
1691 && mouts[outEndpoint]
1692 && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1693 outCableId++ ) {
1694 if ( outCableId >= 16 ) {
1695 outEndpoint ++;
1696 outCableId = 0;
1697 }
1698 }
1699 mout = mouts[outEndpoint];
1700
1701 mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1702 if ( mdevs[i] == NULL )
1703 goto error_end;
1704
1705 }
1706
1707 /* Success! */
1708 for ( i=0 ; i<devices ; i++ ) {
1709 list_add_tail( &mdevs[i]->list, &s->midiDevList );
1710 }
1711 for ( i=0 ; i<inEndpoints ; i++ ) {
1712 list_add_tail( &mins[i]->list, &s->inEndpointList );
1713 }
1714 for ( i=0 ; i<outEndpoints ; i++ ) {
1715 list_add_tail( &mouts[i]->list, &s->outEndpointList );
1716 }
1717
1718 printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1719 for ( i=0 ; i<devices ; i++ ) {
1720 int dm = (mdevs[i]->dev_midi-2)>>4;
1721 if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1722 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1723 dm,
1724 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1725 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1726 } else if ( mdevs[i]->min.ep != NULL ) {
1727 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1728 dm,
1729 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1730 } else if ( mdevs[i]->mout.ep != NULL ) {
1731 printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1732 dm,
1733 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1734 }
1735 }
1736
1737 kfree(mdevs);
1738 return 0;
1739
1740 error_end:
1741 if ( mdevs != NULL ) {
1742 for ( i=0 ; i<devices ; i++ ) {
1743 if ( mdevs[i] != NULL ) {
1744 unregister_sound_midi( mdevs[i]->dev_midi );
1745 kfree(mdevs[i]);
1746 }
1747 }
1748 kfree(mdevs);
1749 }
1750
1751 for ( i=0 ; i<15 ; i++ ) {
1752 if ( mins[i] != NULL ) {
1753 remove_midi_in_endpoint( mins[i] );
1754 }
1755 if ( mouts[i] != NULL ) {
1756 remove_midi_out_endpoint( mouts[i] );
1757 }
1758 }
1759
1760 return -ENOMEM;
1761}
1762
1763/* ------------------------------------------------------------------------- */
1764
1765/** Attempt to scan YAMAHA's device descriptor and detect correct values of
1766 * them.
1767 * Return 0 on succes, negative on failure.
1768 * Called by usb_midi_probe();
1769 **/
1770
1771static int detect_yamaha_device( struct usb_device *d,
1772 struct usb_interface *iface, unsigned int ifnum,
1773 struct usb_midi_state *s)
1774{
1775 struct usb_host_interface *interface;
1776 struct usb_midi_device *u;
1777 unsigned char *buffer;
1778 int bufSize;
1779 int i;
1780 int alts=-1;
1781 int ret;
1782
1783 if (le16_to_cpu(d->descriptor.idVendor) != USB_VENDOR_ID_YAMAHA) {
1784 return -EINVAL;
1785 }
1786
1787 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1788 interface = iface->altsetting + i;
1789
1790 if ( interface->desc.bInterfaceClass != 255 ||
1791 interface->desc.bInterfaceSubClass != 0 )
1792 continue;
1793 alts = interface->desc.bAlternateSetting;
1794 }
1795 if ( alts == -1 ) {
1796 return -EINVAL;
1797 }
1798
1799 printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1800 le16_to_cpu(d->descriptor.idVendor),
1801 le16_to_cpu(d->descriptor.idProduct), ifnum);
1802
1803 i = d->actconfig - d->config;
1804 buffer = d->rawdescriptors[i];
1805 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1806
1807 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1808 if ( u == NULL ) {
1809 return -EINVAL;
1810 }
1811
1812 ret = alloc_usb_midi_device( d, s, u );
1813
1814 kfree(u);
1815
1816 return ret;
1817}
1818
1819
1820/** Scan table of known devices which are only partially compliant with
1821 * the MIDIStreaming specification.
1822 * Called by usb_midi_probe();
1823 *
1824 **/
1825
1826static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1827{
1828 struct usb_midi_device *u;
1829 int i;
1830 int ret = -ENXIO;
1831
1832 for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1833 u=&(usb_midi_devices[i]);
1834
1835 if ( le16_to_cpu(d->descriptor.idVendor) != u->idVendor ||
1836 le16_to_cpu(d->descriptor.idProduct) != u->idProduct ||
1837 ifnum != u->interface )
1838 continue;
1839
1840 ret = alloc_usb_midi_device( d, s, u );
1841 break;
1842 }
1843
1844 return ret;
1845}
1846
1847
1848/** Attempt to match any config of an interface to a MIDISTREAMING interface.
1849 * Returns 0 on success, negative on failure.
1850 * Called by usb_midi_probe();
1851 **/
1852static int detect_midi_subclass(struct usb_device *d,
1853 struct usb_interface *iface, unsigned int ifnum,
1854 struct usb_midi_state *s)
1855{
1856 struct usb_host_interface *interface;
1857 struct usb_midi_device *u;
1858 unsigned char *buffer;
1859 int bufSize;
1860 int i;
1861 int alts=-1;
1862 int ret;
1863
1864 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1865 interface = iface->altsetting + i;
1866
1867 if ( interface->desc.bInterfaceClass != USB_CLASS_AUDIO ||
1868 interface->desc.bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1869 continue;
1870 alts = interface->desc.bAlternateSetting;
1871 }
1872 if ( alts == -1 ) {
1873 return -EINVAL;
1874 }
1875
1876 printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1877 le16_to_cpu(d->descriptor.idVendor),
1878 le16_to_cpu(d->descriptor.idProduct), ifnum);
1879
1880
1881 /* From USB Spec v2.0, Section 9.5.
1882 If the class or vendor specific descriptors use the same format
1883 as standard descriptors (e.g., start with a length byte and
1884 followed by a type byte), they must be returned interleaved with
1885 standard descriptors in the configuration information returned by
1886 a GetDescriptor(Configuration) request. In this case, the class
1887 or vendor-specific descriptors must follow a related standard
1888 descriptor they modify or extend.
1889 */
1890
1891 i = d->actconfig - d->config;
1892 buffer = d->rawdescriptors[i];
1893 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1894
1895 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1896 if ( u == NULL ) {
1897 return -EINVAL;
1898 }
1899
1900 ret = alloc_usb_midi_device( d, s, u );
1901
1902 kfree(u);
1903
1904 return ret;
1905}
1906
1907
1908/** When user has requested a specific device, match it exactly.
1909 *
1910 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1911 * Called by usb_midi_probe();
1912 *
1913 **/
1914static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1915{
1916 struct usb_midi_device u;
1917
1918 if ( le16_to_cpu(d->descriptor.idVendor) != uvendor ||
1919 le16_to_cpu(d->descriptor.idProduct) != uproduct ||
1920 ifnum != uinterface ) {
1921 return -EINVAL;
1922 }
1923
1924 if ( ualt < 0 )
1925 ualt = -1;
1926
1927 if ( umin < 0 || umin > 15 )
1928 umin = 0x01 | USB_DIR_IN;
1929 if ( umout < 0 || umout > 15 )
1930 umout = 0x01;
1931 if ( ucable < 0 || ucable > 15 )
1932 ucable = 0;
1933
1934 u.deviceName = NULL; /* A flag for alloc_usb_midi_device to get device
1935 name from device. */
1936 u.idVendor = uvendor;
1937 u.idProduct = uproduct;
1938 u.interface = uinterface;
1939 u.altSetting = ualt;
1940
1941 u.in[0].endpoint = umin;
1942 u.in[0].cableId = (1<<ucable);
1943
1944 u.out[0].endpoint = umout;
1945 u.out[0].cableId = (1<<ucable);
1946
1947 return alloc_usb_midi_device( d, s, &u );
1948}
1949
1950
1951
1952/* ------------------------------------------------------------------------- */
1953
1954static int usb_midi_probe(struct usb_interface *intf,
1955 const struct usb_device_id *id)
1956{
1957 struct usb_midi_state *s;
1958 struct usb_device *dev = interface_to_usbdev(intf);
1959 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1960
1961 s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
1962 if ( !s )
1963 return -ENOMEM;
1964
1965 memset( s, 0, sizeof(struct usb_midi_state) );
1966 INIT_LIST_HEAD(&s->midiDevList);
1967 INIT_LIST_HEAD(&s->inEndpointList);
1968 INIT_LIST_HEAD(&s->outEndpointList);
1969 s->usbdev = dev;
1970 s->count = 0;
1971 spin_lock_init(&s->lock);
1972
1973 if (
1974 detect_by_hand( dev, ifnum, s ) &&
1975 detect_midi_subclass( dev, intf, ifnum, s ) &&
1976 detect_vendor_specific_device( dev, ifnum, s ) &&
1977 detect_yamaha_device( dev, intf, ifnum, s) ) {
1978 kfree(s);
1979 return -EIO;
1980 }
1981
1982 down(&open_sem);
1983 list_add_tail(&s->mididev, &mididevs);
1984 up(&open_sem);
1985
1986 usb_set_intfdata (intf, s);
1987 return 0;
1988}
1989
1990
1991static void usb_midi_disconnect(struct usb_interface *intf)
1992{
1993 struct usb_midi_state *s = usb_get_intfdata (intf);
1994 struct usb_mididev *m;
1995
1996 if ( !s )
1997 return;
1998
1999 if ( s == (struct usb_midi_state *)-1 ) {
2000 return;
2001 }
2002 if ( !s->usbdev ) {
2003 return;
2004 }
2005 down(&open_sem);
2006 list_del(&s->mididev);
2007 INIT_LIST_HEAD(&s->mididev);
2008 s->usbdev = NULL;
2009 usb_set_intfdata (intf, NULL);
2010
2011 list_for_each_entry(m, &s->midiDevList, list) {
2012 wake_up(&(m->min.ep->wait));
2013 wake_up(&(m->mout.ep->wait));
2014 if ( m->dev_midi >= 0 ) {
2015 unregister_sound_midi(m->dev_midi);
2016 }
2017 m->dev_midi = -1;
2018 }
2019 release_midi_device(s);
2020 wake_up(&open_wait);
2021}
2022
2023/* we want to look at all devices by hand */
2024static struct usb_device_id id_table[] = {
2025 {.driver_info = 42},
2026 {}
2027};
2028
2029static struct usb_driver usb_midi_driver = {
2030 .name = "midi",
2031 .probe = usb_midi_probe,
2032 .disconnect = usb_midi_disconnect,
2033 .id_table = id_table,
2034};
2035
2036/* ------------------------------------------------------------------------- */
2037
2038static int __init usb_midi_init(void)
2039{
2040 return usb_register(&usb_midi_driver);
2041}
2042
2043static void __exit usb_midi_exit(void)
2044{
2045 usb_deregister(&usb_midi_driver);
2046}
2047
2048module_init(usb_midi_init) ;
2049module_exit(usb_midi_exit) ;
2050
2051#ifdef HAVE_ALSA_SUPPORT
2052#define SNDRV_MAIN_OBJECT_FILE
2053#include "../../include/driver.h"
2054#include "../../include/control.h"
2055#include "../../include/info.h"
2056#include "../../include/cs46xx.h"
2057
2058/* ------------------------------------------------------------------------- */
2059
2060static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2061{
2062 return 0;
2063}
2064
2065static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2066{
2067 return 0;
2068}
2069
2070static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2071{
2072 return 0;
2073}
2074
2075
2076/* ------------------------------------------------------------------------- */
2077
2078static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2079{
2080 return 0;
2081}
2082
2083static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2084{
2085 return 0;
2086}
2087
2088static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2089 int up)
2090{
2091 return 0;
2092}
2093
2094/* ------------------------------------------------------------------------- */
2095
2096static snd_rawmidi_ops_t snd_usbmidi_output =
2097{
2098 .open = snd_usbmidi_output_open,
2099 .close = snd_usbmidi_output_close,
2100 .trigger = snd_usbmidi_output_trigger,
2101};
2102static snd_rawmidi_ops_t snd_usbmidi_input =
2103{
2104 .open = snd_usbmidi_input_open,
2105 .close = snd_usbmidi_input_close,
2106 .trigger = snd_usbmidi_input_trigger,
2107};
2108
2109int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2110{
2111 snd_rawmidi_t *rmidi;
2112 int err;
2113
2114 if (rrawmidi)
2115 *rrawmidi = NULL;
2116 if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2117 return err;
2118 strcpy(rmidi->name, "USB-MIDI");
2119
2120 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2121 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2122
2123 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2124
2125 rmidi->private_data = chip;
2126 chip->rmidi = rmidi;
2127 if (rrawmidi)
2128 *rrawmidi = NULL;
2129
2130 return 0;
2131}
2132
2133int snd_usbmidi_create( snd_card_t * card,
2134 struct pci_dev * pci,
2135 usbmidi_t ** rchip )
2136{
2137 usbmidi_t *chip;
2138 int err, idx;
2139 snd_region_t *region;
2140 static snd_device_opt_t ops = {
2141 .dev_free = snd_usbmidi_dev_free,
2142 };
2143
2144 *rchip = NULL;
2145 chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2146 if ( chip == NULL )
2147 return -ENOMEM;
2148}
2149
2150EXPORT_SYMBOL(snd_usbmidi_create);
2151EXPORT_SYMBOL(snd_usbmidi_midi);
2152#endif /* HAVE_ALSA_SUPPORT */
2153
diff --git a/drivers/usb/class/usb-midi.h b/drivers/usb/class/usb-midi.h
deleted file mode 100644
index 358cdef8492e..000000000000
--- a/drivers/usb/class/usb-midi.h
+++ /dev/null
@@ -1,164 +0,0 @@
1/*
2 usb-midi.h -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* ------------------------------------------------------------------------- */
23
24#ifndef _USB_MIDI_H_
25#define _USB_MIDI_H_
26
27#ifndef USB_SUBCLASS_MIDISTREAMING
28#define USB_SUBCLASS_MIDISTREAMING 3
29#endif
30
31/* ------------------------------------------------------------------------- */
32/* Roland MIDI Devices */
33
34#define USB_VENDOR_ID_ROLAND 0x0582
35#define USBMIDI_ROLAND_UA100G 0x0000
36#define USBMIDI_ROLAND_MPU64 0x0002
37#define USBMIDI_ROLAND_SC8850 0x0003
38#define USBMIDI_ROLAND_SC8820 0x0007
39#define USBMIDI_ROLAND_UM2 0x0005
40#define USBMIDI_ROLAND_UM1 0x0009
41#define USBMIDI_ROLAND_PC300 0x0008
42
43/* YAMAHA MIDI Devices */
44#define USB_VENDOR_ID_YAMAHA 0x0499
45#define USBMIDI_YAMAHA_MU1000 0x1001
46
47/* Steinberg MIDI Devices */
48#define USB_VENDOR_ID_STEINBERG 0x0763
49#define USBMIDI_STEINBERG_USB2MIDI 0x1001
50
51/* Mark of the Unicorn MIDI Devices */
52#define USB_VENDOR_ID_MOTU 0x07fd
53#define USBMIDI_MOTU_FASTLANE 0x0001
54
55/* ------------------------------------------------------------------------- */
56/* Supported devices */
57
58struct usb_midi_endpoint {
59 int endpoint;
60 int cableId; /* if bit-n == 1 then cableId-n is enabled (n: 0 - 15) */
61};
62
63struct usb_midi_device {
64 char *deviceName;
65
66 u16 idVendor;
67 u16 idProduct;
68 int interface;
69 int altSetting; /* -1: auto detect */
70
71 struct usb_midi_endpoint in[15];
72 struct usb_midi_endpoint out[15];
73};
74
75static struct usb_midi_device usb_midi_devices[] = {
76 { /* Roland UM-1 */
77 "Roland UM-1",
78 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1, 2, -1,
79 { { 0x81, 1 }, {-1, -1} },
80 { { 0x01, 1,}, {-1, -1} },
81 },
82
83 { /* Roland UM-2 */
84 "Roland UM-2" ,
85 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2, 2, -1,
86 { { 0x81, 3 }, {-1, -1} },
87 { { 0x01, 3,}, {-1, -1} },
88 },
89
90/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
91 { /* Roland UA-100 */
92 "Roland UA-100",
93 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G, 2, -1,
94 { { 0x82, 7 }, {-1, -1} }, /** cables 0,1 and 2 for SYSEX **/
95 { { 0x02, 7 }, {-1, -1} },
96 },
97
98/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
99 { /* Roland SC8850 */
100 "Roland SC8850",
101 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850, 2, -1,
102 { { 0x81, 0x3f }, {-1, -1} },
103 { { 0x01, 0x3f }, {-1, -1} },
104 },
105
106 { /* Roland SC8820 */
107 "Roland SC8820",
108 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
109 { { 0x81, 0x13 }, {-1, -1} },
110 { { 0x01, 0x13 }, {-1, -1} },
111 },
112
113 { /* Roland SC8820 */
114 "Roland SC8820",
115 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
116 { { 0x81, 17 }, {-1, -1} },
117 { { 0x01, 17 }, {-1, -1} },
118 },
119
120 { /* YAMAHA MU1000 */
121 "YAMAHA MU1000",
122 USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000, 0, -1,
123 { { 0x81, 1 }, {-1, -1} },
124 { { 0x01, 15 }, {-1, -1} },
125 },
126 { /* Roland PC-300 */
127 "Roland PC-300",
128 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300, 2, -1,
129 { { 0x81, 1 }, {-1, -1} },
130 { { 0x01, 1 }, {-1, -1} },
131 },
132 { /* MOTU Fastlane USB */
133 "MOTU Fastlane USB",
134 USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE, 1, 0,
135 { { 0x82, 3 }, {-1, -1} },
136 { { 0x02, 3 }, {-1, -1} },
137 }
138};
139
140#define VENDOR_SPECIFIC_USB_MIDI_DEVICES (sizeof(usb_midi_devices)/sizeof(struct usb_midi_device))
141
142/* for Hot-Plugging */
143
144static struct usb_device_id usb_midi_ids [] = {
145 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
146 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING},
147 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1 ) },
148 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2 ) },
149 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G ) },
150 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300 ) },
151 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850 ) },
152 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820 ) },
153 { USB_DEVICE( USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000 ) },
154 { USB_DEVICE( USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE ) },
155/* { USB_DEVICE( USB_VENDOR_ID_STEINBERG, USBMIDI_STEINBERG_USB2MIDI ) },*/
156 { } /* Terminating entry */
157};
158
159MODULE_DEVICE_TABLE (usb, usb_midi_ids);
160
161/* ------------------------------------------------------------------------- */
162#endif /* _USB_MIDI_H_ */
163
164