endpoint.c 29 KB

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