endpoint.c 49 KB

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