usbusx2yaudio.c 29 KB

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