ALSA: usx2y - reparent sound device
[linux/fpc-iii.git] / sound / usb / usx2y / us122l.c
blobfd44946ce4b3a21aa4bae548144fcf6b66df676b
1 /*
2 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <sound/core.h>
20 #include <sound/hwdep.h>
21 #include <sound/pcm.h>
22 #include <sound/initval.h>
23 #define MODNAME "US122L"
24 #include "usb_stream.c"
25 #include "../usbaudio.h"
26 #include "us122l.h"
28 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
29 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
30 MODULE_LICENSE("GPL");
32 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
33 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
34 /* Enable this card */
35 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
37 module_param_array(index, int, NULL, 0444);
38 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
39 module_param_array(id, charp, NULL, 0444);
40 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
44 static int snd_us122l_card_used[SNDRV_CARDS];
47 static int us122l_create_usbmidi(struct snd_card *card)
49 static struct snd_usb_midi_endpoint_info quirk_data = {
50 .out_ep = 4,
51 .in_ep = 3,
52 .out_cables = 0x001,
53 .in_cables = 0x001
55 static struct snd_usb_audio_quirk quirk = {
56 .vendor_name = "US122L",
57 .product_name = NAME_ALLCAPS,
58 .ifnum = 1,
59 .type = QUIRK_MIDI_US122L,
60 .data = &quirk_data
62 struct usb_device *dev = US122L(card)->chip.dev;
63 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
65 return snd_usb_create_midi_interface(&US122L(card)->chip,
66 iface, &quirk);
70 * Wrapper for usb_control_msg().
71 * Allocates a temp buffer to prevent dmaing from/to the stack.
73 static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
74 __u8 request, __u8 requesttype,
75 __u16 value, __u16 index, void *data,
76 __u16 size, int timeout)
78 int err;
79 void *buf = NULL;
81 if (size > 0) {
82 buf = kmemdup(data, size, GFP_KERNEL);
83 if (!buf)
84 return -ENOMEM;
86 err = usb_control_msg(dev, pipe, request, requesttype,
87 value, index, buf, size, timeout);
88 if (size > 0) {
89 memcpy(data, buf, size);
90 kfree(buf);
92 return err;
95 static void pt_info_set(struct usb_device *dev, u8 v)
97 int ret;
99 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
100 'I',
101 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
102 v, 0, NULL, 0, 1000);
103 snd_printdd(KERN_DEBUG "%i\n", ret);
106 static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
108 struct us122l *us122l = area->vm_private_data;
109 atomic_inc(&us122l->mmap_count);
110 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
113 static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
114 struct vm_fault *vmf)
116 unsigned long offset;
117 struct page *page;
118 void *vaddr;
119 struct us122l *us122l = area->vm_private_data;
120 struct usb_stream *s;
122 mutex_lock(&us122l->mutex);
123 s = us122l->sk.s;
124 if (!s)
125 goto unlock;
127 offset = vmf->pgoff << PAGE_SHIFT;
128 if (offset < PAGE_ALIGN(s->read_size))
129 vaddr = (char *)s + offset;
130 else {
131 offset -= PAGE_ALIGN(s->read_size);
132 if (offset >= PAGE_ALIGN(s->write_size))
133 goto unlock;
135 vaddr = us122l->sk.write_page + offset;
137 page = virt_to_page(vaddr);
139 get_page(page);
140 mutex_unlock(&us122l->mutex);
142 vmf->page = page;
144 return 0;
145 unlock:
146 mutex_unlock(&us122l->mutex);
147 return VM_FAULT_SIGBUS;
150 static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
152 struct us122l *us122l = area->vm_private_data;
153 atomic_dec(&us122l->mmap_count);
154 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
157 static struct vm_operations_struct usb_stream_hwdep_vm_ops = {
158 .open = usb_stream_hwdep_vm_open,
159 .fault = usb_stream_hwdep_vm_fault,
160 .close = usb_stream_hwdep_vm_close,
164 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
166 struct us122l *us122l = hw->private_data;
167 struct usb_interface *iface;
168 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
169 if (hw->used >= 2)
170 return -EBUSY;
172 if (!us122l->first)
173 us122l->first = file;
174 iface = usb_ifnum_to_if(us122l->chip.dev, 1);
175 usb_autopm_get_interface(iface);
176 return 0;
179 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
181 struct us122l *us122l = hw->private_data;
182 struct usb_interface *iface = usb_ifnum_to_if(us122l->chip.dev, 1);
183 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
184 usb_autopm_put_interface(iface);
185 if (us122l->first == file)
186 us122l->first = NULL;
187 mutex_lock(&us122l->mutex);
188 if (us122l->master == file)
189 us122l->master = us122l->slave;
191 us122l->slave = NULL;
192 mutex_unlock(&us122l->mutex);
193 return 0;
196 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
197 struct file *filp, struct vm_area_struct *area)
199 unsigned long size = area->vm_end - area->vm_start;
200 struct us122l *us122l = hw->private_data;
201 unsigned long offset;
202 struct usb_stream *s;
203 int err = 0;
204 bool read;
206 offset = area->vm_pgoff << PAGE_SHIFT;
207 mutex_lock(&us122l->mutex);
208 s = us122l->sk.s;
209 read = offset < s->read_size;
210 if (read && area->vm_flags & VM_WRITE) {
211 err = -EPERM;
212 goto out;
214 snd_printdd(KERN_DEBUG "%lu %u\n", size,
215 read ? s->read_size : s->write_size);
216 /* if userspace tries to mmap beyond end of our buffer, fail */
217 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
218 snd_printk(KERN_WARNING "%lu > %u\n", size,
219 read ? s->read_size : s->write_size);
220 err = -EINVAL;
221 goto out;
224 area->vm_ops = &usb_stream_hwdep_vm_ops;
225 area->vm_flags |= VM_RESERVED;
226 area->vm_private_data = us122l;
227 atomic_inc(&us122l->mmap_count);
228 out:
229 mutex_unlock(&us122l->mutex);
230 return err;
233 static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
234 struct file *file, poll_table *wait)
236 struct us122l *us122l = hw->private_data;
237 struct usb_stream *s = us122l->sk.s;
238 unsigned *polled;
239 unsigned int mask;
241 poll_wait(file, &us122l->sk.sleep, wait);
243 switch (s->state) {
244 case usb_stream_ready:
245 if (us122l->first == file)
246 polled = &s->periods_polled;
247 else
248 polled = &us122l->second_periods_polled;
249 if (*polled != s->periods_done) {
250 *polled = s->periods_done;
251 mask = POLLIN | POLLOUT | POLLWRNORM;
252 break;
254 /* Fall through */
255 mask = 0;
256 break;
257 default:
258 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
259 break;
261 return mask;
264 static void us122l_stop(struct us122l *us122l)
266 struct list_head *p;
267 list_for_each(p, &us122l->chip.midi_list)
268 snd_usbmidi_input_stop(p);
270 usb_stream_stop(&us122l->sk);
271 usb_stream_free(&us122l->sk);
274 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
276 unsigned int ep = 0x81;
277 unsigned char data[3];
278 int err;
280 data[0] = rate;
281 data[1] = rate >> 8;
282 data[2] = rate >> 16;
283 err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
284 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
285 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000);
286 if (err < 0)
287 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
288 dev->devnum, rate, ep);
289 return err;
292 static bool us122l_start(struct us122l *us122l,
293 unsigned rate, unsigned period_frames)
295 struct list_head *p;
296 int err;
297 unsigned use_packsize = 0;
298 bool success = false;
300 if (us122l->chip.dev->speed == USB_SPEED_HIGH) {
301 /* The us-122l's descriptor defaults to iso max_packsize 78,
302 which isn't needed for samplerates <= 48000.
303 Lets save some memory:
305 switch (rate) {
306 case 44100:
307 use_packsize = 36;
308 break;
309 case 48000:
310 use_packsize = 42;
311 break;
312 case 88200:
313 use_packsize = 72;
314 break;
317 if (!usb_stream_new(&us122l->sk, us122l->chip.dev, 1, 2,
318 rate, use_packsize, period_frames, 6))
319 goto out;
321 err = us122l_set_sample_rate(us122l->chip.dev, rate);
322 if (err < 0) {
323 us122l_stop(us122l);
324 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
325 goto out;
327 err = usb_stream_start(&us122l->sk);
328 if (err < 0) {
329 us122l_stop(us122l);
330 snd_printk(KERN_ERR "us122l_start error %i \n", err);
331 goto out;
333 list_for_each(p, &us122l->chip.midi_list)
334 snd_usbmidi_input_start(p);
335 success = true;
336 out:
337 return success;
340 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
341 unsigned cmd, unsigned long arg)
343 struct usb_stream_config *cfg;
344 struct us122l *us122l = hw->private_data;
345 unsigned min_period_frames;
346 int err = 0;
347 bool high_speed;
349 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
350 return -ENOTTY;
352 cfg = memdup_user((void *)arg, sizeof(*cfg));
353 if (IS_ERR(cfg))
354 return PTR_ERR(cfg);
356 if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
357 err = -ENXIO;
358 goto free;
360 high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH;
361 if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
362 (!high_speed ||
363 (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
364 cfg->frame_size != 6 ||
365 cfg->period_frames > 0x3000) {
366 err = -EINVAL;
367 goto free;
369 switch (cfg->sample_rate) {
370 case 44100:
371 min_period_frames = 48;
372 break;
373 case 48000:
374 min_period_frames = 52;
375 break;
376 default:
377 min_period_frames = 104;
378 break;
380 if (!high_speed)
381 min_period_frames <<= 1;
382 if (cfg->period_frames < min_period_frames) {
383 err = -EINVAL;
384 goto free;
387 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
389 mutex_lock(&us122l->mutex);
390 if (!us122l->master)
391 us122l->master = file;
392 else if (us122l->master != file) {
393 if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
394 err = -EIO;
395 goto unlock;
397 us122l->slave = file;
399 if (!us122l->sk.s ||
400 memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
401 us122l->sk.s->state == usb_stream_xrun) {
402 us122l_stop(us122l);
403 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
404 err = -EIO;
405 else
406 err = 1;
408 unlock:
409 mutex_unlock(&us122l->mutex);
410 free:
411 kfree(cfg);
412 return err;
415 #define SND_USB_STREAM_ID "USB STREAM"
416 static int usb_stream_hwdep_new(struct snd_card *card)
418 int err;
419 struct snd_hwdep *hw;
420 struct usb_device *dev = US122L(card)->chip.dev;
422 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
423 if (err < 0)
424 return err;
426 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
427 hw->private_data = US122L(card);
428 hw->ops.open = usb_stream_hwdep_open;
429 hw->ops.release = usb_stream_hwdep_release;
430 hw->ops.ioctl = usb_stream_hwdep_ioctl;
431 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
432 hw->ops.mmap = usb_stream_hwdep_mmap;
433 hw->ops.poll = usb_stream_hwdep_poll;
435 sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
436 dev->bus->busnum, dev->devnum);
437 return 0;
441 static bool us122l_create_card(struct snd_card *card)
443 int err;
444 struct us122l *us122l = US122L(card);
446 err = usb_set_interface(us122l->chip.dev, 1, 1);
447 if (err) {
448 snd_printk(KERN_ERR "usb_set_interface error \n");
449 return false;
452 pt_info_set(us122l->chip.dev, 0x11);
453 pt_info_set(us122l->chip.dev, 0x10);
455 if (!us122l_start(us122l, 44100, 256))
456 return false;
458 err = us122l_create_usbmidi(card);
459 if (err < 0) {
460 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
461 us122l_stop(us122l);
462 return false;
464 err = usb_stream_hwdep_new(card);
465 if (err < 0) {
466 /* release the midi resources */
467 struct list_head *p;
468 list_for_each(p, &us122l->chip.midi_list)
469 snd_usbmidi_disconnect(p);
471 us122l_stop(us122l);
472 return false;
474 return true;
477 static void snd_us122l_free(struct snd_card *card)
479 struct us122l *us122l = US122L(card);
480 int index = us122l->chip.index;
481 if (index >= 0 && index < SNDRV_CARDS)
482 snd_us122l_card_used[index] = 0;
485 static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp)
487 int dev;
488 struct snd_card *card;
489 int err;
491 for (dev = 0; dev < SNDRV_CARDS; ++dev)
492 if (enable[dev] && !snd_us122l_card_used[dev])
493 break;
494 if (dev >= SNDRV_CARDS)
495 return -ENODEV;
496 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
497 sizeof(struct us122l), &card);
498 if (err < 0)
499 return err;
500 snd_us122l_card_used[US122L(card)->chip.index = dev] = 1;
501 card->private_free = snd_us122l_free;
502 US122L(card)->chip.dev = device;
503 US122L(card)->chip.card = card;
504 mutex_init(&US122L(card)->mutex);
505 init_waitqueue_head(&US122L(card)->sk.sleep);
506 INIT_LIST_HEAD(&US122L(card)->chip.midi_list);
507 strcpy(card->driver, "USB "NAME_ALLCAPS"");
508 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
509 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
510 card->shortname,
511 le16_to_cpu(device->descriptor.idVendor),
512 le16_to_cpu(device->descriptor.idProduct),
514 US122L(card)->chip.dev->bus->busnum,
515 US122L(card)->chip.dev->devnum
517 *cardp = card;
518 return 0;
521 static int us122l_usb_probe(struct usb_interface *intf,
522 const struct usb_device_id *device_id,
523 struct snd_card **cardp)
525 struct usb_device *device = interface_to_usbdev(intf);
526 struct snd_card *card;
527 int err;
529 err = usx2y_create_card(device, &card);
530 if (err < 0)
531 return err;
533 snd_card_set_dev(card, &intf->dev);
534 if (!us122l_create_card(card)) {
535 snd_card_free(card);
536 return -EINVAL;
539 err = snd_card_register(card);
540 if (err < 0) {
541 snd_card_free(card);
542 return err;
545 usb_get_dev(device);
546 *cardp = card;
547 return 0;
550 static int snd_us122l_probe(struct usb_interface *intf,
551 const struct usb_device_id *id)
553 struct snd_card *card;
554 int err;
556 snd_printdd(KERN_DEBUG"%p:%i\n",
557 intf, intf->cur_altsetting->desc.bInterfaceNumber);
558 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
559 return 0;
561 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
562 if (err < 0) {
563 usb_put_intf(intf);
564 return err;
567 usb_set_intfdata(intf, card);
568 return 0;
571 static void snd_us122l_disconnect(struct usb_interface *intf)
573 struct snd_card *card;
574 struct us122l *us122l;
575 struct list_head *p;
577 card = usb_get_intfdata(intf);
578 if (!card)
579 return;
581 snd_card_disconnect(card);
583 us122l = US122L(card);
584 mutex_lock(&us122l->mutex);
585 us122l_stop(us122l);
586 mutex_unlock(&us122l->mutex);
587 us122l->chip.shutdown = 1;
589 /* release the midi resources */
590 list_for_each(p, &us122l->chip.midi_list) {
591 snd_usbmidi_disconnect(p);
594 usb_put_intf(intf);
595 usb_put_dev(us122l->chip.dev);
597 while (atomic_read(&us122l->mmap_count))
598 msleep(500);
600 snd_card_free(card);
603 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
605 struct snd_card *card;
606 struct us122l *us122l;
607 struct list_head *p;
609 card = usb_get_intfdata(intf);
610 if (!card)
611 return 0;
612 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
614 us122l = US122L(card);
615 if (!us122l)
616 return 0;
618 list_for_each(p, &us122l->chip.midi_list)
619 snd_usbmidi_input_stop(p);
621 mutex_lock(&us122l->mutex);
622 usb_stream_stop(&us122l->sk);
623 mutex_unlock(&us122l->mutex);
625 return 0;
628 static int snd_us122l_resume(struct usb_interface *intf)
630 struct snd_card *card;
631 struct us122l *us122l;
632 struct list_head *p;
633 int err;
635 card = usb_get_intfdata(intf);
636 if (!card)
637 return 0;
639 us122l = US122L(card);
640 if (!us122l)
641 return 0;
643 mutex_lock(&us122l->mutex);
644 /* needed, doesn't restart without: */
645 err = usb_set_interface(us122l->chip.dev, 1, 1);
646 if (err) {
647 snd_printk(KERN_ERR "usb_set_interface error \n");
648 goto unlock;
651 pt_info_set(us122l->chip.dev, 0x11);
652 pt_info_set(us122l->chip.dev, 0x10);
654 err = us122l_set_sample_rate(us122l->chip.dev,
655 us122l->sk.s->cfg.sample_rate);
656 if (err < 0) {
657 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
658 goto unlock;
660 err = usb_stream_start(&us122l->sk);
661 if (err)
662 goto unlock;
664 list_for_each(p, &us122l->chip.midi_list)
665 snd_usbmidi_input_start(p);
666 unlock:
667 mutex_unlock(&us122l->mutex);
668 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
669 return err;
672 static struct usb_device_id snd_us122l_usb_id_table[] = {
674 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
675 .idVendor = 0x0644,
676 .idProduct = USB_ID_US122L
678 /* { */ /* US-144 maybe works when @USB1.1. Untested. */
679 /* .match_flags = USB_DEVICE_ID_MATCH_DEVICE, */
680 /* .idVendor = 0x0644, */
681 /* .idProduct = USB_ID_US144 */
682 /* }, */
683 { /* terminator */ }
686 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
687 static struct usb_driver snd_us122l_usb_driver = {
688 .name = "snd-usb-us122l",
689 .probe = snd_us122l_probe,
690 .disconnect = snd_us122l_disconnect,
691 .suspend = snd_us122l_suspend,
692 .resume = snd_us122l_resume,
693 .reset_resume = snd_us122l_resume,
694 .id_table = snd_us122l_usb_id_table,
695 .supports_autosuspend = 1
699 static int __init snd_us122l_module_init(void)
701 return usb_register(&snd_us122l_usb_driver);
704 static void __exit snd_us122l_module_exit(void)
706 usb_deregister(&snd_us122l_usb_driver);
709 module_init(snd_us122l_module_init)
710 module_exit(snd_us122l_module_exit)