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;
99 atomic_inc(&us122l->mmap_count);
100 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
103 static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
105 unsigned long offset;
108 struct us122l *us122l = vmf->vma->vm_private_data;
109 struct usb_stream *s;
111 mutex_lock(&us122l->mutex);
116 offset = vmf->pgoff << PAGE_SHIFT;
117 if (offset < PAGE_ALIGN(s->read_size)) {
118 vaddr = (char *)s + offset;
120 offset -= PAGE_ALIGN(s->read_size);
121 if (offset >= PAGE_ALIGN(s->write_size))
124 vaddr = us122l->sk.write_page + offset;
126 page = virt_to_page(vaddr);
129 mutex_unlock(&us122l->mutex);
135 mutex_unlock(&us122l->mutex);
136 return VM_FAULT_SIGBUS;
139 static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
141 struct us122l *us122l = area->vm_private_data;
143 atomic_dec(&us122l->mmap_count);
144 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
147 static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
148 .open = usb_stream_hwdep_vm_open,
149 .fault = usb_stream_hwdep_vm_fault,
150 .close = usb_stream_hwdep_vm_close,
153 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
155 struct us122l *us122l = hw->private_data;
156 struct usb_interface *iface;
158 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
163 us122l->first = file;
165 if (us122l->is_us144) {
166 iface = usb_ifnum_to_if(us122l->dev, 0);
167 usb_autopm_get_interface(iface);
169 iface = usb_ifnum_to_if(us122l->dev, 1);
170 usb_autopm_get_interface(iface);
174 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
176 struct us122l *us122l = hw->private_data;
177 struct usb_interface *iface;
179 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
181 if (us122l->is_us144) {
182 iface = usb_ifnum_to_if(us122l->dev, 0);
183 usb_autopm_put_interface(iface);
185 iface = usb_ifnum_to_if(us122l->dev, 1);
186 usb_autopm_put_interface(iface);
187 if (us122l->first == file)
188 us122l->first = NULL;
189 mutex_lock(&us122l->mutex);
190 if (us122l->master == file)
191 us122l->master = us122l->slave;
193 us122l->slave = NULL;
194 mutex_unlock(&us122l->mutex);
198 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
199 struct file *filp, struct vm_area_struct *area)
201 unsigned long size = area->vm_end - area->vm_start;
202 struct us122l *us122l = hw->private_data;
203 unsigned long offset;
204 struct usb_stream *s;
208 offset = area->vm_pgoff << PAGE_SHIFT;
209 mutex_lock(&us122l->mutex);
211 read = offset < s->read_size;
212 if (read && area->vm_flags & VM_WRITE) {
216 snd_printdd(KERN_DEBUG "%lu %u\n", size,
217 read ? s->read_size : s->write_size);
218 /* if userspace tries to mmap beyond end of our buffer, fail */
219 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
220 snd_printk(KERN_WARNING "%lu > %u\n", size,
221 read ? s->read_size : s->write_size);
226 area->vm_ops = &usb_stream_hwdep_vm_ops;
227 vm_flags_set(area, VM_DONTDUMP);
229 vm_flags_set(area, VM_DONTEXPAND);
230 area->vm_private_data = us122l;
231 atomic_inc(&us122l->mmap_count);
233 mutex_unlock(&us122l->mutex);
237 static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
238 struct file *file, poll_table *wait)
240 struct us122l *us122l = hw->private_data;
241 unsigned int *polled;
244 poll_wait(file, &us122l->sk.sleep, wait);
246 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR;
247 if (mutex_trylock(&us122l->mutex)) {
248 struct usb_stream *s = us122l->sk.s;
250 if (s && s->state == usb_stream_ready) {
251 if (us122l->first == file)
252 polled = &s->periods_polled;
254 polled = &us122l->second_periods_polled;
255 if (*polled != s->periods_done) {
256 *polled = s->periods_done;
257 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM;
262 mutex_unlock(&us122l->mutex);
267 static void us122l_stop(struct us122l *us122l)
271 list_for_each(p, &us122l->midi_list)
272 snd_usbmidi_input_stop(p);
274 usb_stream_stop(&us122l->sk);
275 usb_stream_free(&us122l->sk);
278 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
280 unsigned int ep = 0x81;
281 unsigned char data[3];
286 data[2] = rate >> 16;
287 err = usb_control_msg_send(dev, 0, UAC_SET_CUR,
288 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
289 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3,
292 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
293 dev->devnum, rate, ep);
297 static bool us122l_start(struct us122l *us122l,
298 unsigned int rate, unsigned int period_frames)
302 unsigned int use_packsize = 0;
303 bool success = false;
305 if (us122l->dev->speed == USB_SPEED_HIGH) {
306 /* The us-122l's descriptor defaults to iso max_packsize 78,
307 which isn't needed for samplerates <= 48000.
308 Lets save some memory:
322 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
323 rate, use_packsize, period_frames, 6))
326 err = us122l_set_sample_rate(us122l->dev, rate);
329 snd_printk(KERN_ERR "us122l_set_sample_rate error\n");
332 err = usb_stream_start(&us122l->sk);
335 snd_printk(KERN_ERR "%s error %i\n", __func__, err);
338 list_for_each(p, &us122l->midi_list)
339 snd_usbmidi_input_start(p);
345 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
346 unsigned int cmd, unsigned long arg)
348 struct usb_stream_config cfg;
349 struct us122l *us122l = hw->private_data;
350 struct usb_stream *s;
351 unsigned int min_period_frames;
355 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
358 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg)))
361 if (cfg.version != USB_STREAM_INTERFACE_VERSION)
364 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
365 if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000 &&
367 (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) ||
368 cfg.frame_size != 6 ||
369 cfg.period_frames > 0x3000)
372 switch (cfg.sample_rate) {
374 min_period_frames = 48;
377 min_period_frames = 52;
380 min_period_frames = 104;
384 min_period_frames <<= 1;
385 if (cfg.period_frames < min_period_frames)
388 snd_power_wait(hw->card);
390 mutex_lock(&us122l->mutex);
392 if (!us122l->master) {
393 us122l->master = file;
394 } else if (us122l->master != file) {
395 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) {
399 us122l->slave = file;
401 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) ||
402 s->state == usb_stream_xrun) {
404 if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames))
410 mutex_unlock(&us122l->mutex);
411 wake_up_all(&us122l->sk.sleep);
415 #define SND_USB_STREAM_ID "USB STREAM"
416 static int usb_stream_hwdep_new(struct snd_card *card)
419 struct snd_hwdep *hw;
420 struct usb_device *dev = US122L(card)->dev;
422 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
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, "/dev/bus/usb/%03d/%03d/hwdeppcm",
436 dev->bus->busnum, dev->devnum);
440 static bool us122l_create_card(struct snd_card *card)
443 struct us122l *us122l = US122L(card);
445 if (us122l->is_us144) {
446 err = usb_set_interface(us122l->dev, 0, 1);
448 snd_printk(KERN_ERR "usb_set_interface error\n");
452 err = usb_set_interface(us122l->dev, 1, 1);
454 snd_printk(KERN_ERR "usb_set_interface error\n");
458 pt_info_set(us122l->dev, 0x11);
459 pt_info_set(us122l->dev, 0x10);
461 if (!us122l_start(us122l, 44100, 256))
464 if (us122l->is_us144)
465 err = us144_create_usbmidi(card);
467 err = us122l_create_usbmidi(card);
469 snd_printk(KERN_ERR "us122l_create_usbmidi error %i\n", err);
472 err = usb_stream_hwdep_new(card);
474 /* release the midi resources */
477 list_for_each(p, &us122l->midi_list)
478 snd_usbmidi_disconnect(p);
489 static void snd_us122l_free(struct snd_card *card)
491 struct us122l *us122l = US122L(card);
492 int index = us122l->card_index;
494 if (index >= 0 && index < SNDRV_CARDS)
495 snd_us122l_card_used[index] = 0;
498 static int usx2y_create_card(struct usb_device *device,
499 struct usb_interface *intf,
500 struct snd_card **cardp,
504 struct snd_card *card;
507 for (dev = 0; dev < SNDRV_CARDS; ++dev)
508 if (enable[dev] && !snd_us122l_card_used[dev])
510 if (dev >= SNDRV_CARDS)
512 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
513 sizeof(struct us122l), &card);
516 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
517 card->private_free = snd_us122l_free;
518 US122L(card)->dev = device;
519 mutex_init(&US122L(card)->mutex);
520 init_waitqueue_head(&US122L(card)->sk.sleep);
521 US122L(card)->is_us144 = flags & US122L_FLAG_US144;
522 INIT_LIST_HEAD(&US122L(card)->midi_list);
523 strcpy(card->driver, "USB "NAME_ALLCAPS"");
524 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
525 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
527 le16_to_cpu(device->descriptor.idVendor),
528 le16_to_cpu(device->descriptor.idProduct),
530 US122L(card)->dev->bus->busnum,
531 US122L(card)->dev->devnum
537 static int us122l_usb_probe(struct usb_interface *intf,
538 const struct usb_device_id *device_id,
539 struct snd_card **cardp)
541 struct usb_device *device = interface_to_usbdev(intf);
542 struct snd_card *card;
545 err = usx2y_create_card(device, intf, &card, device_id->driver_info);
549 if (!us122l_create_card(card)) {
554 err = snd_card_register(card);
560 usb_get_intf(usb_ifnum_to_if(device, 0));
566 static int snd_us122l_probe(struct usb_interface *intf,
567 const struct usb_device_id *id)
569 struct usb_device *device = interface_to_usbdev(intf);
570 struct snd_card *card;
573 if (id->driver_info & US122L_FLAG_US144 &&
574 device->speed == USB_SPEED_HIGH) {
575 snd_printk(KERN_ERR "disable ehci-hcd to run US-144\n");
579 snd_printdd(KERN_DEBUG"%p:%i\n",
580 intf, intf->cur_altsetting->desc.bInterfaceNumber);
581 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
584 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
590 usb_set_intfdata(intf, card);
594 static void snd_us122l_disconnect(struct usb_interface *intf)
596 struct snd_card *card;
597 struct us122l *us122l;
600 card = usb_get_intfdata(intf);
604 snd_card_disconnect(card);
606 us122l = US122L(card);
607 mutex_lock(&us122l->mutex);
609 mutex_unlock(&us122l->mutex);
611 /* release the midi resources */
612 list_for_each(p, &us122l->midi_list) {
613 snd_usbmidi_disconnect(p);
616 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
617 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
618 usb_put_dev(us122l->dev);
620 while (atomic_read(&us122l->mmap_count))
626 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
628 struct snd_card *card;
629 struct us122l *us122l;
632 card = usb_get_intfdata(intf);
635 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
637 us122l = US122L(card);
641 list_for_each(p, &us122l->midi_list)
642 snd_usbmidi_input_stop(p);
644 mutex_lock(&us122l->mutex);
645 usb_stream_stop(&us122l->sk);
646 mutex_unlock(&us122l->mutex);
651 static int snd_us122l_resume(struct usb_interface *intf)
653 struct snd_card *card;
654 struct us122l *us122l;
658 card = usb_get_intfdata(intf);
662 us122l = US122L(card);
666 mutex_lock(&us122l->mutex);
667 /* needed, doesn't restart without: */
668 if (us122l->is_us144) {
669 err = usb_set_interface(us122l->dev, 0, 1);
671 snd_printk(KERN_ERR "usb_set_interface error\n");
675 err = usb_set_interface(us122l->dev, 1, 1);
677 snd_printk(KERN_ERR "usb_set_interface error\n");
681 pt_info_set(us122l->dev, 0x11);
682 pt_info_set(us122l->dev, 0x10);
684 err = us122l_set_sample_rate(us122l->dev,
685 us122l->sk.s->cfg.sample_rate);
687 snd_printk(KERN_ERR "us122l_set_sample_rate error\n");
690 err = usb_stream_start(&us122l->sk);
694 list_for_each(p, &us122l->midi_list)
695 snd_usbmidi_input_start(p);
697 mutex_unlock(&us122l->mutex);
698 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
702 static const struct usb_device_id snd_us122l_usb_id_table[] = {
704 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
706 .idProduct = USB_ID_US122L
708 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
709 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
711 .idProduct = USB_ID_US144,
712 .driver_info = US122L_FLAG_US144
715 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
717 .idProduct = USB_ID_US122MKII
720 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
722 .idProduct = USB_ID_US144MKII,
723 .driver_info = US122L_FLAG_US144
727 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
729 static struct usb_driver snd_us122l_usb_driver = {
730 .name = "snd-usb-us122l",
731 .probe = snd_us122l_probe,
732 .disconnect = snd_us122l_disconnect,
733 .suspend = snd_us122l_suspend,
734 .resume = snd_us122l_resume,
735 .reset_resume = snd_us122l_resume,
736 .id_table = snd_us122l_usb_id_table,
737 .supports_autosuspend = 1
740 module_usb_driver(snd_us122l_usb_driver);