endpoint.c 49 KB

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