File Index
Files and directories
Sign in
FILE

usbusx2y.c

File preview with syntax highlighting.

linux-6.19.14/sound/usb/usx2y/usbusx2y.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * usbusx2y.c - ALSA USB US-428 Driver
  4 *
  52005-04-14 Karsten Wiese
  6	Version 0.8.7.2:
  7	Call snd_card_free() instead of snd_card_free_in_thread() to prevent oops with dead keyboard symptom.
  8	Tested ok with kernel 2.6.12-rc2.
  9
 102004-12-14 Karsten Wiese
 11	Version 0.8.7.1:
 12	snd_pcm_open for rawusb pcm-devices now returns -EBUSY if called without rawusb's hwdep device being open.
 13
 142004-12-02 Karsten Wiese
 15	Version 0.8.7:
 16	Use macro usb_maxpacket() for portability.
 17
 182004-10-26 Karsten Wiese
 19	Version 0.8.6:
 20	wake_up() process waiting in usx2y_urbs_start() on error.
 21
 222004-10-21 Karsten Wiese
 23	Version 0.8.5:
 24	nrpacks is runtime or compiletime configurable now with tested values from 1 to 4.
 25
 262004-10-03 Karsten Wiese
 27	Version 0.8.2:
 28	Avoid any possible racing while in prepare callback.
 29
 302004-09-30 Karsten Wiese
 31	Version 0.8.0:
 32	Simplified things and made ohci work again.
 33
 342004-09-20 Karsten Wiese
 35	Version 0.7.3:
 36	Use usb_kill_urb() instead of deprecated (kernel 2.6.9) usb_unlink_urb().
 37
 382004-07-13 Karsten Wiese
 39	Version 0.7.1:
 40	Don't sleep in START/STOP callbacks anymore.
 41	us428 channels C/D not handled just for this version, sorry.
 42
 432004-06-21 Karsten Wiese
 44	Version 0.6.4:
 45	Temporarely suspend midi input
 46	to sanely call usb_set_interface() when setting format.
 47
 482004-06-12 Karsten Wiese
 49	Version 0.6.3:
 50	Made it thus the following rule is enforced:
 51	"All pcm substreams of one usx2y have to operate at the same rate & format."
 52
 532004-04-06 Karsten Wiese
 54	Version 0.6.0:
 55	Runs on 2.6.5 kernel without any "--with-debug=" things.
 56	us224 reported running.
 57
 582004-01-14 Karsten Wiese
 59	Version 0.5.1:
 60	Runs with 2.6.1 kernel.
 61
 622003-12-30 Karsten Wiese
 63	Version 0.4.1:
 64	Fix 24Bit 4Channel capturing for the us428.
 65
 662003-11-27 Karsten Wiese, Martin Langer
 67	Version 0.4:
 68	us122 support.
 69	us224 could be tested by uncommenting the sections containing USB_ID_US224
 70
 712003-11-03 Karsten Wiese
 72	Version 0.3:
 73	24Bit support.
 74	"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
 762003-08-22 Karsten Wiese
 77	Version 0.0.8:
 78	Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader.
 79	See:
 80	http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz
 81
 822003-06-18 Karsten Wiese
 83	Version 0.0.5:
 84	changed to compile with kernel 2.4.21 and alsa 0.9.4
 85
 862002-10-16 Karsten Wiese
 87	Version 0.0.4:
 88	compiles again with alsa-current.
 89	USB_ISO_ASAP not used anymore (most of the time), instead
 90	urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore.
 91
 92	To get the best out of this:
 93	Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds.
 94	This helped me much on my slowish PII 400 & PIII 500.
 95	ACPI yet untested but might cause the same bad behaviour.
 96	Use a kernel with lowlatency and preemptiv patches applied.
 97	To autoload snd-usb-midi append a line
 98		post-install snd-usb-us428 modprobe snd-usb-midi
 99	to /etc/modules.conf.
