ua101.c 37 KB

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