usbusx2yaudio.c 29 KB

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