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 bool 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. if (stream->urbs[i])
  409. usb_kill_urb(&stream->urbs[i]->urb);
  410. }
  411. static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index)
  412. {
  413. struct usb_host_interface *alts;
  414. alts = ua->intf[intf_index]->cur_altsetting;
  415. if (alts->desc.bAlternateSetting != 1) {
  416. int err = usb_set_interface(ua->dev,
  417. alts->desc.bInterfaceNumber, 1);
  418. if (err < 0) {
  419. dev_err(&ua->dev->dev,
  420. "cannot initialize interface; error %d: %s\n",
  421. err, usb_error_string(err));
  422. return err;
  423. }
  424. }
  425. return 0;
  426. }
  427. static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index)
  428. {
  429. struct usb_host_interface *alts;
  430. if (!ua->intf[intf_index])
  431. return;
  432. alts = ua->intf[intf_index]->cur_altsetting;
  433. if (alts->desc.bAlternateSetting != 0) {
  434. int err = usb_set_interface(ua->dev,
  435. alts->desc.bInterfaceNumber, 0);
  436. if (err < 0 && !test_bit(DISCONNECTED, &ua->states))
  437. dev_warn(&ua->dev->dev,
  438. "interface reset failed; error %d: %s\n",
  439. err, usb_error_string(err));
  440. }
  441. }
  442. static void stop_usb_capture(struct ua101 *ua)
  443. {
  444. clear_bit(USB_CAPTURE_RUNNING, &ua->states);
  445. kill_stream_urbs(&ua->capture);
  446. disable_iso_interface(ua, INTF_CAPTURE);
  447. }
  448. static int start_usb_capture(struct ua101 *ua)
  449. {
  450. int err;
  451. if (test_bit(DISCONNECTED, &ua->states))
  452. return -ENODEV;
  453. if (test_bit(USB_CAPTURE_RUNNING, &ua->states))
  454. return 0;
  455. kill_stream_urbs(&ua->capture);
  456. err = enable_iso_interface(ua, INTF_CAPTURE);
  457. if (err < 0)
  458. return err;
  459. clear_bit(CAPTURE_URB_COMPLETED, &ua->states);
  460. ua->capture.urbs[0]->urb.complete = first_capture_urb_complete;
  461. ua->rate_feedback_start = 0;
  462. ua->rate_feedback_count = 0;
  463. set_bit(USB_CAPTURE_RUNNING, &ua->states);
  464. err = submit_stream_urbs(ua, &ua->capture);
  465. if (err < 0)
  466. stop_usb_capture(ua);
  467. return err;
  468. }
  469. static void stop_usb_playback(struct ua101 *ua)
  470. {
  471. clear_bit(USB_PLAYBACK_RUNNING, &ua->states);
  472. kill_stream_urbs(&ua->playback);
  473. tasklet_kill(&ua->playback_tasklet);
  474. disable_iso_interface(ua, INTF_PLAYBACK);
  475. }
  476. static int start_usb_playback(struct ua101 *ua)
  477. {
  478. unsigned int i, frames;
  479. struct urb *urb;
  480. int err = 0;
  481. if (test_bit(DISCONNECTED, &ua->states))
  482. return -ENODEV;
  483. if (test_bit(USB_PLAYBACK_RUNNING, &ua->states))
  484. return 0;
  485. kill_stream_urbs(&ua->playback);
  486. tasklet_kill(&ua->playback_tasklet);
  487. err = enable_iso_interface(ua, INTF_PLAYBACK);
  488. if (err < 0)
  489. return err;
  490. clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
  491. ua->playback.urbs[0]->urb.complete =
  492. first_playback_urb_complete;
  493. spin_lock_irq(&ua->lock);
  494. INIT_LIST_HEAD(&ua->ready_playback_urbs);
  495. spin_unlock_irq(&ua->lock);
  496. /*
  497. * We submit the initial URBs all at once, so we have to wait for the
  498. * packet size FIFO to be full.
  499. */
  500. wait_event(ua->rate_feedback_wait,
  501. ua->rate_feedback_count >= ua->playback.queue_length ||
  502. !test_bit(USB_CAPTURE_RUNNING, &ua->states) ||
  503. test_bit(DISCONNECTED, &ua->states));
  504. if (test_bit(DISCONNECTED, &ua->states)) {
  505. stop_usb_playback(ua);
  506. return -ENODEV;
  507. }
  508. if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
  509. stop_usb_playback(ua);
  510. return -EIO;
  511. }
  512. for (i = 0; i < ua->playback.queue_length; ++i) {
  513. /* all initial URBs contain silence */
  514. spin_lock_irq(&ua->lock);
  515. frames = ua->rate_feedback[ua->rate_feedback_start];
  516. add_with_wraparound(ua, &ua->rate_feedback_start, 1);
  517. ua->rate_feedback_count--;
  518. spin_unlock_irq(&ua->lock);
  519. urb = &ua->playback.urbs[i]->urb;
  520. urb->iso_frame_desc[0].length =
  521. frames * ua->playback.frame_bytes;
  522. memset(urb->transfer_buffer, 0,
  523. urb->iso_frame_desc[0].length);
  524. }
  525. set_bit(USB_PLAYBACK_RUNNING, &ua->states);
  526. err = submit_stream_urbs(ua, &ua->playback);
  527. if (err < 0)
  528. stop_usb_playback(ua);
  529. return err;
  530. }
  531. static void abort_alsa_capture(struct ua101 *ua)
  532. {
  533. if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
  534. snd_pcm_stop_xrun(ua->capture.substream);
  535. }
  536. static void abort_alsa_playback(struct ua101 *ua)
  537. {
  538. if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
  539. snd_pcm_stop_xrun(ua->playback.substream);
  540. }
  541. static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream,
  542. unsigned int channels)
  543. {
  544. int err;
  545. substream->runtime->hw.info =
  546. SNDRV_PCM_INFO_MMAP |
  547. SNDRV_PCM_INFO_MMAP_VALID |
  548. SNDRV_PCM_INFO_BATCH |
  549. SNDRV_PCM_INFO_INTERLEAVED |
  550. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  551. SNDRV_PCM_INFO_FIFO_IN_FRAMES;
  552. substream->runtime->hw.formats = ua->format_bit;
  553. substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate);
  554. substream->runtime->hw.rate_min = ua->rate;
  555. substream->runtime->hw.rate_max = ua->rate;
  556. substream->runtime->hw.channels_min = channels;
  557. substream->runtime->hw.channels_max = channels;
  558. substream->runtime->hw.buffer_bytes_max = 45000 * 1024;
  559. substream->runtime->hw.period_bytes_min = 1;
  560. substream->runtime->hw.period_bytes_max = UINT_MAX;
  561. substream->runtime->hw.periods_min = 2;
  562. substream->runtime->hw.periods_max = UINT_MAX;
  563. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  564. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  565. 1500000 / ua->packets_per_second,
  566. UINT_MAX);
  567. if (err < 0)
  568. return err;
  569. err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24);
  570. return err;
  571. }
  572. static int capture_pcm_open(struct snd_pcm_substream *substream)
  573. {
  574. struct ua101 *ua = substream->private_data;
  575. int err;
  576. ua->capture.substream = substream;
  577. err = set_stream_hw(ua, substream, ua->capture.channels);
  578. if (err < 0)
  579. return err;
  580. substream->runtime->hw.fifo_size =
  581. DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
  582. substream->runtime->delay = substream->runtime->hw.fifo_size;
  583. mutex_lock(&ua->mutex);
  584. err = start_usb_capture(ua);
  585. if (err >= 0)
  586. set_bit(ALSA_CAPTURE_OPEN, &ua->states);
  587. mutex_unlock(&ua->mutex);
  588. return err;
  589. }
  590. static int playback_pcm_open(struct snd_pcm_substream *substream)
  591. {
  592. struct ua101 *ua = substream->private_data;
  593. int err;
  594. ua->playback.substream = substream;
  595. err = set_stream_hw(ua, substream, ua->playback.channels);
  596. if (err < 0)
  597. return err;
  598. substream->runtime->hw.fifo_size =
  599. DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
  600. ua->packets_per_second);
  601. mutex_lock(&ua->mutex);
  602. err = start_usb_capture(ua);
  603. if (err < 0)
  604. goto error;
  605. err = start_usb_playback(ua);
  606. if (err < 0) {
  607. if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
  608. stop_usb_capture(ua);
  609. goto error;
  610. }
  611. set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
  612. error:
  613. mutex_unlock(&ua->mutex);
  614. return err;
  615. }
  616. static int capture_pcm_close(struct snd_pcm_substream *substream)
  617. {
  618. struct ua101 *ua = substream->private_data;
  619. mutex_lock(&ua->mutex);
  620. clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
  621. if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
  622. stop_usb_capture(ua);
  623. mutex_unlock(&ua->mutex);
  624. return 0;
  625. }
  626. static int playback_pcm_close(struct snd_pcm_substream *substream)
  627. {
  628. struct ua101 *ua = substream->private_data;
  629. mutex_lock(&ua->mutex);
  630. stop_usb_playback(ua);
  631. clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
  632. if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
  633. stop_usb_capture(ua);
  634. mutex_unlock(&ua->mutex);
  635. return 0;
  636. }
  637. static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
  638. struct snd_pcm_hw_params *hw_params)
  639. {
  640. struct ua101 *ua = substream->private_data;
  641. int err;
  642. mutex_lock(&ua->mutex);
  643. err = start_usb_capture(ua);
  644. mutex_unlock(&ua->mutex);
  645. if (err < 0)
  646. return err;
  647. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  648. params_buffer_bytes(hw_params));
  649. }
  650. static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
  651. struct snd_pcm_hw_params *hw_params)
  652. {
  653. struct ua101 *ua = substream->private_data;
  654. int err;
  655. mutex_lock(&ua->mutex);
  656. err = start_usb_capture(ua);
  657. if (err >= 0)
  658. err = start_usb_playback(ua);
  659. mutex_unlock(&ua->mutex);
  660. if (err < 0)
  661. return err;
  662. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  663. params_buffer_bytes(hw_params));
  664. }
  665. static int ua101_pcm_hw_free(struct snd_pcm_substream *substream)
  666. {
  667. return snd_pcm_lib_free_vmalloc_buffer(substream);
  668. }
  669. static int capture_pcm_prepare(struct snd_pcm_substream *substream)
  670. {
  671. struct ua101 *ua = substream->private_data;
  672. int err;
  673. mutex_lock(&ua->mutex);
  674. err = start_usb_capture(ua);
  675. mutex_unlock(&ua->mutex);
  676. if (err < 0)
  677. return err;
  678. /*
  679. * The EHCI driver schedules the first packet of an iso stream at 10 ms
  680. * in the future, i.e., no data is actually captured for that long.
  681. * Take the wait here so that the stream is known to be actually
  682. * running when the start trigger has been called.
  683. */
  684. wait_event(ua->alsa_capture_wait,
  685. test_bit(CAPTURE_URB_COMPLETED, &ua->states) ||
  686. !test_bit(USB_CAPTURE_RUNNING, &ua->states));
  687. if (test_bit(DISCONNECTED, &ua->states))
  688. return -ENODEV;
  689. if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
  690. return -EIO;
  691. ua->capture.period_pos = 0;
  692. ua->capture.buffer_pos = 0;
  693. return 0;
  694. }
  695. static int playback_pcm_prepare(struct snd_pcm_substream *substream)
  696. {
  697. struct ua101 *ua = substream->private_data;
  698. int err;
  699. mutex_lock(&ua->mutex);
  700. err = start_usb_capture(ua);
  701. if (err >= 0)
  702. err = start_usb_playback(ua);
  703. mutex_unlock(&ua->mutex);
  704. if (err < 0)
  705. return err;
  706. /* see the comment in capture_pcm_prepare() */
  707. wait_event(ua->alsa_playback_wait,
  708. test_bit(PLAYBACK_URB_COMPLETED, &ua->states) ||
  709. !test_bit(USB_PLAYBACK_RUNNING, &ua->states));
  710. if (test_bit(DISCONNECTED, &ua->states))
  711. return -ENODEV;
  712. if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
  713. return -EIO;
  714. substream->runtime->delay = 0;
  715. ua->playback.period_pos = 0;
  716. ua->playback.buffer_pos = 0;
  717. return 0;
  718. }
  719. static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  720. {
  721. struct ua101 *ua = substream->private_data;
  722. switch (cmd) {
  723. case SNDRV_PCM_TRIGGER_START:
  724. if (!test_bit(USB_CAPTURE_RUNNING, &ua->states))
  725. return -EIO;
  726. set_bit(ALSA_CAPTURE_RUNNING, &ua->states);
  727. return 0;
  728. case SNDRV_PCM_TRIGGER_STOP:
  729. clear_bit(ALSA_CAPTURE_RUNNING, &ua->states);
  730. return 0;
  731. default:
  732. return -EINVAL;
  733. }
  734. }
  735. static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  736. {
  737. struct ua101 *ua = substream->private_data;
  738. switch (cmd) {
  739. case SNDRV_PCM_TRIGGER_START:
  740. if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states))
  741. return -EIO;
  742. set_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
  743. return 0;
  744. case SNDRV_PCM_TRIGGER_STOP:
  745. clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states);
  746. return 0;
  747. default:
  748. return -EINVAL;
  749. }
  750. }
  751. static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
  752. struct ua101_stream *stream)
  753. {
  754. unsigned long flags;
  755. unsigned int pos;
  756. spin_lock_irqsave(&ua->lock, flags);
  757. pos = stream->buffer_pos;
  758. spin_unlock_irqrestore(&ua->lock, flags);
  759. return pos;
  760. }
  761. static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
  762. {
  763. struct ua101 *ua = subs->private_data;
  764. return ua101_pcm_pointer(ua, &ua->capture);
  765. }
  766. static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs)
  767. {
  768. struct ua101 *ua = subs->private_data;
  769. return ua101_pcm_pointer(ua, &ua->playback);
  770. }
  771. static struct snd_pcm_ops capture_pcm_ops = {
  772. .open = capture_pcm_open,
  773. .close = capture_pcm_close,
  774. .ioctl = snd_pcm_lib_ioctl,
  775. .hw_params = capture_pcm_hw_params,
  776. .hw_free = ua101_pcm_hw_free,
  777. .prepare = capture_pcm_prepare,
  778. .trigger = capture_pcm_trigger,
  779. .pointer = capture_pcm_pointer,
  780. .page = snd_pcm_lib_get_vmalloc_page,
  781. .mmap = snd_pcm_lib_mmap_vmalloc,
  782. };
  783. static struct snd_pcm_ops playback_pcm_ops = {
  784. .open = playback_pcm_open,
  785. .close = playback_pcm_close,
  786. .ioctl = snd_pcm_lib_ioctl,
  787. .hw_params = playback_pcm_hw_params,
  788. .hw_free = ua101_pcm_hw_free,
  789. .prepare = playback_pcm_prepare,
  790. .trigger = playback_pcm_trigger,
  791. .pointer = playback_pcm_pointer,
  792. .page = snd_pcm_lib_get_vmalloc_page,
  793. .mmap = snd_pcm_lib_mmap_vmalloc,
  794. };
  795. static const struct uac_format_type_i_discrete_descriptor *
  796. find_format_descriptor(struct usb_interface *interface)
  797. {
  798. struct usb_host_interface *alt;
  799. u8 *extra;
  800. int extralen;
  801. if (interface->num_altsetting != 2) {
  802. dev_err(&interface->dev, "invalid num_altsetting\n");
  803. return NULL;
  804. }
  805. alt = &interface->altsetting[0];
  806. if (alt->desc.bNumEndpoints != 0) {
  807. dev_err(&interface->dev, "invalid bNumEndpoints\n");
  808. return NULL;
  809. }
  810. alt = &interface->altsetting[1];
  811. if (alt->desc.bNumEndpoints != 1) {
  812. dev_err(&interface->dev, "invalid bNumEndpoints\n");
  813. return NULL;
  814. }
  815. extra = alt->extra;
  816. extralen = alt->extralen;
  817. while (extralen >= sizeof(struct usb_descriptor_header)) {
  818. struct uac_format_type_i_discrete_descriptor *desc;
  819. desc = (struct uac_format_type_i_discrete_descriptor *)extra;
  820. if (desc->bLength > extralen) {
  821. dev_err(&interface->dev, "descriptor overflow\n");
  822. return NULL;
  823. }
  824. if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) &&
  825. desc->bDescriptorType == USB_DT_CS_INTERFACE &&
  826. desc->bDescriptorSubtype == UAC_FORMAT_TYPE) {
  827. if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM ||
  828. desc->bSamFreqType != 1) {
  829. dev_err(&interface->dev,
  830. "invalid format type\n");
  831. return NULL;
  832. }
  833. return desc;
  834. }
  835. extralen -= desc->bLength;
  836. extra += desc->bLength;
  837. }
  838. dev_err(&interface->dev, "sample format descriptor not found\n");
  839. return NULL;
  840. }
  841. static int detect_usb_format(struct ua101 *ua)
  842. {
  843. const struct uac_format_type_i_discrete_descriptor *fmt_capture;
  844. const struct uac_format_type_i_discrete_descriptor *fmt_playback;
  845. const struct usb_endpoint_descriptor *epd;
  846. unsigned int rate2;
  847. fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]);
  848. fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]);
  849. if (!fmt_capture || !fmt_playback)
  850. return -ENXIO;
  851. switch (fmt_capture->bSubframeSize) {
  852. case 3:
  853. ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE;
  854. break;
  855. case 4:
  856. ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE;
  857. break;
  858. default:
  859. dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n");
  860. return -ENXIO;
  861. }
  862. if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) {
  863. dev_err(&ua->dev->dev,
  864. "playback/capture sample widths do not match\n");
  865. return -ENXIO;
  866. }
  867. if (fmt_capture->bBitResolution != 24 ||
  868. fmt_playback->bBitResolution != 24) {
  869. dev_err(&ua->dev->dev, "sample width is not 24 bits\n");
  870. return -ENXIO;
  871. }
  872. ua->rate = combine_triple(fmt_capture->tSamFreq[0]);
  873. rate2 = combine_triple(fmt_playback->tSamFreq[0]);
  874. if (ua->rate != rate2) {
  875. dev_err(&ua->dev->dev,
  876. "playback/capture rates do not match: %u/%u\n",
  877. rate2, ua->rate);
  878. return -ENXIO;
  879. }
  880. switch (ua->dev->speed) {
  881. case USB_SPEED_FULL:
  882. ua->packets_per_second = 1000;
  883. break;
  884. case USB_SPEED_HIGH:
  885. ua->packets_per_second = 8000;
  886. break;
  887. default:
  888. dev_err(&ua->dev->dev, "unknown device speed\n");
  889. return -ENXIO;
  890. }
  891. ua->capture.channels = fmt_capture->bNrChannels;
  892. ua->playback.channels = fmt_playback->bNrChannels;
  893. ua->capture.frame_bytes =
  894. fmt_capture->bSubframeSize * ua->capture.channels;
  895. ua->playback.frame_bytes =
  896. fmt_playback->bSubframeSize * ua->playback.channels;
  897. epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc;
  898. if (!usb_endpoint_is_isoc_in(epd)) {
  899. dev_err(&ua->dev->dev, "invalid capture endpoint\n");
  900. return -ENXIO;
  901. }
  902. ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd));
  903. ua->capture.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
  904. epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc;
  905. if (!usb_endpoint_is_isoc_out(epd)) {
  906. dev_err(&ua->dev->dev, "invalid playback endpoint\n");
  907. return -ENXIO;
  908. }
  909. ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd));
  910. ua->playback.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize);
  911. return 0;
  912. }
  913. static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
  914. {
  915. unsigned int remaining_packets, packets, packets_per_page, i;
  916. size_t size;
  917. stream->queue_length = queue_length;
  918. stream->queue_length = max(stream->queue_length,
  919. (unsigned int)MIN_QUEUE_LENGTH);
  920. stream->queue_length = min(stream->queue_length,
  921. (unsigned int)MAX_QUEUE_LENGTH);
  922. /*
  923. * The cache pool sizes used by usb_alloc_coherent() (128, 512, 2048) are
  924. * quite bad when used with the packet sizes of this device (e.g. 280,
  925. * 520, 624). Therefore, we allocate and subdivide entire pages, using
  926. * a smaller buffer only for the last chunk.
  927. */
  928. remaining_packets = stream->queue_length;
  929. packets_per_page = PAGE_SIZE / stream->max_packet_bytes;
  930. for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) {
  931. packets = min(remaining_packets, packets_per_page);
  932. size = packets * stream->max_packet_bytes;
  933. stream->buffers[i].addr =
  934. usb_alloc_coherent(ua->dev, size, GFP_KERNEL,
  935. &stream->buffers[i].dma);
  936. if (!stream->buffers[i].addr)
  937. return -ENOMEM;
  938. stream->buffers[i].size = size;
  939. remaining_packets -= packets;
  940. if (!remaining_packets)
  941. break;
  942. }
  943. if (remaining_packets) {
  944. dev_err(&ua->dev->dev, "too many packets\n");
  945. return -ENXIO;
  946. }
  947. return 0;
  948. }
  949. static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream)
  950. {
  951. unsigned int i;
  952. for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i)
  953. usb_free_coherent(ua->dev,
  954. stream->buffers[i].size,
  955. stream->buffers[i].addr,
  956. stream->buffers[i].dma);
  957. }
  958. static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream,
  959. void (*urb_complete)(struct urb *))
  960. {
  961. unsigned max_packet_size = stream->max_packet_bytes;
  962. struct ua101_urb *urb;
  963. unsigned int b, u = 0;
  964. for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) {
  965. unsigned int size = stream->buffers[b].size;
  966. u8 *addr = stream->buffers[b].addr;
  967. dma_addr_t dma = stream->buffers[b].dma;
  968. while (size >= max_packet_size) {
  969. if (u >= stream->queue_length)
  970. goto bufsize_error;
  971. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  972. if (!urb)
  973. return -ENOMEM;
  974. usb_init_urb(&urb->urb);
  975. urb->urb.dev = ua->dev;
  976. urb->urb.pipe = stream->usb_pipe;
  977. urb->urb.transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  978. urb->urb.transfer_buffer = addr;
  979. urb->urb.transfer_dma = dma;
  980. urb->urb.transfer_buffer_length = max_packet_size;
  981. urb->urb.number_of_packets = 1;
  982. urb->urb.interval = 1;
  983. urb->urb.context = ua;
  984. urb->urb.complete = urb_complete;
  985. urb->urb.iso_frame_desc[0].offset = 0;
  986. urb->urb.iso_frame_desc[0].length = max_packet_size;
  987. stream->urbs[u++] = urb;
  988. size -= max_packet_size;
  989. addr += max_packet_size;
  990. dma += max_packet_size;
  991. }
  992. }
  993. if (u == stream->queue_length)
  994. return 0;
  995. bufsize_error:
  996. dev_err(&ua->dev->dev, "internal buffer size error\n");
  997. return -ENXIO;
  998. }
  999. static void free_stream_urbs(struct ua101_stream *stream)
  1000. {
  1001. unsigned int i;
  1002. for (i = 0; i < stream->queue_length; ++i) {
  1003. kfree(stream->urbs[i]);
  1004. stream->urbs[i] = NULL;
  1005. }
  1006. }
  1007. static void free_usb_related_resources(struct ua101 *ua,
  1008. struct usb_interface *interface)
  1009. {
  1010. unsigned int i;
  1011. struct usb_interface *intf;
  1012. mutex_lock(&ua->mutex);
  1013. free_stream_urbs(&ua->capture);
  1014. free_stream_urbs(&ua->playback);
  1015. mutex_unlock(&ua->mutex);
  1016. free_stream_buffers(ua, &ua->capture);
  1017. free_stream_buffers(ua, &ua->playback);
  1018. for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
  1019. mutex_lock(&ua->mutex);
  1020. intf = ua->intf[i];
  1021. ua->intf[i] = NULL;
  1022. mutex_unlock(&ua->mutex);
  1023. if (intf) {
  1024. usb_set_intfdata(intf, NULL);
  1025. if (intf != interface)
  1026. usb_driver_release_interface(&ua101_driver,
  1027. intf);
  1028. }
  1029. }
  1030. }
  1031. static void ua101_card_free(struct snd_card *card)
  1032. {
  1033. struct ua101 *ua = card->private_data;
  1034. mutex_destroy(&ua->mutex);
  1035. }
  1036. static int ua101_probe(struct usb_interface *interface,
  1037. const struct usb_device_id *usb_id)
  1038. {
  1039. static const struct snd_usb_midi_endpoint_info midi_ep = {
  1040. .out_cables = 0x0001,
  1041. .in_cables = 0x0001
  1042. };
  1043. static const struct snd_usb_audio_quirk midi_quirk = {
  1044. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  1045. .data = &midi_ep
  1046. };
  1047. static const int intf_numbers[2][3] = {
  1048. { /* UA-101 */
  1049. [INTF_PLAYBACK] = 0,
  1050. [INTF_CAPTURE] = 1,
  1051. [INTF_MIDI] = 2,
  1052. },
  1053. { /* UA-1000 */
  1054. [INTF_CAPTURE] = 1,
  1055. [INTF_PLAYBACK] = 2,
  1056. [INTF_MIDI] = 3,
  1057. },
  1058. };
  1059. struct snd_card *card;
  1060. struct ua101 *ua;
  1061. unsigned int card_index, i;
  1062. int is_ua1000;
  1063. const char *name;
  1064. char usb_path[32];
  1065. int err;
  1066. is_ua1000 = usb_id->idProduct == 0x0044;
  1067. if (interface->altsetting->desc.bInterfaceNumber !=
  1068. intf_numbers[is_ua1000][0])
  1069. return -ENODEV;
  1070. mutex_lock(&devices_mutex);
  1071. for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
  1072. if (enable[card_index] && !(devices_used & (1 << card_index)))
  1073. break;
  1074. if (card_index >= SNDRV_CARDS) {
  1075. mutex_unlock(&devices_mutex);
  1076. return -ENOENT;
  1077. }
  1078. err = snd_card_new(&interface->dev,
  1079. index[card_index], id[card_index], THIS_MODULE,
  1080. sizeof(*ua), &card);
  1081. if (err < 0) {
  1082. mutex_unlock(&devices_mutex);
  1083. return err;
  1084. }
  1085. card->private_free = ua101_card_free;
  1086. ua = card->private_data;
  1087. ua->dev = interface_to_usbdev(interface);
  1088. ua->card = card;
  1089. ua->card_index = card_index;
  1090. INIT_LIST_HEAD(&ua->midi_list);
  1091. spin_lock_init(&ua->lock);
  1092. mutex_init(&ua->mutex);
  1093. INIT_LIST_HEAD(&ua->ready_playback_urbs);
  1094. tasklet_init(&ua->playback_tasklet,
  1095. playback_tasklet, (unsigned long)ua);
  1096. init_waitqueue_head(&ua->alsa_capture_wait);
  1097. init_waitqueue_head(&ua->rate_feedback_wait);
  1098. init_waitqueue_head(&ua->alsa_playback_wait);
  1099. ua->intf[0] = interface;
  1100. for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) {
  1101. ua->intf[i] = usb_ifnum_to_if(ua->dev,
  1102. intf_numbers[is_ua1000][i]);
  1103. if (!ua->intf[i]) {
  1104. dev_err(&ua->dev->dev, "interface %u not found\n",
  1105. intf_numbers[is_ua1000][i]);
  1106. err = -ENXIO;
  1107. goto probe_error;
  1108. }
  1109. err = usb_driver_claim_interface(&ua101_driver,
  1110. ua->intf[i], ua);
  1111. if (err < 0) {
  1112. ua->intf[i] = NULL;
  1113. err = -EBUSY;
  1114. goto probe_error;
  1115. }
  1116. }
  1117. err = detect_usb_format(ua);
  1118. if (err < 0)
  1119. goto probe_error;
  1120. name = usb_id->idProduct == 0x0044 ? "UA-1000" : "UA-101";
  1121. strcpy(card->driver, "UA-101");
  1122. strcpy(card->shortname, name);
  1123. usb_make_path(ua->dev, usb_path, sizeof(usb_path));
  1124. snprintf(ua->card->longname, sizeof(ua->card->longname),
  1125. "EDIROL %s (serial %s), %u Hz at %s, %s speed", name,
  1126. ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path,
  1127. ua->dev->speed == USB_SPEED_HIGH ? "high" : "full");
  1128. err = alloc_stream_buffers(ua, &ua->capture);
  1129. if (err < 0)
  1130. goto probe_error;
  1131. err = alloc_stream_buffers(ua, &ua->playback);
  1132. if (err < 0)
  1133. goto probe_error;
  1134. err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete);
  1135. if (err < 0)
  1136. goto probe_error;
  1137. err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete);
  1138. if (err < 0)
  1139. goto probe_error;
  1140. err = snd_pcm_new(card, name, 0, 1, 1, &ua->pcm);
  1141. if (err < 0)
  1142. goto probe_error;
  1143. ua->pcm->private_data = ua;
  1144. strcpy(ua->pcm->name, name);
  1145. snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops);
  1146. snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops);
  1147. err = snd_usbmidi_create(card, ua->intf[INTF_MIDI],
  1148. &ua->midi_list, &midi_quirk);
  1149. if (err < 0)
  1150. goto probe_error;
  1151. err = snd_card_register(card);
  1152. if (err < 0)
  1153. goto probe_error;
  1154. usb_set_intfdata(interface, ua);
  1155. devices_used |= 1 << card_index;
  1156. mutex_unlock(&devices_mutex);
  1157. return 0;
  1158. probe_error:
  1159. free_usb_related_resources(ua, interface);
  1160. snd_card_free(card);
  1161. mutex_unlock(&devices_mutex);
  1162. return err;
  1163. }
  1164. static void ua101_disconnect(struct usb_interface *interface)
  1165. {
  1166. struct ua101 *ua = usb_get_intfdata(interface);
  1167. struct list_head *midi;
  1168. if (!ua)
  1169. return;
  1170. mutex_lock(&devices_mutex);
  1171. set_bit(DISCONNECTED, &ua->states);
  1172. wake_up(&ua->rate_feedback_wait);
  1173. /* make sure that userspace cannot create new requests */
  1174. snd_card_disconnect(ua->card);
  1175. /* make sure that there are no pending USB requests */
  1176. list_for_each(midi, &ua->midi_list)
  1177. snd_usbmidi_disconnect(midi);
  1178. abort_alsa_playback(ua);
  1179. abort_alsa_capture(ua);
  1180. mutex_lock(&ua->mutex);
  1181. stop_usb_playback(ua);
  1182. stop_usb_capture(ua);
  1183. mutex_unlock(&ua->mutex);
  1184. free_usb_related_resources(ua, interface);
  1185. devices_used &= ~(1 << ua->card_index);
  1186. snd_card_free_when_closed(ua->card);
  1187. mutex_unlock(&devices_mutex);
  1188. }
  1189. static struct usb_device_id ua101_ids[] = {
  1190. { USB_DEVICE(0x0582, 0x0044) }, /* UA-1000 high speed */
  1191. { USB_DEVICE(0x0582, 0x007d) }, /* UA-101 high speed */
  1192. { USB_DEVICE(0x0582, 0x008d) }, /* UA-101 full speed */
  1193. { }
  1194. };
  1195. MODULE_DEVICE_TABLE(usb, ua101_ids);
  1196. static struct usb_driver ua101_driver = {
  1197. .name = "snd-ua101",
  1198. .id_table = ua101_ids,
  1199. .probe = ua101_probe,
  1200. .disconnect = ua101_disconnect,
  1201. #if 0
  1202. .suspend = ua101_suspend,
  1203. .resume = ua101_resume,
  1204. #endif
  1205. };
  1206. module_usb_driver(ua101_driver);