endpoint.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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 <linux/io.h>
  27. #include "usbaudio.h"
  28. #include "helper.h"
  29. #include "card.h"
  30. #include "endpoint.h"
  31. #include "pcm.h"
  32. #include "quirks.h"
  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. struct snd_usb_endpoint *ep = u->ep;
  83. if (u->buffer_size) {
  84. if (!ep->databuf_sram)
  85. usb_free_coherent(u->ep->chip->dev, u->buffer_size,
  86. u->urb->transfer_buffer,
  87. u->urb->transfer_dma);
  88. }
  89. usb_free_urb(u->urb);
  90. u->urb = NULL;
  91. }
  92. static const char *usb_error_string(int err)
  93. {
  94. switch (err) {
  95. case -ENODEV:
  96. return "no device";
  97. case -ENOENT:
  98. return "endpoint not enabled";
  99. case -EPIPE:
  100. return "endpoint stalled";
  101. case -ENOSPC:
  102. return "not enough bandwidth";
  103. case -ESHUTDOWN:
  104. return "device disabled";
  105. case -EHOSTUNREACH:
  106. return "device suspended";
  107. case -EINVAL:
  108. case -EAGAIN:
  109. case -EFBIG:
  110. case -EMSGSIZE:
  111. return "internal error";
  112. default:
  113. return "unknown error";
  114. }
  115. }
  116. /**
  117. * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
  118. *
  119. * @ep: The snd_usb_endpoint
  120. *
  121. * Determine whether an endpoint is driven by an implicit feedback
  122. * data endpoint source.
  123. */
  124. int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
  125. {
  126. return ep->sync_master &&
  127. ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
  128. ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
  129. usb_pipeout(ep->pipe);
  130. }
  131. /*
  132. * For streaming based on information derived from sync endpoints,
  133. * prepare_outbound_urb_sizes() will call next_packet_size() to
  134. * determine the number of samples to be sent in the next packet.
  135. *
  136. * For implicit feedback, next_packet_size() is unused.
  137. */
  138. int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep)
  139. {
  140. unsigned long flags;
  141. int ret;
  142. if (ep->fill_max)
  143. return ep->maxframesize;
  144. spin_lock_irqsave(&ep->lock, flags);
  145. ep->phase = (ep->phase & 0xffff)
  146. + (ep->freqm << ep->datainterval);
  147. ret = min(ep->phase >> 16, ep->maxframesize);
  148. spin_unlock_irqrestore(&ep->lock, flags);
  149. return ret;
  150. }
  151. static void retire_outbound_urb(struct snd_usb_endpoint *ep,
  152. struct snd_urb_ctx *urb_ctx)
  153. {
  154. if (ep->retire_data_urb)
  155. ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
  156. }
  157. static void retire_inbound_urb(struct snd_usb_endpoint *ep,
  158. struct snd_urb_ctx *urb_ctx)
  159. {
  160. struct urb *urb = urb_ctx->urb;
  161. if (unlikely(ep->skip_packets > 0)) {
  162. ep->skip_packets--;
  163. return;
  164. }
  165. if (ep->sync_slave)
  166. snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
  167. if (ep->retire_data_urb)
  168. ep->retire_data_urb(ep->data_subs, urb);
  169. }
  170. static void prepare_silent_urb(struct snd_usb_endpoint *ep,
  171. struct snd_urb_ctx *ctx)
  172. {
  173. struct urb *urb = ctx->urb;
  174. unsigned int offs = 0;
  175. unsigned int extra = 0;
  176. __le32 packet_length;
  177. int i;
  178. /* For tx_length_quirk, put packet length at start of packet */
  179. if (ep->chip->tx_length_quirk)
  180. extra = sizeof(packet_length);
  181. for (i = 0; i < ctx->packets; ++i) {
  182. unsigned int offset;
  183. unsigned int length;
  184. int counts;
  185. if (ctx->packet_size[i])
  186. counts = ctx->packet_size[i];
  187. else
  188. counts = snd_usb_endpoint_next_packet_size(ep);
  189. length = counts * ep->stride; /* number of silent bytes */
  190. offset = offs * ep->stride + extra * i;
  191. urb->iso_frame_desc[i].offset = offset;
  192. urb->iso_frame_desc[i].length = length + extra;
  193. if (extra) {
  194. packet_length = cpu_to_le32(length);
  195. if (ep->databuf_sram) {
  196. memcpy_toio(urb->transfer_buffer + offset,
  197. &packet_length, sizeof(packet_length));
  198. } else {
  199. memcpy(urb->transfer_buffer + offset,
  200. &packet_length, sizeof(packet_length));
  201. }
  202. }
  203. if (ep->databuf_sram)
  204. memset_io(urb->transfer_buffer + offset + extra,
  205. ep->silence_value, length);
  206. else
  207. memset(urb->transfer_buffer + offset + extra,
  208. ep->silence_value, length);
  209. offs += counts;
  210. }
  211. urb->number_of_packets = ctx->packets;
  212. urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
  213. }
  214. /*
  215. * Prepare a PLAYBACK urb for submission to the bus.
  216. */
  217. static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
  218. struct snd_urb_ctx *ctx)
  219. {
  220. struct urb *urb = ctx->urb;
  221. unsigned char *cp = urb->transfer_buffer;
  222. urb->dev = ep->chip->dev; /* we need to set this at each time */
  223. switch (ep->type) {
  224. case SND_USB_ENDPOINT_TYPE_DATA:
  225. if (ep->prepare_data_urb) {
  226. ep->prepare_data_urb(ep->data_subs, urb);
  227. } else {
  228. /* no data provider, so send silence */
  229. prepare_silent_urb(ep, ctx);
  230. }
  231. break;
  232. case SND_USB_ENDPOINT_TYPE_SYNC:
  233. if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
  234. /*
  235. * fill the length and offset of each urb descriptor.
  236. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  237. */
  238. urb->iso_frame_desc[0].length = 4;
  239. urb->iso_frame_desc[0].offset = 0;
  240. cp[0] = ep->freqn;
  241. cp[1] = ep->freqn >> 8;
  242. cp[2] = ep->freqn >> 16;
  243. cp[3] = ep->freqn >> 24;
  244. } else {
  245. /*
  246. * fill the length and offset of each urb descriptor.
  247. * the fixed 10.14 frequency is passed through the pipe.
  248. */
  249. urb->iso_frame_desc[0].length = 3;
  250. urb->iso_frame_desc[0].offset = 0;
  251. cp[0] = ep->freqn >> 2;
  252. cp[1] = ep->freqn >> 10;
  253. cp[2] = ep->freqn >> 18;
  254. }
  255. break;
  256. }
  257. }
  258. /*
  259. * Prepare a CAPTURE or SYNC urb for submission to the bus.
  260. */
  261. static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
  262. struct snd_urb_ctx *urb_ctx)
  263. {
  264. int i, offs;
  265. struct urb *urb = urb_ctx->urb;
  266. urb->dev = ep->chip->dev; /* we need to set this at each time */
  267. switch (ep->type) {
  268. case SND_USB_ENDPOINT_TYPE_DATA:
  269. offs = 0;
  270. for (i = 0; i < urb_ctx->packets; i++) {
  271. urb->iso_frame_desc[i].offset = offs;
  272. urb->iso_frame_desc[i].length = ep->curpacksize;
  273. offs += ep->curpacksize;
  274. }
  275. urb->transfer_buffer_length = offs;
  276. urb->number_of_packets = urb_ctx->packets;
  277. break;
  278. case SND_USB_ENDPOINT_TYPE_SYNC:
  279. urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
  280. urb->iso_frame_desc[0].offset = 0;
  281. break;
  282. }
  283. }
  284. /*
  285. * Send output urbs that have been prepared previously. URBs are dequeued
  286. * from ep->ready_playback_urbs and in case there there aren't any available
  287. * or there are no packets that have been prepared, this function does
  288. * nothing.
  289. *
  290. * The reason why the functionality of sending and preparing URBs is separated
  291. * is that host controllers don't guarantee the order in which they return
  292. * inbound and outbound packets to their submitters.
  293. *
  294. * This function is only used for implicit feedback endpoints. For endpoints
  295. * driven by dedicated sync endpoints, URBs are immediately re-submitted
  296. * from their completion handler.
  297. */
  298. static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
  299. {
  300. while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
  301. unsigned long flags;
  302. struct snd_usb_packet_info *uninitialized_var(packet);
  303. struct snd_urb_ctx *ctx = NULL;
  304. struct urb *urb;
  305. int err, i;
  306. spin_lock_irqsave(&ep->lock, flags);
  307. if (ep->next_packet_read_pos != ep->next_packet_write_pos) {
  308. packet = ep->next_packet + ep->next_packet_read_pos;
  309. ep->next_packet_read_pos++;
  310. ep->next_packet_read_pos %= MAX_URBS;
  311. /* take URB out of FIFO */
  312. if (!list_empty(&ep->ready_playback_urbs))
  313. ctx = list_first_entry(&ep->ready_playback_urbs,
  314. struct snd_urb_ctx, ready_list);
  315. }
  316. spin_unlock_irqrestore(&ep->lock, flags);
  317. if (ctx == NULL)
  318. return;
  319. list_del_init(&ctx->ready_list);
  320. urb = ctx->urb;
  321. /* copy over the length information */
  322. for (i = 0; i < packet->packets; i++)
  323. ctx->packet_size[i] = packet->packet_size[i];
  324. /* call the data handler to fill in playback data */
  325. prepare_outbound_urb(ep, ctx);
  326. err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
  327. if (err < 0)
  328. usb_audio_err(ep->chip,
  329. "Unable to submit urb #%d: %d (urb %p)\n",
  330. ctx->index, err, ctx->urb);
  331. else
  332. set_bit(ctx->index, &ep->active_mask);
  333. }
  334. }
  335. /*
  336. * complete callback for urbs
  337. */
  338. static void snd_complete_urb(struct urb *urb)
  339. {
  340. struct snd_urb_ctx *ctx = urb->context;
  341. struct snd_usb_endpoint *ep = ctx->ep;
  342. struct snd_pcm_substream *substream;
  343. unsigned long flags;
  344. int err;
  345. if (unlikely(urb->status == -ENOENT || /* unlinked */
  346. urb->status == -ENODEV || /* device removed */
  347. urb->status == -ECONNRESET || /* unlinked */
  348. urb->status == -ESHUTDOWN)) /* device disabled */
  349. goto exit_clear;
  350. /* device disconnected */
  351. if (unlikely(atomic_read(&ep->chip->shutdown)))
  352. goto exit_clear;
  353. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  354. goto exit_clear;
  355. if (usb_pipeout(ep->pipe)) {
  356. retire_outbound_urb(ep, ctx);
  357. /* can be stopped during retire callback */
  358. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  359. goto exit_clear;
  360. if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
  361. spin_lock_irqsave(&ep->lock, flags);
  362. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  363. spin_unlock_irqrestore(&ep->lock, flags);
  364. queue_pending_output_urbs(ep);
  365. goto exit_clear;
  366. }
  367. prepare_outbound_urb(ep, ctx);
  368. /* can be stopped during prepare callback */
  369. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  370. goto exit_clear;
  371. } else {
  372. retire_inbound_urb(ep, ctx);
  373. /* can be stopped during retire callback */
  374. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  375. goto exit_clear;
  376. prepare_inbound_urb(ep, ctx);
  377. }
  378. err = usb_submit_urb(urb, GFP_ATOMIC);
  379. if (err == 0)
  380. return;
  381. usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
  382. if (ep->data_subs && ep->data_subs->pcm_substream) {
  383. substream = ep->data_subs->pcm_substream;
  384. snd_pcm_stop_xrun(substream);
  385. }
  386. exit_clear:
  387. clear_bit(ctx->index, &ep->active_mask);
  388. }
  389. /**
  390. * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
  391. *
  392. * @chip: The chip
  393. * @alts: The USB host interface
  394. * @ep_num: The number of the endpoint to use
  395. * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE
  396. * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
  397. *
  398. * If the requested endpoint has not been added to the given chip before,
  399. * a new instance is created. Otherwise, a pointer to the previoulsy
  400. * created instance is returned. In case of any error, NULL is returned.
  401. *
  402. * New endpoints will be added to chip->ep_list and must be freed by
  403. * calling snd_usb_endpoint_free().
  404. *
  405. * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
  406. * bNumEndpoints > 1 beforehand.
  407. */
  408. struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
  409. struct usb_host_interface *alts,
  410. int ep_num, int direction, int type)
  411. {
  412. struct snd_usb_endpoint *ep;
  413. int is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;
  414. if (WARN_ON(!alts))
  415. return NULL;
  416. mutex_lock(&chip->mutex);
  417. list_for_each_entry(ep, &chip->ep_list, list) {
  418. if (ep->ep_num == ep_num &&
  419. ep->iface == alts->desc.bInterfaceNumber &&
  420. ep->altsetting == alts->desc.bAlternateSetting) {
  421. usb_audio_dbg(ep->chip,
  422. "Re-using EP %x in iface %d,%d @%p\n",
  423. ep_num, ep->iface, ep->altsetting, ep);
  424. goto __exit_unlock;
  425. }
  426. }
  427. usb_audio_info(chip, "Creating new %s %s endpoint #%x\n",
  428. is_playback ? "playback" : "capture",
  429. type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  430. ep_num);
  431. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  432. if (!ep)
  433. goto __exit_unlock;
  434. ep->chip = chip;
  435. spin_lock_init(&ep->lock);
  436. ep->type = type;
  437. ep->ep_num = ep_num;
  438. ep->iface = alts->desc.bInterfaceNumber;
  439. ep->altsetting = alts->desc.bAlternateSetting;
  440. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  441. ep_num &= USB_ENDPOINT_NUMBER_MASK;
  442. if (is_playback)
  443. ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
  444. else
  445. ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
  446. if (type == SND_USB_ENDPOINT_TYPE_SYNC) {
  447. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  448. get_endpoint(alts, 1)->bRefresh >= 1 &&
  449. get_endpoint(alts, 1)->bRefresh <= 9)
  450. ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
  451. else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
  452. ep->syncinterval = 1;
  453. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  454. get_endpoint(alts, 1)->bInterval <= 16)
  455. ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  456. else
  457. ep->syncinterval = 3;
  458. ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  459. /* let controller driver to know endpoint type */
  460. get_endpoint(alts, 1)->bmAttributes |=
  461. USB_ENDPOINT_USAGE_FEEDBACK;
  462. }
  463. list_add_tail(&ep->list, &chip->ep_list);
  464. __exit_unlock:
  465. mutex_unlock(&chip->mutex);
  466. return ep;
  467. }
  468. /*
  469. * wait until all urbs are processed.
  470. */
  471. static int wait_clear_urbs(struct snd_usb_endpoint *ep)
  472. {
  473. unsigned long end_time = jiffies + msecs_to_jiffies(1500);
  474. int alive;
  475. do {
  476. alive = bitmap_weight(&ep->active_mask, ep->nurbs);
  477. if (!alive)
  478. break;
  479. schedule_timeout_uninterruptible(1);
  480. } while (time_before(jiffies, end_time));
  481. if (alive)
  482. usb_audio_err(ep->chip,
  483. "timeout: still %d active urbs on EP #%x\n",
  484. alive, ep->ep_num);
  485. clear_bit(EP_FLAG_STOPPING, &ep->flags);
  486. ep->data_subs = NULL;
  487. ep->sync_slave = NULL;
  488. ep->retire_data_urb = NULL;
  489. ep->prepare_data_urb = NULL;
  490. return 0;
  491. }
  492. /* sync the pending stop operation;
  493. * this function itself doesn't trigger the stop operation
  494. */
  495. void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
  496. {
  497. if (ep && test_bit(EP_FLAG_STOPPING, &ep->flags))
  498. wait_clear_urbs(ep);
  499. }
  500. /*
  501. * unlink active urbs.
  502. */
  503. static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force)
  504. {
  505. unsigned int i;
  506. if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
  507. return -EBADFD;
  508. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  509. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  510. ep->next_packet_read_pos = 0;
  511. ep->next_packet_write_pos = 0;
  512. for (i = 0; i < ep->nurbs; i++) {
  513. if (test_bit(i, &ep->active_mask)) {
  514. if (!test_and_set_bit(i, &ep->unlink_mask)) {
  515. struct urb *u = ep->urb[i].urb;
  516. usb_unlink_urb(u);
  517. }
  518. }
  519. }
  520. return 0;
  521. }
  522. /*
  523. * release an endpoint's urbs
  524. */
  525. static void release_urbs(struct snd_usb_endpoint *ep, int force)
  526. {
  527. int i;
  528. /* route incoming urbs to nirvana */
  529. ep->retire_data_urb = NULL;
  530. ep->prepare_data_urb = NULL;
  531. /* stop urbs */
  532. deactivate_urbs(ep, force);
  533. wait_clear_urbs(ep);
  534. for (i = 0; i < ep->nurbs; i++)
  535. release_urb_ctx(&ep->urb[i]);
  536. if (ep->databuf && ep->databuf_sram) {
  537. if (usb_pipein(ep->pipe))
  538. mtk_usb_free_sram(USB_AUDIO_DATA_IN);
  539. else
  540. mtk_usb_free_sram(USB_AUDIO_DATA_OUT);
  541. }
  542. if (ep->syncbuf) {
  543. if (ep->syncbuf_sram)
  544. mtk_usb_free_sram(USB_AUDIO_DATA_SYNC);
  545. else
  546. usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
  547. ep->syncbuf, ep->sync_dma);
  548. }
  549. ep->syncbuf = NULL;
  550. ep->databuf = NULL;
  551. ep->syncbuf_sram = 0;
  552. ep->databuf_sram = 0;
  553. ep->nurbs = 0;
  554. }
  555. /*
  556. * configure a data endpoint
  557. */
  558. static int data_ep_set_params(struct snd_usb_endpoint *ep,
  559. snd_pcm_format_t pcm_format,
  560. unsigned int channels,
  561. unsigned int period_bytes,
  562. unsigned int frames_per_period,
  563. unsigned int periods_per_buffer,
  564. struct audioformat *fmt,
  565. struct snd_usb_endpoint *sync_ep)
  566. {
  567. unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
  568. unsigned int max_packs_per_period, urbs_per_period, urb_packs;
  569. unsigned int max_urbs, i;
  570. int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels;
  571. unsigned int max_queue;
  572. int tx_length_quirk = (ep->chip->tx_length_quirk &&
  573. usb_pipeout(ep->pipe));
  574. if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
  575. /*
  576. * When operating in DSD DOP mode, the size of a sample frame
  577. * in hardware differs from the actual physical format width
  578. * because we need to make room for the DOP markers.
  579. */
  580. frame_bits += channels << 3;
  581. }
  582. ep->datainterval = fmt->datainterval;
  583. ep->stride = frame_bits >> 3;
  584. switch (pcm_format) {
  585. case SNDRV_PCM_FORMAT_U8:
  586. ep->silence_value = 0x80;
  587. break;
  588. case SNDRV_PCM_FORMAT_DSD_U8:
  589. case SNDRV_PCM_FORMAT_DSD_U16_LE:
  590. case SNDRV_PCM_FORMAT_DSD_U32_LE:
  591. case SNDRV_PCM_FORMAT_DSD_U16_BE:
  592. case SNDRV_PCM_FORMAT_DSD_U32_BE:
  593. ep->silence_value = 0x69;
  594. break;
  595. default:
  596. ep->silence_value = 0;
  597. }
  598. /* assume max. frequency is 50% higher than nominal */
  599. ep->freqmax = ep->freqn + (ep->freqn >> 1);
  600. /* Round up freqmax to nearest integer in order to calculate maximum
  601. * packet size, which must represent a whole number of frames.
  602. * This is accomplished by adding 0x0.ffff before converting the
  603. * Q16.16 format into integer.
  604. * In order to accurately calculate the maximum packet size when
  605. * the data interval is more than 1 (i.e. ep->datainterval > 0),
  606. * multiply by the data interval prior to rounding. For instance,
  607. * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
  608. * frames with a data interval of 1, but 11 (10.25) frames with a
  609. * data interval of 2.
  610. * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
  611. * maximum datainterval value of 3, at USB full speed, higher for
  612. * USB high speed, noting that ep->freqmax is in units of
  613. * frames per packet in Q16.16 format.)
  614. */
  615. maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
  616. (frame_bits >> 3);
  617. if (tx_length_quirk)
  618. maxsize += sizeof(__le32); /* Space for length descriptor */
  619. /* but wMaxPacketSize might reduce this */
  620. if (ep->maxpacksize && ep->maxpacksize < maxsize) {
  621. /* whatever fits into a max. size packet */
  622. unsigned int data_maxsize = maxsize = ep->maxpacksize;
  623. if (tx_length_quirk)
  624. /* Need to remove the length descriptor to calc freq */
  625. data_maxsize -= sizeof(__le32);
  626. ep->freqmax = (data_maxsize / (frame_bits >> 3))
  627. << (16 - ep->datainterval);
  628. }
  629. if (ep->fill_max)
  630. ep->curpacksize = ep->maxpacksize;
  631. else
  632. ep->curpacksize = maxsize;
  633. if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
  634. packs_per_ms = 8 >> ep->datainterval;
  635. max_packs_per_urb = MAX_PACKS_HS;
  636. max_queue = MAX_QUEUE_HS;
  637. } else {
  638. packs_per_ms = 1;
  639. max_packs_per_urb = MAX_PACKS;
  640. max_queue = MAX_QUEUE;
  641. }
  642. if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
  643. max_packs_per_urb = min(max_packs_per_urb,
  644. 1U << sync_ep->syncinterval);
  645. max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
  646. /*
  647. * Capture endpoints need to use small URBs because there's no way
  648. * to tell in advance where the next period will end, and we don't
  649. * want the next URB to complete much after the period ends.
  650. *
  651. * Playback endpoints with implicit sync much use the same parameters
  652. * as their corresponding capture endpoint.
  653. */
  654. if (usb_pipein(ep->pipe) ||
  655. snd_usb_endpoint_implicit_feedback_sink(ep)) {
  656. urb_packs = packs_per_ms;
  657. /*
  658. * Wireless devices can poll at a max rate of once per 4ms.
  659. * For dataintervals less than 5, increase the packet count to
  660. * allow the host controller to use bursting to fill in the
  661. * gaps.
  662. */
  663. if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) {
  664. int interval = ep->datainterval;
  665. while (interval < 5) {
  666. urb_packs <<= 1;
  667. ++interval;
  668. }
  669. }
  670. /* make capture URBs <= 1 ms and smaller than a period */
  671. urb_packs = min(max_packs_per_urb, urb_packs);
  672. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  673. urb_packs >>= 1;
  674. ep->nurbs = MAX_URBS;
  675. usb_audio_info(ep->chip,
  676. "in: frames_per_period=%d, packs_per_ms=%d\n",
  677. frames_per_period, packs_per_ms);
  678. usb_audio_info(ep->chip,
  679. "in: nurbs=%d, urb_packs=%d, periods_per_buffer=%d\n",
  680. ep->nurbs, urb_packs, periods_per_buffer);
  681. /*
  682. * Playback endpoints without implicit sync are adjusted so that
  683. * a period fits as evenly as possible in the smallest number of
  684. * URBs. The total number of URBs is adjusted to the size of the
  685. * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
  686. */
  687. } else {
  688. /* determine how small a packet can be */
  689. minsize = (ep->freqn >> (16 - ep->datainterval)) *
  690. (frame_bits >> 3);
  691. /* with sync from device, assume it can be 12% lower */
  692. if (sync_ep)
  693. minsize -= minsize >> 3;
  694. minsize = max(minsize, 1u);
  695. /* how many packets will contain an entire ALSA period? */
  696. max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize);
  697. /* This is a special case for latency requirement.*/
  698. /* Limit the max packets and max queuein a single URB */
  699. if (periods_per_buffer == 4) {
  700. max_packs_per_urb = packs_per_ms;
  701. max_queue = LOW_LATENCY_MAX_QUEUE;
  702. }
  703. /* how many URBs will contain a period? */
  704. urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
  705. max_packs_per_urb);
  706. /* how many packets are needed in each URB? */
  707. urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
  708. /* limit the number of frames in a single URB */
  709. ep->max_urb_frames = DIV_ROUND_UP(frames_per_period,
  710. urbs_per_period);
  711. /* try to use enough URBs to contain an entire ALSA buffer */
  712. max_urbs = min((unsigned) MAX_URBS,
  713. max_queue * packs_per_ms / urb_packs);
  714. ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer);
  715. if (ep->nurbs < 2)
  716. ep->nurbs++;
  717. usb_audio_info(ep->chip,
  718. "interval=%d, frames_per_period=%d, urbs_per_period=%d\n",
  719. ep->datainterval, frames_per_period, urbs_per_period);
  720. usb_audio_info(ep->chip,
  721. "max_packs_per_period=%d, max_packs_per_urb=%d\n",
  722. max_packs_per_period, max_packs_per_urb);
  723. usb_audio_info(ep->chip,
  724. "nurbs=%d, urbs_per_period=%d, periods_per_buffer=%d\n",
  725. ep->nurbs, urbs_per_period, periods_per_buffer);
  726. }
  727. /* allocate and initialize data urbs */
  728. if (usb_pipein(ep->pipe))
  729. ep->databuf = mtk_usb_alloc_sram(USB_AUDIO_DATA_IN,
  730. ep->nurbs * maxsize * urb_packs, &ep->data_dma);
  731. else
  732. ep->databuf = mtk_usb_alloc_sram(USB_AUDIO_DATA_OUT,
  733. ep->nurbs * maxsize * urb_packs, &ep->data_dma);
  734. if (ep->databuf)
  735. ep->databuf_sram = 1;
  736. for (i = 0; i < ep->nurbs; i++) {
  737. struct snd_urb_ctx *u = &ep->urb[i];
  738. u->index = i;
  739. u->ep = ep;
  740. u->packets = urb_packs;
  741. u->buffer_size = maxsize * u->packets;
  742. if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
  743. u->packets++; /* for transfer delimiter */
  744. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  745. if (!u->urb)
  746. goto out_of_memory;
  747. if (ep->databuf_sram) {
  748. u->urb->transfer_buffer = ep->databuf +
  749. i * u->buffer_size;
  750. u->urb->transfer_dma = ep->data_dma +
  751. i * u->buffer_size;
  752. } else {
  753. /* re-allocate buffer */
  754. u->urb->transfer_buffer =
  755. usb_alloc_coherent(ep->chip->dev,
  756. u->buffer_size, GFP_KERNEL,
  757. &u->urb->transfer_dma);
  758. }
  759. if (!u->urb->transfer_buffer)
  760. goto out_of_memory;
  761. u->urb->pipe = ep->pipe;
  762. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  763. u->urb->interval = 1 << ep->datainterval;
  764. u->urb->context = u;
  765. u->urb->complete = snd_complete_urb;
  766. INIT_LIST_HEAD(&u->ready_list);
  767. }
  768. return 0;
  769. out_of_memory:
  770. release_urbs(ep, 0);
  771. return -ENOMEM;
  772. }
  773. /*
  774. * configure a sync endpoint
  775. */
  776. static int sync_ep_set_params(struct snd_usb_endpoint *ep)
  777. {
  778. int i;
  779. /* FIXME feedback ep force use dram */
  780. #if 0
  781. ep->syncbuf = mtk_usb_alloc_sram(USB_AUDIO_DATA_SYNC,
  782. SYNC_URBS * 4, &ep->sync_dma);
  783. #endif
  784. if (ep->syncbuf) {
  785. ep->syncbuf_sram = 1;
  786. } else {
  787. ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
  788. GFP_KERNEL, &ep->sync_dma);
  789. }
  790. if (!ep->syncbuf)
  791. return -ENOMEM;
  792. for (i = 0; i < SYNC_URBS; i++) {
  793. struct snd_urb_ctx *u = &ep->urb[i];
  794. u->index = i;
  795. u->ep = ep;
  796. u->packets = 1;
  797. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  798. if (!u->urb)
  799. goto out_of_memory;
  800. u->urb->transfer_buffer = ep->syncbuf + i * 4;
  801. u->urb->transfer_dma = ep->sync_dma + i * 4;
  802. u->urb->transfer_buffer_length = 4;
  803. u->urb->pipe = ep->pipe;
  804. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  805. u->urb->number_of_packets = 1;
  806. u->urb->interval = 1 << ep->syncinterval;
  807. u->urb->context = u;
  808. u->urb->complete = snd_complete_urb;
  809. }
  810. ep->nurbs = SYNC_URBS;
  811. return 0;
  812. out_of_memory:
  813. release_urbs(ep, 0);
  814. return -ENOMEM;
  815. }
  816. /**
  817. * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
  818. *
  819. * @ep: the snd_usb_endpoint to configure
  820. * @pcm_format: the audio fomat.
  821. * @channels: the number of audio channels.
  822. * @period_bytes: the number of bytes in one alsa period.
  823. * @period_frames: the number of frames in one alsa period.
  824. * @buffer_periods: the number of periods in one alsa buffer.
  825. * @rate: the frame rate.
  826. * @fmt: the USB audio format information
  827. * @sync_ep: the sync endpoint to use, if any
  828. *
  829. * Determine the number of URBs to be used on this endpoint.
  830. * An endpoint must be configured before it can be started.
  831. * An endpoint that is already running can not be reconfigured.
  832. */
  833. int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
  834. snd_pcm_format_t pcm_format,
  835. unsigned int channels,
  836. unsigned int period_bytes,
  837. unsigned int period_frames,
  838. unsigned int buffer_periods,
  839. unsigned int rate,
  840. struct audioformat *fmt,
  841. struct snd_usb_endpoint *sync_ep)
  842. {
  843. int err;
  844. if (ep->use_count != 0) {
  845. usb_audio_warn(ep->chip,
  846. "Unable to change format on ep #%x: already in use\n",
  847. ep->ep_num);
  848. return -EBUSY;
  849. }
  850. /* release old buffers, if any */
  851. release_urbs(ep, 0);
  852. ep->datainterval = fmt->datainterval;
  853. ep->maxpacksize = fmt->maxpacksize;
  854. ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
  855. if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)
  856. ep->freqn = get_usb_full_speed_rate(rate);
  857. else
  858. ep->freqn = get_usb_high_speed_rate(rate);
  859. /* calculate the frequency in 16.16 format */
  860. ep->freqm = ep->freqn;
  861. ep->freqshift = INT_MIN;
  862. ep->phase = 0;
  863. switch (ep->type) {
  864. case SND_USB_ENDPOINT_TYPE_DATA:
  865. err = data_ep_set_params(ep, pcm_format, channels,
  866. period_bytes, period_frames,
  867. buffer_periods, fmt, sync_ep);
  868. break;
  869. case SND_USB_ENDPOINT_TYPE_SYNC:
  870. err = sync_ep_set_params(ep);
  871. break;
  872. default:
  873. err = -EINVAL;
  874. }
  875. usb_audio_dbg(ep->chip,
  876. "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",
  877. ep->ep_num, ep->type, ep->nurbs, err);
  878. return err;
  879. }
  880. /**
  881. * snd_usb_endpoint_start: start an snd_usb_endpoint
  882. *
  883. * @ep: the endpoint to start
  884. *
  885. * A call to this function will increment the use count of the endpoint.
  886. * In case it is not already running, the URBs for this endpoint will be
  887. * submitted. Otherwise, this function does nothing.
  888. *
  889. * Must be balanced to calls of snd_usb_endpoint_stop().
  890. *
  891. * Returns an error if the URB submission failed, 0 in all other cases.
  892. */
  893. int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
  894. {
  895. int err;
  896. unsigned int i;
  897. if (atomic_read(&ep->chip->shutdown))
  898. return -EBADFD;
  899. /* already running? */
  900. if (++ep->use_count != 1)
  901. return 0;
  902. /* just to be sure */
  903. deactivate_urbs(ep, false);
  904. ep->active_mask = 0;
  905. ep->unlink_mask = 0;
  906. ep->phase = 0;
  907. snd_usb_endpoint_start_quirk(ep);
  908. /*
  909. * If this endpoint has a data endpoint as implicit feedback source,
  910. * don't start the urbs here. Instead, mark them all as available,
  911. * wait for the record urbs to return and queue the playback urbs
  912. * from that context.
  913. */
  914. set_bit(EP_FLAG_RUNNING, &ep->flags);
  915. if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
  916. for (i = 0; i < ep->nurbs; i++) {
  917. struct snd_urb_ctx *ctx = ep->urb + i;
  918. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  919. }
  920. return 0;
  921. }
  922. for (i = 0; i < ep->nurbs; i++) {
  923. struct urb *urb = ep->urb[i].urb;
  924. if (snd_BUG_ON(!urb))
  925. goto __error;
  926. if (usb_pipeout(ep->pipe)) {
  927. prepare_outbound_urb(ep, urb->context);
  928. } else {
  929. prepare_inbound_urb(ep, urb->context);
  930. }
  931. err = usb_submit_urb(urb, GFP_ATOMIC);
  932. if (err < 0) {
  933. usb_audio_err(ep->chip,
  934. "cannot submit urb %d, error %d: %s\n",
  935. i, err, usb_error_string(err));
  936. goto __error;
  937. }
  938. set_bit(i, &ep->active_mask);
  939. }
  940. usb_audio_info(ep->chip, "start %s %s endpoint #%x\n",
  941. usb_pipeout(ep->pipe) ? "out" : "in",
  942. ep->type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  943. ep->ep_num);
  944. return 0;
  945. __error:
  946. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  947. ep->use_count--;
  948. deactivate_urbs(ep, false);
  949. return -EPIPE;
  950. }
  951. /**
  952. * snd_usb_endpoint_stop: stop an snd_usb_endpoint
  953. *
  954. * @ep: the endpoint to stop (may be NULL)
  955. *
  956. * A call to this function will decrement the use count of the endpoint.
  957. * In case the last user has requested the endpoint stop, the URBs will
  958. * actually be deactivated.
  959. *
  960. * Must be balanced to calls of snd_usb_endpoint_start().
  961. *
  962. * The caller needs to synchronize the pending stop operation via
  963. * snd_usb_endpoint_sync_pending_stop().
  964. */
  965. void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
  966. {
  967. if (!ep)
  968. return;
  969. if (snd_BUG_ON(ep->use_count == 0))
  970. return;
  971. if (--ep->use_count == 0) {
  972. deactivate_urbs(ep, false);
  973. set_bit(EP_FLAG_STOPPING, &ep->flags);
  974. usb_audio_info(ep->chip, "stop %s %s endpoint #%x\n",
  975. usb_pipeout(ep->pipe) ? "out" : "in",
  976. ep->type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  977. ep->ep_num);
  978. }
  979. }
  980. /**
  981. * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint
  982. *
  983. * @ep: the endpoint to deactivate
  984. *
  985. * If the endpoint is not currently in use, this functions will
  986. * deactivate its associated URBs.
  987. *
  988. * In case of any active users, this functions does nothing.
  989. */
  990. void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
  991. {
  992. if (!ep)
  993. return;
  994. if (ep->use_count != 0)
  995. return;
  996. deactivate_urbs(ep, true);
  997. wait_clear_urbs(ep);
  998. }
  999. /**
  1000. * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
  1001. *
  1002. * @ep: the endpoint to release
  1003. *
  1004. * This function does not care for the endpoint's use count but will tear
  1005. * down all the streaming URBs immediately.
  1006. */
  1007. void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
  1008. {
  1009. release_urbs(ep, 1);
  1010. }
  1011. /**
  1012. * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
  1013. *
  1014. * @ep: the endpoint to free
  1015. *
  1016. * This free all resources of the given ep.
  1017. */
  1018. void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
  1019. {
  1020. kfree(ep);
  1021. }
  1022. /**
  1023. * snd_usb_handle_sync_urb: parse an USB sync packet
  1024. *
  1025. * @ep: the endpoint to handle the packet
  1026. * @sender: the sending endpoint
  1027. * @urb: the received packet
  1028. *
  1029. * This function is called from the context of an endpoint that received
  1030. * the packet and is used to let another endpoint object handle the payload.
  1031. */
  1032. void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  1033. struct snd_usb_endpoint *sender,
  1034. const struct urb *urb)
  1035. {
  1036. int shift;
  1037. unsigned int f;
  1038. unsigned long flags;
  1039. snd_BUG_ON(ep == sender);
  1040. /*
  1041. * In case the endpoint is operating in implicit feedback mode, prepare
  1042. * a new outbound URB that has the same layout as the received packet
  1043. * and add it to the list of pending urbs. queue_pending_output_urbs()
  1044. * will take care of them later.
  1045. */
  1046. if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
  1047. ep->use_count != 0) {
  1048. /* implicit feedback case */
  1049. int i, bytes = 0;
  1050. struct snd_urb_ctx *in_ctx;
  1051. struct snd_usb_packet_info *out_packet;
  1052. in_ctx = urb->context;
  1053. /* Count overall packet size */
  1054. for (i = 0; i < in_ctx->packets; i++)
  1055. if (urb->iso_frame_desc[i].status == 0)
  1056. bytes += urb->iso_frame_desc[i].actual_length;
  1057. /*
  1058. * skip empty packets. At least M-Audio's Fast Track Ultra stops
  1059. * streaming once it received a 0-byte OUT URB
  1060. */
  1061. if (bytes == 0)
  1062. return;
  1063. spin_lock_irqsave(&ep->lock, flags);
  1064. out_packet = ep->next_packet + ep->next_packet_write_pos;
  1065. /*
  1066. * Iterate through the inbound packet and prepare the lengths
  1067. * for the output packet. The OUT packet we are about to send
  1068. * will have the same amount of payload bytes per stride as the
  1069. * IN packet we just received. Since the actual size is scaled
  1070. * by the stride, use the sender stride to calculate the length
  1071. * in case the number of channels differ between the implicitly
  1072. * fed-back endpoint and the synchronizing endpoint.
  1073. */
  1074. out_packet->packets = in_ctx->packets;
  1075. for (i = 0; i < in_ctx->packets; i++) {
  1076. if (urb->iso_frame_desc[i].status == 0)
  1077. out_packet->packet_size[i] =
  1078. urb->iso_frame_desc[i].actual_length / sender->stride;
  1079. else
  1080. out_packet->packet_size[i] = 0;
  1081. }
  1082. ep->next_packet_write_pos++;
  1083. ep->next_packet_write_pos %= MAX_URBS;
  1084. spin_unlock_irqrestore(&ep->lock, flags);
  1085. queue_pending_output_urbs(ep);
  1086. return;
  1087. }
  1088. /*
  1089. * process after playback sync complete
  1090. *
  1091. * Full speed devices report feedback values in 10.14 format as samples
  1092. * per frame, high speed devices in 16.16 format as samples per
  1093. * microframe.
  1094. *
  1095. * Because the Audio Class 1 spec was written before USB 2.0, many high
  1096. * speed devices use a wrong interpretation, some others use an
  1097. * entirely different format.
  1098. *
  1099. * Therefore, we cannot predict what format any particular device uses
  1100. * and must detect it automatically.
  1101. */
  1102. if (urb->iso_frame_desc[0].status != 0 ||
  1103. urb->iso_frame_desc[0].actual_length < 3)
  1104. return;
  1105. f = le32_to_cpup(urb->transfer_buffer);
  1106. if (urb->iso_frame_desc[0].actual_length == 3)
  1107. f &= 0x00ffffff;
  1108. else
  1109. f &= 0x0fffffff;
  1110. if (f == 0)
  1111. return;
  1112. if (unlikely(sender->tenor_fb_quirk)) {
  1113. /*
  1114. * Devices based on Tenor 8802 chipsets (TEAC UD-H01
  1115. * and others) sometimes change the feedback value
  1116. * by +/- 0x1.0000.
  1117. */
  1118. if (f < ep->freqn - 0x8000)
  1119. f += 0xf000;
  1120. else if (f > ep->freqn + 0x8000)
  1121. f -= 0xf000;
  1122. } else if (unlikely(ep->freqshift == INT_MIN)) {
  1123. /*
  1124. * The first time we see a feedback value, determine its format
  1125. * by shifting it left or right until it matches the nominal
  1126. * frequency value. This assumes that the feedback does not
  1127. * differ from the nominal value more than +50% or -25%.
  1128. */
  1129. shift = 0;
  1130. while (f < ep->freqn - ep->freqn / 4) {
  1131. f <<= 1;
  1132. shift++;
  1133. }
  1134. while (f > ep->freqn + ep->freqn / 2) {
  1135. f >>= 1;
  1136. shift--;
  1137. }
  1138. ep->freqshift = shift;
  1139. } else if (ep->freqshift >= 0)
  1140. f <<= ep->freqshift;
  1141. else
  1142. f >>= -ep->freqshift;
  1143. if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
  1144. /*
  1145. * If the frequency looks valid, set it.
  1146. * This value is referred to in prepare_playback_urb().
  1147. */
  1148. spin_lock_irqsave(&ep->lock, flags);
  1149. ep->freqm = f;
  1150. spin_unlock_irqrestore(&ep->lock, flags);
  1151. } else {
  1152. /*
  1153. * Out of range; maybe the shift value is wrong.
  1154. * Reset it so that we autodetect again the next time.
  1155. */
  1156. ep->freqshift = INT_MIN;
  1157. }
  1158. }