endpoint.c 29 KB

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