usbusx2yaudio.c 29 KB

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