usbusx2yaudio.c 29 KB

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