usbusx2yaudio.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. snd_pcm_stop_xrun(subs->pcm_substream);
  257. for (u = 0; u < NRURBS; u++) {
  258. struct urb *urb = subs->urb[u];
  259. if (NULL != urb)
  260. snd_printdd("%i status=%i start_frame=%i\n",
  261. u, urb->status, urb->start_frame);
  262. }
  263. }
  264. }
  265. usX2Y->prepare_subs = NULL;
  266. wake_up(&usX2Y->prepare_wait_queue);
  267. }
  268. static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
  269. struct snd_usX2Y_substream *subs, struct urb *urb)
  270. {
  271. snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
  272. urb->status = 0;
  273. usX2Y_clients_stop(usX2Y);
  274. }
  275. static void i_usX2Y_urb_complete(struct urb *urb)
  276. {
  277. struct snd_usX2Y_substream *subs = urb->context;
  278. struct usX2Ydev *usX2Y = subs->usX2Y;
  279. if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
  280. snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
  281. usb_get_current_frame_number(usX2Y->dev),
  282. subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
  283. urb->status, urb->start_frame);
  284. return;
  285. }
  286. if (unlikely(urb->status)) {
  287. usX2Y_error_urb_status(usX2Y, subs, urb);
  288. return;
  289. }
  290. subs->completed_urb = urb;
  291. {
  292. struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
  293. *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  294. if (capsubs->completed_urb &&
  295. atomic_read(&capsubs->state) >= state_PREPARED &&
  296. (playbacksubs->completed_urb ||
  297. atomic_read(&playbacksubs->state) < state_PREPARED)) {
  298. if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
  299. usX2Y->wait_iso_frame += nr_of_packs();
  300. else {
  301. snd_printdd("\n");
  302. usX2Y_clients_stop(usX2Y);
  303. }
  304. }
  305. }
  306. }
  307. static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
  308. void (*complete)(struct urb *))
  309. {
  310. int s, u;
  311. for (s = 0; s < 4; s++) {
  312. struct snd_usX2Y_substream *subs = usX2Y->subs[s];
  313. if (NULL != subs)
  314. for (u = 0; u < NRURBS; u++) {
  315. struct urb * urb = subs->urb[u];
  316. if (NULL != urb)
  317. urb->complete = complete;
  318. }
  319. }
  320. }
  321. static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
  322. {
  323. usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
  324. usX2Y->prepare_subs = NULL;
  325. }
  326. static void i_usX2Y_subs_startup(struct urb *urb)
  327. {
  328. struct snd_usX2Y_substream *subs = urb->context;
  329. struct usX2Ydev *usX2Y = subs->usX2Y;
  330. struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
  331. if (NULL != prepare_subs)
  332. if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
  333. usX2Y_subs_startup_finish(usX2Y);
  334. atomic_inc(&prepare_subs->state);
  335. wake_up(&usX2Y->prepare_wait_queue);
  336. }
  337. i_usX2Y_urb_complete(urb);
  338. }
  339. static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
  340. {
  341. snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
  342. subs, subs->endpoint, subs->urb[0], subs->urb[1]);
  343. /* reset the pointer */
  344. subs->hwptr = 0;
  345. subs->hwptr_done = 0;
  346. subs->transfer_done = 0;
  347. }
  348. static void usX2Y_urb_release(struct urb **urb, int free_tb)
  349. {
  350. if (*urb) {
  351. usb_kill_urb(*urb);
  352. if (free_tb)
  353. kfree((*urb)->transfer_buffer);
  354. usb_free_urb(*urb);
  355. *urb = NULL;
  356. }
  357. }
  358. /*
  359. * release a substreams urbs
  360. */
  361. static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
  362. {
  363. int i;
  364. snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
  365. for (i = 0; i < NRURBS; i++)
  366. usX2Y_urb_release(subs->urb + i,
  367. subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
  368. kfree(subs->tmpbuf);
  369. subs->tmpbuf = NULL;
  370. }
  371. /*
  372. * initialize a substream's urbs
  373. */
  374. static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
  375. {
  376. int i;
  377. unsigned int pipe;
  378. int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  379. struct usb_device *dev = subs->usX2Y->dev;
  380. pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
  381. usb_rcvisocpipe(dev, subs->endpoint);
  382. subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
  383. if (!subs->maxpacksize)
  384. return -EINVAL;
  385. if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
  386. subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
  387. if (NULL == subs->tmpbuf) {
  388. snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
  389. return -ENOMEM;
  390. }
  391. }
  392. /* allocate and initialize data urbs */
  393. for (i = 0; i < NRURBS; i++) {
  394. struct urb **purb = subs->urb + i;
  395. if (*purb) {
  396. usb_kill_urb(*purb);
  397. continue;
  398. }
  399. *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
  400. if (NULL == *purb) {
  401. usX2Y_urbs_release(subs);
  402. return -ENOMEM;
  403. }
  404. if (!is_playback && !(*purb)->transfer_buffer) {
  405. /* allocate a capture buffer per urb */
  406. (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
  407. if (NULL == (*purb)->transfer_buffer) {
  408. usX2Y_urbs_release(subs);
  409. return -ENOMEM;
  410. }
  411. }
  412. (*purb)->dev = dev;
  413. (*purb)->pipe = pipe;
  414. (*purb)->number_of_packets = nr_of_packs();
  415. (*purb)->context = subs;
  416. (*purb)->interval = 1;
  417. (*purb)->complete = i_usX2Y_subs_startup;
  418. }
  419. return 0;
  420. }
  421. static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
  422. {
  423. struct usX2Ydev *usX2Y = subs->usX2Y;
  424. usX2Y->prepare_subs = subs;
  425. subs->urb[0]->start_frame = -1;
  426. wmb();
  427. usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
  428. }
  429. static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
  430. {
  431. int i, err;
  432. struct usX2Ydev *usX2Y = subs->usX2Y;
  433. if ((err = usX2Y_urbs_allocate(subs)) < 0)
  434. return err;
  435. subs->completed_urb = NULL;
  436. for (i = 0; i < 4; i++) {
  437. struct snd_usX2Y_substream *subs = usX2Y->subs[i];
  438. if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
  439. goto start;
  440. }
  441. start:
  442. usX2Y_subs_startup(subs);
  443. for (i = 0; i < NRURBS; i++) {
  444. struct urb *urb = subs->urb[i];
  445. if (usb_pipein(urb->pipe)) {
  446. unsigned long pack;
  447. if (0 == i)
  448. atomic_set(&subs->state, state_STARTING3);
  449. urb->dev = usX2Y->dev;
  450. for (pack = 0; pack < nr_of_packs(); pack++) {
  451. urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
  452. urb->iso_frame_desc[pack].length = subs->maxpacksize;
  453. }
  454. urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
  455. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  456. snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
  457. err = -EPIPE;
  458. goto cleanup;
  459. } else
  460. if (i == 0)
  461. usX2Y->wait_iso_frame = urb->start_frame;
  462. urb->transfer_flags = 0;
  463. } else {
  464. atomic_set(&subs->state, state_STARTING1);
  465. break;
  466. }
  467. }
  468. err = 0;
  469. wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
  470. if (atomic_read(&subs->state) != state_PREPARED)
  471. err = -EPIPE;
  472. cleanup:
  473. if (err) {
  474. usX2Y_subs_startup_finish(usX2Y);
  475. usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
  476. }
  477. return err;
  478. }
  479. /*
  480. * return the current pcm pointer. just return the hwptr_done value.
  481. */
  482. static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
  483. {
  484. struct snd_usX2Y_substream *subs = substream->runtime->private_data;
  485. return subs->hwptr_done;
  486. }
  487. /*
  488. * start/stop substream
  489. */
  490. static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  491. {
  492. struct snd_usX2Y_substream *subs = substream->runtime->private_data;
  493. switch (cmd) {
  494. case SNDRV_PCM_TRIGGER_START:
  495. snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
  496. if (atomic_read(&subs->state) == state_PREPARED &&
  497. atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
  498. atomic_set(&subs->state, state_PRERUNNING);
  499. } else {
  500. snd_printdd("\n");
  501. return -EPIPE;
  502. }
  503. break;
  504. case SNDRV_PCM_TRIGGER_STOP:
  505. snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
  506. if (atomic_read(&subs->state) >= state_PRERUNNING)
  507. atomic_set(&subs->state, state_PREPARED);
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. return 0;
  513. }
  514. /*
  515. * allocate a buffer, setup samplerate
  516. *
  517. * so far we use a physically linear buffer although packetize transfer
  518. * doesn't need a continuous area.
  519. * if sg buffer is supported on the later version of alsa, we'll follow
  520. * that.
  521. */
  522. static struct s_c2
  523. {
  524. char c1, c2;
  525. }
  526. SetRate44100[] =
  527. {
  528. { 0x14, 0x08}, // this line sets 44100, well actually a little less
  529. { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
  530. { 0x18, 0x42},
  531. { 0x18, 0x45},
  532. { 0x18, 0x46},
  533. { 0x18, 0x48},
  534. { 0x18, 0x4A},
  535. { 0x18, 0x4C},
  536. { 0x18, 0x4E},
  537. { 0x18, 0x50},
  538. { 0x18, 0x52},
  539. { 0x18, 0x54},
  540. { 0x18, 0x56},
  541. { 0x18, 0x58},
  542. { 0x18, 0x5A},
  543. { 0x18, 0x5C},
  544. { 0x18, 0x5E},
  545. { 0x18, 0x60},
  546. { 0x18, 0x62},
  547. { 0x18, 0x64},
  548. { 0x18, 0x66},
  549. { 0x18, 0x68},
  550. { 0x18, 0x6A},
  551. { 0x18, 0x6C},
  552. { 0x18, 0x6E},
  553. { 0x18, 0x70},
  554. { 0x18, 0x72},
  555. { 0x18, 0x74},
  556. { 0x18, 0x76},
  557. { 0x18, 0x78},
  558. { 0x18, 0x7A},
  559. { 0x18, 0x7C},
  560. { 0x18, 0x7E}
  561. };
  562. static struct s_c2 SetRate48000[] =
  563. {
  564. { 0x14, 0x09}, // this line sets 48000, well actually a little less
  565. { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
  566. { 0x18, 0x42},
  567. { 0x18, 0x45},
  568. { 0x18, 0x46},
  569. { 0x18, 0x48},
  570. { 0x18, 0x4A},
  571. { 0x18, 0x4C},
  572. { 0x18, 0x4E},
  573. { 0x18, 0x50},
  574. { 0x18, 0x52},
  575. { 0x18, 0x54},
  576. { 0x18, 0x56},
  577. { 0x18, 0x58},
  578. { 0x18, 0x5A},
  579. { 0x18, 0x5C},
  580. { 0x18, 0x5E},
  581. { 0x18, 0x60},
  582. { 0x18, 0x62},
  583. { 0x18, 0x64},
  584. { 0x18, 0x66},
  585. { 0x18, 0x68},
  586. { 0x18, 0x6A},
  587. { 0x18, 0x6C},
  588. { 0x18, 0x6E},
  589. { 0x18, 0x70},
  590. { 0x18, 0x73},
  591. { 0x18, 0x74},
  592. { 0x18, 0x76},
  593. { 0x18, 0x78},
  594. { 0x18, 0x7A},
  595. { 0x18, 0x7C},
  596. { 0x18, 0x7E}
  597. };
  598. #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
  599. static void i_usX2Y_04Int(struct urb *urb)
  600. {
  601. struct usX2Ydev *usX2Y = urb->context;
  602. if (urb->status)
  603. snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
  604. if (0 == --usX2Y->US04->len)
  605. wake_up(&usX2Y->In04WaitQueue);
  606. }
  607. static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
  608. {
  609. int err = 0, i;
  610. struct snd_usX2Y_urbSeq *us = NULL;
  611. int *usbdata = NULL;
  612. struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
  613. if (usX2Y->rate != rate) {
  614. us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
  615. if (NULL == us) {
  616. err = -ENOMEM;
  617. goto cleanup;
  618. }
  619. usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
  620. if (NULL == usbdata) {
  621. err = -ENOMEM;
  622. goto cleanup;
  623. }
  624. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  625. if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  626. err = -ENOMEM;
  627. goto cleanup;
  628. }
  629. ((char*)(usbdata + i))[0] = ra[i].c1;
  630. ((char*)(usbdata + i))[1] = ra[i].c2;
  631. usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4),
  632. usbdata + i, 2, i_usX2Y_04Int, usX2Y);
  633. }
  634. us->submitted = 0;
  635. us->len = NOOF_SETRATE_URBS;
  636. usX2Y->US04 = us;
  637. wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
  638. usX2Y->US04 = NULL;
  639. if (us->len)
  640. err = -ENODEV;
  641. cleanup:
  642. if (us) {
  643. us->submitted = 2*NOOF_SETRATE_URBS;
  644. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  645. struct urb *urb = us->urb[i];
  646. if (urb->status) {
  647. if (!err)
  648. err = -ENODEV;
  649. usb_kill_urb(urb);
  650. }
  651. usb_free_urb(urb);
  652. }
  653. usX2Y->US04 = NULL;
  654. kfree(usbdata);
  655. kfree(us);
  656. if (!err)
  657. usX2Y->rate = rate;
  658. }
  659. }
  660. return err;
  661. }
  662. static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
  663. {
  664. int alternate, err;
  665. struct list_head* p;
  666. if (format == SNDRV_PCM_FORMAT_S24_3LE) {
  667. alternate = 2;
  668. usX2Y->stride = 6;
  669. } else {
  670. alternate = 1;
  671. usX2Y->stride = 4;
  672. }
  673. list_for_each(p, &usX2Y->midi_list) {
  674. snd_usbmidi_input_stop(p);
  675. }
  676. usb_kill_urb(usX2Y->In04urb);
  677. if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) {
  678. snd_printk(KERN_ERR "usb_set_interface error \n");
  679. return err;
  680. }
  681. usX2Y->In04urb->dev = usX2Y->dev;
  682. err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
  683. list_for_each(p, &usX2Y->midi_list) {
  684. snd_usbmidi_input_start(p);
  685. }
  686. usX2Y->format = format;
  687. usX2Y->rate = 0;
  688. return err;
  689. }
  690. static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
  691. struct snd_pcm_hw_params *hw_params)
  692. {
  693. int err = 0;
  694. unsigned int rate = params_rate(hw_params);
  695. snd_pcm_format_t format = params_format(hw_params);
  696. struct snd_card *card = substream->pstr->pcm->card;
  697. struct usX2Ydev *dev = usX2Y(card);
  698. int i;
  699. mutex_lock(&usX2Y(card)->pcm_mutex);
  700. snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
  701. /* all pcm substreams off one usX2Y have to operate at the same
  702. * rate & format
  703. */
  704. for (i = 0; i < dev->pcm_devs * 2; i++) {
  705. struct snd_usX2Y_substream *subs = dev->subs[i];
  706. struct snd_pcm_substream *test_substream;
  707. if (!subs)
  708. continue;
  709. test_substream = subs->pcm_substream;
  710. if (!test_substream || test_substream == substream ||
  711. !test_substream->runtime)
  712. continue;
  713. if ((test_substream->runtime->format &&
  714. test_substream->runtime->format != format) ||
  715. (test_substream->runtime->rate &&
  716. test_substream->runtime->rate != rate)) {
  717. err = -EINVAL;
  718. goto error;
  719. }
  720. }
  721. err = snd_pcm_lib_malloc_pages(substream,
  722. params_buffer_bytes(hw_params));
  723. if (err < 0) {
  724. snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
  725. substream, params_buffer_bytes(hw_params), err);
  726. goto error;
  727. }
  728. error:
  729. mutex_unlock(&usX2Y(card)->pcm_mutex);
  730. return err;
  731. }
  732. /*
  733. * free the buffer
  734. */
  735. static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
  736. {
  737. struct snd_pcm_runtime *runtime = substream->runtime;
  738. struct snd_usX2Y_substream *subs = runtime->private_data;
  739. mutex_lock(&subs->usX2Y->pcm_mutex);
  740. snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
  741. if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
  742. struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
  743. atomic_set(&subs->state, state_STOPPED);
  744. usX2Y_urbs_release(subs);
  745. if (!cap_subs->pcm_substream ||
  746. !cap_subs->pcm_substream->runtime ||
  747. !cap_subs->pcm_substream->runtime->status ||
  748. cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
  749. atomic_set(&cap_subs->state, state_STOPPED);
  750. usX2Y_urbs_release(cap_subs);
  751. }
  752. } else {
  753. struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  754. if (atomic_read(&playback_subs->state) < state_PREPARED) {
  755. atomic_set(&subs->state, state_STOPPED);
  756. usX2Y_urbs_release(subs);
  757. }
  758. }
  759. mutex_unlock(&subs->usX2Y->pcm_mutex);
  760. return snd_pcm_lib_free_pages(substream);
  761. }
  762. /*
  763. * prepare callback
  764. *
  765. * set format and initialize urbs
  766. */
  767. static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
  768. {
  769. struct snd_pcm_runtime *runtime = substream->runtime;
  770. struct snd_usX2Y_substream *subs = runtime->private_data;
  771. struct usX2Ydev *usX2Y = subs->usX2Y;
  772. struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
  773. int err = 0;
  774. snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
  775. mutex_lock(&usX2Y->pcm_mutex);
  776. usX2Y_subs_prepare(subs);
  777. // Start hardware streams
  778. // SyncStream first....
  779. if (atomic_read(&capsubs->state) < state_PREPARED) {
  780. if (usX2Y->format != runtime->format)
  781. if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
  782. goto up_prepare_mutex;
  783. if (usX2Y->rate != runtime->rate)
  784. if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
  785. goto up_prepare_mutex;
  786. snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
  787. if (0 > (err = usX2Y_urbs_start(capsubs)))
  788. goto up_prepare_mutex;
  789. }
  790. if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
  791. err = usX2Y_urbs_start(subs);
  792. up_prepare_mutex:
  793. mutex_unlock(&usX2Y->pcm_mutex);
  794. return err;
  795. }
  796. static struct snd_pcm_hardware snd_usX2Y_2c =
  797. {
  798. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  799. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  800. SNDRV_PCM_INFO_MMAP_VALID |
  801. SNDRV_PCM_INFO_BATCH),
  802. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
  803. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  804. .rate_min = 44100,
  805. .rate_max = 48000,
  806. .channels_min = 2,
  807. .channels_max = 2,
  808. .buffer_bytes_max = (2*128*1024),
  809. .period_bytes_min = 64,
  810. .period_bytes_max = (128*1024),
  811. .periods_min = 2,
  812. .periods_max = 1024,
  813. .fifo_size = 0
  814. };
  815. static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
  816. {
  817. struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **)
  818. snd_pcm_substream_chip(substream))[substream->stream];
  819. struct snd_pcm_runtime *runtime = substream->runtime;
  820. if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
  821. return -EBUSY;
  822. runtime->hw = snd_usX2Y_2c;
  823. runtime->private_data = subs;
  824. subs->pcm_substream = substream;
  825. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
  826. return 0;
  827. }
  828. static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
  829. {
  830. struct snd_pcm_runtime *runtime = substream->runtime;
  831. struct snd_usX2Y_substream *subs = runtime->private_data;
  832. subs->pcm_substream = NULL;
  833. return 0;
  834. }
  835. static struct snd_pcm_ops snd_usX2Y_pcm_ops =
  836. {
  837. .open = snd_usX2Y_pcm_open,
  838. .close = snd_usX2Y_pcm_close,
  839. .ioctl = snd_pcm_lib_ioctl,
  840. .hw_params = snd_usX2Y_pcm_hw_params,
  841. .hw_free = snd_usX2Y_pcm_hw_free,
  842. .prepare = snd_usX2Y_pcm_prepare,
  843. .trigger = snd_usX2Y_pcm_trigger,
  844. .pointer = snd_usX2Y_pcm_pointer,
  845. };
  846. /*
  847. * free a usb stream instance
  848. */
  849. static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
  850. {
  851. kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
  852. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
  853. kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
  854. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
  855. }
  856. static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
  857. {
  858. struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
  859. if (usX2Y_stream)
  860. usX2Y_audio_stream_free(usX2Y_stream);
  861. }
  862. static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
  863. {
  864. struct snd_pcm *pcm;
  865. int err, i;
  866. struct snd_usX2Y_substream **usX2Y_substream =
  867. usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs;
  868. for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
  869. i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
  870. usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
  871. if (NULL == usX2Y_substream[i]) {
  872. snd_printk(KERN_ERR "cannot malloc\n");
  873. return -ENOMEM;
  874. }
  875. usX2Y_substream[i]->usX2Y = usX2Y(card);
  876. }
  877. if (playback_endpoint)
  878. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
  879. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
  880. err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs,
  881. playback_endpoint ? 1 : 0, 1,
  882. &pcm);
  883. if (err < 0) {
  884. usX2Y_audio_stream_free(usX2Y_substream);
  885. return err;
  886. }
  887. if (playback_endpoint)
  888. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
  889. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
  890. pcm->private_data = usX2Y_substream;
  891. pcm->private_free = snd_usX2Y_pcm_private_free;
  892. pcm->info_flags = 0;
  893. sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs);
  894. if ((playback_endpoint &&
  895. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  896. SNDRV_DMA_TYPE_CONTINUOUS,
  897. snd_dma_continuous_data(GFP_KERNEL),
  898. 64*1024, 128*1024))) ||
  899. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  900. SNDRV_DMA_TYPE_CONTINUOUS,
  901. snd_dma_continuous_data(GFP_KERNEL),
  902. 64*1024, 128*1024))) {
  903. snd_usX2Y_pcm_private_free(pcm);
  904. return err;
  905. }
  906. usX2Y(card)->pcm_devs++;
  907. return 0;
  908. }
  909. /*
  910. * create a chip instance and set its names.
  911. */
  912. int usX2Y_audio_create(struct snd_card *card)
  913. {
  914. int err = 0;
  915. INIT_LIST_HEAD(&usX2Y(card)->pcm_list);
  916. if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
  917. return err;
  918. if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428)
  919. if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
  920. return err;
  921. if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122)
  922. err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
  923. return err;
  924. }