usbusx2yaudio.c 28 KB

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