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