usbusx2yaudio.c 28 KB

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