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