100
101	known problems:
102	sliders, knobs, lights not yet handled except MASTER Volume slider.
103	"pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does.
104	KDE3: "Enable full duplex operation" deadlocks.
105
1062002-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.....
110	for alsa 0.9 rc3.
111
1122002-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.
115
116 *   Copyright (c) 2002 - 2004 Karsten Wiese
117 */
118
119#include <linux/init.h>
120#include <linux/module.h>
121#include <linux/moduleparam.h>
122#include <linux/slab.h>
123#include <linux/interrupt.h>
124#include <linux/usb.h>
125#include <sound/core.h>
126#include <sound/initval.h>
127#include <sound/pcm.h>
128
129#include <sound/rawmidi.h>
130#include "usx2y.h"
131#include "usbusx2y.h"
132#include "usX2Yhwdep.h"
133
134MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
135MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.8.7.2");
136MODULE_LICENSE("GPL");
137
138static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
139static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
140static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
141
142module_param_array(index, int, NULL, 0444);
143MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
144module_param_array(id, charp, NULL, 0444);
145MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
146module_param_array(enable, bool, NULL, 0444);
147MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
148
149static int snd_usx2y_card_used[SNDRV_CARDS];
150
151static void snd_usx2y_card_private_free(struct snd_card *card);
152static void usx2y_unlinkseq(struct snd_usx2y_async_seq *s);
153
154#ifdef USX2Y_NRPACKS_VARIABLE
155int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
156module_param(nrpacks, int, 0444);
157MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
158#endif
159
160/*
161 * pipe 4 is used for switching the lamps, setting samplerate, volumes ....
162 */
163static void i_usx2y_out04_int(struct urb *urb)
164{
165#ifdef CONFIG_SND_DEBUG
166	if (urb->status) {
167		int i;
168		struct usx2ydev *usx2y = urb->context;
169
170		for (i = 0; i < 10 && usx2y->as04.urb[i] != urb; i++)
171			;
172		dev_dbg(&urb->dev->dev, "%s urb %i status=%i\n", __func__, i, urb->status);
173	}
174#endif
175}
176
177static void i_usx2y_in04_int(struct urb *urb)
178{
179	int			err = 0;
180	struct usx2ydev		*usx2y = urb->context;
181	struct us428ctls_sharedmem	*us428ctls = usx2y->us428ctls_sharedmem;
182	struct us428_p4out *p4out;
183	int i, j, n, diff, send;
184
185	usx2y->in04_int_calls++;
186
187	if (urb->status) {
188		dev_dbg(&urb->dev->dev, "Interrupt Pipe 4 came back with status=%i\n", urb->status);
189		return;
190	}
191
192	if (us428ctls) {
193		diff = -1;
194		if (us428ctls->ctl_snapshot_last == -2) {
195			diff = 0;
196			memcpy(usx2y->in04_last, usx2y->in04_buf, sizeof(usx2y->in04_last));
197			us428ctls->ctl_snapshot_last = -1;
198		} else {
199			for (i = 0; i < 21; i++) {
200				if (usx2y->in04_last[i] != ((char *)usx2y->in04_buf)[i]) {
201					if (diff < 0)
202						diff = i;
203					usx2y->in04_last[i] = ((char *)usx2y->in04_buf)[i];
204				}
205			}
206		}
207		if (diff >= 0) {
208			n = us428ctls->ctl_snapshot_last + 1;
209			if (n >= N_US428_CTL_BUFS || n < 0)
210				n = 0;
211			memcpy(us428ctls->ctl_snapshot + n, usx2y->in04_buf, sizeof(us428ctls->ctl_snapshot[0]));
212			us428ctls->ctl_snapshot_differs_at[n] = diff;
213			us428ctls->ctl_snapshot_last = n;
214			wake_up(&usx2y->us428ctls_wait_queue_head);
215		}
216	}
217
218	if (usx2y->us04) {
219		if (!usx2y->us04->submitted) {
220			do {
221				err = usb_submit_urb(usx2y->us04->urb[usx2y->us04->submitted++], GFP_ATOMIC);
222			} while (!err && usx2y->us04->submitted < usx2y->us04->len);
223		}
224	} else {
225		if (us428ctls && us428ctls->p4out_last >= 0 && us428ctls->p4out_last < N_US428_P4OUT_BUFS) {
226			if (us428ctls->p4out_last != us428ctls->p4out_sent) {
227				send = us428ctls->p4out_sent + 1;
228				if (send >= N_US428_P4OUT_BUFS)
229					send = 0;
230				for (j = 0; j < URBS_ASYNC_SEQ && !err; ++j) {
231					if (!usx2y->as04.urb[j]->status) {
232						p4out = us428ctls->p4out + send;	// FIXME if more than 1 p4out is new, 1 gets lost.
233						usb_fill_bulk_urb(usx2y->as04.urb[j], usx2y->dev,
234								  usb_sndbulkpipe(usx2y->dev, 0x04), &p4out->val.vol,
235								  p4out->type == ELT_LIGHT ? sizeof(struct us428_lights) : 5,
236								  i_usx2y_out04_int, usx2y);
237						err = usb_submit_urb(usx2y->as04.urb[j], GFP_ATOMIC);
238						us428ctls->p4out_sent = send;
239						break;
240					}
241				}
242			}
243		}
244	}
245
246	if (err)
247		dev_err(&urb->dev->dev, "in04_int() usb_submit_urb err=%i\n", err);
248
249	urb->dev = usx2y->dev;
250	usb_submit_urb(urb, GFP_ATOMIC);
251}
252
253/*
254 * Prepare some urbs
255 */
256int usx2y_async_seq04_init(struct usx2ydev *usx2y)
257{
258	int	err = 0, i;
259
260	if (WARN_ON(usx2y->as04.buffer))
261		return -EBUSY;
262
263	usx2y->as04.buffer = kmalloc_array(URBS_ASYNC_SEQ,
264					   URB_DATA_LEN_ASYNC_SEQ, GFP_KERNEL);
265	if (!usx2y->as04.buffer) {
266		err = -ENOMEM;
267	} else {
268		for (i = 0; i < URBS_ASYNC_SEQ; ++i) {
269			usx2y->as04.urb[i] = usb_alloc_urb(0, GFP_KERNEL);
270			if (!usx2y->as04.urb[i]) {
271				err = -ENOMEM;
272				break;
273			}
274			usb_fill_bulk_urb(usx2y->as04.urb[i], usx2y->dev,
275					  usb_sndbulkpipe(usx2y->dev, 0x04),
276					  usx2y->as04.buffer + URB_DATA_LEN_ASYNC_SEQ * i, 0,
277					  i_usx2y_out04_int, usx2y);
278			err = usb_urb_ep_type_check(usx2y->as04.urb[i]);
279			if (err < 0)
280				break;
281		}
282	}
283	if (err)
284		usx2y_unlinkseq(&usx2y->as04);
285	return err;
286}
287
288int usx2y_in04_init(struct usx2ydev *usx2y)
289{
290	int err;
291
292	if (WARN_ON(usx2y->in04_urb))
293		return -EBUSY;
294
295	usx2y->in04_urb = usb_alloc_urb(0, GFP_KERNEL);
296	if (!usx2y->in04_urb) {
297		err = -ENOMEM;
298		goto error;
299	}
300
301	usx2y->in04_buf = kmalloc(21, GFP_KERNEL);
302	if (!usx2y->in04_buf) {
303		err = -ENOMEM;
304		goto error;
305	}
306
307	init_waitqueue_head(&usx2y->in04_wait_queue);
308	usb_fill_int_urb(usx2y->in04_urb, usx2y->dev, usb_rcvintpipe(usx2y->dev, 0x4),
309			 usx2y->in04_buf, 21,
310			 i_usx2y_in04_int, usx2y,
311			 10);
312	if (usb_urb_ep_type_check(usx2y->in04_urb)) {
313		err = -EINVAL;
314		goto error;
315	}
316	return usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
317
318 error:
319	kfree(usx2y->in04_buf);
320	usb_free_urb(usx2y->in04_urb);
321	usx2y->in04_buf = NULL;
322	usx2y->in04_urb = NULL;
323	return err;
324}
325
326static void usx2y_unlinkseq(struct snd_usx2y_async_seq *s)
327{
328	int	i;
329
330	for (i = 0; i < URBS_ASYNC_SEQ; ++i) {
331		if (!s->urb[i])
332			continue;
333		usb_kill_urb(s->urb[i]);
334		usb_free_urb(s->urb[i]);
335		s->urb[i] = NULL;
336	}
337	kfree(s->buffer);
338	s->buffer = NULL;
339}
340
341static const struct usb_device_id snd_usx2y_usb_id_table[] = {
342	{
343		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
344		.idVendor =	0x1604,
345		.idProduct =	USB_ID_US428
346	},
347	{
348		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
349		.idVendor =	0x1604,
350		.idProduct =	USB_ID_US122
351	},
352	{
353		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
354		.idVendor =	0x1604,
355		.idProduct =	USB_ID_US224
356	},
357	{ /* terminator */ }
358};
359MODULE_DEVICE_TABLE(usb, snd_usx2y_usb_id_table);
360
361static int usx2y_create_card(struct usb_device *device,
362			     struct usb_interface *intf,
363			     struct snd_card **cardp)
364{
365	int		dev;
366	struct snd_card *card;
367	int err;
368
369	for (dev = 0; dev < SNDRV_CARDS; ++dev)
370		if (enable[dev] && !snd_usx2y_card_used[dev])
371			break;
372	if (dev >= SNDRV_CARDS)
373		return -ENODEV;
374	err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
375			   sizeof(struct usx2ydev), &card);
376	if (err < 0)
377		return err;
378	snd_usx2y_card_used[usx2y(card)->card_index = dev] = 1;
379	card->private_free = snd_usx2y_card_private_free;
380	usx2y(card)->dev = device;
381	init_waitqueue_head(&usx2y(card)->prepare_wait_queue);
382	init_waitqueue_head(&usx2y(card)->us428ctls_wait_queue_head);
383	mutex_init(&usx2y(card)->pcm_mutex);
384	INIT_LIST_HEAD(&usx2y(card)->midi_list);
385	strscpy(card->driver, "USB "NAME_ALLCAPS"");
386	sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
387	sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
388		card->shortname,
389		le16_to_cpu(device->descriptor.idVendor),
390		le16_to_cpu(device->descriptor.idProduct),
391		0,//us428(card)->usbmidi.ifnum,
392		usx2y(card)->dev->bus->busnum, usx2y(card)->dev->devnum);
393	*cardp = card;
394	return 0;
395}
396
397static void snd_usx2y_card_private_free(struct snd_card *card)
398{
399	struct usx2ydev *usx2y = usx2y(card);
400
401	kfree(usx2y->in04_buf);
402	usb_free_urb(usx2y->in04_urb);
403	if (usx2y->us428ctls_sharedmem)
404		free_pages_exact(usx2y->us428ctls_sharedmem,
405				 US428_SHAREDMEM_PAGES);
406	if (usx2y->card_index >= 0 && usx2y->card_index < SNDRV_CARDS)
407		snd_usx2y_card_used[usx2y->card_index] = 0;
408}
409
410static void snd_usx2y_disconnect(struct usb_interface *intf)
411{
412	struct snd_card *card;
413	struct usx2ydev *usx2y;
414	struct list_head *p;
415
416	card = usb_get_intfdata(intf);
417	if (!card)
418		return;
419	usx2y = usx2y(card);
420	usx2y->chip_status = USX2Y_STAT_CHIP_HUP;
421	usx2y_unlinkseq(&usx2y->as04);
422	usb_kill_urb(usx2y->in04_urb);
423	snd_card_disconnect(card);
424
425	/* release the midi resources */
426	list_for_each(p, &usx2y->midi_list) {
427		snd_usbmidi_disconnect(p);
428	}
429	if (usx2y->us428ctls_sharedmem)
430		wake_up(&usx2y->us428ctls_wait_queue_head);
431	snd_card_free_when_closed(card);
432}
433
434static int snd_usx2y_probe(struct usb_interface *intf,
435			   const struct usb_device_id *id)
436{
437	struct usb_device *device = interface_to_usbdev(intf);
438	struct snd_card *card;
439	int err;
440
441#ifdef USX2Y_NRPACKS_VARIABLE
442	if (nrpacks < 0 || nrpacks > USX2Y_NRPACKS_MAX)
443		return -EINVAL;
444#endif
445
446	if (le16_to_cpu(device->descriptor.idVendor) != 0x1604 ||
447	    (le16_to_cpu(device->descriptor.idProduct) != USB_ID_US122 &&
448	     le16_to_cpu(device->descriptor.idProduct) != USB_ID_US224 &&
449	     le16_to_cpu(device->descriptor.idProduct) != USB_ID_US428))
450		return -EINVAL;
451
452	err = usx2y_create_card(device, intf, &card);
453	if (err < 0)
454		return err;
455	err = usx2y_hwdep_new(card, device);
456	if (err < 0)
457		goto error;
458	err = snd_card_register(card);
459	if (err < 0)
460		goto error;
461
462	dev_set_drvdata(&intf->dev, card);
463	return 0;
464
465 error:
466	snd_card_free(card);
467	return err;
468}
469
470static struct usb_driver snd_usx2y_usb_driver = {
471	.name =		"snd-usb-usx2y",
472	.probe =	snd_usx2y_probe,
473	.disconnect =	snd_usx2y_disconnect,
474	.id_table =	snd_usx2y_usb_id_table,
475};
476module_usb_driver(snd_usx2y_usb_driver);
~/ Read-only file preview.