usbusx2yaudio.c 29 KB

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