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 <sound/core.h>
20 #include <sound/hwdep.h>
21 #include <sound/pcm.h>
22 #include <sound/initval.h>
23 #define MODNAME "US122L"
24 #include "usb_stream.c"
25 #include "../usbaudio.h"
28 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
29 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS
" Version 0.5");
30 MODULE_LICENSE("GPL");
32 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
33 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
34 /* Enable this card */
35 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
37 module_param_array(index
, int, NULL
, 0444);
38 MODULE_PARM_DESC(index
, "Index value for "NAME_ALLCAPS
".");
39 module_param_array(id
, charp
, NULL
, 0444);
40 MODULE_PARM_DESC(id
, "ID string for "NAME_ALLCAPS
".");
41 module_param_array(enable
, bool, NULL
, 0444);
42 MODULE_PARM_DESC(enable
, "Enable "NAME_ALLCAPS
".");
44 static int snd_us122l_card_used
[SNDRV_CARDS
];
47 static int us122l_create_usbmidi(struct snd_card
*card
)
49 static struct snd_usb_midi_endpoint_info quirk_data
= {
55 static struct snd_usb_audio_quirk quirk
= {
56 .vendor_name
= "US122L",
57 .product_name
= NAME_ALLCAPS
,
59 .type
= QUIRK_MIDI_US122L
,
62 struct usb_device
*dev
= US122L(card
)->chip
.dev
;
63 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 1);
65 return snd_usb_create_midi_interface(&US122L(card
)->chip
,
69 static int us144_create_usbmidi(struct snd_card
*card
)
71 static struct snd_usb_midi_endpoint_info quirk_data
= {
77 static struct snd_usb_audio_quirk quirk
= {
78 .vendor_name
= "US144",
79 .product_name
= NAME_ALLCAPS
,
81 .type
= QUIRK_MIDI_US122L
,
84 struct usb_device
*dev
= US122L(card
)->chip
.dev
;
85 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 0);
87 return snd_usb_create_midi_interface(&US122L(card
)->chip
,
92 * Wrapper for usb_control_msg().
93 * Allocates a temp buffer to prevent dmaing from/to the stack.
95 static int us122l_ctl_msg(struct usb_device
*dev
, unsigned int pipe
,
96 __u8 request
, __u8 requesttype
,
97 __u16 value
, __u16 index
, void *data
,
98 __u16 size
, int timeout
)
104 buf
= kmemdup(data
, size
, GFP_KERNEL
);
108 err
= usb_control_msg(dev
, pipe
, request
, requesttype
,
109 value
, index
, buf
, size
, timeout
);
111 memcpy(data
, buf
, size
);
117 static void pt_info_set(struct usb_device
*dev
, u8 v
)
121 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
123 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
124 v
, 0, NULL
, 0, 1000);
125 snd_printdd(KERN_DEBUG
"%i\n", ret
);
128 static void usb_stream_hwdep_vm_open(struct vm_area_struct
*area
)
130 struct us122l
*us122l
= area
->vm_private_data
;
131 atomic_inc(&us122l
->mmap_count
);
132 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
135 static int usb_stream_hwdep_vm_fault(struct vm_area_struct
*area
,
136 struct vm_fault
*vmf
)
138 unsigned long offset
;
141 struct us122l
*us122l
= area
->vm_private_data
;
142 struct usb_stream
*s
;
144 mutex_lock(&us122l
->mutex
);
149 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
150 if (offset
< PAGE_ALIGN(s
->read_size
))
151 vaddr
= (char *)s
+ offset
;
153 offset
-= PAGE_ALIGN(s
->read_size
);
154 if (offset
>= PAGE_ALIGN(s
->write_size
))
157 vaddr
= us122l
->sk
.write_page
+ offset
;
159 page
= virt_to_page(vaddr
);
162 mutex_unlock(&us122l
->mutex
);
168 mutex_unlock(&us122l
->mutex
);
169 return VM_FAULT_SIGBUS
;
172 static void usb_stream_hwdep_vm_close(struct vm_area_struct
*area
)
174 struct us122l
*us122l
= area
->vm_private_data
;
175 atomic_dec(&us122l
->mmap_count
);
176 snd_printdd(KERN_DEBUG
"%i\n", atomic_read(&us122l
->mmap_count
));
179 static const struct vm_operations_struct usb_stream_hwdep_vm_ops
= {
180 .open
= usb_stream_hwdep_vm_open
,
181 .fault
= usb_stream_hwdep_vm_fault
,
182 .close
= usb_stream_hwdep_vm_close
,
186 static int usb_stream_hwdep_open(struct snd_hwdep
*hw
, struct file
*file
)
188 struct us122l
*us122l
= hw
->private_data
;
189 struct usb_interface
*iface
;
190 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
195 us122l
->first
= file
;
197 if (us122l
->chip
.dev
->descriptor
.idProduct
== USB_ID_US144
) {
198 iface
= usb_ifnum_to_if(us122l
->chip
.dev
, 0);
199 usb_autopm_get_interface(iface
);
201 iface
= usb_ifnum_to_if(us122l
->chip
.dev
, 1);
202 usb_autopm_get_interface(iface
);
206 static int usb_stream_hwdep_release(struct snd_hwdep
*hw
, struct file
*file
)
208 struct us122l
*us122l
= hw
->private_data
;
209 struct usb_interface
*iface
;
210 snd_printdd(KERN_DEBUG
"%p %p\n", hw
, file
);
212 if (us122l
->chip
.dev
->descriptor
.idProduct
== USB_ID_US144
) {
213 iface
= usb_ifnum_to_if(us122l
->chip
.dev
, 0);
214 usb_autopm_put_interface(iface
);
216 iface
= usb_ifnum_to_if(us122l
->chip
.dev
, 1);
217 usb_autopm_put_interface(iface
);
218 if (us122l
->first
== file
)
219 us122l
->first
= NULL
;
220 mutex_lock(&us122l
->mutex
);
221 if (us122l
->master
== file
)
222 us122l
->master
= us122l
->slave
;
224 us122l
->slave
= NULL
;
225 mutex_unlock(&us122l
->mutex
);
229 static int usb_stream_hwdep_mmap(struct snd_hwdep
*hw
,
230 struct file
*filp
, struct vm_area_struct
*area
)
232 unsigned long size
= area
->vm_end
- area
->vm_start
;
233 struct us122l
*us122l
= hw
->private_data
;
234 unsigned long offset
;
235 struct usb_stream
*s
;
239 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
240 mutex_lock(&us122l
->mutex
);
242 read
= offset
< s
->read_size
;
243 if (read
&& area
->vm_flags
& VM_WRITE
) {
247 snd_printdd(KERN_DEBUG
"%lu %u\n", size
,
248 read
? s
->read_size
: s
->write_size
);
249 /* if userspace tries to mmap beyond end of our buffer, fail */
250 if (size
> PAGE_ALIGN(read
? s
->read_size
: s
->write_size
)) {
251 snd_printk(KERN_WARNING
"%lu > %u\n", size
,
252 read
? s
->read_size
: s
->write_size
);
257 area
->vm_ops
= &usb_stream_hwdep_vm_ops
;
258 area
->vm_flags
|= VM_RESERVED
;
259 area
->vm_private_data
= us122l
;
260 atomic_inc(&us122l
->mmap_count
);
262 mutex_unlock(&us122l
->mutex
);
266 static unsigned int usb_stream_hwdep_poll(struct snd_hwdep
*hw
,
267 struct file
*file
, poll_table
*wait
)
269 struct us122l
*us122l
= hw
->private_data
;
270 struct usb_stream
*s
= us122l
->sk
.s
;
274 poll_wait(file
, &us122l
->sk
.sleep
, wait
);
277 case usb_stream_ready
:
278 if (us122l
->first
== file
)
279 polled
= &s
->periods_polled
;
281 polled
= &us122l
->second_periods_polled
;
282 if (*polled
!= s
->periods_done
) {
283 *polled
= s
->periods_done
;
284 mask
= POLLIN
| POLLOUT
| POLLWRNORM
;
291 mask
= POLLIN
| POLLOUT
| POLLWRNORM
| POLLERR
;
297 static void us122l_stop(struct us122l
*us122l
)
300 list_for_each(p
, &us122l
->chip
.midi_list
)
301 snd_usbmidi_input_stop(p
);
303 usb_stream_stop(&us122l
->sk
);
304 usb_stream_free(&us122l
->sk
);
307 static int us122l_set_sample_rate(struct usb_device
*dev
, int rate
)
309 unsigned int ep
= 0x81;
310 unsigned char data
[3];
315 data
[2] = rate
>> 16;
316 err
= us122l_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), SET_CUR
,
317 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
318 SAMPLING_FREQ_CONTROL
<< 8, ep
, data
, 3, 1000);
320 snd_printk(KERN_ERR
"%d: cannot set freq %d to ep 0x%x\n",
321 dev
->devnum
, rate
, ep
);
325 static bool us122l_start(struct us122l
*us122l
,
326 unsigned rate
, unsigned period_frames
)
330 unsigned use_packsize
= 0;
331 bool success
= false;
333 if (us122l
->chip
.dev
->speed
== USB_SPEED_HIGH
) {
334 /* The us-122l's descriptor defaults to iso max_packsize 78,
335 which isn't needed for samplerates <= 48000.
336 Lets save some memory:
350 if (!usb_stream_new(&us122l
->sk
, us122l
->chip
.dev
, 1, 2,
351 rate
, use_packsize
, period_frames
, 6))
354 err
= us122l_set_sample_rate(us122l
->chip
.dev
, rate
);
357 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
360 err
= usb_stream_start(&us122l
->sk
);
363 snd_printk(KERN_ERR
"us122l_start error %i \n", err
);
366 list_for_each(p
, &us122l
->chip
.midi_list
)
367 snd_usbmidi_input_start(p
);
373 static int usb_stream_hwdep_ioctl(struct snd_hwdep
*hw
, struct file
*file
,
374 unsigned cmd
, unsigned long arg
)
376 struct usb_stream_config
*cfg
;
377 struct us122l
*us122l
= hw
->private_data
;
378 unsigned min_period_frames
;
382 if (cmd
!= SNDRV_USB_STREAM_IOCTL_SET_PARAMS
)
385 cfg
= memdup_user((void *)arg
, sizeof(*cfg
));
389 if (cfg
->version
!= USB_STREAM_INTERFACE_VERSION
) {
393 high_speed
= us122l
->chip
.dev
->speed
== USB_SPEED_HIGH
;
394 if ((cfg
->sample_rate
!= 44100 && cfg
->sample_rate
!= 48000 &&
396 (cfg
->sample_rate
!= 88200 && cfg
->sample_rate
!= 96000))) ||
397 cfg
->frame_size
!= 6 ||
398 cfg
->period_frames
> 0x3000) {
402 switch (cfg
->sample_rate
) {
404 min_period_frames
= 48;
407 min_period_frames
= 52;
410 min_period_frames
= 104;
414 min_period_frames
<<= 1;
415 if (cfg
->period_frames
< min_period_frames
) {
420 snd_power_wait(hw
->card
, SNDRV_CTL_POWER_D0
);
422 mutex_lock(&us122l
->mutex
);
424 us122l
->master
= file
;
425 else if (us122l
->master
!= file
) {
426 if (memcmp(cfg
, &us122l
->sk
.s
->cfg
, sizeof(*cfg
))) {
430 us122l
->slave
= file
;
433 memcmp(cfg
, &us122l
->sk
.s
->cfg
, sizeof(*cfg
)) ||
434 us122l
->sk
.s
->state
== usb_stream_xrun
) {
436 if (!us122l_start(us122l
, cfg
->sample_rate
, cfg
->period_frames
))
442 mutex_unlock(&us122l
->mutex
);
448 #define SND_USB_STREAM_ID "USB STREAM"
449 static int usb_stream_hwdep_new(struct snd_card
*card
)
452 struct snd_hwdep
*hw
;
453 struct usb_device
*dev
= US122L(card
)->chip
.dev
;
455 err
= snd_hwdep_new(card
, SND_USB_STREAM_ID
, 0, &hw
);
459 hw
->iface
= SNDRV_HWDEP_IFACE_USB_STREAM
;
460 hw
->private_data
= US122L(card
);
461 hw
->ops
.open
= usb_stream_hwdep_open
;
462 hw
->ops
.release
= usb_stream_hwdep_release
;
463 hw
->ops
.ioctl
= usb_stream_hwdep_ioctl
;
464 hw
->ops
.ioctl_compat
= usb_stream_hwdep_ioctl
;
465 hw
->ops
.mmap
= usb_stream_hwdep_mmap
;
466 hw
->ops
.poll
= usb_stream_hwdep_poll
;
468 sprintf(hw
->name
, "/proc/bus/usb/%03d/%03d/hwdeppcm",
469 dev
->bus
->busnum
, dev
->devnum
);
474 static bool us122l_create_card(struct snd_card
*card
)
477 struct us122l
*us122l
= US122L(card
);
479 if (us122l
->chip
.dev
->descriptor
.idProduct
== USB_ID_US144
) {
480 err
= usb_set_interface(us122l
->chip
.dev
, 0, 1);
482 snd_printk(KERN_ERR
"usb_set_interface error \n");
486 err
= usb_set_interface(us122l
->chip
.dev
, 1, 1);
488 snd_printk(KERN_ERR
"usb_set_interface error \n");
492 pt_info_set(us122l
->chip
.dev
, 0x11);
493 pt_info_set(us122l
->chip
.dev
, 0x10);
495 if (!us122l_start(us122l
, 44100, 256))
498 if (us122l
->chip
.dev
->descriptor
.idProduct
== USB_ID_US144
)
499 err
= us144_create_usbmidi(card
);
501 err
= us122l_create_usbmidi(card
);
503 snd_printk(KERN_ERR
"us122l_create_usbmidi error %i \n", err
);
507 err
= usb_stream_hwdep_new(card
);
509 /* release the midi resources */
511 list_for_each(p
, &us122l
->chip
.midi_list
)
512 snd_usbmidi_disconnect(p
);
520 static void snd_us122l_free(struct snd_card
*card
)
522 struct us122l
*us122l
= US122L(card
);
523 int index
= us122l
->chip
.index
;
524 if (index
>= 0 && index
< SNDRV_CARDS
)
525 snd_us122l_card_used
[index
] = 0;
528 static int usx2y_create_card(struct usb_device
*device
, struct snd_card
**cardp
)
531 struct snd_card
*card
;
534 for (dev
= 0; dev
< SNDRV_CARDS
; ++dev
)
535 if (enable
[dev
] && !snd_us122l_card_used
[dev
])
537 if (dev
>= SNDRV_CARDS
)
539 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
,
540 sizeof(struct us122l
), &card
);
543 snd_us122l_card_used
[US122L(card
)->chip
.index
= dev
] = 1;
544 card
->private_free
= snd_us122l_free
;
545 US122L(card
)->chip
.dev
= device
;
546 US122L(card
)->chip
.card
= card
;
547 mutex_init(&US122L(card
)->mutex
);
548 init_waitqueue_head(&US122L(card
)->sk
.sleep
);
549 INIT_LIST_HEAD(&US122L(card
)->chip
.midi_list
);
550 strcpy(card
->driver
, "USB "NAME_ALLCAPS
"");
551 sprintf(card
->shortname
, "TASCAM "NAME_ALLCAPS
"");
552 sprintf(card
->longname
, "%s (%x:%x if %d at %03d/%03d)",
554 le16_to_cpu(device
->descriptor
.idVendor
),
555 le16_to_cpu(device
->descriptor
.idProduct
),
557 US122L(card
)->chip
.dev
->bus
->busnum
,
558 US122L(card
)->chip
.dev
->devnum
564 static int us122l_usb_probe(struct usb_interface
*intf
,
565 const struct usb_device_id
*device_id
,
566 struct snd_card
**cardp
)
568 struct usb_device
*device
= interface_to_usbdev(intf
);
569 struct snd_card
*card
;
572 err
= usx2y_create_card(device
, &card
);
576 snd_card_set_dev(card
, &intf
->dev
);
577 if (!us122l_create_card(card
)) {
582 err
= snd_card_register(card
);
588 usb_get_intf(usb_ifnum_to_if(device
, 0));
594 static int snd_us122l_probe(struct usb_interface
*intf
,
595 const struct usb_device_id
*id
)
597 struct usb_device
*device
= interface_to_usbdev(intf
);
598 struct snd_card
*card
;
601 if (device
->descriptor
.idProduct
== USB_ID_US144
602 && device
->speed
== USB_SPEED_HIGH
) {
603 snd_printk(KERN_ERR
"disable ehci-hcd to run US-144 \n");
607 snd_printdd(KERN_DEBUG
"%p:%i\n",
608 intf
, intf
->cur_altsetting
->desc
.bInterfaceNumber
);
609 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 1)
612 err
= us122l_usb_probe(usb_get_intf(intf
), id
, &card
);
618 usb_set_intfdata(intf
, card
);
622 static void snd_us122l_disconnect(struct usb_interface
*intf
)
624 struct snd_card
*card
;
625 struct us122l
*us122l
;
628 card
= usb_get_intfdata(intf
);
632 snd_card_disconnect(card
);
634 us122l
= US122L(card
);
635 mutex_lock(&us122l
->mutex
);
637 mutex_unlock(&us122l
->mutex
);
638 us122l
->chip
.shutdown
= 1;
640 /* release the midi resources */
641 list_for_each(p
, &us122l
->chip
.midi_list
) {
642 snd_usbmidi_disconnect(p
);
645 usb_put_intf(usb_ifnum_to_if(us122l
->chip
.dev
, 0));
646 usb_put_intf(usb_ifnum_to_if(us122l
->chip
.dev
, 1));
647 usb_put_dev(us122l
->chip
.dev
);
649 while (atomic_read(&us122l
->mmap_count
))
655 static int snd_us122l_suspend(struct usb_interface
*intf
, pm_message_t message
)
657 struct snd_card
*card
;
658 struct us122l
*us122l
;
661 card
= usb_get_intfdata(intf
);
664 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
666 us122l
= US122L(card
);
670 list_for_each(p
, &us122l
->chip
.midi_list
)
671 snd_usbmidi_input_stop(p
);
673 mutex_lock(&us122l
->mutex
);
674 usb_stream_stop(&us122l
->sk
);
675 mutex_unlock(&us122l
->mutex
);
680 static int snd_us122l_resume(struct usb_interface
*intf
)
682 struct snd_card
*card
;
683 struct us122l
*us122l
;
687 card
= usb_get_intfdata(intf
);
691 us122l
= US122L(card
);
695 mutex_lock(&us122l
->mutex
);
696 /* needed, doesn't restart without: */
697 if (us122l
->chip
.dev
->descriptor
.idProduct
== USB_ID_US144
) {
698 err
= usb_set_interface(us122l
->chip
.dev
, 0, 1);
700 snd_printk(KERN_ERR
"usb_set_interface error \n");
704 err
= usb_set_interface(us122l
->chip
.dev
, 1, 1);
706 snd_printk(KERN_ERR
"usb_set_interface error \n");
710 pt_info_set(us122l
->chip
.dev
, 0x11);
711 pt_info_set(us122l
->chip
.dev
, 0x10);
713 err
= us122l_set_sample_rate(us122l
->chip
.dev
,
714 us122l
->sk
.s
->cfg
.sample_rate
);
716 snd_printk(KERN_ERR
"us122l_set_sample_rate error \n");
719 err
= usb_stream_start(&us122l
->sk
);
723 list_for_each(p
, &us122l
->chip
.midi_list
)
724 snd_usbmidi_input_start(p
);
726 mutex_unlock(&us122l
->mutex
);
727 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
731 static struct usb_device_id snd_us122l_usb_id_table
[] = {
733 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
735 .idProduct
= USB_ID_US122L
737 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
738 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
740 .idProduct
= USB_ID_US144
745 MODULE_DEVICE_TABLE(usb
, snd_us122l_usb_id_table
);
746 static struct usb_driver snd_us122l_usb_driver
= {
747 .name
= "snd-usb-us122l",
748 .probe
= snd_us122l_probe
,
749 .disconnect
= snd_us122l_disconnect
,
750 .suspend
= snd_us122l_suspend
,
751 .resume
= snd_us122l_resume
,
752 .reset_resume
= snd_us122l_resume
,
753 .id_table
= snd_us122l_usb_id_table
,
754 .supports_autosuspend
= 1
758 static int __init
snd_us122l_module_init(void)
760 return usb_register(&snd_us122l_usb_driver
);
763 static void __exit
snd_us122l_module_exit(void)
765 usb_deregister(&snd_us122l_usb_driver
);
768 module_init(snd_us122l_module_init
)
769 module_exit(snd_us122l_module_exit
)