endpoint.c 30 KB

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