usbusx2yaudio.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * US-X2Y AUDIO
  3. * Copyright (c) 2002-2004 by Karsten Wiese
  4. *
  5. * based on
  6. *
  7. * (Tentative) USB Audio Driver for ALSA
  8. *
  9. * Main and PCM part
  10. *
  11. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  12. *
  13. * Many codes borrowed from audio.c by
  14. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  15. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  16. *
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  31. */
  32. #include <sound/driver.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/usb.h>
  35. #include <sound/core.h>
  36. #include <sound/info.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include "usx2y.h"
  40. #include "usbusx2y.h"
  41. #define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
  42. 1 to 4 have been tested ok on uhci.
  43. To use 3 on ohci, you'd need a patch:
  44. look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
  45. "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
  46. .
  47. 1, 2 and 4 work out of the box on ohci, if I recall correctly.
  48. Bigger is safer operation,
  49. smaller gives lower latencies.
  50. */
  51. #define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
  52. nrpacks set to 1, you might as well comment
  53. this #define out, and thereby produce smaller, faster code.
  54. You'd also set USX2Y_NRPACKS to 1 then.
  55. */
  56. #ifdef USX2Y_NRPACKS_VARIABLE
  57. static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
  58. #define nr_of_packs() nrpacks
  59. module_param(nrpacks, int, 0444);
  60. MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
  61. #else
  62. #define nr_of_packs() USX2Y_NRPACKS
  63. #endif
  64. static int usX2Y_urb_capt_retire(snd_usX2Y_substream_t *subs)
  65. {
  66. struct urb *urb = subs->completed_urb;
  67. snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
  68. unsigned char *cp;
  69. int i, len, lens = 0, hwptr_done = subs->hwptr_done;
  70. usX2Ydev_t *usX2Y = subs->usX2Y;
  71. for (i = 0; i < nr_of_packs(); i++) {
  72. cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  73. if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
  74. snd_printk(KERN_ERR "activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status);
  75. return urb->iso_frame_desc[i].status;
  76. }
  77. len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
  78. if (! len) {
  79. snd_printd("0 == len ERROR!\n");
  80. continue;
  81. }
  82. /* copy a data chunk */
  83. if ((hwptr_done + len) > runtime->buffer_size) {
  84. int cnt = runtime->buffer_size - hwptr_done;
  85. int blen = cnt * usX2Y->stride;
  86. memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
  87. memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
  88. } else {
  89. memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, len * usX2Y->stride);
  90. }
  91. lens += len;
  92. if ((hwptr_done += len) >= runtime->buffer_size)
  93. hwptr_done -= runtime->buffer_size;
  94. }
  95. subs->hwptr_done = hwptr_done;
  96. subs->transfer_done += lens;
  97. /* update the pointer, call callback if necessary */
  98. if (subs->transfer_done >= runtime->period_size) {
  99. subs->transfer_done -= runtime->period_size;
  100. snd_pcm_period_elapsed(subs->pcm_substream);
  101. }
  102. return 0;
  103. }
  104. /*
  105. * prepare urb for playback data pipe
  106. *
  107. * we copy the data directly from the pcm buffer.
  108. * the current position to be copied is held in hwptr field.
  109. * since a urb can handle only a single linear buffer, if the total
  110. * transferred area overflows the buffer boundary, we cannot send
  111. * it directly from the buffer. thus the data is once copied to
  112. * a temporary buffer and urb points to that.
  113. */
  114. static int usX2Y_urb_play_prepare(snd_usX2Y_substream_t *subs,
  115. struct urb *cap_urb,
  116. struct urb *urb)
  117. {
  118. int count, counts, pack;
  119. usX2Ydev_t* usX2Y = subs->usX2Y;
  120. snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
  121. count = 0;
  122. for (pack = 0; pack < nr_of_packs(); pack++) {
  123. /* calculate the size of a packet */
  124. counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
  125. count += counts;
  126. if (counts < 43 || counts > 50) {
  127. snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
  128. return -EPIPE;
  129. }
  130. /* set up descriptor */
  131. urb->iso_frame_desc[pack].offset = pack ?
  132. urb->iso_frame_desc[pack - 1].offset + urb->iso_frame_desc[pack - 1].length :
  133. 0;
  134. urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
  135. }
  136. if (atomic_read(&subs->state) >= state_PRERUNNING)
  137. if (subs->hwptr + count > runtime->buffer_size) {
  138. /* err, the transferred area goes over buffer boundary.
  139. * copy the data to the temp buffer.
  140. */
  141. int len;
  142. len = runtime->buffer_size - subs->hwptr;
  143. urb->transfer_buffer = subs->tmpbuf;
  144. memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * usX2Y->stride, len * usX2Y->stride);
  145. memcpy(subs->tmpbuf + len * usX2Y->stride, runtime->dma_area, (count - len) * usX2Y->stride);
  146. subs->hwptr += count;
  147. subs->hwptr -= runtime->buffer_size;
  148. } else {
  149. /* set the buffer pointer */
  150. urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
  151. if ((subs->hwptr += count) >= runtime->buffer_size)
  152. subs->hwptr -= runtime->buffer_size;
  153. }
  154. else
  155. urb->transfer_buffer = subs->tmpbuf;
  156. urb->transfer_buffer_length = count * usX2Y->stride;
  157. return 0;
  158. }
  159. /*
  160. * process after playback data complete
  161. *
  162. * update the current position and call callback if a period is processed.
  163. */
  164. static void usX2Y_urb_play_retire(snd_usX2Y_substream_t *subs, struct urb *urb)
  165. {
  166. snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
  167. int len = urb->actual_length / subs->usX2Y->stride;
  168. subs->transfer_done += len;
  169. subs->hwptr_done += len;
  170. if (subs->hwptr_done >= runtime->buffer_size)
  171. subs->hwptr_done -= runtime->buffer_size;
  172. if (subs->transfer_done >= runtime->period_size) {
  173. subs->transfer_done -= runtime->period_size;
  174. snd_pcm_period_elapsed(subs->pcm_substream);
  175. }
  176. }
  177. static int usX2Y_urb_submit(snd_usX2Y_substream_t *subs, struct urb *urb, int frame)
  178. {
  179. int err;
  180. if (!urb)
  181. return -ENODEV;
  182. urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
  183. urb->hcpriv = NULL;
  184. urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */
  185. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  186. snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
  187. return err;
  188. }
  189. return 0;
  190. }
  191. static inline int usX2Y_usbframe_complete(snd_usX2Y_substream_t *capsubs, snd_usX2Y_substream_t *playbacksubs, int frame)
  192. {
  193. int err, state;
  194. struct urb *urb = playbacksubs->completed_urb;
  195. state = atomic_read(&playbacksubs->state);
  196. if (NULL != urb) {
  197. if (state == state_RUNNING)
  198. usX2Y_urb_play_retire(playbacksubs, urb);
  199. else
  200. if (state >= state_PRERUNNING)
  201. atomic_inc(&playbacksubs->state);
  202. } else {
  203. switch (state) {
  204. case state_STARTING1:
  205. urb = playbacksubs->urb[0];
  206. atomic_inc(&playbacksubs->state);
  207. break;
  208. case state_STARTING2:
  209. urb = playbacksubs->urb[1];
  210. atomic_inc(&playbacksubs->state);
  211. break;
  212. }
  213. }
  214. if (urb) {
  215. if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
  216. (err = usX2Y_urb_submit(playbacksubs, urb, frame)))
  217. return err;
  218. }
  219. playbacksubs->completed_urb = NULL;
  220. state = atomic_read(&capsubs->state);
  221. if (state >= state_PREPARED) {
  222. if (state == state_RUNNING) {
  223. if ((err = usX2Y_urb_capt_retire(capsubs)))
  224. return err;
  225. } else if (state >= state_PRERUNNING)
  226. atomic_inc(&capsubs->state);
  227. if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
  228. return err;
  229. }
  230. capsubs->completed_urb = NULL;
  231. return 0;
  232. }
  233. static void usX2Y_clients_stop(usX2Ydev_t *usX2Y)
  234. {
  235. int s, u;
  236. for (s = 0; s < 4; s++) {
  237. snd_usX2Y_substream_t *subs = usX2Y->subs[s];
  238. if (subs) {
  239. snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
  240. atomic_set(&subs->state, state_STOPPED);
  241. }
  242. }
  243. for (s = 0; s < 4; s++) {
  244. snd_usX2Y_substream_t *subs = usX2Y->subs[s];
  245. if (subs) {
  246. if (atomic_read(&subs->state) >= state_PRERUNNING) {
  247. snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  248. }
  249. for (u = 0; u < NRURBS; u++) {
  250. struct urb *urb = subs->urb[u];
  251. if (NULL != urb)
  252. snd_printdd("%i status=%i start_frame=%i\n", u, urb->status, urb->start_frame);
  253. }
  254. }
  255. }
  256. usX2Y->prepare_subs = NULL;
  257. wake_up(&usX2Y->prepare_wait_queue);
  258. }
  259. static void usX2Y_error_urb_status(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
  260. {
  261. snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
  262. urb->status = 0;
  263. usX2Y_clients_stop(usX2Y);
  264. }
  265. static void usX2Y_error_sequence(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
  266. {
  267. snd_printk(KERN_ERR "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
  268. KERN_ERR "Most propably some urb of usb-frame %i is still missing.\n"
  269. KERN_ERR "Cause could be too long delays in usb-hcd interrupt handling.\n",
  270. usb_get_current_frame_number(usX2Y->chip.dev),
  271. subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
  272. usX2Y_clients_stop(usX2Y);
  273. }
  274. static void i_usX2Y_urb_complete(struct urb *urb, struct pt_regs *regs)
  275. {
  276. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
  277. usX2Ydev_t *usX2Y = subs->usX2Y;
  278. if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
  279. snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame);
  280. return;
  281. }
  282. if (unlikely(urb->status)) {
  283. usX2Y_error_urb_status(usX2Y, subs, urb);
  284. return;
  285. }
  286. if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame))
  287. subs->completed_urb = urb;
  288. else {
  289. usX2Y_error_sequence(usX2Y, subs, urb);
  290. return;
  291. }
  292. {
  293. snd_usX2Y_substream_t *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
  294. *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
  295. if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED &&
  296. (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) {
  297. if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
  298. if (nr_of_packs() <= urb->start_frame &&
  299. urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci
  300. usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs();
  301. else
  302. usX2Y->wait_iso_frame += nr_of_packs();
  303. } else {
  304. snd_printdd("\n");
  305. usX2Y_clients_stop(usX2Y);
  306. }
  307. }
  308. }
  309. }
  310. static void usX2Y_urbs_set_complete(usX2Ydev_t * usX2Y, void (*complete)(struct urb *, struct pt_regs *))
  311. {
  312. int s, u;
  313. for (s = 0; s < 4; s++) {
  314. snd_usX2Y_substream_t *subs = usX2Y->subs[s];
  315. if (NULL != subs)
  316. for (u = 0; u < NRURBS; u++) {
  317. struct urb * urb = subs->urb[u];
  318. if (NULL != urb)
  319. urb->complete = complete;
  320. }
  321. }
  322. }
  323. static void usX2Y_subs_startup_finish(usX2Ydev_t * usX2Y)
  324. {
  325. usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
  326. usX2Y->prepare_subs = NULL;
  327. }
  328. static void i_usX2Y_subs_startup(struct urb *urb, struct pt_regs *regs)
  329. {
  330. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
  331. usX2Ydev_t *usX2Y = subs->usX2Y;
  332. snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs;
  333. if (NULL != prepare_subs)
  334. if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
  335. usX2Y_subs_startup_finish(usX2Y);
  336. atomic_inc(&prepare_subs->state);
  337. wake_up(&usX2Y->prepare_wait_queue);
  338. }
  339. i_usX2Y_urb_complete(urb, regs);
  340. }
  341. static void usX2Y_subs_prepare(snd_usX2Y_substream_t *subs)
  342. {
  343. snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n", 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(snd_usX2Y_substream_t *subs)
  363. {
  364. int i;
  365. snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
  366. for (i = 0; i < NRURBS; i++)
  367. usX2Y_urb_release(subs->urb + i, 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(snd_usX2Y_substream_t *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->chip.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(snd_usX2Y_substream_t *subs)
  422. {
  423. usX2Ydev_t *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(snd_usX2Y_substream_t *subs)
  430. {
  431. int i, err;
  432. usX2Ydev_t *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. snd_usX2Y_substream_t *subs = usX2Y->subs[i];
  438. if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
  439. goto start;
  440. }
  441. usX2Y->wait_iso_frame = -1;
  442. start:
  443. usX2Y_subs_startup(subs);
  444. for (i = 0; i < NRURBS; i++) {
  445. struct urb *urb = subs->urb[i];
  446. if (usb_pipein(urb->pipe)) {
  447. unsigned long pack;
  448. if (0 == i)
  449. atomic_set(&subs->state, state_STARTING3);
  450. urb->dev = usX2Y->chip.dev;
  451. urb->transfer_flags = URB_ISO_ASAP;
  452. for (pack = 0; pack < nr_of_packs(); pack++) {
  453. urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
  454. urb->iso_frame_desc[pack].length = subs->maxpacksize;
  455. }
  456. urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
  457. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  458. snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
  459. err = -EPIPE;
  460. goto cleanup;
  461. } else {
  462. if (0 > usX2Y->wait_iso_frame)
  463. usX2Y->wait_iso_frame = urb->start_frame;
  464. }
  465. urb->transfer_flags = 0;
  466. } else {
  467. atomic_set(&subs->state, state_STARTING1);
  468. break;
  469. }
  470. }
  471. err = 0;
  472. wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
  473. if (atomic_read(&subs->state) != state_PREPARED)
  474. err = -EPIPE;
  475. cleanup:
  476. if (err) {
  477. usX2Y_subs_startup_finish(usX2Y);
  478. usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
  479. }
  480. return err;
  481. }
  482. /*
  483. * return the current pcm pointer. just return the hwptr_done value.
  484. */
  485. static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(snd_pcm_substream_t *substream)
  486. {
  487. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
  488. return subs->hwptr_done;
  489. }
  490. /*
  491. * start/stop substream
  492. */
  493. static int snd_usX2Y_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  494. {
  495. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
  496. switch (cmd) {
  497. case SNDRV_PCM_TRIGGER_START:
  498. snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
  499. if (atomic_read(&subs->state) == state_PREPARED &&
  500. atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
  501. atomic_set(&subs->state, state_PRERUNNING);
  502. } else {
  503. snd_printdd("\n");
  504. return -EPIPE;
  505. }
  506. break;
  507. case SNDRV_PCM_TRIGGER_STOP:
  508. snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
  509. if (atomic_read(&subs->state) >= state_PRERUNNING)
  510. atomic_set(&subs->state, state_PREPARED);
  511. break;
  512. default:
  513. return -EINVAL;
  514. }
  515. return 0;
  516. }
  517. /*
  518. * allocate a buffer, setup samplerate
  519. *
  520. * so far we use a physically linear buffer although packetize transfer
  521. * doesn't need a continuous area.
  522. * if sg buffer is supported on the later version of alsa, we'll follow
  523. * that.
  524. */
  525. static struct s_c2
  526. {
  527. char c1, c2;
  528. }
  529. SetRate44100[] =
  530. {
  531. { 0x14, 0x08}, // this line sets 44100, well actually a little less
  532. { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
  533. { 0x18, 0x42},
  534. { 0x18, 0x45},
  535. { 0x18, 0x46},
  536. { 0x18, 0x48},
  537. { 0x18, 0x4A},
  538. { 0x18, 0x4C},
  539. { 0x18, 0x4E},
  540. { 0x18, 0x50},
  541. { 0x18, 0x52},
  542. { 0x18, 0x54},
  543. { 0x18, 0x56},
  544. { 0x18, 0x58},
  545. { 0x18, 0x5A},
  546. { 0x18, 0x5C},
  547. { 0x18, 0x5E},
  548. { 0x18, 0x60},
  549. { 0x18, 0x62},
  550. { 0x18, 0x64},
  551. { 0x18, 0x66},
  552. { 0x18, 0x68},
  553. { 0x18, 0x6A},
  554. { 0x18, 0x6C},
  555. { 0x18, 0x6E},
  556. { 0x18, 0x70},
  557. { 0x18, 0x72},
  558. { 0x18, 0x74},
  559. { 0x18, 0x76},
  560. { 0x18, 0x78},
  561. { 0x18, 0x7A},
  562. { 0x18, 0x7C},
  563. { 0x18, 0x7E}
  564. };
  565. static struct s_c2 SetRate48000[] =
  566. {
  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(SetRate48000)
  602. static void i_usX2Y_04Int(struct urb* urb, struct pt_regs *regs)
  603. {
  604. usX2Ydev_t* usX2Y = urb->context;
  605. if (urb->status)
  606. snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
  607. if (0 == --usX2Y->US04->len)
  608. wake_up(&usX2Y->In04WaitQueue);
  609. }
  610. static int usX2Y_rate_set(usX2Ydev_t *usX2Y, int rate)
  611. {
  612. int err = 0, i;
  613. snd_usX2Y_urbSeq_t *us = NULL;
  614. int *usbdata = NULL;
  615. struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
  616. if (usX2Y->rate != rate) {
  617. us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
  618. if (NULL == us) {
  619. err = -ENOMEM;
  620. goto cleanup;
  621. }
  622. usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
  623. if (NULL == usbdata) {
  624. err = -ENOMEM;
  625. goto cleanup;
  626. }
  627. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  628. if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  629. err = -ENOMEM;
  630. goto cleanup;
  631. }
  632. ((char*)(usbdata + i))[0] = ra[i].c1;
  633. ((char*)(usbdata + i))[1] = ra[i].c2;
  634. usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4),
  635. usbdata + i, 2, i_usX2Y_04Int, usX2Y);
  636. #ifdef OLD_USB
  637. us->urb[i]->transfer_flags = USB_QUEUE_BULK;
  638. #endif
  639. }
  640. us->submitted = 0;
  641. us->len = NOOF_SETRATE_URBS;
  642. usX2Y->US04 = us;
  643. wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
  644. usX2Y->US04 = NULL;
  645. if (us->len)
  646. err = -ENODEV;
  647. cleanup:
  648. if (us) {
  649. us->submitted = 2*NOOF_SETRATE_URBS;
  650. for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
  651. struct urb *urb = us->urb[i];
  652. if (urb->status) {
  653. if (!err)
  654. err = -ENODEV;
  655. usb_kill_urb(urb);
  656. }
  657. usb_free_urb(urb);
  658. }
  659. usX2Y->US04 = NULL;
  660. kfree(usbdata);
  661. kfree(us);
  662. if (!err)
  663. usX2Y->rate = rate;
  664. }
  665. }
  666. return err;
  667. }
  668. static int usX2Y_format_set(usX2Ydev_t *usX2Y, snd_pcm_format_t format)
  669. {
  670. int alternate, err;
  671. struct list_head* p;
  672. if (format == SNDRV_PCM_FORMAT_S24_3LE) {
  673. alternate = 2;
  674. usX2Y->stride = 6;
  675. } else {
  676. alternate = 1;
  677. usX2Y->stride = 4;
  678. }
  679. list_for_each(p, &usX2Y->chip.midi_list) {
  680. snd_usbmidi_input_stop(p);
  681. }
  682. usb_kill_urb(usX2Y->In04urb);
  683. if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) {
  684. snd_printk(KERN_ERR "usb_set_interface error \n");
  685. return err;
  686. }
  687. usX2Y->In04urb->dev = usX2Y->chip.dev;
  688. err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
  689. list_for_each(p, &usX2Y->chip.midi_list) {
  690. snd_usbmidi_input_start(p);
  691. }
  692. usX2Y->format = format;
  693. usX2Y->rate = 0;
  694. return err;
  695. }
  696. static int snd_usX2Y_pcm_hw_params(snd_pcm_substream_t *substream,
  697. snd_pcm_hw_params_t *hw_params)
  698. {
  699. int err = 0;
  700. unsigned int rate = params_rate(hw_params);
  701. snd_pcm_format_t format = params_format(hw_params);
  702. snd_card_t *card = substream->pstr->pcm->card;
  703. struct list_head *list;
  704. snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
  705. // all pcm substreams off one usX2Y have to operate at the same rate & format
  706. list_for_each(list, &card->devices) {
  707. snd_device_t *dev;
  708. snd_pcm_t *pcm;
  709. int s;
  710. dev = snd_device(list);
  711. if (dev->type != SNDRV_DEV_PCM)
  712. continue;
  713. pcm = dev->device_data;
  714. for (s = 0; s < 2; ++s) {
  715. snd_pcm_substream_t *test_substream;
  716. test_substream = pcm->streams[s].substream;
  717. if (test_substream && test_substream != substream &&
  718. test_substream->runtime &&
  719. ((test_substream->runtime->format &&
  720. test_substream->runtime->format != format) ||
  721. (test_substream->runtime->rate &&
  722. test_substream->runtime->rate != rate)))
  723. return -EINVAL;
  724. }
  725. }
  726. if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
  727. snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n", substream, params_buffer_bytes(hw_params), err);
  728. return err;
  729. }
  730. return 0;
  731. }
  732. /*
  733. * free the buffer
  734. */
  735. static int snd_usX2Y_pcm_hw_free(snd_pcm_substream_t *substream)
  736. {
  737. snd_pcm_runtime_t *runtime = substream->runtime;
  738. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
  739. down(&subs->usX2Y->prepare_mutex);
  740. snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
  741. if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
  742. snd_usX2Y_substream_t *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. snd_usX2Y_substream_t *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. up(&subs->usX2Y->prepare_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(snd_pcm_substream_t *substream)
  768. {
  769. snd_pcm_runtime_t *runtime = substream->runtime;
  770. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
  771. usX2Ydev_t *usX2Y = subs->usX2Y;
  772. snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
  773. int err = 0;
  774. snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
  775. down(&usX2Y->prepare_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. up(&usX2Y->prepare_mutex);
  794. return err;
  795. }
  796. static snd_pcm_hardware_t 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. .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(snd_pcm_substream_t *substream)
  815. {
  816. snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **)
  817. snd_pcm_substream_chip(substream))[substream->stream];
  818. snd_pcm_runtime_t *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(snd_pcm_substream_t *substream)
  828. {
  829. snd_pcm_runtime_t *runtime = substream->runtime;
  830. snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
  831. int err = 0;
  832. subs->pcm_substream = NULL;
  833. return err;
  834. }
  835. static snd_pcm_ops_t 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(snd_usX2Y_substream_t **usX2Y_substream)
  850. {
  851. if (NULL != usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]) {
  852. kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
  853. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
  854. }
  855. kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
  856. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
  857. }
  858. static void snd_usX2Y_pcm_private_free(snd_pcm_t *pcm)
  859. {
  860. snd_usX2Y_substream_t **usX2Y_stream = pcm->private_data;
  861. if (usX2Y_stream)
  862. usX2Y_audio_stream_free(usX2Y_stream);
  863. }
  864. static int usX2Y_audio_stream_new(snd_card_t *card, int playback_endpoint, int capture_endpoint)
  865. {
  866. snd_pcm_t *pcm;
  867. int err, i;
  868. snd_usX2Y_substream_t **usX2Y_substream =
  869. usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs;
  870. for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
  871. i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
  872. usX2Y_substream[i] = kzalloc(sizeof(snd_usX2Y_substream_t), GFP_KERNEL);
  873. if (NULL == usX2Y_substream[i]) {
  874. snd_printk(KERN_ERR "cannot malloc\n");
  875. return -ENOMEM;
  876. }
  877. usX2Y_substream[i]->usX2Y = usX2Y(card);
  878. }
  879. if (playback_endpoint)
  880. usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
  881. usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
  882. err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs,
  883. playback_endpoint ? 1 : 0, 1,
  884. &pcm);
  885. if (err < 0) {
  886. usX2Y_audio_stream_free(usX2Y_substream);
  887. return err;
  888. }
  889. if (playback_endpoint)
  890. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
  891. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
  892. pcm->private_data = usX2Y_substream;
  893. pcm->private_free = snd_usX2Y_pcm_private_free;
  894. pcm->info_flags = 0;
  895. sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs);
  896. if ((playback_endpoint &&
  897. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  898. SNDRV_DMA_TYPE_CONTINUOUS,
  899. snd_dma_continuous_data(GFP_KERNEL),
  900. 64*1024, 128*1024))) ||
  901. 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  902. SNDRV_DMA_TYPE_CONTINUOUS,
  903. snd_dma_continuous_data(GFP_KERNEL),
  904. 64*1024, 128*1024))) {
  905. snd_usX2Y_pcm_private_free(pcm);
  906. return err;
  907. }
  908. usX2Y(card)->chip.pcm_devs++;
  909. return 0;
  910. }
  911. /*
  912. * create a chip instance and set its names.
  913. */
  914. int usX2Y_audio_create(snd_card_t* card)
  915. {
  916. int err = 0;
  917. INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list);
  918. if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
  919. return err;
  920. if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428)
  921. if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
  922. return err;
  923. if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122)
  924. err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
  925. return err;
  926. }