ua101.c 37 KB

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