endpoint.c 28 KB

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