endpoint.c 29 KB

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