endpoint.c 49 KB

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