urb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. */
  17. #include <linux/gfp.h>
  18. #include <linux/init.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/audio.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include "usbaudio.h"
  24. #include "helper.h"
  25. #include "card.h"
  26. #include "urb.h"
  27. #include "pcm.h"
  28. /*
  29. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  30. * this will overflow at approx 524 kHz
  31. */
  32. static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  33. {
  34. return ((rate << 13) + 62) / 125;
  35. }
  36. /*
  37. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  38. * this will overflow at approx 4 MHz
  39. */
  40. static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  41. {
  42. return ((rate << 10) + 62) / 125;
  43. }
  44. /*
  45. * unlink active urbs.
  46. */
  47. static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
  48. {
  49. struct snd_usb_audio *chip = subs->stream->chip;
  50. unsigned int i;
  51. int async;
  52. subs->running = 0;
  53. if (!force && subs->stream->chip->shutdown) /* to be sure... */
  54. return -EBADFD;
  55. async = !can_sleep && chip->async_unlink;
  56. if (!async && in_interrupt())
  57. return 0;
  58. for (i = 0; i < subs->nurbs; i++) {
  59. if (test_bit(i, &subs->active_mask)) {
  60. if (!test_and_set_bit(i, &subs->unlink_mask)) {
  61. struct urb *u = subs->dataurb[i].urb;
  62. if (async)
  63. usb_unlink_urb(u);
  64. else
  65. usb_kill_urb(u);
  66. }
  67. }
  68. }
  69. if (subs->syncpipe) {
  70. for (i = 0; i < SYNC_URBS; i++) {
  71. if (test_bit(i+16, &subs->active_mask)) {
  72. if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
  73. struct urb *u = subs->syncurb[i].urb;
  74. if (async)
  75. usb_unlink_urb(u);
  76. else
  77. usb_kill_urb(u);
  78. }
  79. }
  80. }
  81. }
  82. return 0;
  83. }
  84. /*
  85. * release a urb data
  86. */
  87. static void release_urb_ctx(struct snd_urb_ctx *u)
  88. {
  89. if (u->urb) {
  90. if (u->buffer_size)
  91. usb_free_coherent(u->subs->dev, u->buffer_size,
  92. u->urb->transfer_buffer,
  93. u->urb->transfer_dma);
  94. usb_free_urb(u->urb);
  95. u->urb = NULL;
  96. }
  97. }
  98. /*
  99. * wait until all urbs are processed.
  100. */
  101. static int wait_clear_urbs(struct snd_usb_substream *subs)
  102. {
  103. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  104. unsigned int i;
  105. int alive;
  106. do {
  107. alive = 0;
  108. for (i = 0; i < subs->nurbs; i++) {
  109. if (test_bit(i, &subs->active_mask))
  110. alive++;
  111. }
  112. if (subs->syncpipe) {
  113. for (i = 0; i < SYNC_URBS; i++) {
  114. if (test_bit(i + 16, &subs->active_mask))
  115. alive++;
  116. }
  117. }
  118. if (! alive)
  119. break;
  120. schedule_timeout_uninterruptible(1);
  121. } while (time_before(jiffies, end_time));
  122. if (alive)
  123. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  124. return 0;
  125. }
  126. /*
  127. * release a substream
  128. */
  129. void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
  130. {
  131. int i;
  132. /* stop urbs (to be sure) */
  133. deactivate_urbs(subs, force, 1);
  134. wait_clear_urbs(subs);
  135. for (i = 0; i < MAX_URBS; i++)
  136. release_urb_ctx(&subs->dataurb[i]);
  137. for (i = 0; i < SYNC_URBS; i++)
  138. release_urb_ctx(&subs->syncurb[i]);
  139. usb_free_coherent(subs->dev, SYNC_URBS * 4,
  140. subs->syncbuf, subs->sync_dma);
  141. subs->syncbuf = NULL;
  142. subs->nurbs = 0;
  143. }
  144. /*
  145. * complete callback from data urb
  146. */
  147. static void snd_complete_urb(struct urb *urb)
  148. {
  149. struct snd_urb_ctx *ctx = urb->context;
  150. struct snd_usb_substream *subs = ctx->subs;
  151. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  152. int err = 0;
  153. if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
  154. !subs->running || /* can be stopped during retire callback */
  155. (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
  156. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  157. clear_bit(ctx->index, &subs->active_mask);
  158. if (err < 0) {
  159. snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
  160. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  161. }
  162. }
  163. }
  164. /*
  165. * complete callback from sync urb
  166. */
  167. static void snd_complete_sync_urb(struct urb *urb)
  168. {
  169. struct snd_urb_ctx *ctx = urb->context;
  170. struct snd_usb_substream *subs = ctx->subs;
  171. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  172. int err = 0;
  173. if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
  174. !subs->running || /* can be stopped during retire callback */
  175. (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
  176. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  177. clear_bit(ctx->index + 16, &subs->active_mask);
  178. if (err < 0) {
  179. snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
  180. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  181. }
  182. }
  183. }
  184. /*
  185. * initialize a substream for plaback/capture
  186. */
  187. int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
  188. unsigned int period_bytes,
  189. unsigned int rate,
  190. unsigned int frame_bits)
  191. {
  192. unsigned int maxsize, i;
  193. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  194. unsigned int urb_packs, total_packs, packs_per_ms;
  195. struct snd_usb_audio *chip = subs->stream->chip;
  196. /* calculate the frequency in 16.16 format */
  197. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  198. subs->freqn = get_usb_full_speed_rate(rate);
  199. else
  200. subs->freqn = get_usb_high_speed_rate(rate);
  201. subs->freqm = subs->freqn;
  202. /* calculate max. frequency */
  203. if (subs->maxpacksize) {
  204. /* whatever fits into a max. size packet */
  205. maxsize = subs->maxpacksize;
  206. subs->freqmax = (maxsize / (frame_bits >> 3))
  207. << (16 - subs->datainterval);
  208. } else {
  209. /* no max. packet size: just take 25% higher than nominal */
  210. subs->freqmax = subs->freqn + (subs->freqn >> 2);
  211. maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
  212. >> (16 - subs->datainterval);
  213. }
  214. subs->phase = 0;
  215. if (subs->fill_max)
  216. subs->curpacksize = subs->maxpacksize;
  217. else
  218. subs->curpacksize = maxsize;
  219. if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
  220. packs_per_ms = 8 >> subs->datainterval;
  221. else
  222. packs_per_ms = 1;
  223. if (is_playback) {
  224. urb_packs = max(chip->nrpacks, 1);
  225. urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
  226. } else
  227. urb_packs = 1;
  228. urb_packs *= packs_per_ms;
  229. if (subs->syncpipe)
  230. urb_packs = min(urb_packs, 1U << subs->syncinterval);
  231. /* decide how many packets to be used */
  232. if (is_playback) {
  233. unsigned int minsize, maxpacks;
  234. /* determine how small a packet can be */
  235. minsize = (subs->freqn >> (16 - subs->datainterval))
  236. * (frame_bits >> 3);
  237. /* with sync from device, assume it can be 12% lower */
  238. if (subs->syncpipe)
  239. minsize -= minsize >> 3;
  240. minsize = max(minsize, 1u);
  241. total_packs = (period_bytes + minsize - 1) / minsize;
  242. /* we need at least two URBs for queueing */
  243. if (total_packs < 2) {
  244. total_packs = 2;
  245. } else {
  246. /* and we don't want too long a queue either */
  247. maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  248. total_packs = min(total_packs, maxpacks);
  249. }
  250. } else {
  251. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  252. urb_packs >>= 1;
  253. total_packs = MAX_URBS * urb_packs;
  254. }
  255. subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  256. if (subs->nurbs > MAX_URBS) {
  257. /* too much... */
  258. subs->nurbs = MAX_URBS;
  259. total_packs = MAX_URBS * urb_packs;
  260. } else if (subs->nurbs < 2) {
  261. /* too little - we need at least two packets
  262. * to ensure contiguous playback/capture
  263. */
  264. subs->nurbs = 2;
  265. }
  266. /* allocate and initialize data urbs */
  267. for (i = 0; i < subs->nurbs; i++) {
  268. struct snd_urb_ctx *u = &subs->dataurb[i];
  269. u->index = i;
  270. u->subs = subs;
  271. u->packets = (i + 1) * total_packs / subs->nurbs
  272. - i * total_packs / subs->nurbs;
  273. u->buffer_size = maxsize * u->packets;
  274. if (subs->fmt_type == UAC_FORMAT_TYPE_II)
  275. u->packets++; /* for transfer delimiter */
  276. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  277. if (!u->urb)
  278. goto out_of_memory;
  279. u->urb->transfer_buffer =
  280. usb_alloc_coherent(subs->dev, u->buffer_size,
  281. GFP_KERNEL, &u->urb->transfer_dma);
  282. if (!u->urb->transfer_buffer)
  283. goto out_of_memory;
  284. u->urb->pipe = subs->datapipe;
  285. u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  286. u->urb->interval = 1 << subs->datainterval;
  287. u->urb->context = u;
  288. u->urb->complete = snd_complete_urb;
  289. }
  290. if (subs->syncpipe) {
  291. /* allocate and initialize sync urbs */
  292. subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
  293. GFP_KERNEL, &subs->sync_dma);
  294. if (!subs->syncbuf)
  295. goto out_of_memory;
  296. for (i = 0; i < SYNC_URBS; i++) {
  297. struct snd_urb_ctx *u = &subs->syncurb[i];
  298. u->index = i;
  299. u->subs = subs;
  300. u->packets = 1;
  301. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  302. if (!u->urb)
  303. goto out_of_memory;
  304. u->urb->transfer_buffer = subs->syncbuf + i * 4;
  305. u->urb->transfer_dma = subs->sync_dma + i * 4;
  306. u->urb->transfer_buffer_length = 4;
  307. u->urb->pipe = subs->syncpipe;
  308. u->urb->transfer_flags = URB_ISO_ASAP |
  309. URB_NO_TRANSFER_DMA_MAP;
  310. u->urb->number_of_packets = 1;
  311. u->urb->interval = 1 << subs->syncinterval;
  312. u->urb->context = u;
  313. u->urb->complete = snd_complete_sync_urb;
  314. }
  315. }
  316. return 0;
  317. out_of_memory:
  318. snd_usb_release_substream_urbs(subs, 0);
  319. return -ENOMEM;
  320. }
  321. /*
  322. * prepare urb for full speed capture sync pipe
  323. *
  324. * fill the length and offset of each urb descriptor.
  325. * the fixed 10.14 frequency is passed through the pipe.
  326. */
  327. static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
  328. struct snd_pcm_runtime *runtime,
  329. struct urb *urb)
  330. {
  331. unsigned char *cp = urb->transfer_buffer;
  332. struct snd_urb_ctx *ctx = urb->context;
  333. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  334. urb->iso_frame_desc[0].length = 3;
  335. urb->iso_frame_desc[0].offset = 0;
  336. cp[0] = subs->freqn >> 2;
  337. cp[1] = subs->freqn >> 10;
  338. cp[2] = subs->freqn >> 18;
  339. return 0;
  340. }
  341. /*
  342. * prepare urb for high speed capture sync pipe
  343. *
  344. * fill the length and offset of each urb descriptor.
  345. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  346. */
  347. static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
  348. struct snd_pcm_runtime *runtime,
  349. struct urb *urb)
  350. {
  351. unsigned char *cp = urb->transfer_buffer;
  352. struct snd_urb_ctx *ctx = urb->context;
  353. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  354. urb->iso_frame_desc[0].length = 4;
  355. urb->iso_frame_desc[0].offset = 0;
  356. cp[0] = subs->freqn;
  357. cp[1] = subs->freqn >> 8;
  358. cp[2] = subs->freqn >> 16;
  359. cp[3] = subs->freqn >> 24;
  360. return 0;
  361. }
  362. /*
  363. * process after capture sync complete
  364. * - nothing to do
  365. */
  366. static int retire_capture_sync_urb(struct snd_usb_substream *subs,
  367. struct snd_pcm_runtime *runtime,
  368. struct urb *urb)
  369. {
  370. return 0;
  371. }
  372. /*
  373. * prepare urb for capture data pipe
  374. *
  375. * fill the offset and length of each descriptor.
  376. *
  377. * we use a temporary buffer to write the captured data.
  378. * since the length of written data is determined by host, we cannot
  379. * write onto the pcm buffer directly... the data is thus copied
  380. * later at complete callback to the global buffer.
  381. */
  382. static int prepare_capture_urb(struct snd_usb_substream *subs,
  383. struct snd_pcm_runtime *runtime,
  384. struct urb *urb)
  385. {
  386. int i, offs;
  387. struct snd_urb_ctx *ctx = urb->context;
  388. offs = 0;
  389. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  390. for (i = 0; i < ctx->packets; i++) {
  391. urb->iso_frame_desc[i].offset = offs;
  392. urb->iso_frame_desc[i].length = subs->curpacksize;
  393. offs += subs->curpacksize;
  394. }
  395. urb->transfer_buffer_length = offs;
  396. urb->number_of_packets = ctx->packets;
  397. return 0;
  398. }
  399. /*
  400. * process after capture complete
  401. *
  402. * copy the data from each desctiptor to the pcm buffer, and
  403. * update the current position.
  404. */
  405. static int retire_capture_urb(struct snd_usb_substream *subs,
  406. struct snd_pcm_runtime *runtime,
  407. struct urb *urb)
  408. {
  409. unsigned long flags;
  410. unsigned char *cp;
  411. int i;
  412. unsigned int stride, frames, bytes, oldptr;
  413. int period_elapsed = 0;
  414. stride = runtime->frame_bits >> 3;
  415. for (i = 0; i < urb->number_of_packets; i++) {
  416. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  417. if (urb->iso_frame_desc[i].status) {
  418. snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  419. // continue;
  420. }
  421. bytes = urb->iso_frame_desc[i].actual_length;
  422. frames = bytes / stride;
  423. if (!subs->txfr_quirk)
  424. bytes = frames * stride;
  425. if (bytes % (runtime->sample_bits >> 3) != 0) {
  426. #ifdef CONFIG_SND_DEBUG_VERBOSE
  427. int oldbytes = bytes;
  428. #endif
  429. bytes = frames * stride;
  430. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  431. oldbytes, bytes);
  432. }
  433. /* update the current pointer */
  434. spin_lock_irqsave(&subs->lock, flags);
  435. oldptr = subs->hwptr_done;
  436. subs->hwptr_done += bytes;
  437. if (subs->hwptr_done >= runtime->buffer_size * stride)
  438. subs->hwptr_done -= runtime->buffer_size * stride;
  439. frames = (bytes + (oldptr % stride)) / stride;
  440. subs->transfer_done += frames;
  441. if (subs->transfer_done >= runtime->period_size) {
  442. subs->transfer_done -= runtime->period_size;
  443. period_elapsed = 1;
  444. }
  445. spin_unlock_irqrestore(&subs->lock, flags);
  446. /* copy a data chunk */
  447. if (oldptr + bytes > runtime->buffer_size * stride) {
  448. unsigned int bytes1 =
  449. runtime->buffer_size * stride - oldptr;
  450. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  451. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  452. } else {
  453. memcpy(runtime->dma_area + oldptr, cp, bytes);
  454. }
  455. }
  456. if (period_elapsed)
  457. snd_pcm_period_elapsed(subs->pcm_substream);
  458. return 0;
  459. }
  460. /*
  461. * Process after capture complete when paused. Nothing to do.
  462. */
  463. static int retire_paused_capture_urb(struct snd_usb_substream *subs,
  464. struct snd_pcm_runtime *runtime,
  465. struct urb *urb)
  466. {
  467. return 0;
  468. }
  469. /*
  470. * prepare urb for full speed playback sync pipe
  471. *
  472. * set up the offset and length to receive the current frequency.
  473. */
  474. static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
  475. struct snd_pcm_runtime *runtime,
  476. struct urb *urb)
  477. {
  478. struct snd_urb_ctx *ctx = urb->context;
  479. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  480. urb->iso_frame_desc[0].length = 3;
  481. urb->iso_frame_desc[0].offset = 0;
  482. return 0;
  483. }
  484. /*
  485. * prepare urb for high speed playback sync pipe
  486. *
  487. * set up the offset and length to receive the current frequency.
  488. */
  489. static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
  490. struct snd_pcm_runtime *runtime,
  491. struct urb *urb)
  492. {
  493. struct snd_urb_ctx *ctx = urb->context;
  494. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  495. urb->iso_frame_desc[0].length = 4;
  496. urb->iso_frame_desc[0].offset = 0;
  497. return 0;
  498. }
  499. /*
  500. * process after full speed playback sync complete
  501. *
  502. * retrieve the current 10.14 frequency from pipe, and set it.
  503. * the value is referred in prepare_playback_urb().
  504. */
  505. static int retire_playback_sync_urb(struct snd_usb_substream *subs,
  506. struct snd_pcm_runtime *runtime,
  507. struct urb *urb)
  508. {
  509. unsigned int f;
  510. unsigned long flags;
  511. if (urb->iso_frame_desc[0].status == 0 &&
  512. urb->iso_frame_desc[0].actual_length == 3) {
  513. f = combine_triple((u8*)urb->transfer_buffer) << 2;
  514. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  515. spin_lock_irqsave(&subs->lock, flags);
  516. subs->freqm = f;
  517. spin_unlock_irqrestore(&subs->lock, flags);
  518. }
  519. }
  520. return 0;
  521. }
  522. /*
  523. * process after high speed playback sync complete
  524. *
  525. * retrieve the current 12.13 frequency from pipe, and set it.
  526. * the value is referred in prepare_playback_urb().
  527. */
  528. static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
  529. struct snd_pcm_runtime *runtime,
  530. struct urb *urb)
  531. {
  532. unsigned int f;
  533. unsigned long flags;
  534. if (urb->iso_frame_desc[0].status == 0 &&
  535. urb->iso_frame_desc[0].actual_length == 4) {
  536. f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
  537. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  538. spin_lock_irqsave(&subs->lock, flags);
  539. subs->freqm = f;
  540. spin_unlock_irqrestore(&subs->lock, flags);
  541. }
  542. }
  543. return 0;
  544. }
  545. /*
  546. * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
  547. *
  548. * These devices return the number of samples per packet instead of the number
  549. * of samples per microframe.
  550. */
  551. static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
  552. struct snd_pcm_runtime *runtime,
  553. struct urb *urb)
  554. {
  555. unsigned int f;
  556. unsigned long flags;
  557. if (urb->iso_frame_desc[0].status == 0 &&
  558. urb->iso_frame_desc[0].actual_length == 4) {
  559. f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
  560. f >>= subs->datainterval;
  561. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  562. spin_lock_irqsave(&subs->lock, flags);
  563. subs->freqm = f;
  564. spin_unlock_irqrestore(&subs->lock, flags);
  565. }
  566. }
  567. return 0;
  568. }
  569. /* determine the number of frames in the next packet */
  570. static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
  571. {
  572. if (subs->fill_max)
  573. return subs->maxframesize;
  574. else {
  575. subs->phase = (subs->phase & 0xffff)
  576. + (subs->freqm << subs->datainterval);
  577. return min(subs->phase >> 16, subs->maxframesize);
  578. }
  579. }
  580. /*
  581. * Prepare urb for streaming before playback starts or when paused.
  582. *
  583. * We don't have any data, so we send silence.
  584. */
  585. static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
  586. struct snd_pcm_runtime *runtime,
  587. struct urb *urb)
  588. {
  589. unsigned int i, offs, counts;
  590. struct snd_urb_ctx *ctx = urb->context;
  591. int stride = runtime->frame_bits >> 3;
  592. offs = 0;
  593. urb->dev = ctx->subs->dev;
  594. for (i = 0; i < ctx->packets; ++i) {
  595. counts = snd_usb_audio_next_packet_size(subs);
  596. urb->iso_frame_desc[i].offset = offs * stride;
  597. urb->iso_frame_desc[i].length = counts * stride;
  598. offs += counts;
  599. }
  600. urb->number_of_packets = ctx->packets;
  601. urb->transfer_buffer_length = offs * stride;
  602. memset(urb->transfer_buffer,
  603. runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
  604. offs * stride);
  605. return 0;
  606. }
  607. /*
  608. * prepare urb for playback data pipe
  609. *
  610. * Since a URB can handle only a single linear buffer, we must use double
  611. * buffering when the data to be transferred overflows the buffer boundary.
  612. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  613. * for all URBs.
  614. */
  615. static int prepare_playback_urb(struct snd_usb_substream *subs,
  616. struct snd_pcm_runtime *runtime,
  617. struct urb *urb)
  618. {
  619. int i, stride;
  620. unsigned int counts, frames, bytes;
  621. unsigned long flags;
  622. int period_elapsed = 0;
  623. struct snd_urb_ctx *ctx = urb->context;
  624. stride = runtime->frame_bits >> 3;
  625. frames = 0;
  626. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  627. urb->number_of_packets = 0;
  628. spin_lock_irqsave(&subs->lock, flags);
  629. for (i = 0; i < ctx->packets; i++) {
  630. counts = snd_usb_audio_next_packet_size(subs);
  631. /* set up descriptor */
  632. urb->iso_frame_desc[i].offset = frames * stride;
  633. urb->iso_frame_desc[i].length = counts * stride;
  634. frames += counts;
  635. urb->number_of_packets++;
  636. subs->transfer_done += counts;
  637. if (subs->transfer_done >= runtime->period_size) {
  638. subs->transfer_done -= runtime->period_size;
  639. period_elapsed = 1;
  640. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  641. if (subs->transfer_done > 0) {
  642. /* FIXME: fill-max mode is not
  643. * supported yet */
  644. frames -= subs->transfer_done;
  645. counts -= subs->transfer_done;
  646. urb->iso_frame_desc[i].length =
  647. counts * stride;
  648. subs->transfer_done = 0;
  649. }
  650. i++;
  651. if (i < ctx->packets) {
  652. /* add a transfer delimiter */
  653. urb->iso_frame_desc[i].offset =
  654. frames * stride;
  655. urb->iso_frame_desc[i].length = 0;
  656. urb->number_of_packets++;
  657. }
  658. break;
  659. }
  660. }
  661. if (period_elapsed) /* finish at the period boundary */
  662. break;
  663. }
  664. bytes = frames * stride;
  665. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  666. /* err, the transferred area goes over buffer boundary. */
  667. unsigned int bytes1 =
  668. runtime->buffer_size * stride - subs->hwptr_done;
  669. memcpy(urb->transfer_buffer,
  670. runtime->dma_area + subs->hwptr_done, bytes1);
  671. memcpy(urb->transfer_buffer + bytes1,
  672. runtime->dma_area, bytes - bytes1);
  673. } else {
  674. memcpy(urb->transfer_buffer,
  675. runtime->dma_area + subs->hwptr_done, bytes);
  676. }
  677. subs->hwptr_done += bytes;
  678. if (subs->hwptr_done >= runtime->buffer_size * stride)
  679. subs->hwptr_done -= runtime->buffer_size * stride;
  680. runtime->delay += frames;
  681. spin_unlock_irqrestore(&subs->lock, flags);
  682. urb->transfer_buffer_length = bytes;
  683. if (period_elapsed)
  684. snd_pcm_period_elapsed(subs->pcm_substream);
  685. return 0;
  686. }
  687. /*
  688. * process after playback data complete
  689. * - decrease the delay count again
  690. */
  691. static int retire_playback_urb(struct snd_usb_substream *subs,
  692. struct snd_pcm_runtime *runtime,
  693. struct urb *urb)
  694. {
  695. unsigned long flags;
  696. int stride = runtime->frame_bits >> 3;
  697. int processed = urb->transfer_buffer_length / stride;
  698. spin_lock_irqsave(&subs->lock, flags);
  699. if (processed > runtime->delay)
  700. runtime->delay = 0;
  701. else
  702. runtime->delay -= processed;
  703. spin_unlock_irqrestore(&subs->lock, flags);
  704. return 0;
  705. }
  706. static const char *usb_error_string(int err)
  707. {
  708. switch (err) {
  709. case -ENODEV:
  710. return "no device";
  711. case -ENOENT:
  712. return "endpoint not enabled";
  713. case -EPIPE:
  714. return "endpoint stalled";
  715. case -ENOSPC:
  716. return "not enough bandwidth";
  717. case -ESHUTDOWN:
  718. return "device disabled";
  719. case -EHOSTUNREACH:
  720. return "device suspended";
  721. case -EINVAL:
  722. case -EAGAIN:
  723. case -EFBIG:
  724. case -EMSGSIZE:
  725. return "internal error";
  726. default:
  727. return "unknown error";
  728. }
  729. }
  730. /*
  731. * set up and start data/sync urbs
  732. */
  733. static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
  734. {
  735. unsigned int i;
  736. int err;
  737. if (subs->stream->chip->shutdown)
  738. return -EBADFD;
  739. for (i = 0; i < subs->nurbs; i++) {
  740. if (snd_BUG_ON(!subs->dataurb[i].urb))
  741. return -EINVAL;
  742. if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  743. snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  744. goto __error;
  745. }
  746. }
  747. if (subs->syncpipe) {
  748. for (i = 0; i < SYNC_URBS; i++) {
  749. if (snd_BUG_ON(!subs->syncurb[i].urb))
  750. return -EINVAL;
  751. if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  752. snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  753. goto __error;
  754. }
  755. }
  756. }
  757. subs->active_mask = 0;
  758. subs->unlink_mask = 0;
  759. subs->running = 1;
  760. for (i = 0; i < subs->nurbs; i++) {
  761. err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
  762. if (err < 0) {
  763. snd_printk(KERN_ERR "cannot submit datapipe "
  764. "for urb %d, error %d: %s\n",
  765. i, err, usb_error_string(err));
  766. goto __error;
  767. }
  768. set_bit(i, &subs->active_mask);
  769. }
  770. if (subs->syncpipe) {
  771. for (i = 0; i < SYNC_URBS; i++) {
  772. err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
  773. if (err < 0) {
  774. snd_printk(KERN_ERR "cannot submit syncpipe "
  775. "for urb %d, error %d: %s\n",
  776. i, err, usb_error_string(err));
  777. goto __error;
  778. }
  779. set_bit(i + 16, &subs->active_mask);
  780. }
  781. }
  782. return 0;
  783. __error:
  784. // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  785. deactivate_urbs(subs, 0, 0);
  786. return -EPIPE;
  787. }
  788. /*
  789. */
  790. static struct snd_urb_ops audio_urb_ops[2] = {
  791. {
  792. .prepare = prepare_nodata_playback_urb,
  793. .retire = retire_playback_urb,
  794. .prepare_sync = prepare_playback_sync_urb,
  795. .retire_sync = retire_playback_sync_urb,
  796. },
  797. {
  798. .prepare = prepare_capture_urb,
  799. .retire = retire_capture_urb,
  800. .prepare_sync = prepare_capture_sync_urb,
  801. .retire_sync = retire_capture_sync_urb,
  802. },
  803. };
  804. static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
  805. {
  806. .prepare = prepare_nodata_playback_urb,
  807. .retire = retire_playback_urb,
  808. .prepare_sync = prepare_playback_sync_urb_hs,
  809. .retire_sync = retire_playback_sync_urb_hs,
  810. },
  811. {
  812. .prepare = prepare_capture_urb,
  813. .retire = retire_capture_urb,
  814. .prepare_sync = prepare_capture_sync_urb_hs,
  815. .retire_sync = retire_capture_sync_urb,
  816. },
  817. };
  818. /*
  819. * initialize the substream instance.
  820. */
  821. void snd_usb_init_substream(struct snd_usb_stream *as,
  822. int stream, struct audioformat *fp)
  823. {
  824. struct snd_usb_substream *subs = &as->substream[stream];
  825. INIT_LIST_HEAD(&subs->fmt_list);
  826. spin_lock_init(&subs->lock);
  827. subs->stream = as;
  828. subs->direction = stream;
  829. subs->dev = as->chip->dev;
  830. subs->txfr_quirk = as->chip->txfr_quirk;
  831. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
  832. subs->ops = audio_urb_ops[stream];
  833. } else {
  834. subs->ops = audio_urb_ops_high_speed[stream];
  835. switch (as->chip->usb_id) {
  836. case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
  837. case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
  838. case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
  839. subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
  840. break;
  841. case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */
  842. case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
  843. subs->ops.prepare_sync = prepare_playback_sync_urb;
  844. subs->ops.retire_sync = retire_playback_sync_urb;
  845. break;
  846. }
  847. }
  848. snd_usb_set_pcm_ops(as->pcm, stream);
  849. list_add_tail(&fp->list, &subs->fmt_list);
  850. subs->formats |= fp->formats;
  851. subs->endpoint = fp->endpoint;
  852. subs->num_formats++;
  853. subs->fmt_type = fp->fmt_type;
  854. }
  855. int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  856. {
  857. struct snd_usb_substream *subs = substream->runtime->private_data;
  858. switch (cmd) {
  859. case SNDRV_PCM_TRIGGER_START:
  860. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  861. subs->ops.prepare = prepare_playback_urb;
  862. return 0;
  863. case SNDRV_PCM_TRIGGER_STOP:
  864. return deactivate_urbs(subs, 0, 0);
  865. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  866. subs->ops.prepare = prepare_nodata_playback_urb;
  867. return 0;
  868. }
  869. return -EINVAL;
  870. }
  871. int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  872. {
  873. struct snd_usb_substream *subs = substream->runtime->private_data;
  874. switch (cmd) {
  875. case SNDRV_PCM_TRIGGER_START:
  876. subs->ops.retire = retire_capture_urb;
  877. return start_urbs(subs, substream->runtime);
  878. case SNDRV_PCM_TRIGGER_STOP:
  879. return deactivate_urbs(subs, 0, 0);
  880. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  881. subs->ops.retire = retire_paused_capture_urb;
  882. return 0;
  883. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  884. subs->ops.retire = retire_capture_urb;
  885. return 0;
  886. }
  887. return -EINVAL;
  888. }
  889. int snd_usb_substream_prepare(struct snd_usb_substream *subs,
  890. struct snd_pcm_runtime *runtime)
  891. {
  892. /* clear urbs (to be sure) */
  893. deactivate_urbs(subs, 0, 1);
  894. wait_clear_urbs(subs);
  895. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  896. * updates for all URBs would happen at the same time when starting */
  897. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  898. subs->ops.prepare = prepare_nodata_playback_urb;
  899. return start_urbs(subs, runtime);
  900. }
  901. return 0;
  902. }