18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Edirol UA-101/UA-1000 driver 48c2ecf20Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/init.h> 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/slab.h> 108c2ecf20Sopenharmony_ci#include <linux/usb.h> 118c2ecf20Sopenharmony_ci#include <linux/usb/audio.h> 128c2ecf20Sopenharmony_ci#include <sound/core.h> 138c2ecf20Sopenharmony_ci#include <sound/initval.h> 148c2ecf20Sopenharmony_ci#include <sound/pcm.h> 158c2ecf20Sopenharmony_ci#include <sound/pcm_params.h> 168c2ecf20Sopenharmony_ci#include "../usbaudio.h" 178c2ecf20Sopenharmony_ci#include "../midi.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Edirol UA-101/1000 driver"); 208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 228c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}"); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* 258c2ecf20Sopenharmony_ci * Should not be lower than the minimum scheduling delay of the host 268c2ecf20Sopenharmony_ci * controller. Some Intel controllers need more than one frame; as long as 278c2ecf20Sopenharmony_ci * that driver doesn't tell us about this, use 1.5 frames just to be sure. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci#define MIN_QUEUE_LENGTH 12 308c2ecf20Sopenharmony_ci/* Somewhat random. */ 318c2ecf20Sopenharmony_ci#define MAX_QUEUE_LENGTH 30 328c2ecf20Sopenharmony_ci/* 338c2ecf20Sopenharmony_ci * This magic value optimizes memory usage efficiency for the UA-101's packet 348c2ecf20Sopenharmony_ci * sizes at all sample rates, taking into account the stupid cache pool sizes 358c2ecf20Sopenharmony_ci * that usb_alloc_coherent() uses. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci#define DEFAULT_QUEUE_LENGTH 21 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define MAX_PACKET_SIZE 672 /* hardware specific */ 408c2ecf20Sopenharmony_ci#define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \ 418c2ecf20Sopenharmony_ci PAGE_SIZE / MAX_PACKET_SIZE) 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 448c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 458c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 468c2ecf20Sopenharmony_cistatic unsigned int queue_length = 21; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444); 498c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "card index"); 508c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444); 518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string"); 528c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444); 538c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "enable card"); 548c2ecf20Sopenharmony_cimodule_param(queue_length, uint, 0644); 558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(queue_length, "USB queue length in microframes, " 568c2ecf20Sopenharmony_ci __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH)); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cienum { 598c2ecf20Sopenharmony_ci INTF_PLAYBACK, 608c2ecf20Sopenharmony_ci INTF_CAPTURE, 618c2ecf20Sopenharmony_ci INTF_MIDI, 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci INTF_COUNT 648c2ecf20Sopenharmony_ci}; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* bits in struct ua101::states */ 678c2ecf20Sopenharmony_cienum { 688c2ecf20Sopenharmony_ci USB_CAPTURE_RUNNING, 698c2ecf20Sopenharmony_ci USB_PLAYBACK_RUNNING, 708c2ecf20Sopenharmony_ci ALSA_CAPTURE_OPEN, 718c2ecf20Sopenharmony_ci ALSA_PLAYBACK_OPEN, 728c2ecf20Sopenharmony_ci ALSA_CAPTURE_RUNNING, 738c2ecf20Sopenharmony_ci ALSA_PLAYBACK_RUNNING, 748c2ecf20Sopenharmony_ci CAPTURE_URB_COMPLETED, 758c2ecf20Sopenharmony_ci PLAYBACK_URB_COMPLETED, 768c2ecf20Sopenharmony_ci DISCONNECTED, 778c2ecf20Sopenharmony_ci}; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistruct ua101 { 808c2ecf20Sopenharmony_ci struct usb_device *dev; 818c2ecf20Sopenharmony_ci struct snd_card *card; 828c2ecf20Sopenharmony_ci struct usb_interface *intf[INTF_COUNT]; 838c2ecf20Sopenharmony_ci int card_index; 848c2ecf20Sopenharmony_ci struct snd_pcm *pcm; 858c2ecf20Sopenharmony_ci struct list_head midi_list; 868c2ecf20Sopenharmony_ci u64 format_bit; 878c2ecf20Sopenharmony_ci unsigned int rate; 888c2ecf20Sopenharmony_ci unsigned int packets_per_second; 898c2ecf20Sopenharmony_ci spinlock_t lock; 908c2ecf20Sopenharmony_ci struct mutex mutex; 918c2ecf20Sopenharmony_ci unsigned long states; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci /* FIFO to synchronize playback rate to capture rate */ 948c2ecf20Sopenharmony_ci unsigned int rate_feedback_start; 958c2ecf20Sopenharmony_ci unsigned int rate_feedback_count; 968c2ecf20Sopenharmony_ci u8 rate_feedback[MAX_QUEUE_LENGTH]; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci struct list_head ready_playback_urbs; 998c2ecf20Sopenharmony_ci struct work_struct playback_work; 1008c2ecf20Sopenharmony_ci wait_queue_head_t alsa_capture_wait; 1018c2ecf20Sopenharmony_ci wait_queue_head_t rate_feedback_wait; 1028c2ecf20Sopenharmony_ci wait_queue_head_t alsa_playback_wait; 1038c2ecf20Sopenharmony_ci struct ua101_stream { 1048c2ecf20Sopenharmony_ci struct snd_pcm_substream *substream; 1058c2ecf20Sopenharmony_ci unsigned int usb_pipe; 1068c2ecf20Sopenharmony_ci unsigned int channels; 1078c2ecf20Sopenharmony_ci unsigned int frame_bytes; 1088c2ecf20Sopenharmony_ci unsigned int max_packet_bytes; 1098c2ecf20Sopenharmony_ci unsigned int period_pos; 1108c2ecf20Sopenharmony_ci unsigned int buffer_pos; 1118c2ecf20Sopenharmony_ci unsigned int queue_length; 1128c2ecf20Sopenharmony_ci struct ua101_urb { 1138c2ecf20Sopenharmony_ci struct urb urb; 1148c2ecf20Sopenharmony_ci struct usb_iso_packet_descriptor iso_frame_desc[1]; 1158c2ecf20Sopenharmony_ci struct list_head ready_list; 1168c2ecf20Sopenharmony_ci } *urbs[MAX_QUEUE_LENGTH]; 1178c2ecf20Sopenharmony_ci struct { 1188c2ecf20Sopenharmony_ci unsigned int size; 1198c2ecf20Sopenharmony_ci void *addr; 1208c2ecf20Sopenharmony_ci dma_addr_t dma; 1218c2ecf20Sopenharmony_ci } buffers[MAX_MEMORY_BUFFERS]; 1228c2ecf20Sopenharmony_ci } capture, playback; 1238c2ecf20Sopenharmony_ci}; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(devices_mutex); 1268c2ecf20Sopenharmony_cistatic unsigned int devices_used; 1278c2ecf20Sopenharmony_cistatic struct usb_driver ua101_driver; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic void abort_alsa_playback(struct ua101 *ua); 1308c2ecf20Sopenharmony_cistatic void abort_alsa_capture(struct ua101 *ua); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic const char *usb_error_string(int err) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci switch (err) { 1358c2ecf20Sopenharmony_ci case -ENODEV: 1368c2ecf20Sopenharmony_ci return "no device"; 1378c2ecf20Sopenharmony_ci case -ENOENT: 1388c2ecf20Sopenharmony_ci return "endpoint not enabled"; 1398c2ecf20Sopenharmony_ci case -EPIPE: 1408c2ecf20Sopenharmony_ci return "endpoint stalled"; 1418c2ecf20Sopenharmony_ci case -ENOSPC: 1428c2ecf20Sopenharmony_ci return "not enough bandwidth"; 1438c2ecf20Sopenharmony_ci case -ESHUTDOWN: 1448c2ecf20Sopenharmony_ci return "device disabled"; 1458c2ecf20Sopenharmony_ci case -EHOSTUNREACH: 1468c2ecf20Sopenharmony_ci return "device suspended"; 1478c2ecf20Sopenharmony_ci case -EINVAL: 1488c2ecf20Sopenharmony_ci case -EAGAIN: 1498c2ecf20Sopenharmony_ci case -EFBIG: 1508c2ecf20Sopenharmony_ci case -EMSGSIZE: 1518c2ecf20Sopenharmony_ci return "internal error"; 1528c2ecf20Sopenharmony_ci default: 1538c2ecf20Sopenharmony_ci return "unknown error"; 1548c2ecf20Sopenharmony_ci } 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic void abort_usb_capture(struct ua101 *ua) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) { 1608c2ecf20Sopenharmony_ci wake_up(&ua->alsa_capture_wait); 1618c2ecf20Sopenharmony_ci wake_up(&ua->rate_feedback_wait); 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic void abort_usb_playback(struct ua101 *ua) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states)) 1688c2ecf20Sopenharmony_ci wake_up(&ua->alsa_playback_wait); 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cistatic void playback_urb_complete(struct urb *usb_urb) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci struct ua101_urb *urb = (struct ua101_urb *)usb_urb; 1748c2ecf20Sopenharmony_ci struct ua101 *ua = urb->urb.context; 1758c2ecf20Sopenharmony_ci unsigned long flags; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ 1788c2ecf20Sopenharmony_ci urb->urb.status == -ENODEV || /* device removed */ 1798c2ecf20Sopenharmony_ci urb->urb.status == -ECONNRESET || /* unlinked */ 1808c2ecf20Sopenharmony_ci urb->urb.status == -ESHUTDOWN)) { /* device disabled */ 1818c2ecf20Sopenharmony_ci abort_usb_playback(ua); 1828c2ecf20Sopenharmony_ci abort_alsa_playback(ua); 1838c2ecf20Sopenharmony_ci return; 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { 1878c2ecf20Sopenharmony_ci /* append URB to FIFO */ 1888c2ecf20Sopenharmony_ci spin_lock_irqsave(&ua->lock, flags); 1898c2ecf20Sopenharmony_ci list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); 1908c2ecf20Sopenharmony_ci if (ua->rate_feedback_count > 0) 1918c2ecf20Sopenharmony_ci queue_work(system_highpri_wq, &ua->playback_work); 1928c2ecf20Sopenharmony_ci ua->playback.substream->runtime->delay -= 1938c2ecf20Sopenharmony_ci urb->urb.iso_frame_desc[0].length / 1948c2ecf20Sopenharmony_ci ua->playback.frame_bytes; 1958c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic void first_playback_urb_complete(struct urb *urb) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct ua101 *ua = urb->context; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci urb->complete = playback_urb_complete; 2048c2ecf20Sopenharmony_ci playback_urb_complete(urb); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci set_bit(PLAYBACK_URB_COMPLETED, &ua->states); 2078c2ecf20Sopenharmony_ci wake_up(&ua->alsa_playback_wait); 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci/* copy data from the ALSA ring buffer into the URB buffer */ 2118c2ecf20Sopenharmony_cistatic bool copy_playback_data(struct ua101_stream *stream, struct urb *urb, 2128c2ecf20Sopenharmony_ci unsigned int frames) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime; 2158c2ecf20Sopenharmony_ci unsigned int frame_bytes, frames1; 2168c2ecf20Sopenharmony_ci const u8 *source; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci runtime = stream->substream->runtime; 2198c2ecf20Sopenharmony_ci frame_bytes = stream->frame_bytes; 2208c2ecf20Sopenharmony_ci source = runtime->dma_area + stream->buffer_pos * frame_bytes; 2218c2ecf20Sopenharmony_ci if (stream->buffer_pos + frames <= runtime->buffer_size) { 2228c2ecf20Sopenharmony_ci memcpy(urb->transfer_buffer, source, frames * frame_bytes); 2238c2ecf20Sopenharmony_ci } else { 2248c2ecf20Sopenharmony_ci /* wrap around at end of ring buffer */ 2258c2ecf20Sopenharmony_ci frames1 = runtime->buffer_size - stream->buffer_pos; 2268c2ecf20Sopenharmony_ci memcpy(urb->transfer_buffer, source, frames1 * frame_bytes); 2278c2ecf20Sopenharmony_ci memcpy(urb->transfer_buffer + frames1 * frame_bytes, 2288c2ecf20Sopenharmony_ci runtime->dma_area, (frames - frames1) * frame_bytes); 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci stream->buffer_pos += frames; 2328c2ecf20Sopenharmony_ci if (stream->buffer_pos >= runtime->buffer_size) 2338c2ecf20Sopenharmony_ci stream->buffer_pos -= runtime->buffer_size; 2348c2ecf20Sopenharmony_ci stream->period_pos += frames; 2358c2ecf20Sopenharmony_ci if (stream->period_pos >= runtime->period_size) { 2368c2ecf20Sopenharmony_ci stream->period_pos -= runtime->period_size; 2378c2ecf20Sopenharmony_ci return true; 2388c2ecf20Sopenharmony_ci } 2398c2ecf20Sopenharmony_ci return false; 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_cistatic inline void add_with_wraparound(struct ua101 *ua, 2438c2ecf20Sopenharmony_ci unsigned int *value, unsigned int add) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci *value += add; 2468c2ecf20Sopenharmony_ci if (*value >= ua->playback.queue_length) 2478c2ecf20Sopenharmony_ci *value -= ua->playback.queue_length; 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic void playback_work(struct work_struct *work) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci struct ua101 *ua = container_of(work, struct ua101, playback_work); 2538c2ecf20Sopenharmony_ci unsigned long flags; 2548c2ecf20Sopenharmony_ci unsigned int frames; 2558c2ecf20Sopenharmony_ci struct ua101_urb *urb; 2568c2ecf20Sopenharmony_ci bool do_period_elapsed = false; 2578c2ecf20Sopenharmony_ci int err; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states))) 2608c2ecf20Sopenharmony_ci return; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci /* 2638c2ecf20Sopenharmony_ci * Synchronizing the playback rate to the capture rate is done by using 2648c2ecf20Sopenharmony_ci * the same sequence of packet sizes for both streams. 2658c2ecf20Sopenharmony_ci * Submitting a playback URB therefore requires both a ready URB and 2668c2ecf20Sopenharmony_ci * the size of the corresponding capture packet, i.e., both playback 2678c2ecf20Sopenharmony_ci * and capture URBs must have been completed. Since the USB core does 2688c2ecf20Sopenharmony_ci * not guarantee that playback and capture complete callbacks are 2698c2ecf20Sopenharmony_ci * called alternately, we use two FIFOs for packet sizes and read URBs; 2708c2ecf20Sopenharmony_ci * submitting playback URBs is possible as long as both FIFOs are 2718c2ecf20Sopenharmony_ci * nonempty. 2728c2ecf20Sopenharmony_ci */ 2738c2ecf20Sopenharmony_ci spin_lock_irqsave(&ua->lock, flags); 2748c2ecf20Sopenharmony_ci while (ua->rate_feedback_count > 0 && 2758c2ecf20Sopenharmony_ci !list_empty(&ua->ready_playback_urbs)) { 2768c2ecf20Sopenharmony_ci /* take packet size out of FIFO */ 2778c2ecf20Sopenharmony_ci frames = ua->rate_feedback[ua->rate_feedback_start]; 2788c2ecf20Sopenharmony_ci add_with_wraparound(ua, &ua->rate_feedback_start, 1); 2798c2ecf20Sopenharmony_ci ua->rate_feedback_count--; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* take URB out of FIFO */ 2828c2ecf20Sopenharmony_ci urb = list_first_entry(&ua->ready_playback_urbs, 2838c2ecf20Sopenharmony_ci struct ua101_urb, ready_list); 2848c2ecf20Sopenharmony_ci list_del(&urb->ready_list); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci /* fill packet with data or silence */ 2878c2ecf20Sopenharmony_ci urb->urb.iso_frame_desc[0].length = 2888c2ecf20Sopenharmony_ci frames * ua->playback.frame_bytes; 2898c2ecf20Sopenharmony_ci if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 2908c2ecf20Sopenharmony_ci do_period_elapsed |= copy_playback_data(&ua->playback, 2918c2ecf20Sopenharmony_ci &urb->urb, 2928c2ecf20Sopenharmony_ci frames); 2938c2ecf20Sopenharmony_ci else 2948c2ecf20Sopenharmony_ci memset(urb->urb.transfer_buffer, 0, 2958c2ecf20Sopenharmony_ci urb->urb.iso_frame_desc[0].length); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci /* and off you go ... */ 2988c2ecf20Sopenharmony_ci err = usb_submit_urb(&urb->urb, GFP_ATOMIC); 2998c2ecf20Sopenharmony_ci if (unlikely(err < 0)) { 3008c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 3018c2ecf20Sopenharmony_ci abort_usb_playback(ua); 3028c2ecf20Sopenharmony_ci abort_alsa_playback(ua); 3038c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "USB request error %d: %s\n", 3048c2ecf20Sopenharmony_ci err, usb_error_string(err)); 3058c2ecf20Sopenharmony_ci return; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci ua->playback.substream->runtime->delay += frames; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 3108c2ecf20Sopenharmony_ci if (do_period_elapsed) 3118c2ecf20Sopenharmony_ci snd_pcm_period_elapsed(ua->playback.substream); 3128c2ecf20Sopenharmony_ci} 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci/* copy data from the URB buffer into the ALSA ring buffer */ 3158c2ecf20Sopenharmony_cistatic bool copy_capture_data(struct ua101_stream *stream, struct urb *urb, 3168c2ecf20Sopenharmony_ci unsigned int frames) 3178c2ecf20Sopenharmony_ci{ 3188c2ecf20Sopenharmony_ci struct snd_pcm_runtime *runtime; 3198c2ecf20Sopenharmony_ci unsigned int frame_bytes, frames1; 3208c2ecf20Sopenharmony_ci u8 *dest; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci runtime = stream->substream->runtime; 3238c2ecf20Sopenharmony_ci frame_bytes = stream->frame_bytes; 3248c2ecf20Sopenharmony_ci dest = runtime->dma_area + stream->buffer_pos * frame_bytes; 3258c2ecf20Sopenharmony_ci if (stream->buffer_pos + frames <= runtime->buffer_size) { 3268c2ecf20Sopenharmony_ci memcpy(dest, urb->transfer_buffer, frames * frame_bytes); 3278c2ecf20Sopenharmony_ci } else { 3288c2ecf20Sopenharmony_ci /* wrap around at end of ring buffer */ 3298c2ecf20Sopenharmony_ci frames1 = runtime->buffer_size - stream->buffer_pos; 3308c2ecf20Sopenharmony_ci memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes); 3318c2ecf20Sopenharmony_ci memcpy(runtime->dma_area, 3328c2ecf20Sopenharmony_ci urb->transfer_buffer + frames1 * frame_bytes, 3338c2ecf20Sopenharmony_ci (frames - frames1) * frame_bytes); 3348c2ecf20Sopenharmony_ci } 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci stream->buffer_pos += frames; 3378c2ecf20Sopenharmony_ci if (stream->buffer_pos >= runtime->buffer_size) 3388c2ecf20Sopenharmony_ci stream->buffer_pos -= runtime->buffer_size; 3398c2ecf20Sopenharmony_ci stream->period_pos += frames; 3408c2ecf20Sopenharmony_ci if (stream->period_pos >= runtime->period_size) { 3418c2ecf20Sopenharmony_ci stream->period_pos -= runtime->period_size; 3428c2ecf20Sopenharmony_ci return true; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci return false; 3458c2ecf20Sopenharmony_ci} 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_cistatic void capture_urb_complete(struct urb *urb) 3488c2ecf20Sopenharmony_ci{ 3498c2ecf20Sopenharmony_ci struct ua101 *ua = urb->context; 3508c2ecf20Sopenharmony_ci struct ua101_stream *stream = &ua->capture; 3518c2ecf20Sopenharmony_ci unsigned long flags; 3528c2ecf20Sopenharmony_ci unsigned int frames, write_ptr; 3538c2ecf20Sopenharmony_ci bool do_period_elapsed; 3548c2ecf20Sopenharmony_ci int err; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci if (unlikely(urb->status == -ENOENT || /* unlinked */ 3578c2ecf20Sopenharmony_ci urb->status == -ENODEV || /* device removed */ 3588c2ecf20Sopenharmony_ci urb->status == -ECONNRESET || /* unlinked */ 3598c2ecf20Sopenharmony_ci urb->status == -ESHUTDOWN)) /* device disabled */ 3608c2ecf20Sopenharmony_ci goto stream_stopped; 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0) 3638c2ecf20Sopenharmony_ci frames = urb->iso_frame_desc[0].actual_length / 3648c2ecf20Sopenharmony_ci stream->frame_bytes; 3658c2ecf20Sopenharmony_ci else 3668c2ecf20Sopenharmony_ci frames = 0; 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci spin_lock_irqsave(&ua->lock, flags); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 3718c2ecf20Sopenharmony_ci do_period_elapsed = copy_capture_data(stream, urb, frames); 3728c2ecf20Sopenharmony_ci else 3738c2ecf20Sopenharmony_ci do_period_elapsed = false; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 3768c2ecf20Sopenharmony_ci err = usb_submit_urb(urb, GFP_ATOMIC); 3778c2ecf20Sopenharmony_ci if (unlikely(err < 0)) { 3788c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 3798c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "USB request error %d: %s\n", 3808c2ecf20Sopenharmony_ci err, usb_error_string(err)); 3818c2ecf20Sopenharmony_ci goto stream_stopped; 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci /* append packet size to FIFO */ 3858c2ecf20Sopenharmony_ci write_ptr = ua->rate_feedback_start; 3868c2ecf20Sopenharmony_ci add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); 3878c2ecf20Sopenharmony_ci ua->rate_feedback[write_ptr] = frames; 3888c2ecf20Sopenharmony_ci if (ua->rate_feedback_count < ua->playback.queue_length) { 3898c2ecf20Sopenharmony_ci ua->rate_feedback_count++; 3908c2ecf20Sopenharmony_ci if (ua->rate_feedback_count == 3918c2ecf20Sopenharmony_ci ua->playback.queue_length) 3928c2ecf20Sopenharmony_ci wake_up(&ua->rate_feedback_wait); 3938c2ecf20Sopenharmony_ci } else { 3948c2ecf20Sopenharmony_ci /* 3958c2ecf20Sopenharmony_ci * Ring buffer overflow; this happens when the playback 3968c2ecf20Sopenharmony_ci * stream is not running. Throw away the oldest entry, 3978c2ecf20Sopenharmony_ci * so that the playback stream, when it starts, sees 3988c2ecf20Sopenharmony_ci * the most recent packet sizes. 3998c2ecf20Sopenharmony_ci */ 4008c2ecf20Sopenharmony_ci add_with_wraparound(ua, &ua->rate_feedback_start, 1); 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && 4038c2ecf20Sopenharmony_ci !list_empty(&ua->ready_playback_urbs)) 4048c2ecf20Sopenharmony_ci queue_work(system_highpri_wq, &ua->playback_work); 4058c2ecf20Sopenharmony_ci } 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (do_period_elapsed) 4108c2ecf20Sopenharmony_ci snd_pcm_period_elapsed(stream->substream); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci return; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_cistream_stopped: 4158c2ecf20Sopenharmony_ci abort_usb_playback(ua); 4168c2ecf20Sopenharmony_ci abort_usb_capture(ua); 4178c2ecf20Sopenharmony_ci abort_alsa_playback(ua); 4188c2ecf20Sopenharmony_ci abort_alsa_capture(ua); 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_cistatic void first_capture_urb_complete(struct urb *urb) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci struct ua101 *ua = urb->context; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci urb->complete = capture_urb_complete; 4268c2ecf20Sopenharmony_ci capture_urb_complete(urb); 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci set_bit(CAPTURE_URB_COMPLETED, &ua->states); 4298c2ecf20Sopenharmony_ci wake_up(&ua->alsa_capture_wait); 4308c2ecf20Sopenharmony_ci} 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_cistatic int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream) 4338c2ecf20Sopenharmony_ci{ 4348c2ecf20Sopenharmony_ci unsigned int i; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci for (i = 0; i < stream->queue_length; ++i) { 4378c2ecf20Sopenharmony_ci int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL); 4388c2ecf20Sopenharmony_ci if (err < 0) { 4398c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "USB request error %d: %s\n", 4408c2ecf20Sopenharmony_ci err, usb_error_string(err)); 4418c2ecf20Sopenharmony_ci return err; 4428c2ecf20Sopenharmony_ci } 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci return 0; 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic void kill_stream_urbs(struct ua101_stream *stream) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci unsigned int i; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci for (i = 0; i < stream->queue_length; ++i) 4528c2ecf20Sopenharmony_ci if (stream->urbs[i]) 4538c2ecf20Sopenharmony_ci usb_kill_urb(&stream->urbs[i]->urb); 4548c2ecf20Sopenharmony_ci} 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_cistatic int enable_iso_interface(struct ua101 *ua, unsigned int intf_index) 4578c2ecf20Sopenharmony_ci{ 4588c2ecf20Sopenharmony_ci struct usb_host_interface *alts; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci alts = ua->intf[intf_index]->cur_altsetting; 4618c2ecf20Sopenharmony_ci if (alts->desc.bAlternateSetting != 1) { 4628c2ecf20Sopenharmony_ci int err = usb_set_interface(ua->dev, 4638c2ecf20Sopenharmony_ci alts->desc.bInterfaceNumber, 1); 4648c2ecf20Sopenharmony_ci if (err < 0) { 4658c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, 4668c2ecf20Sopenharmony_ci "cannot initialize interface; error %d: %s\n", 4678c2ecf20Sopenharmony_ci err, usb_error_string(err)); 4688c2ecf20Sopenharmony_ci return err; 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci } 4718c2ecf20Sopenharmony_ci return 0; 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_cistatic void disable_iso_interface(struct ua101 *ua, unsigned int intf_index) 4758c2ecf20Sopenharmony_ci{ 4768c2ecf20Sopenharmony_ci struct usb_host_interface *alts; 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci if (!ua->intf[intf_index]) 4798c2ecf20Sopenharmony_ci return; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci alts = ua->intf[intf_index]->cur_altsetting; 4828c2ecf20Sopenharmony_ci if (alts->desc.bAlternateSetting != 0) { 4838c2ecf20Sopenharmony_ci int err = usb_set_interface(ua->dev, 4848c2ecf20Sopenharmony_ci alts->desc.bInterfaceNumber, 0); 4858c2ecf20Sopenharmony_ci if (err < 0 && !test_bit(DISCONNECTED, &ua->states)) 4868c2ecf20Sopenharmony_ci dev_warn(&ua->dev->dev, 4878c2ecf20Sopenharmony_ci "interface reset failed; error %d: %s\n", 4888c2ecf20Sopenharmony_ci err, usb_error_string(err)); 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic void stop_usb_capture(struct ua101 *ua) 4938c2ecf20Sopenharmony_ci{ 4948c2ecf20Sopenharmony_ci clear_bit(USB_CAPTURE_RUNNING, &ua->states); 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci kill_stream_urbs(&ua->capture); 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci disable_iso_interface(ua, INTF_CAPTURE); 4998c2ecf20Sopenharmony_ci} 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cistatic int start_usb_capture(struct ua101 *ua) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci int err; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci if (test_bit(DISCONNECTED, &ua->states)) 5068c2ecf20Sopenharmony_ci return -ENODEV; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) 5098c2ecf20Sopenharmony_ci return 0; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci kill_stream_urbs(&ua->capture); 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci err = enable_iso_interface(ua, INTF_CAPTURE); 5148c2ecf20Sopenharmony_ci if (err < 0) 5158c2ecf20Sopenharmony_ci return err; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci clear_bit(CAPTURE_URB_COMPLETED, &ua->states); 5188c2ecf20Sopenharmony_ci ua->capture.urbs[0]->urb.complete = first_capture_urb_complete; 5198c2ecf20Sopenharmony_ci ua->rate_feedback_start = 0; 5208c2ecf20Sopenharmony_ci ua->rate_feedback_count = 0; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci set_bit(USB_CAPTURE_RUNNING, &ua->states); 5238c2ecf20Sopenharmony_ci err = submit_stream_urbs(ua, &ua->capture); 5248c2ecf20Sopenharmony_ci if (err < 0) 5258c2ecf20Sopenharmony_ci stop_usb_capture(ua); 5268c2ecf20Sopenharmony_ci return err; 5278c2ecf20Sopenharmony_ci} 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_cistatic void stop_usb_playback(struct ua101 *ua) 5308c2ecf20Sopenharmony_ci{ 5318c2ecf20Sopenharmony_ci clear_bit(USB_PLAYBACK_RUNNING, &ua->states); 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci kill_stream_urbs(&ua->playback); 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci cancel_work_sync(&ua->playback_work); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci disable_iso_interface(ua, INTF_PLAYBACK); 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int start_usb_playback(struct ua101 *ua) 5418c2ecf20Sopenharmony_ci{ 5428c2ecf20Sopenharmony_ci unsigned int i, frames; 5438c2ecf20Sopenharmony_ci struct urb *urb; 5448c2ecf20Sopenharmony_ci int err = 0; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci if (test_bit(DISCONNECTED, &ua->states)) 5478c2ecf20Sopenharmony_ci return -ENODEV; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 5508c2ecf20Sopenharmony_ci return 0; 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci kill_stream_urbs(&ua->playback); 5538c2ecf20Sopenharmony_ci cancel_work_sync(&ua->playback_work); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci err = enable_iso_interface(ua, INTF_PLAYBACK); 5568c2ecf20Sopenharmony_ci if (err < 0) 5578c2ecf20Sopenharmony_ci return err; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); 5608c2ecf20Sopenharmony_ci ua->playback.urbs[0]->urb.complete = 5618c2ecf20Sopenharmony_ci first_playback_urb_complete; 5628c2ecf20Sopenharmony_ci spin_lock_irq(&ua->lock); 5638c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ua->ready_playback_urbs); 5648c2ecf20Sopenharmony_ci spin_unlock_irq(&ua->lock); 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci /* 5678c2ecf20Sopenharmony_ci * We submit the initial URBs all at once, so we have to wait for the 5688c2ecf20Sopenharmony_ci * packet size FIFO to be full. 5698c2ecf20Sopenharmony_ci */ 5708c2ecf20Sopenharmony_ci wait_event(ua->rate_feedback_wait, 5718c2ecf20Sopenharmony_ci ua->rate_feedback_count >= ua->playback.queue_length || 5728c2ecf20Sopenharmony_ci !test_bit(USB_CAPTURE_RUNNING, &ua->states) || 5738c2ecf20Sopenharmony_ci test_bit(DISCONNECTED, &ua->states)); 5748c2ecf20Sopenharmony_ci if (test_bit(DISCONNECTED, &ua->states)) { 5758c2ecf20Sopenharmony_ci stop_usb_playback(ua); 5768c2ecf20Sopenharmony_ci return -ENODEV; 5778c2ecf20Sopenharmony_ci } 5788c2ecf20Sopenharmony_ci if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) { 5798c2ecf20Sopenharmony_ci stop_usb_playback(ua); 5808c2ecf20Sopenharmony_ci return -EIO; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci for (i = 0; i < ua->playback.queue_length; ++i) { 5848c2ecf20Sopenharmony_ci /* all initial URBs contain silence */ 5858c2ecf20Sopenharmony_ci spin_lock_irq(&ua->lock); 5868c2ecf20Sopenharmony_ci frames = ua->rate_feedback[ua->rate_feedback_start]; 5878c2ecf20Sopenharmony_ci add_with_wraparound(ua, &ua->rate_feedback_start, 1); 5888c2ecf20Sopenharmony_ci ua->rate_feedback_count--; 5898c2ecf20Sopenharmony_ci spin_unlock_irq(&ua->lock); 5908c2ecf20Sopenharmony_ci urb = &ua->playback.urbs[i]->urb; 5918c2ecf20Sopenharmony_ci urb->iso_frame_desc[0].length = 5928c2ecf20Sopenharmony_ci frames * ua->playback.frame_bytes; 5938c2ecf20Sopenharmony_ci memset(urb->transfer_buffer, 0, 5948c2ecf20Sopenharmony_ci urb->iso_frame_desc[0].length); 5958c2ecf20Sopenharmony_ci } 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci set_bit(USB_PLAYBACK_RUNNING, &ua->states); 5988c2ecf20Sopenharmony_ci err = submit_stream_urbs(ua, &ua->playback); 5998c2ecf20Sopenharmony_ci if (err < 0) 6008c2ecf20Sopenharmony_ci stop_usb_playback(ua); 6018c2ecf20Sopenharmony_ci return err; 6028c2ecf20Sopenharmony_ci} 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_cistatic void abort_alsa_capture(struct ua101 *ua) 6058c2ecf20Sopenharmony_ci{ 6068c2ecf20Sopenharmony_ci if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) 6078c2ecf20Sopenharmony_ci snd_pcm_stop_xrun(ua->capture.substream); 6088c2ecf20Sopenharmony_ci} 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_cistatic void abort_alsa_playback(struct ua101 *ua) 6118c2ecf20Sopenharmony_ci{ 6128c2ecf20Sopenharmony_ci if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) 6138c2ecf20Sopenharmony_ci snd_pcm_stop_xrun(ua->playback.substream); 6148c2ecf20Sopenharmony_ci} 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_cistatic int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream, 6178c2ecf20Sopenharmony_ci unsigned int channels) 6188c2ecf20Sopenharmony_ci{ 6198c2ecf20Sopenharmony_ci int err; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci substream->runtime->hw.info = 6228c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP | 6238c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_MMAP_VALID | 6248c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_BATCH | 6258c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_INTERLEAVED | 6268c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_BLOCK_TRANSFER | 6278c2ecf20Sopenharmony_ci SNDRV_PCM_INFO_FIFO_IN_FRAMES; 6288c2ecf20Sopenharmony_ci substream->runtime->hw.formats = ua->format_bit; 6298c2ecf20Sopenharmony_ci substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate); 6308c2ecf20Sopenharmony_ci substream->runtime->hw.rate_min = ua->rate; 6318c2ecf20Sopenharmony_ci substream->runtime->hw.rate_max = ua->rate; 6328c2ecf20Sopenharmony_ci substream->runtime->hw.channels_min = channels; 6338c2ecf20Sopenharmony_ci substream->runtime->hw.channels_max = channels; 6348c2ecf20Sopenharmony_ci substream->runtime->hw.buffer_bytes_max = 45000 * 1024; 6358c2ecf20Sopenharmony_ci substream->runtime->hw.period_bytes_min = 1; 6368c2ecf20Sopenharmony_ci substream->runtime->hw.period_bytes_max = UINT_MAX; 6378c2ecf20Sopenharmony_ci substream->runtime->hw.periods_min = 2; 6388c2ecf20Sopenharmony_ci substream->runtime->hw.periods_max = UINT_MAX; 6398c2ecf20Sopenharmony_ci err = snd_pcm_hw_constraint_minmax(substream->runtime, 6408c2ecf20Sopenharmony_ci SNDRV_PCM_HW_PARAM_PERIOD_TIME, 6418c2ecf20Sopenharmony_ci 1500000 / ua->packets_per_second, 6428c2ecf20Sopenharmony_ci UINT_MAX); 6438c2ecf20Sopenharmony_ci if (err < 0) 6448c2ecf20Sopenharmony_ci return err; 6458c2ecf20Sopenharmony_ci err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24); 6468c2ecf20Sopenharmony_ci return err; 6478c2ecf20Sopenharmony_ci} 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_cistatic int capture_pcm_open(struct snd_pcm_substream *substream) 6508c2ecf20Sopenharmony_ci{ 6518c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 6528c2ecf20Sopenharmony_ci int err; 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci ua->capture.substream = substream; 6558c2ecf20Sopenharmony_ci err = set_stream_hw(ua, substream, ua->capture.channels); 6568c2ecf20Sopenharmony_ci if (err < 0) 6578c2ecf20Sopenharmony_ci return err; 6588c2ecf20Sopenharmony_ci substream->runtime->hw.fifo_size = 6598c2ecf20Sopenharmony_ci DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); 6608c2ecf20Sopenharmony_ci substream->runtime->delay = substream->runtime->hw.fifo_size; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 6638c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 6648c2ecf20Sopenharmony_ci if (err >= 0) 6658c2ecf20Sopenharmony_ci set_bit(ALSA_CAPTURE_OPEN, &ua->states); 6668c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 6678c2ecf20Sopenharmony_ci return err; 6688c2ecf20Sopenharmony_ci} 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_cistatic int playback_pcm_open(struct snd_pcm_substream *substream) 6718c2ecf20Sopenharmony_ci{ 6728c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 6738c2ecf20Sopenharmony_ci int err; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci ua->playback.substream = substream; 6768c2ecf20Sopenharmony_ci err = set_stream_hw(ua, substream, ua->playback.channels); 6778c2ecf20Sopenharmony_ci if (err < 0) 6788c2ecf20Sopenharmony_ci return err; 6798c2ecf20Sopenharmony_ci substream->runtime->hw.fifo_size = 6808c2ecf20Sopenharmony_ci DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, 6818c2ecf20Sopenharmony_ci ua->packets_per_second); 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 6848c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 6858c2ecf20Sopenharmony_ci if (err < 0) 6868c2ecf20Sopenharmony_ci goto error; 6878c2ecf20Sopenharmony_ci err = start_usb_playback(ua); 6888c2ecf20Sopenharmony_ci if (err < 0) { 6898c2ecf20Sopenharmony_ci if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 6908c2ecf20Sopenharmony_ci stop_usb_capture(ua); 6918c2ecf20Sopenharmony_ci goto error; 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci set_bit(ALSA_PLAYBACK_OPEN, &ua->states); 6948c2ecf20Sopenharmony_cierror: 6958c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 6968c2ecf20Sopenharmony_ci return err; 6978c2ecf20Sopenharmony_ci} 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_cistatic int capture_pcm_close(struct snd_pcm_substream *substream) 7008c2ecf20Sopenharmony_ci{ 7018c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7048c2ecf20Sopenharmony_ci clear_bit(ALSA_CAPTURE_OPEN, &ua->states); 7058c2ecf20Sopenharmony_ci if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) 7068c2ecf20Sopenharmony_ci stop_usb_capture(ua); 7078c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7088c2ecf20Sopenharmony_ci return 0; 7098c2ecf20Sopenharmony_ci} 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_cistatic int playback_pcm_close(struct snd_pcm_substream *substream) 7128c2ecf20Sopenharmony_ci{ 7138c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7168c2ecf20Sopenharmony_ci stop_usb_playback(ua); 7178c2ecf20Sopenharmony_ci clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); 7188c2ecf20Sopenharmony_ci if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) 7198c2ecf20Sopenharmony_ci stop_usb_capture(ua); 7208c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7218c2ecf20Sopenharmony_ci return 0; 7228c2ecf20Sopenharmony_ci} 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_cistatic int capture_pcm_hw_params(struct snd_pcm_substream *substream, 7258c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *hw_params) 7268c2ecf20Sopenharmony_ci{ 7278c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7288c2ecf20Sopenharmony_ci int err; 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7318c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 7328c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7338c2ecf20Sopenharmony_ci return err; 7348c2ecf20Sopenharmony_ci} 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_cistatic int playback_pcm_hw_params(struct snd_pcm_substream *substream, 7378c2ecf20Sopenharmony_ci struct snd_pcm_hw_params *hw_params) 7388c2ecf20Sopenharmony_ci{ 7398c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7408c2ecf20Sopenharmony_ci int err; 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7438c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 7448c2ecf20Sopenharmony_ci if (err >= 0) 7458c2ecf20Sopenharmony_ci err = start_usb_playback(ua); 7468c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7478c2ecf20Sopenharmony_ci return err; 7488c2ecf20Sopenharmony_ci} 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_cistatic int capture_pcm_prepare(struct snd_pcm_substream *substream) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7538c2ecf20Sopenharmony_ci int err; 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7568c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 7578c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7588c2ecf20Sopenharmony_ci if (err < 0) 7598c2ecf20Sopenharmony_ci return err; 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci /* 7628c2ecf20Sopenharmony_ci * The EHCI driver schedules the first packet of an iso stream at 10 ms 7638c2ecf20Sopenharmony_ci * in the future, i.e., no data is actually captured for that long. 7648c2ecf20Sopenharmony_ci * Take the wait here so that the stream is known to be actually 7658c2ecf20Sopenharmony_ci * running when the start trigger has been called. 7668c2ecf20Sopenharmony_ci */ 7678c2ecf20Sopenharmony_ci wait_event(ua->alsa_capture_wait, 7688c2ecf20Sopenharmony_ci test_bit(CAPTURE_URB_COMPLETED, &ua->states) || 7698c2ecf20Sopenharmony_ci !test_bit(USB_CAPTURE_RUNNING, &ua->states)); 7708c2ecf20Sopenharmony_ci if (test_bit(DISCONNECTED, &ua->states)) 7718c2ecf20Sopenharmony_ci return -ENODEV; 7728c2ecf20Sopenharmony_ci if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) 7738c2ecf20Sopenharmony_ci return -EIO; 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_ci ua->capture.period_pos = 0; 7768c2ecf20Sopenharmony_ci ua->capture.buffer_pos = 0; 7778c2ecf20Sopenharmony_ci return 0; 7788c2ecf20Sopenharmony_ci} 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_cistatic int playback_pcm_prepare(struct snd_pcm_substream *substream) 7818c2ecf20Sopenharmony_ci{ 7828c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 7838c2ecf20Sopenharmony_ci int err; 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 7868c2ecf20Sopenharmony_ci err = start_usb_capture(ua); 7878c2ecf20Sopenharmony_ci if (err >= 0) 7888c2ecf20Sopenharmony_ci err = start_usb_playback(ua); 7898c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 7908c2ecf20Sopenharmony_ci if (err < 0) 7918c2ecf20Sopenharmony_ci return err; 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci /* see the comment in capture_pcm_prepare() */ 7948c2ecf20Sopenharmony_ci wait_event(ua->alsa_playback_wait, 7958c2ecf20Sopenharmony_ci test_bit(PLAYBACK_URB_COMPLETED, &ua->states) || 7968c2ecf20Sopenharmony_ci !test_bit(USB_PLAYBACK_RUNNING, &ua->states)); 7978c2ecf20Sopenharmony_ci if (test_bit(DISCONNECTED, &ua->states)) 7988c2ecf20Sopenharmony_ci return -ENODEV; 7998c2ecf20Sopenharmony_ci if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 8008c2ecf20Sopenharmony_ci return -EIO; 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci substream->runtime->delay = 0; 8038c2ecf20Sopenharmony_ci ua->playback.period_pos = 0; 8048c2ecf20Sopenharmony_ci ua->playback.buffer_pos = 0; 8058c2ecf20Sopenharmony_ci return 0; 8068c2ecf20Sopenharmony_ci} 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_cistatic int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 8098c2ecf20Sopenharmony_ci{ 8108c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci switch (cmd) { 8138c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 8148c2ecf20Sopenharmony_ci if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) 8158c2ecf20Sopenharmony_ci return -EIO; 8168c2ecf20Sopenharmony_ci set_bit(ALSA_CAPTURE_RUNNING, &ua->states); 8178c2ecf20Sopenharmony_ci return 0; 8188c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 8198c2ecf20Sopenharmony_ci clear_bit(ALSA_CAPTURE_RUNNING, &ua->states); 8208c2ecf20Sopenharmony_ci return 0; 8218c2ecf20Sopenharmony_ci default: 8228c2ecf20Sopenharmony_ci return -EINVAL; 8238c2ecf20Sopenharmony_ci } 8248c2ecf20Sopenharmony_ci} 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_cistatic int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 8278c2ecf20Sopenharmony_ci{ 8288c2ecf20Sopenharmony_ci struct ua101 *ua = substream->private_data; 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci switch (cmd) { 8318c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_START: 8328c2ecf20Sopenharmony_ci if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) 8338c2ecf20Sopenharmony_ci return -EIO; 8348c2ecf20Sopenharmony_ci set_bit(ALSA_PLAYBACK_RUNNING, &ua->states); 8358c2ecf20Sopenharmony_ci return 0; 8368c2ecf20Sopenharmony_ci case SNDRV_PCM_TRIGGER_STOP: 8378c2ecf20Sopenharmony_ci clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states); 8388c2ecf20Sopenharmony_ci return 0; 8398c2ecf20Sopenharmony_ci default: 8408c2ecf20Sopenharmony_ci return -EINVAL; 8418c2ecf20Sopenharmony_ci } 8428c2ecf20Sopenharmony_ci} 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_cistatic inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua, 8458c2ecf20Sopenharmony_ci struct ua101_stream *stream) 8468c2ecf20Sopenharmony_ci{ 8478c2ecf20Sopenharmony_ci unsigned long flags; 8488c2ecf20Sopenharmony_ci unsigned int pos; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci spin_lock_irqsave(&ua->lock, flags); 8518c2ecf20Sopenharmony_ci pos = stream->buffer_pos; 8528c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ua->lock, flags); 8538c2ecf20Sopenharmony_ci return pos; 8548c2ecf20Sopenharmony_ci} 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs) 8578c2ecf20Sopenharmony_ci{ 8588c2ecf20Sopenharmony_ci struct ua101 *ua = subs->private_data; 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci return ua101_pcm_pointer(ua, &ua->capture); 8618c2ecf20Sopenharmony_ci} 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs) 8648c2ecf20Sopenharmony_ci{ 8658c2ecf20Sopenharmony_ci struct ua101 *ua = subs->private_data; 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci return ua101_pcm_pointer(ua, &ua->playback); 8688c2ecf20Sopenharmony_ci} 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops capture_pcm_ops = { 8718c2ecf20Sopenharmony_ci .open = capture_pcm_open, 8728c2ecf20Sopenharmony_ci .close = capture_pcm_close, 8738c2ecf20Sopenharmony_ci .hw_params = capture_pcm_hw_params, 8748c2ecf20Sopenharmony_ci .prepare = capture_pcm_prepare, 8758c2ecf20Sopenharmony_ci .trigger = capture_pcm_trigger, 8768c2ecf20Sopenharmony_ci .pointer = capture_pcm_pointer, 8778c2ecf20Sopenharmony_ci}; 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops playback_pcm_ops = { 8808c2ecf20Sopenharmony_ci .open = playback_pcm_open, 8818c2ecf20Sopenharmony_ci .close = playback_pcm_close, 8828c2ecf20Sopenharmony_ci .hw_params = playback_pcm_hw_params, 8838c2ecf20Sopenharmony_ci .prepare = playback_pcm_prepare, 8848c2ecf20Sopenharmony_ci .trigger = playback_pcm_trigger, 8858c2ecf20Sopenharmony_ci .pointer = playback_pcm_pointer, 8868c2ecf20Sopenharmony_ci}; 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_cistatic const struct uac_format_type_i_discrete_descriptor * 8898c2ecf20Sopenharmony_cifind_format_descriptor(struct usb_interface *interface) 8908c2ecf20Sopenharmony_ci{ 8918c2ecf20Sopenharmony_ci struct usb_host_interface *alt; 8928c2ecf20Sopenharmony_ci u8 *extra; 8938c2ecf20Sopenharmony_ci int extralen; 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci if (interface->num_altsetting != 2) { 8968c2ecf20Sopenharmony_ci dev_err(&interface->dev, "invalid num_altsetting\n"); 8978c2ecf20Sopenharmony_ci return NULL; 8988c2ecf20Sopenharmony_ci } 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci alt = &interface->altsetting[0]; 9018c2ecf20Sopenharmony_ci if (alt->desc.bNumEndpoints != 0) { 9028c2ecf20Sopenharmony_ci dev_err(&interface->dev, "invalid bNumEndpoints\n"); 9038c2ecf20Sopenharmony_ci return NULL; 9048c2ecf20Sopenharmony_ci } 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci alt = &interface->altsetting[1]; 9078c2ecf20Sopenharmony_ci if (alt->desc.bNumEndpoints != 1) { 9088c2ecf20Sopenharmony_ci dev_err(&interface->dev, "invalid bNumEndpoints\n"); 9098c2ecf20Sopenharmony_ci return NULL; 9108c2ecf20Sopenharmony_ci } 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci extra = alt->extra; 9138c2ecf20Sopenharmony_ci extralen = alt->extralen; 9148c2ecf20Sopenharmony_ci while (extralen >= sizeof(struct usb_descriptor_header)) { 9158c2ecf20Sopenharmony_ci struct uac_format_type_i_discrete_descriptor *desc; 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci desc = (struct uac_format_type_i_discrete_descriptor *)extra; 9188c2ecf20Sopenharmony_ci if (desc->bLength > extralen) { 9198c2ecf20Sopenharmony_ci dev_err(&interface->dev, "descriptor overflow\n"); 9208c2ecf20Sopenharmony_ci return NULL; 9218c2ecf20Sopenharmony_ci } 9228c2ecf20Sopenharmony_ci if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) && 9238c2ecf20Sopenharmony_ci desc->bDescriptorType == USB_DT_CS_INTERFACE && 9248c2ecf20Sopenharmony_ci desc->bDescriptorSubtype == UAC_FORMAT_TYPE) { 9258c2ecf20Sopenharmony_ci if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM || 9268c2ecf20Sopenharmony_ci desc->bSamFreqType != 1) { 9278c2ecf20Sopenharmony_ci dev_err(&interface->dev, 9288c2ecf20Sopenharmony_ci "invalid format type\n"); 9298c2ecf20Sopenharmony_ci return NULL; 9308c2ecf20Sopenharmony_ci } 9318c2ecf20Sopenharmony_ci return desc; 9328c2ecf20Sopenharmony_ci } 9338c2ecf20Sopenharmony_ci extralen -= desc->bLength; 9348c2ecf20Sopenharmony_ci extra += desc->bLength; 9358c2ecf20Sopenharmony_ci } 9368c2ecf20Sopenharmony_ci dev_err(&interface->dev, "sample format descriptor not found\n"); 9378c2ecf20Sopenharmony_ci return NULL; 9388c2ecf20Sopenharmony_ci} 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_cistatic int detect_usb_format(struct ua101 *ua) 9418c2ecf20Sopenharmony_ci{ 9428c2ecf20Sopenharmony_ci const struct uac_format_type_i_discrete_descriptor *fmt_capture; 9438c2ecf20Sopenharmony_ci const struct uac_format_type_i_discrete_descriptor *fmt_playback; 9448c2ecf20Sopenharmony_ci const struct usb_endpoint_descriptor *epd; 9458c2ecf20Sopenharmony_ci unsigned int rate2; 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]); 9488c2ecf20Sopenharmony_ci fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]); 9498c2ecf20Sopenharmony_ci if (!fmt_capture || !fmt_playback) 9508c2ecf20Sopenharmony_ci return -ENXIO; 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci switch (fmt_capture->bSubframeSize) { 9538c2ecf20Sopenharmony_ci case 3: 9548c2ecf20Sopenharmony_ci ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE; 9558c2ecf20Sopenharmony_ci break; 9568c2ecf20Sopenharmony_ci case 4: 9578c2ecf20Sopenharmony_ci ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE; 9588c2ecf20Sopenharmony_ci break; 9598c2ecf20Sopenharmony_ci default: 9608c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n"); 9618c2ecf20Sopenharmony_ci return -ENXIO; 9628c2ecf20Sopenharmony_ci } 9638c2ecf20Sopenharmony_ci if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) { 9648c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, 9658c2ecf20Sopenharmony_ci "playback/capture sample widths do not match\n"); 9668c2ecf20Sopenharmony_ci return -ENXIO; 9678c2ecf20Sopenharmony_ci } 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci if (fmt_capture->bBitResolution != 24 || 9708c2ecf20Sopenharmony_ci fmt_playback->bBitResolution != 24) { 9718c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "sample width is not 24 bits\n"); 9728c2ecf20Sopenharmony_ci return -ENXIO; 9738c2ecf20Sopenharmony_ci } 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci ua->rate = combine_triple(fmt_capture->tSamFreq[0]); 9768c2ecf20Sopenharmony_ci rate2 = combine_triple(fmt_playback->tSamFreq[0]); 9778c2ecf20Sopenharmony_ci if (ua->rate != rate2) { 9788c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, 9798c2ecf20Sopenharmony_ci "playback/capture rates do not match: %u/%u\n", 9808c2ecf20Sopenharmony_ci rate2, ua->rate); 9818c2ecf20Sopenharmony_ci return -ENXIO; 9828c2ecf20Sopenharmony_ci } 9838c2ecf20Sopenharmony_ci 9848c2ecf20Sopenharmony_ci switch (ua->dev->speed) { 9858c2ecf20Sopenharmony_ci case USB_SPEED_FULL: 9868c2ecf20Sopenharmony_ci ua->packets_per_second = 1000; 9878c2ecf20Sopenharmony_ci break; 9888c2ecf20Sopenharmony_ci case USB_SPEED_HIGH: 9898c2ecf20Sopenharmony_ci ua->packets_per_second = 8000; 9908c2ecf20Sopenharmony_ci break; 9918c2ecf20Sopenharmony_ci default: 9928c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "unknown device speed\n"); 9938c2ecf20Sopenharmony_ci return -ENXIO; 9948c2ecf20Sopenharmony_ci } 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci ua->capture.channels = fmt_capture->bNrChannels; 9978c2ecf20Sopenharmony_ci ua->playback.channels = fmt_playback->bNrChannels; 9988c2ecf20Sopenharmony_ci ua->capture.frame_bytes = 9998c2ecf20Sopenharmony_ci fmt_capture->bSubframeSize * ua->capture.channels; 10008c2ecf20Sopenharmony_ci ua->playback.frame_bytes = 10018c2ecf20Sopenharmony_ci fmt_playback->bSubframeSize * ua->playback.channels; 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc; 10048c2ecf20Sopenharmony_ci if (!usb_endpoint_is_isoc_in(epd) || usb_endpoint_maxp(epd) == 0) { 10058c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "invalid capture endpoint\n"); 10068c2ecf20Sopenharmony_ci return -ENXIO; 10078c2ecf20Sopenharmony_ci } 10088c2ecf20Sopenharmony_ci ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd)); 10098c2ecf20Sopenharmony_ci ua->capture.max_packet_bytes = usb_endpoint_maxp(epd); 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc; 10128c2ecf20Sopenharmony_ci if (!usb_endpoint_is_isoc_out(epd) || usb_endpoint_maxp(epd) == 0) { 10138c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "invalid playback endpoint\n"); 10148c2ecf20Sopenharmony_ci return -ENXIO; 10158c2ecf20Sopenharmony_ci } 10168c2ecf20Sopenharmony_ci ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd)); 10178c2ecf20Sopenharmony_ci ua->playback.max_packet_bytes = usb_endpoint_maxp(epd); 10188c2ecf20Sopenharmony_ci return 0; 10198c2ecf20Sopenharmony_ci} 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_cistatic int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream) 10228c2ecf20Sopenharmony_ci{ 10238c2ecf20Sopenharmony_ci unsigned int remaining_packets, packets, packets_per_page, i; 10248c2ecf20Sopenharmony_ci size_t size; 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci stream->queue_length = queue_length; 10278c2ecf20Sopenharmony_ci stream->queue_length = max(stream->queue_length, 10288c2ecf20Sopenharmony_ci (unsigned int)MIN_QUEUE_LENGTH); 10298c2ecf20Sopenharmony_ci stream->queue_length = min(stream->queue_length, 10308c2ecf20Sopenharmony_ci (unsigned int)MAX_QUEUE_LENGTH); 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci /* 10338c2ecf20Sopenharmony_ci * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are 10348c2ecf20Sopenharmony_ci * quite bad when used with the packet sizes of this device (e.g. 280, 10358c2ecf20Sopenharmony_ci * 520, 624). Therefore, we allocate and subdivide entire pages, using 10368c2ecf20Sopenharmony_ci * a smaller buffer only for the last chunk. 10378c2ecf20Sopenharmony_ci */ 10388c2ecf20Sopenharmony_ci remaining_packets = stream->queue_length; 10398c2ecf20Sopenharmony_ci packets_per_page = PAGE_SIZE / stream->max_packet_bytes; 10408c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) { 10418c2ecf20Sopenharmony_ci packets = min(remaining_packets, packets_per_page); 10428c2ecf20Sopenharmony_ci size = packets * stream->max_packet_bytes; 10438c2ecf20Sopenharmony_ci stream->buffers[i].addr = 10448c2ecf20Sopenharmony_ci usb_alloc_coherent(ua->dev, size, GFP_KERNEL, 10458c2ecf20Sopenharmony_ci &stream->buffers[i].dma); 10468c2ecf20Sopenharmony_ci if (!stream->buffers[i].addr) 10478c2ecf20Sopenharmony_ci return -ENOMEM; 10488c2ecf20Sopenharmony_ci stream->buffers[i].size = size; 10498c2ecf20Sopenharmony_ci remaining_packets -= packets; 10508c2ecf20Sopenharmony_ci if (!remaining_packets) 10518c2ecf20Sopenharmony_ci break; 10528c2ecf20Sopenharmony_ci } 10538c2ecf20Sopenharmony_ci if (remaining_packets) { 10548c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "too many packets\n"); 10558c2ecf20Sopenharmony_ci return -ENXIO; 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci return 0; 10588c2ecf20Sopenharmony_ci} 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_cistatic void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream) 10618c2ecf20Sopenharmony_ci{ 10628c2ecf20Sopenharmony_ci unsigned int i; 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) 10658c2ecf20Sopenharmony_ci usb_free_coherent(ua->dev, 10668c2ecf20Sopenharmony_ci stream->buffers[i].size, 10678c2ecf20Sopenharmony_ci stream->buffers[i].addr, 10688c2ecf20Sopenharmony_ci stream->buffers[i].dma); 10698c2ecf20Sopenharmony_ci} 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_cistatic int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream, 10728c2ecf20Sopenharmony_ci void (*urb_complete)(struct urb *)) 10738c2ecf20Sopenharmony_ci{ 10748c2ecf20Sopenharmony_ci unsigned max_packet_size = stream->max_packet_bytes; 10758c2ecf20Sopenharmony_ci struct ua101_urb *urb; 10768c2ecf20Sopenharmony_ci unsigned int b, u = 0; 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_ci for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) { 10798c2ecf20Sopenharmony_ci unsigned int size = stream->buffers[b].size; 10808c2ecf20Sopenharmony_ci u8 *addr = stream->buffers[b].addr; 10818c2ecf20Sopenharmony_ci dma_addr_t dma = stream->buffers[b].dma; 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci while (size >= max_packet_size) { 10848c2ecf20Sopenharmony_ci if (u >= stream->queue_length) 10858c2ecf20Sopenharmony_ci goto bufsize_error; 10868c2ecf20Sopenharmony_ci urb = kmalloc(sizeof(*urb), GFP_KERNEL); 10878c2ecf20Sopenharmony_ci if (!urb) 10888c2ecf20Sopenharmony_ci return -ENOMEM; 10898c2ecf20Sopenharmony_ci usb_init_urb(&urb->urb); 10908c2ecf20Sopenharmony_ci urb->urb.dev = ua->dev; 10918c2ecf20Sopenharmony_ci urb->urb.pipe = stream->usb_pipe; 10928c2ecf20Sopenharmony_ci urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP; 10938c2ecf20Sopenharmony_ci urb->urb.transfer_buffer = addr; 10948c2ecf20Sopenharmony_ci urb->urb.transfer_dma = dma; 10958c2ecf20Sopenharmony_ci urb->urb.transfer_buffer_length = max_packet_size; 10968c2ecf20Sopenharmony_ci urb->urb.number_of_packets = 1; 10978c2ecf20Sopenharmony_ci urb->urb.interval = 1; 10988c2ecf20Sopenharmony_ci urb->urb.context = ua; 10998c2ecf20Sopenharmony_ci urb->urb.complete = urb_complete; 11008c2ecf20Sopenharmony_ci urb->urb.iso_frame_desc[0].offset = 0; 11018c2ecf20Sopenharmony_ci urb->urb.iso_frame_desc[0].length = max_packet_size; 11028c2ecf20Sopenharmony_ci stream->urbs[u++] = urb; 11038c2ecf20Sopenharmony_ci size -= max_packet_size; 11048c2ecf20Sopenharmony_ci addr += max_packet_size; 11058c2ecf20Sopenharmony_ci dma += max_packet_size; 11068c2ecf20Sopenharmony_ci } 11078c2ecf20Sopenharmony_ci } 11088c2ecf20Sopenharmony_ci if (u == stream->queue_length) 11098c2ecf20Sopenharmony_ci return 0; 11108c2ecf20Sopenharmony_cibufsize_error: 11118c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "internal buffer size error\n"); 11128c2ecf20Sopenharmony_ci return -ENXIO; 11138c2ecf20Sopenharmony_ci} 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_cistatic void free_stream_urbs(struct ua101_stream *stream) 11168c2ecf20Sopenharmony_ci{ 11178c2ecf20Sopenharmony_ci unsigned int i; 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_ci for (i = 0; i < stream->queue_length; ++i) { 11208c2ecf20Sopenharmony_ci kfree(stream->urbs[i]); 11218c2ecf20Sopenharmony_ci stream->urbs[i] = NULL; 11228c2ecf20Sopenharmony_ci } 11238c2ecf20Sopenharmony_ci} 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_cistatic void free_usb_related_resources(struct ua101 *ua, 11268c2ecf20Sopenharmony_ci struct usb_interface *interface) 11278c2ecf20Sopenharmony_ci{ 11288c2ecf20Sopenharmony_ci unsigned int i; 11298c2ecf20Sopenharmony_ci struct usb_interface *intf; 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 11328c2ecf20Sopenharmony_ci free_stream_urbs(&ua->capture); 11338c2ecf20Sopenharmony_ci free_stream_urbs(&ua->playback); 11348c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 11358c2ecf20Sopenharmony_ci free_stream_buffers(ua, &ua->capture); 11368c2ecf20Sopenharmony_ci free_stream_buffers(ua, &ua->playback); 11378c2ecf20Sopenharmony_ci 11388c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) { 11398c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 11408c2ecf20Sopenharmony_ci intf = ua->intf[i]; 11418c2ecf20Sopenharmony_ci ua->intf[i] = NULL; 11428c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 11438c2ecf20Sopenharmony_ci if (intf) { 11448c2ecf20Sopenharmony_ci usb_set_intfdata(intf, NULL); 11458c2ecf20Sopenharmony_ci if (intf != interface) 11468c2ecf20Sopenharmony_ci usb_driver_release_interface(&ua101_driver, 11478c2ecf20Sopenharmony_ci intf); 11488c2ecf20Sopenharmony_ci } 11498c2ecf20Sopenharmony_ci } 11508c2ecf20Sopenharmony_ci} 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_cistatic void ua101_card_free(struct snd_card *card) 11538c2ecf20Sopenharmony_ci{ 11548c2ecf20Sopenharmony_ci struct ua101 *ua = card->private_data; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci mutex_destroy(&ua->mutex); 11578c2ecf20Sopenharmony_ci} 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_cistatic int ua101_probe(struct usb_interface *interface, 11608c2ecf20Sopenharmony_ci const struct usb_device_id *usb_id) 11618c2ecf20Sopenharmony_ci{ 11628c2ecf20Sopenharmony_ci static const struct snd_usb_midi_endpoint_info midi_ep = { 11638c2ecf20Sopenharmony_ci .out_cables = 0x0001, 11648c2ecf20Sopenharmony_ci .in_cables = 0x0001 11658c2ecf20Sopenharmony_ci }; 11668c2ecf20Sopenharmony_ci static const struct snd_usb_audio_quirk midi_quirk = { 11678c2ecf20Sopenharmony_ci .type = QUIRK_MIDI_FIXED_ENDPOINT, 11688c2ecf20Sopenharmony_ci .data = &midi_ep 11698c2ecf20Sopenharmony_ci }; 11708c2ecf20Sopenharmony_ci static const int intf_numbers[2][3] = { 11718c2ecf20Sopenharmony_ci { /* UA-101 */ 11728c2ecf20Sopenharmony_ci [INTF_PLAYBACK] = 0, 11738c2ecf20Sopenharmony_ci [INTF_CAPTURE] = 1, 11748c2ecf20Sopenharmony_ci [INTF_MIDI] = 2, 11758c2ecf20Sopenharmony_ci }, 11768c2ecf20Sopenharmony_ci { /* UA-1000 */ 11778c2ecf20Sopenharmony_ci [INTF_CAPTURE] = 1, 11788c2ecf20Sopenharmony_ci [INTF_PLAYBACK] = 2, 11798c2ecf20Sopenharmony_ci [INTF_MIDI] = 3, 11808c2ecf20Sopenharmony_ci }, 11818c2ecf20Sopenharmony_ci }; 11828c2ecf20Sopenharmony_ci struct snd_card *card; 11838c2ecf20Sopenharmony_ci struct ua101 *ua; 11848c2ecf20Sopenharmony_ci unsigned int card_index, i; 11858c2ecf20Sopenharmony_ci int is_ua1000; 11868c2ecf20Sopenharmony_ci const char *name; 11878c2ecf20Sopenharmony_ci char usb_path[32]; 11888c2ecf20Sopenharmony_ci int err; 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci is_ua1000 = usb_id->idProduct == 0x0044; 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_ci if (interface->altsetting->desc.bInterfaceNumber != 11938c2ecf20Sopenharmony_ci intf_numbers[is_ua1000][0]) 11948c2ecf20Sopenharmony_ci return -ENODEV; 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci mutex_lock(&devices_mutex); 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) 11998c2ecf20Sopenharmony_ci if (enable[card_index] && !(devices_used & (1 << card_index))) 12008c2ecf20Sopenharmony_ci break; 12018c2ecf20Sopenharmony_ci if (card_index >= SNDRV_CARDS) { 12028c2ecf20Sopenharmony_ci mutex_unlock(&devices_mutex); 12038c2ecf20Sopenharmony_ci return -ENOENT; 12048c2ecf20Sopenharmony_ci } 12058c2ecf20Sopenharmony_ci err = snd_card_new(&interface->dev, 12068c2ecf20Sopenharmony_ci index[card_index], id[card_index], THIS_MODULE, 12078c2ecf20Sopenharmony_ci sizeof(*ua), &card); 12088c2ecf20Sopenharmony_ci if (err < 0) { 12098c2ecf20Sopenharmony_ci mutex_unlock(&devices_mutex); 12108c2ecf20Sopenharmony_ci return err; 12118c2ecf20Sopenharmony_ci } 12128c2ecf20Sopenharmony_ci card->private_free = ua101_card_free; 12138c2ecf20Sopenharmony_ci ua = card->private_data; 12148c2ecf20Sopenharmony_ci ua->dev = interface_to_usbdev(interface); 12158c2ecf20Sopenharmony_ci ua->card = card; 12168c2ecf20Sopenharmony_ci ua->card_index = card_index; 12178c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ua->midi_list); 12188c2ecf20Sopenharmony_ci spin_lock_init(&ua->lock); 12198c2ecf20Sopenharmony_ci mutex_init(&ua->mutex); 12208c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&ua->ready_playback_urbs); 12218c2ecf20Sopenharmony_ci INIT_WORK(&ua->playback_work, playback_work); 12228c2ecf20Sopenharmony_ci init_waitqueue_head(&ua->alsa_capture_wait); 12238c2ecf20Sopenharmony_ci init_waitqueue_head(&ua->rate_feedback_wait); 12248c2ecf20Sopenharmony_ci init_waitqueue_head(&ua->alsa_playback_wait); 12258c2ecf20Sopenharmony_ci 12268c2ecf20Sopenharmony_ci ua->intf[0] = interface; 12278c2ecf20Sopenharmony_ci for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) { 12288c2ecf20Sopenharmony_ci ua->intf[i] = usb_ifnum_to_if(ua->dev, 12298c2ecf20Sopenharmony_ci intf_numbers[is_ua1000][i]); 12308c2ecf20Sopenharmony_ci if (!ua->intf[i]) { 12318c2ecf20Sopenharmony_ci dev_err(&ua->dev->dev, "interface %u not found\n", 12328c2ecf20Sopenharmony_ci intf_numbers[is_ua1000][i]); 12338c2ecf20Sopenharmony_ci err = -ENXIO; 12348c2ecf20Sopenharmony_ci goto probe_error; 12358c2ecf20Sopenharmony_ci } 12368c2ecf20Sopenharmony_ci err = usb_driver_claim_interface(&ua101_driver, 12378c2ecf20Sopenharmony_ci ua->intf[i], ua); 12388c2ecf20Sopenharmony_ci if (err < 0) { 12398c2ecf20Sopenharmony_ci ua->intf[i] = NULL; 12408c2ecf20Sopenharmony_ci err = -EBUSY; 12418c2ecf20Sopenharmony_ci goto probe_error; 12428c2ecf20Sopenharmony_ci } 12438c2ecf20Sopenharmony_ci } 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci err = detect_usb_format(ua); 12468c2ecf20Sopenharmony_ci if (err < 0) 12478c2ecf20Sopenharmony_ci goto probe_error; 12488c2ecf20Sopenharmony_ci 12498c2ecf20Sopenharmony_ci name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101"; 12508c2ecf20Sopenharmony_ci strcpy(card->driver, "UA-101"); 12518c2ecf20Sopenharmony_ci strcpy(card->shortname, name); 12528c2ecf20Sopenharmony_ci usb_make_path(ua->dev, usb_path, sizeof(usb_path)); 12538c2ecf20Sopenharmony_ci snprintf(ua->card->longname, sizeof(ua->card->longname), 12548c2ecf20Sopenharmony_ci "EDIROL %s (serial %s), %u Hz at %s, %s speed", name, 12558c2ecf20Sopenharmony_ci ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path, 12568c2ecf20Sopenharmony_ci ua->dev->speed == USB_SPEED_HIGH ? "high" : "full"); 12578c2ecf20Sopenharmony_ci 12588c2ecf20Sopenharmony_ci err = alloc_stream_buffers(ua, &ua->capture); 12598c2ecf20Sopenharmony_ci if (err < 0) 12608c2ecf20Sopenharmony_ci goto probe_error; 12618c2ecf20Sopenharmony_ci err = alloc_stream_buffers(ua, &ua->playback); 12628c2ecf20Sopenharmony_ci if (err < 0) 12638c2ecf20Sopenharmony_ci goto probe_error; 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_ci err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete); 12668c2ecf20Sopenharmony_ci if (err < 0) 12678c2ecf20Sopenharmony_ci goto probe_error; 12688c2ecf20Sopenharmony_ci err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete); 12698c2ecf20Sopenharmony_ci if (err < 0) 12708c2ecf20Sopenharmony_ci goto probe_error; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm); 12738c2ecf20Sopenharmony_ci if (err < 0) 12748c2ecf20Sopenharmony_ci goto probe_error; 12758c2ecf20Sopenharmony_ci ua->pcm->private_data = ua; 12768c2ecf20Sopenharmony_ci strcpy(ua->pcm->name, name); 12778c2ecf20Sopenharmony_ci snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops); 12788c2ecf20Sopenharmony_ci snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops); 12798c2ecf20Sopenharmony_ci snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC, 12808c2ecf20Sopenharmony_ci NULL, 0, 0); 12818c2ecf20Sopenharmony_ci 12828c2ecf20Sopenharmony_ci err = snd_usbmidi_create(card, ua->intf[INTF_MIDI], 12838c2ecf20Sopenharmony_ci &ua->midi_list, &midi_quirk); 12848c2ecf20Sopenharmony_ci if (err < 0) 12858c2ecf20Sopenharmony_ci goto probe_error; 12868c2ecf20Sopenharmony_ci 12878c2ecf20Sopenharmony_ci err = snd_card_register(card); 12888c2ecf20Sopenharmony_ci if (err < 0) 12898c2ecf20Sopenharmony_ci goto probe_error; 12908c2ecf20Sopenharmony_ci 12918c2ecf20Sopenharmony_ci usb_set_intfdata(interface, ua); 12928c2ecf20Sopenharmony_ci devices_used |= 1 << card_index; 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci mutex_unlock(&devices_mutex); 12958c2ecf20Sopenharmony_ci return 0; 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ciprobe_error: 12988c2ecf20Sopenharmony_ci free_usb_related_resources(ua, interface); 12998c2ecf20Sopenharmony_ci snd_card_free(card); 13008c2ecf20Sopenharmony_ci mutex_unlock(&devices_mutex); 13018c2ecf20Sopenharmony_ci return err; 13028c2ecf20Sopenharmony_ci} 13038c2ecf20Sopenharmony_ci 13048c2ecf20Sopenharmony_cistatic void ua101_disconnect(struct usb_interface *interface) 13058c2ecf20Sopenharmony_ci{ 13068c2ecf20Sopenharmony_ci struct ua101 *ua = usb_get_intfdata(interface); 13078c2ecf20Sopenharmony_ci struct list_head *midi; 13088c2ecf20Sopenharmony_ci 13098c2ecf20Sopenharmony_ci if (!ua) 13108c2ecf20Sopenharmony_ci return; 13118c2ecf20Sopenharmony_ci 13128c2ecf20Sopenharmony_ci mutex_lock(&devices_mutex); 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci set_bit(DISCONNECTED, &ua->states); 13158c2ecf20Sopenharmony_ci wake_up(&ua->rate_feedback_wait); 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_ci /* make sure that userspace cannot create new requests */ 13188c2ecf20Sopenharmony_ci snd_card_disconnect(ua->card); 13198c2ecf20Sopenharmony_ci 13208c2ecf20Sopenharmony_ci /* make sure that there are no pending USB requests */ 13218c2ecf20Sopenharmony_ci list_for_each(midi, &ua->midi_list) 13228c2ecf20Sopenharmony_ci snd_usbmidi_disconnect(midi); 13238c2ecf20Sopenharmony_ci abort_alsa_playback(ua); 13248c2ecf20Sopenharmony_ci abort_alsa_capture(ua); 13258c2ecf20Sopenharmony_ci mutex_lock(&ua->mutex); 13268c2ecf20Sopenharmony_ci stop_usb_playback(ua); 13278c2ecf20Sopenharmony_ci stop_usb_capture(ua); 13288c2ecf20Sopenharmony_ci mutex_unlock(&ua->mutex); 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_ci free_usb_related_resources(ua, interface); 13318c2ecf20Sopenharmony_ci 13328c2ecf20Sopenharmony_ci devices_used &= ~(1 << ua->card_index); 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_ci snd_card_free_when_closed(ua->card); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci mutex_unlock(&devices_mutex); 13378c2ecf20Sopenharmony_ci} 13388c2ecf20Sopenharmony_ci 13398c2ecf20Sopenharmony_cistatic const struct usb_device_id ua101_ids[] = { 13408c2ecf20Sopenharmony_ci { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */ 13418c2ecf20Sopenharmony_ci { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */ 13428c2ecf20Sopenharmony_ci { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */ 13438c2ecf20Sopenharmony_ci { } 13448c2ecf20Sopenharmony_ci}; 13458c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, ua101_ids); 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_cistatic struct usb_driver ua101_driver = { 13488c2ecf20Sopenharmony_ci .name = "snd-ua101", 13498c2ecf20Sopenharmony_ci .id_table = ua101_ids, 13508c2ecf20Sopenharmony_ci .probe = ua101_probe, 13518c2ecf20Sopenharmony_ci .disconnect = ua101_disconnect, 13528c2ecf20Sopenharmony_ci#if 0 13538c2ecf20Sopenharmony_ci .suspend = ua101_suspend, 13548c2ecf20Sopenharmony_ci .resume = ua101_resume, 13558c2ecf20Sopenharmony_ci#endif 13568c2ecf20Sopenharmony_ci}; 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_cimodule_usb_driver(ua101_driver); 1359