urb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. subs->freqshift = INT_MIN;
  203. /* calculate max. frequency */
  204. if (subs->maxpacksize) {
  205. /* whatever fits into a max. size packet */
  206. maxsize = subs->maxpacksize;
  207. subs->freqmax = (maxsize / (frame_bits >> 3))
  208. << (16 - subs->datainterval);
  209. } else {
  210. /* no max. packet size: just take 25% higher than nominal */
  211. subs->freqmax = subs->freqn + (subs->freqn >> 2);
  212. maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
  213. >> (16 - subs->datainterval);
  214. }
  215. subs->phase = 0;
  216. if (subs->fill_max)
  217. subs->curpacksize = subs->maxpacksize;
  218. else
  219. subs->curpacksize = maxsize;
  220. if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
  221. packs_per_ms = 8 >> subs->datainterval;
  222. else
  223. packs_per_ms = 1;
  224. if (is_playback) {
  225. urb_packs = max(chip->nrpacks, 1);
  226. urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
  227. } else
  228. urb_packs = 1;
  229. urb_packs *= packs_per_ms;
  230. if (subs->syncpipe)
  231. urb_packs = min(urb_packs, 1U << subs->syncinterval);
  232. /* decide how many packets to be used */
  233. if (is_playback) {
  234. unsigned int minsize, maxpacks;
  235. /* determine how small a packet can be */
  236. minsize = (subs->freqn >> (16 - subs->datainterval))
  237. * (frame_bits >> 3);
  238. /* with sync from device, assume it can be 12% lower */
  239. if (subs->syncpipe)
  240. minsize -= minsize >> 3;
  241. minsize = max(minsize, 1u);
  242. total_packs = (period_bytes + minsize - 1) / minsize;
  243. /* we need at least two URBs for queueing */
  244. if (total_packs < 2) {
  245. total_packs = 2;
  246. } else {
  247. /* and we don't want too long a queue either */
  248. maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  249. total_packs = min(total_packs, maxpacks);
  250. }
  251. } else {
  252. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  253. urb_packs >>= 1;
  254. total_packs = MAX_URBS * urb_packs;
  255. }
  256. subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  257. if (subs->nurbs > MAX_URBS) {
  258. /* too much... */
  259. subs->nurbs = MAX_URBS;
  260. total_packs = MAX_URBS * urb_packs;
  261. } else if (subs->nurbs < 2) {
  262. /* too little - we need at least two packets
  263. * to ensure contiguous playback/capture
  264. */
  265. subs->nurbs = 2;
  266. }
  267. /* allocate and initialize data urbs */
  268. for (i = 0; i < subs->nurbs; i++) {
  269. struct snd_urb_ctx *u = &subs->dataurb[i];
  270. u->index = i;
  271. u->subs = subs;
  272. u->packets = (i + 1) * total_packs / subs->nurbs
  273. - i * total_packs / subs->nurbs;
  274. u->buffer_size = maxsize * u->packets;
  275. if (subs->fmt_type == UAC_FORMAT_TYPE_II)
  276. u->packets++; /* for transfer delimiter */
  277. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  278. if (!u->urb)
  279. goto out_of_memory;
  280. u->urb->transfer_buffer =
  281. usb_alloc_coherent(subs->dev, u->buffer_size,
  282. GFP_KERNEL, &u->urb->transfer_dma);
  283. if (!u->urb->transfer_buffer)
  284. goto out_of_memory;
  285. u->urb->pipe = subs->datapipe;
  286. u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  287. u->urb->interval = 1 << subs->datainterval;
  288. u->urb->context = u;
  289. u->urb->complete = snd_complete_urb;
  290. }
  291. if (subs->syncpipe) {
  292. /* allocate and initialize sync urbs */
  293. subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
  294. GFP_KERNEL, &subs->sync_dma);
  295. if (!subs->syncbuf)
  296. goto out_of_memory;
  297. for (i = 0; i < SYNC_URBS; i++) {
  298. struct snd_urb_ctx *u = &subs->syncurb[i];
  299. u->index = i;
  300. u->subs = subs;
  301. u->packets = 1;
  302. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  303. if (!u->urb)
  304. goto out_of_memory;
  305. u->urb->transfer_buffer = subs->syncbuf + i * 4;
  306. u->urb->transfer_dma = subs->sync_dma + i * 4;
  307. u->urb->transfer_buffer_length = 4;
  308. u->urb->pipe = subs->syncpipe;
  309. u->urb->transfer_flags = URB_ISO_ASAP |
  310. URB_NO_TRANSFER_DMA_MAP;
  311. u->urb->number_of_packets = 1;
  312. u->urb->interval = 1 << subs->syncinterval;
  313. u->urb->context = u;
  314. u->urb->complete = snd_complete_sync_urb;
  315. }
  316. }
  317. return 0;
  318. out_of_memory:
  319. snd_usb_release_substream_urbs(subs, 0);
  320. return -ENOMEM;
  321. }
  322. /*
  323. * prepare urb for full speed capture sync pipe
  324. *
  325. * fill the length and offset of each urb descriptor.
  326. * the fixed 10.14 frequency is passed through the pipe.
  327. */
  328. static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
  329. struct snd_pcm_runtime *runtime,
  330. struct urb *urb)
  331. {
  332. unsigned char *cp = urb->transfer_buffer;
  333. struct snd_urb_ctx *ctx = urb->context;
  334. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  335. urb->iso_frame_desc[0].length = 3;
  336. urb->iso_frame_desc[0].offset = 0;
  337. cp[0] = subs->freqn >> 2;
  338. cp[1] = subs->freqn >> 10;
  339. cp[2] = subs->freqn >> 18;
  340. return 0;
  341. }
  342. /*
  343. * prepare urb for high speed capture sync pipe
  344. *
  345. * fill the length and offset of each urb descriptor.
  346. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  347. */
  348. static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
  349. struct snd_pcm_runtime *runtime,
  350. struct urb *urb)
  351. {
  352. unsigned char *cp = urb->transfer_buffer;
  353. struct snd_urb_ctx *ctx = urb->context;
  354. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  355. urb->iso_frame_desc[0].length = 4;
  356. urb->iso_frame_desc[0].offset = 0;
  357. cp[0] = subs->freqn;
  358. cp[1] = subs->freqn >> 8;
  359. cp[2] = subs->freqn >> 16;
  360. cp[3] = subs->freqn >> 24;
  361. return 0;
  362. }
  363. /*
  364. * process after capture sync complete
  365. * - nothing to do
  366. */
  367. static int retire_capture_sync_urb(struct snd_usb_substream *subs,
  368. struct snd_pcm_runtime *runtime,
  369. struct urb *urb)
  370. {
  371. return 0;
  372. }
  373. /*
  374. * prepare urb for capture data pipe
  375. *
  376. * fill the offset and length of each descriptor.
  377. *
  378. * we use a temporary buffer to write the captured data.
  379. * since the length of written data is determined by host, we cannot
  380. * write onto the pcm buffer directly... the data is thus copied
  381. * later at complete callback to the global buffer.
  382. */
  383. static int prepare_capture_urb(struct snd_usb_substream *subs,
  384. struct snd_pcm_runtime *runtime,
  385. struct urb *urb)
  386. {
  387. int i, offs;
  388. struct snd_urb_ctx *ctx = urb->context;
  389. offs = 0;
  390. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  391. for (i = 0; i < ctx->packets; i++) {
  392. urb->iso_frame_desc[i].offset = offs;
  393. urb->iso_frame_desc[i].length = subs->curpacksize;
  394. offs += subs->curpacksize;
  395. }
  396. urb->transfer_buffer_length = offs;
  397. urb->number_of_packets = ctx->packets;
  398. return 0;
  399. }
  400. /*
  401. * process after capture complete
  402. *
  403. * copy the data from each desctiptor to the pcm buffer, and
  404. * update the current position.
  405. */
  406. static int retire_capture_urb(struct snd_usb_substream *subs,
  407. struct snd_pcm_runtime *runtime,
  408. struct urb *urb)
  409. {
  410. unsigned long flags;
  411. unsigned char *cp;
  412. int i;
  413. unsigned int stride, frames, bytes, oldptr;
  414. int period_elapsed = 0;
  415. stride = runtime->frame_bits >> 3;
  416. for (i = 0; i < urb->number_of_packets; i++) {
  417. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  418. if (urb->iso_frame_desc[i].status) {
  419. snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  420. // continue;
  421. }
  422. bytes = urb->iso_frame_desc[i].actual_length;
  423. frames = bytes / stride;
  424. if (!subs->txfr_quirk)
  425. bytes = frames * stride;
  426. if (bytes % (runtime->sample_bits >> 3) != 0) {
  427. #ifdef CONFIG_SND_DEBUG_VERBOSE
  428. int oldbytes = bytes;
  429. #endif
  430. bytes = frames * stride;
  431. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  432. oldbytes, bytes);
  433. }
  434. /* update the current pointer */
  435. spin_lock_irqsave(&subs->lock, flags);
  436. oldptr = subs->hwptr_done;
  437. subs->hwptr_done += bytes;
  438. if (subs->hwptr_done >= runtime->buffer_size * stride)
  439. subs->hwptr_done -= runtime->buffer_size * stride;
  440. frames = (bytes + (oldptr % stride)) / stride;
  441. subs->transfer_done += frames;
  442. if (subs->transfer_done >= runtime->period_size) {
  443. subs->transfer_done -= runtime->period_size;
  444. period_elapsed = 1;
  445. }
  446. spin_unlock_irqrestore(&subs->lock, flags);
  447. /* copy a data chunk */
  448. if (oldptr + bytes > runtime->buffer_size * stride) {
  449. unsigned int bytes1 =
  450. runtime->buffer_size * stride - oldptr;
  451. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  452. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  453. } else {
  454. memcpy(runtime->dma_area + oldptr, cp, bytes);
  455. }
  456. }
  457. if (period_elapsed)
  458. snd_pcm_period_elapsed(subs->pcm_substream);
  459. return 0;
  460. }
  461. /*
  462. * Process after capture complete when paused. Nothing to do.
  463. */
  464. static int retire_paused_capture_urb(struct snd_usb_substream *subs,
  465. struct snd_pcm_runtime *runtime,
  466. struct urb *urb)
  467. {
  468. return 0;
  469. }
  470. /*
  471. * prepare urb for playback sync pipe
  472. *
  473. * set up the offset and length to receive the current frequency.
  474. */
  475. static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
  476. struct snd_pcm_runtime *runtime,
  477. struct urb *urb)
  478. {
  479. struct snd_urb_ctx *ctx = urb->context;
  480. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  481. urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);
  482. urb->iso_frame_desc[0].offset = 0;
  483. return 0;
  484. }
  485. /*
  486. * process after playback sync complete
  487. *
  488. * Full speed devices report feedback values in 10.14 format as samples per
  489. * frame, high speed devices in 16.16 format as samples per microframe.
  490. * Because the Audio Class 1 spec was written before USB 2.0, many high speed
  491. * devices use a wrong interpretation, some others use an entirely different
  492. * format. Therefore, we cannot predict what format any particular device uses
  493. * and must detect it automatically.
  494. */
  495. static int retire_playback_sync_urb(struct snd_usb_substream *subs,
  496. struct snd_pcm_runtime *runtime,
  497. struct urb *urb)
  498. {
  499. unsigned int f;
  500. int shift;
  501. unsigned long flags;
  502. if (urb->iso_frame_desc[0].status != 0 ||
  503. urb->iso_frame_desc[0].actual_length < 3)
  504. return 0;
  505. f = le32_to_cpup(urb->transfer_buffer);
  506. if (urb->iso_frame_desc[0].actual_length == 3)
  507. f &= 0x00ffffff;
  508. else
  509. f &= 0x0fffffff;
  510. if (f == 0)
  511. return 0;
  512. if (unlikely(subs->freqshift == INT_MIN)) {
  513. /*
  514. * The first time we see a feedback value, determine its format
  515. * by shifting it left or right until it matches the nominal
  516. * frequency value. This assumes that the feedback does not
  517. * differ from the nominal value more than +50% or -25%.
  518. */
  519. shift = 0;
  520. while (f < subs->freqn - subs->freqn / 4) {
  521. f <<= 1;
  522. shift++;
  523. }
  524. while (f > subs->freqn + subs->freqn / 2) {
  525. f >>= 1;
  526. shift--;
  527. }
  528. subs->freqshift = shift;
  529. }
  530. else if (subs->freqshift >= 0)
  531. f <<= subs->freqshift;
  532. else
  533. f >>= -subs->freqshift;
  534. if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {
  535. /*
  536. * If the frequency looks valid, set it.
  537. * This value is referred to in prepare_playback_urb().
  538. */
  539. spin_lock_irqsave(&subs->lock, flags);
  540. subs->freqm = f;
  541. spin_unlock_irqrestore(&subs->lock, flags);
  542. } else {
  543. /*
  544. * Out of range; maybe the shift value is wrong.
  545. * Reset it so that we autodetect again the next time.
  546. */
  547. subs->freqshift = INT_MIN;
  548. }
  549. return 0;
  550. }
  551. /* determine the number of frames in the next packet */
  552. static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
  553. {
  554. if (subs->fill_max)
  555. return subs->maxframesize;
  556. else {
  557. subs->phase = (subs->phase & 0xffff)
  558. + (subs->freqm << subs->datainterval);
  559. return min(subs->phase >> 16, subs->maxframesize);
  560. }
  561. }
  562. /*
  563. * Prepare urb for streaming before playback starts or when paused.
  564. *
  565. * We don't have any data, so we send silence.
  566. */
  567. static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
  568. struct snd_pcm_runtime *runtime,
  569. struct urb *urb)
  570. {
  571. unsigned int i, offs, counts;
  572. struct snd_urb_ctx *ctx = urb->context;
  573. int stride = runtime->frame_bits >> 3;
  574. offs = 0;
  575. urb->dev = ctx->subs->dev;
  576. for (i = 0; i < ctx->packets; ++i) {
  577. counts = snd_usb_audio_next_packet_size(subs);
  578. urb->iso_frame_desc[i].offset = offs * stride;
  579. urb->iso_frame_desc[i].length = counts * stride;
  580. offs += counts;
  581. }
  582. urb->number_of_packets = ctx->packets;
  583. urb->transfer_buffer_length = offs * stride;
  584. memset(urb->transfer_buffer,
  585. runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
  586. offs * stride);
  587. return 0;
  588. }
  589. /*
  590. * prepare urb for playback data pipe
  591. *
  592. * Since a URB can handle only a single linear buffer, we must use double
  593. * buffering when the data to be transferred overflows the buffer boundary.
  594. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  595. * for all URBs.
  596. */
  597. static int prepare_playback_urb(struct snd_usb_substream *subs,
  598. struct snd_pcm_runtime *runtime,
  599. struct urb *urb)
  600. {
  601. int i, stride;
  602. unsigned int counts, frames, bytes;
  603. unsigned long flags;
  604. int period_elapsed = 0;
  605. struct snd_urb_ctx *ctx = urb->context;
  606. stride = runtime->frame_bits >> 3;
  607. frames = 0;
  608. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  609. urb->number_of_packets = 0;
  610. spin_lock_irqsave(&subs->lock, flags);
  611. for (i = 0; i < ctx->packets; i++) {
  612. counts = snd_usb_audio_next_packet_size(subs);
  613. /* set up descriptor */
  614. urb->iso_frame_desc[i].offset = frames * stride;
  615. urb->iso_frame_desc[i].length = counts * stride;
  616. frames += counts;
  617. urb->number_of_packets++;
  618. subs->transfer_done += counts;
  619. if (subs->transfer_done >= runtime->period_size) {
  620. subs->transfer_done -= runtime->period_size;
  621. period_elapsed = 1;
  622. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  623. if (subs->transfer_done > 0) {
  624. /* FIXME: fill-max mode is not
  625. * supported yet */
  626. frames -= subs->transfer_done;
  627. counts -= subs->transfer_done;
  628. urb->iso_frame_desc[i].length =
  629. counts * stride;
  630. subs->transfer_done = 0;
  631. }
  632. i++;
  633. if (i < ctx->packets) {
  634. /* add a transfer delimiter */
  635. urb->iso_frame_desc[i].offset =
  636. frames * stride;
  637. urb->iso_frame_desc[i].length = 0;
  638. urb->number_of_packets++;
  639. }
  640. break;
  641. }
  642. }
  643. if (period_elapsed) /* finish at the period boundary */
  644. break;
  645. }
  646. bytes = frames * stride;
  647. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  648. /* err, the transferred area goes over buffer boundary. */
  649. unsigned int bytes1 =
  650. runtime->buffer_size * stride - subs->hwptr_done;
  651. memcpy(urb->transfer_buffer,
  652. runtime->dma_area + subs->hwptr_done, bytes1);
  653. memcpy(urb->transfer_buffer + bytes1,
  654. runtime->dma_area, bytes - bytes1);
  655. } else {
  656. memcpy(urb->transfer_buffer,
  657. runtime->dma_area + subs->hwptr_done, bytes);
  658. }
  659. subs->hwptr_done += bytes;
  660. if (subs->hwptr_done >= runtime->buffer_size * stride)
  661. subs->hwptr_done -= runtime->buffer_size * stride;
  662. runtime->delay += frames;
  663. spin_unlock_irqrestore(&subs->lock, flags);
  664. urb->transfer_buffer_length = bytes;
  665. if (period_elapsed)
  666. snd_pcm_period_elapsed(subs->pcm_substream);
  667. return 0;
  668. }
  669. /*
  670. * process after playback data complete
  671. * - decrease the delay count again
  672. */
  673. static int retire_playback_urb(struct snd_usb_substream *subs,
  674. struct snd_pcm_runtime *runtime,
  675. struct urb *urb)
  676. {
  677. unsigned long flags;
  678. int stride = runtime->frame_bits >> 3;
  679. int processed = urb->transfer_buffer_length / stride;
  680. spin_lock_irqsave(&subs->lock, flags);
  681. if (processed > runtime->delay)
  682. runtime->delay = 0;
  683. else
  684. runtime->delay -= processed;
  685. spin_unlock_irqrestore(&subs->lock, flags);
  686. return 0;
  687. }
  688. static const char *usb_error_string(int err)
  689. {
  690. switch (err) {
  691. case -ENODEV:
  692. return "no device";
  693. case -ENOENT:
  694. return "endpoint not enabled";
  695. case -EPIPE:
  696. return "endpoint stalled";
  697. case -ENOSPC:
  698. return "not enough bandwidth";
  699. case -ESHUTDOWN:
  700. return "device disabled";
  701. case -EHOSTUNREACH:
  702. return "device suspended";
  703. case -EINVAL:
  704. case -EAGAIN:
  705. case -EFBIG:
  706. case -EMSGSIZE:
  707. return "internal error";
  708. default:
  709. return "unknown error";
  710. }
  711. }
  712. /*
  713. * set up and start data/sync urbs
  714. */
  715. static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
  716. {
  717. unsigned int i;
  718. int err;
  719. if (subs->stream->chip->shutdown)
  720. return -EBADFD;
  721. for (i = 0; i < subs->nurbs; i++) {
  722. if (snd_BUG_ON(!subs->dataurb[i].urb))
  723. return -EINVAL;
  724. if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  725. snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  726. goto __error;
  727. }
  728. }
  729. if (subs->syncpipe) {
  730. for (i = 0; i < SYNC_URBS; i++) {
  731. if (snd_BUG_ON(!subs->syncurb[i].urb))
  732. return -EINVAL;
  733. if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  734. snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  735. goto __error;
  736. }
  737. }
  738. }
  739. subs->active_mask = 0;
  740. subs->unlink_mask = 0;
  741. subs->running = 1;
  742. for (i = 0; i < subs->nurbs; i++) {
  743. err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
  744. if (err < 0) {
  745. snd_printk(KERN_ERR "cannot submit datapipe "
  746. "for urb %d, error %d: %s\n",
  747. i, err, usb_error_string(err));
  748. goto __error;
  749. }
  750. set_bit(i, &subs->active_mask);
  751. }
  752. if (subs->syncpipe) {
  753. for (i = 0; i < SYNC_URBS; i++) {
  754. err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
  755. if (err < 0) {
  756. snd_printk(KERN_ERR "cannot submit syncpipe "
  757. "for urb %d, error %d: %s\n",
  758. i, err, usb_error_string(err));
  759. goto __error;
  760. }
  761. set_bit(i + 16, &subs->active_mask);
  762. }
  763. }
  764. return 0;
  765. __error:
  766. // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  767. deactivate_urbs(subs, 0, 0);
  768. return -EPIPE;
  769. }
  770. /*
  771. */
  772. static struct snd_urb_ops audio_urb_ops[2] = {
  773. {
  774. .prepare = prepare_nodata_playback_urb,
  775. .retire = retire_playback_urb,
  776. .prepare_sync = prepare_playback_sync_urb,
  777. .retire_sync = retire_playback_sync_urb,
  778. },
  779. {
  780. .prepare = prepare_capture_urb,
  781. .retire = retire_capture_urb,
  782. .prepare_sync = prepare_capture_sync_urb,
  783. .retire_sync = retire_capture_sync_urb,
  784. },
  785. };
  786. /*
  787. * initialize the substream instance.
  788. */
  789. void snd_usb_init_substream(struct snd_usb_stream *as,
  790. int stream, struct audioformat *fp)
  791. {
  792. struct snd_usb_substream *subs = &as->substream[stream];
  793. INIT_LIST_HEAD(&subs->fmt_list);
  794. spin_lock_init(&subs->lock);
  795. subs->stream = as;
  796. subs->direction = stream;
  797. subs->dev = as->chip->dev;
  798. subs->txfr_quirk = as->chip->txfr_quirk;
  799. subs->ops = audio_urb_ops[stream];
  800. if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)
  801. subs->ops.prepare_sync = prepare_capture_sync_urb_hs;
  802. snd_usb_set_pcm_ops(as->pcm, stream);
  803. list_add_tail(&fp->list, &subs->fmt_list);
  804. subs->formats |= fp->formats;
  805. subs->endpoint = fp->endpoint;
  806. subs->num_formats++;
  807. subs->fmt_type = fp->fmt_type;
  808. }
  809. int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  810. {
  811. struct snd_usb_substream *subs = substream->runtime->private_data;
  812. switch (cmd) {
  813. case SNDRV_PCM_TRIGGER_START:
  814. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  815. subs->ops.prepare = prepare_playback_urb;
  816. return 0;
  817. case SNDRV_PCM_TRIGGER_STOP:
  818. return deactivate_urbs(subs, 0, 0);
  819. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  820. subs->ops.prepare = prepare_nodata_playback_urb;
  821. return 0;
  822. }
  823. return -EINVAL;
  824. }
  825. int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  826. {
  827. struct snd_usb_substream *subs = substream->runtime->private_data;
  828. switch (cmd) {
  829. case SNDRV_PCM_TRIGGER_START:
  830. subs->ops.retire = retire_capture_urb;
  831. return start_urbs(subs, substream->runtime);
  832. case SNDRV_PCM_TRIGGER_STOP:
  833. return deactivate_urbs(subs, 0, 0);
  834. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  835. subs->ops.retire = retire_paused_capture_urb;
  836. return 0;
  837. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  838. subs->ops.retire = retire_capture_urb;
  839. return 0;
  840. }
  841. return -EINVAL;
  842. }
  843. int snd_usb_substream_prepare(struct snd_usb_substream *subs,
  844. struct snd_pcm_runtime *runtime)
  845. {
  846. /* clear urbs (to be sure) */
  847. deactivate_urbs(subs, 0, 1);
  848. wait_clear_urbs(subs);
  849. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  850. * updates for all URBs would happen at the same time when starting */
  851. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  852. subs->ops.prepare = prepare_nodata_playback_urb;
  853. return start_urbs(subs, runtime);
  854. }
  855. return 0;
  856. }