ua101.c 35 KB

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