endpoint.c 29 KB

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