ua101.c 36 KB

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