endpoint.c 26 KB

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