Lines Matching +full:capture +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Edirol UA-101/UA-1000 driver
19 MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
32 * This magic value optimizes memory usage efficiency for the UA-101's packet
55 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
92 /* FIFO to synchronize playback rate to capture rate */
105 unsigned int channels; member
121 } capture, playback; member
134 case -ENODEV: in usb_error_string()
136 case -ENOENT: in usb_error_string()
138 case -EPIPE: in usb_error_string()
140 case -ENOSPC: in usb_error_string()
142 case -ESHUTDOWN: in usb_error_string()
144 case -EHOSTUNREACH: in usb_error_string()
146 case -EINVAL: in usb_error_string()
147 case -EAGAIN: in usb_error_string()
148 case -EFBIG: in usb_error_string()
149 case -EMSGSIZE: in usb_error_string()
158 if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) { in abort_usb_capture()
159 wake_up(&ua->alsa_capture_wait); in abort_usb_capture()
160 wake_up(&ua->rate_feedback_wait); in abort_usb_capture()
166 if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states)) in abort_usb_playback()
167 wake_up(&ua->alsa_playback_wait); in abort_usb_playback()
173 struct ua101 *ua = urb->urb.context; in playback_urb_complete()
176 if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ in playback_urb_complete()
177 urb->urb.status == -ENODEV || /* device removed */ in playback_urb_complete()
178 urb->urb.status == -ECONNRESET || /* unlinked */ in playback_urb_complete()
179 urb->urb.status == -ESHUTDOWN)) { /* device disabled */ in playback_urb_complete()
185 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { in playback_urb_complete()
187 spin_lock_irqsave(&ua->lock, flags); in playback_urb_complete()
188 list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); in playback_urb_complete()
189 if (ua->rate_feedback_count > 0) in playback_urb_complete()
190 queue_work(system_highpri_wq, &ua->playback_work); in playback_urb_complete()
191 ua->playback.substream->runtime->delay -= in playback_urb_complete()
192 urb->urb.iso_frame_desc[0].length / in playback_urb_complete()
193 ua->playback.frame_bytes; in playback_urb_complete()
194 spin_unlock_irqrestore(&ua->lock, flags); in playback_urb_complete()
200 struct ua101 *ua = urb->context; in first_playback_urb_complete()
202 urb->complete = playback_urb_complete; in first_playback_urb_complete()
205 set_bit(PLAYBACK_URB_COMPLETED, &ua->states); in first_playback_urb_complete()
206 wake_up(&ua->alsa_playback_wait); in first_playback_urb_complete()
217 runtime = stream->substream->runtime; in copy_playback_data()
218 frame_bytes = stream->frame_bytes; in copy_playback_data()
219 source = runtime->dma_area + stream->buffer_pos * frame_bytes; in copy_playback_data()
220 if (stream->buffer_pos + frames <= runtime->buffer_size) { in copy_playback_data()
221 memcpy(urb->transfer_buffer, source, frames * frame_bytes); in copy_playback_data()
224 frames1 = runtime->buffer_size - stream->buffer_pos; in copy_playback_data()
225 memcpy(urb->transfer_buffer, source, frames1 * frame_bytes); in copy_playback_data()
226 memcpy(urb->transfer_buffer + frames1 * frame_bytes, in copy_playback_data()
227 runtime->dma_area, (frames - frames1) * frame_bytes); in copy_playback_data()
230 stream->buffer_pos += frames; in copy_playback_data()
231 if (stream->buffer_pos >= runtime->buffer_size) in copy_playback_data()
232 stream->buffer_pos -= runtime->buffer_size; in copy_playback_data()
233 stream->period_pos += frames; in copy_playback_data()
234 if (stream->period_pos >= runtime->period_size) { in copy_playback_data()
235 stream->period_pos -= runtime->period_size; in copy_playback_data()
245 if (*value >= ua->playback.queue_length) in add_with_wraparound()
246 *value -= ua->playback.queue_length; in add_with_wraparound()
258 if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states))) in playback_work()
262 * Synchronizing the playback rate to the capture rate is done by using in playback_work()
265 * the size of the corresponding capture packet, i.e., both playback in playback_work()
266 * and capture URBs must have been completed. Since the USB core does in playback_work()
267 * not guarantee that playback and capture complete callbacks are in playback_work()
272 spin_lock_irqsave(&ua->lock, flags); in playback_work()
273 while (ua->rate_feedback_count > 0 && in playback_work()
274 !list_empty(&ua->ready_playback_urbs)) { in playback_work()
276 frames = ua->rate_feedback[ua->rate_feedback_start]; in playback_work()
277 add_with_wraparound(ua, &ua->rate_feedback_start, 1); in playback_work()
278 ua->rate_feedback_count--; in playback_work()
281 urb = list_first_entry(&ua->ready_playback_urbs, in playback_work()
283 list_del(&urb->ready_list); in playback_work()
286 urb->urb.iso_frame_desc[0].length = in playback_work()
287 frames * ua->playback.frame_bytes; in playback_work()
288 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) in playback_work()
289 do_period_elapsed |= copy_playback_data(&ua->playback, in playback_work()
290 &urb->urb, in playback_work()
293 memset(urb->urb.transfer_buffer, 0, in playback_work()
294 urb->urb.iso_frame_desc[0].length); in playback_work()
297 err = usb_submit_urb(&urb->urb, GFP_ATOMIC); in playback_work()
299 spin_unlock_irqrestore(&ua->lock, flags); in playback_work()
302 dev_err(&ua->dev->dev, "USB request error %d: %s\n", in playback_work()
306 ua->playback.substream->runtime->delay += frames; in playback_work()
308 spin_unlock_irqrestore(&ua->lock, flags); in playback_work()
310 snd_pcm_period_elapsed(ua->playback.substream); in playback_work()
321 runtime = stream->substream->runtime; in copy_capture_data()
322 frame_bytes = stream->frame_bytes; in copy_capture_data()
323 dest = runtime->dma_area + stream->buffer_pos * frame_bytes; in copy_capture_data()
324 if (stream->buffer_pos + frames <= runtime->buffer_size) { in copy_capture_data()
325 memcpy(dest, urb->transfer_buffer, frames * frame_bytes); in copy_capture_data()
328 frames1 = runtime->buffer_size - stream->buffer_pos; in copy_capture_data()
329 memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes); in copy_capture_data()
330 memcpy(runtime->dma_area, in copy_capture_data()
331 urb->transfer_buffer + frames1 * frame_bytes, in copy_capture_data()
332 (frames - frames1) * frame_bytes); in copy_capture_data()
335 stream->buffer_pos += frames; in copy_capture_data()
336 if (stream->buffer_pos >= runtime->buffer_size) in copy_capture_data()
337 stream->buffer_pos -= runtime->buffer_size; in copy_capture_data()
338 stream->period_pos += frames; in copy_capture_data()
339 if (stream->period_pos >= runtime->period_size) { in copy_capture_data()
340 stream->period_pos -= runtime->period_size; in copy_capture_data()
348 struct ua101 *ua = urb->context; in capture_urb_complete()
349 struct ua101_stream *stream = &ua->capture; in capture_urb_complete()
355 if (unlikely(urb->status == -ENOENT || /* unlinked */ in capture_urb_complete()
356 urb->status == -ENODEV || /* device removed */ in capture_urb_complete()
357 urb->status == -ECONNRESET || /* unlinked */ in capture_urb_complete()
358 urb->status == -ESHUTDOWN)) /* device disabled */ in capture_urb_complete()
361 if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0) in capture_urb_complete()
362 frames = urb->iso_frame_desc[0].actual_length / in capture_urb_complete()
363 stream->frame_bytes; in capture_urb_complete()
367 spin_lock_irqsave(&ua->lock, flags); in capture_urb_complete()
369 if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) in capture_urb_complete()
374 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { in capture_urb_complete()
377 spin_unlock_irqrestore(&ua->lock, flags); in capture_urb_complete()
378 dev_err(&ua->dev->dev, "USB request error %d: %s\n", in capture_urb_complete()
384 write_ptr = ua->rate_feedback_start; in capture_urb_complete()
385 add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); in capture_urb_complete()
386 ua->rate_feedback[write_ptr] = frames; in capture_urb_complete()
387 if (ua->rate_feedback_count < ua->playback.queue_length) { in capture_urb_complete()
388 ua->rate_feedback_count++; in capture_urb_complete()
389 if (ua->rate_feedback_count == in capture_urb_complete()
390 ua->playback.queue_length) in capture_urb_complete()
391 wake_up(&ua->rate_feedback_wait); in capture_urb_complete()
399 add_with_wraparound(ua, &ua->rate_feedback_start, 1); in capture_urb_complete()
401 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && in capture_urb_complete()
402 !list_empty(&ua->ready_playback_urbs)) in capture_urb_complete()
403 queue_work(system_highpri_wq, &ua->playback_work); in capture_urb_complete()
406 spin_unlock_irqrestore(&ua->lock, flags); in capture_urb_complete()
409 snd_pcm_period_elapsed(stream->substream); in capture_urb_complete()
422 struct ua101 *ua = urb->context; in first_capture_urb_complete()
424 urb->complete = capture_urb_complete; in first_capture_urb_complete()
427 set_bit(CAPTURE_URB_COMPLETED, &ua->states); in first_capture_urb_complete()
428 wake_up(&ua->alsa_capture_wait); in first_capture_urb_complete()
435 for (i = 0; i < stream->queue_length; ++i) { in submit_stream_urbs()
436 int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL); in submit_stream_urbs()
438 dev_err(&ua->dev->dev, "USB request error %d: %s\n", in submit_stream_urbs()
450 for (i = 0; i < stream->queue_length; ++i) in kill_stream_urbs()
451 if (stream->urbs[i]) in kill_stream_urbs()
452 usb_kill_urb(&stream->urbs[i]->urb); in kill_stream_urbs()
459 alts = ua->intf[intf_index]->cur_altsetting; in enable_iso_interface()
460 if (alts->desc.bAlternateSetting != 1) { in enable_iso_interface()
461 int err = usb_set_interface(ua->dev, in enable_iso_interface()
462 alts->desc.bInterfaceNumber, 1); in enable_iso_interface()
464 dev_err(&ua->dev->dev, in enable_iso_interface()
477 if (!ua->intf[intf_index]) in disable_iso_interface()
480 alts = ua->intf[intf_index]->cur_altsetting; in disable_iso_interface()
481 if (alts->desc.bAlternateSetting != 0) { in disable_iso_interface()
482 int err = usb_set_interface(ua->dev, in disable_iso_interface()
483 alts->desc.bInterfaceNumber, 0); in disable_iso_interface()
484 if (err < 0 && !test_bit(DISCONNECTED, &ua->states)) in disable_iso_interface()
485 dev_warn(&ua->dev->dev, in disable_iso_interface()
493 clear_bit(USB_CAPTURE_RUNNING, &ua->states); in stop_usb_capture()
495 kill_stream_urbs(&ua->capture); in stop_usb_capture()
504 if (test_bit(DISCONNECTED, &ua->states)) in start_usb_capture()
505 return -ENODEV; in start_usb_capture()
507 if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) in start_usb_capture()
510 kill_stream_urbs(&ua->capture); in start_usb_capture()
516 clear_bit(CAPTURE_URB_COMPLETED, &ua->states); in start_usb_capture()
517 ua->capture.urbs[0]->urb.complete = first_capture_urb_complete; in start_usb_capture()
518 ua->rate_feedback_start = 0; in start_usb_capture()
519 ua->rate_feedback_count = 0; in start_usb_capture()
521 set_bit(USB_CAPTURE_RUNNING, &ua->states); in start_usb_capture()
522 err = submit_stream_urbs(ua, &ua->capture); in start_usb_capture()
530 clear_bit(USB_PLAYBACK_RUNNING, &ua->states); in stop_usb_playback()
532 kill_stream_urbs(&ua->playback); in stop_usb_playback()
534 cancel_work_sync(&ua->playback_work); in stop_usb_playback()
545 if (test_bit(DISCONNECTED, &ua->states)) in start_usb_playback()
546 return -ENODEV; in start_usb_playback()
548 if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) in start_usb_playback()
551 kill_stream_urbs(&ua->playback); in start_usb_playback()
552 cancel_work_sync(&ua->playback_work); in start_usb_playback()
558 clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); in start_usb_playback()
559 ua->playback.urbs[0]->urb.complete = in start_usb_playback()
561 spin_lock_irq(&ua->lock); in start_usb_playback()
562 INIT_LIST_HEAD(&ua->ready_playback_urbs); in start_usb_playback()
563 spin_unlock_irq(&ua->lock); in start_usb_playback()
569 wait_event(ua->rate_feedback_wait, in start_usb_playback()
570 ua->rate_feedback_count >= ua->playback.queue_length || in start_usb_playback()
571 !test_bit(USB_CAPTURE_RUNNING, &ua->states) || in start_usb_playback()
572 test_bit(DISCONNECTED, &ua->states)); in start_usb_playback()
573 if (test_bit(DISCONNECTED, &ua->states)) { in start_usb_playback()
575 return -ENODEV; in start_usb_playback()
577 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) { in start_usb_playback()
579 return -EIO; in start_usb_playback()
582 for (i = 0; i < ua->playback.queue_length; ++i) { in start_usb_playback()
584 spin_lock_irq(&ua->lock); in start_usb_playback()
585 frames = ua->rate_feedback[ua->rate_feedback_start]; in start_usb_playback()
586 add_with_wraparound(ua, &ua->rate_feedback_start, 1); in start_usb_playback()
587 ua->rate_feedback_count--; in start_usb_playback()
588 spin_unlock_irq(&ua->lock); in start_usb_playback()
589 urb = &ua->playback.urbs[i]->urb; in start_usb_playback()
590 urb->iso_frame_desc[0].length = in start_usb_playback()
591 frames * ua->playback.frame_bytes; in start_usb_playback()
592 memset(urb->transfer_buffer, 0, in start_usb_playback()
593 urb->iso_frame_desc[0].length); in start_usb_playback()
596 set_bit(USB_PLAYBACK_RUNNING, &ua->states); in start_usb_playback()
597 err = submit_stream_urbs(ua, &ua->playback); in start_usb_playback()
605 if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) in abort_alsa_capture()
606 snd_pcm_stop_xrun(ua->capture.substream); in abort_alsa_capture()
611 if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) in abort_alsa_playback()
612 snd_pcm_stop_xrun(ua->playback.substream); in abort_alsa_playback()
616 unsigned int channels) in set_stream_hw() argument
620 substream->runtime->hw.info = in set_stream_hw()
627 substream->runtime->hw.formats = ua->format_bit; in set_stream_hw()
628 substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate); in set_stream_hw()
629 substream->runtime->hw.rate_min = ua->rate; in set_stream_hw()
630 substream->runtime->hw.rate_max = ua->rate; in set_stream_hw()
631 substream->runtime->hw.channels_min = channels; in set_stream_hw()
632 substream->runtime->hw.channels_max = channels; in set_stream_hw()
633 substream->runtime->hw.buffer_bytes_max = 45000 * 1024; in set_stream_hw()
634 substream->runtime->hw.period_bytes_min = 1; in set_stream_hw()
635 substream->runtime->hw.period_bytes_max = UINT_MAX; in set_stream_hw()
636 substream->runtime->hw.periods_min = 2; in set_stream_hw()
637 substream->runtime->hw.periods_max = UINT_MAX; in set_stream_hw()
638 err = snd_pcm_hw_constraint_minmax(substream->runtime, in set_stream_hw()
640 1500000 / ua->packets_per_second, in set_stream_hw()
644 err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24); in set_stream_hw()
650 struct ua101 *ua = substream->private_data; in capture_pcm_open()
653 ua->capture.substream = substream; in capture_pcm_open()
654 err = set_stream_hw(ua, substream, ua->capture.channels); in capture_pcm_open()
657 substream->runtime->hw.fifo_size = in capture_pcm_open()
658 DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); in capture_pcm_open()
659 substream->runtime->delay = substream->runtime->hw.fifo_size; in capture_pcm_open()
661 mutex_lock(&ua->mutex); in capture_pcm_open()
664 set_bit(ALSA_CAPTURE_OPEN, &ua->states); in capture_pcm_open()
665 mutex_unlock(&ua->mutex); in capture_pcm_open()
671 struct ua101 *ua = substream->private_data; in playback_pcm_open()
674 ua->playback.substream = substream; in playback_pcm_open()
675 err = set_stream_hw(ua, substream, ua->playback.channels); in playback_pcm_open()
678 substream->runtime->hw.fifo_size = in playback_pcm_open()
679 DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, in playback_pcm_open()
680 ua->packets_per_second); in playback_pcm_open()
682 mutex_lock(&ua->mutex); in playback_pcm_open()
688 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) in playback_pcm_open()
692 set_bit(ALSA_PLAYBACK_OPEN, &ua->states); in playback_pcm_open()
694 mutex_unlock(&ua->mutex); in playback_pcm_open()
700 struct ua101 *ua = substream->private_data; in capture_pcm_close()
702 mutex_lock(&ua->mutex); in capture_pcm_close()
703 clear_bit(ALSA_CAPTURE_OPEN, &ua->states); in capture_pcm_close()
704 if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) in capture_pcm_close()
706 mutex_unlock(&ua->mutex); in capture_pcm_close()
712 struct ua101 *ua = substream->private_data; in playback_pcm_close()
714 mutex_lock(&ua->mutex); in playback_pcm_close()
716 clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); in playback_pcm_close()
717 if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) in playback_pcm_close()
719 mutex_unlock(&ua->mutex); in playback_pcm_close()
726 struct ua101 *ua = substream->private_data; in capture_pcm_hw_params()
729 mutex_lock(&ua->mutex); in capture_pcm_hw_params()
731 mutex_unlock(&ua->mutex); in capture_pcm_hw_params()
738 struct ua101 *ua = substream->private_data; in playback_pcm_hw_params()
741 mutex_lock(&ua->mutex); in playback_pcm_hw_params()
745 mutex_unlock(&ua->mutex); in playback_pcm_hw_params()
751 struct ua101 *ua = substream->private_data; in capture_pcm_prepare()
754 mutex_lock(&ua->mutex); in capture_pcm_prepare()
756 mutex_unlock(&ua->mutex); in capture_pcm_prepare()
766 wait_event(ua->alsa_capture_wait, in capture_pcm_prepare()
767 test_bit(CAPTURE_URB_COMPLETED, &ua->states) || in capture_pcm_prepare()
768 !test_bit(USB_CAPTURE_RUNNING, &ua->states)); in capture_pcm_prepare()
769 if (test_bit(DISCONNECTED, &ua->states)) in capture_pcm_prepare()
770 return -ENODEV; in capture_pcm_prepare()
771 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) in capture_pcm_prepare()
772 return -EIO; in capture_pcm_prepare()
774 ua->capture.period_pos = 0; in capture_pcm_prepare()
775 ua->capture.buffer_pos = 0; in capture_pcm_prepare()
781 struct ua101 *ua = substream->private_data; in playback_pcm_prepare()
784 mutex_lock(&ua->mutex); in playback_pcm_prepare()
788 mutex_unlock(&ua->mutex); in playback_pcm_prepare()
793 wait_event(ua->alsa_playback_wait, in playback_pcm_prepare()
794 test_bit(PLAYBACK_URB_COMPLETED, &ua->states) || in playback_pcm_prepare()
795 !test_bit(USB_PLAYBACK_RUNNING, &ua->states)); in playback_pcm_prepare()
796 if (test_bit(DISCONNECTED, &ua->states)) in playback_pcm_prepare()
797 return -ENODEV; in playback_pcm_prepare()
798 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) in playback_pcm_prepare()
799 return -EIO; in playback_pcm_prepare()
801 substream->runtime->delay = 0; in playback_pcm_prepare()
802 ua->playback.period_pos = 0; in playback_pcm_prepare()
803 ua->playback.buffer_pos = 0; in playback_pcm_prepare()
809 struct ua101 *ua = substream->private_data; in capture_pcm_trigger()
813 if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) in capture_pcm_trigger()
814 return -EIO; in capture_pcm_trigger()
815 set_bit(ALSA_CAPTURE_RUNNING, &ua->states); in capture_pcm_trigger()
818 clear_bit(ALSA_CAPTURE_RUNNING, &ua->states); in capture_pcm_trigger()
821 return -EINVAL; in capture_pcm_trigger()
827 struct ua101 *ua = substream->private_data; in playback_pcm_trigger()
831 if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) in playback_pcm_trigger()
832 return -EIO; in playback_pcm_trigger()
833 set_bit(ALSA_PLAYBACK_RUNNING, &ua->states); in playback_pcm_trigger()
836 clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states); in playback_pcm_trigger()
839 return -EINVAL; in playback_pcm_trigger()
849 spin_lock_irqsave(&ua->lock, flags); in ua101_pcm_pointer()
850 pos = stream->buffer_pos; in ua101_pcm_pointer()
851 spin_unlock_irqrestore(&ua->lock, flags); in ua101_pcm_pointer()
857 struct ua101 *ua = subs->private_data; in capture_pcm_pointer()
859 return ua101_pcm_pointer(ua, &ua->capture); in capture_pcm_pointer()
864 struct ua101 *ua = subs->private_data; in playback_pcm_pointer()
866 return ua101_pcm_pointer(ua, &ua->playback); in playback_pcm_pointer()
894 if (interface->num_altsetting != 2) { in find_format_descriptor()
895 dev_err(&interface->dev, "invalid num_altsetting\n"); in find_format_descriptor()
899 alt = &interface->altsetting[0]; in find_format_descriptor()
900 if (alt->desc.bNumEndpoints != 0) { in find_format_descriptor()
901 dev_err(&interface->dev, "invalid bNumEndpoints\n"); in find_format_descriptor()
905 alt = &interface->altsetting[1]; in find_format_descriptor()
906 if (alt->desc.bNumEndpoints != 1) { in find_format_descriptor()
907 dev_err(&interface->dev, "invalid bNumEndpoints\n"); in find_format_descriptor()
911 extra = alt->extra; in find_format_descriptor()
912 extralen = alt->extralen; in find_format_descriptor()
917 if (desc->bLength > extralen) { in find_format_descriptor()
918 dev_err(&interface->dev, "descriptor overflow\n"); in find_format_descriptor()
921 if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) && in find_format_descriptor()
922 desc->bDescriptorType == USB_DT_CS_INTERFACE && in find_format_descriptor()
923 desc->bDescriptorSubtype == UAC_FORMAT_TYPE) { in find_format_descriptor()
924 if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM || in find_format_descriptor()
925 desc->bSamFreqType != 1) { in find_format_descriptor()
926 dev_err(&interface->dev, in find_format_descriptor()
932 extralen -= desc->bLength; in find_format_descriptor()
933 extra += desc->bLength; in find_format_descriptor()
935 dev_err(&interface->dev, "sample format descriptor not found\n"); in find_format_descriptor()
946 fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]); in detect_usb_format()
947 fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]); in detect_usb_format()
949 return -ENXIO; in detect_usb_format()
951 switch (fmt_capture->bSubframeSize) { in detect_usb_format()
953 ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE; in detect_usb_format()
956 ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE; in detect_usb_format()
959 dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n"); in detect_usb_format()
960 return -ENXIO; in detect_usb_format()
962 if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) { in detect_usb_format()
963 dev_err(&ua->dev->dev, in detect_usb_format()
964 "playback/capture sample widths do not match\n"); in detect_usb_format()
965 return -ENXIO; in detect_usb_format()
968 if (fmt_capture->bBitResolution != 24 || in detect_usb_format()
969 fmt_playback->bBitResolution != 24) { in detect_usb_format()
970 dev_err(&ua->dev->dev, "sample width is not 24 bits\n"); in detect_usb_format()
971 return -ENXIO; in detect_usb_format()
974 ua->rate = combine_triple(fmt_capture->tSamFreq[0]); in detect_usb_format()
975 rate2 = combine_triple(fmt_playback->tSamFreq[0]); in detect_usb_format()
976 if (ua->rate != rate2) { in detect_usb_format()
977 dev_err(&ua->dev->dev, in detect_usb_format()
978 "playback/capture rates do not match: %u/%u\n", in detect_usb_format()
979 rate2, ua->rate); in detect_usb_format()
980 return -ENXIO; in detect_usb_format()
983 switch (ua->dev->speed) { in detect_usb_format()
985 ua->packets_per_second = 1000; in detect_usb_format()
988 ua->packets_per_second = 8000; in detect_usb_format()
991 dev_err(&ua->dev->dev, "unknown device speed\n"); in detect_usb_format()
992 return -ENXIO; in detect_usb_format()
995 ua->capture.channels = fmt_capture->bNrChannels; in detect_usb_format()
996 ua->playback.channels = fmt_playback->bNrChannels; in detect_usb_format()
997 ua->capture.frame_bytes = in detect_usb_format()
998 fmt_capture->bSubframeSize * ua->capture.channels; in detect_usb_format()
999 ua->playback.frame_bytes = in detect_usb_format()
1000 fmt_playback->bSubframeSize * ua->playback.channels; in detect_usb_format()
1002 epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc; in detect_usb_format()
1004 dev_err(&ua->dev->dev, "invalid capture endpoint\n"); in detect_usb_format()
1005 return -ENXIO; in detect_usb_format()
1007 ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd)); in detect_usb_format()
1008 ua->capture.max_packet_bytes = usb_endpoint_maxp(epd); in detect_usb_format()
1010 epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc; in detect_usb_format()
1012 dev_err(&ua->dev->dev, "invalid playback endpoint\n"); in detect_usb_format()
1013 return -ENXIO; in detect_usb_format()
1015 ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd)); in detect_usb_format()
1016 ua->playback.max_packet_bytes = usb_endpoint_maxp(epd); in detect_usb_format()
1025 stream->queue_length = queue_length; in alloc_stream_buffers()
1026 stream->queue_length = max(stream->queue_length, in alloc_stream_buffers()
1028 stream->queue_length = min(stream->queue_length, in alloc_stream_buffers()
1037 remaining_packets = stream->queue_length; in alloc_stream_buffers()
1038 packets_per_page = PAGE_SIZE / stream->max_packet_bytes; in alloc_stream_buffers()
1039 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) { in alloc_stream_buffers()
1041 size = packets * stream->max_packet_bytes; in alloc_stream_buffers()
1042 stream->buffers[i].addr = in alloc_stream_buffers()
1043 usb_alloc_coherent(ua->dev, size, GFP_KERNEL, in alloc_stream_buffers()
1044 &stream->buffers[i].dma); in alloc_stream_buffers()
1045 if (!stream->buffers[i].addr) in alloc_stream_buffers()
1046 return -ENOMEM; in alloc_stream_buffers()
1047 stream->buffers[i].size = size; in alloc_stream_buffers()
1048 remaining_packets -= packets; in alloc_stream_buffers()
1053 dev_err(&ua->dev->dev, "too many packets\n"); in alloc_stream_buffers()
1054 return -ENXIO; in alloc_stream_buffers()
1063 for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) in free_stream_buffers()
1064 usb_free_coherent(ua->dev, in free_stream_buffers()
1065 stream->buffers[i].size, in free_stream_buffers()
1066 stream->buffers[i].addr, in free_stream_buffers()
1067 stream->buffers[i].dma); in free_stream_buffers()
1073 unsigned max_packet_size = stream->max_packet_bytes; in alloc_stream_urbs()
1077 for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) { in alloc_stream_urbs()
1078 unsigned int size = stream->buffers[b].size; in alloc_stream_urbs()
1079 u8 *addr = stream->buffers[b].addr; in alloc_stream_urbs()
1080 dma_addr_t dma = stream->buffers[b].dma; in alloc_stream_urbs()
1083 if (u >= stream->queue_length) in alloc_stream_urbs()
1087 return -ENOMEM; in alloc_stream_urbs()
1088 usb_init_urb(&urb->urb); in alloc_stream_urbs()
1089 urb->urb.dev = ua->dev; in alloc_stream_urbs()
1090 urb->urb.pipe = stream->usb_pipe; in alloc_stream_urbs()
1091 urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP; in alloc_stream_urbs()
1092 urb->urb.transfer_buffer = addr; in alloc_stream_urbs()
1093 urb->urb.transfer_dma = dma; in alloc_stream_urbs()
1094 urb->urb.transfer_buffer_length = max_packet_size; in alloc_stream_urbs()
1095 urb->urb.number_of_packets = 1; in alloc_stream_urbs()
1096 urb->urb.interval = 1; in alloc_stream_urbs()
1097 urb->urb.context = ua; in alloc_stream_urbs()
1098 urb->urb.complete = urb_complete; in alloc_stream_urbs()
1099 urb->urb.iso_frame_desc[0].offset = 0; in alloc_stream_urbs()
1100 urb->urb.iso_frame_desc[0].length = max_packet_size; in alloc_stream_urbs()
1101 stream->urbs[u++] = urb; in alloc_stream_urbs()
1102 size -= max_packet_size; in alloc_stream_urbs()
1107 if (u == stream->queue_length) in alloc_stream_urbs()
1110 dev_err(&ua->dev->dev, "internal buffer size error\n"); in alloc_stream_urbs()
1111 return -ENXIO; in alloc_stream_urbs()
1118 for (i = 0; i < stream->queue_length; ++i) { in free_stream_urbs()
1119 kfree(stream->urbs[i]); in free_stream_urbs()
1120 stream->urbs[i] = NULL; in free_stream_urbs()
1130 mutex_lock(&ua->mutex); in free_usb_related_resources()
1131 free_stream_urbs(&ua->capture); in free_usb_related_resources()
1132 free_stream_urbs(&ua->playback); in free_usb_related_resources()
1133 mutex_unlock(&ua->mutex); in free_usb_related_resources()
1134 free_stream_buffers(ua, &ua->capture); in free_usb_related_resources()
1135 free_stream_buffers(ua, &ua->playback); in free_usb_related_resources()
1137 for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) { in free_usb_related_resources()
1138 mutex_lock(&ua->mutex); in free_usb_related_resources()
1139 intf = ua->intf[i]; in free_usb_related_resources()
1140 ua->intf[i] = NULL; in free_usb_related_resources()
1141 mutex_unlock(&ua->mutex); in free_usb_related_resources()
1153 struct ua101 *ua = card->private_data; in ua101_card_free()
1155 mutex_destroy(&ua->mutex); in ua101_card_free()
1170 { /* UA-101 */ in ua101_probe()
1175 { /* UA-1000 */ in ua101_probe()
1189 is_ua1000 = usb_id->idProduct == 0x0044; in ua101_probe()
1191 if (interface->altsetting->desc.bInterfaceNumber != in ua101_probe()
1193 return -ENODEV; in ua101_probe()
1202 return -ENOENT; in ua101_probe()
1204 err = snd_card_new(&interface->dev, in ua101_probe()
1211 card->private_free = ua101_card_free; in ua101_probe()
1212 ua = card->private_data; in ua101_probe()
1213 ua->dev = interface_to_usbdev(interface); in ua101_probe()
1214 ua->card = card; in ua101_probe()
1215 ua->card_index = card_index; in ua101_probe()
1216 INIT_LIST_HEAD(&ua->midi_list); in ua101_probe()
1217 spin_lock_init(&ua->lock); in ua101_probe()
1218 mutex_init(&ua->mutex); in ua101_probe()
1219 INIT_LIST_HEAD(&ua->ready_playback_urbs); in ua101_probe()
1220 INIT_WORK(&ua->playback_work, playback_work); in ua101_probe()
1221 init_waitqueue_head(&ua->alsa_capture_wait); in ua101_probe()
1222 init_waitqueue_head(&ua->rate_feedback_wait); in ua101_probe()
1223 init_waitqueue_head(&ua->alsa_playback_wait); in ua101_probe()
1225 ua->intf[0] = interface; in ua101_probe()
1226 for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) { in ua101_probe()
1227 ua->intf[i] = usb_ifnum_to_if(ua->dev, in ua101_probe()
1229 if (!ua->intf[i]) { in ua101_probe()
1230 dev_err(&ua->dev->dev, "interface %u not found\n", in ua101_probe()
1232 err = -ENXIO; in ua101_probe()
1236 ua->intf[i], ua); in ua101_probe()
1238 ua->intf[i] = NULL; in ua101_probe()
1239 err = -EBUSY; in ua101_probe()
1248 name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101"; in ua101_probe()
1249 strcpy(card->driver, "UA-101"); in ua101_probe()
1250 strcpy(card->shortname, name); in ua101_probe()
1251 usb_make_path(ua->dev, usb_path, sizeof(usb_path)); in ua101_probe()
1252 snprintf(ua->card->longname, sizeof(ua->card->longname), in ua101_probe()
1254 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path, in ua101_probe()
1255 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full"); in ua101_probe()
1257 err = alloc_stream_buffers(ua, &ua->capture); in ua101_probe()
1260 err = alloc_stream_buffers(ua, &ua->playback); in ua101_probe()
1264 err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete); in ua101_probe()
1267 err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete); in ua101_probe()
1271 err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm); in ua101_probe()
1274 ua->pcm->private_data = ua; in ua101_probe()
1275 strcpy(ua->pcm->name, name); in ua101_probe()
1276 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops); in ua101_probe()
1277 snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops); in ua101_probe()
1278 snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC, in ua101_probe()
1281 err = snd_usbmidi_create(card, ua->intf[INTF_MIDI], in ua101_probe()
1282 &ua->midi_list, &midi_quirk); in ua101_probe()
1313 set_bit(DISCONNECTED, &ua->states); in ua101_disconnect()
1314 wake_up(&ua->rate_feedback_wait); in ua101_disconnect()
1317 snd_card_disconnect(ua->card); in ua101_disconnect()
1320 list_for_each(midi, &ua->midi_list) in ua101_disconnect()
1324 mutex_lock(&ua->mutex); in ua101_disconnect()
1327 mutex_unlock(&ua->mutex); in ua101_disconnect()
1331 devices_used &= ~(1 << ua->card_index); in ua101_disconnect()
1333 snd_card_free_when_closed(ua->card); in ua101_disconnect()
1339 { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
1340 { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
1341 { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
1347 .name = "snd-ua101",