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