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
)
87 usb_control_msg_send(dev
, 0, 'I',
88 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
89 v
, 0, NULL
, 0, 1000, GFP_NOIO
);
92 static vm_fault_t
usb_stream_hwdep_vm_fault(struct vm_fault
*vmf
)
97 struct us122l
*us122l
= vmf
->vma
->vm_private_data
;
100 mutex_lock(&us122l
->mutex
);
105 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
106 if (offset
< PAGE_ALIGN(s
->read_size
)) {
107 vaddr
= (char *)s
+ offset
;
109 offset
-= PAGE_ALIGN(s
->read_size
);
110 if (offset
>= PAGE_ALIGN(s
->write_size
))
113 vaddr
= us122l
->sk
.write_page
+ offset
;
115 page
= virt_to_page(vaddr
);
118 mutex_unlock(&us122l
->mutex
);
124 mutex_unlock(&us122l
->mutex
);
125 return VM_FAULT_SIGBUS
;
129 static const struct vm_operations_struct usb_stream_hwdep_vm_ops
= {
130 .fault
= usb_stream_hwdep_vm_fault
,
133 static int usb_stream_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
135 struct us122l
*us122l
= hw
->private_data
;
136 struct usb_interface
*iface
;
142 us122l
->first
= file
;
144 if (us122l
->is_us144
) {
145 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
146 usb_autopm_get_interface(iface
);
148 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
149 usb_autopm_get_interface(iface
);
153 static int usb_stream_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
155 struct us122l
*us122l
= hw
->private_data
;
156 struct usb_interface
*iface
;
158 if (us122l
->is_us144
) {
159 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
160 usb_autopm_put_interface(iface
);
162 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
163 usb_autopm_put_interface(iface
);
164 if (us122l
->first
== file
)
165 us122l
->first
= NULL
;
166 mutex_lock(&us122l
->mutex
);
167 if (us122l
->master
== file
)
168 us122l
->master
= us122l
->slave
;
170 us122l
->slave
= NULL
;
171 mutex_unlock(&us122l
->mutex
);
175 static int usb_stream_hwdep_mmap(struct snd_hwdep
*hw
,
176 struct file
*filp
, struct vm_area_struct
*area
)
178 unsigned long size
= area
->vm_end
- area
->vm_start
;
179 struct us122l
*us122l
= hw
->private_data
;
180 unsigned long offset
;
181 struct usb_stream
*s
;
185 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
186 mutex_lock(&us122l
->mutex
);
188 read
= offset
< s
->read_size
;
189 if (read
&& area
->vm_flags
& VM_WRITE
) {
193 /* if userspace tries to mmap beyond end of our buffer, fail */
194 if (size
> PAGE_ALIGN(read
? s
->read_size
: s
->write_size
)) {
195 dev_warn(hw
->card
->dev
, "%s: size %lu > %u\n", __func__
,
196 size
, read
? s
->read_size
: s
->write_size
);
201 area
->vm_ops
= &usb_stream_hwdep_vm_ops
;
202 vm_flags_set(area
, VM_DONTDUMP
);
204 vm_flags_set(area
, VM_DONTEXPAND
);
205 area
->vm_private_data
= us122l
;
207 mutex_unlock(&us122l
->mutex
);
211 static __poll_t
usb_stream_hwdep_poll(struct snd_hwdep
*hw
,
212 struct file
*file
, poll_table
*wait
)
214 struct us122l
*us122l
= hw
->private_data
;
215 unsigned int *polled
;
218 poll_wait(file
, &us122l
->sk
.sleep
, wait
);
220 mask
= EPOLLIN
| EPOLLOUT
| EPOLLWRNORM
| EPOLLERR
;
221 if (mutex_trylock(&us122l
->mutex
)) {
222 struct usb_stream
*s
= us122l
->sk
.s
;
224 if (s
&& s
->state
== usb_stream_ready
) {
225 if (us122l
->first
== file
)
226 polled
= &s
->periods_polled
;
228 polled
= &us122l
->second_periods_polled
;
229 if (*polled
!= s
->periods_done
) {
230 *polled
= s
->periods_done
;
231 mask
= EPOLLIN
| EPOLLOUT
| EPOLLWRNORM
;
236 mutex_unlock(&us122l
->mutex
);
241 static void us122l_stop(struct us122l
*us122l
)
245 list_for_each(p
, &us122l
->midi_list
)
246 snd_usbmidi_input_stop(p
);
248 usb_stream_stop(&us122l
->sk
);
249 usb_stream_free(&us122l
->sk
);
252 static int us122l_set_sample_rate(struct usb_device
*dev
, int rate
)
254 unsigned int ep
= 0x81;
255 unsigned char data
[3];
260 data
[2] = rate
>> 16;
261 err
= usb_control_msg_send(dev
, 0, UAC_SET_CUR
,
262 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
263 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3,
266 dev_err(&dev
->dev
, "%d: cannot set freq %d to ep 0x%x\n",
267 dev
->devnum
, rate
, ep
);
271 static bool us122l_start(struct us122l
*us122l
,
272 unsigned int rate
, unsigned int period_frames
)
276 unsigned int use_packsize
= 0;
277 bool success
= false;
279 if (us122l
->dev
->speed
== USB_SPEED_HIGH
) {
280 /* The us-122l's descriptor defaults to iso max_packsize 78,
281 which isn't needed for samplerates <= 48000.
282 Lets save some memory:
296 if (!usb_stream_new(&us122l
->sk
, us122l
->dev
, 1, 2,
297 rate
, use_packsize
, period_frames
, 6))
300 err
= us122l_set_sample_rate(us122l
->dev
, rate
);
303 dev_err(&us122l
->dev
->dev
, "us122l_set_sample_rate error\n");
306 err
= usb_stream_start(&us122l
->sk
);
309 dev_err(&us122l
->dev
->dev
, "%s error %i\n", __func__
, err
);
312 list_for_each(p
, &us122l
->midi_list
)
313 snd_usbmidi_input_start(p
);
319 static int usb_stream_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
320 unsigned int cmd
, unsigned long arg
)
322 struct usb_stream_config cfg
;
323 struct us122l
*us122l
= hw
->private_data
;
324 struct usb_stream
*s
;
325 unsigned int min_period_frames
;
329 if (cmd
!= SNDRV_USB_STREAM_IOCTL_SET_PARAMS
)
332 if (copy_from_user(&cfg
, (void __user
*)arg
, sizeof(cfg
)))
335 if (cfg
.version
!= USB_STREAM_INTERFACE_VERSION
)
338 high_speed
= us122l
->dev
->speed
== USB_SPEED_HIGH
;
339 if ((cfg
.sample_rate
!= 44100 && cfg
.sample_rate
!= 48000 &&
341 (cfg
.sample_rate
!= 88200 && cfg
.sample_rate
!= 96000))) ||
342 cfg
.frame_size
!= 6 ||
343 cfg
.period_frames
> 0x3000)
346 switch (cfg
.sample_rate
) {
348 min_period_frames
= 48;
351 min_period_frames
= 52;
354 min_period_frames
= 104;
358 min_period_frames
<<= 1;
359 if (cfg
.period_frames
< min_period_frames
)
362 snd_power_wait(hw
->card
);
364 mutex_lock(&us122l
->mutex
);
366 if (!us122l
->master
) {
367 us122l
->master
= file
;
368 } else if (us122l
->master
!= file
) {
369 if (!s
|| memcmp(&cfg
, &s
->cfg
, sizeof(cfg
))) {
373 us122l
->slave
= file
;
375 if (!s
|| memcmp(&cfg
, &s
->cfg
, sizeof(cfg
)) ||
376 s
->state
== usb_stream_xrun
) {
378 if (!us122l_start(us122l
, cfg
.sample_rate
, cfg
.period_frames
))
384 mutex_unlock(&us122l
->mutex
);
385 wake_up_all(&us122l
->sk
.sleep
);
389 #define SND_USB_STREAM_ID "USB STREAM"
390 static int usb_stream_hwdep_new(struct snd_card
*card
)
393 struct snd_hwdep
*hw
;
394 struct usb_device
*dev
= US122L(card
)->dev
;
396 err
= snd_hwdep_new(card
, SND_USB_STREAM_ID
, 0, &hw
);
400 hw
->iface
= SNDRV_HWDEP_IFACE_USB_STREAM
;
401 hw
->private_data
= US122L(card
);
402 hw
->ops
.open
= usb_stream_hwdep_open
;
403 hw
->ops
.release
= usb_stream_hwdep_release
;
404 hw
->ops
.ioctl
= usb_stream_hwdep_ioctl
;
405 hw
->ops
.ioctl_compat
= usb_stream_hwdep_ioctl
;
406 hw
->ops
.mmap
= usb_stream_hwdep_mmap
;
407 hw
->ops
.poll
= usb_stream_hwdep_poll
;
409 sprintf(hw
->name
, "/dev/bus/usb/%03d/%03d/hwdeppcm",
410 dev
->bus
->busnum
, dev
->devnum
);
414 static bool us122l_create_card(struct snd_card
*card
)
417 struct us122l
*us122l
= US122L(card
);
419 if (us122l
->is_us144
) {
420 err
= usb_set_interface(us122l
->dev
, 0, 1);
422 dev_err(card
->dev
, "usb_set_interface error\n");
426 err
= usb_set_interface(us122l
->dev
, 1, 1);
428 dev_err(card
->dev
, "usb_set_interface error\n");
432 pt_info_set(us122l
->dev
, 0x11);
433 pt_info_set(us122l
->dev
, 0x10);
435 if (!us122l_start(us122l
, 44100, 256))
438 if (us122l
->is_us144
)
439 err
= us144_create_usbmidi(card
);
441 err
= us122l_create_usbmidi(card
);
443 dev_err(card
->dev
, "us122l_create_usbmidi error %i\n", err
);
446 err
= usb_stream_hwdep_new(card
);
448 /* release the midi resources */
451 list_for_each(p
, &us122l
->midi_list
)
452 snd_usbmidi_disconnect(p
);
463 static void snd_us122l_free(struct snd_card
*card
)
465 struct us122l
*us122l
= US122L(card
);
466 int index
= us122l
->card_index
;
468 if (index
>= 0 && index
< SNDRV_CARDS
)
469 snd_us122l_card_used
[index
] = 0;
472 static int usx2y_create_card(struct usb_device
*device
,
473 struct usb_interface
*intf
,
474 struct snd_card
**cardp
,
478 struct snd_card
*card
;
481 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
482 if (enable
[dev
] && !snd_us122l_card_used
[dev
])
484 if (dev
>= SNDRV_CARDS
)
486 err
= snd_card_new(&intf
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
487 sizeof(struct us122l
), &card
);
490 snd_us122l_card_used
[US122L(card
)->card_index
= dev
] = 1;
491 card
->private_free
= snd_us122l_free
;
492 US122L(card
)->dev
= device
;
493 mutex_init(&US122L(card
)->mutex
);
494 US122L(card
)->sk
.dev
= device
;
495 init_waitqueue_head(&US122L(card
)->sk
.sleep
);
496 US122L(card
)->is_us144
= flags
& US122L_FLAG_US144
;
497 INIT_LIST_HEAD(&US122L(card
)->midi_list
);
498 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
499 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
500 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
502 le16_to_cpu(device
->descriptor
.idVendor
),
503 le16_to_cpu(device
->descriptor
.idProduct
),
505 US122L(card
)->dev
->bus
->busnum
,
506 US122L(card
)->dev
->devnum
512 static int us122l_usb_probe(struct usb_interface
*intf
,
513 const struct usb_device_id
*device_id
,
514 struct snd_card
**cardp
)
516 struct usb_device
*device
= interface_to_usbdev(intf
);
517 struct snd_card
*card
;
520 err
= usx2y_create_card(device
, intf
, &card
, device_id
->driver_info
);
524 if (!us122l_create_card(card
)) {
529 err
= snd_card_register(card
);
535 usb_get_intf(usb_ifnum_to_if(device
, 0));
541 static int snd_us122l_probe(struct usb_interface
*intf
,
542 const struct usb_device_id
*id
)
544 struct usb_device
*device
= interface_to_usbdev(intf
);
545 struct snd_card
*card
;
548 if (id
->driver_info
& US122L_FLAG_US144
&&
549 device
->speed
== USB_SPEED_HIGH
) {
550 dev_err(&device
->dev
, "disable ehci-hcd to run US-144\n");
554 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
557 err
= us122l_usb_probe(usb_get_intf(intf
), id
, &card
);
563 usb_set_intfdata(intf
, card
);
567 static void snd_us122l_disconnect(struct usb_interface
*intf
)
569 struct snd_card
*card
;
570 struct us122l
*us122l
;
573 card
= usb_get_intfdata(intf
);
577 snd_card_disconnect(card
);
579 us122l
= US122L(card
);
580 mutex_lock(&us122l
->mutex
);
582 mutex_unlock(&us122l
->mutex
);
584 /* release the midi resources */
585 list_for_each(p
, &us122l
->midi_list
) {
586 snd_usbmidi_disconnect(p
);
589 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 0));
590 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 1));
591 usb_put_dev(us122l
->dev
);
593 snd_card_free_when_closed(card
);
596 static int snd_us122l_suspend(struct usb_interface
*intf
, pm_message_t message
)
598 struct snd_card
*card
;
599 struct us122l
*us122l
;
602 card
= usb_get_intfdata(intf
);
605 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
607 us122l
= US122L(card
);
611 list_for_each(p
, &us122l
->midi_list
)
612 snd_usbmidi_input_stop(p
);
614 mutex_lock(&us122l
->mutex
);
615 usb_stream_stop(&us122l
->sk
);
616 mutex_unlock(&us122l
->mutex
);
621 static int snd_us122l_resume(struct usb_interface
*intf
)
623 struct snd_card
*card
;
624 struct us122l
*us122l
;
628 card
= usb_get_intfdata(intf
);
632 us122l
= US122L(card
);
636 mutex_lock(&us122l
->mutex
);
637 /* needed, doesn't restart without: */
638 if (us122l
->is_us144
) {
639 err
= usb_set_interface(us122l
->dev
, 0, 1);
641 dev_err(&us122l
->dev
->dev
, "usb_set_interface error\n");
645 err
= usb_set_interface(us122l
->dev
, 1, 1);
647 dev_err(&us122l
->dev
->dev
, "usb_set_interface error\n");
651 pt_info_set(us122l
->dev
, 0x11);
652 pt_info_set(us122l
->dev
, 0x10);
654 err
= us122l_set_sample_rate(us122l
->dev
,
655 us122l
->sk
.s
->cfg
.sample_rate
);
657 dev_err(&us122l
->dev
->dev
, "us122l_set_sample_rate error\n");
660 err
= usb_stream_start(&us122l
->sk
);
664 list_for_each(p
, &us122l
->midi_list
)
665 snd_usbmidi_input_start(p
);
667 mutex_unlock(&us122l
->mutex
);
668 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
672 static const struct usb_device_id snd_us122l_usb_id_table
[] = {
674 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
676 .idProduct
= USB_ID_US122L
678 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
679 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
681 .idProduct
= USB_ID_US144
,
682 .driver_info
= US122L_FLAG_US144
685 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
687 .idProduct
= USB_ID_US122MKII
690 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
692 .idProduct
= USB_ID_US144MKII
,
693 .driver_info
= US122L_FLAG_US144
697 MODULE_DEVICE_TABLE(usb
, snd_us122l_usb_id_table
);
699 static struct usb_driver snd_us122l_usb_driver
= {
700 .name
= "snd-usb-us122l",
701 .probe
= snd_us122l_probe
,
702 .disconnect
= snd_us122l_disconnect
,
703 .suspend
= snd_us122l_suspend
,
704 .resume
= snd_us122l_resume
,
705 .reset_resume
= snd_us122l_resume
,
706 .id_table
= snd_us122l_usb_id_table
,
707 .supports_autosuspend
= 1
710 module_usb_driver(snd_us122l_usb_driver
);