endpoint.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. */
  4. #include <linux/gfp.h>
  5. #include <linux/init.h>
  6. #include <linux/ratelimit.h>
  7. #include <linux/usb.h>
  8. #include <linux/usb/audio.h>
  9. #include <linux/slab.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include "usbaudio.h"
  14. #include "helper.h"
  15. #include "card.h"
  16. #include "endpoint.h"
  17. #include "pcm.h"
  18. #include "clock.h"
  19. #include "quirks.h"
  20. enum {
  21. EP_STATE_STOPPED,
  22. EP_STATE_RUNNING,
  23. EP_STATE_STOPPING,
  24. };
  25. /* interface refcounting */
  26. struct snd_usb_iface_ref {
  27. unsigned char iface;
  28. bool need_setup;
  29. int opened;
  30. int altset;
  31. struct list_head list;
  32. };
  33. /* clock refcounting */
  34. struct snd_usb_clock_ref {
  35. unsigned char clock;
  36. atomic_t locked;
  37. int opened;
  38. int rate;
  39. bool need_setup;
  40. struct list_head list;
  41. };
  42. /*
  43. * snd_usb_endpoint is a model that abstracts everything related to an
  44. * USB endpoint and its streaming.
  45. *
  46. * There are functions to activate and deactivate the streaming URBs and
  47. * optional callbacks to let the pcm logic handle the actual content of the
  48. * packets for playback and record. Thus, the bus streaming and the audio
  49. * handlers are fully decoupled.
  50. *
  51. * There are two different types of endpoints in audio applications.
  52. *
  53. * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
  54. * inbound and outbound traffic.
  55. *
  56. * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
  57. * expect the payload to carry Q10.14 / Q16.16 formatted sync information
  58. * (3 or 4 bytes).
  59. *
  60. * Each endpoint has to be configured prior to being used by calling
  61. * snd_usb_endpoint_set_params().
  62. *
  63. * The model incorporates a reference counting, so that multiple users
  64. * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
  65. * only the first user will effectively start the URBs, and only the last
  66. * one to stop it will tear the URBs down again.
  67. */
  68. /*
  69. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  70. * this will overflow at approx 524 kHz
  71. */
  72. static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  73. {
  74. return ((rate << 13) + 62) / 125;
  75. }
  76. /*
  77. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  78. * this will overflow at approx 4 MHz
  79. */
  80. static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  81. {
  82. return ((rate << 10) + 62) / 125;
  83. }
  84. /*
  85. * release a urb data
  86. */
  87. static void release_urb_ctx(struct snd_urb_ctx *u)
  88. {
  89. if (u->urb && u->buffer_size)
  90. usb_free_coherent(u->ep->chip->dev, u->buffer_size,
  91. u->urb->transfer_buffer,
  92. u->urb->transfer_dma);
  93. usb_free_urb(u->urb);
  94. u->urb = NULL;
  95. u->buffer_size = 0;
  96. }
  97. static const char *usb_error_string(int err)
  98. {
  99. switch (err) {
  100. case -ENODEV:
  101. return "no device";
  102. case -ENOENT:
  103. return "endpoint not enabled";
  104. case -EPIPE:
  105. return "endpoint stalled";
  106. case -ENOSPC:
  107. return "not enough bandwidth";
  108. case -ESHUTDOWN:
  109. return "device disabled";
  110. case -EHOSTUNREACH:
  111. return "device suspended";
  112. case -EINVAL:
  113. case -EAGAIN:
  114. case -EFBIG:
  115. case -EMSGSIZE:
  116. return "internal error";
  117. default:
  118. return "unknown error";
  119. }
  120. }
  121. static inline bool ep_state_running(struct snd_usb_endpoint *ep)
  122. {
  123. return atomic_read(&ep->state) == EP_STATE_RUNNING;
  124. }
  125. static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new)
  126. {
  127. return atomic_try_cmpxchg(&ep->state, &old, new);
  128. }
  129. /**
  130. * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
  131. *
  132. * @ep: The snd_usb_endpoint
  133. *
  134. * Determine whether an endpoint is driven by an implicit feedback
  135. * data endpoint source.
  136. */
  137. int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
  138. {
  139. return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
  140. }
  141. /*
  142. * Return the number of samples to be sent in the next packet
  143. * for streaming based on information derived from sync endpoints
  144. *
  145. * This won't be used for implicit feedback which takes the packet size
  146. * returned from the sync source
  147. */
  148. static int slave_next_packet_size(struct snd_usb_endpoint *ep,
  149. unsigned int avail)
  150. {
  151. unsigned long flags;
  152. unsigned int phase;
  153. int ret;
  154. if (ep->fill_max)
  155. return ep->maxframesize;
  156. spin_lock_irqsave(&ep->lock, flags);
  157. phase = (ep->phase & 0xffff) + (ep->freqm << ep->datainterval);
  158. ret = min(phase >> 16, ep->maxframesize);
  159. if (avail && ret >= avail)
  160. ret = -EAGAIN;
  161. else
  162. ep->phase = phase;
  163. spin_unlock_irqrestore(&ep->lock, flags);
  164. return ret;
  165. }
  166. /*
  167. * Return the number of samples to be sent in the next packet
  168. * for adaptive and synchronous endpoints
  169. */
  170. static int next_packet_size(struct snd_usb_endpoint *ep, unsigned int avail)
  171. {
  172. unsigned int sample_accum;
  173. int ret;
  174. if (ep->fill_max)
  175. return ep->maxframesize;
  176. sample_accum = ep->sample_accum + ep->sample_rem;
  177. if (sample_accum >= ep->pps) {
  178. sample_accum -= ep->pps;
  179. ret = ep->packsize[1];
  180. } else {
  181. ret = ep->packsize[0];
  182. }
  183. if (avail && ret >= avail)
  184. ret = -EAGAIN;
  185. else
  186. ep->sample_accum = sample_accum;
  187. return ret;
  188. }
  189. /*
  190. * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
  191. * in the next packet
  192. *
  193. * If the size is equal or exceeds @avail, don't proceed but return -EAGAIN
  194. * Exception: @avail = 0 for skipping the check.
  195. */
  196. int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
  197. struct snd_urb_ctx *ctx, int idx,
  198. unsigned int avail)
  199. {
  200. unsigned int packet;
  201. packet = ctx->packet_size[idx];
  202. if (packet) {
  203. if (avail && packet >= avail)
  204. return -EAGAIN;
  205. return packet;
  206. }
  207. if (ep->sync_source)
  208. return slave_next_packet_size(ep, avail);
  209. else
  210. return next_packet_size(ep, avail);
  211. }
  212. static void call_retire_callback(struct snd_usb_endpoint *ep,
  213. struct urb *urb)
  214. {
  215. struct snd_usb_substream *data_subs;
  216. data_subs = READ_ONCE(ep->data_subs);
  217. if (data_subs && ep->retire_data_urb)
  218. ep->retire_data_urb(data_subs, urb);
  219. }
  220. static void retire_outbound_urb(struct snd_usb_endpoint *ep,
  221. struct snd_urb_ctx *urb_ctx)
  222. {
  223. call_retire_callback(ep, urb_ctx->urb);
  224. }
  225. static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  226. struct snd_usb_endpoint *sender,
  227. const struct urb *urb);
  228. static void retire_inbound_urb(struct snd_usb_endpoint *ep,
  229. struct snd_urb_ctx *urb_ctx)
  230. {
  231. struct urb *urb = urb_ctx->urb;
  232. struct snd_usb_endpoint *sync_sink;
  233. if (unlikely(ep->skip_packets > 0)) {
  234. ep->skip_packets--;
  235. return;
  236. }
  237. sync_sink = READ_ONCE(ep->sync_sink);
  238. if (sync_sink)
  239. snd_usb_handle_sync_urb(sync_sink, ep, urb);
  240. call_retire_callback(ep, urb);
  241. }
  242. static inline bool has_tx_length_quirk(struct snd_usb_audio *chip)
  243. {
  244. return chip->quirk_flags & QUIRK_FLAG_TX_LENGTH;
  245. }
  246. static void prepare_silent_urb(struct snd_usb_endpoint *ep,
  247. struct snd_urb_ctx *ctx)
  248. {
  249. struct urb *urb = ctx->urb;
  250. unsigned int offs = 0;
  251. unsigned int extra = 0;
  252. __le32 packet_length;
  253. int i;
  254. /* For tx_length_quirk, put packet length at start of packet */
  255. if (has_tx_length_quirk(ep->chip))
  256. extra = sizeof(packet_length);
  257. for (i = 0; i < ctx->packets; ++i) {
  258. unsigned int offset;
  259. unsigned int length;
  260. int counts;
  261. counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, 0);
  262. length = counts * ep->stride; /* number of silent bytes */
  263. offset = offs * ep->stride + extra * i;
  264. urb->iso_frame_desc[i].offset = offset;
  265. urb->iso_frame_desc[i].length = length + extra;
  266. if (extra) {
  267. packet_length = cpu_to_le32(length);
  268. memcpy(urb->transfer_buffer + offset,
  269. &packet_length, sizeof(packet_length));
  270. }
  271. memset(urb->transfer_buffer + offset + extra,
  272. ep->silence_value, length);
  273. offs += counts;
  274. }
  275. urb->number_of_packets = ctx->packets;
  276. urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
  277. ctx->queued = 0;
  278. }
  279. /*
  280. * Prepare a PLAYBACK urb for submission to the bus.
  281. */
  282. static int prepare_outbound_urb(struct snd_usb_endpoint *ep,
  283. struct snd_urb_ctx *ctx,
  284. bool in_stream_lock)
  285. {
  286. struct urb *urb = ctx->urb;
  287. unsigned char *cp = urb->transfer_buffer;
  288. struct snd_usb_substream *data_subs;
  289. urb->dev = ep->chip->dev; /* we need to set this at each time */
  290. switch (ep->type) {
  291. case SND_USB_ENDPOINT_TYPE_DATA:
  292. data_subs = READ_ONCE(ep->data_subs);
  293. if (data_subs && ep->prepare_data_urb)
  294. return ep->prepare_data_urb(data_subs, urb, in_stream_lock);
  295. /* no data provider, so send silence */
  296. prepare_silent_urb(ep, ctx);
  297. break;
  298. case SND_USB_ENDPOINT_TYPE_SYNC:
  299. if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
  300. /*
  301. * fill the length and offset of each urb descriptor.
  302. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  303. */
  304. urb->iso_frame_desc[0].length = 4;
  305. urb->iso_frame_desc[0].offset = 0;
  306. cp[0] = ep->freqn;
  307. cp[1] = ep->freqn >> 8;
  308. cp[2] = ep->freqn >> 16;
  309. cp[3] = ep->freqn >> 24;
  310. } else {
  311. /*
  312. * fill the length and offset of each urb descriptor.
  313. * the fixed 10.14 frequency is passed through the pipe.
  314. */
  315. urb->iso_frame_desc[0].length = 3;
  316. urb->iso_frame_desc[0].offset = 0;
  317. cp[0] = ep->freqn >> 2;
  318. cp[1] = ep->freqn >> 10;
  319. cp[2] = ep->freqn >> 18;
  320. }
  321. break;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Prepare a CAPTURE or SYNC urb for submission to the bus.
  327. */
  328. static int prepare_inbound_urb(struct snd_usb_endpoint *ep,
  329. struct snd_urb_ctx *urb_ctx)
  330. {
  331. int i, offs;
  332. struct urb *urb = urb_ctx->urb;
  333. urb->dev = ep->chip->dev; /* we need to set this at each time */
  334. switch (ep->type) {
  335. case SND_USB_ENDPOINT_TYPE_DATA:
  336. offs = 0;
  337. for (i = 0; i < urb_ctx->packets; i++) {
  338. urb->iso_frame_desc[i].offset = offs;
  339. urb->iso_frame_desc[i].length = ep->curpacksize;
  340. offs += ep->curpacksize;
  341. }
  342. urb->transfer_buffer_length = offs;
  343. urb->number_of_packets = urb_ctx->packets;
  344. break;
  345. case SND_USB_ENDPOINT_TYPE_SYNC:
  346. urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
  347. urb->iso_frame_desc[0].offset = 0;
  348. break;
  349. }
  350. return 0;
  351. }
  352. /* notify an error as XRUN to the assigned PCM data substream */
  353. static void notify_xrun(struct snd_usb_endpoint *ep)
  354. {
  355. struct snd_usb_substream *data_subs;
  356. struct snd_pcm_substream *psubs;
  357. data_subs = READ_ONCE(ep->data_subs);
  358. if (!data_subs)
  359. return;
  360. psubs = data_subs->pcm_substream;
  361. if (psubs && psubs->runtime &&
  362. psubs->runtime->state == SNDRV_PCM_STATE_RUNNING)
  363. snd_pcm_stop_xrun(psubs);
  364. }
  365. static struct snd_usb_packet_info *
  366. next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
  367. {
  368. struct snd_usb_packet_info *p;
  369. p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
  370. ARRAY_SIZE(ep->next_packet);
  371. ep->next_packet_queued++;
  372. return p;
  373. }
  374. static struct snd_usb_packet_info *
  375. next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
  376. {
  377. struct snd_usb_packet_info *p;
  378. p = ep->next_packet + ep->next_packet_head;
  379. ep->next_packet_head++;
  380. ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
  381. ep->next_packet_queued--;
  382. return p;
  383. }
  384. static void push_back_to_ready_list(struct snd_usb_endpoint *ep,
  385. struct snd_urb_ctx *ctx)
  386. {
  387. unsigned long flags;
  388. spin_lock_irqsave(&ep->lock, flags);
  389. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  390. spin_unlock_irqrestore(&ep->lock, flags);
  391. }
  392. /*
  393. * Send output urbs that have been prepared previously. URBs are dequeued
  394. * from ep->ready_playback_urbs and in case there aren't any available
  395. * or there are no packets that have been prepared, this function does
  396. * nothing.
  397. *
  398. * The reason why the functionality of sending and preparing URBs is separated
  399. * is that host controllers don't guarantee the order in which they return
  400. * inbound and outbound packets to their submitters.
  401. *
  402. * This function is used both for implicit feedback endpoints and in low-
  403. * latency playback mode.
  404. */
  405. int snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep,
  406. bool in_stream_lock)
  407. {
  408. bool implicit_fb = snd_usb_endpoint_implicit_feedback_sink(ep);
  409. while (ep_state_running(ep)) {
  410. unsigned long flags;
  411. struct snd_usb_packet_info *packet;
  412. struct snd_urb_ctx *ctx = NULL;
  413. int err, i;
  414. spin_lock_irqsave(&ep->lock, flags);
  415. if ((!implicit_fb || ep->next_packet_queued > 0) &&
  416. !list_empty(&ep->ready_playback_urbs)) {
  417. /* take URB out of FIFO */
  418. ctx = list_first_entry(&ep->ready_playback_urbs,
  419. struct snd_urb_ctx, ready_list);
  420. list_del_init(&ctx->ready_list);
  421. if (implicit_fb)
  422. packet = next_packet_fifo_dequeue(ep);
  423. }
  424. spin_unlock_irqrestore(&ep->lock, flags);
  425. if (ctx == NULL)
  426. break;
  427. /* copy over the length information */
  428. if (implicit_fb) {
  429. for (i = 0; i < packet->packets; i++)
  430. ctx->packet_size[i] = packet->packet_size[i];
  431. }
  432. /* call the data handler to fill in playback data */
  433. err = prepare_outbound_urb(ep, ctx, in_stream_lock);
  434. /* can be stopped during prepare callback */
  435. if (unlikely(!ep_state_running(ep)))
  436. break;
  437. if (err < 0) {
  438. /* push back to ready list again for -EAGAIN */
  439. if (err == -EAGAIN) {
  440. push_back_to_ready_list(ep, ctx);
  441. break;
  442. }
  443. if (!in_stream_lock)
  444. notify_xrun(ep);
  445. return -EPIPE;
  446. }
  447. if (!atomic_read(&ep->chip->shutdown))
  448. err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
  449. else
  450. err = -ENODEV;
  451. if (err < 0) {
  452. if (!atomic_read(&ep->chip->shutdown)) {
  453. usb_audio_err(ep->chip,
  454. "Unable to submit urb #%d: %d at %s\n",
  455. ctx->index, err, __func__);
  456. if (!in_stream_lock)
  457. notify_xrun(ep);
  458. }
  459. return -EPIPE;
  460. }
  461. set_bit(ctx->index, &ep->active_mask);
  462. atomic_inc(&ep->submitted_urbs);
  463. }
  464. return 0;
  465. }
  466. /*
  467. * complete callback for urbs
  468. */
  469. static void snd_complete_urb(struct urb *urb)
  470. {
  471. struct snd_urb_ctx *ctx = urb->context;
  472. struct snd_usb_endpoint *ep = ctx->ep;
  473. int err;
  474. if (unlikely(urb->status == -ENOENT || /* unlinked */
  475. urb->status == -ENODEV || /* device removed */
  476. urb->status == -ECONNRESET || /* unlinked */
  477. urb->status == -ESHUTDOWN)) /* device disabled */
  478. goto exit_clear;
  479. /* device disconnected */
  480. if (unlikely(atomic_read(&ep->chip->shutdown)))
  481. goto exit_clear;
  482. if (unlikely(!ep_state_running(ep)))
  483. goto exit_clear;
  484. if (usb_pipeout(ep->pipe)) {
  485. retire_outbound_urb(ep, ctx);
  486. /* can be stopped during retire callback */
  487. if (unlikely(!ep_state_running(ep)))
  488. goto exit_clear;
  489. /* in low-latency and implicit-feedback modes, push back the
  490. * URB to ready list at first, then process as much as possible
  491. */
  492. if (ep->lowlatency_playback ||
  493. snd_usb_endpoint_implicit_feedback_sink(ep)) {
  494. push_back_to_ready_list(ep, ctx);
  495. clear_bit(ctx->index, &ep->active_mask);
  496. snd_usb_queue_pending_output_urbs(ep, false);
  497. /* decrement at last, and check xrun */
  498. if (atomic_dec_and_test(&ep->submitted_urbs) &&
  499. !snd_usb_endpoint_implicit_feedback_sink(ep))
  500. notify_xrun(ep);
  501. return;
  502. }
  503. /* in non-lowlatency mode, no error handling for prepare */
  504. prepare_outbound_urb(ep, ctx, false);
  505. /* can be stopped during prepare callback */
  506. if (unlikely(!ep_state_running(ep)))
  507. goto exit_clear;
  508. } else {
  509. retire_inbound_urb(ep, ctx);
  510. /* can be stopped during retire callback */
  511. if (unlikely(!ep_state_running(ep)))
  512. goto exit_clear;
  513. prepare_inbound_urb(ep, ctx);
  514. }
  515. if (!atomic_read(&ep->chip->shutdown))
  516. err = usb_submit_urb(urb, GFP_ATOMIC);
  517. else
  518. err = -ENODEV;
  519. if (err == 0)
  520. return;
  521. if (!atomic_read(&ep->chip->shutdown)) {
  522. usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
  523. notify_xrun(ep);
  524. }
  525. exit_clear:
  526. clear_bit(ctx->index, &ep->active_mask);
  527. atomic_dec(&ep->submitted_urbs);
  528. }
  529. /*
  530. * Find or create a refcount object for the given interface
  531. *
  532. * The objects are released altogether in snd_usb_endpoint_free_all()
  533. */
  534. static struct snd_usb_iface_ref *
  535. iface_ref_find(struct snd_usb_audio *chip, int iface)
  536. {
  537. struct snd_usb_iface_ref *ip;
  538. list_for_each_entry(ip, &chip->iface_ref_list, list)
  539. if (ip->iface == iface)
  540. return ip;
  541. ip = kzalloc(sizeof(*ip), GFP_KERNEL);
  542. if (!ip)
  543. return NULL;
  544. ip->iface = iface;
  545. list_add_tail(&ip->list, &chip->iface_ref_list);
  546. return ip;
  547. }
  548. /* Similarly, a refcount object for clock */
  549. static struct snd_usb_clock_ref *
  550. clock_ref_find(struct snd_usb_audio *chip, int clock)
  551. {
  552. struct snd_usb_clock_ref *ref;
  553. list_for_each_entry(ref, &chip->clock_ref_list, list)
  554. if (ref->clock == clock)
  555. return ref;
  556. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  557. if (!ref)
  558. return NULL;
  559. ref->clock = clock;
  560. atomic_set(&ref->locked, 0);
  561. list_add_tail(&ref->list, &chip->clock_ref_list);
  562. return ref;
  563. }
  564. /*
  565. * Get the existing endpoint object corresponding EP
  566. * Returns NULL if not present.
  567. */
  568. struct snd_usb_endpoint *
  569. snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
  570. {
  571. struct snd_usb_endpoint *ep;
  572. list_for_each_entry(ep, &chip->ep_list, list) {
  573. if (ep->ep_num == ep_num)
  574. return ep;
  575. }
  576. return NULL;
  577. }
  578. #define ep_type_name(type) \
  579. (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
  580. /**
  581. * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
  582. *
  583. * @chip: The chip
  584. * @ep_num: The number of the endpoint to use
  585. * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
  586. *
  587. * If the requested endpoint has not been added to the given chip before,
  588. * a new instance is created.
  589. *
  590. * Returns zero on success or a negative error code.
  591. *
  592. * New endpoints will be added to chip->ep_list and freed by
  593. * calling snd_usb_endpoint_free_all().
  594. *
  595. * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
  596. * bNumEndpoints > 1 beforehand.
  597. */
  598. int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
  599. {
  600. struct snd_usb_endpoint *ep;
  601. bool is_playback;
  602. ep = snd_usb_get_endpoint(chip, ep_num);
  603. if (ep)
  604. return 0;
  605. usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
  606. ep_type_name(type),
  607. ep_num);
  608. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  609. if (!ep)
  610. return -ENOMEM;
  611. ep->chip = chip;
  612. spin_lock_init(&ep->lock);
  613. ep->type = type;
  614. ep->ep_num = ep_num;
  615. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  616. atomic_set(&ep->submitted_urbs, 0);
  617. is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
  618. ep_num &= USB_ENDPOINT_NUMBER_MASK;
  619. if (is_playback)
  620. ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
  621. else
  622. ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
  623. list_add_tail(&ep->list, &chip->ep_list);
  624. return 0;
  625. }
  626. /* Set up syncinterval and maxsyncsize for a sync EP */
  627. static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
  628. struct snd_usb_endpoint *ep)
  629. {
  630. struct usb_host_interface *alts;
  631. struct usb_endpoint_descriptor *desc;
  632. alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
  633. if (!alts)
  634. return;
  635. desc = get_endpoint(alts, ep->ep_idx);
  636. if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  637. desc->bRefresh >= 1 && desc->bRefresh <= 9)
  638. ep->syncinterval = desc->bRefresh;
  639. else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
  640. ep->syncinterval = 1;
  641. else if (desc->bInterval >= 1 && desc->bInterval <= 16)
  642. ep->syncinterval = desc->bInterval - 1;
  643. else
  644. ep->syncinterval = 3;
  645. ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
  646. }
  647. static bool endpoint_compatible(struct snd_usb_endpoint *ep,
  648. const struct audioformat *fp,
  649. const struct snd_pcm_hw_params *params)
  650. {
  651. if (!ep->opened)
  652. return false;
  653. if (ep->cur_audiofmt != fp)
  654. return false;
  655. if (ep->cur_rate != params_rate(params) ||
  656. ep->cur_format != params_format(params) ||
  657. ep->cur_period_frames != params_period_size(params) ||
  658. ep->cur_buffer_periods != params_periods(params))
  659. return false;
  660. return true;
  661. }
  662. /*
  663. * Check whether the given fp and hw params are compatible with the current
  664. * setup of the target EP for implicit feedback sync
  665. */
  666. bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
  667. struct snd_usb_endpoint *ep,
  668. const struct audioformat *fp,
  669. const struct snd_pcm_hw_params *params)
  670. {
  671. bool ret;
  672. mutex_lock(&chip->mutex);
  673. ret = endpoint_compatible(ep, fp, params);
  674. mutex_unlock(&chip->mutex);
  675. return ret;
  676. }
  677. /*
  678. * snd_usb_endpoint_open: Open the endpoint
  679. *
  680. * Called from hw_params to assign the endpoint to the substream.
  681. * It's reference-counted, and only the first opener is allowed to set up
  682. * arbitrary parameters. The later opener must be compatible with the
  683. * former opened parameters.
  684. * The endpoint needs to be closed via snd_usb_endpoint_close() later.
  685. *
  686. * Note that this function doesn't configure the endpoint. The substream
  687. * needs to set it up later via snd_usb_endpoint_set_params() and
  688. * snd_usb_endpoint_prepare().
  689. */
  690. struct snd_usb_endpoint *
  691. snd_usb_endpoint_open(struct snd_usb_audio *chip,
  692. const struct audioformat *fp,
  693. const struct snd_pcm_hw_params *params,
  694. bool is_sync_ep,
  695. bool fixed_rate)
  696. {
  697. struct snd_usb_endpoint *ep;
  698. int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
  699. mutex_lock(&chip->mutex);
  700. ep = snd_usb_get_endpoint(chip, ep_num);
  701. if (!ep) {
  702. usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
  703. goto unlock;
  704. }
  705. if (!ep->opened) {
  706. if (is_sync_ep) {
  707. ep->iface = fp->sync_iface;
  708. ep->altsetting = fp->sync_altsetting;
  709. ep->ep_idx = fp->sync_ep_idx;
  710. } else {
  711. ep->iface = fp->iface;
  712. ep->altsetting = fp->altsetting;
  713. ep->ep_idx = fp->ep_idx;
  714. }
  715. usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
  716. ep_num, ep->iface, ep->altsetting, ep->ep_idx);
  717. ep->iface_ref = iface_ref_find(chip, ep->iface);
  718. if (!ep->iface_ref) {
  719. ep = NULL;
  720. goto unlock;
  721. }
  722. if (fp->protocol != UAC_VERSION_1) {
  723. ep->clock_ref = clock_ref_find(chip, fp->clock);
  724. if (!ep->clock_ref) {
  725. ep = NULL;
  726. goto unlock;
  727. }
  728. ep->clock_ref->opened++;
  729. }
  730. ep->cur_audiofmt = fp;
  731. ep->cur_channels = fp->channels;
  732. ep->cur_rate = params_rate(params);
  733. ep->cur_format = params_format(params);
  734. ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
  735. ep->cur_channels / 8;
  736. ep->cur_period_frames = params_period_size(params);
  737. ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
  738. ep->cur_buffer_periods = params_periods(params);
  739. if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
  740. endpoint_set_syncinterval(chip, ep);
  741. ep->implicit_fb_sync = fp->implicit_fb;
  742. ep->need_setup = true;
  743. ep->need_prepare = true;
  744. ep->fixed_rate = fixed_rate;
  745. usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
  746. ep->cur_channels, ep->cur_rate,
  747. snd_pcm_format_name(ep->cur_format),
  748. ep->cur_period_bytes, ep->cur_buffer_periods,
  749. ep->implicit_fb_sync);
  750. } else {
  751. if (WARN_ON(!ep->iface_ref)) {
  752. ep = NULL;
  753. goto unlock;
  754. }
  755. if (!endpoint_compatible(ep, fp, params)) {
  756. usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
  757. ep_num);
  758. ep = NULL;
  759. goto unlock;
  760. }
  761. usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
  762. ep_num, ep->opened);
  763. }
  764. if (!ep->iface_ref->opened++)
  765. ep->iface_ref->need_setup = true;
  766. ep->opened++;
  767. unlock:
  768. mutex_unlock(&chip->mutex);
  769. return ep;
  770. }
  771. /*
  772. * snd_usb_endpoint_set_sync: Link data and sync endpoints
  773. *
  774. * Pass NULL to sync_ep to unlink again
  775. */
  776. void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
  777. struct snd_usb_endpoint *data_ep,
  778. struct snd_usb_endpoint *sync_ep)
  779. {
  780. data_ep->sync_source = sync_ep;
  781. }
  782. /*
  783. * Set data endpoint callbacks and the assigned data stream
  784. *
  785. * Called at PCM trigger and cleanups.
  786. * Pass NULL to deactivate each callback.
  787. */
  788. void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
  789. int (*prepare)(struct snd_usb_substream *subs,
  790. struct urb *urb,
  791. bool in_stream_lock),
  792. void (*retire)(struct snd_usb_substream *subs,
  793. struct urb *urb),
  794. struct snd_usb_substream *data_subs)
  795. {
  796. ep->prepare_data_urb = prepare;
  797. ep->retire_data_urb = retire;
  798. if (data_subs)
  799. ep->lowlatency_playback = data_subs->lowlatency_playback;
  800. else
  801. ep->lowlatency_playback = false;
  802. WRITE_ONCE(ep->data_subs, data_subs);
  803. }
  804. static int endpoint_set_interface(struct snd_usb_audio *chip,
  805. struct snd_usb_endpoint *ep,
  806. bool set)
  807. {
  808. int altset = set ? ep->altsetting : 0;
  809. int err;
  810. int retries = 0;
  811. const int max_retries = 5;
  812. if (ep->iface_ref->altset == altset)
  813. return 0;
  814. /* already disconnected? */
  815. if (unlikely(atomic_read(&chip->shutdown)))
  816. return -ENODEV;
  817. usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
  818. ep->iface, altset, ep->ep_num);
  819. retry:
  820. err = usb_set_interface(chip->dev, ep->iface, altset);
  821. if (err < 0) {
  822. if (err == -EPROTO && ++retries <= max_retries) {
  823. msleep(5 * (1 << (retries - 1)));
  824. goto retry;
  825. }
  826. usb_audio_err_ratelimited(
  827. chip, "%d:%d: usb_set_interface failed (%d)\n",
  828. ep->iface, altset, err);
  829. return err;
  830. }
  831. if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
  832. msleep(50);
  833. ep->iface_ref->altset = altset;
  834. return 0;
  835. }
  836. /*
  837. * snd_usb_endpoint_close: Close the endpoint
  838. *
  839. * Unreference the already opened endpoint via snd_usb_endpoint_open().
  840. */
  841. void snd_usb_endpoint_close(struct snd_usb_audio *chip,
  842. struct snd_usb_endpoint *ep)
  843. {
  844. mutex_lock(&chip->mutex);
  845. usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
  846. ep->ep_num, ep->opened);
  847. if (!--ep->iface_ref->opened &&
  848. !(chip->quirk_flags & QUIRK_FLAG_IFACE_SKIP_CLOSE))
  849. endpoint_set_interface(chip, ep, false);
  850. if (!--ep->opened) {
  851. if (ep->clock_ref) {
  852. if (!--ep->clock_ref->opened)
  853. ep->clock_ref->rate = 0;
  854. }
  855. ep->iface = 0;
  856. ep->altsetting = 0;
  857. ep->cur_audiofmt = NULL;
  858. ep->cur_rate = 0;
  859. ep->iface_ref = NULL;
  860. ep->clock_ref = NULL;
  861. usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
  862. }
  863. mutex_unlock(&chip->mutex);
  864. }
  865. /* Prepare for suspening EP, called from the main suspend handler */
  866. void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
  867. {
  868. ep->need_prepare = true;
  869. if (ep->iface_ref)
  870. ep->iface_ref->need_setup = true;
  871. if (ep->clock_ref)
  872. ep->clock_ref->rate = 0;
  873. }
  874. /*
  875. * wait until all urbs are processed.
  876. */
  877. static int wait_clear_urbs(struct snd_usb_endpoint *ep)
  878. {
  879. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  880. int alive;
  881. if (atomic_read(&ep->state) != EP_STATE_STOPPING)
  882. return 0;
  883. do {
  884. alive = atomic_read(&ep->submitted_urbs);
  885. if (!alive)
  886. break;
  887. schedule_timeout_uninterruptible(1);
  888. } while (time_before(jiffies, end_time));
  889. if (alive)
  890. usb_audio_err(ep->chip,
  891. "timeout: still %d active urbs on EP #%x\n",
  892. alive, ep->ep_num);
  893. if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) {
  894. ep->sync_sink = NULL;
  895. snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
  896. }
  897. return 0;
  898. }
  899. /* sync the pending stop operation;
  900. * this function itself doesn't trigger the stop operation
  901. */
  902. void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
  903. {
  904. if (ep)
  905. wait_clear_urbs(ep);
  906. }
  907. /*
  908. * Stop active urbs
  909. *
  910. * This function moves the EP to STOPPING state if it's being RUNNING.
  911. */
  912. static int stop_urbs(struct snd_usb_endpoint *ep, bool force, bool keep_pending)
  913. {
  914. unsigned int i;
  915. unsigned long flags;
  916. if (!force && atomic_read(&ep->running))
  917. return -EBUSY;
  918. if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING))
  919. return 0;
  920. spin_lock_irqsave(&ep->lock, flags);
  921. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  922. ep->next_packet_head = 0;
  923. ep->next_packet_queued = 0;
  924. spin_unlock_irqrestore(&ep->lock, flags);
  925. if (keep_pending)
  926. return 0;
  927. for (i = 0; i < ep->nurbs; i++) {
  928. if (test_bit(i, &ep->active_mask)) {
  929. if (!test_and_set_bit(i, &ep->unlink_mask)) {
  930. struct urb *u = ep->urb[i].urb;
  931. usb_unlink_urb(u);
  932. }
  933. }
  934. }
  935. return 0;
  936. }
  937. /*
  938. * release an endpoint's urbs
  939. */
  940. static int release_urbs(struct snd_usb_endpoint *ep, bool force)
  941. {
  942. int i, err;
  943. /* route incoming urbs to nirvana */
  944. snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
  945. /* stop and unlink urbs */
  946. err = stop_urbs(ep, force, false);
  947. if (err)
  948. return err;
  949. wait_clear_urbs(ep);
  950. for (i = 0; i < ep->nurbs; i++)
  951. release_urb_ctx(&ep->urb[i]);
  952. usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
  953. ep->syncbuf, ep->sync_dma);
  954. ep->syncbuf = NULL;
  955. ep->nurbs = 0;
  956. return 0;
  957. }
  958. /*
  959. * configure a data endpoint
  960. */
  961. static int data_ep_set_params(struct snd_usb_endpoint *ep)
  962. {
  963. struct snd_usb_audio *chip = ep->chip;
  964. unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
  965. unsigned int max_packs_per_period, urbs_per_period, urb_packs;
  966. unsigned int max_urbs, i;
  967. const struct audioformat *fmt = ep->cur_audiofmt;
  968. int frame_bits = ep->cur_frame_bytes * 8;
  969. int tx_length_quirk = (has_tx_length_quirk(chip) &&
  970. usb_pipeout(ep->pipe));
  971. usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
  972. ep->ep_num, ep->pipe);
  973. if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
  974. /*
  975. * When operating in DSD DOP mode, the size of a sample frame
  976. * in hardware differs from the actual physical format width
  977. * because we need to make room for the DOP markers.
  978. */
  979. frame_bits += ep->cur_channels << 3;
  980. }
  981. ep->datainterval = fmt->datainterval;
  982. ep->stride = frame_bits >> 3;
  983. switch (ep->cur_format) {
  984. case SNDRV_PCM_FORMAT_U8:
  985. ep->silence_value = 0x80;
  986. break;
  987. case SNDRV_PCM_FORMAT_DSD_U8:
  988. case SNDRV_PCM_FORMAT_DSD_U16_LE:
  989. case SNDRV_PCM_FORMAT_DSD_U32_LE:
  990. case SNDRV_PCM_FORMAT_DSD_U16_BE:
  991. case SNDRV_PCM_FORMAT_DSD_U32_BE:
  992. ep->silence_value = 0x69;
  993. break;
  994. default:
  995. ep->silence_value = 0;
  996. }
  997. /* assume max. frequency is 50% higher than nominal */
  998. ep->freqmax = ep->freqn + (ep->freqn >> 1);
  999. /* Round up freqmax to nearest integer in order to calculate maximum
  1000. * packet size, which must represent a whole number of frames.
  1001. * This is accomplished by adding 0x0.ffff before converting the
  1002. * Q16.16 format into integer.
  1003. * In order to accurately calculate the maximum packet size when
  1004. * the data interval is more than 1 (i.e. ep->datainterval > 0),
  1005. * multiply by the data interval prior to rounding. For instance,
  1006. * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
  1007. * frames with a data interval of 1, but 11 (10.25) frames with a
  1008. * data interval of 2.
  1009. * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
  1010. * maximum datainterval value of 3, at USB full speed, higher for
  1011. * USB high speed, noting that ep->freqmax is in units of
  1012. * frames per packet in Q16.16 format.)
  1013. */
  1014. maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
  1015. (frame_bits >> 3);
  1016. if (tx_length_quirk)
  1017. maxsize += sizeof(__le32); /* Space for length descriptor */
  1018. /* but wMaxPacketSize might reduce this */
  1019. if (ep->maxpacksize && ep->maxpacksize < maxsize) {
  1020. /* whatever fits into a max. size packet */
  1021. unsigned int data_maxsize = maxsize = ep->maxpacksize;
  1022. if (tx_length_quirk)
  1023. /* Need to remove the length descriptor to calc freq */
  1024. data_maxsize -= sizeof(__le32);
  1025. ep->freqmax = (data_maxsize / (frame_bits >> 3))
  1026. << (16 - ep->datainterval);
  1027. }
  1028. if (ep->fill_max)
  1029. ep->curpacksize = ep->maxpacksize;
  1030. else
  1031. ep->curpacksize = maxsize;
  1032. if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
  1033. packs_per_ms = 8 >> ep->datainterval;
  1034. max_packs_per_urb = MAX_PACKS_HS;
  1035. } else {
  1036. packs_per_ms = 1;
  1037. max_packs_per_urb = MAX_PACKS;
  1038. }
  1039. if (ep->sync_source && !ep->implicit_fb_sync)
  1040. max_packs_per_urb = min(max_packs_per_urb,
  1041. 1U << ep->sync_source->syncinterval);
  1042. max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
  1043. /*
  1044. * Capture endpoints need to use small URBs because there's no way
  1045. * to tell in advance where the next period will end, and we don't
  1046. * want the next URB to complete much after the period ends.
  1047. *
  1048. * Playback endpoints with implicit sync much use the same parameters
  1049. * as their corresponding capture endpoint.
  1050. */
  1051. if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
  1052. /* make capture URBs <= 1 ms and smaller than a period */
  1053. urb_packs = min(max_packs_per_urb, packs_per_ms);
  1054. while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
  1055. urb_packs >>= 1;
  1056. ep->nurbs = MAX_URBS;
  1057. /*
  1058. * Playback endpoints without implicit sync are adjusted so that
  1059. * a period fits as evenly as possible in the smallest number of
  1060. * URBs. The total number of URBs is adjusted to the size of the
  1061. * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
  1062. */
  1063. } else {
  1064. /* determine how small a packet can be */
  1065. minsize = (ep->freqn >> (16 - ep->datainterval)) *
  1066. (frame_bits >> 3);
  1067. /* with sync from device, assume it can be 12% lower */
  1068. if (ep->sync_source)
  1069. minsize -= minsize >> 3;
  1070. minsize = max(minsize, 1u);
  1071. /* how many packets will contain an entire ALSA period? */
  1072. max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
  1073. /* how many URBs will contain a period? */
  1074. urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
  1075. max_packs_per_urb);
  1076. /* how many packets are needed in each URB? */
  1077. urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
  1078. /* limit the number of frames in a single URB */
  1079. ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
  1080. urbs_per_period);
  1081. /* try to use enough URBs to contain an entire ALSA buffer */
  1082. max_urbs = min((unsigned) MAX_URBS,
  1083. MAX_QUEUE * packs_per_ms / urb_packs);
  1084. ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
  1085. }
  1086. /* allocate and initialize data urbs */
  1087. for (i = 0; i < ep->nurbs; i++) {
  1088. struct snd_urb_ctx *u = &ep->urb[i];
  1089. u->index = i;
  1090. u->ep = ep;
  1091. u->packets = urb_packs;
  1092. u->buffer_size = maxsize * u->packets;
  1093. if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
  1094. u->packets++; /* for transfer delimiter */
  1095. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  1096. if (!u->urb)
  1097. goto out_of_memory;
  1098. u->urb->transfer_buffer =
  1099. usb_alloc_coherent(chip->dev, u->buffer_size,
  1100. GFP_KERNEL, &u->urb->transfer_dma);
  1101. if (!u->urb->transfer_buffer)
  1102. goto out_of_memory;
  1103. u->urb->pipe = ep->pipe;
  1104. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1105. u->urb->interval = 1 << ep->datainterval;
  1106. u->urb->context = u;
  1107. u->urb->complete = snd_complete_urb;
  1108. INIT_LIST_HEAD(&u->ready_list);
  1109. }
  1110. return 0;
  1111. out_of_memory:
  1112. release_urbs(ep, false);
  1113. return -ENOMEM;
  1114. }
  1115. /*
  1116. * configure a sync endpoint
  1117. */
  1118. static int sync_ep_set_params(struct snd_usb_endpoint *ep)
  1119. {
  1120. struct snd_usb_audio *chip = ep->chip;
  1121. int i;
  1122. usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
  1123. ep->ep_num, ep->pipe);
  1124. ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
  1125. GFP_KERNEL, &ep->sync_dma);
  1126. if (!ep->syncbuf)
  1127. return -ENOMEM;
  1128. ep->nurbs = SYNC_URBS;
  1129. for (i = 0; i < SYNC_URBS; i++) {
  1130. struct snd_urb_ctx *u = &ep->urb[i];
  1131. u->index = i;
  1132. u->ep = ep;
  1133. u->packets = 1;
  1134. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  1135. if (!u->urb)
  1136. goto out_of_memory;
  1137. u->urb->transfer_buffer = ep->syncbuf + i * 4;
  1138. u->urb->transfer_dma = ep->sync_dma + i * 4;
  1139. u->urb->transfer_buffer_length = 4;
  1140. u->urb->pipe = ep->pipe;
  1141. u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1142. u->urb->number_of_packets = 1;
  1143. u->urb->interval = 1 << ep->syncinterval;
  1144. u->urb->context = u;
  1145. u->urb->complete = snd_complete_urb;
  1146. }
  1147. return 0;
  1148. out_of_memory:
  1149. release_urbs(ep, false);
  1150. return -ENOMEM;
  1151. }
  1152. /* update the rate of the referred clock; return the actual rate */
  1153. static int update_clock_ref_rate(struct snd_usb_audio *chip,
  1154. struct snd_usb_endpoint *ep)
  1155. {
  1156. struct snd_usb_clock_ref *clock = ep->clock_ref;
  1157. int rate = ep->cur_rate;
  1158. if (!clock || clock->rate == rate)
  1159. return rate;
  1160. if (clock->rate) {
  1161. if (atomic_read(&clock->locked))
  1162. return clock->rate;
  1163. if (clock->rate != rate) {
  1164. usb_audio_err(chip, "Mismatched sample rate %d vs %d for EP 0x%x\n",
  1165. clock->rate, rate, ep->ep_num);
  1166. return clock->rate;
  1167. }
  1168. }
  1169. clock->rate = rate;
  1170. clock->need_setup = true;
  1171. return rate;
  1172. }
  1173. /*
  1174. * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
  1175. *
  1176. * It's called either from hw_params callback.
  1177. * Determine the number of URBs to be used on this endpoint.
  1178. * An endpoint must be configured before it can be started.
  1179. * An endpoint that is already running can not be reconfigured.
  1180. */
  1181. int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
  1182. struct snd_usb_endpoint *ep)
  1183. {
  1184. const struct audioformat *fmt = ep->cur_audiofmt;
  1185. int err = 0;
  1186. mutex_lock(&chip->mutex);
  1187. if (!ep->need_setup)
  1188. goto unlock;
  1189. /* release old buffers, if any */
  1190. err = release_urbs(ep, false);
  1191. if (err < 0)
  1192. goto unlock;
  1193. ep->datainterval = fmt->datainterval;
  1194. ep->maxpacksize = fmt->maxpacksize;
  1195. ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
  1196. if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
  1197. ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
  1198. ep->pps = 1000 >> ep->datainterval;
  1199. } else {
  1200. ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
  1201. ep->pps = 8000 >> ep->datainterval;
  1202. }
  1203. ep->sample_rem = ep->cur_rate % ep->pps;
  1204. ep->packsize[0] = ep->cur_rate / ep->pps;
  1205. ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
  1206. /* calculate the frequency in 16.16 format */
  1207. ep->freqm = ep->freqn;
  1208. ep->freqshift = INT_MIN;
  1209. ep->phase = 0;
  1210. switch (ep->type) {
  1211. case SND_USB_ENDPOINT_TYPE_DATA:
  1212. err = data_ep_set_params(ep);
  1213. break;
  1214. case SND_USB_ENDPOINT_TYPE_SYNC:
  1215. err = sync_ep_set_params(ep);
  1216. break;
  1217. default:
  1218. err = -EINVAL;
  1219. }
  1220. usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
  1221. if (err < 0)
  1222. goto unlock;
  1223. /* some unit conversions in runtime */
  1224. ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
  1225. ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
  1226. err = update_clock_ref_rate(chip, ep);
  1227. if (err >= 0) {
  1228. ep->need_setup = false;
  1229. err = 0;
  1230. }
  1231. unlock:
  1232. mutex_unlock(&chip->mutex);
  1233. return err;
  1234. }
  1235. static int init_sample_rate(struct snd_usb_audio *chip,
  1236. struct snd_usb_endpoint *ep)
  1237. {
  1238. struct snd_usb_clock_ref *clock = ep->clock_ref;
  1239. int rate, err;
  1240. rate = update_clock_ref_rate(chip, ep);
  1241. if (rate < 0)
  1242. return rate;
  1243. if (clock && !clock->need_setup)
  1244. return 0;
  1245. if (!ep->fixed_rate) {
  1246. err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, rate);
  1247. if (err < 0) {
  1248. if (clock)
  1249. clock->rate = 0; /* reset rate */
  1250. return err;
  1251. }
  1252. }
  1253. if (clock)
  1254. clock->need_setup = false;
  1255. return 0;
  1256. }
  1257. /*
  1258. * snd_usb_endpoint_prepare: Prepare the endpoint
  1259. *
  1260. * This function sets up the EP to be fully usable state.
  1261. * It's called either from prepare callback.
  1262. * The function checks need_setup flag, and performs nothing unless needed,
  1263. * so it's safe to call this multiple times.
  1264. *
  1265. * This returns zero if unchanged, 1 if the configuration has changed,
  1266. * or a negative error code.
  1267. */
  1268. int snd_usb_endpoint_prepare(struct snd_usb_audio *chip,
  1269. struct snd_usb_endpoint *ep)
  1270. {
  1271. bool iface_first;
  1272. int err = 0;
  1273. mutex_lock(&chip->mutex);
  1274. if (WARN_ON(!ep->iface_ref))
  1275. goto unlock;
  1276. if (!ep->need_prepare)
  1277. goto unlock;
  1278. /* If the interface has been already set up, just set EP parameters */
  1279. if (!ep->iface_ref->need_setup) {
  1280. /* sample rate setup of UAC1 is per endpoint, and we need
  1281. * to update at each EP configuration
  1282. */
  1283. if (ep->cur_audiofmt->protocol == UAC_VERSION_1) {
  1284. err = init_sample_rate(chip, ep);
  1285. if (err < 0)
  1286. goto unlock;
  1287. }
  1288. goto done;
  1289. }
  1290. /* Need to deselect altsetting at first */
  1291. endpoint_set_interface(chip, ep, false);
  1292. /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
  1293. * to be set up before parameter setups
  1294. */
  1295. iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
  1296. /* Workaround for devices that require the interface setup at first like UAC1 */
  1297. if (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST)
  1298. iface_first = true;
  1299. if (iface_first) {
  1300. err = endpoint_set_interface(chip, ep, true);
  1301. if (err < 0)
  1302. goto unlock;
  1303. }
  1304. err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
  1305. if (err < 0)
  1306. goto unlock;
  1307. err = init_sample_rate(chip, ep);
  1308. if (err < 0)
  1309. goto unlock;
  1310. err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
  1311. if (err < 0)
  1312. goto unlock;
  1313. /* for UAC2/3, enable the interface altset here at last */
  1314. if (!iface_first) {
  1315. err = endpoint_set_interface(chip, ep, true);
  1316. if (err < 0)
  1317. goto unlock;
  1318. }
  1319. ep->iface_ref->need_setup = false;
  1320. done:
  1321. ep->need_prepare = false;
  1322. err = 1;
  1323. unlock:
  1324. mutex_unlock(&chip->mutex);
  1325. return err;
  1326. }
  1327. /* get the current rate set to the given clock by any endpoint */
  1328. int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock)
  1329. {
  1330. struct snd_usb_clock_ref *ref;
  1331. int rate = 0;
  1332. if (!clock)
  1333. return 0;
  1334. mutex_lock(&chip->mutex);
  1335. list_for_each_entry(ref, &chip->clock_ref_list, list) {
  1336. if (ref->clock == clock) {
  1337. rate = ref->rate;
  1338. break;
  1339. }
  1340. }
  1341. mutex_unlock(&chip->mutex);
  1342. return rate;
  1343. }
  1344. /**
  1345. * snd_usb_endpoint_start: start an snd_usb_endpoint
  1346. *
  1347. * @ep: the endpoint to start
  1348. *
  1349. * A call to this function will increment the running count of the endpoint.
  1350. * In case it is not already running, the URBs for this endpoint will be
  1351. * submitted. Otherwise, this function does nothing.
  1352. *
  1353. * Must be balanced to calls of snd_usb_endpoint_stop().
  1354. *
  1355. * Returns an error if the URB submission failed, 0 in all other cases.
  1356. */
  1357. int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
  1358. {
  1359. bool is_playback = usb_pipeout(ep->pipe);
  1360. int err;
  1361. unsigned int i;
  1362. if (atomic_read(&ep->chip->shutdown))
  1363. return -EBADFD;
  1364. if (ep->sync_source)
  1365. WRITE_ONCE(ep->sync_source->sync_sink, ep);
  1366. usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
  1367. ep_type_name(ep->type), ep->ep_num,
  1368. atomic_read(&ep->running));
  1369. /* already running? */
  1370. if (atomic_inc_return(&ep->running) != 1)
  1371. return 0;
  1372. if (ep->clock_ref)
  1373. atomic_inc(&ep->clock_ref->locked);
  1374. ep->active_mask = 0;
  1375. ep->unlink_mask = 0;
  1376. ep->phase = 0;
  1377. ep->sample_accum = 0;
  1378. snd_usb_endpoint_start_quirk(ep);
  1379. /*
  1380. * If this endpoint has a data endpoint as implicit feedback source,
  1381. * don't start the urbs here. Instead, mark them all as available,
  1382. * wait for the record urbs to return and queue the playback urbs
  1383. * from that context.
  1384. */
  1385. if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING))
  1386. goto __error;
  1387. if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
  1388. !(ep->chip->quirk_flags & QUIRK_FLAG_PLAYBACK_FIRST)) {
  1389. usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
  1390. i = 0;
  1391. goto fill_rest;
  1392. }
  1393. for (i = 0; i < ep->nurbs; i++) {
  1394. struct urb *urb = ep->urb[i].urb;
  1395. if (snd_BUG_ON(!urb))
  1396. goto __error;
  1397. if (is_playback)
  1398. err = prepare_outbound_urb(ep, urb->context, true);
  1399. else
  1400. err = prepare_inbound_urb(ep, urb->context);
  1401. if (err < 0) {
  1402. /* stop filling at applptr */
  1403. if (err == -EAGAIN)
  1404. break;
  1405. usb_audio_dbg(ep->chip,
  1406. "EP 0x%x: failed to prepare urb: %d\n",
  1407. ep->ep_num, err);
  1408. goto __error;
  1409. }
  1410. if (!atomic_read(&ep->chip->shutdown))
  1411. err = usb_submit_urb(urb, GFP_ATOMIC);
  1412. else
  1413. err = -ENODEV;
  1414. if (err < 0) {
  1415. if (!atomic_read(&ep->chip->shutdown))
  1416. usb_audio_err(ep->chip,
  1417. "cannot submit urb %d, error %d: %s\n",
  1418. i, err, usb_error_string(err));
  1419. goto __error;
  1420. }
  1421. set_bit(i, &ep->active_mask);
  1422. atomic_inc(&ep->submitted_urbs);
  1423. }
  1424. if (!i) {
  1425. usb_audio_dbg(ep->chip, "XRUN at starting EP 0x%x\n",
  1426. ep->ep_num);
  1427. goto __error;
  1428. }
  1429. usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
  1430. i, ep->ep_num);
  1431. fill_rest:
  1432. /* put the remaining URBs to ready list */
  1433. if (is_playback) {
  1434. for (; i < ep->nurbs; i++)
  1435. push_back_to_ready_list(ep, ep->urb + i);
  1436. }
  1437. return 0;
  1438. __error:
  1439. snd_usb_endpoint_stop(ep, false);
  1440. return -EPIPE;
  1441. }
  1442. /**
  1443. * snd_usb_endpoint_stop: stop an snd_usb_endpoint
  1444. *
  1445. * @ep: the endpoint to stop (may be NULL)
  1446. * @keep_pending: keep in-flight URBs
  1447. *
  1448. * A call to this function will decrement the running count of the endpoint.
  1449. * In case the last user has requested the endpoint stop, the URBs will
  1450. * actually be deactivated.
  1451. *
  1452. * Must be balanced to calls of snd_usb_endpoint_start().
  1453. *
  1454. * The caller needs to synchronize the pending stop operation via
  1455. * snd_usb_endpoint_sync_pending_stop().
  1456. */
  1457. void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool keep_pending)
  1458. {
  1459. if (!ep)
  1460. return;
  1461. usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
  1462. ep_type_name(ep->type), ep->ep_num,
  1463. atomic_read(&ep->running));
  1464. if (snd_BUG_ON(!atomic_read(&ep->running)))
  1465. return;
  1466. if (!atomic_dec_return(&ep->running)) {
  1467. if (ep->sync_source)
  1468. WRITE_ONCE(ep->sync_source->sync_sink, NULL);
  1469. stop_urbs(ep, false, keep_pending);
  1470. if (ep->clock_ref)
  1471. atomic_dec(&ep->clock_ref->locked);
  1472. if (ep->chip->quirk_flags & QUIRK_FLAG_FORCE_IFACE_RESET &&
  1473. usb_pipeout(ep->pipe)) {
  1474. ep->need_prepare = true;
  1475. if (ep->iface_ref)
  1476. ep->iface_ref->need_setup = true;
  1477. }
  1478. }
  1479. }
  1480. /**
  1481. * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
  1482. *
  1483. * @ep: the endpoint to release
  1484. *
  1485. * This function does not care for the endpoint's running count but will tear
  1486. * down all the streaming URBs immediately.
  1487. */
  1488. void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
  1489. {
  1490. release_urbs(ep, true);
  1491. }
  1492. /**
  1493. * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint
  1494. * @chip: The chip
  1495. *
  1496. * This free all endpoints and those resources
  1497. */
  1498. void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
  1499. {
  1500. struct snd_usb_endpoint *ep, *en;
  1501. struct snd_usb_iface_ref *ip, *in;
  1502. struct snd_usb_clock_ref *cp, *cn;
  1503. list_for_each_entry_safe(ep, en, &chip->ep_list, list)
  1504. kfree(ep);
  1505. list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
  1506. kfree(ip);
  1507. list_for_each_entry_safe(cp, cn, &chip->clock_ref_list, list)
  1508. kfree(cp);
  1509. }
  1510. /*
  1511. * snd_usb_handle_sync_urb: parse an USB sync packet
  1512. *
  1513. * @ep: the endpoint to handle the packet
  1514. * @sender: the sending endpoint
  1515. * @urb: the received packet
  1516. *
  1517. * This function is called from the context of an endpoint that received
  1518. * the packet and is used to let another endpoint object handle the payload.
  1519. */
  1520. static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  1521. struct snd_usb_endpoint *sender,
  1522. const struct urb *urb)
  1523. {
  1524. int shift;
  1525. unsigned int f;
  1526. unsigned long flags;
  1527. snd_BUG_ON(ep == sender);
  1528. /*
  1529. * In case the endpoint is operating in implicit feedback mode, prepare
  1530. * a new outbound URB that has the same layout as the received packet
  1531. * and add it to the list of pending urbs. queue_pending_output_urbs()
  1532. * will take care of them later.
  1533. */
  1534. if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
  1535. atomic_read(&ep->running)) {
  1536. /* implicit feedback case */
  1537. int i, bytes = 0;
  1538. struct snd_urb_ctx *in_ctx;
  1539. struct snd_usb_packet_info *out_packet;
  1540. in_ctx = urb->context;
  1541. /* Count overall packet size */
  1542. for (i = 0; i < in_ctx->packets; i++)
  1543. if (urb->iso_frame_desc[i].status == 0)
  1544. bytes += urb->iso_frame_desc[i].actual_length;
  1545. /*
  1546. * skip empty packets. At least M-Audio's Fast Track Ultra stops
  1547. * streaming once it received a 0-byte OUT URB
  1548. */
  1549. if (bytes == 0)
  1550. return;
  1551. spin_lock_irqsave(&ep->lock, flags);
  1552. if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
  1553. spin_unlock_irqrestore(&ep->lock, flags);
  1554. usb_audio_err(ep->chip,
  1555. "next package FIFO overflow EP 0x%x\n",
  1556. ep->ep_num);
  1557. notify_xrun(ep);
  1558. return;
  1559. }
  1560. out_packet = next_packet_fifo_enqueue(ep);
  1561. /*
  1562. * Iterate through the inbound packet and prepare the lengths
  1563. * for the output packet. The OUT packet we are about to send
  1564. * will have the same amount of payload bytes per stride as the
  1565. * IN packet we just received. Since the actual size is scaled
  1566. * by the stride, use the sender stride to calculate the length
  1567. * in case the number of channels differ between the implicitly
  1568. * fed-back endpoint and the synchronizing endpoint.
  1569. */
  1570. out_packet->packets = in_ctx->packets;
  1571. for (i = 0; i < in_ctx->packets; i++) {
  1572. if (urb->iso_frame_desc[i].status == 0)
  1573. out_packet->packet_size[i] =
  1574. urb->iso_frame_desc[i].actual_length / sender->stride;
  1575. else
  1576. out_packet->packet_size[i] = 0;
  1577. }
  1578. spin_unlock_irqrestore(&ep->lock, flags);
  1579. snd_usb_queue_pending_output_urbs(ep, false);
  1580. return;
  1581. }
  1582. /*
  1583. * process after playback sync complete
  1584. *
  1585. * Full speed devices report feedback values in 10.14 format as samples
  1586. * per frame, high speed devices in 16.16 format as samples per
  1587. * microframe.
  1588. *
  1589. * Because the Audio Class 1 spec was written before USB 2.0, many high
  1590. * speed devices use a wrong interpretation, some others use an
  1591. * entirely different format.
  1592. *
  1593. * Therefore, we cannot predict what format any particular device uses
  1594. * and must detect it automatically.
  1595. */
  1596. if (urb->iso_frame_desc[0].status != 0 ||
  1597. urb->iso_frame_desc[0].actual_length < 3)
  1598. return;
  1599. f = le32_to_cpup(urb->transfer_buffer);
  1600. if (urb->iso_frame_desc[0].actual_length == 3)
  1601. f &= 0x00ffffff;
  1602. else
  1603. f &= 0x0fffffff;
  1604. if (f == 0)
  1605. return;
  1606. if (unlikely(sender->tenor_fb_quirk)) {
  1607. /*
  1608. * Devices based on Tenor 8802 chipsets (TEAC UD-H01
  1609. * and others) sometimes change the feedback value
  1610. * by +/- 0x1.0000.
  1611. */
  1612. if (f < ep->freqn - 0x8000)
  1613. f += 0xf000;
  1614. else if (f > ep->freqn + 0x8000)
  1615. f -= 0xf000;
  1616. } else if (unlikely(ep->freqshift == INT_MIN)) {
  1617. /*
  1618. * The first time we see a feedback value, determine its format
  1619. * by shifting it left or right until it matches the nominal
  1620. * frequency value. This assumes that the feedback does not
  1621. * differ from the nominal value more than +50% or -25%.
  1622. */
  1623. shift = 0;
  1624. while (f < ep->freqn - ep->freqn / 4) {
  1625. f <<= 1;
  1626. shift++;
  1627. }
  1628. while (f > ep->freqn + ep->freqn / 2) {
  1629. f >>= 1;
  1630. shift--;
  1631. }
  1632. ep->freqshift = shift;
  1633. } else if (ep->freqshift >= 0)
  1634. f <<= ep->freqshift;
  1635. else
  1636. f >>= -ep->freqshift;
  1637. if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
  1638. /*
  1639. * If the frequency looks valid, set it.
  1640. * This value is referred to in prepare_playback_urb().
  1641. */
  1642. spin_lock_irqsave(&ep->lock, flags);
  1643. ep->freqm = f;
  1644. spin_unlock_irqrestore(&ep->lock, flags);
  1645. } else {
  1646. /*
  1647. * Out of range; maybe the shift value is wrong.
  1648. * Reset it so that we autodetect again the next time.
  1649. */
  1650. ep->freqshift = INT_MIN;
  1651. }
  1652. }