Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Edirol UA-101/UA-1000 driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/usb/audio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <sound/initval.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <sound/pcm_params.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "../usbaudio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "../midi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) MODULE_DESCRIPTION("Edirol UA-101/1000 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101},{Edirol,UA-1000}}");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * Should not be lower than the minimum scheduling delay of the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * controller.  Some Intel controllers need more than one frame; as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * that driver doesn't tell us about this, use 1.5 frames just to be sure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define MIN_QUEUE_LENGTH	12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) /* Somewhat random. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define MAX_QUEUE_LENGTH	30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * This magic value optimizes memory usage efficiency for the UA-101's packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * sizes at all sample rates, taking into account the stupid cache pool sizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * that usb_alloc_coherent() uses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define DEFAULT_QUEUE_LENGTH	21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define MAX_PACKET_SIZE		672 /* hardware specific */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define MAX_MEMORY_BUFFERS	DIV_ROUND_UP(MAX_QUEUE_LENGTH, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 					     PAGE_SIZE / MAX_PACKET_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static unsigned int queue_length = 21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) module_param_array(index, int, NULL, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) MODULE_PARM_DESC(index, "card index");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) module_param_array(id, charp, NULL, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) MODULE_PARM_DESC(id, "ID string");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) module_param_array(enable, bool, NULL, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) MODULE_PARM_DESC(enable, "enable card");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) module_param(queue_length, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) MODULE_PARM_DESC(queue_length, "USB queue length in microframes, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		 __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	INTF_PLAYBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	INTF_CAPTURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	INTF_MIDI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	INTF_COUNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) /* bits in struct ua101::states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	USB_CAPTURE_RUNNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	USB_PLAYBACK_RUNNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	ALSA_CAPTURE_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	ALSA_PLAYBACK_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	ALSA_CAPTURE_RUNNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	ALSA_PLAYBACK_RUNNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	CAPTURE_URB_COMPLETED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	PLAYBACK_URB_COMPLETED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	DISCONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) struct ua101 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	struct usb_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct snd_card *card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct usb_interface *intf[INTF_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	int card_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	struct snd_pcm *pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	struct list_head midi_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	u64 format_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	unsigned int rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	unsigned int packets_per_second;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	unsigned long states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	/* FIFO to synchronize playback rate to capture rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	unsigned int rate_feedback_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	unsigned int rate_feedback_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	u8 rate_feedback[MAX_QUEUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct list_head ready_playback_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct work_struct playback_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	wait_queue_head_t alsa_capture_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	wait_queue_head_t rate_feedback_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	wait_queue_head_t alsa_playback_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct ua101_stream {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		unsigned int usb_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		unsigned int channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		unsigned int frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		unsigned int max_packet_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		unsigned int period_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		unsigned int buffer_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		unsigned int queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		struct ua101_urb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 			struct urb urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			struct usb_iso_packet_descriptor iso_frame_desc[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 			struct list_head ready_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		} *urbs[MAX_QUEUE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 			void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			dma_addr_t dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		} buffers[MAX_MEMORY_BUFFERS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	} capture, playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static DEFINE_MUTEX(devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static unsigned int devices_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) static struct usb_driver ua101_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) static void abort_alsa_playback(struct ua101 *ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static void abort_alsa_capture(struct ua101 *ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) static const char *usb_error_string(int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		return "no device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		return "endpoint not enabled";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		return "endpoint stalled";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	case -ENOSPC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		return "not enough bandwidth";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		return "device disabled";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	case -EHOSTUNREACH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		return "device suspended";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	case -EINVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	case -EFBIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	case -EMSGSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		return "internal error";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		return "unknown error";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) static void abort_usb_capture(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		wake_up(&ua->alsa_capture_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		wake_up(&ua->rate_feedback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static void abort_usb_playback(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		wake_up(&ua->alsa_playback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static void playback_urb_complete(struct urb *usb_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct ua101 *ua = urb->urb.context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	if (unlikely(urb->urb.status == -ENOENT ||	/* unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		     urb->urb.status == -ENODEV ||	/* device removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		     urb->urb.status == -ECONNRESET ||	/* unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		     urb->urb.status == -ESHUTDOWN)) {	/* device disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		abort_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		abort_alsa_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		/* append URB to FIFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		spin_lock_irqsave(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		if (ua->rate_feedback_count > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 			queue_work(system_highpri_wq, &ua->playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		ua->playback.substream->runtime->delay -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				urb->urb.iso_frame_desc[0].length /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 						ua->playback.frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) static void first_playback_urb_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct ua101 *ua = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	urb->complete = playback_urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	playback_urb_complete(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	set_bit(PLAYBACK_URB_COMPLETED, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	wake_up(&ua->alsa_playback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) /* copy data from the ALSA ring buffer into the URB buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			       unsigned int frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct snd_pcm_runtime *runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	unsigned int frame_bytes, frames1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	const u8 *source;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	runtime = stream->substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	frame_bytes = stream->frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	source = runtime->dma_area + stream->buffer_pos * frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	if (stream->buffer_pos + frames <= runtime->buffer_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		memcpy(urb->transfer_buffer, source, frames * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		/* wrap around at end of ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		frames1 = runtime->buffer_size - stream->buffer_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		memcpy(urb->transfer_buffer, source, frames1 * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		memcpy(urb->transfer_buffer + frames1 * frame_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		       runtime->dma_area, (frames - frames1) * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	stream->buffer_pos += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	if (stream->buffer_pos >= runtime->buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		stream->buffer_pos -= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	stream->period_pos += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	if (stream->period_pos >= runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		stream->period_pos -= runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static inline void add_with_wraparound(struct ua101 *ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 				       unsigned int *value, unsigned int add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	*value += add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (*value >= ua->playback.queue_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		*value -= ua->playback.queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static void playback_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	struct ua101 *ua = container_of(work, struct ua101, playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	unsigned int frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	struct ua101_urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	bool do_period_elapsed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	 * Synchronizing the playback rate to the capture rate is done by using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	 * the same sequence of packet sizes for both streams.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	 * Submitting a playback URB therefore requires both a ready URB and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	 * the size of the corresponding capture packet, i.e., both playback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	 * and capture URBs must have been completed.  Since the USB core does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	 * not guarantee that playback and capture complete callbacks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	 * called alternately, we use two FIFOs for packet sizes and read URBs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	 * submitting playback URBs is possible as long as both FIFOs are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	 * nonempty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	spin_lock_irqsave(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	while (ua->rate_feedback_count > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	       !list_empty(&ua->ready_playback_urbs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		/* take packet size out of FIFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		frames = ua->rate_feedback[ua->rate_feedback_start];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		add_with_wraparound(ua, &ua->rate_feedback_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		ua->rate_feedback_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		/* take URB out of FIFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		urb = list_first_entry(&ua->ready_playback_urbs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 				       struct ua101_urb, ready_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		list_del(&urb->ready_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		/* fill packet with data or silence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		urb->urb.iso_frame_desc[0].length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			frames * ua->playback.frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			do_period_elapsed |= copy_playback_data(&ua->playback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 								&urb->urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 								frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			memset(urb->urb.transfer_buffer, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			       urb->urb.iso_frame_desc[0].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		/* and off you go ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		if (unlikely(err < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 			spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			abort_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			abort_alsa_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			dev_err(&ua->dev->dev, "USB request error %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 				err, usb_error_string(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		ua->playback.substream->runtime->delay += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (do_period_elapsed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		snd_pcm_period_elapsed(ua->playback.substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) /* copy data from the URB buffer into the ALSA ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 			      unsigned int frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	struct snd_pcm_runtime *runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	unsigned int frame_bytes, frames1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	u8 *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	runtime = stream->substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	frame_bytes = stream->frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	dest = runtime->dma_area + stream->buffer_pos * frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (stream->buffer_pos + frames <= runtime->buffer_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		memcpy(dest, urb->transfer_buffer, frames * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		/* wrap around at end of ring buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		frames1 = runtime->buffer_size - stream->buffer_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		memcpy(runtime->dma_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		       urb->transfer_buffer + frames1 * frame_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		       (frames - frames1) * frame_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	stream->buffer_pos += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (stream->buffer_pos >= runtime->buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		stream->buffer_pos -= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	stream->period_pos += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (stream->period_pos >= runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		stream->period_pos -= runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) static void capture_urb_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	struct ua101 *ua = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	struct ua101_stream *stream = &ua->capture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	unsigned int frames, write_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	bool do_period_elapsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (unlikely(urb->status == -ENOENT ||		/* unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		     urb->status == -ENODEV ||		/* device removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		     urb->status == -ECONNRESET ||	/* unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		     urb->status == -ESHUTDOWN))	/* device disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		goto stream_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		frames = urb->iso_frame_desc[0].actual_length /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 			stream->frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	spin_lock_irqsave(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		do_period_elapsed = copy_capture_data(stream, urb, frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		do_period_elapsed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		if (unlikely(err < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 			spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 			dev_err(&ua->dev->dev, "USB request error %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 				err, usb_error_string(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			goto stream_stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		/* append packet size to FIFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		write_ptr = ua->rate_feedback_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		ua->rate_feedback[write_ptr] = frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		if (ua->rate_feedback_count < ua->playback.queue_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			ua->rate_feedback_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			if (ua->rate_feedback_count ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 						ua->playback.queue_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 				wake_up(&ua->rate_feedback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			 * Ring buffer overflow; this happens when the playback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			 * stream is not running.  Throw away the oldest entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			 * so that the playback stream, when it starts, sees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			 * the most recent packet sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			add_with_wraparound(ua, &ua->rate_feedback_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		    !list_empty(&ua->ready_playback_urbs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 			queue_work(system_highpri_wq, &ua->playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (do_period_elapsed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		snd_pcm_period_elapsed(stream->substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) stream_stopped:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	abort_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	abort_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	abort_alsa_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	abort_alsa_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static void first_capture_urb_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	struct ua101 *ua = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	urb->complete = capture_urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	capture_urb_complete(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	set_bit(CAPTURE_URB_COMPLETED, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	wake_up(&ua->alsa_capture_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	for (i = 0; i < stream->queue_length; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			dev_err(&ua->dev->dev, "USB request error %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				err, usb_error_string(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) static void kill_stream_urbs(struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	for (i = 0; i < stream->queue_length; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		if (stream->urbs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			usb_kill_urb(&stream->urbs[i]->urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	alts = ua->intf[intf_index]->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	if (alts->desc.bAlternateSetting != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		int err = usb_set_interface(ua->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 					    alts->desc.bInterfaceNumber, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 			dev_err(&ua->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 				"cannot initialize interface; error %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 				err, usb_error_string(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	if (!ua->intf[intf_index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	alts = ua->intf[intf_index]->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (alts->desc.bAlternateSetting != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		int err = usb_set_interface(ua->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 					    alts->desc.bInterfaceNumber, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			dev_warn(&ua->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 				 "interface reset failed; error %d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				 err, usb_error_string(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static void stop_usb_capture(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	clear_bit(USB_CAPTURE_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	kill_stream_urbs(&ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	disable_iso_interface(ua, INTF_CAPTURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) static int start_usb_capture(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	if (test_bit(DISCONNECTED, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	kill_stream_urbs(&ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	err = enable_iso_interface(ua, INTF_CAPTURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	ua->rate_feedback_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	ua->rate_feedback_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	set_bit(USB_CAPTURE_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	err = submit_stream_urbs(ua, &ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		stop_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) static void stop_usb_playback(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	kill_stream_urbs(&ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	cancel_work_sync(&ua->playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	disable_iso_interface(ua, INTF_PLAYBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) static int start_usb_playback(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	unsigned int i, frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (test_bit(DISCONNECTED, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	kill_stream_urbs(&ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	cancel_work_sync(&ua->playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	err = enable_iso_interface(ua, INTF_PLAYBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	ua->playback.urbs[0]->urb.complete =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		first_playback_urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	spin_lock_irq(&ua->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	INIT_LIST_HEAD(&ua->ready_playback_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	spin_unlock_irq(&ua->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	 * We submit the initial URBs all at once, so we have to wait for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	 * packet size FIFO to be full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	wait_event(ua->rate_feedback_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		   ua->rate_feedback_count >= ua->playback.queue_length ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		   !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		   test_bit(DISCONNECTED, &ua->states));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (test_bit(DISCONNECTED, &ua->states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		stop_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		stop_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	for (i = 0; i < ua->playback.queue_length; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		/* all initial URBs contain silence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		spin_lock_irq(&ua->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		frames = ua->rate_feedback[ua->rate_feedback_start];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		add_with_wraparound(ua, &ua->rate_feedback_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		ua->rate_feedback_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		spin_unlock_irq(&ua->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		urb = &ua->playback.urbs[i]->urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		urb->iso_frame_desc[0].length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			frames * ua->playback.frame_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		memset(urb->transfer_buffer, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		       urb->iso_frame_desc[0].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	set_bit(USB_PLAYBACK_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	err = submit_stream_urbs(ua, &ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		stop_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) static void abort_alsa_capture(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		snd_pcm_stop_xrun(ua->capture.substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) static void abort_alsa_playback(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		snd_pcm_stop_xrun(ua->playback.substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			 unsigned int channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	substream->runtime->hw.info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		SNDRV_PCM_INFO_MMAP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		SNDRV_PCM_INFO_MMAP_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		SNDRV_PCM_INFO_BATCH |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		SNDRV_PCM_INFO_INTERLEAVED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		SNDRV_PCM_INFO_FIFO_IN_FRAMES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	substream->runtime->hw.formats = ua->format_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	substream->runtime->hw.rate_min = ua->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	substream->runtime->hw.rate_max = ua->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	substream->runtime->hw.channels_min = channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	substream->runtime->hw.channels_max = channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	substream->runtime->hw.period_bytes_min = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	substream->runtime->hw.period_bytes_max = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	substream->runtime->hw.periods_min = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	substream->runtime->hw.periods_max = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	err = snd_pcm_hw_constraint_minmax(substream->runtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 					   1500000 / ua->packets_per_second,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 					   UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static int capture_pcm_open(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	ua->capture.substream = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	err = set_stream_hw(ua, substream, ua->capture.channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	substream->runtime->hw.fifo_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	substream->runtime->delay = substream->runtime->hw.fifo_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		set_bit(ALSA_CAPTURE_OPEN, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) static int playback_pcm_open(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	ua->playback.substream = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	err = set_stream_hw(ua, substream, ua->playback.channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	substream->runtime->hw.fifo_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				  ua->packets_per_second);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	err = start_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			stop_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) static int capture_pcm_close(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		stop_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static int playback_pcm_close(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	stop_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		stop_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 				 struct snd_pcm_hw_params *hw_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				  struct snd_pcm_hw_params *hw_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		err = start_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static int capture_pcm_prepare(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	 * The EHCI driver schedules the first packet of an iso stream at 10 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	 * in the future, i.e., no data is actually captured for that long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	 * Take the wait here so that the stream is known to be actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	 * running when the start trigger has been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	wait_event(ua->alsa_capture_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		   test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		   !test_bit(USB_CAPTURE_RUNNING, &ua->states));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (test_bit(DISCONNECTED, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	ua->capture.period_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	ua->capture.buffer_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static int playback_pcm_prepare(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	err = start_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		err = start_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	/* see the comment in capture_pcm_prepare() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	wait_event(ua->alsa_playback_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		   test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		   !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (test_bit(DISCONNECTED, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	substream->runtime->delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	ua->playback.period_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	ua->playback.buffer_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct ua101 *ua = substream->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 						  struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	unsigned int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	spin_lock_irqsave(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	pos = stream->buffer_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	spin_unlock_irqrestore(&ua->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	struct ua101 *ua = subs->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	return ua101_pcm_pointer(ua, &ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct ua101 *ua = subs->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	return ua101_pcm_pointer(ua, &ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) static const struct snd_pcm_ops capture_pcm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	.open = capture_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	.close = capture_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	.hw_params = capture_pcm_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	.prepare = capture_pcm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	.trigger = capture_pcm_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	.pointer = capture_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) static const struct snd_pcm_ops playback_pcm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	.open = playback_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	.close = playback_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	.hw_params = playback_pcm_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	.prepare = playback_pcm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	.trigger = playback_pcm_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	.pointer = playback_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) static const struct uac_format_type_i_discrete_descriptor *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) find_format_descriptor(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct usb_host_interface *alt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	u8 *extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	int extralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (interface->num_altsetting != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		dev_err(&interface->dev, "invalid num_altsetting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	alt = &interface->altsetting[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	if (alt->desc.bNumEndpoints != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		dev_err(&interface->dev, "invalid bNumEndpoints\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	alt = &interface->altsetting[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (alt->desc.bNumEndpoints != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		dev_err(&interface->dev, "invalid bNumEndpoints\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	extra = alt->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	extralen = alt->extralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	while (extralen >= sizeof(struct usb_descriptor_header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		struct uac_format_type_i_discrete_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		desc = (struct uac_format_type_i_discrete_descriptor *)extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		if (desc->bLength > extralen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			dev_err(&interface->dev, "descriptor overflow\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		    desc->bDescriptorType == USB_DT_CS_INTERFACE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		    desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			    desc->bSamFreqType != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				dev_err(&interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 					"invalid format type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			return desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		extralen -= desc->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		extra += desc->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	dev_err(&interface->dev, "sample format descriptor not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static int detect_usb_format(struct ua101 *ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	const struct uac_format_type_i_discrete_descriptor *fmt_capture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	const struct uac_format_type_i_discrete_descriptor *fmt_playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	const struct usb_endpoint_descriptor *epd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	unsigned int rate2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (!fmt_capture || !fmt_playback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	switch (fmt_capture->bSubframeSize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		dev_err(&ua->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			"playback/capture sample widths do not match\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	if (fmt_capture->bBitResolution != 24 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	    fmt_playback->bBitResolution != 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	rate2 = combine_triple(fmt_playback->tSamFreq[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	if (ua->rate != rate2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		dev_err(&ua->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			"playback/capture rates do not match: %u/%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			rate2, ua->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	switch (ua->dev->speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	case USB_SPEED_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		ua->packets_per_second = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	case USB_SPEED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		ua->packets_per_second = 8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		dev_err(&ua->dev->dev, "unknown device speed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	ua->capture.channels = fmt_capture->bNrChannels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	ua->playback.channels = fmt_playback->bNrChannels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	ua->capture.frame_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		fmt_capture->bSubframeSize * ua->capture.channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	ua->playback.frame_bytes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		fmt_playback->bSubframeSize * ua->playback.channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	if (!usb_endpoint_is_isoc_in(epd) || usb_endpoint_maxp(epd) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		dev_err(&ua->dev->dev, "invalid capture endpoint\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	ua->capture.max_packet_bytes = usb_endpoint_maxp(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (!usb_endpoint_is_isoc_out(epd) || usb_endpoint_maxp(epd) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		dev_err(&ua->dev->dev, "invalid playback endpoint\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	ua->playback.max_packet_bytes = usb_endpoint_maxp(epd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	unsigned int remaining_packets, packets, packets_per_page, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	stream->queue_length = queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	stream->queue_length = max(stream->queue_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 				   (unsigned int)MIN_QUEUE_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	stream->queue_length = min(stream->queue_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 				   (unsigned int)MAX_QUEUE_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	 * quite bad when used with the packet sizes of this device (e.g. 280,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	 * 520, 624).  Therefore, we allocate and subdivide entire pages, using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	 * a smaller buffer only for the last chunk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	remaining_packets = stream->queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		packets = min(remaining_packets, packets_per_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		size = packets * stream->max_packet_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		stream->buffers[i].addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 					   &stream->buffers[i].dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		if (!stream->buffers[i].addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		stream->buffers[i].size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		remaining_packets -= packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		if (!remaining_packets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	if (remaining_packets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		dev_err(&ua->dev->dev, "too many packets\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		usb_free_coherent(ua->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 				  stream->buffers[i].size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 				  stream->buffers[i].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 				  stream->buffers[i].dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			     void (*urb_complete)(struct urb *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	unsigned max_packet_size = stream->max_packet_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	struct ua101_urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	unsigned int b, u = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		unsigned int size = stream->buffers[b].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		u8 *addr = stream->buffers[b].addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		dma_addr_t dma = stream->buffers[b].dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		while (size >= max_packet_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			if (u >= stream->queue_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 				goto bufsize_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			urb = kmalloc(sizeof(*urb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 			usb_init_urb(&urb->urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			urb->urb.dev = ua->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			urb->urb.pipe = stream->usb_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			urb->urb.transfer_buffer = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			urb->urb.transfer_dma = dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			urb->urb.transfer_buffer_length = max_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			urb->urb.number_of_packets = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			urb->urb.interval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			urb->urb.context = ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			urb->urb.complete = urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			urb->urb.iso_frame_desc[0].offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			urb->urb.iso_frame_desc[0].length = max_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			stream->urbs[u++] = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			size -= max_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			addr += max_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			dma += max_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	if (u == stream->queue_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) bufsize_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	dev_err(&ua->dev->dev, "internal buffer size error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) static void free_stream_urbs(struct ua101_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	for (i = 0; i < stream->queue_length; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		kfree(stream->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		stream->urbs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static void free_usb_related_resources(struct ua101 *ua,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 				       struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	free_stream_urbs(&ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	free_stream_urbs(&ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	free_stream_buffers(ua, &ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	free_stream_buffers(ua, &ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		intf = ua->intf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		ua->intf[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		if (intf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			if (intf != interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 				usb_driver_release_interface(&ua101_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 							     intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static void ua101_card_free(struct snd_card *card)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct ua101 *ua = card->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	mutex_destroy(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) static int ua101_probe(struct usb_interface *interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		       const struct usb_device_id *usb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	static const struct snd_usb_midi_endpoint_info midi_ep = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		.out_cables = 0x0001,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		.in_cables = 0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	static const struct snd_usb_audio_quirk midi_quirk = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		.type = QUIRK_MIDI_FIXED_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		.data = &midi_ep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	static const int intf_numbers[2][3] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		{	/* UA-101 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			[INTF_PLAYBACK] = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			[INTF_CAPTURE] = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			[INTF_MIDI] = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		{	/* UA-1000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			[INTF_CAPTURE] = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			[INTF_PLAYBACK] = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 			[INTF_MIDI] = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	struct snd_card *card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	struct ua101 *ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	unsigned int card_index, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	int is_ua1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	char usb_path[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	is_ua1000 = usb_id->idProduct == 0x0044;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	if (interface->altsetting->desc.bInterfaceNumber !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	    intf_numbers[is_ua1000][0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		if (enable[card_index] && !(devices_used & (1 << card_index)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (card_index >= SNDRV_CARDS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	err = snd_card_new(&interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 			   index[card_index], id[card_index], THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 			   sizeof(*ua), &card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	card->private_free = ua101_card_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	ua = card->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	ua->dev = interface_to_usbdev(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	ua->card = card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	ua->card_index = card_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	INIT_LIST_HEAD(&ua->midi_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	spin_lock_init(&ua->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	mutex_init(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	INIT_LIST_HEAD(&ua->ready_playback_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	INIT_WORK(&ua->playback_work, playback_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	init_waitqueue_head(&ua->alsa_capture_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	init_waitqueue_head(&ua->rate_feedback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	init_waitqueue_head(&ua->alsa_playback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	ua->intf[0] = interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		ua->intf[i] = usb_ifnum_to_if(ua->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 					      intf_numbers[is_ua1000][i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		if (!ua->intf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 			dev_err(&ua->dev->dev, "interface %u not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				intf_numbers[is_ua1000][i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			err = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		err = usb_driver_claim_interface(&ua101_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 						 ua->intf[i], ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			ua->intf[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	err = detect_usb_format(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	strcpy(card->driver, "UA-101");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	strcpy(card->shortname, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	usb_make_path(ua->dev, usb_path, sizeof(usb_path));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	snprintf(ua->card->longname, sizeof(ua->card->longname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		 "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		 ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		 ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	err = alloc_stream_buffers(ua, &ua->capture);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	err = alloc_stream_buffers(ua, &ua->playback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	ua->pcm->private_data = ua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	strcpy(ua->pcm->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	snd_pcm_set_managed_buffer_all(ua->pcm, SNDRV_DMA_TYPE_VMALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 				       NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 				 &ua->midi_list, &midi_quirk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	err = snd_card_register(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		goto probe_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	usb_set_intfdata(interface, ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	devices_used |= 1 << card_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) probe_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	free_usb_related_resources(ua, interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	snd_card_free(card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) static void ua101_disconnect(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	struct ua101 *ua = usb_get_intfdata(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	struct list_head *midi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	if (!ua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	set_bit(DISCONNECTED, &ua->states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	wake_up(&ua->rate_feedback_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	/* make sure that userspace cannot create new requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	snd_card_disconnect(ua->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	/* make sure that there are no pending USB requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	list_for_each(midi, &ua->midi_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		snd_usbmidi_disconnect(midi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	abort_alsa_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	abort_alsa_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	mutex_lock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	stop_usb_playback(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	stop_usb_capture(ua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	mutex_unlock(&ua->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	free_usb_related_resources(ua, interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	devices_used &= ~(1 << ua->card_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	snd_card_free_when_closed(ua->card);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) static const struct usb_device_id ua101_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	{ USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	{ USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	{ USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) MODULE_DEVICE_TABLE(usb, ua101_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) static struct usb_driver ua101_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	.name = "snd-ua101",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	.id_table = ua101_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	.probe = ua101_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	.disconnect = ua101_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	.suspend = ua101_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	.resume = ua101_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) module_usb_driver(ua101_driver);