ua101.c 38 KB

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