endpoint.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include "usbaudio.h"
  27. #include "helper.h"
  28. #include "card.h"
  29. #include "endpoint.h"
  30. #include "pcm.h"
  31. #include "quirks.h"
  32. #define EP_FLAG_RUNNING 1
  33. #define EP_FLAG_STOPPING 2
  34. /*
  35. * snd_usb_endpoint is a model that abstracts everything related to an
  36. * USB endpoint and its streaming.
  37. *
  38. * There are functions to activate and deactivate the streaming URBs and
  39. * optional callbacks to let the pcm logic handle the actual content of the
  40. * packets for playback and record. Thus, the bus streaming and the audio
  41. * handlers are fully decoupled.
  42. *
  43. * There are two different types of endpoints in audio applications.
  44. *
  45. * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
  46. * inbound and outbound traffic.
  47. *
  48. * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
  49. * expect the payload to carry Q10.14 / Q16.16 formatted sync information
  50. * (3 or 4 bytes).
  51. *
  52. * Each endpoint has to be configured prior to being used by calling
  53. * snd_usb_endpoint_set_params().
  54. *
  55. * The model incorporates a reference counting, so that multiple users
  56. * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
  57. * only the first user will effectively start the URBs, and only the last
  58. * one to stop it will tear the URBs down again.
  59. */
  60. /*
  61. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  62. * this will overflow at approx 524 kHz
  63. */
  64. static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  65. {
  66. return ((rate << 13) + 62) / 125;
  67. }
  68. /*
  69. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  70. * this will overflow at approx 4 MHz
  71. */
  72. static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  73. {
  74. return ((rate << 10) + 62) / 125;
  75. }
  76. /*
  77. * release a urb data
  78. */
  79. static void release_urb_ctx(struct snd_urb_ctx *u)
  80. {
  81. if (u->buffer_size)
  82. usb_free_coherent(u->ep->chip->dev, u->buffer_size,
  83. u->urb->transfer_buffer,
  84. u->urb->transfer_dma);
  85. usb_free_urb(u->urb);
  86. u->urb = NULL;
  87. }
  88. static const char *usb_error_string(int err)
  89. {
  90. switch (err) {
  91. case -ENODEV:
  92. return "no device";
  93. case -ENOENT:
  94. return "endpoint not enabled";
  95. case -EPIPE:
  96. return "endpoint stalled";
  97. case -ENOSPC:
  98. return "not enough bandwidth";
  99. case -ESHUTDOWN:
  100. return "device disabled";
  101. case -EHOSTUNREACH:
  102. return "device suspended";
  103. case -EINVAL:
  104. case -EAGAIN:
  105. case -EFBIG:
  106. case -EMSGSIZE:
  107. return "internal error";
  108. default:
  109. return "unknown error";
  110. }
  111. }
  112. /**
  113. * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
  114. *
  115. * @ep: The snd_usb_endpoint
  116. *
  117. * Determine whether an endpoint is driven by an implicit feedback
  118. * data endpoint source.
  119. */
  120. int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
  121. {
  122. return ep->sync_master &&
  123. ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
  124. ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
  125. usb_pipeout(ep->pipe);
  126. }
  127. /*
  128. * For streaming based on information derived from sync endpoints,
  129. * prepare_outbound_urb_sizes() will call next_packet_size() to
  130. * determine the number of samples to be sent in the next packet.
  131. *
  132. * For implicit feedback, next_packet_size() is unused.
  133. */
  134. int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep)
  135. {
  136. unsigned long flags;
  137. int ret;
  138. if (ep->fill_max)
  139. return ep->maxframesize;
  140. spin_lock_irqsave(&ep->lock, flags);
  141. ep->phase = (ep->phase & 0xffff)
  142. + (ep->freqm << ep->datainterval);
  143. ret = min(ep->phase >> 16, ep->maxframesize);
  144. spin_unlock_irqrestore(&ep->lock, flags);
  145. return ret;
  146. }
  147. static void retire_outbound_urb(struct snd_usb_endpoint *ep,
  148. struct snd_urb_ctx *urb_ctx)
  149. {
  150. if (ep->retire_data_urb)
  151. ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
  152. }
  153. static void retire_inbound_urb(struct snd_usb_endpoint *ep,
  154. struct snd_urb_ctx *urb_ctx)
  155. {
  156. struct urb *urb = urb_ctx->urb;
  157. if (unlikely(ep->skip_packets > 0)) {
  158. ep->skip_packets--;
  159. return;
  160. }
  161. if (ep->sync_slave)
  162. snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
  163. if (ep->retire_data_urb)
  164. ep->retire_data_urb(ep->data_subs, urb);
  165. }
  166. /*
  167. * Prepare a PLAYBACK urb for submission to the bus.
  168. */
  169. static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
  170. struct snd_urb_ctx *ctx)
  171. {
  172. int i;
  173. struct urb *urb = ctx->urb;
  174. unsigned char *cp = urb->transfer_buffer;
  175. urb->dev = ep->chip->dev; /* we need to set this at each time */
  176. switch (ep->type) {
  177. case SND_USB_ENDPOINT_TYPE_DATA:
  178. if (ep->prepare_data_urb) {
  179. ep->prepare_data_urb(ep->data_subs, urb);
  180. } else {
  181. /* no data provider, so send silence */
  182. unsigned int offs = 0;
  183. for (i = 0; i < ctx->packets; ++i) {
  184. int counts;
  185. if (ctx->packet_size[i])
  186. counts = ctx->packet_size[i];
  187. else
  188. counts = snd_usb_endpoint_next_packet_size(ep);
  189. urb->iso_frame_desc[i].offset = offs * ep->stride;
  190. urb->iso_frame_desc[i].length = counts * ep->stride;
  191. offs += counts;
  192. }
  193. urb->number_of_packets = ctx->packets;
  194. urb->transfer_buffer_length = offs * ep->stride;
  195. memset(urb->transfer_buffer, ep->silence_value,
  196. offs * ep->stride);
  197. }
  198. break;
  199. case SND_USB_ENDPOINT_TYPE_SYNC:
  200. if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
  201. /*
  202. * fill the length and offset of each urb descriptor.
  203. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  204. */
  205. urb->iso_frame_desc[0].length = 4;
  206. urb->iso_frame_desc[0].offset = 0;
  207. cp[0] = ep->freqn;
  208. cp[1] = ep->freqn >> 8;
  209. cp[2] = ep->freqn >> 16;
  210. cp[3] = ep->freqn >> 24;
  211. } else {
  212. /*
  213. * fill the length and offset of each urb descriptor.
  214. * the fixed 10.14 frequency is passed through the pipe.
  215. */
  216. urb->iso_frame_desc[0].length = 3;
  217. urb->iso_frame_desc[0].offset = 0;
  218. cp[0] = ep->freqn >> 2;
  219. cp[1] = ep->freqn >> 10;
  220. cp[2] = ep->freqn >> 18;
  221. }
  222. break;
  223. }
  224. }
  225. /*
  226. * Prepare a CAPTURE or SYNC urb for submission to the bus.
  227. */
  228. static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
  229. struct snd_urb_ctx *urb_ctx)
  230. {
  231. int i, offs;
  232. struct urb *urb = urb_ctx->urb;
  233. urb->dev = ep->chip->dev; /* we need to set this at each time */
  234. switch (ep->type) {
  235. case SND_USB_ENDPOINT_TYPE_DATA:
  236. offs = 0;
  237. for (i = 0; i < urb_ctx->packets; i++) {
  238. urb->iso_frame_desc[i].offset = offs;
  239. urb->iso_frame_desc[i].length = ep->curpacksize;
  240. offs += ep->curpacksize;
  241. }
  242. urb->transfer_buffer_length = offs;
  243. urb->number_of_packets = urb_ctx->packets;
  244. break;
  245. case SND_USB_ENDPOINT_TYPE_SYNC:
  246. urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
  247. urb->iso_frame_desc[0].offset = 0;
  248. break;
  249. }
  250. }
  251. /*
  252. * Send output urbs that have been prepared previously. URBs are dequeued
  253. * from ep->ready_playback_urbs and in case there there aren't any available
  254. * or there are no packets that have been prepared, this function does
  255. * nothing.
  256. *
  257. * The reason why the functionality of sending and preparing URBs is separated
  258. * is that host controllers don't guarantee the order in which they return
  259. * inbound and outbound packets to their submitters.
  260. *
  261. * This function is only used for implicit feedback endpoints. For endpoints
  262. * driven by dedicated sync endpoints, URBs are immediately re-submitted
  263. * from their completion handler.
  264. */
  265. static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
  266. {
  267. while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
  268. unsigned long flags;
  269. struct snd_usb_packet_info *uninitialized_var(packet);
  270. struct snd_urb_ctx *ctx = NULL;
  271. struct urb *urb;
  272. int err, i;
  273. spin_lock_irqsave(&ep->lock, flags);
  274. if (ep->next_packet_read_pos != ep->next_packet_write_pos) {
  275. packet = ep->next_packet + ep->next_packet_read_pos;
  276. ep->next_packet_read_pos++;
  277. ep->next_packet_read_pos %= MAX_URBS;
  278. /* take URB out of FIFO */
  279. if (!list_empty(&ep->ready_playback_urbs))
  280. ctx = list_first_entry(&ep->ready_playback_urbs,
  281. struct snd_urb_ctx, ready_list);
  282. }
  283. spin_unlock_irqrestore(&ep->lock, flags);
  284. if (ctx == NULL)
  285. return;
  286. list_del_init(&ctx->ready_list);
  287. urb = ctx->urb;
  288. /* copy over the length information */
  289. for (i = 0; i < packet->packets; i++)
  290. ctx->packet_size[i] = packet->packet_size[i];
  291. /* call the data handler to fill in playback data */
  292. prepare_outbound_urb(ep, ctx);
  293. err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
  294. if (err < 0)
  295. snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n",
  296. ctx->index, err, ctx->urb);
  297. else
  298. set_bit(ctx->index, &ep->active_mask);
  299. }
  300. }
  301. /*
  302. * complete callback for urbs
  303. */
  304. static void snd_complete_urb(struct urb *urb)
  305. {
  306. struct snd_urb_ctx *ctx = urb->context;
  307. struct snd_usb_endpoint *ep = ctx->ep;
  308. int err;
  309. if (unlikely(urb->status == -ENOENT || /* unlinked */
  310. urb->status == -ENODEV || /* device removed */
  311. urb->status == -ECONNRESET || /* unlinked */
  312. urb->status == -ESHUTDOWN || /* device disabled */
  313. ep->chip->shutdown)) /* device disconnected */
  314. goto exit_clear;
  315. if (usb_pipeout(ep->pipe)) {
  316. retire_outbound_urb(ep, ctx);
  317. /* can be stopped during retire callback */
  318. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  319. goto exit_clear;
  320. if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
  321. unsigned long flags;
  322. spin_lock_irqsave(&ep->lock, flags);
  323. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  324. spin_unlock_irqrestore(&ep->lock, flags);
  325. queue_pending_output_urbs(ep);
  326. goto exit_clear;
  327. }
  328. prepare_outbound_urb(ep, ctx);
  329. } else {
  330. retire_inbound_urb(ep, ctx);
  331. /* can be stopped during retire callback */
  332. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  333. goto exit_clear;
  334. prepare_inbound_urb(ep, ctx);
  335. }
  336. err = usb_submit_urb(urb, GFP_ATOMIC);
  337. if (err == 0)
  338. return;
  339. snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err);
  340. //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  341. exit_clear:
  342. clear_bit(ctx->index, &ep->active_mask);
  343. }
  344. /**
  345. * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
  346. *
  347. * @chip: The chip
  348. * @alts: The USB host interface
  349. * @ep_num: The number of the endpoint to use
  350. * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE
  351. * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
  352. *
  353. * If the requested endpoint has not been added to the given chip before,
  354. * a new instance is created. Otherwise, a pointer to the previoulsy
  355. * created instance is returned. In case of any error, NULL is returned.
  356. *
  357. * New endpoints will be added to chip->ep_list and must be freed by
  358. * calling snd_usb_endpoint_free().
  359. */
  360. struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
  361. struct usb_host_interface *alts,
  362. int ep_num, int direction, int type)
  363. {
  364. struct snd_usb_endpoint *ep;
  365. int is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;
  366. if (WARN_ON(!alts))
  367. return NULL;
  368. mutex_lock(&chip->mutex);
  369. list_for_each_entry(ep, &chip->ep_list, list) {
  370. if (ep->ep_num == ep_num &&
  371. ep->iface == alts->desc.bInterfaceNumber &&
  372. ep->altsetting == alts->desc.bAlternateSetting) {
  373. snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n",
  374. ep_num, ep->iface, ep->altsetting, ep);
  375. goto __exit_unlock;
  376. }
  377. }
  378. snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n",
  379. is_playback ? "playback" : "capture",
  380. type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  381. ep_num);
  382. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  383. if (!ep)
  384. goto __exit_unlock;
  385. ep->chip = chip;
  386. spin_lock_init(&ep->lock);
  387. ep->type = type;
  388. ep->ep_num = ep_num;
  389. ep->iface = alts->desc.bInterfaceNumber;
  390. ep->altsetting = alts->desc.bAlternateSetting;
  391. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  392. ep_num &= USB_ENDPOINT_NUMBER_MASK;
  393. if (is_playback)
  394. ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
  395. else
  396. ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
  397. if (type == SND_USB_ENDPOINT_TYPE_SYNC) {
  398. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  399. get_endpoint(alts, 1)->bRefresh >= 1 &&
  400. get_endpoint(alts, 1)->bRefresh <= 9)
  401. ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
  402. else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
  403. ep->syncinterval = 1;
  404. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  405. get_endpoint(alts, 1)->bInterval <= 16)
  406. ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  407. else
  408. ep->syncinterval = 3;
  409. ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  410. }
  411. list_add_tail(&ep->list, &chip->ep_list);
  412. __exit_unlock:
  413. mutex_unlock(&chip->mutex);
  414. return ep;
  415. }
  416. /*
  417. * wait until all urbs are processed.
  418. */
  419. static int wait_clear_urbs(struct snd_usb_endpoint *ep)
  420. {
  421. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  422. int alive;
  423. do {
  424. alive = bitmap_weight(&ep->active_mask, ep->nurbs);
  425. if (!alive)
  426. break;
  427. schedule_timeout_uninterruptible(1);
  428. } while (time_before(jiffies, end_time));
  429. if (alive)
  430. snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n",
  431. alive, ep->ep_num);
  432. clear_bit(EP_FLAG_STOPPING, &ep->flags);
  433. return 0;
  434. }
  435. /* sync the pending stop operation;
  436. * this function itself doesn't trigger the stop operation
  437. */
  438. void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
  439. {
  440. if (ep && test_bit(EP_FLAG_STOPPING, &ep->flags))
  441. wait_clear_urbs(ep);
  442. }
  443. /*
  444. * unlink active urbs.
  445. */
  446. static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force)
  447. {
  448. unsigned int i;
  449. if (!force && ep->chip->shutdown) /* to be sure... */
  450. return -EBADFD;
  451. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  452. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  453. ep->next_packet_read_pos = 0;
  454. ep->next_packet_write_pos = 0;
  455. for (i = 0; i < ep->nurbs; i++) {
  456. if (test_bit(i, &ep->active_mask)) {
  457. if (!test_and_set_bit(i, &ep->unlink_mask)) {
  458. struct urb *u = ep->urb[i].urb;
  459. usb_unlink_urb(u);
  460. }
  461. }
  462. }
  463. return 0;
  464. }
  465. /*
  466. * release an endpoint's urbs
  467. */
  468. static void release_urbs(struct snd_usb_endpoint *ep, int force)
  469. {
  470. int i;
  471. /* route incoming urbs to nirvana */
  472. ep->retire_data_urb = NULL;
  473. ep->prepare_data_urb = NULL;
  474. /* stop urbs */
  475. deactivate_urbs(ep, force);
  476. wait_clear_urbs(ep);
  477. for (i = 0; i < ep->nurbs; i++)
  478. release_urb_ctx(&ep->urb[i]);
  479. if (ep->syncbuf)
  480. usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
  481. ep->syncbuf, ep->sync_dma);
  482. ep->syncbuf = NULL;
  483. ep->nurbs = 0;
  484. }
  485. /*
  486. * configure a data endpoint
  487. */
  488. static int data_ep_set_params(struct snd_usb_endpoint *ep,
  489. snd_pcm_format_t pcm_format,
  490. unsigned int channels,
  491. unsigned int period_bytes,
  492. unsigned int frames_per_period,
  493. unsigned int periods_per_buffer,
  494. struct audioformat *fmt,
  495. struct snd_usb_endpoint *sync_ep)
  496. {
  497. unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
  498. unsigned int max_packs_per_period, urbs_per_period, urb_packs;
  499. unsigned int max_urbs, i;
  500. int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels;
  501. if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
  502. /*
  503. * When operating in DSD DOP mode, the size of a sample frame
  504. * in hardware differs from the actual physical format width
  505. * because we need to make room for the DOP markers.
  506. */
  507. frame_bits += channels << 3;
  508. }
  509. ep->datainterval = fmt->datainterval;
  510. ep->stride = frame_bits >> 3;
  511. ep->silence_value = pcm_format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0;
  512. /* assume max. frequency is 25% higher than nominal */
  513. ep->freqmax = ep->freqn + (ep->freqn >> 2);
  514. maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3))
  515. >> (16 - ep->datainterval);
  516. /* but wMaxPacketSize might reduce this */
  517. if (ep->maxpacksize && ep->maxpacksize < maxsize) {
  518. /* whatever fits into a max. size packet */
  519. maxsize = ep->maxpacksize;
  520. ep->freqmax = (maxsize / (frame_bits >> 3))
  521. << (16 - ep->datainterval);
  522. }
  523. if (ep->fill_max)
  524. ep->curpacksize = ep->maxpacksize;
  525. else
  526. ep->curpacksize = maxsize;
  527. if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
  528. packs_per_ms = 8 >> ep->datainterval;
  529. max_packs_per_urb = MAX_PACKS_HS;
  530. } else {
  531. packs_per_ms = 1;
  532. max_packs_per_urb = MAX_PACKS;
  533. }
  534. if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
  535. max_packs_per_urb = min(max_packs_per_urb,
  536. 1U << sync_ep->syncinterval);
  537. max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
  538. /*
  539. * Capture endpoints need to use small URBs because there's no way
  540. * to tell in advance where the next period will end, and we don't
  541. * want the next URB to complete much after the period ends.
  542. *
  543. * Playback endpoints with implicit sync much use the same parameters
  544. * as their corresponding capture endpoint.
  545. */
  546. if (usb_pipein(ep->pipe) ||
  547. snd_usb_endpoint_implicit_feedback_sink(ep)) {
  548. /* make capture URBs <= 1 ms and smaller than a period */
  549. urb_packs = min(max_packs_per_urb, packs_per_ms);
  550. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  551. urb_packs >>= 1;
  552. ep->nurbs = MAX_URBS;
  553. /*
  554. * Playback endpoints without implicit sync are adjusted so that
  555. * a period fits as evenly as possible in the smallest number of
  556. * URBs. The total number of URBs is adjusted to the size of the
  557. * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
  558. */
  559. } else {
  560. /* determine how small a packet can be */
  561. minsize = (ep->freqn >> (16 - ep->datainterval)) *
  562. (frame_bits >> 3);
  563. /* with sync from device, assume it can be 12% lower */
  564. if (sync_ep)
  565. minsize -= minsize >> 3;
  566. minsize = max(minsize, 1u);
  567. /* how many packets will contain an entire ALSA period? */
  568. max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize);
  569. /* how many URBs will contain a period? */
  570. urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
  571. max_packs_per_urb);
  572. /* how many packets are needed in each URB? */
  573. urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
  574. /* limit the number of frames in a single URB */
  575. ep->max_urb_frames = DIV_ROUND_UP(frames_per_period,
  576. urbs_per_period);
  577. /* try to use enough URBs to contain an entire ALSA buffer */
  578. max_urbs = min((unsigned) MAX_URBS,
  579. MAX_QUEUE * packs_per_ms / urb_packs);
  580. ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer);
  581. }
  582. /* allocate and initialize data urbs */
  583. for (i = 0; i < ep->nurbs; i++) {
  584. struct snd_urb_ctx *u = &ep->urb[i];
  585. u->index = i;
  586. u->ep = ep;
  587. u->packets = urb_packs;
  588. u->buffer_size = maxsize * u->packets;
  589. if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
  590. u->packets++; /* for transfer delimiter */
  591. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  592. if (!u->urb)
  593. goto out_of_memory;
  594. u->urb->transfer_buffer =
  595. usb_alloc_coherent(ep->chip->dev, u->buffer_size,
  596. GFP_KERNEL, &u->urb->transfer_dma);
  597. if (!u->urb->transfer_buffer)
  598. goto out_of_memory;
  599. u->urb->pipe = ep->pipe;
  600. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  601. u->urb->interval = 1 << ep->datainterval;
  602. u->urb->context = u;
  603. u->urb->complete = snd_complete_urb;
  604. INIT_LIST_HEAD(&u->ready_list);
  605. }
  606. return 0;
  607. out_of_memory:
  608. release_urbs(ep, 0);
  609. return -ENOMEM;
  610. }
  611. /*
  612. * configure a sync endpoint
  613. */
  614. static int sync_ep_set_params(struct snd_usb_endpoint *ep)
  615. {
  616. int i;
  617. ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
  618. GFP_KERNEL, &ep->sync_dma);
  619. if (!ep->syncbuf)
  620. return -ENOMEM;
  621. for (i = 0; i < SYNC_URBS; i++) {
  622. struct snd_urb_ctx *u = &ep->urb[i];
  623. u->index = i;
  624. u->ep = ep;
  625. u->packets = 1;
  626. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  627. if (!u->urb)
  628. goto out_of_memory;
  629. u->urb->transfer_buffer = ep->syncbuf + i * 4;
  630. u->urb->transfer_dma = ep->sync_dma + i * 4;
  631. u->urb->transfer_buffer_length = 4;
  632. u->urb->pipe = ep->pipe;
  633. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  634. u->urb->number_of_packets = 1;
  635. u->urb->interval = 1 << ep->syncinterval;
  636. u->urb->context = u;
  637. u->urb->complete = snd_complete_urb;
  638. }
  639. ep->nurbs = SYNC_URBS;
  640. return 0;
  641. out_of_memory:
  642. release_urbs(ep, 0);
  643. return -ENOMEM;
  644. }
  645. /**
  646. * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
  647. *
  648. * @ep: the snd_usb_endpoint to configure
  649. * @pcm_format: the audio fomat.
  650. * @channels: the number of audio channels.
  651. * @period_bytes: the number of bytes in one alsa period.
  652. * @period_frames: the number of frames in one alsa period.
  653. * @buffer_periods: the number of periods in one alsa buffer.
  654. * @rate: the frame rate.
  655. * @fmt: the USB audio format information
  656. * @sync_ep: the sync endpoint to use, if any
  657. *
  658. * Determine the number of URBs to be used on this endpoint.
  659. * An endpoint must be configured before it can be started.
  660. * An endpoint that is already running can not be reconfigured.
  661. */
  662. int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
  663. snd_pcm_format_t pcm_format,
  664. unsigned int channels,
  665. unsigned int period_bytes,
  666. unsigned int period_frames,
  667. unsigned int buffer_periods,
  668. unsigned int rate,
  669. struct audioformat *fmt,
  670. struct snd_usb_endpoint *sync_ep)
  671. {
  672. int err;
  673. if (ep->use_count != 0) {
  674. snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n",
  675. ep->ep_num);
  676. return -EBUSY;
  677. }
  678. /* release old buffers, if any */
  679. release_urbs(ep, 0);
  680. ep->datainterval = fmt->datainterval;
  681. ep->maxpacksize = fmt->maxpacksize;
  682. ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
  683. if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)
  684. ep->freqn = get_usb_full_speed_rate(rate);
  685. else
  686. ep->freqn = get_usb_high_speed_rate(rate);
  687. /* calculate the frequency in 16.16 format */
  688. ep->freqm = ep->freqn;
  689. ep->freqshift = INT_MIN;
  690. ep->phase = 0;
  691. switch (ep->type) {
  692. case SND_USB_ENDPOINT_TYPE_DATA:
  693. err = data_ep_set_params(ep, pcm_format, channels,
  694. period_bytes, period_frames,
  695. buffer_periods, fmt, sync_ep);
  696. break;
  697. case SND_USB_ENDPOINT_TYPE_SYNC:
  698. err = sync_ep_set_params(ep);
  699. break;
  700. default:
  701. err = -EINVAL;
  702. }
  703. snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",
  704. ep->ep_num, ep->type, ep->nurbs, err);
  705. return err;
  706. }
  707. /**
  708. * snd_usb_endpoint_start: start an snd_usb_endpoint
  709. *
  710. * @ep: the endpoint to start
  711. * @can_sleep: flag indicating whether the operation is executed in
  712. * non-atomic context
  713. *
  714. * A call to this function will increment the use count of the endpoint.
  715. * In case it is not already running, the URBs for this endpoint will be
  716. * submitted. Otherwise, this function does nothing.
  717. *
  718. * Must be balanced to calls of snd_usb_endpoint_stop().
  719. *
  720. * Returns an error if the URB submission failed, 0 in all other cases.
  721. */
  722. int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, bool can_sleep)
  723. {
  724. int err;
  725. unsigned int i;
  726. if (ep->chip->shutdown)
  727. return -EBADFD;
  728. /* already running? */
  729. if (++ep->use_count != 1)
  730. return 0;
  731. /* just to be sure */
  732. deactivate_urbs(ep, false);
  733. if (can_sleep)
  734. wait_clear_urbs(ep);
  735. ep->active_mask = 0;
  736. ep->unlink_mask = 0;
  737. ep->phase = 0;
  738. snd_usb_endpoint_start_quirk(ep);
  739. /*
  740. * If this endpoint has a data endpoint as implicit feedback source,
  741. * don't start the urbs here. Instead, mark them all as available,
  742. * wait for the record urbs to return and queue the playback urbs
  743. * from that context.
  744. */
  745. set_bit(EP_FLAG_RUNNING, &ep->flags);
  746. if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
  747. for (i = 0; i < ep->nurbs; i++) {
  748. struct snd_urb_ctx *ctx = ep->urb + i;
  749. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  750. }
  751. return 0;
  752. }
  753. for (i = 0; i < ep->nurbs; i++) {
  754. struct urb *urb = ep->urb[i].urb;
  755. if (snd_BUG_ON(!urb))
  756. goto __error;
  757. if (usb_pipeout(ep->pipe)) {
  758. prepare_outbound_urb(ep, urb->context);
  759. } else {
  760. prepare_inbound_urb(ep, urb->context);
  761. }
  762. err = usb_submit_urb(urb, GFP_ATOMIC);
  763. if (err < 0) {
  764. snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n",
  765. i, err, usb_error_string(err));
  766. goto __error;
  767. }
  768. set_bit(i, &ep->active_mask);
  769. }
  770. return 0;
  771. __error:
  772. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  773. ep->use_count--;
  774. deactivate_urbs(ep, false);
  775. return -EPIPE;
  776. }
  777. /**
  778. * snd_usb_endpoint_stop: stop an snd_usb_endpoint
  779. *
  780. * @ep: the endpoint to stop (may be NULL)
  781. *
  782. * A call to this function will decrement the use count of the endpoint.
  783. * In case the last user has requested the endpoint stop, the URBs will
  784. * actually be deactivated.
  785. *
  786. * Must be balanced to calls of snd_usb_endpoint_start().
  787. *
  788. * The caller needs to synchronize the pending stop operation via
  789. * snd_usb_endpoint_sync_pending_stop().
  790. */
  791. void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
  792. {
  793. if (!ep)
  794. return;
  795. if (snd_BUG_ON(ep->use_count == 0))
  796. return;
  797. if (--ep->use_count == 0) {
  798. deactivate_urbs(ep, false);
  799. ep->data_subs = NULL;
  800. ep->sync_slave = NULL;
  801. ep->retire_data_urb = NULL;
  802. ep->prepare_data_urb = NULL;
  803. set_bit(EP_FLAG_STOPPING, &ep->flags);
  804. }
  805. }
  806. /**
  807. * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint
  808. *
  809. * @ep: the endpoint to deactivate
  810. *
  811. * If the endpoint is not currently in use, this functions will
  812. * deactivate its associated URBs.
  813. *
  814. * In case of any active users, this functions does nothing.
  815. */
  816. void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
  817. {
  818. if (!ep)
  819. return;
  820. if (ep->use_count != 0)
  821. return;
  822. deactivate_urbs(ep, true);
  823. wait_clear_urbs(ep);
  824. }
  825. /**
  826. * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
  827. *
  828. * @ep: the list header of the endpoint to free
  829. *
  830. * This function does not care for the endpoint's use count but will tear
  831. * down all the streaming URBs immediately and free all resources.
  832. */
  833. void snd_usb_endpoint_free(struct list_head *head)
  834. {
  835. struct snd_usb_endpoint *ep;
  836. ep = list_entry(head, struct snd_usb_endpoint, list);
  837. release_urbs(ep, 1);
  838. kfree(ep);
  839. }
  840. /**
  841. * snd_usb_handle_sync_urb: parse an USB sync packet
  842. *
  843. * @ep: the endpoint to handle the packet
  844. * @sender: the sending endpoint
  845. * @urb: the received packet
  846. *
  847. * This function is called from the context of an endpoint that received
  848. * the packet and is used to let another endpoint object handle the payload.
  849. */
  850. void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  851. struct snd_usb_endpoint *sender,
  852. const struct urb *urb)
  853. {
  854. int shift;
  855. unsigned int f;
  856. unsigned long flags;
  857. snd_BUG_ON(ep == sender);
  858. /*
  859. * In case the endpoint is operating in implicit feedback mode, prepare
  860. * a new outbound URB that has the same layout as the received packet
  861. * and add it to the list of pending urbs. queue_pending_output_urbs()
  862. * will take care of them later.
  863. */
  864. if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
  865. ep->use_count != 0) {
  866. /* implicit feedback case */
  867. int i, bytes = 0;
  868. struct snd_urb_ctx *in_ctx;
  869. struct snd_usb_packet_info *out_packet;
  870. in_ctx = urb->context;
  871. /* Count overall packet size */
  872. for (i = 0; i < in_ctx->packets; i++)
  873. if (urb->iso_frame_desc[i].status == 0)
  874. bytes += urb->iso_frame_desc[i].actual_length;
  875. /*
  876. * skip empty packets. At least M-Audio's Fast Track Ultra stops
  877. * streaming once it received a 0-byte OUT URB
  878. */
  879. if (bytes == 0)
  880. return;
  881. spin_lock_irqsave(&ep->lock, flags);
  882. out_packet = ep->next_packet + ep->next_packet_write_pos;
  883. /*
  884. * Iterate through the inbound packet and prepare the lengths
  885. * for the output packet. The OUT packet we are about to send
  886. * will have the same amount of payload bytes per stride as the
  887. * IN packet we just received. Since the actual size is scaled
  888. * by the stride, use the sender stride to calculate the length
  889. * in case the number of channels differ between the implicitly
  890. * fed-back endpoint and the synchronizing endpoint.
  891. */
  892. out_packet->packets = in_ctx->packets;
  893. for (i = 0; i < in_ctx->packets; i++) {
  894. if (urb->iso_frame_desc[i].status == 0)
  895. out_packet->packet_size[i] =
  896. urb->iso_frame_desc[i].actual_length / sender->stride;
  897. else
  898. out_packet->packet_size[i] = 0;
  899. }
  900. ep->next_packet_write_pos++;
  901. ep->next_packet_write_pos %= MAX_URBS;
  902. spin_unlock_irqrestore(&ep->lock, flags);
  903. queue_pending_output_urbs(ep);
  904. return;
  905. }
  906. /*
  907. * process after playback sync complete
  908. *
  909. * Full speed devices report feedback values in 10.14 format as samples
  910. * per frame, high speed devices in 16.16 format as samples per
  911. * microframe.
  912. *
  913. * Because the Audio Class 1 spec was written before USB 2.0, many high
  914. * speed devices use a wrong interpretation, some others use an
  915. * entirely different format.
  916. *
  917. * Therefore, we cannot predict what format any particular device uses
  918. * and must detect it automatically.
  919. */
  920. if (urb->iso_frame_desc[0].status != 0 ||
  921. urb->iso_frame_desc[0].actual_length < 3)
  922. return;
  923. f = le32_to_cpup(urb->transfer_buffer);
  924. if (urb->iso_frame_desc[0].actual_length == 3)
  925. f &= 0x00ffffff;
  926. else
  927. f &= 0x0fffffff;
  928. if (f == 0)
  929. return;
  930. if (unlikely(ep->freqshift == INT_MIN)) {
  931. /*
  932. * The first time we see a feedback value, determine its format
  933. * by shifting it left or right until it matches the nominal
  934. * frequency value. This assumes that the feedback does not
  935. * differ from the nominal value more than +50% or -25%.
  936. */
  937. shift = 0;
  938. while (f < ep->freqn - ep->freqn / 4) {
  939. f <<= 1;
  940. shift++;
  941. }
  942. while (f > ep->freqn + ep->freqn / 2) {
  943. f >>= 1;
  944. shift--;
  945. }
  946. ep->freqshift = shift;
  947. } else if (ep->freqshift >= 0)
  948. f <<= ep->freqshift;
  949. else
  950. f >>= -ep->freqshift;
  951. if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
  952. /*
  953. * If the frequency looks valid, set it.
  954. * This value is referred to in prepare_playback_urb().
  955. */
  956. spin_lock_irqsave(&ep->lock, flags);
  957. ep->freqm = f;
  958. spin_unlock_irqrestore(&ep->lock, flags);
  959. } else {
  960. /*
  961. * Out of range; maybe the shift value is wrong.
  962. * Reset it so that we autodetect again the next time.
  963. */
  964. ep->freqshift = INT_MIN;
  965. }
  966. }