usbusx2yaudio.c 27 KB

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