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
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 <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/module.h>
23 #include <sound/core.h>
24 #include <sound/hwdep.h>
25 #include <sound/pcm.h>
26 #include <sound/initval.h>
27 #define MODNAME "US122L"
28 #include "usb_stream.c"
29 #include "../usbaudio.h"
33 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
34 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS
" Version 0.5");
35 MODULE_LICENSE("GPL");
37 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
38 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
39 /* Enable this card */
40 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
42 module_param_array(index
, int, NULL
, 0444);
43 MODULE_PARM_DESC(index
, "Index value for "NAME_ALLCAPS
".");
44 module_param_array(id
, charp
, NULL
, 0444);
45 MODULE_PARM_DESC(id
, "ID string for "NAME_ALLCAPS
".");
46 module_param_array(enable
, bool, NULL
, 0444);
47 MODULE_PARM_DESC(enable
, "Enable "NAME_ALLCAPS
".");
49 /* driver_info flags */
50 #define US122L_FLAG_US144 BIT(0)
52 static int snd_us122l_card_used
[SNDRV_CARDS
];
54 static int us122l_create_usbmidi(struct snd_card
*card
)
56 static struct snd_usb_midi_endpoint_info quirk_data
= {
62 static struct snd_usb_audio_quirk quirk
= {
63 .vendor_name
= "US122L",
64 .product_name
= NAME_ALLCAPS
,
66 .type
= QUIRK_MIDI_US122L
,
69 struct usb_device
*dev
= US122L(card
)->dev
;
70 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 1);
72 return snd_usbmidi_create(card
, iface
,
73 &US122L(card
)->midi_list
, &quirk
);
76 static int us144_create_usbmidi(struct snd_card
*card
)
78 static struct snd_usb_midi_endpoint_info quirk_data
= {
84 static struct snd_usb_audio_quirk quirk
= {
85 .vendor_name
= "US144",
86 .product_name
= NAME_ALLCAPS
,
88 .type
= QUIRK_MIDI_US122L
,
91 struct usb_device
*dev
= US122L(card
)->dev
;
92 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 0);
94 return snd_usbmidi_create(card
, iface
,
95 &US122L(card
)->midi_list
, &quirk
);
99 * Wrapper for usb_control_msg().
100 * Allocates a temp buffer to prevent dmaing from/to the stack.
102 static int us122l_ctl_msg(struct usb_device
*dev
, unsigned int pipe
,
103 __u8 request
, __u8 requesttype
,
104 __u16 value
, __u16 index
, void *data
,
105 __u16 size
, int timeout
)
111 buf
= kmemdup(data
, size
, GFP_KERNEL
);
115 err
= usb_control_msg(dev
, pipe
, request
, requesttype
,
116 value
, index
, buf
, size
, timeout
);
118 memcpy(data
, buf
, size
);
124 static void pt_info_set(struct usb_device
*dev
, u8 v
)
128 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
130 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
131 v
, 0, NULL
, 0, 1000);
132 snd_printdd(KERN_DEBUG
"%i\n", ret
);
135 static void usb_stream_hwdep_vm_open(struct vm_area_struct
*area
)
137 struct us122l
*us122l
= area
->vm_private_data
;
138 atomic_inc(&us122l
->mmap_count
);
139 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
142 static int usb_stream_hwdep_vm_fault(struct vm_fault
*vmf
)
144 unsigned long offset
;
147 struct us122l
*us122l
= vmf
->vma
->vm_private_data
;
148 struct usb_stream
*s
;
150 mutex_lock(&us122l
->mutex
);
155 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
156 if (offset
< PAGE_ALIGN(s
->read_size
))
157 vaddr
= (char *)s
+ offset
;
159 offset
-= PAGE_ALIGN(s
->read_size
);
160 if (offset
>= PAGE_ALIGN(s
->write_size
))
163 vaddr
= us122l
->sk
.write_page
+ offset
;
165 page
= virt_to_page(vaddr
);
168 mutex_unlock(&us122l
->mutex
);
174 mutex_unlock(&us122l
->mutex
);
175 return VM_FAULT_SIGBUS
;
178 static void usb_stream_hwdep_vm_close(struct vm_area_struct
*area
)
180 struct us122l
*us122l
= area
->vm_private_data
;
181 atomic_dec(&us122l
->mmap_count
);
182 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
185 static const struct vm_operations_struct usb_stream_hwdep_vm_ops
= {
186 .open
= usb_stream_hwdep_vm_open
,
187 .fault
= usb_stream_hwdep_vm_fault
,
188 .close
= usb_stream_hwdep_vm_close
,
192 static int usb_stream_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
194 struct us122l
*us122l
= hw
->private_data
;
195 struct usb_interface
*iface
;
196 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
201 us122l
->first
= file
;
203 if (us122l
->is_us144
) {
204 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
205 usb_autopm_get_interface(iface
);
207 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
208 usb_autopm_get_interface(iface
);
212 static int usb_stream_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
214 struct us122l
*us122l
= hw
->private_data
;
215 struct usb_interface
*iface
;
216 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
218 if (us122l
->is_us144
) {
219 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
220 usb_autopm_put_interface(iface
);
222 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
223 usb_autopm_put_interface(iface
);
224 if (us122l
->first
== file
)
225 us122l
->first
= NULL
;
226 mutex_lock(&us122l
->mutex
);
227 if (us122l
->master
== file
)
228 us122l
->master
= us122l
->slave
;
230 us122l
->slave
= NULL
;
231 mutex_unlock(&us122l
->mutex
);
235 static int usb_stream_hwdep_mmap(struct snd_hwdep
*hw
,
236 struct file
*filp
, struct vm_area_struct
*area
)
238 unsigned long size
= area
->vm_end
- area
->vm_start
;
239 struct us122l
*us122l
= hw
->private_data
;
240 unsigned long offset
;
241 struct usb_stream
*s
;
245 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
246 mutex_lock(&us122l
->mutex
);
248 read
= offset
< s
->read_size
;
249 if (read
&& area
->vm_flags
& VM_WRITE
) {
253 snd_printdd(KERN_DEBUG
"%lu %u\n", size
,
254 read
? s
->read_size
: s
->write_size
);
255 /* if userspace tries to mmap beyond end of our buffer, fail */
256 if (size
> PAGE_ALIGN(read
? s
->read_size
: s
->write_size
)) {
257 snd_printk(KERN_WARNING
"%lu > %u\n", size
,
258 read
? s
->read_size
: s
->write_size
);
263 area
->vm_ops
= &usb_stream_hwdep_vm_ops
;
264 area
->vm_flags
|= VM_DONTDUMP
;
266 area
->vm_flags
|= VM_DONTEXPAND
;
267 area
->vm_private_data
= us122l
;
268 atomic_inc(&us122l
->mmap_count
);
270 mutex_unlock(&us122l
->mutex
);
274 static unsigned int usb_stream_hwdep_poll(struct snd_hwdep
*hw
,
275 struct file
*file
, poll_table
*wait
)
277 struct us122l
*us122l
= hw
->private_data
;
281 poll_wait(file
, &us122l
->sk
.sleep
, wait
);
283 mask
= POLLIN
| POLLOUT
| POLLWRNORM
| POLLERR
;
284 if (mutex_trylock(&us122l
->mutex
)) {
285 struct usb_stream
*s
= us122l
->sk
.s
;
286 if (s
&& s
->state
== usb_stream_ready
) {
287 if (us122l
->first
== file
)
288 polled
= &s
->periods_polled
;
290 polled
= &us122l
->second_periods_polled
;
291 if (*polled
!= s
->periods_done
) {
292 *polled
= s
->periods_done
;
293 mask
= POLLIN
| POLLOUT
| POLLWRNORM
;
297 mutex_unlock(&us122l
->mutex
);
302 static void us122l_stop(struct us122l
*us122l
)
305 list_for_each(p
, &us122l
->midi_list
)
306 snd_usbmidi_input_stop(p
);
308 usb_stream_stop(&us122l
->sk
);
309 usb_stream_free(&us122l
->sk
);
312 static int us122l_set_sample_rate(struct usb_device
*dev
, int rate
)
314 unsigned int ep
= 0x81;
315 unsigned char data
[3];
320 data
[2] = rate
>> 16;
321 err
= us122l_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
322 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
323 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3, 1000);
325 snd_printk(KERN_ERR
"%d: cannot set freq %d to ep 0x%x\n",
326 dev
->devnum
, rate
, ep
);
330 static bool us122l_start(struct us122l
*us122l
,
331 unsigned rate
, unsigned period_frames
)
335 unsigned use_packsize
= 0;
336 bool success
= false;
338 if (us122l
->dev
->speed
== USB_SPEED_HIGH
) {
339 /* The us-122l's descriptor defaults to iso max_packsize 78,
340 which isn't needed for samplerates <= 48000.
341 Lets save some memory:
355 if (!usb_stream_new(&us122l
->sk
, us122l
->dev
, 1, 2,
356 rate
, use_packsize
, period_frames
, 6))
359 err
= us122l_set_sample_rate(us122l
->dev
, rate
);
362 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
365 err
= usb_stream_start(&us122l
->sk
);
368 snd_printk(KERN_ERR
"us122l_start error %i \n", err
);
371 list_for_each(p
, &us122l
->midi_list
)
372 snd_usbmidi_input_start(p
);
378 static int usb_stream_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
379 unsigned cmd
, unsigned long arg
)
381 struct usb_stream_config
*cfg
;
382 struct us122l
*us122l
= hw
->private_data
;
383 struct usb_stream
*s
;
384 unsigned min_period_frames
;
388 if (cmd
!= SNDRV_USB_STREAM_IOCTL_SET_PARAMS
)
391 cfg
= memdup_user((void *)arg
, sizeof(*cfg
));
395 if (cfg
->version
!= USB_STREAM_INTERFACE_VERSION
) {
399 high_speed
= us122l
->dev
->speed
== USB_SPEED_HIGH
;
400 if ((cfg
->sample_rate
!= 44100 && cfg
->sample_rate
!= 48000 &&
402 (cfg
->sample_rate
!= 88200 && cfg
->sample_rate
!= 96000))) ||
403 cfg
->frame_size
!= 6 ||
404 cfg
->period_frames
> 0x3000) {
408 switch (cfg
->sample_rate
) {
410 min_period_frames
= 48;
413 min_period_frames
= 52;
416 min_period_frames
= 104;
420 min_period_frames
<<= 1;
421 if (cfg
->period_frames
< min_period_frames
) {
426 snd_power_wait(hw
->card
, SNDRV_CTL_POWER_D0
);
428 mutex_lock(&us122l
->mutex
);
431 us122l
->master
= file
;
432 else if (us122l
->master
!= file
) {
433 if (!s
|| memcmp(cfg
, &s
->cfg
, sizeof(*cfg
))) {
437 us122l
->slave
= file
;
439 if (!s
|| memcmp(cfg
, &s
->cfg
, sizeof(*cfg
)) ||
440 s
->state
== usb_stream_xrun
) {
442 if (!us122l_start(us122l
, cfg
->sample_rate
, cfg
->period_frames
))
448 mutex_unlock(&us122l
->mutex
);
451 wake_up_all(&us122l
->sk
.sleep
);
455 #define SND_USB_STREAM_ID "USB STREAM"
456 static int usb_stream_hwdep_new(struct snd_card
*card
)
459 struct snd_hwdep
*hw
;
460 struct usb_device
*dev
= US122L(card
)->dev
;
462 err
= snd_hwdep_new(card
, SND_USB_STREAM_ID
, 0, &hw
);
466 hw
->iface
= SNDRV_HWDEP_IFACE_USB_STREAM
;
467 hw
->private_data
= US122L(card
);
468 hw
->ops
.open
= usb_stream_hwdep_open
;
469 hw
->ops
.release
= usb_stream_hwdep_release
;
470 hw
->ops
.ioctl
= usb_stream_hwdep_ioctl
;
471 hw
->ops
.ioctl_compat
= usb_stream_hwdep_ioctl
;
472 hw
->ops
.mmap
= usb_stream_hwdep_mmap
;
473 hw
->ops
.poll
= usb_stream_hwdep_poll
;
475 sprintf(hw
->name
, "/dev/bus/usb/%03d/%03d/hwdeppcm",
476 dev
->bus
->busnum
, dev
->devnum
);
481 static bool us122l_create_card(struct snd_card
*card
)
484 struct us122l
*us122l
= US122L(card
);
486 if (us122l
->is_us144
) {
487 err
= usb_set_interface(us122l
->dev
, 0, 1);
489 snd_printk(KERN_ERR
"usb_set_interface error \n");
493 err
= usb_set_interface(us122l
->dev
, 1, 1);
495 snd_printk(KERN_ERR
"usb_set_interface error \n");
499 pt_info_set(us122l
->dev
, 0x11);
500 pt_info_set(us122l
->dev
, 0x10);
502 if (!us122l_start(us122l
, 44100, 256))
505 if (us122l
->is_us144
)
506 err
= us144_create_usbmidi(card
);
508 err
= us122l_create_usbmidi(card
);
510 snd_printk(KERN_ERR
"us122l_create_usbmidi error %i \n", err
);
513 err
= usb_stream_hwdep_new(card
);
515 /* release the midi resources */
517 list_for_each(p
, &us122l
->midi_list
)
518 snd_usbmidi_disconnect(p
);
529 static void snd_us122l_free(struct snd_card
*card
)
531 struct us122l
*us122l
= US122L(card
);
532 int index
= us122l
->card_index
;
533 if (index
>= 0 && index
< SNDRV_CARDS
)
534 snd_us122l_card_used
[index
] = 0;
537 static int usx2y_create_card(struct usb_device
*device
,
538 struct usb_interface
*intf
,
539 struct snd_card
**cardp
,
543 struct snd_card
*card
;
546 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
547 if (enable
[dev
] && !snd_us122l_card_used
[dev
])
549 if (dev
>= SNDRV_CARDS
)
551 err
= snd_card_new(&intf
->dev
, index
[dev
], id
[dev
], THIS_MODULE
,
552 sizeof(struct us122l
), &card
);
555 snd_us122l_card_used
[US122L(card
)->card_index
= dev
] = 1;
556 card
->private_free
= snd_us122l_free
;
557 US122L(card
)->dev
= device
;
558 mutex_init(&US122L(card
)->mutex
);
559 init_waitqueue_head(&US122L(card
)->sk
.sleep
);
560 US122L(card
)->is_us144
= flags
& US122L_FLAG_US144
;
561 INIT_LIST_HEAD(&US122L(card
)->midi_list
);
562 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
563 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
564 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
566 le16_to_cpu(device
->descriptor
.idVendor
),
567 le16_to_cpu(device
->descriptor
.idProduct
),
569 US122L(card
)->dev
->bus
->busnum
,
570 US122L(card
)->dev
->devnum
576 static int us122l_usb_probe(struct usb_interface
*intf
,
577 const struct usb_device_id
*device_id
,
578 struct snd_card
**cardp
)
580 struct usb_device
*device
= interface_to_usbdev(intf
);
581 struct snd_card
*card
;
584 err
= usx2y_create_card(device
, intf
, &card
, device_id
->driver_info
);
588 if (!us122l_create_card(card
)) {
593 err
= snd_card_register(card
);
599 usb_get_intf(usb_ifnum_to_if(device
, 0));
605 static int snd_us122l_probe(struct usb_interface
*intf
,
606 const struct usb_device_id
*id
)
608 struct usb_device
*device
= interface_to_usbdev(intf
);
609 struct snd_card
*card
;
612 if (id
->driver_info
& US122L_FLAG_US144
&&
613 device
->speed
== USB_SPEED_HIGH
) {
614 snd_printk(KERN_ERR
"disable ehci-hcd to run US-144 \n");
618 snd_printdd(KERN_DEBUG
"%p:%i\n",
619 intf
, intf
->cur_altsetting
->desc
.bInterfaceNumber
);
620 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
623 err
= us122l_usb_probe(usb_get_intf(intf
), id
, &card
);
629 usb_set_intfdata(intf
, card
);
633 static void snd_us122l_disconnect(struct usb_interface
*intf
)
635 struct snd_card
*card
;
636 struct us122l
*us122l
;
639 card
= usb_get_intfdata(intf
);
643 snd_card_disconnect(card
);
645 us122l
= US122L(card
);
646 mutex_lock(&us122l
->mutex
);
648 mutex_unlock(&us122l
->mutex
);
650 /* release the midi resources */
651 list_for_each(p
, &us122l
->midi_list
) {
652 snd_usbmidi_disconnect(p
);
655 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 0));
656 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 1));
657 usb_put_dev(us122l
->dev
);
659 while (atomic_read(&us122l
->mmap_count
))
665 static int snd_us122l_suspend(struct usb_interface
*intf
, pm_message_t message
)
667 struct snd_card
*card
;
668 struct us122l
*us122l
;
671 card
= usb_get_intfdata(intf
);
674 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
676 us122l
= US122L(card
);
680 list_for_each(p
, &us122l
->midi_list
)
681 snd_usbmidi_input_stop(p
);
683 mutex_lock(&us122l
->mutex
);
684 usb_stream_stop(&us122l
->sk
);
685 mutex_unlock(&us122l
->mutex
);
690 static int snd_us122l_resume(struct usb_interface
*intf
)
692 struct snd_card
*card
;
693 struct us122l
*us122l
;
697 card
= usb_get_intfdata(intf
);
701 us122l
= US122L(card
);
705 mutex_lock(&us122l
->mutex
);
706 /* needed, doesn't restart without: */
707 if (us122l
->is_us144
) {
708 err
= usb_set_interface(us122l
->dev
, 0, 1);
710 snd_printk(KERN_ERR
"usb_set_interface error \n");
714 err
= usb_set_interface(us122l
->dev
, 1, 1);
716 snd_printk(KERN_ERR
"usb_set_interface error \n");
720 pt_info_set(us122l
->dev
, 0x11);
721 pt_info_set(us122l
->dev
, 0x10);
723 err
= us122l_set_sample_rate(us122l
->dev
,
724 us122l
->sk
.s
->cfg
.sample_rate
);
726 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
729 err
= usb_stream_start(&us122l
->sk
);
733 list_for_each(p
, &us122l
->midi_list
)
734 snd_usbmidi_input_start(p
);
736 mutex_unlock(&us122l
->mutex
);
737 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
741 static const struct usb_device_id snd_us122l_usb_id_table
[] = {
743 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
745 .idProduct
= USB_ID_US122L
747 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
748 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
750 .idProduct
= USB_ID_US144
,
751 .driver_info
= US122L_FLAG_US144
754 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
756 .idProduct
= USB_ID_US122MKII
759 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
761 .idProduct
= USB_ID_US144MKII
,
762 .driver_info
= US122L_FLAG_US144
767 MODULE_DEVICE_TABLE(usb
, snd_us122l_usb_id_table
);
768 static struct usb_driver snd_us122l_usb_driver
= {
769 .name
= "snd-usb-us122l",
770 .probe
= snd_us122l_probe
,
771 .disconnect
= snd_us122l_disconnect
,
772 .suspend
= snd_us122l_suspend
,
773 .resume
= snd_us122l_resume
,
774 .reset_resume
= snd_us122l_resume
,
775 .id_table
= snd_us122l_usb_id_table
,
776 .supports_autosuspend
= 1
779 module_usb_driver(snd_us122l_usb_driver
);