2 * usbusy2y.c - ALSA USB US-428 Driver
4 2005-04-14 Karsten Wiese
6 Call snd_card_free() instead of snd_card_free_in_thread() to prevent oops with dead keyboard symptom.
7 Tested ok with kernel 2.6.12-rc2.
9 2004-12-14 Karsten Wiese
11 snd_pcm_open for rawusb pcm-devices now returns -EBUSY if called without rawusb's hwdep device being open.
13 2004-12-02 Karsten Wiese
15 Use macro usb_maxpacket() for portability.
17 2004-10-26 Karsten Wiese
19 wake_up() process waiting in usX2Y_urbs_start() on error.
21 2004-10-21 Karsten Wiese
23 nrpacks is runtime or compiletime configurable now with tested values from 1 to 4.
25 2004-10-03 Karsten Wiese
27 Avoid any possible racing while in prepare callback.
29 2004-09-30 Karsten Wiese
31 Simplified things and made ohci work again.
33 2004-09-20 Karsten Wiese
35 Use usb_kill_urb() instead of deprecated (kernel 2.6.9) usb_unlink_urb().
37 2004-07-13 Karsten Wiese
39 Don't sleep in START/STOP callbacks anymore.
40 us428 channels C/D not handled just for this version, sorry.
42 2004-06-21 Karsten Wiese
44 Temporarely suspend midi input
45 to sanely call usb_set_interface() when setting format.
47 2004-06-12 Karsten Wiese
49 Made it thus the following rule is enforced:
50 "All pcm substreams of one usX2Y have to operate at the same rate & format."
52 2004-04-06 Karsten Wiese
54 Runs on 2.6.5 kernel without any "--with-debug=" things.
55 us224 reported running.
57 2004-01-14 Karsten Wiese
59 Runs with 2.6.1 kernel.
61 2003-12-30 Karsten Wiese
63 Fix 24Bit 4Channel capturing for the us428.
65 2003-11-27 Karsten Wiese, Martin Langer
68 us224 could be tested by uncommenting the sections containing USB_ID_US224
70 2003-11-03 Karsten Wiese
73 "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.
75 2003-08-22 Karsten Wiese
77 Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader.
79 http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz
81 2003-06-18 Karsten Wiese
83 changed to compile with kernel 2.4.21 and alsa 0.9.4
85 2002-10-16 Karsten Wiese
87 compiles again with alsa-current.
88 USB_ISO_ASAP not used anymore (most of the time), instead
89 urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore.
91 To get the best out of this:
92 Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds.
93 This helped me much on my slowish PII 400 & PIII 500.
94 ACPI yet untested but might cause the same bad behaviour.
95 Use a kernel with lowlatency and preemptiv patches applied.
96 To autoload snd-usb-midi append a line
97 post-install snd-usb-us428 modprobe snd-usb-midi
101 sliders, knobs, lights not yet handled except MASTER Volume slider.
102 "pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does.
103 KDE3: "Enable full duplex operation" deadlocks.
106 2002-08-31 Karsten Wiese
107 Version 0.0.3: audio also simplex;
108 simplifying: iso urbs only 1 packet, melted structs.
109 ASYNC_UNLINK not used anymore: no more crashes so far.....
112 2002-08-09 Karsten Wiese
113 Version 0.0.2: midi works with snd-usb-midi, audio (only fullduplex now) with i.e. bristol.
114 The firmware has been sniffed from win2k us-428 driver 3.09.
116 * Copyright (c) 2002 - 2004 Karsten Wiese
118 * This program is free software; you can redistribute it and/or modify
119 * it under the terms of the GNU General Public License as published by
120 * the Free Software Foundation; either version 2 of the License, or
121 * (at your option) any later version.
123 * This program is distributed in the hope that it will be useful,
124 * but WITHOUT ANY WARRANTY; without even the implied warranty of
125 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
126 * GNU General Public License for more details.
128 * You should have received a copy of the GNU General Public License
129 * along with this program; if not, write to the Free Software
130 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
133 #include <linux/init.h>
134 #include <linux/module.h>
135 #include <linux/moduleparam.h>
136 #include <linux/interrupt.h>
137 #include <linux/usb.h>
138 #include <sound/core.h>
139 #include <sound/initval.h>
140 #include <sound/pcm.h>
142 #include <sound/rawmidi.h>
144 #include "usbusx2y.h"
145 #include "usX2Yhwdep.h"
149 MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
150 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS
" Version 0.8.7.2");
151 MODULE_LICENSE("GPL");
152 MODULE_SUPPORTED_DEVICE("{{TASCAM(0x1604), "NAME_ALLCAPS
"(0x8001)(0x8005)(0x8007) }}");
154 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
155 static char* id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
156 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
; /* Enable this card */
158 module_param_array(index
, int, NULL
, 0444);
159 MODULE_PARM_DESC(index
, "Index value for "NAME_ALLCAPS
".");
160 module_param_array(id
, charp
, NULL
, 0444);
161 MODULE_PARM_DESC(id
, "ID string for "NAME_ALLCAPS
".");
162 module_param_array(enable
, bool, NULL
, 0444);
163 MODULE_PARM_DESC(enable
, "Enable "NAME_ALLCAPS
".");
166 static int snd_usX2Y_card_used
[SNDRV_CARDS
];
168 static void usX2Y_usb_disconnect(struct usb_device
* usb_device
, void* ptr
);
169 static void snd_usX2Y_card_private_free(struct snd_card
*card
);
172 * pipe 4 is used for switching the lamps, setting samplerate, volumes ....
174 static void i_usX2Y_Out04Int(struct urb
*urb
)
176 #ifdef CONFIG_SND_DEBUG
179 struct usX2Ydev
*usX2Y
= urb
->context
;
180 for (i
= 0; i
< 10 && usX2Y
->AS04
.urb
[i
] != urb
; i
++);
181 snd_printdd("i_usX2Y_Out04Int() urb %i status=%i\n", i
, urb
->status
);
186 static void i_usX2Y_In04Int(struct urb
*urb
)
189 struct usX2Ydev
*usX2Y
= urb
->context
;
190 struct us428ctls_sharedmem
*us428ctls
= usX2Y
->us428ctls_sharedmem
;
192 usX2Y
->In04IntCalls
++;
195 snd_printdd("Interrupt Pipe 4 came back with status=%i\n", urb
->status
);
199 // printk("%i:0x%02X ", 8, (int)((unsigned char*)usX2Y->In04Buf)[8]); Master volume shows 0 here if fader is at max during boot ?!?
202 if (-2 == us428ctls
->CtlSnapShotLast
) {
204 memcpy(usX2Y
->In04Last
, usX2Y
->In04Buf
, sizeof(usX2Y
->In04Last
));
205 us428ctls
->CtlSnapShotLast
= -1;
208 for (i
= 0; i
< 21; i
++) {
209 if (usX2Y
->In04Last
[i
] != ((char*)usX2Y
->In04Buf
)[i
]) {
212 usX2Y
->In04Last
[i
] = ((char*)usX2Y
->In04Buf
)[i
];
217 int n
= us428ctls
->CtlSnapShotLast
+ 1;
218 if (n
>= N_us428_ctl_BUFS
|| n
< 0)
220 memcpy(us428ctls
->CtlSnapShot
+ n
, usX2Y
->In04Buf
, sizeof(us428ctls
->CtlSnapShot
[0]));
221 us428ctls
->CtlSnapShotDiffersAt
[n
] = diff
;
222 us428ctls
->CtlSnapShotLast
= n
;
223 wake_up(&usX2Y
->us428ctls_wait_queue_head
);
229 if (0 == usX2Y
->US04
->submitted
)
231 err
= usb_submit_urb(usX2Y
->US04
->urb
[usX2Y
->US04
->submitted
++], GFP_ATOMIC
);
232 while (!err
&& usX2Y
->US04
->submitted
< usX2Y
->US04
->len
);
234 if (us428ctls
&& us428ctls
->p4outLast
>= 0 && us428ctls
->p4outLast
< N_us428_p4out_BUFS
) {
235 if (us428ctls
->p4outLast
!= us428ctls
->p4outSent
) {
236 int j
, send
= us428ctls
->p4outSent
+ 1;
237 if (send
>= N_us428_p4out_BUFS
)
239 for (j
= 0; j
< URBS_AsyncSeq
&& !err
; ++j
)
240 if (0 == usX2Y
->AS04
.urb
[j
]->status
) {
241 struct us428_p4out
*p4out
= us428ctls
->p4out
+ send
; // FIXME if more then 1 p4out is new, 1 gets lost.
242 usb_fill_bulk_urb(usX2Y
->AS04
.urb
[j
], usX2Y
->chip
.dev
,
243 usb_sndbulkpipe(usX2Y
->chip
.dev
, 0x04), &p4out
->val
.vol
,
244 p4out
->type
== eLT_Light
? sizeof(struct us428_lights
) : 5,
245 i_usX2Y_Out04Int
, usX2Y
);
246 err
= usb_submit_urb(usX2Y
->AS04
.urb
[j
], GFP_ATOMIC
);
247 us428ctls
->p4outSent
= send
;
254 snd_printk(KERN_ERR
"In04Int() usb_submit_urb err=%i\n", err
);
256 urb
->dev
= usX2Y
->chip
.dev
;
257 usb_submit_urb(urb
, GFP_ATOMIC
);
263 int usX2Y_AsyncSeq04_init(struct usX2Ydev
*usX2Y
)
268 if (NULL
== (usX2Y
->AS04
.buffer
= kmalloc(URB_DataLen_AsyncSeq
*URBS_AsyncSeq
, GFP_KERNEL
))) {
271 for (i
= 0; i
< URBS_AsyncSeq
; ++i
) {
272 if (NULL
== (usX2Y
->AS04
.urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
))) {
276 usb_fill_bulk_urb( usX2Y
->AS04
.urb
[i
], usX2Y
->chip
.dev
,
277 usb_sndbulkpipe(usX2Y
->chip
.dev
, 0x04),
278 usX2Y
->AS04
.buffer
+ URB_DataLen_AsyncSeq
*i
, 0,
279 i_usX2Y_Out04Int
, usX2Y
285 int usX2Y_In04_init(struct usX2Ydev
*usX2Y
)
287 if (! (usX2Y
->In04urb
= usb_alloc_urb(0, GFP_KERNEL
)))
290 if (! (usX2Y
->In04Buf
= kmalloc(21, GFP_KERNEL
))) {
291 usb_free_urb(usX2Y
->In04urb
);
295 init_waitqueue_head(&usX2Y
->In04WaitQueue
);
296 usb_fill_int_urb(usX2Y
->In04urb
, usX2Y
->chip
.dev
, usb_rcvintpipe(usX2Y
->chip
.dev
, 0x4),
298 i_usX2Y_In04Int
, usX2Y
,
300 return usb_submit_urb(usX2Y
->In04urb
, GFP_KERNEL
);
303 static void usX2Y_unlinkSeq(struct snd_usX2Y_AsyncSeq
*S
)
306 for (i
= 0; i
< URBS_AsyncSeq
; ++i
) {
308 usb_kill_urb(S
->urb
[i
]);
309 usb_free_urb(S
->urb
[i
]);
317 static struct usb_device_id snd_usX2Y_usb_id_table
[] = {
319 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
321 .idProduct
= USB_ID_US428
324 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
326 .idProduct
= USB_ID_US122
329 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
331 .idProduct
= USB_ID_US224
336 static struct snd_card
*usX2Y_create_card(struct usb_device
*device
)
339 struct snd_card
* card
;
340 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
341 if (enable
[dev
] && !snd_usX2Y_card_used
[dev
])
343 if (dev
>= SNDRV_CARDS
)
345 card
= snd_card_new(index
[dev
], id
[dev
], THIS_MODULE
, sizeof(struct usX2Ydev
));
348 snd_usX2Y_card_used
[usX2Y(card
)->chip
.index
= dev
] = 1;
349 card
->private_free
= snd_usX2Y_card_private_free
;
350 usX2Y(card
)->chip
.dev
= device
;
351 usX2Y(card
)->chip
.card
= card
;
352 init_waitqueue_head(&usX2Y(card
)->prepare_wait_queue
);
353 mutex_init(&usX2Y(card
)->prepare_mutex
);
354 INIT_LIST_HEAD(&usX2Y(card
)->chip
.midi_list
);
355 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
356 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
357 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
359 le16_to_cpu(device
->descriptor
.idVendor
),
360 le16_to_cpu(device
->descriptor
.idProduct
),
361 0,//us428(card)->usbmidi.ifnum,
362 usX2Y(card
)->chip
.dev
->bus
->busnum
, usX2Y(card
)->chip
.dev
->devnum
364 snd_card_set_dev(card
, &device
->dev
);
369 static void *usX2Y_usb_probe(struct usb_device
*device
, struct usb_interface
*intf
, const struct usb_device_id
*device_id
)
372 struct snd_card
* card
;
373 if (le16_to_cpu(device
->descriptor
.idVendor
) != 0x1604 ||
374 (le16_to_cpu(device
->descriptor
.idProduct
) != USB_ID_US122
&&
375 le16_to_cpu(device
->descriptor
.idProduct
) != USB_ID_US224
&&
376 le16_to_cpu(device
->descriptor
.idProduct
) != USB_ID_US428
) ||
377 !(card
= usX2Y_create_card(device
)))
379 if ((err
= usX2Y_hwdep_new(card
, device
)) < 0 ||
380 (err
= snd_card_register(card
)) < 0) {
388 * new 2.5 USB kernel API
390 static int snd_usX2Y_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
393 chip
= usX2Y_usb_probe(interface_to_usbdev(intf
), intf
, id
);
395 dev_set_drvdata(&intf
->dev
, chip
);
401 static void snd_usX2Y_disconnect(struct usb_interface
*intf
)
403 usX2Y_usb_disconnect(interface_to_usbdev(intf
),
404 dev_get_drvdata(&intf
->dev
));
407 MODULE_DEVICE_TABLE(usb
, snd_usX2Y_usb_id_table
);
408 static struct usb_driver snd_usX2Y_usb_driver
= {
409 .name
= "snd-usb-usx2y",
410 .probe
= snd_usX2Y_probe
,
411 .disconnect
= snd_usX2Y_disconnect
,
412 .id_table
= snd_usX2Y_usb_id_table
,
415 static void snd_usX2Y_card_private_free(struct snd_card
*card
)
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;
428 static void usX2Y_usb_disconnect(struct usb_device
*device
, void* ptr
)
431 struct snd_card
*card
= ptr
;
432 struct usX2Ydev
*usX2Y
= usX2Y(card
);
434 usX2Y
->chip
.shutdown
= 1;
435 usX2Y
->chip_status
= USX2Y_STAT_CHIP_HUP
;
436 usX2Y_unlinkSeq(&usX2Y
->AS04
);
437 usb_kill_urb(usX2Y
->In04urb
);
438 snd_card_disconnect(card
);
439 /* release the midi resources */
440 list_for_each(p
, &usX2Y
->chip
.midi_list
) {
441 snd_usbmidi_disconnect(p
);
443 if (usX2Y
->us428ctls_sharedmem
)
444 wake_up(&usX2Y
->us428ctls_wait_queue_head
);
449 static int __init
snd_usX2Y_module_init(void)
451 return usb_register(&snd_usX2Y_usb_driver
);
454 static void __exit
snd_usX2Y_module_exit(void)
456 usb_deregister(&snd_usX2Y_usb_driver
);
459 module_init(snd_usX2Y_module_init
)
460 module_exit(snd_usX2Y_module_exit
)