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