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
);
86 * Wrapper for usb_control_msg().
87 * Allocates a temp buffer to prevent dmaing from/to the stack.
89 static int us122l_ctl_msg(struct usb_device
*dev
, unsigned int pipe
,
90 __u8 request
, __u8 requesttype
,
91 __u16 value
, __u16 index
, void *data
,
92 __u16 size
, int timeout
)
98 buf
= kmemdup(data
, size
, GFP_KERNEL
);
102 err
= usb_control_msg(dev
, pipe
, request
, requesttype
,
103 value
, index
, buf
, size
, timeout
);
105 memcpy(data
, buf
, size
);
111 static void pt_info_set(struct usb_device
*dev
, u8 v
)
115 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
117 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
118 v
, 0, NULL
, 0, 1000);
119 snd_printdd(KERN_DEBUG
"%i\n", ret
);
122 static void usb_stream_hwdep_vm_open(struct vm_area_struct
*area
)
124 struct us122l
*us122l
= area
->vm_private_data
;
125 atomic_inc(&us122l
->mmap_count
);
126 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
129 static vm_fault_t
usb_stream_hwdep_vm_fault(struct vm_fault
*vmf
)
131 unsigned long offset
;
134 struct us122l
*us122l
= vmf
->vma
->vm_private_data
;
135 struct usb_stream
*s
;
137 mutex_lock(&us122l
->mutex
);
142 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
143 if (offset
< PAGE_ALIGN(s
->read_size
))
144 vaddr
= (char *)s
+ offset
;
146 offset
-= PAGE_ALIGN(s
->read_size
);
147 if (offset
>= PAGE_ALIGN(s
->write_size
))
150 vaddr
= us122l
->sk
.write_page
+ offset
;
152 page
= virt_to_page(vaddr
);
155 mutex_unlock(&us122l
->mutex
);
161 mutex_unlock(&us122l
->mutex
);
162 return VM_FAULT_SIGBUS
;
165 static void usb_stream_hwdep_vm_close(struct vm_area_struct
*area
)
167 struct us122l
*us122l
= area
->vm_private_data
;
168 atomic_dec(&us122l
->mmap_count
);
169 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
172 static const struct vm_operations_struct usb_stream_hwdep_vm_ops
= {
173 .open
= usb_stream_hwdep_vm_open
,
174 .fault
= usb_stream_hwdep_vm_fault
,
175 .close
= usb_stream_hwdep_vm_close
,
179 static int usb_stream_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
181 struct us122l
*us122l
= hw
->private_data
;
182 struct usb_interface
*iface
;
183 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
188 us122l
->first
= file
;
190 if (us122l
->is_us144
) {
191 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
192 usb_autopm_get_interface(iface
);
194 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
195 usb_autopm_get_interface(iface
);
199 static int usb_stream_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
201 struct us122l
*us122l
= hw
->private_data
;
202 struct usb_interface
*iface
;
203 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
205 if (us122l
->is_us144
) {
206 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
207 usb_autopm_put_interface(iface
);
209 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
210 usb_autopm_put_interface(iface
);
211 if (us122l
->first
== file
)
212 us122l
->first
= NULL
;
213 mutex_lock(&us122l
->mutex
);
214 if (us122l
->master
== file
)
215 us122l
->master
= us122l
->slave
;
217 us122l
->slave
= NULL
;
218 mutex_unlock(&us122l
->mutex
);
222 static int usb_stream_hwdep_mmap(struct snd_hwdep
*hw
,
223 struct file
*filp
, struct vm_area_struct
*area
)
225 unsigned long size
= area
->vm_end
- area
->vm_start
;
226 struct us122l
*us122l
= hw
->private_data
;
227 unsigned long offset
;
228 struct usb_stream
*s
;
232 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
233 mutex_lock(&us122l
->mutex
);
235 read
= offset
< s
->read_size
;
236 if (read
&& area
->vm_flags
& VM_WRITE
) {
240 snd_printdd(KERN_DEBUG
"%lu %u\n", size
,
241 read
? s
->read_size
: s
->write_size
);
242 /* if userspace tries to mmap beyond end of our buffer, fail */
243 if (size
> PAGE_ALIGN(read
? s
->read_size
: s
->write_size
)) {
244 snd_printk(KERN_WARNING
"%lu > %u\n", size
,
245 read
? s
->read_size
: s
->write_size
);
250 area
->vm_ops
= &usb_stream_hwdep_vm_ops
;
251 area
->vm_flags
|= VM_DONTDUMP
;
253 area
->vm_flags
|= VM_DONTEXPAND
;
254 area
->vm_private_data
= us122l
;
255 atomic_inc(&us122l
->mmap_count
);
257 mutex_unlock(&us122l
->mutex
);
261 static __poll_t
usb_stream_hwdep_poll(struct snd_hwdep
*hw
,
262 struct file
*file
, poll_table
*wait
)
264 struct us122l
*us122l
= hw
->private_data
;
268 poll_wait(file
, &us122l
->sk
.sleep
, wait
);
270 mask
= EPOLLIN
| EPOLLOUT
| EPOLLWRNORM
| EPOLLERR
;
271 if (mutex_trylock(&us122l
->mutex
)) {
272 struct usb_stream
*s
= us122l
->sk
.s
;
273 if (s
&& s
->state
== usb_stream_ready
) {
274 if (us122l
->first
== file
)
275 polled
= &s
->periods_polled
;
277 polled
= &us122l
->second_periods_polled
;
278 if (*polled
!= s
->periods_done
) {
279 *polled
= s
->periods_done
;
280 mask
= EPOLLIN
| EPOLLOUT
| EPOLLWRNORM
;
284 mutex_unlock(&us122l
->mutex
);
289 static void us122l_stop(struct us122l
*us122l
)
292 list_for_each(p
, &us122l
->midi_list
)
293 snd_usbmidi_input_stop(p
);
295 usb_stream_stop(&us122l
->sk
);
296 usb_stream_free(&us122l
->sk
);
299 static int us122l_set_sample_rate(struct usb_device
*dev
, int rate
)
301 unsigned int ep
= 0x81;
302 unsigned char data
[3];
307 data
[2] = rate
>> 16;
308 err
= us122l_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
309 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
310 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3, 1000);
312 snd_printk(KERN_ERR
"%d: cannot set freq %d to ep 0x%x\n",
313 dev
->devnum
, rate
, ep
);
317 static bool us122l_start(struct us122l
*us122l
,
318 unsigned rate
, unsigned period_frames
)
322 unsigned use_packsize
= 0;
323 bool success
= false;
325 if (us122l
->dev
->speed
== USB_SPEED_HIGH
) {
326 /* The us-122l's descriptor defaults to iso max_packsize 78,
327 which isn't needed for samplerates <= 48000.
328 Lets save some memory:
342 if (!usb_stream_new(&us122l
->sk
, us122l
->dev
, 1, 2,
343 rate
, use_packsize
, period_frames
, 6))
346 err
= us122l_set_sample_rate(us122l
->dev
, rate
);
349 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
352 err
= usb_stream_start(&us122l
->sk
);
355 snd_printk(KERN_ERR
"us122l_start error %i \n", err
);
358 list_for_each(p
, &us122l
->midi_list
)
359 snd_usbmidi_input_start(p
);
365 static int usb_stream_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
366 unsigned cmd
, unsigned long arg
)
368 struct usb_stream_config cfg
;
369 struct us122l
*us122l
= hw
->private_data
;
370 struct usb_stream
*s
;
371 unsigned min_period_frames
;
375 if (cmd
!= SNDRV_USB_STREAM_IOCTL_SET_PARAMS
)
378 if (copy_from_user(&cfg
, (void __user
*)arg
, sizeof(cfg
)))
381 if (cfg
.version
!= USB_STREAM_INTERFACE_VERSION
)
384 high_speed
= us122l
->dev
->speed
== USB_SPEED_HIGH
;
385 if ((cfg
.sample_rate
!= 44100 && cfg
.sample_rate
!= 48000 &&
387 (cfg
.sample_rate
!= 88200 && cfg
.sample_rate
!= 96000))) ||
388 cfg
.frame_size
!= 6 ||
389 cfg
.period_frames
> 0x3000)
392 switch (cfg
.sample_rate
) {
394 min_period_frames
= 48;
397 min_period_frames
= 52;
400 min_period_frames
= 104;
404 min_period_frames
<<= 1;
405 if (cfg
.period_frames
< min_period_frames
)
408 snd_power_wait(hw
->card
, SNDRV_CTL_POWER_D0
);
410 mutex_lock(&us122l
->mutex
);
413 us122l
->master
= file
;
414 else if (us122l
->master
!= file
) {
415 if (!s
|| memcmp(&cfg
, &s
->cfg
, sizeof(cfg
))) {
419 us122l
->slave
= file
;
421 if (!s
|| memcmp(&cfg
, &s
->cfg
, sizeof(cfg
)) ||
422 s
->state
== usb_stream_xrun
) {
424 if (!us122l_start(us122l
, cfg
.sample_rate
, cfg
.period_frames
))
430 mutex_unlock(&us122l
->mutex
);
431 wake_up_all(&us122l
->sk
.sleep
);
435 #define SND_USB_STREAM_ID "USB STREAM"
436 static int usb_stream_hwdep_new(struct snd_card
*card
)
439 struct snd_hwdep
*hw
;
440 struct usb_device
*dev
= US122L(card
)->dev
;
442 err
= snd_hwdep_new(card
, SND_USB_STREAM_ID
, 0, &hw
);
446 hw
->iface
= SNDRV_HWDEP_IFACE_USB_STREAM
;
447 hw
->private_data
= US122L(card
);
448 hw
->ops
.open
= usb_stream_hwdep_open
;
449 hw
->ops
.release
= usb_stream_hwdep_release
;
450 hw
->ops
.ioctl
= usb_stream_hwdep_ioctl
;
451 hw
->ops
.ioctl_compat
= usb_stream_hwdep_ioctl
;
452 hw
->ops
.mmap
= usb_stream_hwdep_mmap
;
453 hw
->ops
.poll
= usb_stream_hwdep_poll
;
455 sprintf(hw
->name
, "/dev/bus/usb/%03d/%03d/hwdeppcm",
456 dev
->bus
->busnum
, dev
->devnum
);
461 static bool us122l_create_card(struct snd_card
*card
)
464 struct us122l
*us122l
= US122L(card
);
466 if (us122l
->is_us144
) {
467 err
= usb_set_interface(us122l
->dev
, 0, 1);
469 snd_printk(KERN_ERR
"usb_set_interface error \n");
473 err
= usb_set_interface(us122l
->dev
, 1, 1);
475 snd_printk(KERN_ERR
"usb_set_interface error \n");
479 pt_info_set(us122l
->dev
, 0x11);
480 pt_info_set(us122l
->dev
, 0x10);
482 if (!us122l_start(us122l
, 44100, 256))
485 if (us122l
->is_us144
)
486 err
= us144_create_usbmidi(card
);
488 err
= us122l_create_usbmidi(card
);
490 snd_printk(KERN_ERR
"us122l_create_usbmidi error %i \n", err
);
493 err
= usb_stream_hwdep_new(card
);
495 /* release the midi resources */
497 list_for_each(p
, &us122l
->midi_list
)
498 snd_usbmidi_disconnect(p
);
509 static void snd_us122l_free(struct snd_card
*card
)
511 struct us122l
*us122l
= US122L(card
);
512 int index
= us122l
->card_index
;
513 if (index
>= 0 && index
< SNDRV_CARDS
)
514 snd_us122l_card_used
[index
] = 0;
517 static int usx2y_create_card(struct usb_device
*device
,
518 struct usb_interface
*intf
,
519 struct snd_card
**cardp
,
523 struct snd_card
*card
;
526 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
527 if (enable
[dev
] && !snd_us122l_card_used
[dev
])
529 if (dev
>= SNDRV_CARDS
)
531 err
= snd_card_new(&intf
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
532 sizeof(struct us122l
), &card
);
535 snd_us122l_card_used
[US122L(card
)->card_index
= dev
] = 1;
536 card
->private_free
= snd_us122l_free
;
537 US122L(card
)->dev
= device
;
538 mutex_init(&US122L(card
)->mutex
);
539 init_waitqueue_head(&US122L(card
)->sk
.sleep
);
540 US122L(card
)->is_us144
= flags
& US122L_FLAG_US144
;
541 INIT_LIST_HEAD(&US122L(card
)->midi_list
);
542 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
543 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
544 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
546 le16_to_cpu(device
->descriptor
.idVendor
),
547 le16_to_cpu(device
->descriptor
.idProduct
),
549 US122L(card
)->dev
->bus
->busnum
,
550 US122L(card
)->dev
->devnum
556 static int us122l_usb_probe(struct usb_interface
*intf
,
557 const struct usb_device_id
*device_id
,
558 struct snd_card
**cardp
)
560 struct usb_device
*device
= interface_to_usbdev(intf
);
561 struct snd_card
*card
;
564 err
= usx2y_create_card(device
, intf
, &card
, device_id
->driver_info
);
568 if (!us122l_create_card(card
)) {
573 err
= snd_card_register(card
);
579 usb_get_intf(usb_ifnum_to_if(device
, 0));
585 static int snd_us122l_probe(struct usb_interface
*intf
,
586 const struct usb_device_id
*id
)
588 struct usb_device
*device
= interface_to_usbdev(intf
);
589 struct snd_card
*card
;
592 if (id
->driver_info
& US122L_FLAG_US144
&&
593 device
->speed
== USB_SPEED_HIGH
) {
594 snd_printk(KERN_ERR
"disable ehci-hcd to run US-144 \n");
598 snd_printdd(KERN_DEBUG
"%p:%i\n",
599 intf
, intf
->cur_altsetting
->desc
.bInterfaceNumber
);
600 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
603 err
= us122l_usb_probe(usb_get_intf(intf
), id
, &card
);
609 usb_set_intfdata(intf
, card
);
613 static void snd_us122l_disconnect(struct usb_interface
*intf
)
615 struct snd_card
*card
;
616 struct us122l
*us122l
;
619 card
= usb_get_intfdata(intf
);
623 snd_card_disconnect(card
);
625 us122l
= US122L(card
);
626 mutex_lock(&us122l
->mutex
);
628 mutex_unlock(&us122l
->mutex
);
630 /* release the midi resources */
631 list_for_each(p
, &us122l
->midi_list
) {
632 snd_usbmidi_disconnect(p
);
635 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 0));
636 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 1));
637 usb_put_dev(us122l
->dev
);
639 while (atomic_read(&us122l
->mmap_count
))
645 static int snd_us122l_suspend(struct usb_interface
*intf
, pm_message_t message
)
647 struct snd_card
*card
;
648 struct us122l
*us122l
;
651 card
= usb_get_intfdata(intf
);
654 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
656 us122l
= US122L(card
);
660 list_for_each(p
, &us122l
->midi_list
)
661 snd_usbmidi_input_stop(p
);
663 mutex_lock(&us122l
->mutex
);
664 usb_stream_stop(&us122l
->sk
);
665 mutex_unlock(&us122l
->mutex
);
670 static int snd_us122l_resume(struct usb_interface
*intf
)
672 struct snd_card
*card
;
673 struct us122l
*us122l
;
677 card
= usb_get_intfdata(intf
);
681 us122l
= US122L(card
);
685 mutex_lock(&us122l
->mutex
);
686 /* needed, doesn't restart without: */
687 if (us122l
->is_us144
) {
688 err
= usb_set_interface(us122l
->dev
, 0, 1);
690 snd_printk(KERN_ERR
"usb_set_interface error \n");
694 err
= usb_set_interface(us122l
->dev
, 1, 1);
696 snd_printk(KERN_ERR
"usb_set_interface error \n");
700 pt_info_set(us122l
->dev
, 0x11);
701 pt_info_set(us122l
->dev
, 0x10);
703 err
= us122l_set_sample_rate(us122l
->dev
,
704 us122l
->sk
.s
->cfg
.sample_rate
);
706 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
709 err
= usb_stream_start(&us122l
->sk
);
713 list_for_each(p
, &us122l
->midi_list
)
714 snd_usbmidi_input_start(p
);
716 mutex_unlock(&us122l
->mutex
);
717 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
721 static const struct usb_device_id snd_us122l_usb_id_table
[] = {
723 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
725 .idProduct
= USB_ID_US122L
727 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
728 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
730 .idProduct
= USB_ID_US144
,
731 .driver_info
= US122L_FLAG_US144
734 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
736 .idProduct
= USB_ID_US122MKII
739 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
741 .idProduct
= USB_ID_US144MKII
,
742 .driver_info
= US122L_FLAG_US144
747 MODULE_DEVICE_TABLE(usb
, snd_us122l_usb_id_table
);
748 static struct usb_driver snd_us122l_usb_driver
= {
749 .name
= "snd-usb-us122l",
750 .probe
= snd_us122l_probe
,
751 .disconnect
= snd_us122l_disconnect
,
752 .suspend
= snd_us122l_suspend
,
753 .resume
= snd_us122l_resume
,
754 .reset_resume
= snd_us122l_resume
,
755 .id_table
= snd_us122l_usb_id_table
,
756 .supports_autosuspend
= 1
759 module_usb_driver(snd_us122l_usb_driver
);