urb.c 26 KB

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