usbusx2yaudio.c 30 KB

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