usbusx2yaudio.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * US-X2Y AUDIO
  3. * Copyright (c) 2002-2004 by Karsten Wiese
  4. *
  5. * based on
  6. *
  7. * (Tentative) USB Audio Driver for ALSA
  8. *
  9. * Main and PCM part
  10. *
  11. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  12. *
  13. * Many codes borrowed from audio.c by
  14. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  15. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  16. *
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  31. */
  32. #include <linux/interrupt.h>
  33. #include <linux/slab.h>
  34. #include <linux/usb.h>
  35. #include <linux/moduleparam.h>
  36. #include <sound/core.h>
  37. #include <sound/info.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include "usx2y.h"
  41. #include "usbusx2y.h"
  42. #define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
  43. 1 to 4 have been tested ok on uhci.
  44. To use 3 on ohci, you'd need a patch:
  45. look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
  46. "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
  47. .
  48. 1, 2 and 4 work out of the box on ohci, if I recall correctly.
  49. Bigger is safer operation,
  50. smaller gives lower latencies.
  51. */
  52. #define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
  53. nrpacks set to 1, you might as well comment
  54. this #define out, and thereby produce smaller, faster code.
  55. You'd also set USX2Y_NRPACKS to 1 then.
  56. */
  57. #ifdef USX2Y_NRPACKS_VARIABLE
  58. static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
  59. #define nr_of_packs() nrpacks
  60. module_param(nrpacks, int, 0444);
  61. MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
  62. #else
  63. #define nr_of_packs() USX2Y_NRPACKS
  64. #endif
  65. static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
  66. {
  67. struct urb *urb = subs->completed_urb;
  68. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  69. unsigned char *cp;
  70. int i, len, lens = 0, hwptr_done = subs->hwptr_done;
  71. struct usX2Ydev *usX2Y = subs->usX2Y;
  72. for (i = 0; i < nr_of_packs(); i++) {
  73. cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  74. if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
  75. snd_printk(KERN_ERR "active frame status %i. "
  76. "Most probably some hardware problem.\n",
  77. urb->iso_frame_desc[i].status);
  78. return urb->iso_frame_desc[i].status;
  79. }
  80. len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
  81. if (! len) {
  82. snd_printd("0 == len ERROR!\n");
  83. continue;
  84. }
  85. /* copy a data chunk */
  86. if ((hwptr_done + len) > runtime->buffer_size) {
  87. int cnt = runtime->buffer_size - hwptr_done;
  88. int blen = cnt * usX2Y->stride;
  89. memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
  90. memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
  91. } else {
  92. memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
  93. len * usX2Y->stride);
  94. }
  95. lens += len;
  96. if ((hwptr_done += len) >= runtime->buffer_size)
  97. hwptr_done -= runtime->buffer_size;
  98. }
  99. subs->hwptr_done = hwptr_done;
  100. subs->transfer_done += lens;
  101. /* update the pointer, call callback if necessary */
  102. if (subs->transfer_done >= runtime->period_size) {
  103. subs->transfer_done -= runtime->period_size;
  104. snd_pcm_period_elapsed(subs->pcm_substream);
  105. }
  106. return 0;
  107. }
  108. /*
  109. * prepare urb for playback data pipe
  110. *
  111. * we copy the data directly from the pcm buffer.
  112. * the current position to be copied is held in hwptr field.
  113. * since a urb can handle only a single linear buffer, if the total
  114. * transferred area overflows the buffer boundary, we cannot send
  115. * it directly from the buffer. thus the data is once copied to
  116. * a temporary buffer and urb points to that.
  117. */
  118. static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
  119. struct urb *cap_urb,
  120. struct urb *urb)
  121. {
  122. int count, counts, pack;
  123. struct usX2Ydev *usX2Y = subs->usX2Y;
  124. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  125. count = 0;
  126. for (pack = 0; pack < nr_of_packs(); pack++) {
  127. /* calculate the size of a packet */
  128. counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
  129. count += counts;
  130. if (counts < 43 || counts > 50) {
  131. snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
  132. return -EPIPE;
  133. }
  134. /* set up descriptor */
  135. urb->iso_frame_desc[pack].offset = pack ?
  136. urb->iso_frame_desc[pack - 1].offset +
  137. urb->iso_frame_desc[pack - 1].length :
  138. 0;
  139. urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
  140. }
  141. if (atomic_read(&subs->state) >= state_PRERUNNING)
  142. if (subs->hwptr + count > runtime->buffer_size) {
  143. /* err, the transferred area goes over buffer boundary.
  144. * copy the data to the temp buffer.
  145. */
  146. int len;
  147. len = runtime->buffer_size - subs->hwptr;
  148. urb->transfer_buffer = subs->tmpbuf;
  149. memcpy(subs->tmpbuf, runtime->dma_area +
  150. subs->hwptr * usX2Y->stride, len * usX2Y->stride);
  151. memcpy(subs->tmpbuf + len * usX2Y->stride,
  152. runtime->dma_area, (count - len) * usX2Y->stride);
  153. subs->hwptr += count;
  154. subs->hwptr -= runtime->buffer_size;
  155. } else {
  156. /* set the buffer pointer */
  157. urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
  158. if ((subs->hwptr += count) >= runtime->buffer_size)
  159. subs->hwptr -= runtime->buffer_size;
  160. }
  161. else
  162. urb->transfer_buffer = subs->tmpbuf;
  163. urb->transfer_buffer_length = count * usX2Y->stride;
  164. return 0;
  165. }
  166. /*
  167. * process after playback data complete
  168. *
  169. * update the current position and call callback if a period is processed.
  170. */
  171. static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
  172. {
  173. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  174. int len = urb->actual_length / subs->usX2Y->stride;
  175. subs->transfer_done += len;
  176. subs->hwptr_done += len;
  177. if (subs->hwptr_done >= runtime->buffer_size)
  178. subs->hwptr_done -= runtime->buffer_size;
  179. if (subs->transfer_done >= runtime->period_size) {
  180. subs->transfer_done -= runtime->period_size;
  181. snd_pcm_period_elapsed(subs->pcm_substream);
  182. }
  183. }
  184. static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
  185. {
  186. int err;
  187. if (!urb)
  188. return -ENODEV;
  189. urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
  190. urb->hcpriv = NULL;
  191. urb->dev = subs->usX2Y->dev; /* we need to set this at each time */
  192. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  193. snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
  194. return err;
  195. }
  196. return 0;
  197. }
  198. static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
  199. struct snd_usX2Y_substream *playbacksubs,
  200. int frame)
  201. {
  202. int err, state;
  203. struct urb *urb = playbacksubs->completed_urb;
  204. state = atomic_read(&playbacksubs->state);
  205. if (NULL != urb) {
  206. if (state == state_RUNNING)
  207. usX2Y_urb_play_retire(playbacksubs, urb);
  208. else if (state >= state_PRERUNNING)
  209. atomic_inc(&playbacksubs->state);
  210. } else {
  211. switch (state) {
  212. case state_STARTING1:
  213. urb = playbacksubs->urb[0];
  214. atomic_inc(&playbacksubs->state);
  215. break;
  216. case state_STARTING2:
  217. urb = playbacksubs->urb[1];
  218. atomic_inc(&playbacksubs->state);
  219. break;
  220. }
  221. }
  222. if (urb) {
  223. if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
  224. (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
  225. return err;
  226. }
  227. }
  228. playbacksubs->completed_urb = NULL;
  229. state = atomic_read(&capsubs->state);
  230. if (state >= state_PREPARED) {
  231. if (state == state_RUNNING) {
  232. if ((err = usX2Y_urb_capt_retire(capsubs)))
  233. return err;
  234. } else if (state >= state_PRERUNNING)
  235. atomic_inc(&capsubs->state);
  236. if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
  237. return err;
  238. }
  239. capsubs->completed_urb = NULL;
  240. return 0;
  241. }
  242. static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
  243. {
  244. int s, u;
  245. for (s = 0; s < 4; s++) {
  246. struct snd_usX2Y_substream *subs = usX2Y->subs[s];
  247. if (subs) {
  248. snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
  249. atomic_set(&subs->state, state_STOPPED);
  250. }
  251. }
  252. for (s = 0; s < 4; s++) {
  253. struct snd_usX2Y_substream *subs = usX2Y->subs[s];
  254. if (subs) {
  255. if (atomic_read(&subs->state) >= state_PRERUNNING) {
  256. unsigned long flags;
  257. snd_pcm_stream_lock_irqsave(subs->pcm_substream, flags);
  258. snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  259. snd_pcm_stream_unlock_irqrestore(subs->pcm_substream, flags);
  260. }
  261. for (u = 0; u < NRURBS; u++) {
  262. struct urb *urb = subs->urb[u];
  263. if (NULL != urb)
  264. snd_printdd("%i status=%i start_frame=%i\n",
  265. u, urb->status, urb->start_frame);
  266. }
  267. }
  268. }
  269. usX2Y->prepare_subs = NULL;
  270. wake_up(&usX2Y->prepare_wait_queue);
  271. }
  272. static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
  273. struct snd_usX2Y_substream *subs, struct urb *urb)
  274. {
  275. snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
  276. urb->status = 0;
  277. usX2Y_clients_stop(usX2Y);
  278. }
  279. static void usX2Y_error_sequence(struct usX2Ydev *usX2Y,
  280. struct snd_usX2Y_substream *subs, struct urb *urb)
  281. {
  282. snd_printk(KERN_ERR
  283. "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
  284. "Most probably some urb of usb-frame %i is still missing.\n"
  285. "Cause could be too long delays in usb-hcd interrupt handling.\n",
  286. usb_get_current_frame_number(usX2Y->dev),
  287. subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
  288. usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
  289. usX2Y_clients_stop(usX2Y);
  290. }
  291. static void i_usX2Y_urb_complete(struct urb *urb)
  292. {
  293. struct snd_usX2Y_substream *subs = urb->context;
  294. struct usX2Ydev *usX2Y = subs->usX2Y;
  295. if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
  296. snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
  297. usb_get_current_frame_number(usX2Y->dev),
  298. subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
  299. urb->status, urb->start_frame);
  300. return;
  301. }
  302. if (unlikely(urb->status)) {
  303. usX2Y_error_urb_status(usX2Y, subs, urb);
  304. return;
  305. }
  306. if (likely((urb->start_frame & 0xFFFF) == (usX2Y->wait_iso_frame & 0xFFFF)))
  307. subs->completed_urb = urb;
  308. else {
  309. usX2Y_error_sequence(usX2Y, subs, urb);
  310. return;
  311. }
  312. {
  313. struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
  314. *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  315. if (capsubs->completed_urb &&
  316. atomic_read(&capsubs->state) >= state_PREPARED &&
  317. (playbacksubs->completed_urb ||
  318. atomic_read(&playbacksubs->state) < state_PREPARED)) {
  319. if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
  320. usX2Y->wait_iso_frame += nr_of_packs();
  321. else {
  322. snd_printdd("\n");
  323. usX2Y_clients_stop(usX2Y);
  324. }
  325. }
  326. }
  327. }
  328. static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
  329. void (*complete)(struct urb *))
  330. {
  331. int s, u;
  332. for (s = 0; s < 4; s++) {
  333. struct snd_usX2Y_substream *subs = usX2Y->subs[s];
  334. if (NULL != subs)
  335. for (u = 0; u < NRURBS; u++) {
  336. struct urb * urb = subs->urb[u];
  337. if (NULL != urb)
  338. urb->complete = complete;
  339. }
  340. }
  341. }
  342. static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
  343. {
  344. usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
  345. usX2Y->prepare_subs = NULL;
  346. }
  347. static void i_usX2Y_subs_startup(struct urb *urb)
  348. {
  349. struct snd_usX2Y_substream *subs = urb->context;
  350. struct usX2Ydev *usX2Y = subs->usX2Y;
  351. struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
  352. if (NULL != prepare_subs)
  353. if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
  354. usX2Y_subs_startup_finish(usX2Y);
  355. atomic_inc(&prepare_subs->state);
  356. wake_up(&usX2Y->prepare_wait_queue);
  357. }
  358. i_usX2Y_urb_complete(urb);
  359. }
  360. static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
  361. {
  362. snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
  363. subs, subs->endpoint, subs->urb[0], subs->urb[1]);
  364. /* reset the pointer */
  365. subs->hwptr = 0;
  366. subs->hwptr_done = 0;
  367. subs->transfer_done = 0;
  368. }
  369. static void usX2Y_urb_release(struct urb **urb, int free_tb)
  370. {
  371. if (*urb) {
  372. usb_kill_urb(*urb);
  373. if (free_tb)
  374. kfree((*urb)->transfer_buffer);
  375. usb_free_urb(*urb);
  376. *urb = NULL;
  377. }
  378. }
  379. /*
  380. * release a substreams urbs
  381. */
  382. static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
  383. {
  384. int i;
  385. snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
  386. for (i = 0; i < NRURBS; i++)
  387. usX2Y_urb_release(subs->urb + i,
  388. subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
  389. kfree(subs->tmpbuf);
  390. subs->tmpbuf = NULL;
  391. }
  392. /*
  393. * initialize a substream's urbs
  394. */
  395. static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
  396. {
  397. int i;
  398. unsigned int pipe;
  399. int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  400. struct usb_device *dev = subs->usX2Y->dev;
  401. pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
  402. usb_rcvisocpipe(dev, subs->endpoint);
  403. subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
  404. if (!subs->maxpacksize)
  405. return -EINVAL;
  406. if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
  407. subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
  408. if (NULL == subs->tmpbuf) {
  409. snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
  410. return -ENOMEM;
  411. }
  412. }
  413. /* allocate and initialize data urbs */
  414. for (i = 0; i < NRURBS; i++) {
  415. struct urb **purb = subs->urb + i;
  416. if (*purb) {
  417. usb_kill_urb(*purb);
  418. continue;
  419. }
  420. *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
  421. if (NULL == *purb) {
  422. usX2Y_urbs_release(subs);
  423. return -ENOMEM;
  424. }
  425. if (!is_playback && !(*purb)->transfer_buffer) {
  426. /* allocate a capture buffer per urb */
  427. (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
  428. if (NULL == (*purb)->transfer_buffer) {
  429. usX2Y_urbs_release(subs);
  430. return -ENOMEM;
  431. }
  432. }
  433. (*purb)->dev = dev;
  434. (*purb)->pipe = pipe;
  435. (*purb)->number_of_packets = nr_of_packs();
  436. (*purb)->context = subs;
  437. (*purb)->interval = 1;
  438. (*purb)->complete = i_usX2Y_subs_startup;
  439. }
  440. return 0;
  441. }
  442. static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
  443. {
  444. struct usX2Ydev *usX2Y = subs->usX2Y;
  445. usX2Y->prepare_subs = subs;
  446. subs->urb[0]->start_frame = -1;
  447. wmb();
  448. usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
  449. }
  450. static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
  451. {
  452. int i, err;
  453. struct usX2Ydev *usX2Y = subs->usX2Y;
  454. if ((err = usX2Y_urbs_allocate(subs)) < 0)
  455. return err;
  456. subs->completed_urb = NULL;
  457. for (i = 0; i < 4; i++) {
  458. struct snd_usX2Y_substream *subs = usX2Y->subs[i];
  459. if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
  460. goto start;
  461. }
  462. start:
  463. usX2Y_subs_startup(subs);
  464. for (i = 0; i < NRURBS; i++) {
  465. struct urb *urb = subs->urb[i];
  466. if (usb_pipein(urb->pipe)) {
  467. unsigned long pack;
  468. if (0 == i)
  469. atomic_set(&subs->state, state_STARTING3);
  470. urb->dev = usX2Y->dev;
  471. for (pack = 0; pack < nr_of_packs(); pack++) {
  472. urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
  473. urb->iso_frame_desc[pack].length = subs->maxpacksize;
  474. }
  475. urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
  476. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  477. snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
  478. err = -EPIPE;
  479. goto cleanup;
  480. } else
  481. if (i == 0)
  482. usX2Y->wait_iso_frame = urb->start_frame;
  483. urb->transfer_flags = 0;
  484. } else {
  485. atomic_set(&subs->state, state_STARTING1);
  486. break;
  487. }
  488. }
  489. err = 0;
  490. wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
  491. if (atomic_read(&subs->state) != state_PREPARED)
  492. err = -EPIPE;
  493. cleanup:
  494. if (err) {
  495. usX2Y_subs_startup_finish(usX2Y);
  496. usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
  497. }
  498. return err;
  499. }
  500. /*
  501. * return the current pcm pointer. just return the hwptr_done value.
  502. */
  503. static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
  504. {
  505. struct snd_usX2Y_substream *subs = substream->runtime->private_data;
  506. return subs->hwptr_done;
  507. }
  508. /*
  509. * start/stop substream
  510. */
  511. static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  512. {
  513. struct snd_usX2Y_substream *subs = substream->runtime->private_data;
  514. switch (cmd) {
  515. case SNDRV_PCM_TRIGGER_START:
  516. snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
  517. if (atomic_read(&subs->state) == state_PREPARED &&
  518. atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
  519. atomic_set(&subs->state, state_PRERUNNING);
  520. } else {
  521. snd_printdd("\n");
  522. return -EPIPE;
  523. }
  524. break;
  525. case SNDRV_PCM_TRIGGER_STOP:
  526. snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
  527. if (atomic_read(&subs->state) >= state_PRERUNNING)
  528. atomic_set(&subs->state, state_PREPARED);
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. return 0;
  534. }
  535. /*
  536. * allocate a buffer, setup samplerate
  537. *
  538. * so far we use a physically linear buffer although packetize transfer
  539. * doesn't need a continuous area.
  540. * if sg buffer is supported on the later version of alsa, we'll follow
  541. * that.
  542. */
  543. static struct s_c2
  544. {
  545. char c1, c2;
  546. }
  547. SetRate44100[] =
  548. {
  549. { 0x14, 0x08}, // this line sets 44100, well actually a little less
  550. { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
  551. { 0x18, 0x42},
  552. { 0x18, 0x45},
  553. { 0x18, 0x46},
  554. { 0x18, 0x48},
  555. { 0x18, 0x4A},
  556. { 0x18, 0x4C},
  557. { 0x18, 0x4E},
  558. { 0x18, 0x50},
  559. { 0x18, 0x52},
  560. { 0x18, 0x54},
  561. { 0x18, 0x56},
  562. { 0x18, 0x58},
  563. { 0x18, 0x5A},
  564. { 0x18, 0x5C},
  565. { 0x18, 0x5E},
  566. { 0x18, 0x60},
  567. { 0x18, 0x62},
  568. { 0x18, 0x64},
  569. { 0x18, 0x66},
  570. { 0x18, 0x68},
  571. { 0x18, 0x6A},
  572. { 0x18, 0x6C},
  573. { 0x18, 0x6E},
  574. { 0x18, 0x70},
  575. { 0x18, 0x72},
  576. { 0x18, 0x74},
  577. { 0x18, 0x76},
  578. { 0x18, 0x78},
  579. { 0x18, 0x7A},
  580. { 0x18, 0x7C},
  581. { 0x18, 0x7E}
  582. };
  583. static struct s_c2 SetRate48000[] =
  584. {
  585. { 0x14, 0x09}, // this line sets 48000, well actually a little less
  586. { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
  587. { 0x18, 0x42},
  588. { 0x18, 0x45},
  589. { 0x18, 0x46},
  590. { 0x18, 0x48},
  591. { 0x18, 0x4A},
  592. { 0x18, 0x4C},
  593. { 0x18, 0x4E},
  594. { 0x18, 0x50},
  595. { 0x18, 0x52},
  596. { 0x18, 0x54},
  597. { 0x18, 0x56},
  598. { 0x18, 0x58},
  599. { 0x18, 0x5A},
  600. { 0x18, 0x5C},
  601. { 0x18, 0x5E},
  602. { 0x18, 0x60},
  603. { 0x18, 0x62},
  604. { 0x18, 0x64},
  605. { 0x18, 0x66},
  606. { 0x18, 0x68},
  607. { 0x18, 0x6A},
  608. { 0x18, 0x6C},
  609. { 0x18, 0x6E},
  610. { 0x18, 0x70},
  611. { 0x18, 0x73},
  612. { 0x18, 0x74},
  613. { 0x18, 0x76},
  614. { 0x18, 0x78},
  615. { 0x18, 0x7A},
  616. { 0x18, 0x7C},
  617. { 0x18, 0x7E}
  618. };
  619. #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
  620. static void i_usX2Y_04Int(struct urb *urb)
  621. {
  622. struct usX2Ydev *usX2Y = urb->context;
  623. if (urb->status)
  624. snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
  625. if (0 == --usX2Y->US04->len)
  626. wake_up(&usX2Y->In04WaitQueue);
  627. }
  628. static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
  629. {
  630. int err = 0, i;
  631. struct snd_usX2Y_urbSeq *us = NULL;
  632. int *usbdata = NULL;
  633. struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
  634. if (usX2Y->rate != rate) {
  635. us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
  636. if (NULL == us) {
  637. err = -ENOMEM;
  638. goto cleanup;
  639. }
  640. usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
  641. if (NULL == usbdata) {
  642. err = -ENOMEM;
  643. goto cleanup;
  644. }
  645. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  646. if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  647. err = -ENOMEM;
  648. goto cleanup;
  649. }
  650. ((char*)(usbdata + i))[0] = ra[i].c1;
  651. ((char*)(usbdata + i))[1] = ra[i].c2;
  652. usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4),
  653. usbdata + i, 2, i_usX2Y_04Int, usX2Y);
  654. }
  655. us->submitted = 0;
  656. us->len = NOOF_SETRATE_URBS;
  657. usX2Y->US04 = us;
  658. wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
  659. usX2Y->US04 = NULL;
  660. if (us->len)
  661. err = -ENODEV;
  662. cleanup:
  663. if (us) {
  664. us->submitted = 2*NOOF_SETRATE_URBS;
  665. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  666. struct urb *urb = us->urb[i];
  667. if (urb->status) {
  668. if (!err)
  669. err = -ENODEV;
  670. usb_kill_urb(urb);
  671. }
  672. usb_free_urb(urb);
  673. }
  674. usX2Y->US04 = NULL;
  675. kfree(usbdata);
  676. kfree(us);
  677. if (!err)
  678. usX2Y->rate = rate;
  679. }
  680. }
  681. return err;
  682. }
  683. static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
  684. {
  685. int alternate, err;
  686. struct list_head* p;
  687. if (format == SNDRV_PCM_FORMAT_S24_3LE) {
  688. alternate = 2;
  689. usX2Y->stride = 6;
  690. } else {
  691. alternate = 1;
  692. usX2Y->stride = 4;
  693. }
  694. list_for_each(p, &usX2Y->midi_list) {
  695. snd_usbmidi_input_stop(p);
  696. }
  697. usb_kill_urb(usX2Y->In04urb);
  698. if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) {
  699. snd_printk(KERN_ERR "usb_set_interface error \n");
  700. return err;
  701. }
  702. usX2Y->In04urb->dev = usX2Y->dev;
  703. err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
  704. list_for_each(p, &usX2Y->midi_list) {
  705. snd_usbmidi_input_start(p);
  706. }
  707. usX2Y->format = format;
  708. usX2Y->rate = 0;
  709. return err;
  710. }
  711. static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
  712. struct snd_pcm_hw_params *hw_params)
  713. {
  714. int err = 0;
  715. unsigned int rate = params_rate(hw_params);
  716. snd_pcm_format_t format = params_format(hw_params);
  717. struct snd_card *card = substream->pstr->pcm->card;
  718. struct list_head *list;
  719. snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
  720. // all pcm substreams off one usX2Y have to operate at the same rate & format
  721. list_for_each(list, &card->devices) {
  722. struct snd_device *dev;
  723. struct snd_pcm *pcm;
  724. int s;
  725. dev = snd_device(list);
  726. if (dev->type != SNDRV_DEV_PCM)
  727. continue;
  728. pcm = dev->device_data;
  729. for (s = 0; s < 2; ++s) {
  730. struct snd_pcm_substream *test_substream;
  731. test_substream = pcm->streams[s].substream;
  732. if (test_substream && test_substream != substream &&
  733. test_substream->runtime &&
  734. ((test_substream->runtime->format &&
  735. test_substream->runtime->format != format) ||
  736. (test_substream->runtime->rate &&
  737. test_substream->runtime->rate != rate)))
  738. return -EINVAL;
  739. }
  740. }
  741. if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
  742. snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
  743. substream, params_buffer_bytes(hw_params), err);
  744. return err;
  745. }
  746. return 0;
  747. }
  748. /*
  749. * free the buffer
  750. */
  751. static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
  752. {
  753. struct snd_pcm_runtime *runtime = substream->runtime;
  754. struct snd_usX2Y_substream *subs = runtime->private_data;
  755. mutex_lock(&subs->usX2Y->prepare_mutex);
  756. snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
  757. if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
  758. struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
  759. atomic_set(&subs->state, state_STOPPED);
  760. usX2Y_urbs_release(subs);
  761. if (!cap_subs->pcm_substream ||
  762. !cap_subs->pcm_substream->runtime ||
  763. !cap_subs->pcm_substream->runtime->status ||
  764. cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
  765. atomic_set(&cap_subs->state, state_STOPPED);
  766. usX2Y_urbs_release(cap_subs);
  767. }
  768. } else {
  769. struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  770. if (atomic_read(&playback_subs->state) < state_PREPARED) {
  771. atomic_set(&subs->state, state_STOPPED);
  772. usX2Y_urbs_release(subs);
  773. }
  774. }
  775. mutex_unlock(&subs->usX2Y->prepare_mutex);
  776. return snd_pcm_lib_free_pages(substream);
  777. }
  778. /*
  779. * prepare callback
  780. *
  781. * set format and initialize urbs
  782. */
  783. static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
  784. {
  785. struct snd_pcm_runtime *runtime = substream->runtime;
  786. struct snd_usX2Y_substream *subs = runtime->private_data;
  787. struct usX2Ydev *usX2Y = subs->usX2Y;
  788. struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
  789. int err = 0;
  790. snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
  791. mutex_lock(&usX2Y->prepare_mutex);
  792. usX2Y_subs_prepare(subs);
  793. // Start hardware streams
  794. // SyncStream first....
  795. if (atomic_read(&capsubs->state) < state_PREPARED) {
  796. if (usX2Y->format != runtime->format)
  797. if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
  798. goto up_prepare_mutex;
  799. if (usX2Y->rate != runtime->rate)
  800. if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
  801. goto up_prepare_mutex;
  802. snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
  803. if (0 > (err = usX2Y_urbs_start(capsubs)))
  804. goto up_prepare_mutex;
  805. }
  806. if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
  807. err = usX2Y_urbs_start(subs);
  808. up_prepare_mutex:
  809. mutex_unlock(&usX2Y->prepare_mutex);
  810. return err;
  811. }
  812. static struct snd_pcm_hardware snd_usX2Y_2c =
  813. {
  814. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  815. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  816. SNDRV_PCM_INFO_MMAP_VALID |
  817. SNDRV_PCM_INFO_BATCH),
  818. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
  819. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  820. .rate_min = 44100,
  821. .rate_max = 48000,
  822. .channels_min = 2,
  823. .channels_max = 2,
  824. .buffer_bytes_max = (2*128*1024),
  825. .period_bytes_min = 64,
  826. .period_bytes_max = (128*1024),
  827. .periods_min = 2,
  828. .periods_max = 1024,
  829. .fifo_size = 0
  830. };
  831. static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
  832. {
  833. struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **)
  834. snd_pcm_substream_chip(substream))[substream->stream];
  835. struct snd_pcm_runtime *runtime = substream->runtime;
  836. if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
  837. return -EBUSY;
  838. runtime->hw = snd_usX2Y_2c;
  839. runtime->private_data = subs;
  840. subs->pcm_substream = substream;
  841. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
  842. return 0;
  843. }
  844. static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
  845. {
  846. struct snd_pcm_runtime *runtime = substream->runtime;
  847. struct snd_usX2Y_substream *subs = runtime->private_data;
  848. subs->pcm_substream = NULL;
  849. return 0;
  850. }
  851. static struct snd_pcm_ops snd_usX2Y_pcm_ops =
  852. {
  853. .open = snd_usX2Y_pcm_open,
  854. .close = snd_usX2Y_pcm_close,
  855. .ioctl = snd_pcm_lib_ioctl,
  856. .hw_params = snd_usX2Y_pcm_hw_params,
  857. .hw_free = snd_usX2Y_pcm_hw_free,
  858. .prepare = snd_usX2Y_pcm_prepare,
  859. .trigger = snd_usX2Y_pcm_trigger,
  860. .pointer = snd_usX2Y_pcm_pointer,
  861. };
  862. /*
  863. * free a usb stream instance
  864. */
  865. static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
  866. {
  867. kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
  868. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
  869. kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
  870. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
  871. }
  872. static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
  873. {
  874. struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
  875. if (usX2Y_stream)
  876. usX2Y_audio_stream_free(usX2Y_stream);
  877. }
  878. static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
  879. {
  880. struct snd_pcm *pcm;
  881. int err, i;
  882. struct snd_usX2Y_substream **usX2Y_substream =
  883. usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs;
  884. for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
  885. i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
  886. usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
  887. if (NULL == usX2Y_substream[i]) {
  888. snd_printk(KERN_ERR "cannot malloc\n");
  889. return -ENOMEM;
  890. }
  891. usX2Y_substream[i]->usX2Y = usX2Y(card);
  892. }
  893. if (playback_endpoint)
  894. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
  895. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
  896. err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs,
  897. playback_endpoint ? 1 : 0, 1,
  898. &pcm);
  899. if (err < 0) {
  900. usX2Y_audio_stream_free(usX2Y_substream);
  901. return err;
  902. }
  903. if (playback_endpoint)
  904. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
  905. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
  906. pcm->private_data = usX2Y_substream;
  907. pcm->private_free = snd_usX2Y_pcm_private_free;
  908. pcm->info_flags = 0;
  909. sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs);
  910. if ((playback_endpoint &&
  911. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  912. SNDRV_DMA_TYPE_CONTINUOUS,
  913. snd_dma_continuous_data(GFP_KERNEL),
  914. 64*1024, 128*1024))) ||
  915. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  916. SNDRV_DMA_TYPE_CONTINUOUS,
  917. snd_dma_continuous_data(GFP_KERNEL),
  918. 64*1024, 128*1024))) {
  919. snd_usX2Y_pcm_private_free(pcm);
  920. return err;
  921. }
  922. usX2Y(card)->pcm_devs++;
  923. return 0;
  924. }
  925. /*
  926. * create a chip instance and set its names.
  927. */
  928. int usX2Y_audio_create(struct snd_card *card)
  929. {
  930. int err = 0;
  931. INIT_LIST_HEAD(&usX2Y(card)->pcm_list);
  932. if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
  933. return err;
  934. if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428)
  935. if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
  936. return err;
  937. if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122)
  938. err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
  939. return err;
  940. }