This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / sound / usb / usx2y / usbusx2y.c
1 /*
2  * usbus428.c - ALSA USB US-428 Driver
3  *
4 2004-07-13 Karsten Wiese
5         Version 0.7.1:
6         Don't sleep in START/STOP callbacks anymore.
7         us428 channels C/D not handled just for this version, sorry.
8
9 2004-06-21 Karsten Wiese
10         Version 0.6.4:
11         Temporarely suspend midi input
12         to sanely call usb_set_interface() when setting format.
13
14 2004-06-12 Karsten Wiese
15         Version 0.6.3:
16         Made it thus the following rule is enforced:
17         "All pcm substreams of one usX2Y have to operate at the same rate & format."
18
19 2004-04-06 Karsten Wiese
20         Version 0.6.0:
21         Runs on 2.6.5 kernel without any "--with-debug=" things.
22         us224 reported running.
23
24 2004-01-14 Karsten Wiese
25         Version 0.5.1:
26         Runs with 2.6.1 kernel.
27
28 2003-12-30 Karsten Wiese
29         Version 0.4.1:
30         Fix 24Bit 4Channel capturing for the us428.
31
32 2003-11-27 Karsten Wiese, Martin Langer
33         Version 0.4:
34         us122 support.
35         us224 could be tested by uncommenting the sections containing USB_ID_US224
36
37 2003-11-03 Karsten Wiese
38         Version 0.3:
39         24Bit support. 
40         "arecord -D hw:1 -c 2 -r 48000 -M -f S24_3LE|aplay -D hw:1 -c 2 -r 48000 -M -f S24_3LE" works.
41
42 2003-08-22 Karsten Wiese
43         Version 0.0.8:
44         Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader.
45         See:
46         http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz
47
48 2003-06-18 Karsten Wiese
49         Version 0.0.5:
50         changed to compile with kernel 2.4.21 and alsa 0.9.4
51
52 2002-10-16 Karsten Wiese
53         Version 0.0.4:
54         compiles again with alsa-current.
55         USB_ISO_ASAP not used anymore (most of the time), instead
56         urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore.
57
58         To get the best out of this:
59         Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds.
60         This helped me much on my slowish PII 400 & PIII 500.
61         ACPI yet untested but might cause the same bad behaviour.
62         Use a kernel with lowlatency and preemptiv patches applied.
63         To autoload snd-usb-midi append a line 
64                 post-install snd-usb-us428 modprobe snd-usb-midi
65         to /etc/modules.conf.
66
67         known problems:
68         sliders, knobs, lights not yet handled except MASTER Volume slider.
69         "pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does.
70         KDE3: "Enable full duplex operation" deadlocks.
71
72         
73 2002-08-31 Karsten Wiese
74         Version 0.0.3: audio also simplex;
75         simplifying: iso urbs only 1 packet, melted structs.
76         ASYNC_UNLINK not used anymore: no more crashes so far.....
77         for alsa 0.9 rc3.
78
79 2002-08-09 Karsten Wiese
80         Version 0.0.2: midi works with snd-usb-midi, audio (only fullduplex now) with i.e. bristol.
81         The firmware has been sniffed from win2k us-428 driver 3.09.
82
83  *   Copyright (c) 2002 Karsten Wiese
84  *
85  *   This program is free software; you can redistribute it and/or modify
86  *   it under the terms of the GNU General Public License as published by
87  *   the Free Software Foundation; either version 2 of the License, or
88  *   (at your option) any later version.
89  *
90  *   This program is distributed in the hope that it will be useful,
91  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
92  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
93  *   GNU General Public License for more details.
94  *
95  *   You should have received a copy of the GNU General Public License
96  *   along with this program; if not, write to the Free Software
97  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
98 */
99
100 #include <sound/driver.h>
101 #include <linux/init.h>
102 #include <linux/module.h>
103 #include <linux/moduleparam.h>
104 #include <linux/interrupt.h>
105 #include <linux/usb.h>
106 #include <sound/core.h>
107 #include <sound/initval.h>
108 #include <sound/pcm.h>
109
110 #include <sound/rawmidi.h>
111 #include "usx2y.h"
112 #include "usbusx2y.h"
113 #include "usX2Yhwdep.h"
114
115
116
117 MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
118 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.7.2");
119 MODULE_LICENSE("GPL");
120 MODULE_SUPPORTED_DEVICE("{{TASCAM(0x1604), "NAME_ALLCAPS"(0x8001)(0x8005)(0x8007) }}");
121
122 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
123 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
124 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
125 static int boot_devs;
126
127 module_param_array(index, int, boot_devs, 0444);
128 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
129 module_param_array(id, charp, boot_devs, 0444);
130 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
131 module_param_array(enable, bool, boot_devs, 0444);
132 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
133
134
135 static int snd_usX2Y_card_used[SNDRV_CARDS];
136
137 static void usX2Y_usb_disconnect(struct usb_device* usb_device, void* ptr);
138 static void snd_usX2Y_card_private_free(snd_card_t *card);
139
140 /* 
141  * pipe 4 is used for switching the lamps, setting samplerate, volumes ....   
142  */
143 static void i_usX2Y_Out04Int(struct urb* urb, struct pt_regs *regs)
144 {
145 #ifdef CONFIG_SND_DEBUG
146         if (urb->status) {
147                 int             i;
148                 usX2Ydev_t*     usX2Y = urb->context;
149                 for (i = 0; i < 10 && usX2Y->AS04.urb[i] != urb; i++);
150                 snd_printdd("i_usX2Y_Out04Int() urb %i status=%i\n", i, urb->status);
151         }
152 #endif
153 }
154
155 static void i_usX2Y_In04Int(struct urb* urb, struct pt_regs *regs)
156 {
157         int                     err = 0;
158         usX2Ydev_t              *usX2Y = urb->context;
159         us428ctls_sharedmem_t   *us428ctls = usX2Y->us428ctls_sharedmem;
160
161         usX2Y->In04IntCalls++;
162
163         if (urb->status) {
164                 snd_printdd("Interrupt Pipe 4 came back with status=%i\n", urb->status);
165                 return;
166         }
167
168         //      printk("%i:0x%02X ", 8, (int)((unsigned char*)usX2Y->In04Buf)[8]); Master volume shows 0 here if fader is at max during boot ?!?
169         if (us428ctls) {
170                 int diff = -1;
171                 if (-2 == us428ctls->CtlSnapShotLast) {
172                         diff = 0;
173                         memcpy(usX2Y->In04Last, usX2Y->In04Buf, sizeof(usX2Y->In04Last));
174                         us428ctls->CtlSnapShotLast = -1;
175                 } else {
176                         int i;
177                         for (i = 0; i < 21; i++) {
178                                 if (usX2Y->In04Last[i] != ((char*)usX2Y->In04Buf)[i]) {
179                                         if (diff < 0)
180                                                 diff = i;
181                                         usX2Y->In04Last[i] = ((char*)usX2Y->In04Buf)[i];
182                                 }
183                         }
184                 }
185                 if (0 <= diff) {
186                         int n = us428ctls->CtlSnapShotLast + 1;
187                         if (n >= N_us428_ctl_BUFS  ||  n < 0)
188                                 n = 0;
189                         memcpy(us428ctls->CtlSnapShot + n, usX2Y->In04Buf, sizeof(us428ctls->CtlSnapShot[0]));
190                         us428ctls->CtlSnapShotDiffersAt[n] = diff;
191                         us428ctls->CtlSnapShotLast = n;
192                         wake_up(&usX2Y->us428ctls_wait_queue_head);
193                 }
194         }
195         
196         
197         if (usX2Y->US04) {
198                 if (0 == usX2Y->US04->submitted)
199                         do
200                                 err = usb_submit_urb(usX2Y->US04->urb[usX2Y->US04->submitted++], GFP_ATOMIC);
201                         while (!err && usX2Y->US04->submitted < usX2Y->US04->len);
202         } else
203                 if (us428ctls && us428ctls->p4outLast >= 0 && us428ctls->p4outLast < N_us428_p4out_BUFS) {
204                         if (us428ctls->p4outLast != us428ctls->p4outSent) {
205                                 int j, send = us428ctls->p4outSent + 1;
206                                 if (send >= N_us428_p4out_BUFS)
207                                         send = 0;
208                                 for (j = 0; j < URBS_AsyncSeq  &&  !err; ++j)
209                                         if (0 == usX2Y->AS04.urb[j]->status) {
210                                                 us428_p4out_t *p4out = us428ctls->p4out + send; // FIXME if more then 1 p4out is new, 1 gets lost.
211                                                 usb_fill_bulk_urb(usX2Y->AS04.urb[j], usX2Y->chip.dev,
212                                                                   usb_sndbulkpipe(usX2Y->chip.dev, 0x04), &p4out->val.vol, 
213                                                                   p4out->type == eLT_Light ? sizeof(us428_lights_t) : 5,
214                                                                   i_usX2Y_Out04Int, usX2Y);
215                                                 err = usb_submit_urb(usX2Y->AS04.urb[j], GFP_ATOMIC);
216                                                 us428ctls->p4outSent = send;
217                                                 break;
218                                         }
219                         }
220                 }
221
222         if (err) {
223                 snd_printk("In04Int() usb_submit_urb err=%i\n", err);
224         }
225
226         urb->dev = usX2Y->chip.dev;
227         usb_submit_urb(urb, GFP_ATOMIC);
228 }
229
230 /*
231  * Prepare some urbs
232  */
233 int usX2Y_AsyncSeq04_init(usX2Ydev_t* usX2Y)
234 {
235         int     err = 0,
236                 i;
237
238         if (NULL == (usX2Y->AS04.buffer = kmalloc(URB_DataLen_AsyncSeq*URBS_AsyncSeq, GFP_KERNEL))) {
239                 err = -ENOMEM;
240         } else
241                 for (i = 0; i < URBS_AsyncSeq; ++i) {
242                         if (NULL == (usX2Y->AS04.urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
243                                 err = -ENOMEM;
244                                 break;
245                         }
246                         usb_fill_bulk_urb(      usX2Y->AS04.urb[i], usX2Y->chip.dev,
247                                                 usb_sndbulkpipe(usX2Y->chip.dev, 0x04),
248                                                 usX2Y->AS04.buffer + URB_DataLen_AsyncSeq*i, 0,
249                                                 i_usX2Y_Out04Int, usX2Y
250                                 );
251                 }
252         return err;
253 }
254
255 int usX2Y_In04_init(usX2Ydev_t* usX2Y)
256 {
257         int     err = 0;
258         if (! (usX2Y->In04urb = usb_alloc_urb(0, GFP_KERNEL)))
259                 return -ENOMEM;
260
261         if (! (usX2Y->In04Buf = kmalloc(21, GFP_KERNEL))) {
262                 usb_free_urb(usX2Y->In04urb);
263                 return -ENOMEM;
264         }
265          
266         init_waitqueue_head(&usX2Y->In04WaitQueue);
267         usb_fill_int_urb(usX2Y->In04urb, usX2Y->chip.dev, usb_rcvintpipe(usX2Y->chip.dev, 0x4),
268                          usX2Y->In04Buf, 21,
269                          i_usX2Y_In04Int, usX2Y,
270                          10);
271         err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
272         return err;
273 }
274
275 static void usX2Y_unlinkSeq(snd_usX2Y_AsyncSeq_t* S)
276 {
277         int     i;
278         for (i = 0; i < URBS_AsyncSeq; ++i) {
279                 if (S[i].urb) {
280                         usb_unlink_urb(S->urb[i]);
281                         usb_free_urb(S->urb[i]);
282                         S->urb[i] = NULL;
283                 }
284         }
285         if (S->buffer)
286                 kfree(S->buffer);
287 }
288
289
290 static struct usb_device_id snd_usX2Y_usb_id_table[] = {
291         {
292                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
293                 .idVendor =     0x1604,
294                 .idProduct =    USB_ID_US428 
295         },
296         {
297                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
298                 .idVendor =     0x1604,
299                 .idProduct =    USB_ID_US122 
300         },
301         {
302                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
303                 .idVendor =     0x1604,
304                 .idProduct =    USB_ID_US224
305         },
306         { /* terminator */ }
307 };
308
309 static snd_card_t* usX2Y_create_card(struct usb_device* device)
310 {
311         int             dev;
312         snd_card_t*     card;
313         for (dev = 0; dev < SNDRV_CARDS; ++dev)
314                 if (enable[dev] && !snd_usX2Y_card_used[dev])
315                         break;
316         if (dev >= SNDRV_CARDS)
317                 return NULL;
318         card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(usX2Ydev_t));
319         if (!card)
320                 return NULL;
321         snd_usX2Y_card_used[usX2Y(card)->chip.index = dev] = 1;
322         card->private_free = snd_usX2Y_card_private_free;
323         usX2Y(card)->chip.dev = device;
324         usX2Y(card)->chip.card = card;
325         init_MUTEX (&usX2Y(card)->open_mutex);
326         INIT_LIST_HEAD(&usX2Y(card)->chip.midi_list);
327         strcpy(card->driver, "USB "NAME_ALLCAPS"");
328         sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
329         sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
330                 card->shortname, 
331                 device->descriptor.idVendor, device->descriptor.idProduct,
332                 0,//us428(card)->usbmidi.ifnum,
333                 usX2Y(card)->chip.dev->bus->busnum, usX2Y(card)->chip.dev->devnum
334                 );
335         snd_card_set_dev(card, &device->dev);
336         return card;
337 }
338
339
340 static void* usX2Y_usb_probe(struct usb_device* device, struct usb_interface *intf, const struct usb_device_id* device_id)
341 {
342         int             err;
343         snd_card_t*     card;
344         if (device->descriptor.idVendor != 0x1604 ||
345             (device->descriptor.idProduct != USB_ID_US122 &&
346              device->descriptor.idProduct != USB_ID_US224 &&
347              device->descriptor.idProduct != USB_ID_US428) ||
348             !(card = usX2Y_create_card(device)))
349                 return NULL;
350         if ((err = usX2Y_hwdep_new(card, device)) < 0  ||
351             (err = snd_card_register(card)) < 0) {
352                 snd_card_free(card);
353                 return NULL;
354         }
355         return card;
356 }
357
358 /*
359  * new 2.5 USB kernel API
360  */
361 static int snd_usX2Y_probe(struct usb_interface *intf, const struct usb_device_id *id)
362 {
363         void *chip;
364         chip = usX2Y_usb_probe(interface_to_usbdev(intf), intf, id);
365         if (chip) {
366                 dev_set_drvdata(&intf->dev, chip);
367                 return 0;
368         } else
369                 return -EIO;
370 }
371
372 static void snd_usX2Y_disconnect(struct usb_interface *intf)
373 {
374         usX2Y_usb_disconnect(interface_to_usbdev(intf),
375                                  dev_get_drvdata(&intf->dev));
376 }
377
378 MODULE_DEVICE_TABLE(usb, snd_usX2Y_usb_id_table);
379 static struct usb_driver snd_usX2Y_usb_driver = {
380         .owner =        THIS_MODULE,
381         .name =         "snd-usb-usx2y",
382         .probe =        snd_usX2Y_probe,
383         .disconnect =   snd_usX2Y_disconnect,
384         .id_table =     snd_usX2Y_usb_id_table,
385 };
386
387 static void snd_usX2Y_card_private_free(snd_card_t *card)
388 {
389         if (usX2Y(card)->In04Buf)
390                 kfree(usX2Y(card)->In04Buf);
391         usb_free_urb(usX2Y(card)->In04urb);
392         if (usX2Y(card)->us428ctls_sharedmem)
393                 snd_free_pages(usX2Y(card)->us428ctls_sharedmem, sizeof(*usX2Y(card)->us428ctls_sharedmem));
394         if (usX2Y(card)->chip.index >= 0  &&  usX2Y(card)->chip.index < SNDRV_CARDS)
395                 snd_usX2Y_card_used[usX2Y(card)->chip.index] = 0;
396 }
397
398 /*
399  * Frees the device.
400  */
401 static void usX2Y_usb_disconnect(struct usb_device* device, void* ptr)
402 {
403         if (ptr) {
404                 usX2Ydev_t* usX2Y = usX2Y((snd_card_t*)ptr);
405                 struct list_head* p;
406                 if (usX2Y->chip_status == USX2Y_STAT_CHIP_HUP)  // on 2.6.1 kernel snd_usbmidi_disconnect()
407                         return;                                 // calls us back. better leave :-) .
408                 usX2Y->chip.shutdown = 1;
409                 usX2Y->chip_status = USX2Y_STAT_CHIP_HUP;
410                 usX2Y_unlinkSeq(&usX2Y->AS04);
411                 usb_unlink_urb(usX2Y->In04urb);
412                 snd_card_disconnect((snd_card_t*)ptr);
413                 /* release the midi resources */
414                 list_for_each(p, &usX2Y->chip.midi_list) {
415                         snd_usbmidi_disconnect(p, &snd_usX2Y_usb_driver);
416                 }
417                 if (usX2Y->us428ctls_sharedmem) 
418                         wake_up(&usX2Y->us428ctls_wait_queue_head);
419                 snd_card_free_in_thread((snd_card_t*)ptr);
420         }
421 }
422
423 static int __init snd_usX2Y_module_init(void)
424 {
425         return usb_register(&snd_usX2Y_usb_driver);
426 }
427
428 static void __exit snd_usX2Y_module_exit(void)
429 {
430         usb_deregister(&snd_usX2Y_usb_driver);
431 }
432
433 module_init(snd_usX2Y_module_init)
434 module_exit(snd_usX2Y_module_exit)