ua101.c 39 KB

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