endpoint.c 28 KB

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