2 * Edirol UA-101/UA-1000 driver
3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * This driver is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2.
8 * This driver is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this driver. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
28 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
29 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
30 MODULE_LICENSE("GPL v2");
31 MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}");
34 * Should not be lower than the minimum scheduling delay of the host
35 * controller. Some Intel controllers need more than one frame; as long as
36 * that driver doesn't tell us about this, use 1.5 frames just to be sure.
38 #define MIN_QUEUE_LENGTH 12
39 /* Somewhat random. */
40 #define MAX_QUEUE_LENGTH 30
42 * This magic value optimizes memory usage efficiency for the UA-101's packet
43 * sizes at all sample rates, taking into account the stupid cache pool sizes
44 * that usb_alloc_coherent() uses.
46 #define DEFAULT_QUEUE_LENGTH 21
48 #define MAX_PACKET_SIZE 672 /* hardware specific */
49 #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
50 PAGE_SIZE / MAX_PACKET_SIZE)
52 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
53 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
54 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
55 static unsigned int queue_length
= 21;
57 module_param_array(index
, int, NULL
, 0444);
58 MODULE_PARM_DESC(index
, "card index");
59 module_param_array(id
, charp
, NULL
, 0444);
60 MODULE_PARM_DESC(id
, "ID string");
61 module_param_array(enable
, bool, NULL
, 0444);
62 MODULE_PARM_DESC(enable
, "enable card");
63 module_param(queue_length
, uint
, 0644);
64 MODULE_PARM_DESC(queue_length
, "USB queue length in microframes, "
65 __stringify(MIN_QUEUE_LENGTH
)"-"__stringify(MAX_QUEUE_LENGTH
));
75 /* bits in struct ua101::states */
82 ALSA_PLAYBACK_RUNNING
,
83 CAPTURE_URB_COMPLETED
,
84 PLAYBACK_URB_COMPLETED
,
89 struct usb_device
*dev
;
90 struct snd_card
*card
;
91 struct usb_interface
*intf
[INTF_COUNT
];
94 struct list_head midi_list
;
97 unsigned int packets_per_second
;
100 unsigned long states
;
102 /* FIFO to synchronize playback rate to capture rate */
103 unsigned int rate_feedback_start
;
104 unsigned int rate_feedback_count
;
105 u8 rate_feedback
[MAX_QUEUE_LENGTH
];
107 struct list_head ready_playback_urbs
;
108 struct tasklet_struct playback_tasklet
;
109 wait_queue_head_t alsa_capture_wait
;
110 wait_queue_head_t rate_feedback_wait
;
111 wait_queue_head_t alsa_playback_wait
;
112 struct ua101_stream
{
113 struct snd_pcm_substream
*substream
;
114 unsigned int usb_pipe
;
115 unsigned int channels
;
116 unsigned int frame_bytes
;
117 unsigned int max_packet_bytes
;
118 unsigned int period_pos
;
119 unsigned int buffer_pos
;
120 unsigned int queue_length
;
123 struct usb_iso_packet_descriptor iso_frame_desc
[1];
124 struct list_head ready_list
;
125 } *urbs
[MAX_QUEUE_LENGTH
];
130 } buffers
[MAX_MEMORY_BUFFERS
];
134 static DEFINE_MUTEX(devices_mutex
);
135 static unsigned int devices_used
;
136 static struct usb_driver ua101_driver
;
138 static void abort_alsa_playback(struct ua101
*ua
);
139 static void abort_alsa_capture(struct ua101
*ua
);
141 static const char *usb_error_string(int err
)
147 return "endpoint not enabled";
149 return "endpoint stalled";
151 return "not enough bandwidth";
153 return "device disabled";
155 return "device suspended";
160 return "internal error";
162 return "unknown error";
166 static void abort_usb_capture(struct ua101
*ua
)
168 if (test_and_clear_bit(USB_CAPTURE_RUNNING
, &ua
->states
)) {
169 wake_up(&ua
->alsa_capture_wait
);
170 wake_up(&ua
->rate_feedback_wait
);
174 static void abort_usb_playback(struct ua101
*ua
)
176 if (test_and_clear_bit(USB_PLAYBACK_RUNNING
, &ua
->states
))
177 wake_up(&ua
->alsa_playback_wait
);
180 static void playback_urb_complete(struct urb
*usb_urb
)
182 struct ua101_urb
*urb
= (struct ua101_urb
*)usb_urb
;
183 struct ua101
*ua
= urb
->urb
.context
;
186 if (unlikely(urb
->urb
.status
== -ENOENT
|| /* unlinked */
187 urb
->urb
.status
== -ENODEV
|| /* device removed */
188 urb
->urb
.status
== -ECONNRESET
|| /* unlinked */
189 urb
->urb
.status
== -ESHUTDOWN
)) { /* device disabled */
190 abort_usb_playback(ua
);
191 abort_alsa_playback(ua
);
195 if (test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
)) {
196 /* append URB to FIFO */
197 spin_lock_irqsave(&ua
->lock
, flags
);
198 list_add_tail(&urb
->ready_list
, &ua
->ready_playback_urbs
);
199 if (ua
->rate_feedback_count
> 0)
200 tasklet_schedule(&ua
->playback_tasklet
);
201 ua
->playback
.substream
->runtime
->delay
-=
202 urb
->urb
.iso_frame_desc
[0].length
/
203 ua
->playback
.frame_bytes
;
204 spin_unlock_irqrestore(&ua
->lock
, flags
);
208 static void first_playback_urb_complete(struct urb
*urb
)
210 struct ua101
*ua
= urb
->context
;
212 urb
->complete
= playback_urb_complete
;
213 playback_urb_complete(urb
);
215 set_bit(PLAYBACK_URB_COMPLETED
, &ua
->states
);
216 wake_up(&ua
->alsa_playback_wait
);
219 /* copy data from the ALSA ring buffer into the URB buffer */
220 static bool copy_playback_data(struct ua101_stream
*stream
, struct urb
*urb
,
223 struct snd_pcm_runtime
*runtime
;
224 unsigned int frame_bytes
, frames1
;
227 runtime
= stream
->substream
->runtime
;
228 frame_bytes
= stream
->frame_bytes
;
229 source
= runtime
->dma_area
+ stream
->buffer_pos
* frame_bytes
;
230 if (stream
->buffer_pos
+ frames
<= runtime
->buffer_size
) {
231 memcpy(urb
->transfer_buffer
, source
, frames
* frame_bytes
);
233 /* wrap around at end of ring buffer */
234 frames1
= runtime
->buffer_size
- stream
->buffer_pos
;
235 memcpy(urb
->transfer_buffer
, source
, frames1
* frame_bytes
);
236 memcpy(urb
->transfer_buffer
+ frames1
* frame_bytes
,
237 runtime
->dma_area
, (frames
- frames1
) * frame_bytes
);
240 stream
->buffer_pos
+= frames
;
241 if (stream
->buffer_pos
>= runtime
->buffer_size
)
242 stream
->buffer_pos
-= runtime
->buffer_size
;
243 stream
->period_pos
+= frames
;
244 if (stream
->period_pos
>= runtime
->period_size
) {
245 stream
->period_pos
-= runtime
->period_size
;
251 static inline void add_with_wraparound(struct ua101
*ua
,
252 unsigned int *value
, unsigned int add
)
255 if (*value
>= ua
->playback
.queue_length
)
256 *value
-= ua
->playback
.queue_length
;
259 static void playback_tasklet(unsigned long data
)
261 struct ua101
*ua
= (void *)data
;
264 struct ua101_urb
*urb
;
265 bool do_period_elapsed
= false;
268 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
)))
272 * Synchronizing the playback rate to the capture rate is done by using
273 * the same sequence of packet sizes for both streams.
274 * Submitting a playback URB therefore requires both a ready URB and
275 * the size of the corresponding capture packet, i.e., both playback
276 * and capture URBs must have been completed. Since the USB core does
277 * not guarantee that playback and capture complete callbacks are
278 * called alternately, we use two FIFOs for packet sizes and read URBs;
279 * submitting playback URBs is possible as long as both FIFOs are
282 spin_lock_irqsave(&ua
->lock
, flags
);
283 while (ua
->rate_feedback_count
> 0 &&
284 !list_empty(&ua
->ready_playback_urbs
)) {
285 /* take packet size out of FIFO */
286 frames
= ua
->rate_feedback
[ua
->rate_feedback_start
];
287 add_with_wraparound(ua
, &ua
->rate_feedback_start
, 1);
288 ua
->rate_feedback_count
--;
290 /* take URB out of FIFO */
291 urb
= list_first_entry(&ua
->ready_playback_urbs
,
292 struct ua101_urb
, ready_list
);
293 list_del(&urb
->ready_list
);
295 /* fill packet with data or silence */
296 urb
->urb
.iso_frame_desc
[0].length
=
297 frames
* ua
->playback
.frame_bytes
;
298 if (test_bit(ALSA_PLAYBACK_RUNNING
, &ua
->states
))
299 do_period_elapsed
|= copy_playback_data(&ua
->playback
,
303 memset(urb
->urb
.transfer_buffer
, 0,
304 urb
->urb
.iso_frame_desc
[0].length
);
306 /* and off you go ... */
307 err
= usb_submit_urb(&urb
->urb
, GFP_ATOMIC
);
308 if (unlikely(err
< 0)) {
309 spin_unlock_irqrestore(&ua
->lock
, flags
);
310 abort_usb_playback(ua
);
311 abort_alsa_playback(ua
);
312 dev_err(&ua
->dev
->dev
, "USB request error %d: %s\n",
313 err
, usb_error_string(err
));
316 ua
->playback
.substream
->runtime
->delay
+= frames
;
318 spin_unlock_irqrestore(&ua
->lock
, flags
);
319 if (do_period_elapsed
)
320 snd_pcm_period_elapsed(ua
->playback
.substream
);
323 /* copy data from the URB buffer into the ALSA ring buffer */
324 static bool copy_capture_data(struct ua101_stream
*stream
, struct urb
*urb
,
327 struct snd_pcm_runtime
*runtime
;
328 unsigned int frame_bytes
, frames1
;
331 runtime
= stream
->substream
->runtime
;
332 frame_bytes
= stream
->frame_bytes
;
333 dest
= runtime
->dma_area
+ stream
->buffer_pos
* frame_bytes
;
334 if (stream
->buffer_pos
+ frames
<= runtime
->buffer_size
) {
335 memcpy(dest
, urb
->transfer_buffer
, frames
* frame_bytes
);
337 /* wrap around at end of ring buffer */
338 frames1
= runtime
->buffer_size
- stream
->buffer_pos
;
339 memcpy(dest
, urb
->transfer_buffer
, frames1
* frame_bytes
);
340 memcpy(runtime
->dma_area
,
341 urb
->transfer_buffer
+ frames1
* frame_bytes
,
342 (frames
- frames1
) * frame_bytes
);
345 stream
->buffer_pos
+= frames
;
346 if (stream
->buffer_pos
>= runtime
->buffer_size
)
347 stream
->buffer_pos
-= runtime
->buffer_size
;
348 stream
->period_pos
+= frames
;
349 if (stream
->period_pos
>= runtime
->period_size
) {
350 stream
->period_pos
-= runtime
->period_size
;
356 static void capture_urb_complete(struct urb
*urb
)
358 struct ua101
*ua
= urb
->context
;
359 struct ua101_stream
*stream
= &ua
->capture
;
361 unsigned int frames
, write_ptr
;
362 bool do_period_elapsed
;
365 if (unlikely(urb
->status
== -ENOENT
|| /* unlinked */
366 urb
->status
== -ENODEV
|| /* device removed */
367 urb
->status
== -ECONNRESET
|| /* unlinked */
368 urb
->status
== -ESHUTDOWN
)) /* device disabled */
371 if (urb
->status
>= 0 && urb
->iso_frame_desc
[0].status
>= 0)
372 frames
= urb
->iso_frame_desc
[0].actual_length
/
377 spin_lock_irqsave(&ua
->lock
, flags
);
379 if (frames
> 0 && test_bit(ALSA_CAPTURE_RUNNING
, &ua
->states
))
380 do_period_elapsed
= copy_capture_data(stream
, urb
, frames
);
382 do_period_elapsed
= false;
384 if (test_bit(USB_CAPTURE_RUNNING
, &ua
->states
)) {
385 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
386 if (unlikely(err
< 0)) {
387 spin_unlock_irqrestore(&ua
->lock
, flags
);
388 dev_err(&ua
->dev
->dev
, "USB request error %d: %s\n",
389 err
, usb_error_string(err
));
393 /* append packet size to FIFO */
394 write_ptr
= ua
->rate_feedback_start
;
395 add_with_wraparound(ua
, &write_ptr
, ua
->rate_feedback_count
);
396 ua
->rate_feedback
[write_ptr
] = frames
;
397 if (ua
->rate_feedback_count
< ua
->playback
.queue_length
) {
398 ua
->rate_feedback_count
++;
399 if (ua
->rate_feedback_count
==
400 ua
->playback
.queue_length
)
401 wake_up(&ua
->rate_feedback_wait
);
404 * Ring buffer overflow; this happens when the playback
405 * stream is not running. Throw away the oldest entry,
406 * so that the playback stream, when it starts, sees
407 * the most recent packet sizes.
409 add_with_wraparound(ua
, &ua
->rate_feedback_start
, 1);
411 if (test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
) &&
412 !list_empty(&ua
->ready_playback_urbs
))
413 tasklet_schedule(&ua
->playback_tasklet
);
416 spin_unlock_irqrestore(&ua
->lock
, flags
);
418 if (do_period_elapsed
)
419 snd_pcm_period_elapsed(stream
->substream
);
424 abort_usb_playback(ua
);
425 abort_usb_capture(ua
);
426 abort_alsa_playback(ua
);
427 abort_alsa_capture(ua
);
430 static void first_capture_urb_complete(struct urb
*urb
)
432 struct ua101
*ua
= urb
->context
;
434 urb
->complete
= capture_urb_complete
;
435 capture_urb_complete(urb
);
437 set_bit(CAPTURE_URB_COMPLETED
, &ua
->states
);
438 wake_up(&ua
->alsa_capture_wait
);
441 static int submit_stream_urbs(struct ua101
*ua
, struct ua101_stream
*stream
)
445 for (i
= 0; i
< stream
->queue_length
; ++i
) {
446 int err
= usb_submit_urb(&stream
->urbs
[i
]->urb
, GFP_KERNEL
);
448 dev_err(&ua
->dev
->dev
, "USB request error %d: %s\n",
449 err
, usb_error_string(err
));
456 static void kill_stream_urbs(struct ua101_stream
*stream
)
460 for (i
= 0; i
< stream
->queue_length
; ++i
)
461 usb_kill_urb(&stream
->urbs
[i
]->urb
);
464 static int enable_iso_interface(struct ua101
*ua
, unsigned int intf_index
)
466 struct usb_host_interface
*alts
;
468 alts
= ua
->intf
[intf_index
]->cur_altsetting
;
469 if (alts
->desc
.bAlternateSetting
!= 1) {
470 int err
= usb_set_interface(ua
->dev
,
471 alts
->desc
.bInterfaceNumber
, 1);
473 dev_err(&ua
->dev
->dev
,
474 "cannot initialize interface; error %d: %s\n",
475 err
, usb_error_string(err
));
482 static void disable_iso_interface(struct ua101
*ua
, unsigned int intf_index
)
484 struct usb_host_interface
*alts
;
486 alts
= ua
->intf
[intf_index
]->cur_altsetting
;
487 if (alts
->desc
.bAlternateSetting
!= 0) {
488 int err
= usb_set_interface(ua
->dev
,
489 alts
->desc
.bInterfaceNumber
, 0);
490 if (err
< 0 && !test_bit(DISCONNECTED
, &ua
->states
))
491 dev_warn(&ua
->dev
->dev
,
492 "interface reset failed; error %d: %s\n",
493 err
, usb_error_string(err
));
497 static void stop_usb_capture(struct ua101
*ua
)
499 clear_bit(USB_CAPTURE_RUNNING
, &ua
->states
);
501 kill_stream_urbs(&ua
->capture
);
503 disable_iso_interface(ua
, INTF_CAPTURE
);
506 static int start_usb_capture(struct ua101
*ua
)
510 if (test_bit(DISCONNECTED
, &ua
->states
))
513 if (test_bit(USB_CAPTURE_RUNNING
, &ua
->states
))
516 kill_stream_urbs(&ua
->capture
);
518 err
= enable_iso_interface(ua
, INTF_CAPTURE
);
522 clear_bit(CAPTURE_URB_COMPLETED
, &ua
->states
);
523 ua
->capture
.urbs
[0]->urb
.complete
= first_capture_urb_complete
;
524 ua
->rate_feedback_start
= 0;
525 ua
->rate_feedback_count
= 0;
527 set_bit(USB_CAPTURE_RUNNING
, &ua
->states
);
528 err
= submit_stream_urbs(ua
, &ua
->capture
);
530 stop_usb_capture(ua
);
534 static void stop_usb_playback(struct ua101
*ua
)
536 clear_bit(USB_PLAYBACK_RUNNING
, &ua
->states
);
538 kill_stream_urbs(&ua
->playback
);
540 tasklet_kill(&ua
->playback_tasklet
);
542 disable_iso_interface(ua
, INTF_PLAYBACK
);
545 static int start_usb_playback(struct ua101
*ua
)
547 unsigned int i
, frames
;
551 if (test_bit(DISCONNECTED
, &ua
->states
))
554 if (test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
))
557 kill_stream_urbs(&ua
->playback
);
558 tasklet_kill(&ua
->playback_tasklet
);
560 err
= enable_iso_interface(ua
, INTF_PLAYBACK
);
564 clear_bit(PLAYBACK_URB_COMPLETED
, &ua
->states
);
565 ua
->playback
.urbs
[0]->urb
.complete
=
566 first_playback_urb_complete
;
567 spin_lock_irq(&ua
->lock
);
568 INIT_LIST_HEAD(&ua
->ready_playback_urbs
);
569 spin_unlock_irq(&ua
->lock
);
572 * We submit the initial URBs all at once, so we have to wait for the
573 * packet size FIFO to be full.
575 wait_event(ua
->rate_feedback_wait
,
576 ua
->rate_feedback_count
>= ua
->playback
.queue_length
||
577 !test_bit(USB_CAPTURE_RUNNING
, &ua
->states
) ||
578 test_bit(DISCONNECTED
, &ua
->states
));
579 if (test_bit(DISCONNECTED
, &ua
->states
)) {
580 stop_usb_playback(ua
);
583 if (!test_bit(USB_CAPTURE_RUNNING
, &ua
->states
)) {
584 stop_usb_playback(ua
);
588 for (i
= 0; i
< ua
->playback
.queue_length
; ++i
) {
589 /* all initial URBs contain silence */
590 spin_lock_irq(&ua
->lock
);
591 frames
= ua
->rate_feedback
[ua
->rate_feedback_start
];
592 add_with_wraparound(ua
, &ua
->rate_feedback_start
, 1);
593 ua
->rate_feedback_count
--;
594 spin_unlock_irq(&ua
->lock
);
595 urb
= &ua
->playback
.urbs
[i
]->urb
;
596 urb
->iso_frame_desc
[0].length
=
597 frames
* ua
->playback
.frame_bytes
;
598 memset(urb
->transfer_buffer
, 0,
599 urb
->iso_frame_desc
[0].length
);
602 set_bit(USB_PLAYBACK_RUNNING
, &ua
->states
);
603 err
= submit_stream_urbs(ua
, &ua
->playback
);
605 stop_usb_playback(ua
);
609 static void abort_alsa_capture(struct ua101
*ua
)
611 if (test_bit(ALSA_CAPTURE_RUNNING
, &ua
->states
))
612 snd_pcm_stop(ua
->capture
.substream
, SNDRV_PCM_STATE_XRUN
);
615 static void abort_alsa_playback(struct ua101
*ua
)
617 if (test_bit(ALSA_PLAYBACK_RUNNING
, &ua
->states
))
618 snd_pcm_stop(ua
->playback
.substream
, SNDRV_PCM_STATE_XRUN
);
621 static int set_stream_hw(struct ua101
*ua
, struct snd_pcm_substream
*substream
,
622 unsigned int channels
)
626 substream
->runtime
->hw
.info
=
627 SNDRV_PCM_INFO_MMAP
|
628 SNDRV_PCM_INFO_MMAP_VALID
|
629 SNDRV_PCM_INFO_BATCH
|
630 SNDRV_PCM_INFO_INTERLEAVED
|
631 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
632 SNDRV_PCM_INFO_FIFO_IN_FRAMES
;
633 substream
->runtime
->hw
.formats
= ua
->format_bit
;
634 substream
->runtime
->hw
.rates
= snd_pcm_rate_to_rate_bit(ua
->rate
);
635 substream
->runtime
->hw
.rate_min
= ua
->rate
;
636 substream
->runtime
->hw
.rate_max
= ua
->rate
;
637 substream
->runtime
->hw
.channels_min
= channels
;
638 substream
->runtime
->hw
.channels_max
= channels
;
639 substream
->runtime
->hw
.buffer_bytes_max
= 45000 * 1024;
640 substream
->runtime
->hw
.period_bytes_min
= 1;
641 substream
->runtime
->hw
.period_bytes_max
= UINT_MAX
;
642 substream
->runtime
->hw
.periods_min
= 2;
643 substream
->runtime
->hw
.periods_max
= UINT_MAX
;
644 err
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
645 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
646 1500000 / ua
->packets_per_second
,
650 err
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0, 32, 24);
654 static int capture_pcm_open(struct snd_pcm_substream
*substream
)
656 struct ua101
*ua
= substream
->private_data
;
659 ua
->capture
.substream
= substream
;
660 err
= set_stream_hw(ua
, substream
, ua
->capture
.channels
);
663 substream
->runtime
->hw
.fifo_size
=
664 DIV_ROUND_CLOSEST(ua
->rate
, ua
->packets_per_second
);
665 substream
->runtime
->delay
= substream
->runtime
->hw
.fifo_size
;
667 mutex_lock(&ua
->mutex
);
668 err
= start_usb_capture(ua
);
670 set_bit(ALSA_CAPTURE_OPEN
, &ua
->states
);
671 mutex_unlock(&ua
->mutex
);
675 static int playback_pcm_open(struct snd_pcm_substream
*substream
)
677 struct ua101
*ua
= substream
->private_data
;
680 ua
->playback
.substream
= substream
;
681 err
= set_stream_hw(ua
, substream
, ua
->playback
.channels
);
684 substream
->runtime
->hw
.fifo_size
=
685 DIV_ROUND_CLOSEST(ua
->rate
* ua
->playback
.queue_length
,
686 ua
->packets_per_second
);
688 mutex_lock(&ua
->mutex
);
689 err
= start_usb_capture(ua
);
692 err
= start_usb_playback(ua
);
694 if (!test_bit(ALSA_CAPTURE_OPEN
, &ua
->states
))
695 stop_usb_capture(ua
);
698 set_bit(ALSA_PLAYBACK_OPEN
, &ua
->states
);
700 mutex_unlock(&ua
->mutex
);
704 static int capture_pcm_close(struct snd_pcm_substream
*substream
)
706 struct ua101
*ua
= substream
->private_data
;
708 mutex_lock(&ua
->mutex
);
709 clear_bit(ALSA_CAPTURE_OPEN
, &ua
->states
);
710 if (!test_bit(ALSA_PLAYBACK_OPEN
, &ua
->states
))
711 stop_usb_capture(ua
);
712 mutex_unlock(&ua
->mutex
);
716 static int playback_pcm_close(struct snd_pcm_substream
*substream
)
718 struct ua101
*ua
= substream
->private_data
;
720 mutex_lock(&ua
->mutex
);
721 stop_usb_playback(ua
);
722 clear_bit(ALSA_PLAYBACK_OPEN
, &ua
->states
);
723 if (!test_bit(ALSA_CAPTURE_OPEN
, &ua
->states
))
724 stop_usb_capture(ua
);
725 mutex_unlock(&ua
->mutex
);
729 static int capture_pcm_hw_params(struct snd_pcm_substream
*substream
,
730 struct snd_pcm_hw_params
*hw_params
)
732 struct ua101
*ua
= substream
->private_data
;
735 mutex_lock(&ua
->mutex
);
736 err
= start_usb_capture(ua
);
737 mutex_unlock(&ua
->mutex
);
741 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
742 params_buffer_bytes(hw_params
));
745 static int playback_pcm_hw_params(struct snd_pcm_substream
*substream
,
746 struct snd_pcm_hw_params
*hw_params
)
748 struct ua101
*ua
= substream
->private_data
;
751 mutex_lock(&ua
->mutex
);
752 err
= start_usb_capture(ua
);
754 err
= start_usb_playback(ua
);
755 mutex_unlock(&ua
->mutex
);
759 return snd_pcm_lib_alloc_vmalloc_buffer(substream
,
760 params_buffer_bytes(hw_params
));
763 static int ua101_pcm_hw_free(struct snd_pcm_substream
*substream
)
765 return snd_pcm_lib_free_vmalloc_buffer(substream
);
768 static int capture_pcm_prepare(struct snd_pcm_substream
*substream
)
770 struct ua101
*ua
= substream
->private_data
;
773 mutex_lock(&ua
->mutex
);
774 err
= start_usb_capture(ua
);
775 mutex_unlock(&ua
->mutex
);
780 * The EHCI driver schedules the first packet of an iso stream at 10 ms
781 * in the future, i.e., no data is actually captured for that long.
782 * Take the wait here so that the stream is known to be actually
783 * running when the start trigger has been called.
785 wait_event(ua
->alsa_capture_wait
,
786 test_bit(CAPTURE_URB_COMPLETED
, &ua
->states
) ||
787 !test_bit(USB_CAPTURE_RUNNING
, &ua
->states
));
788 if (test_bit(DISCONNECTED
, &ua
->states
))
790 if (!test_bit(USB_CAPTURE_RUNNING
, &ua
->states
))
793 ua
->capture
.period_pos
= 0;
794 ua
->capture
.buffer_pos
= 0;
798 static int playback_pcm_prepare(struct snd_pcm_substream
*substream
)
800 struct ua101
*ua
= substream
->private_data
;
803 mutex_lock(&ua
->mutex
);
804 err
= start_usb_capture(ua
);
806 err
= start_usb_playback(ua
);
807 mutex_unlock(&ua
->mutex
);
811 /* see the comment in capture_pcm_prepare() */
812 wait_event(ua
->alsa_playback_wait
,
813 test_bit(PLAYBACK_URB_COMPLETED
, &ua
->states
) ||
814 !test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
));
815 if (test_bit(DISCONNECTED
, &ua
->states
))
817 if (!test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
))
820 substream
->runtime
->delay
= 0;
821 ua
->playback
.period_pos
= 0;
822 ua
->playback
.buffer_pos
= 0;
826 static int capture_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
828 struct ua101
*ua
= substream
->private_data
;
831 case SNDRV_PCM_TRIGGER_START
:
832 if (!test_bit(USB_CAPTURE_RUNNING
, &ua
->states
))
834 set_bit(ALSA_CAPTURE_RUNNING
, &ua
->states
);
836 case SNDRV_PCM_TRIGGER_STOP
:
837 clear_bit(ALSA_CAPTURE_RUNNING
, &ua
->states
);
844 static int playback_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
846 struct ua101
*ua
= substream
->private_data
;
849 case SNDRV_PCM_TRIGGER_START
:
850 if (!test_bit(USB_PLAYBACK_RUNNING
, &ua
->states
))
852 set_bit(ALSA_PLAYBACK_RUNNING
, &ua
->states
);
854 case SNDRV_PCM_TRIGGER_STOP
:
855 clear_bit(ALSA_PLAYBACK_RUNNING
, &ua
->states
);
862 static inline snd_pcm_uframes_t
ua101_pcm_pointer(struct ua101
*ua
,
863 struct ua101_stream
*stream
)
868 spin_lock_irqsave(&ua
->lock
, flags
);
869 pos
= stream
->buffer_pos
;
870 spin_unlock_irqrestore(&ua
->lock
, flags
);
874 static snd_pcm_uframes_t
capture_pcm_pointer(struct snd_pcm_substream
*subs
)
876 struct ua101
*ua
= subs
->private_data
;
878 return ua101_pcm_pointer(ua
, &ua
->capture
);
881 static snd_pcm_uframes_t
playback_pcm_pointer(struct snd_pcm_substream
*subs
)
883 struct ua101
*ua
= subs
->private_data
;
885 return ua101_pcm_pointer(ua
, &ua
->playback
);
888 static struct snd_pcm_ops capture_pcm_ops
= {
889 .open
= capture_pcm_open
,
890 .close
= capture_pcm_close
,
891 .ioctl
= snd_pcm_lib_ioctl
,
892 .hw_params
= capture_pcm_hw_params
,
893 .hw_free
= ua101_pcm_hw_free
,
894 .prepare
= capture_pcm_prepare
,
895 .trigger
= capture_pcm_trigger
,
896 .pointer
= capture_pcm_pointer
,
897 .page
= snd_pcm_lib_get_vmalloc_page
,
898 .mmap
= snd_pcm_lib_mmap_vmalloc
,
901 static struct snd_pcm_ops playback_pcm_ops
= {
902 .open
= playback_pcm_open
,
903 .close
= playback_pcm_close
,
904 .ioctl
= snd_pcm_lib_ioctl
,
905 .hw_params
= playback_pcm_hw_params
,
906 .hw_free
= ua101_pcm_hw_free
,
907 .prepare
= playback_pcm_prepare
,
908 .trigger
= playback_pcm_trigger
,
909 .pointer
= playback_pcm_pointer
,
910 .page
= snd_pcm_lib_get_vmalloc_page
,
911 .mmap
= snd_pcm_lib_mmap_vmalloc
,
914 static const struct uac_format_type_i_discrete_descriptor
*
915 find_format_descriptor(struct usb_interface
*interface
)
917 struct usb_host_interface
*alt
;
921 if (interface
->num_altsetting
!= 2) {
922 dev_err(&interface
->dev
, "invalid num_altsetting\n");
926 alt
= &interface
->altsetting
[0];
927 if (alt
->desc
.bNumEndpoints
!= 0) {
928 dev_err(&interface
->dev
, "invalid bNumEndpoints\n");
932 alt
= &interface
->altsetting
[1];
933 if (alt
->desc
.bNumEndpoints
!= 1) {
934 dev_err(&interface
->dev
, "invalid bNumEndpoints\n");
939 extralen
= alt
->extralen
;
940 while (extralen
>= sizeof(struct usb_descriptor_header
)) {
941 struct uac_format_type_i_discrete_descriptor
*desc
;
943 desc
= (struct uac_format_type_i_discrete_descriptor
*)extra
;
944 if (desc
->bLength
> extralen
) {
945 dev_err(&interface
->dev
, "descriptor overflow\n");
948 if (desc
->bLength
== UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
949 desc
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
950 desc
->bDescriptorSubtype
== UAC_FORMAT_TYPE
) {
951 if (desc
->bFormatType
!= UAC_FORMAT_TYPE_I_PCM
||
952 desc
->bSamFreqType
!= 1) {
953 dev_err(&interface
->dev
,
954 "invalid format type\n");
959 extralen
-= desc
->bLength
;
960 extra
+= desc
->bLength
;
962 dev_err(&interface
->dev
, "sample format descriptor not found\n");
966 static int detect_usb_format(struct ua101
*ua
)
968 const struct uac_format_type_i_discrete_descriptor
*fmt_capture
;
969 const struct uac_format_type_i_discrete_descriptor
*fmt_playback
;
970 const struct usb_endpoint_descriptor
*epd
;
973 fmt_capture
= find_format_descriptor(ua
->intf
[INTF_CAPTURE
]);
974 fmt_playback
= find_format_descriptor(ua
->intf
[INTF_PLAYBACK
]);
975 if (!fmt_capture
|| !fmt_playback
)
978 switch (fmt_capture
->bSubframeSize
) {
980 ua
->format_bit
= SNDRV_PCM_FMTBIT_S24_3LE
;
983 ua
->format_bit
= SNDRV_PCM_FMTBIT_S32_LE
;
986 dev_err(&ua
->dev
->dev
, "sample width is not 24 or 32 bits\n");
989 if (fmt_capture
->bSubframeSize
!= fmt_playback
->bSubframeSize
) {
990 dev_err(&ua
->dev
->dev
,
991 "playback/capture sample widths do not match\n");
995 if (fmt_capture
->bBitResolution
!= 24 ||
996 fmt_playback
->bBitResolution
!= 24) {
997 dev_err(&ua
->dev
->dev
, "sample width is not 24 bits\n");
1001 ua
->rate
= combine_triple(fmt_capture
->tSamFreq
[0]);
1002 rate2
= combine_triple(fmt_playback
->tSamFreq
[0]);
1003 if (ua
->rate
!= rate2
) {
1004 dev_err(&ua
->dev
->dev
,
1005 "playback/capture rates do not match: %u/%u\n",
1010 switch (ua
->dev
->speed
) {
1011 case USB_SPEED_FULL
:
1012 ua
->packets_per_second
= 1000;
1014 case USB_SPEED_HIGH
:
1015 ua
->packets_per_second
= 8000;
1018 dev_err(&ua
->dev
->dev
, "unknown device speed\n");
1022 ua
->capture
.channels
= fmt_capture
->bNrChannels
;
1023 ua
->playback
.channels
= fmt_playback
->bNrChannels
;
1024 ua
->capture
.frame_bytes
=
1025 fmt_capture
->bSubframeSize
* ua
->capture
.channels
;
1026 ua
->playback
.frame_bytes
=
1027 fmt_playback
->bSubframeSize
* ua
->playback
.channels
;
1029 epd
= &ua
->intf
[INTF_CAPTURE
]->altsetting
[1].endpoint
[0].desc
;
1030 if (!usb_endpoint_is_isoc_in(epd
)) {
1031 dev_err(&ua
->dev
->dev
, "invalid capture endpoint\n");
1034 ua
->capture
.usb_pipe
= usb_rcvisocpipe(ua
->dev
, usb_endpoint_num(epd
));
1035 ua
->capture
.max_packet_bytes
= le16_to_cpu(epd
->wMaxPacketSize
);
1037 epd
= &ua
->intf
[INTF_PLAYBACK
]->altsetting
[1].endpoint
[0].desc
;
1038 if (!usb_endpoint_is_isoc_out(epd
)) {
1039 dev_err(&ua
->dev
->dev
, "invalid playback endpoint\n");
1042 ua
->playback
.usb_pipe
= usb_sndisocpipe(ua
->dev
, usb_endpoint_num(epd
));
1043 ua
->playback
.max_packet_bytes
= le16_to_cpu(epd
->wMaxPacketSize
);
1047 static int alloc_stream_buffers(struct ua101
*ua
, struct ua101_stream
*stream
)
1049 unsigned int remaining_packets
, packets
, packets_per_page
, i
;
1052 stream
->queue_length
= queue_length
;
1053 stream
->queue_length
= max(stream
->queue_length
,
1054 (unsigned int)MIN_QUEUE_LENGTH
);
1055 stream
->queue_length
= min(stream
->queue_length
,
1056 (unsigned int)MAX_QUEUE_LENGTH
);
1059 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
1060 * quite bad when used with the packet sizes of this device (e.g. 280,
1061 * 520, 624). Therefore, we allocate and subdivide entire pages, using
1062 * a smaller buffer only for the last chunk.
1064 remaining_packets
= stream
->queue_length
;
1065 packets_per_page
= PAGE_SIZE
/ stream
->max_packet_bytes
;
1066 for (i
= 0; i
< ARRAY_SIZE(stream
->buffers
); ++i
) {
1067 packets
= min(remaining_packets
, packets_per_page
);
1068 size
= packets
* stream
->max_packet_bytes
;
1069 stream
->buffers
[i
].addr
=
1070 usb_alloc_coherent(ua
->dev
, size
, GFP_KERNEL
,
1071 &stream
->buffers
[i
].dma
);
1072 if (!stream
->buffers
[i
].addr
)
1074 stream
->buffers
[i
].size
= size
;
1075 remaining_packets
-= packets
;
1076 if (!remaining_packets
)
1079 if (remaining_packets
) {
1080 dev_err(&ua
->dev
->dev
, "too many packets\n");
1086 static void free_stream_buffers(struct ua101
*ua
, struct ua101_stream
*stream
)
1090 for (i
= 0; i
< ARRAY_SIZE(stream
->buffers
); ++i
)
1091 usb_free_coherent(ua
->dev
,
1092 stream
->buffers
[i
].size
,
1093 stream
->buffers
[i
].addr
,
1094 stream
->buffers
[i
].dma
);
1097 static int alloc_stream_urbs(struct ua101
*ua
, struct ua101_stream
*stream
,
1098 void (*urb_complete
)(struct urb
*))
1100 unsigned max_packet_size
= stream
->max_packet_bytes
;
1101 struct ua101_urb
*urb
;
1102 unsigned int b
, u
= 0;
1104 for (b
= 0; b
< ARRAY_SIZE(stream
->buffers
); ++b
) {
1105 unsigned int size
= stream
->buffers
[b
].size
;
1106 u8
*addr
= stream
->buffers
[b
].addr
;
1107 dma_addr_t dma
= stream
->buffers
[b
].dma
;
1109 while (size
>= max_packet_size
) {
1110 if (u
>= stream
->queue_length
)
1112 urb
= kmalloc(sizeof(*urb
), GFP_KERNEL
);
1115 usb_init_urb(&urb
->urb
);
1116 urb
->urb
.dev
= ua
->dev
;
1117 urb
->urb
.pipe
= stream
->usb_pipe
;
1118 urb
->urb
.transfer_flags
= URB_ISO_ASAP
|
1119 URB_NO_TRANSFER_DMA_MAP
;
1120 urb
->urb
.transfer_buffer
= addr
;
1121 urb
->urb
.transfer_dma
= dma
;
1122 urb
->urb
.transfer_buffer_length
= max_packet_size
;
1123 urb
->urb
.number_of_packets
= 1;
1124 urb
->urb
.interval
= 1;
1125 urb
->urb
.context
= ua
;
1126 urb
->urb
.complete
= urb_complete
;
1127 urb
->urb
.iso_frame_desc
[0].offset
= 0;
1128 urb
->urb
.iso_frame_desc
[0].length
= max_packet_size
;
1129 stream
->urbs
[u
++] = urb
;
1130 size
-= max_packet_size
;
1131 addr
+= max_packet_size
;
1132 dma
+= max_packet_size
;
1135 if (u
== stream
->queue_length
)
1138 dev_err(&ua
->dev
->dev
, "internal buffer size error\n");
1142 static void free_stream_urbs(struct ua101_stream
*stream
)
1146 for (i
= 0; i
< stream
->queue_length
; ++i
)
1147 kfree(stream
->urbs
[i
]);
1150 static void free_usb_related_resources(struct ua101
*ua
,
1151 struct usb_interface
*interface
)
1155 free_stream_urbs(&ua
->capture
);
1156 free_stream_urbs(&ua
->playback
);
1157 free_stream_buffers(ua
, &ua
->capture
);
1158 free_stream_buffers(ua
, &ua
->playback
);
1160 for (i
= 0; i
< ARRAY_SIZE(ua
->intf
); ++i
)
1162 usb_set_intfdata(ua
->intf
[i
], NULL
);
1163 if (ua
->intf
[i
] != interface
)
1164 usb_driver_release_interface(&ua101_driver
,
1169 static void ua101_card_free(struct snd_card
*card
)
1171 struct ua101
*ua
= card
->private_data
;
1173 mutex_destroy(&ua
->mutex
);
1176 static int ua101_probe(struct usb_interface
*interface
,
1177 const struct usb_device_id
*usb_id
)
1179 static const struct snd_usb_midi_endpoint_info midi_ep
= {
1180 .out_cables
= 0x0001,
1183 static const struct snd_usb_audio_quirk midi_quirk
= {
1184 .type
= QUIRK_MIDI_FIXED_ENDPOINT
,
1187 static const int intf_numbers
[2][3] = {
1189 [INTF_PLAYBACK
] = 0,
1195 [INTF_PLAYBACK
] = 2,
1199 struct snd_card
*card
;
1201 unsigned int card_index
, i
;
1207 is_ua1000
= usb_id
->idProduct
== 0x0044;
1209 if (interface
->altsetting
->desc
.bInterfaceNumber
!=
1210 intf_numbers
[is_ua1000
][0])
1213 mutex_lock(&devices_mutex
);
1215 for (card_index
= 0; card_index
< SNDRV_CARDS
; ++card_index
)
1216 if (enable
[card_index
] && !(devices_used
& (1 << card_index
)))
1218 if (card_index
>= SNDRV_CARDS
) {
1219 mutex_unlock(&devices_mutex
);
1222 err
= snd_card_create(index
[card_index
], id
[card_index
], THIS_MODULE
,
1223 sizeof(*ua
), &card
);
1225 mutex_unlock(&devices_mutex
);
1228 card
->private_free
= ua101_card_free
;
1229 ua
= card
->private_data
;
1230 ua
->dev
= interface_to_usbdev(interface
);
1232 ua
->card_index
= card_index
;
1233 INIT_LIST_HEAD(&ua
->midi_list
);
1234 spin_lock_init(&ua
->lock
);
1235 mutex_init(&ua
->mutex
);
1236 INIT_LIST_HEAD(&ua
->ready_playback_urbs
);
1237 tasklet_init(&ua
->playback_tasklet
,
1238 playback_tasklet
, (unsigned long)ua
);
1239 init_waitqueue_head(&ua
->alsa_capture_wait
);
1240 init_waitqueue_head(&ua
->rate_feedback_wait
);
1241 init_waitqueue_head(&ua
->alsa_playback_wait
);
1243 ua
->intf
[0] = interface
;
1244 for (i
= 1; i
< ARRAY_SIZE(ua
->intf
); ++i
) {
1245 ua
->intf
[i
] = usb_ifnum_to_if(ua
->dev
,
1246 intf_numbers
[is_ua1000
][i
]);
1248 dev_err(&ua
->dev
->dev
, "interface %u not found\n",
1249 intf_numbers
[is_ua1000
][i
]);
1253 err
= usb_driver_claim_interface(&ua101_driver
,
1262 snd_card_set_dev(card
, &interface
->dev
);
1264 err
= detect_usb_format(ua
);
1268 name
= usb_id
->idProduct
== 0x0044 ? "UA-1000" : "UA-101";
1269 strcpy(card
->driver
, "UA-101");
1270 strcpy(card
->shortname
, name
);
1271 usb_make_path(ua
->dev
, usb_path
, sizeof(usb_path
));
1272 snprintf(ua
->card
->longname
, sizeof(ua
->card
->longname
),
1273 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name
,
1274 ua
->dev
->serial
? ua
->dev
->serial
: "?", ua
->rate
, usb_path
,
1275 ua
->dev
->speed
== USB_SPEED_HIGH
? "high" : "full");
1277 err
= alloc_stream_buffers(ua
, &ua
->capture
);
1280 err
= alloc_stream_buffers(ua
, &ua
->playback
);
1284 err
= alloc_stream_urbs(ua
, &ua
->capture
, capture_urb_complete
);
1287 err
= alloc_stream_urbs(ua
, &ua
->playback
, playback_urb_complete
);
1291 err
= snd_pcm_new(card
, name
, 0, 1, 1, &ua
->pcm
);
1294 ua
->pcm
->private_data
= ua
;
1295 strcpy(ua
->pcm
->name
, name
);
1296 snd_pcm_set_ops(ua
->pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &playback_pcm_ops
);
1297 snd_pcm_set_ops(ua
->pcm
, SNDRV_PCM_STREAM_CAPTURE
, &capture_pcm_ops
);
1299 err
= snd_usbmidi_create(card
, ua
->intf
[INTF_MIDI
],
1300 &ua
->midi_list
, &midi_quirk
);
1304 err
= snd_card_register(card
);
1308 usb_set_intfdata(interface
, ua
);
1309 devices_used
|= 1 << card_index
;
1311 mutex_unlock(&devices_mutex
);
1315 free_usb_related_resources(ua
, interface
);
1316 snd_card_free(card
);
1317 mutex_unlock(&devices_mutex
);
1321 static void ua101_disconnect(struct usb_interface
*interface
)
1323 struct ua101
*ua
= usb_get_intfdata(interface
);
1324 struct list_head
*midi
;
1329 mutex_lock(&devices_mutex
);
1331 set_bit(DISCONNECTED
, &ua
->states
);
1332 wake_up(&ua
->rate_feedback_wait
);
1334 /* make sure that userspace cannot create new requests */
1335 snd_card_disconnect(ua
->card
);
1337 /* make sure that there are no pending USB requests */
1338 __list_for_each(midi
, &ua
->midi_list
)
1339 snd_usbmidi_disconnect(midi
);
1340 abort_alsa_playback(ua
);
1341 abort_alsa_capture(ua
);
1342 mutex_lock(&ua
->mutex
);
1343 stop_usb_playback(ua
);
1344 stop_usb_capture(ua
);
1345 mutex_unlock(&ua
->mutex
);
1347 free_usb_related_resources(ua
, interface
);
1349 devices_used
&= ~(1 << ua
->card_index
);
1351 snd_card_free_when_closed(ua
->card
);
1353 mutex_unlock(&devices_mutex
);
1356 static struct usb_device_id ua101_ids
[] = {
1357 { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1358 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1359 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1362 MODULE_DEVICE_TABLE(usb
, ua101_ids
);
1364 static struct usb_driver ua101_driver
= {
1365 .name
= "snd-ua101",
1366 .id_table
= ua101_ids
,
1367 .probe
= ua101_probe
,
1368 .disconnect
= ua101_disconnect
,
1370 .suspend
= ua101_suspend
,
1371 .resume
= ua101_resume
,
1375 static int __init
alsa_card_ua101_init(void)
1377 return usb_register(&ua101_driver
);
1380 static void __exit
alsa_card_ua101_exit(void)
1382 usb_deregister(&ua101_driver
);
1383 mutex_destroy(&devices_mutex
);
1386 module_init(alsa_card_ua101_init
);
1387 module_exit(alsa_card_ua101_exit
);