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 static int snd_us122l_card_used
[SNDRV_CARDS
];
52 static int us122l_create_usbmidi(struct snd_card
*card
)
54 static struct snd_usb_midi_endpoint_info quirk_data
= {
60 static struct snd_usb_audio_quirk quirk
= {
61 .vendor_name
= "US122L",
62 .product_name
= NAME_ALLCAPS
,
64 .type
= QUIRK_MIDI_US122L
,
67 struct usb_device
*dev
= US122L(card
)->dev
;
68 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 1);
70 return snd_usbmidi_create(card
, iface
,
71 &US122L(card
)->midi_list
, &quirk
);
74 static int us144_create_usbmidi(struct snd_card
*card
)
76 static struct snd_usb_midi_endpoint_info quirk_data
= {
82 static struct snd_usb_audio_quirk quirk
= {
83 .vendor_name
= "US144",
84 .product_name
= NAME_ALLCAPS
,
86 .type
= QUIRK_MIDI_US122L
,
89 struct usb_device
*dev
= US122L(card
)->dev
;
90 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 0);
92 return snd_usbmidi_create(card
, iface
,
93 &US122L(card
)->midi_list
, &quirk
);
97 * Wrapper for usb_control_msg().
98 * Allocates a temp buffer to prevent dmaing from/to the stack.
100 static int us122l_ctl_msg(struct usb_device
*dev
, unsigned int pipe
,
101 __u8 request
, __u8 requesttype
,
102 __u16 value
, __u16 index
, void *data
,
103 __u16 size
, int timeout
)
109 buf
= kmemdup(data
, size
, GFP_KERNEL
);
113 err
= usb_control_msg(dev
, pipe
, request
, requesttype
,
114 value
, index
, buf
, size
, timeout
);
116 memcpy(data
, buf
, size
);
122 static void pt_info_set(struct usb_device
*dev
, u8 v
)
126 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
128 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
129 v
, 0, NULL
, 0, 1000);
130 snd_printdd(KERN_DEBUG
"%i\n", ret
);
133 static void usb_stream_hwdep_vm_open(struct vm_area_struct
*area
)
135 struct us122l
*us122l
= area
->vm_private_data
;
136 atomic_inc(&us122l
->mmap_count
);
137 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
140 static int usb_stream_hwdep_vm_fault(struct vm_area_struct
*area
,
141 struct vm_fault
*vmf
)
143 unsigned long offset
;
146 struct us122l
*us122l
= area
->vm_private_data
;
147 struct usb_stream
*s
;
149 mutex_lock(&us122l
->mutex
);
154 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
155 if (offset
< PAGE_ALIGN(s
->read_size
))
156 vaddr
= (char *)s
+ offset
;
158 offset
-= PAGE_ALIGN(s
->read_size
);
159 if (offset
>= PAGE_ALIGN(s
->write_size
))
162 vaddr
= us122l
->sk
.write_page
+ offset
;
164 page
= virt_to_page(vaddr
);
167 mutex_unlock(&us122l
->mutex
);
173 mutex_unlock(&us122l
->mutex
);
174 return VM_FAULT_SIGBUS
;
177 static void usb_stream_hwdep_vm_close(struct vm_area_struct
*area
)
179 struct us122l
*us122l
= area
->vm_private_data
;
180 atomic_dec(&us122l
->mmap_count
);
181 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
184 static const struct vm_operations_struct usb_stream_hwdep_vm_ops
= {
185 .open
= usb_stream_hwdep_vm_open
,
186 .fault
= usb_stream_hwdep_vm_fault
,
187 .close
= usb_stream_hwdep_vm_close
,
191 static int usb_stream_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
193 struct us122l
*us122l
= hw
->private_data
;
194 struct usb_interface
*iface
;
195 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
200 us122l
->first
= file
;
202 if (us122l
->dev
->descriptor
.idProduct
== USB_ID_US144
||
203 us122l
->dev
->descriptor
.idProduct
== USB_ID_US144MKII
) {
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
->dev
->descriptor
.idProduct
== USB_ID_US144
||
219 us122l
->dev
->descriptor
.idProduct
== USB_ID_US144MKII
) {
220 iface
= usb_ifnum_to_if(us122l
->dev
, 0);
221 usb_autopm_put_interface(iface
);
223 iface
= usb_ifnum_to_if(us122l
->dev
, 1);
224 usb_autopm_put_interface(iface
);
225 if (us122l
->first
== file
)
226 us122l
->first
= NULL
;
227 mutex_lock(&us122l
->mutex
);
228 if (us122l
->master
== file
)
229 us122l
->master
= us122l
->slave
;
231 us122l
->slave
= NULL
;
232 mutex_unlock(&us122l
->mutex
);
236 static int usb_stream_hwdep_mmap(struct snd_hwdep
*hw
,
237 struct file
*filp
, struct vm_area_struct
*area
)
239 unsigned long size
= area
->vm_end
- area
->vm_start
;
240 struct us122l
*us122l
= hw
->private_data
;
241 unsigned long offset
;
242 struct usb_stream
*s
;
246 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
247 mutex_lock(&us122l
->mutex
);
249 read
= offset
< s
->read_size
;
250 if (read
&& area
->vm_flags
& VM_WRITE
) {
254 snd_printdd(KERN_DEBUG
"%lu %u\n", size
,
255 read
? s
->read_size
: s
->write_size
);
256 /* if userspace tries to mmap beyond end of our buffer, fail */
257 if (size
> PAGE_ALIGN(read
? s
->read_size
: s
->write_size
)) {
258 snd_printk(KERN_WARNING
"%lu > %u\n", size
,
259 read
? s
->read_size
: s
->write_size
);
264 area
->vm_ops
= &usb_stream_hwdep_vm_ops
;
265 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
266 area
->vm_private_data
= us122l
;
267 atomic_inc(&us122l
->mmap_count
);
269 mutex_unlock(&us122l
->mutex
);
273 static unsigned int usb_stream_hwdep_poll(struct snd_hwdep
*hw
,
274 struct file
*file
, poll_table
*wait
)
276 struct us122l
*us122l
= hw
->private_data
;
280 poll_wait(file
, &us122l
->sk
.sleep
, wait
);
282 mask
= POLLIN
| POLLOUT
| POLLWRNORM
| POLLERR
;
283 if (mutex_trylock(&us122l
->mutex
)) {
284 struct usb_stream
*s
= us122l
->sk
.s
;
285 if (s
&& s
->state
== usb_stream_ready
) {
286 if (us122l
->first
== file
)
287 polled
= &s
->periods_polled
;
289 polled
= &us122l
->second_periods_polled
;
290 if (*polled
!= s
->periods_done
) {
291 *polled
= s
->periods_done
;
292 mask
= POLLIN
| POLLOUT
| POLLWRNORM
;
296 mutex_unlock(&us122l
->mutex
);
301 static void us122l_stop(struct us122l
*us122l
)
304 list_for_each(p
, &us122l
->midi_list
)
305 snd_usbmidi_input_stop(p
);
307 usb_stream_stop(&us122l
->sk
);
308 usb_stream_free(&us122l
->sk
);
311 static int us122l_set_sample_rate(struct usb_device
*dev
, int rate
)
313 unsigned int ep
= 0x81;
314 unsigned char data
[3];
319 data
[2] = rate
>> 16;
320 err
= us122l_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
321 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
322 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3, 1000);
324 snd_printk(KERN_ERR
"%d: cannot set freq %d to ep 0x%x\n",
325 dev
->devnum
, rate
, ep
);
329 static bool us122l_start(struct us122l
*us122l
,
330 unsigned rate
, unsigned period_frames
)
334 unsigned use_packsize
= 0;
335 bool success
= false;
337 if (us122l
->dev
->speed
== USB_SPEED_HIGH
) {
338 /* The us-122l's descriptor defaults to iso max_packsize 78,
339 which isn't needed for samplerates <= 48000.
340 Lets save some memory:
354 if (!usb_stream_new(&us122l
->sk
, us122l
->dev
, 1, 2,
355 rate
, use_packsize
, period_frames
, 6))
358 err
= us122l_set_sample_rate(us122l
->dev
, rate
);
361 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
364 err
= usb_stream_start(&us122l
->sk
);
367 snd_printk(KERN_ERR
"us122l_start error %i \n", err
);
370 list_for_each(p
, &us122l
->midi_list
)
371 snd_usbmidi_input_start(p
);
377 static int usb_stream_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
378 unsigned cmd
, unsigned long arg
)
380 struct usb_stream_config
*cfg
;
381 struct us122l
*us122l
= hw
->private_data
;
382 struct usb_stream
*s
;
383 unsigned min_period_frames
;
387 if (cmd
!= SNDRV_USB_STREAM_IOCTL_SET_PARAMS
)
390 cfg
= memdup_user((void *)arg
, sizeof(*cfg
));
394 if (cfg
->version
!= USB_STREAM_INTERFACE_VERSION
) {
398 high_speed
= us122l
->dev
->speed
== USB_SPEED_HIGH
;
399 if ((cfg
->sample_rate
!= 44100 && cfg
->sample_rate
!= 48000 &&
401 (cfg
->sample_rate
!= 88200 && cfg
->sample_rate
!= 96000))) ||
402 cfg
->frame_size
!= 6 ||
403 cfg
->period_frames
> 0x3000) {
407 switch (cfg
->sample_rate
) {
409 min_period_frames
= 48;
412 min_period_frames
= 52;
415 min_period_frames
= 104;
419 min_period_frames
<<= 1;
420 if (cfg
->period_frames
< min_period_frames
) {
425 snd_power_wait(hw
->card
, SNDRV_CTL_POWER_D0
);
427 mutex_lock(&us122l
->mutex
);
430 us122l
->master
= file
;
431 else if (us122l
->master
!= file
) {
432 if (!s
|| memcmp(cfg
, &s
->cfg
, sizeof(*cfg
))) {
436 us122l
->slave
= file
;
438 if (!s
|| memcmp(cfg
, &s
->cfg
, sizeof(*cfg
)) ||
439 s
->state
== usb_stream_xrun
) {
441 if (!us122l_start(us122l
, cfg
->sample_rate
, cfg
->period_frames
))
447 mutex_unlock(&us122l
->mutex
);
450 wake_up_all(&us122l
->sk
.sleep
);
454 #define SND_USB_STREAM_ID "USB STREAM"
455 static int usb_stream_hwdep_new(struct snd_card
*card
)
458 struct snd_hwdep
*hw
;
459 struct usb_device
*dev
= US122L(card
)->dev
;
461 err
= snd_hwdep_new(card
, SND_USB_STREAM_ID
, 0, &hw
);
465 hw
->iface
= SNDRV_HWDEP_IFACE_USB_STREAM
;
466 hw
->private_data
= US122L(card
);
467 hw
->ops
.open
= usb_stream_hwdep_open
;
468 hw
->ops
.release
= usb_stream_hwdep_release
;
469 hw
->ops
.ioctl
= usb_stream_hwdep_ioctl
;
470 hw
->ops
.ioctl_compat
= usb_stream_hwdep_ioctl
;
471 hw
->ops
.mmap
= usb_stream_hwdep_mmap
;
472 hw
->ops
.poll
= usb_stream_hwdep_poll
;
474 sprintf(hw
->name
, "/proc/bus/usb/%03d/%03d/hwdeppcm",
475 dev
->bus
->busnum
, dev
->devnum
);
480 static bool us122l_create_card(struct snd_card
*card
)
483 struct us122l
*us122l
= US122L(card
);
485 if (us122l
->dev
->descriptor
.idProduct
== USB_ID_US144
||
486 us122l
->dev
->descriptor
.idProduct
== USB_ID_US144MKII
) {
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
->dev
->descriptor
.idProduct
== USB_ID_US144
||
506 us122l
->dev
->descriptor
.idProduct
== USB_ID_US144MKII
)
507 err
= us144_create_usbmidi(card
);
509 err
= us122l_create_usbmidi(card
);
511 snd_printk(KERN_ERR
"us122l_create_usbmidi error %i \n", err
);
515 err
= usb_stream_hwdep_new(card
);
517 /* release the midi resources */
519 list_for_each(p
, &us122l
->midi_list
)
520 snd_usbmidi_disconnect(p
);
528 static void snd_us122l_free(struct snd_card
*card
)
530 struct us122l
*us122l
= US122L(card
);
531 int index
= us122l
->card_index
;
532 if (index
>= 0 && index
< SNDRV_CARDS
)
533 snd_us122l_card_used
[index
] = 0;
536 static int usx2y_create_card(struct usb_device
*device
, struct snd_card
**cardp
)
539 struct snd_card
*card
;
542 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
543 if (enable
[dev
] && !snd_us122l_card_used
[dev
])
545 if (dev
>= SNDRV_CARDS
)
547 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
,
548 sizeof(struct us122l
), &card
);
551 snd_us122l_card_used
[US122L(card
)->card_index
= dev
] = 1;
552 card
->private_free
= snd_us122l_free
;
553 US122L(card
)->dev
= device
;
554 mutex_init(&US122L(card
)->mutex
);
555 init_waitqueue_head(&US122L(card
)->sk
.sleep
);
556 INIT_LIST_HEAD(&US122L(card
)->midi_list
);
557 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
558 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
559 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
561 le16_to_cpu(device
->descriptor
.idVendor
),
562 le16_to_cpu(device
->descriptor
.idProduct
),
564 US122L(card
)->dev
->bus
->busnum
,
565 US122L(card
)->dev
->devnum
571 static int us122l_usb_probe(struct usb_interface
*intf
,
572 const struct usb_device_id
*device_id
,
573 struct snd_card
**cardp
)
575 struct usb_device
*device
= interface_to_usbdev(intf
);
576 struct snd_card
*card
;
579 err
= usx2y_create_card(device
, &card
);
583 snd_card_set_dev(card
, &intf
->dev
);
584 if (!us122l_create_card(card
)) {
589 err
= snd_card_register(card
);
595 usb_get_intf(usb_ifnum_to_if(device
, 0));
601 static int snd_us122l_probe(struct usb_interface
*intf
,
602 const struct usb_device_id
*id
)
604 struct usb_device
*device
= interface_to_usbdev(intf
);
605 struct snd_card
*card
;
608 if ((device
->descriptor
.idProduct
== USB_ID_US144
||
609 device
->descriptor
.idProduct
== USB_ID_US144MKII
)
610 && device
->speed
== USB_SPEED_HIGH
) {
611 snd_printk(KERN_ERR
"disable ehci-hcd to run US-144 \n");
615 snd_printdd(KERN_DEBUG
"%p:%i\n",
616 intf
, intf
->cur_altsetting
->desc
.bInterfaceNumber
);
617 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
620 err
= us122l_usb_probe(usb_get_intf(intf
), id
, &card
);
626 usb_set_intfdata(intf
, card
);
630 static void snd_us122l_disconnect(struct usb_interface
*intf
)
632 struct snd_card
*card
;
633 struct us122l
*us122l
;
636 card
= usb_get_intfdata(intf
);
640 snd_card_disconnect(card
);
642 us122l
= US122L(card
);
643 mutex_lock(&us122l
->mutex
);
645 mutex_unlock(&us122l
->mutex
);
647 /* release the midi resources */
648 list_for_each(p
, &us122l
->midi_list
) {
649 snd_usbmidi_disconnect(p
);
652 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 0));
653 usb_put_intf(usb_ifnum_to_if(us122l
->dev
, 1));
654 usb_put_dev(us122l
->dev
);
656 while (atomic_read(&us122l
->mmap_count
))
662 static int snd_us122l_suspend(struct usb_interface
*intf
, pm_message_t message
)
664 struct snd_card
*card
;
665 struct us122l
*us122l
;
668 card
= usb_get_intfdata(intf
);
671 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
673 us122l
= US122L(card
);
677 list_for_each(p
, &us122l
->midi_list
)
678 snd_usbmidi_input_stop(p
);
680 mutex_lock(&us122l
->mutex
);
681 usb_stream_stop(&us122l
->sk
);
682 mutex_unlock(&us122l
->mutex
);
687 static int snd_us122l_resume(struct usb_interface
*intf
)
689 struct snd_card
*card
;
690 struct us122l
*us122l
;
694 card
= usb_get_intfdata(intf
);
698 us122l
= US122L(card
);
702 mutex_lock(&us122l
->mutex
);
703 /* needed, doesn't restart without: */
704 if (us122l
->dev
->descriptor
.idProduct
== USB_ID_US144
||
705 us122l
->dev
->descriptor
.idProduct
== USB_ID_US144MKII
) {
706 err
= usb_set_interface(us122l
->dev
, 0, 1);
708 snd_printk(KERN_ERR
"usb_set_interface error \n");
712 err
= usb_set_interface(us122l
->dev
, 1, 1);
714 snd_printk(KERN_ERR
"usb_set_interface error \n");
718 pt_info_set(us122l
->dev
, 0x11);
719 pt_info_set(us122l
->dev
, 0x10);
721 err
= us122l_set_sample_rate(us122l
->dev
,
722 us122l
->sk
.s
->cfg
.sample_rate
);
724 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
727 err
= usb_stream_start(&us122l
->sk
);
731 list_for_each(p
, &us122l
->midi_list
)
732 snd_usbmidi_input_start(p
);
734 mutex_unlock(&us122l
->mutex
);
735 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
739 static struct usb_device_id snd_us122l_usb_id_table
[] = {
741 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
743 .idProduct
= USB_ID_US122L
745 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
746 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
748 .idProduct
= USB_ID_US144
751 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
753 .idProduct
= USB_ID_US122MKII
756 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
758 .idProduct
= USB_ID_US144MKII
763 MODULE_DEVICE_TABLE(usb
, snd_us122l_usb_id_table
);
764 static struct usb_driver snd_us122l_usb_driver
= {
765 .name
= "snd-usb-us122l",
766 .probe
= snd_us122l_probe
,
767 .disconnect
= snd_us122l_disconnect
,
768 .suspend
= snd_us122l_suspend
,
769 .resume
= snd_us122l_resume
,
770 .reset_resume
= snd_us122l_resume
,
771 .id_table
= snd_us122l_usb_id_table
,
772 .supports_autosuspend
= 1
775 module_usb_driver(snd_us122l_usb_driver
);