1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
6 #include <linux/slab.h>
8 #include <linux/usb/audio.h>
9 #include <linux/module.h>
10 #include <sound/core.h>
11 #include <sound/hwdep.h>
12 #include <sound/pcm.h>
13 #include <sound/initval.h>
14 #define MODNAME "US122L"
15 #include "usb_stream.c"
16 #include "../usbaudio.h"
20 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
21 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
22 MODULE_LICENSE("GPL");
24 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
25 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
26 /* Enable this card */
27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
36 /* driver_info flags */
37 #define US122L_FLAG_US144 BIT(0)
39 static int snd_us122l_card_used[SNDRV_CARDS];
41 static int us122l_create_usbmidi(struct snd_card *card)
43 static const struct snd_usb_midi_endpoint_info quirk_data = {
49 static const struct snd_usb_audio_quirk quirk = {
50 .vendor_name = "US122L",
51 .product_name = NAME_ALLCAPS,
53 .type = QUIRK_MIDI_US122L,
56 struct usb_device *dev = US122L(card)->dev;
57 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
59 return snd_usbmidi_create(card, iface,
60 &US122L(card)->midi_list, &quirk);
63 static int us144_create_usbmidi(struct snd_card *card)
65 static const struct snd_usb_midi_endpoint_info quirk_data = {
71 static const struct snd_usb_audio_quirk quirk = {
72 .vendor_name = "US144",
73 .product_name = NAME_ALLCAPS,
75 .type = QUIRK_MIDI_US122L,
78 struct usb_device *dev = US122L(card)->dev;
79 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
81 return snd_usbmidi_create(card, iface,
82 &US122L(card)->midi_list, &quirk);
85 static void pt_info_set(struct usb_device *dev, u8 v)
89 ret = usb_control_msg_send(dev, 0, 'I',
90 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 v, 0, NULL, 0, 1000, GFP_NOIO);
92 snd_printdd(KERN_DEBUG "%i\n", ret);
95 static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
97 struct us122l *us122l = area->vm_private_data;
98 atomic_inc(&us122l->mmap_count);
99 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
102 static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
104 unsigned long offset;
107 struct us122l *us122l = vmf->vma->vm_private_data;
108 struct usb_stream *s;
110 mutex_lock(&us122l->mutex);
115 offset = vmf->pgoff << PAGE_SHIFT;
116 if (offset < PAGE_ALIGN(s->read_size))
117 vaddr = (char *)s + offset;
119 offset -= PAGE_ALIGN(s->read_size);
120 if (offset >= PAGE_ALIGN(s->write_size))
123 vaddr = us122l->sk.write_page + offset;
125 page = virt_to_page(vaddr);
128 mutex_unlock(&us122l->mutex);
134 mutex_unlock(&us122l->mutex);
135 return VM_FAULT_SIGBUS;
138 static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
140 struct us122l *us122l = area->vm_private_data;
141 atomic_dec(&us122l->mmap_count);
142 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
145 static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
146 .open = usb_stream_hwdep_vm_open,
147 .fault = usb_stream_hwdep_vm_fault,
148 .close = usb_stream_hwdep_vm_close,
152 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
154 struct us122l *us122l = hw->private_data;
155 struct usb_interface *iface;
156 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
161 us122l->first = file;
163 if (us122l->is_us144) {
164 iface = usb_ifnum_to_if(us122l->dev, 0);
165 usb_autopm_get_interface(iface);
167 iface = usb_ifnum_to_if(us122l->dev, 1);
168 usb_autopm_get_interface(iface);
172 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
174 struct us122l *us122l = hw->private_data;
175 struct usb_interface *iface;
176 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
178 if (us122l->is_us144) {
179 iface = usb_ifnum_to_if(us122l->dev, 0);
180 usb_autopm_put_interface(iface);
182 iface = usb_ifnum_to_if(us122l->dev, 1);
183 usb_autopm_put_interface(iface);
184 if (us122l->first == file)
185 us122l->first = NULL;
186 mutex_lock(&us122l->mutex);
187 if (us122l->master == file)
188 us122l->master = us122l->slave;
190 us122l->slave = NULL;
191 mutex_unlock(&us122l->mutex);
195 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
196 struct file *filp, struct vm_area_struct *area)
198 unsigned long size = area->vm_end - area->vm_start;
199 struct us122l *us122l = hw->private_data;
200 unsigned long offset;
201 struct usb_stream *s;
205 offset = area->vm_pgoff << PAGE_SHIFT;
206 mutex_lock(&us122l->mutex);
208 read = offset < s->read_size;
209 if (read && area->vm_flags & VM_WRITE) {
213 snd_printdd(KERN_DEBUG "%lu %u\n", size,
214 read ? s->read_size : s->write_size);
215 /* if userspace tries to mmap beyond end of our buffer, fail */
216 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
217 snd_printk(KERN_WARNING "%lu > %u\n", size,
218 read ? s->read_size : s->write_size);
223 area->vm_ops = &usb_stream_hwdep_vm_ops;
224 area->vm_flags |= VM_DONTDUMP;
226 area->vm_flags |= VM_DONTEXPAND;
227 area->vm_private_data = us122l;
228 atomic_inc(&us122l->mmap_count);
230 mutex_unlock(&us122l->mutex);
234 static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
235 struct file *file, poll_table *wait)
237 struct us122l *us122l = hw->private_data;
241 poll_wait(file, &us122l->sk.sleep, wait);
243 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR;
244 if (mutex_trylock(&us122l->mutex)) {
245 struct usb_stream *s = us122l->sk.s;
246 if (s && s->state == usb_stream_ready) {
247 if (us122l->first == file)
248 polled = &s->periods_polled;
250 polled = &us122l->second_periods_polled;
251 if (*polled != s->periods_done) {
252 *polled = s->periods_done;
253 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM;
257 mutex_unlock(&us122l->mutex);
262 static void us122l_stop(struct us122l *us122l)
265 list_for_each(p, &us122l->midi_list)
266 snd_usbmidi_input_stop(p);
268 usb_stream_stop(&us122l->sk);
269 usb_stream_free(&us122l->sk);
272 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
274 unsigned int ep = 0x81;
275 unsigned char data[3];
280 data[2] = rate >> 16;
281 err = usb_control_msg_send(dev, 0, UAC_SET_CUR,
282 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
283 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3,
286 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
287 dev->devnum, rate, ep);
291 static bool us122l_start(struct us122l *us122l,
292 unsigned rate, unsigned period_frames)
296 unsigned use_packsize = 0;
297 bool success = false;
299 if (us122l->dev->speed == USB_SPEED_HIGH) {
300 /* The us-122l's descriptor defaults to iso max_packsize 78,
301 which isn't needed for samplerates <= 48000.
302 Lets save some memory:
316 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
317 rate, use_packsize, period_frames, 6))
320 err = us122l_set_sample_rate(us122l->dev, rate);
323 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
326 err = usb_stream_start(&us122l->sk);
329 snd_printk(KERN_ERR "us122l_start error %i \n", err);
332 list_for_each(p, &us122l->midi_list)
333 snd_usbmidi_input_start(p);
339 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
340 unsigned cmd, unsigned long arg)
342 struct usb_stream_config cfg;
343 struct us122l *us122l = hw->private_data;
344 struct usb_stream *s;
345 unsigned min_period_frames;
349 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
352 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg)))
355 if (cfg.version != USB_STREAM_INTERFACE_VERSION)
358 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
359 if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000 &&
361 (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) ||
362 cfg.frame_size != 6 ||
363 cfg.period_frames > 0x3000)
366 switch (cfg.sample_rate) {
368 min_period_frames = 48;
371 min_period_frames = 52;
374 min_period_frames = 104;
378 min_period_frames <<= 1;
379 if (cfg.period_frames < min_period_frames)
382 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
384 mutex_lock(&us122l->mutex);
387 us122l->master = file;
388 else if (us122l->master != file) {
389 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) {
393 us122l->slave = file;
395 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) ||
396 s->state == usb_stream_xrun) {
398 if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames))
404 mutex_unlock(&us122l->mutex);
405 wake_up_all(&us122l->sk.sleep);
409 #define SND_USB_STREAM_ID "USB STREAM"
410 static int usb_stream_hwdep_new(struct snd_card *card)
413 struct snd_hwdep *hw;
414 struct usb_device *dev = US122L(card)->dev;
416 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
420 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
421 hw->private_data = US122L(card);
422 hw->ops.open = usb_stream_hwdep_open;
423 hw->ops.release = usb_stream_hwdep_release;
424 hw->ops.ioctl = usb_stream_hwdep_ioctl;
425 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
426 hw->ops.mmap = usb_stream_hwdep_mmap;
427 hw->ops.poll = usb_stream_hwdep_poll;
429 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm",
430 dev->bus->busnum, dev->devnum);
435 static bool us122l_create_card(struct snd_card *card)
438 struct us122l *us122l = US122L(card);
440 if (us122l->is_us144) {
441 err = usb_set_interface(us122l->dev, 0, 1);
443 snd_printk(KERN_ERR "usb_set_interface error \n");
447 err = usb_set_interface(us122l->dev, 1, 1);
449 snd_printk(KERN_ERR "usb_set_interface error \n");
453 pt_info_set(us122l->dev, 0x11);
454 pt_info_set(us122l->dev, 0x10);
456 if (!us122l_start(us122l, 44100, 256))
459 if (us122l->is_us144)
460 err = us144_create_usbmidi(card);
462 err = us122l_create_usbmidi(card);
464 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
467 err = usb_stream_hwdep_new(card);
469 /* release the midi resources */
471 list_for_each(p, &us122l->midi_list)
472 snd_usbmidi_disconnect(p);
483 static void snd_us122l_free(struct snd_card *card)
485 struct us122l *us122l = US122L(card);
486 int index = us122l->card_index;
487 if (index >= 0 && index < SNDRV_CARDS)
488 snd_us122l_card_used[index] = 0;
491 static int usx2y_create_card(struct usb_device *device,
492 struct usb_interface *intf,
493 struct snd_card **cardp,
497 struct snd_card *card;
500 for (dev = 0; dev < SNDRV_CARDS; ++dev)
501 if (enable[dev] && !snd_us122l_card_used[dev])
503 if (dev >= SNDRV_CARDS)
505 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
506 sizeof(struct us122l), &card);
509 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
510 card->private_free = snd_us122l_free;
511 US122L(card)->dev = device;
512 mutex_init(&US122L(card)->mutex);
513 init_waitqueue_head(&US122L(card)->sk.sleep);
514 US122L(card)->is_us144 = flags & US122L_FLAG_US144;
515 INIT_LIST_HEAD(&US122L(card)->midi_list);
516 strcpy(card->driver, "USB "NAME_ALLCAPS"");
517 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
518 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
520 le16_to_cpu(device->descriptor.idVendor),
521 le16_to_cpu(device->descriptor.idProduct),
523 US122L(card)->dev->bus->busnum,
524 US122L(card)->dev->devnum
530 static int us122l_usb_probe(struct usb_interface *intf,
531 const struct usb_device_id *device_id,
532 struct snd_card **cardp)
534 struct usb_device *device = interface_to_usbdev(intf);
535 struct snd_card *card;
538 err = usx2y_create_card(device, intf, &card, device_id->driver_info);
542 if (!us122l_create_card(card)) {
547 err = snd_card_register(card);
553 usb_get_intf(usb_ifnum_to_if(device, 0));
559 static int snd_us122l_probe(struct usb_interface *intf,
560 const struct usb_device_id *id)
562 struct usb_device *device = interface_to_usbdev(intf);
563 struct snd_card *card;
566 if (id->driver_info & US122L_FLAG_US144 &&
567 device->speed == USB_SPEED_HIGH) {
568 snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
572 snd_printdd(KERN_DEBUG"%p:%i\n",
573 intf, intf->cur_altsetting->desc.bInterfaceNumber);
574 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
577 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
583 usb_set_intfdata(intf, card);
587 static void snd_us122l_disconnect(struct usb_interface *intf)
589 struct snd_card *card;
590 struct us122l *us122l;
593 card = usb_get_intfdata(intf);
597 snd_card_disconnect(card);
599 us122l = US122L(card);
600 mutex_lock(&us122l->mutex);
602 mutex_unlock(&us122l->mutex);
604 /* release the midi resources */
605 list_for_each(p, &us122l->midi_list) {
606 snd_usbmidi_disconnect(p);
609 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
610 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
611 usb_put_dev(us122l->dev);
613 while (atomic_read(&us122l->mmap_count))
619 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
621 struct snd_card *card;
622 struct us122l *us122l;
625 card = usb_get_intfdata(intf);
628 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
630 us122l = US122L(card);
634 list_for_each(p, &us122l->midi_list)
635 snd_usbmidi_input_stop(p);
637 mutex_lock(&us122l->mutex);
638 usb_stream_stop(&us122l->sk);
639 mutex_unlock(&us122l->mutex);
644 static int snd_us122l_resume(struct usb_interface *intf)
646 struct snd_card *card;
647 struct us122l *us122l;
651 card = usb_get_intfdata(intf);
655 us122l = US122L(card);
659 mutex_lock(&us122l->mutex);
660 /* needed, doesn't restart without: */
661 if (us122l->is_us144) {
662 err = usb_set_interface(us122l->dev, 0, 1);
664 snd_printk(KERN_ERR "usb_set_interface error \n");
668 err = usb_set_interface(us122l->dev, 1, 1);
670 snd_printk(KERN_ERR "usb_set_interface error \n");
674 pt_info_set(us122l->dev, 0x11);
675 pt_info_set(us122l->dev, 0x10);
677 err = us122l_set_sample_rate(us122l->dev,
678 us122l->sk.s->cfg.sample_rate);
680 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
683 err = usb_stream_start(&us122l->sk);
687 list_for_each(p, &us122l->midi_list)
688 snd_usbmidi_input_start(p);
690 mutex_unlock(&us122l->mutex);
691 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
695 static const struct usb_device_id snd_us122l_usb_id_table[] = {
697 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
699 .idProduct = USB_ID_US122L
701 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
702 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
704 .idProduct = USB_ID_US144,
705 .driver_info = US122L_FLAG_US144
708 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
710 .idProduct = USB_ID_US122MKII
713 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
715 .idProduct = USB_ID_US144MKII,
716 .driver_info = US122L_FLAG_US144
721 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
722 static struct usb_driver snd_us122l_usb_driver = {
723 .name = "snd-usb-us122l",
724 .probe = snd_us122l_probe,
725 .disconnect = snd_us122l_disconnect,
726 .suspend = snd_us122l_suspend,
727 .resume = snd_us122l_resume,
728 .reset_resume = snd_us122l_resume,
729 .id_table = snd_us122l_usb_id_table,
730 .supports_autosuspend = 1
733 module_usb_driver(snd_us122l_usb_driver);