endpoint.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. */
  17. #include <linux/gfp.h>
  18. #include <linux/init.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/audio.h>
  22. #include <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include "usbaudio.h"
  27. #include "helper.h"
  28. #include "card.h"
  29. #include "endpoint.h"
  30. #include "pcm.h"
  31. #define EP_FLAG_ACTIVATED 0
  32. #define EP_FLAG_RUNNING 1
  33. /*
  34. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  35. * this will overflow at approx 524 kHz
  36. */
  37. static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  38. {
  39. return ((rate << 13) + 62) / 125;
  40. }
  41. /*
  42. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  43. * this will overflow at approx 4 MHz
  44. */
  45. static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  46. {
  47. return ((rate << 10) + 62) / 125;
  48. }
  49. /*
  50. * unlink active urbs.
  51. */
  52. static int deactivate_urbs_old(struct snd_usb_substream *subs, int force, int can_sleep)
  53. {
  54. struct snd_usb_audio *chip = subs->stream->chip;
  55. unsigned int i;
  56. int async;
  57. subs->running = 0;
  58. if (!force && subs->stream->chip->shutdown) /* to be sure... */
  59. return -EBADFD;
  60. async = !can_sleep && chip->async_unlink;
  61. if (!async && in_interrupt())
  62. return 0;
  63. for (i = 0; i < subs->nurbs; i++) {
  64. if (test_bit(i, &subs->active_mask)) {
  65. if (!test_and_set_bit(i, &subs->unlink_mask)) {
  66. struct urb *u = subs->dataurb[i].urb;
  67. if (async)
  68. usb_unlink_urb(u);
  69. else
  70. usb_kill_urb(u);
  71. }
  72. }
  73. }
  74. if (subs->syncpipe) {
  75. for (i = 0; i < SYNC_URBS; i++) {
  76. if (test_bit(i+16, &subs->active_mask)) {
  77. if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
  78. struct urb *u = subs->syncurb[i].urb;
  79. if (async)
  80. usb_unlink_urb(u);
  81. else
  82. usb_kill_urb(u);
  83. }
  84. }
  85. }
  86. }
  87. return 0;
  88. }
  89. /*
  90. * release a urb data
  91. */
  92. static void release_urb_ctx(struct snd_urb_ctx *u)
  93. {
  94. if (u->urb) {
  95. if (u->buffer_size)
  96. usb_free_coherent(u->subs->dev, u->buffer_size,
  97. u->urb->transfer_buffer,
  98. u->urb->transfer_dma);
  99. usb_free_urb(u->urb);
  100. u->urb = NULL;
  101. }
  102. }
  103. /*
  104. * wait until all urbs are processed.
  105. */
  106. static int wait_clear_urbs_old(struct snd_usb_substream *subs)
  107. {
  108. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  109. unsigned int i;
  110. int alive;
  111. do {
  112. alive = 0;
  113. for (i = 0; i < subs->nurbs; i++) {
  114. if (test_bit(i, &subs->active_mask))
  115. alive++;
  116. }
  117. if (subs->syncpipe) {
  118. for (i = 0; i < SYNC_URBS; i++) {
  119. if (test_bit(i + 16, &subs->active_mask))
  120. alive++;
  121. }
  122. }
  123. if (! alive)
  124. break;
  125. schedule_timeout_uninterruptible(1);
  126. } while (time_before(jiffies, end_time));
  127. if (alive)
  128. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  129. return 0;
  130. }
  131. /*
  132. * release a substream
  133. */
  134. void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
  135. {
  136. int i;
  137. /* stop urbs (to be sure) */
  138. deactivate_urbs_old(subs, force, 1);
  139. wait_clear_urbs_old(subs);
  140. for (i = 0; i < MAX_URBS; i++)
  141. release_urb_ctx(&subs->dataurb[i]);
  142. for (i = 0; i < SYNC_URBS; i++)
  143. release_urb_ctx(&subs->syncurb[i]);
  144. usb_free_coherent(subs->dev, SYNC_URBS * 4,
  145. subs->syncbuf, subs->sync_dma);
  146. subs->syncbuf = NULL;
  147. subs->nurbs = 0;
  148. }
  149. /*
  150. * complete callback from data urb
  151. */
  152. static void snd_complete_urb_old(struct urb *urb)
  153. {
  154. struct snd_urb_ctx *ctx = urb->context;
  155. struct snd_usb_substream *subs = ctx->subs;
  156. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  157. int err = 0;
  158. if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
  159. !subs->running || /* can be stopped during retire callback */
  160. (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
  161. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  162. clear_bit(ctx->index, &subs->active_mask);
  163. if (err < 0) {
  164. snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
  165. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  166. }
  167. }
  168. }
  169. /*
  170. * complete callback from sync urb
  171. */
  172. static void snd_complete_sync_urb(struct urb *urb)
  173. {
  174. struct snd_urb_ctx *ctx = urb->context;
  175. struct snd_usb_substream *subs = ctx->subs;
  176. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  177. int err = 0;
  178. if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
  179. !subs->running || /* can be stopped during retire callback */
  180. (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
  181. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  182. clear_bit(ctx->index + 16, &subs->active_mask);
  183. if (err < 0) {
  184. snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
  185. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  186. }
  187. }
  188. }
  189. /*
  190. * initialize a substream for plaback/capture
  191. */
  192. int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
  193. unsigned int period_bytes,
  194. unsigned int rate,
  195. unsigned int frame_bits)
  196. {
  197. unsigned int maxsize, i;
  198. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  199. unsigned int urb_packs, total_packs, packs_per_ms;
  200. struct snd_usb_audio *chip = subs->stream->chip;
  201. /* calculate the frequency in 16.16 format */
  202. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  203. subs->freqn = get_usb_full_speed_rate(rate);
  204. else
  205. subs->freqn = get_usb_high_speed_rate(rate);
  206. subs->freqm = subs->freqn;
  207. subs->freqshift = INT_MIN;
  208. /* calculate max. frequency */
  209. if (subs->maxpacksize) {
  210. /* whatever fits into a max. size packet */
  211. maxsize = subs->maxpacksize;
  212. subs->freqmax = (maxsize / (frame_bits >> 3))
  213. << (16 - subs->datainterval);
  214. } else {
  215. /* no max. packet size: just take 25% higher than nominal */
  216. subs->freqmax = subs->freqn + (subs->freqn >> 2);
  217. maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
  218. >> (16 - subs->datainterval);
  219. }
  220. subs->phase = 0;
  221. if (subs->fill_max)
  222. subs->curpacksize = subs->maxpacksize;
  223. else
  224. subs->curpacksize = maxsize;
  225. if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
  226. packs_per_ms = 8 >> subs->datainterval;
  227. else
  228. packs_per_ms = 1;
  229. if (is_playback) {
  230. urb_packs = max(chip->nrpacks, 1);
  231. urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
  232. } else
  233. urb_packs = 1;
  234. urb_packs *= packs_per_ms;
  235. if (subs->syncpipe)
  236. urb_packs = min(urb_packs, 1U << subs->syncinterval);
  237. /* decide how many packets to be used */
  238. if (is_playback) {
  239. unsigned int minsize, maxpacks;
  240. /* determine how small a packet can be */
  241. minsize = (subs->freqn >> (16 - subs->datainterval))
  242. * (frame_bits >> 3);
  243. /* with sync from device, assume it can be 12% lower */
  244. if (subs->syncpipe)
  245. minsize -= minsize >> 3;
  246. minsize = max(minsize, 1u);
  247. total_packs = (period_bytes + minsize - 1) / minsize;
  248. /* we need at least two URBs for queueing */
  249. if (total_packs < 2) {
  250. total_packs = 2;
  251. } else {
  252. /* and we don't want too long a queue either */
  253. maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  254. total_packs = min(total_packs, maxpacks);
  255. }
  256. } else {
  257. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  258. urb_packs >>= 1;
  259. total_packs = MAX_URBS * urb_packs;
  260. }
  261. subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  262. if (subs->nurbs > MAX_URBS) {
  263. /* too much... */
  264. subs->nurbs = MAX_URBS;
  265. total_packs = MAX_URBS * urb_packs;
  266. } else if (subs->nurbs < 2) {
  267. /* too little - we need at least two packets
  268. * to ensure contiguous playback/capture
  269. */
  270. subs->nurbs = 2;
  271. }
  272. /* allocate and initialize data urbs */
  273. for (i = 0; i < subs->nurbs; i++) {
  274. struct snd_urb_ctx *u = &subs->dataurb[i];
  275. u->index = i;
  276. u->subs = subs;
  277. u->packets = (i + 1) * total_packs / subs->nurbs
  278. - i * total_packs / subs->nurbs;
  279. u->buffer_size = maxsize * u->packets;
  280. if (subs->fmt_type == UAC_FORMAT_TYPE_II)
  281. u->packets++; /* for transfer delimiter */
  282. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  283. if (!u->urb)
  284. goto out_of_memory;
  285. u->urb->transfer_buffer =
  286. usb_alloc_coherent(subs->dev, u->buffer_size,
  287. GFP_KERNEL, &u->urb->transfer_dma);
  288. if (!u->urb->transfer_buffer)
  289. goto out_of_memory;
  290. u->urb->pipe = subs->datapipe;
  291. u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  292. u->urb->interval = 1 << subs->datainterval;
  293. u->urb->context = u;
  294. u->urb->complete = snd_complete_urb_old;
  295. }
  296. if (subs->syncpipe) {
  297. /* allocate and initialize sync urbs */
  298. subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
  299. GFP_KERNEL, &subs->sync_dma);
  300. if (!subs->syncbuf)
  301. goto out_of_memory;
  302. for (i = 0; i < SYNC_URBS; i++) {
  303. struct snd_urb_ctx *u = &subs->syncurb[i];
  304. u->index = i;
  305. u->subs = subs;
  306. u->packets = 1;
  307. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  308. if (!u->urb)
  309. goto out_of_memory;
  310. u->urb->transfer_buffer = subs->syncbuf + i * 4;
  311. u->urb->transfer_dma = subs->sync_dma + i * 4;
  312. u->urb->transfer_buffer_length = 4;
  313. u->urb->pipe = subs->syncpipe;
  314. u->urb->transfer_flags = URB_ISO_ASAP |
  315. URB_NO_TRANSFER_DMA_MAP;
  316. u->urb->number_of_packets = 1;
  317. u->urb->interval = 1 << subs->syncinterval;
  318. u->urb->context = u;
  319. u->urb->complete = snd_complete_sync_urb;
  320. }
  321. }
  322. return 0;
  323. out_of_memory:
  324. snd_usb_release_substream_urbs(subs, 0);
  325. return -ENOMEM;
  326. }
  327. /*
  328. * prepare urb for full speed capture sync pipe
  329. *
  330. * fill the length and offset of each urb descriptor.
  331. * the fixed 10.14 frequency is passed through the pipe.
  332. */
  333. static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
  334. struct snd_pcm_runtime *runtime,
  335. struct urb *urb)
  336. {
  337. unsigned char *cp = urb->transfer_buffer;
  338. struct snd_urb_ctx *ctx = urb->context;
  339. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  340. urb->iso_frame_desc[0].length = 3;
  341. urb->iso_frame_desc[0].offset = 0;
  342. cp[0] = subs->freqn >> 2;
  343. cp[1] = subs->freqn >> 10;
  344. cp[2] = subs->freqn >> 18;
  345. return 0;
  346. }
  347. /*
  348. * prepare urb for high speed capture sync pipe
  349. *
  350. * fill the length and offset of each urb descriptor.
  351. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  352. */
  353. static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
  354. struct snd_pcm_runtime *runtime,
  355. struct urb *urb)
  356. {
  357. unsigned char *cp = urb->transfer_buffer;
  358. struct snd_urb_ctx *ctx = urb->context;
  359. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  360. urb->iso_frame_desc[0].length = 4;
  361. urb->iso_frame_desc[0].offset = 0;
  362. cp[0] = subs->freqn;
  363. cp[1] = subs->freqn >> 8;
  364. cp[2] = subs->freqn >> 16;
  365. cp[3] = subs->freqn >> 24;
  366. return 0;
  367. }
  368. /*
  369. * process after capture sync complete
  370. * - nothing to do
  371. */
  372. static int retire_capture_sync_urb(struct snd_usb_substream *subs,
  373. struct snd_pcm_runtime *runtime,
  374. struct urb *urb)
  375. {
  376. return 0;
  377. }
  378. /*
  379. * prepare urb for capture data pipe
  380. *
  381. * fill the offset and length of each descriptor.
  382. *
  383. * we use a temporary buffer to write the captured data.
  384. * since the length of written data is determined by host, we cannot
  385. * write onto the pcm buffer directly... the data is thus copied
  386. * later at complete callback to the global buffer.
  387. */
  388. static int prepare_capture_urb(struct snd_usb_substream *subs,
  389. struct snd_pcm_runtime *runtime,
  390. struct urb *urb)
  391. {
  392. int i, offs;
  393. struct snd_urb_ctx *ctx = urb->context;
  394. offs = 0;
  395. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  396. for (i = 0; i < ctx->packets; i++) {
  397. urb->iso_frame_desc[i].offset = offs;
  398. urb->iso_frame_desc[i].length = subs->curpacksize;
  399. offs += subs->curpacksize;
  400. }
  401. urb->transfer_buffer_length = offs;
  402. urb->number_of_packets = ctx->packets;
  403. return 0;
  404. }
  405. /*
  406. * process after capture complete
  407. *
  408. * copy the data from each desctiptor to the pcm buffer, and
  409. * update the current position.
  410. */
  411. static int retire_capture_urb(struct snd_usb_substream *subs,
  412. struct snd_pcm_runtime *runtime,
  413. struct urb *urb)
  414. {
  415. unsigned long flags;
  416. unsigned char *cp;
  417. int i;
  418. unsigned int stride, frames, bytes, oldptr;
  419. int period_elapsed = 0;
  420. stride = runtime->frame_bits >> 3;
  421. for (i = 0; i < urb->number_of_packets; i++) {
  422. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  423. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  424. snd_printdd("frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  425. // continue;
  426. }
  427. bytes = urb->iso_frame_desc[i].actual_length;
  428. frames = bytes / stride;
  429. if (!subs->txfr_quirk)
  430. bytes = frames * stride;
  431. if (bytes % (runtime->sample_bits >> 3) != 0) {
  432. #ifdef CONFIG_SND_DEBUG_VERBOSE
  433. int oldbytes = bytes;
  434. #endif
  435. bytes = frames * stride;
  436. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  437. oldbytes, bytes);
  438. }
  439. /* update the current pointer */
  440. spin_lock_irqsave(&subs->lock, flags);
  441. oldptr = subs->hwptr_done;
  442. subs->hwptr_done += bytes;
  443. if (subs->hwptr_done >= runtime->buffer_size * stride)
  444. subs->hwptr_done -= runtime->buffer_size * stride;
  445. frames = (bytes + (oldptr % stride)) / stride;
  446. subs->transfer_done += frames;
  447. if (subs->transfer_done >= runtime->period_size) {
  448. subs->transfer_done -= runtime->period_size;
  449. period_elapsed = 1;
  450. }
  451. spin_unlock_irqrestore(&subs->lock, flags);
  452. /* copy a data chunk */
  453. if (oldptr + bytes > runtime->buffer_size * stride) {
  454. unsigned int bytes1 =
  455. runtime->buffer_size * stride - oldptr;
  456. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  457. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  458. } else {
  459. memcpy(runtime->dma_area + oldptr, cp, bytes);
  460. }
  461. }
  462. if (period_elapsed)
  463. snd_pcm_period_elapsed(subs->pcm_substream);
  464. return 0;
  465. }
  466. /*
  467. * Process after capture complete when paused. Nothing to do.
  468. */
  469. static int retire_paused_capture_urb(struct snd_usb_substream *subs,
  470. struct snd_pcm_runtime *runtime,
  471. struct urb *urb)
  472. {
  473. return 0;
  474. }
  475. /*
  476. * prepare urb for playback sync pipe
  477. *
  478. * set up the offset and length to receive the current frequency.
  479. */
  480. static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
  481. struct snd_pcm_runtime *runtime,
  482. struct urb *urb)
  483. {
  484. struct snd_urb_ctx *ctx = urb->context;
  485. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  486. urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);
  487. urb->iso_frame_desc[0].offset = 0;
  488. return 0;
  489. }
  490. /*
  491. * process after playback sync complete
  492. *
  493. * Full speed devices report feedback values in 10.14 format as samples per
  494. * frame, high speed devices in 16.16 format as samples per microframe.
  495. * Because the Audio Class 1 spec was written before USB 2.0, many high speed
  496. * devices use a wrong interpretation, some others use an entirely different
  497. * format. Therefore, we cannot predict what format any particular device uses
  498. * and must detect it automatically.
  499. */
  500. static int retire_playback_sync_urb(struct snd_usb_substream *subs,
  501. struct snd_pcm_runtime *runtime,
  502. struct urb *urb)
  503. {
  504. unsigned int f;
  505. int shift;
  506. unsigned long flags;
  507. if (urb->iso_frame_desc[0].status != 0 ||
  508. urb->iso_frame_desc[0].actual_length < 3)
  509. return 0;
  510. f = le32_to_cpup(urb->transfer_buffer);
  511. if (urb->iso_frame_desc[0].actual_length == 3)
  512. f &= 0x00ffffff;
  513. else
  514. f &= 0x0fffffff;
  515. if (f == 0)
  516. return 0;
  517. if (unlikely(subs->freqshift == INT_MIN)) {
  518. /*
  519. * The first time we see a feedback value, determine its format
  520. * by shifting it left or right until it matches the nominal
  521. * frequency value. This assumes that the feedback does not
  522. * differ from the nominal value more than +50% or -25%.
  523. */
  524. shift = 0;
  525. while (f < subs->freqn - subs->freqn / 4) {
  526. f <<= 1;
  527. shift++;
  528. }
  529. while (f > subs->freqn + subs->freqn / 2) {
  530. f >>= 1;
  531. shift--;
  532. }
  533. subs->freqshift = shift;
  534. }
  535. else if (subs->freqshift >= 0)
  536. f <<= subs->freqshift;
  537. else
  538. f >>= -subs->freqshift;
  539. if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {
  540. /*
  541. * If the frequency looks valid, set it.
  542. * This value is referred to in prepare_playback_urb().
  543. */
  544. spin_lock_irqsave(&subs->lock, flags);
  545. subs->freqm = f;
  546. spin_unlock_irqrestore(&subs->lock, flags);
  547. } else {
  548. /*
  549. * Out of range; maybe the shift value is wrong.
  550. * Reset it so that we autodetect again the next time.
  551. */
  552. subs->freqshift = INT_MIN;
  553. }
  554. return 0;
  555. }
  556. /* determine the number of frames in the next packet */
  557. static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
  558. {
  559. if (subs->fill_max)
  560. return subs->maxframesize;
  561. else {
  562. subs->phase = (subs->phase & 0xffff)
  563. + (subs->freqm << subs->datainterval);
  564. return min(subs->phase >> 16, subs->maxframesize);
  565. }
  566. }
  567. /*
  568. * Prepare urb for streaming before playback starts or when paused.
  569. *
  570. * We don't have any data, so we send silence.
  571. */
  572. static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
  573. struct snd_pcm_runtime *runtime,
  574. struct urb *urb)
  575. {
  576. unsigned int i, offs, counts;
  577. struct snd_urb_ctx *ctx = urb->context;
  578. int stride = runtime->frame_bits >> 3;
  579. offs = 0;
  580. urb->dev = ctx->subs->dev;
  581. for (i = 0; i < ctx->packets; ++i) {
  582. counts = snd_usb_audio_next_packet_size(subs);
  583. urb->iso_frame_desc[i].offset = offs * stride;
  584. urb->iso_frame_desc[i].length = counts * stride;
  585. offs += counts;
  586. }
  587. urb->number_of_packets = ctx->packets;
  588. urb->transfer_buffer_length = offs * stride;
  589. memset(urb->transfer_buffer,
  590. runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
  591. offs * stride);
  592. return 0;
  593. }
  594. /*
  595. * prepare urb for playback data pipe
  596. *
  597. * Since a URB can handle only a single linear buffer, we must use double
  598. * buffering when the data to be transferred overflows the buffer boundary.
  599. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  600. * for all URBs.
  601. */
  602. static int prepare_playback_urb(struct snd_usb_substream *subs,
  603. struct snd_pcm_runtime *runtime,
  604. struct urb *urb)
  605. {
  606. int i, stride;
  607. unsigned int counts, frames, bytes;
  608. unsigned long flags;
  609. int period_elapsed = 0;
  610. struct snd_urb_ctx *ctx = urb->context;
  611. stride = runtime->frame_bits >> 3;
  612. frames = 0;
  613. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  614. urb->number_of_packets = 0;
  615. spin_lock_irqsave(&subs->lock, flags);
  616. for (i = 0; i < ctx->packets; i++) {
  617. counts = snd_usb_audio_next_packet_size(subs);
  618. /* set up descriptor */
  619. urb->iso_frame_desc[i].offset = frames * stride;
  620. urb->iso_frame_desc[i].length = counts * stride;
  621. frames += counts;
  622. urb->number_of_packets++;
  623. subs->transfer_done += counts;
  624. if (subs->transfer_done >= runtime->period_size) {
  625. subs->transfer_done -= runtime->period_size;
  626. period_elapsed = 1;
  627. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  628. if (subs->transfer_done > 0) {
  629. /* FIXME: fill-max mode is not
  630. * supported yet */
  631. frames -= subs->transfer_done;
  632. counts -= subs->transfer_done;
  633. urb->iso_frame_desc[i].length =
  634. counts * stride;
  635. subs->transfer_done = 0;
  636. }
  637. i++;
  638. if (i < ctx->packets) {
  639. /* add a transfer delimiter */
  640. urb->iso_frame_desc[i].offset =
  641. frames * stride;
  642. urb->iso_frame_desc[i].length = 0;
  643. urb->number_of_packets++;
  644. }
  645. break;
  646. }
  647. }
  648. if (period_elapsed) /* finish at the period boundary */
  649. break;
  650. }
  651. bytes = frames * stride;
  652. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  653. /* err, the transferred area goes over buffer boundary. */
  654. unsigned int bytes1 =
  655. runtime->buffer_size * stride - subs->hwptr_done;
  656. memcpy(urb->transfer_buffer,
  657. runtime->dma_area + subs->hwptr_done, bytes1);
  658. memcpy(urb->transfer_buffer + bytes1,
  659. runtime->dma_area, bytes - bytes1);
  660. } else {
  661. memcpy(urb->transfer_buffer,
  662. runtime->dma_area + subs->hwptr_done, bytes);
  663. }
  664. subs->hwptr_done += bytes;
  665. if (subs->hwptr_done >= runtime->buffer_size * stride)
  666. subs->hwptr_done -= runtime->buffer_size * stride;
  667. /* update delay with exact number of samples queued */
  668. runtime->delay = subs->last_delay;
  669. runtime->delay += frames;
  670. subs->last_delay = runtime->delay;
  671. /* realign last_frame_number */
  672. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  673. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  674. spin_unlock_irqrestore(&subs->lock, flags);
  675. urb->transfer_buffer_length = bytes;
  676. if (period_elapsed)
  677. snd_pcm_period_elapsed(subs->pcm_substream);
  678. return 0;
  679. }
  680. /*
  681. * process after playback data complete
  682. * - decrease the delay count again
  683. */
  684. static int retire_playback_urb(struct snd_usb_substream *subs,
  685. struct snd_pcm_runtime *runtime,
  686. struct urb *urb)
  687. {
  688. unsigned long flags;
  689. int stride = runtime->frame_bits >> 3;
  690. int processed = urb->transfer_buffer_length / stride;
  691. int est_delay;
  692. spin_lock_irqsave(&subs->lock, flags);
  693. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  694. /* update delay with exact number of samples played */
  695. if (processed > subs->last_delay)
  696. subs->last_delay = 0;
  697. else
  698. subs->last_delay -= processed;
  699. runtime->delay = subs->last_delay;
  700. /*
  701. * Report when delay estimate is off by more than 2ms.
  702. * The error should be lower than 2ms since the estimate relies
  703. * on two reads of a counter updated every ms.
  704. */
  705. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  706. snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
  707. est_delay, subs->last_delay);
  708. spin_unlock_irqrestore(&subs->lock, flags);
  709. return 0;
  710. }
  711. static const char *usb_error_string(int err)
  712. {
  713. switch (err) {
  714. case -ENODEV:
  715. return "no device";
  716. case -ENOENT:
  717. return "endpoint not enabled";
  718. case -EPIPE:
  719. return "endpoint stalled";
  720. case -ENOSPC:
  721. return "not enough bandwidth";
  722. case -ESHUTDOWN:
  723. return "device disabled";
  724. case -EHOSTUNREACH:
  725. return "device suspended";
  726. case -EINVAL:
  727. case -EAGAIN:
  728. case -EFBIG:
  729. case -EMSGSIZE:
  730. return "internal error";
  731. default:
  732. return "unknown error";
  733. }
  734. }
  735. /*
  736. * set up and start data/sync urbs
  737. */
  738. static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
  739. {
  740. unsigned int i;
  741. int err;
  742. if (subs->stream->chip->shutdown)
  743. return -EBADFD;
  744. for (i = 0; i < subs->nurbs; i++) {
  745. if (snd_BUG_ON(!subs->dataurb[i].urb))
  746. return -EINVAL;
  747. if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  748. snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  749. goto __error;
  750. }
  751. }
  752. if (subs->syncpipe) {
  753. for (i = 0; i < SYNC_URBS; i++) {
  754. if (snd_BUG_ON(!subs->syncurb[i].urb))
  755. return -EINVAL;
  756. if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  757. snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  758. goto __error;
  759. }
  760. }
  761. }
  762. subs->active_mask = 0;
  763. subs->unlink_mask = 0;
  764. subs->running = 1;
  765. for (i = 0; i < subs->nurbs; i++) {
  766. err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
  767. if (err < 0) {
  768. snd_printk(KERN_ERR "cannot submit datapipe "
  769. "for urb %d, error %d: %s\n",
  770. i, err, usb_error_string(err));
  771. goto __error;
  772. }
  773. set_bit(i, &subs->active_mask);
  774. }
  775. if (subs->syncpipe) {
  776. for (i = 0; i < SYNC_URBS; i++) {
  777. err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
  778. if (err < 0) {
  779. snd_printk(KERN_ERR "cannot submit syncpipe "
  780. "for urb %d, error %d: %s\n",
  781. i, err, usb_error_string(err));
  782. goto __error;
  783. }
  784. set_bit(i + 16, &subs->active_mask);
  785. }
  786. }
  787. return 0;
  788. __error:
  789. // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  790. deactivate_urbs_old(subs, 0, 0);
  791. return -EPIPE;
  792. }
  793. /*
  794. */
  795. static struct snd_urb_ops audio_urb_ops[2] = {
  796. {
  797. .prepare = prepare_nodata_playback_urb,
  798. .retire = retire_playback_urb,
  799. .prepare_sync = prepare_playback_sync_urb,
  800. .retire_sync = retire_playback_sync_urb,
  801. },
  802. {
  803. .prepare = prepare_capture_urb,
  804. .retire = retire_capture_urb,
  805. .prepare_sync = prepare_capture_sync_urb,
  806. .retire_sync = retire_capture_sync_urb,
  807. },
  808. };
  809. /*
  810. * initialize the substream instance.
  811. */
  812. void snd_usb_init_substream(struct snd_usb_stream *as,
  813. int stream, struct audioformat *fp)
  814. {
  815. struct snd_usb_substream *subs = &as->substream[stream];
  816. INIT_LIST_HEAD(&subs->fmt_list);
  817. spin_lock_init(&subs->lock);
  818. subs->stream = as;
  819. subs->direction = stream;
  820. subs->dev = as->chip->dev;
  821. subs->txfr_quirk = as->chip->txfr_quirk;
  822. subs->ops = audio_urb_ops[stream];
  823. if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)
  824. subs->ops.prepare_sync = prepare_capture_sync_urb_hs;
  825. snd_usb_set_pcm_ops(as->pcm, stream);
  826. list_add_tail(&fp->list, &subs->fmt_list);
  827. subs->formats |= fp->formats;
  828. subs->endpoint = fp->endpoint;
  829. subs->num_formats++;
  830. subs->fmt_type = fp->fmt_type;
  831. }
  832. int snd_usb_substream_prepare(struct snd_usb_substream *subs,
  833. struct snd_pcm_runtime *runtime)
  834. {
  835. /* clear urbs (to be sure) */
  836. deactivate_urbs_old(subs, 0, 1);
  837. wait_clear_urbs_old(subs);
  838. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  839. * updates for all URBs would happen at the same time when starting */
  840. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  841. subs->ops.prepare = prepare_nodata_playback_urb;
  842. return start_urbs(subs, runtime);
  843. }
  844. return 0;
  845. }
  846. int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep)
  847. {
  848. return ep->sync_master &&
  849. ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
  850. ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
  851. usb_pipeout(ep->pipe);
  852. }
  853. /* determine the number of frames in the next packet */
  854. static int next_packet_size(struct snd_usb_endpoint *ep)
  855. {
  856. unsigned long flags;
  857. int ret;
  858. if (ep->fill_max)
  859. return ep->maxframesize;
  860. spin_lock_irqsave(&ep->lock, flags);
  861. ep->phase = (ep->phase & 0xffff)
  862. + (ep->freqm << ep->datainterval);
  863. ret = min(ep->phase >> 16, ep->maxframesize);
  864. spin_unlock_irqrestore(&ep->lock, flags);
  865. return ret;
  866. }
  867. static void retire_outbound_urb(struct snd_usb_endpoint *ep,
  868. struct snd_urb_ctx *urb_ctx)
  869. {
  870. if (ep->retire_data_urb)
  871. ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
  872. }
  873. static void retire_inbound_urb(struct snd_usb_endpoint *ep,
  874. struct snd_urb_ctx *urb_ctx)
  875. {
  876. struct urb *urb = urb_ctx->urb;
  877. if (ep->sync_slave)
  878. snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
  879. if (ep->retire_data_urb)
  880. ep->retire_data_urb(ep->data_subs, urb);
  881. }
  882. static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep,
  883. struct snd_urb_ctx *ctx)
  884. {
  885. int i;
  886. for (i = 0; i < ctx->packets; ++i)
  887. ctx->packet_size[i] = next_packet_size(ep);
  888. }
  889. /*
  890. * Prepare a PLAYBACK urb for submission to the bus.
  891. */
  892. static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
  893. struct snd_urb_ctx *ctx)
  894. {
  895. int i;
  896. struct urb *urb = ctx->urb;
  897. unsigned char *cp = urb->transfer_buffer;
  898. urb->dev = ep->chip->dev; /* we need to set this at each time */
  899. switch (ep->type) {
  900. case SND_USB_ENDPOINT_TYPE_DATA:
  901. if (ep->prepare_data_urb) {
  902. ep->prepare_data_urb(ep->data_subs, urb);
  903. } else {
  904. /* no data provider, so send silence */
  905. unsigned int offs = 0;
  906. for (i = 0; i < ctx->packets; ++i) {
  907. int counts = ctx->packet_size[i];
  908. urb->iso_frame_desc[i].offset = offs * ep->stride;
  909. urb->iso_frame_desc[i].length = counts * ep->stride;
  910. offs += counts;
  911. }
  912. urb->number_of_packets = ctx->packets;
  913. urb->transfer_buffer_length = offs * ep->stride;
  914. memset(urb->transfer_buffer, ep->silence_value,
  915. offs * ep->stride);
  916. }
  917. break;
  918. case SND_USB_ENDPOINT_TYPE_SYNC:
  919. if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
  920. /*
  921. * fill the length and offset of each urb descriptor.
  922. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  923. */
  924. urb->iso_frame_desc[0].length = 4;
  925. urb->iso_frame_desc[0].offset = 0;
  926. cp[0] = ep->freqn;
  927. cp[1] = ep->freqn >> 8;
  928. cp[2] = ep->freqn >> 16;
  929. cp[3] = ep->freqn >> 24;
  930. } else {
  931. /*
  932. * fill the length and offset of each urb descriptor.
  933. * the fixed 10.14 frequency is passed through the pipe.
  934. */
  935. urb->iso_frame_desc[0].length = 3;
  936. urb->iso_frame_desc[0].offset = 0;
  937. cp[0] = ep->freqn >> 2;
  938. cp[1] = ep->freqn >> 10;
  939. cp[2] = ep->freqn >> 18;
  940. }
  941. break;
  942. }
  943. }
  944. /*
  945. * Prepare a CAPTURE or SYNC urb for submission to the bus.
  946. */
  947. static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
  948. struct snd_urb_ctx *urb_ctx)
  949. {
  950. int i, offs;
  951. struct urb *urb = urb_ctx->urb;
  952. urb->dev = ep->chip->dev; /* we need to set this at each time */
  953. switch (ep->type) {
  954. case SND_USB_ENDPOINT_TYPE_DATA:
  955. offs = 0;
  956. for (i = 0; i < urb_ctx->packets; i++) {
  957. urb->iso_frame_desc[i].offset = offs;
  958. urb->iso_frame_desc[i].length = ep->curpacksize;
  959. offs += ep->curpacksize;
  960. }
  961. urb->transfer_buffer_length = offs;
  962. urb->number_of_packets = urb_ctx->packets;
  963. break;
  964. case SND_USB_ENDPOINT_TYPE_SYNC:
  965. urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
  966. urb->iso_frame_desc[0].offset = 0;
  967. break;
  968. }
  969. }
  970. static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
  971. {
  972. while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
  973. unsigned long flags;
  974. struct snd_usb_packet_info *packet;
  975. struct snd_urb_ctx *ctx = NULL;
  976. struct urb *urb;
  977. int err, i;
  978. spin_lock_irqsave(&ep->lock, flags);
  979. if (ep->next_packet_read_pos != ep->next_packet_write_pos) {
  980. packet = ep->next_packet + ep->next_packet_read_pos;
  981. ep->next_packet_read_pos++;
  982. ep->next_packet_read_pos %= MAX_URBS;
  983. /* take URB out of FIFO */
  984. if (!list_empty(&ep->ready_playback_urbs))
  985. ctx = list_first_entry(&ep->ready_playback_urbs,
  986. struct snd_urb_ctx, ready_list);
  987. }
  988. spin_unlock_irqrestore(&ep->lock, flags);
  989. if (ctx == NULL)
  990. return;
  991. list_del_init(&ctx->ready_list);
  992. urb = ctx->urb;
  993. /* copy over the length information */
  994. for (i = 0; i < packet->packets; i++)
  995. ctx->packet_size[i] = packet->packet_size[i];
  996. prepare_outbound_urb(ep, ctx);
  997. err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
  998. if (err < 0)
  999. snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n",
  1000. ctx->index, err, ctx->urb);
  1001. else
  1002. set_bit(ctx->index, &ep->active_mask);
  1003. }
  1004. }
  1005. /*
  1006. * complete callback for urbs
  1007. */
  1008. static void snd_complete_urb(struct urb *urb)
  1009. {
  1010. struct snd_urb_ctx *ctx = urb->context;
  1011. struct snd_usb_endpoint *ep = ctx->ep;
  1012. int err;
  1013. if (unlikely(urb->status == -ENOENT || /* unlinked */
  1014. urb->status == -ENODEV || /* device removed */
  1015. urb->status == -ECONNRESET || /* unlinked */
  1016. urb->status == -ESHUTDOWN || /* device disabled */
  1017. ep->chip->shutdown)) /* device disconnected */
  1018. goto exit_clear;
  1019. if (usb_pipeout(ep->pipe)) {
  1020. retire_outbound_urb(ep, ctx);
  1021. /* can be stopped during retire callback */
  1022. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  1023. goto exit_clear;
  1024. if (snd_usb_endpoint_implict_feedback_sink(ep)) {
  1025. unsigned long flags;
  1026. spin_lock_irqsave(&ep->lock, flags);
  1027. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  1028. spin_unlock_irqrestore(&ep->lock, flags);
  1029. queue_pending_output_urbs(ep);
  1030. goto exit_clear;
  1031. }
  1032. prepare_outbound_urb_sizes(ep, ctx);
  1033. prepare_outbound_urb(ep, ctx);
  1034. } else {
  1035. retire_inbound_urb(ep, ctx);
  1036. /* can be stopped during retire callback */
  1037. if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
  1038. goto exit_clear;
  1039. prepare_inbound_urb(ep, ctx);
  1040. }
  1041. err = usb_submit_urb(urb, GFP_ATOMIC);
  1042. if (err == 0)
  1043. return;
  1044. snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err);
  1045. //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1046. exit_clear:
  1047. clear_bit(ctx->index, &ep->active_mask);
  1048. }
  1049. struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
  1050. struct usb_host_interface *alts,
  1051. int ep_num, int direction, int type)
  1052. {
  1053. struct list_head *p;
  1054. struct snd_usb_endpoint *ep;
  1055. int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;
  1056. mutex_lock(&chip->mutex);
  1057. list_for_each(p, &chip->ep_list) {
  1058. ep = list_entry(p, struct snd_usb_endpoint, list);
  1059. if (ep->ep_num == ep_num &&
  1060. ep->iface == alts->desc.bInterfaceNumber &&
  1061. ep->alt_idx == alts->desc.bAlternateSetting) {
  1062. snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n",
  1063. ep_num, ep->iface, ep->alt_idx, ep);
  1064. goto __exit_unlock;
  1065. }
  1066. }
  1067. snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n",
  1068. is_playback ? "playback" : "capture",
  1069. type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  1070. ep_num);
  1071. /* select the alt setting once so the endpoints become valid */
  1072. ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber,
  1073. alts->desc.bAlternateSetting);
  1074. if (ret < 0) {
  1075. snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n",
  1076. __func__, ret);
  1077. ep = NULL;
  1078. goto __exit_unlock;
  1079. }
  1080. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  1081. if (!ep)
  1082. goto __exit_unlock;
  1083. ep->chip = chip;
  1084. spin_lock_init(&ep->lock);
  1085. ep->type = type;
  1086. ep->ep_num = ep_num;
  1087. ep->iface = alts->desc.bInterfaceNumber;
  1088. ep->alt_idx = alts->desc.bAlternateSetting;
  1089. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  1090. ep_num &= USB_ENDPOINT_NUMBER_MASK;
  1091. if (is_playback)
  1092. ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
  1093. else
  1094. ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
  1095. if (type == SND_USB_ENDPOINT_TYPE_SYNC) {
  1096. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1097. get_endpoint(alts, 1)->bRefresh >= 1 &&
  1098. get_endpoint(alts, 1)->bRefresh <= 9)
  1099. ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
  1100. else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
  1101. ep->syncinterval = 1;
  1102. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  1103. get_endpoint(alts, 1)->bInterval <= 16)
  1104. ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  1105. else
  1106. ep->syncinterval = 3;
  1107. ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  1108. }
  1109. list_add_tail(&ep->list, &chip->ep_list);
  1110. __exit_unlock:
  1111. mutex_unlock(&chip->mutex);
  1112. return ep;
  1113. }
  1114. /*
  1115. * wait until all urbs are processed.
  1116. */
  1117. static int wait_clear_urbs(struct snd_usb_endpoint *ep)
  1118. {
  1119. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  1120. unsigned int i;
  1121. int alive;
  1122. do {
  1123. alive = 0;
  1124. for (i = 0; i < ep->nurbs; i++)
  1125. if (test_bit(i, &ep->active_mask))
  1126. alive++;
  1127. if (!alive)
  1128. break;
  1129. schedule_timeout_uninterruptible(1);
  1130. } while (time_before(jiffies, end_time));
  1131. if (alive)
  1132. snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n",
  1133. alive, ep->ep_num);
  1134. return 0;
  1135. }
  1136. /*
  1137. * unlink active urbs.
  1138. */
  1139. static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep)
  1140. {
  1141. unsigned long flags;
  1142. unsigned int i;
  1143. int async;
  1144. if (!force && ep->chip->shutdown) /* to be sure... */
  1145. return -EBADFD;
  1146. async = !can_sleep && ep->chip->async_unlink;
  1147. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  1148. INIT_LIST_HEAD(&ep->ready_playback_urbs);
  1149. ep->next_packet_read_pos = 0;
  1150. ep->next_packet_write_pos = 0;
  1151. if (!async && in_interrupt())
  1152. return 0;
  1153. for (i = 0; i < ep->nurbs; i++) {
  1154. if (test_bit(i, &ep->active_mask)) {
  1155. if (!test_and_set_bit(i, &ep->unlink_mask)) {
  1156. struct urb *u = ep->urb[i].urb;
  1157. if (async)
  1158. usb_unlink_urb(u);
  1159. else
  1160. usb_kill_urb(u);
  1161. }
  1162. }
  1163. }
  1164. return 0;
  1165. }
  1166. /*
  1167. * release an endpoint's urbs
  1168. */
  1169. static void release_urbs(struct snd_usb_endpoint *ep, int force)
  1170. {
  1171. int i;
  1172. /* route incoming urbs to nirvana */
  1173. ep->retire_data_urb = NULL;
  1174. ep->prepare_data_urb = NULL;
  1175. /* stop urbs */
  1176. deactivate_urbs(ep, force, 1);
  1177. wait_clear_urbs(ep);
  1178. for (i = 0; i < ep->nurbs; i++)
  1179. release_urb_ctx(&ep->urb[i]);
  1180. if (ep->syncbuf)
  1181. usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
  1182. ep->syncbuf, ep->sync_dma);
  1183. ep->syncbuf = NULL;
  1184. ep->nurbs = 0;
  1185. }
  1186. static int data_ep_set_params(struct snd_usb_endpoint *ep,
  1187. struct snd_pcm_hw_params *hw_params,
  1188. struct audioformat *fmt,
  1189. struct snd_usb_endpoint *sync_ep)
  1190. {
  1191. unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms;
  1192. int period_bytes = params_period_bytes(hw_params);
  1193. int format = params_format(hw_params);
  1194. int is_playback = usb_pipeout(ep->pipe);
  1195. int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) *
  1196. params_channels(hw_params);
  1197. ep->datainterval = fmt->datainterval;
  1198. ep->stride = frame_bits >> 3;
  1199. ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0;
  1200. /* calculate max. frequency */
  1201. if (ep->maxpacksize) {
  1202. /* whatever fits into a max. size packet */
  1203. maxsize = ep->maxpacksize;
  1204. ep->freqmax = (maxsize / (frame_bits >> 3))
  1205. << (16 - ep->datainterval);
  1206. } else {
  1207. /* no max. packet size: just take 25% higher than nominal */
  1208. ep->freqmax = ep->freqn + (ep->freqn >> 2);
  1209. maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3))
  1210. >> (16 - ep->datainterval);
  1211. }
  1212. if (ep->fill_max)
  1213. ep->curpacksize = ep->maxpacksize;
  1214. else
  1215. ep->curpacksize = maxsize;
  1216. if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL)
  1217. packs_per_ms = 8 >> ep->datainterval;
  1218. else
  1219. packs_per_ms = 1;
  1220. if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {
  1221. urb_packs = max(ep->chip->nrpacks, 1);
  1222. urb_packs = min(urb_packs, (unsigned int) MAX_PACKS);
  1223. } else {
  1224. urb_packs = 1;
  1225. }
  1226. urb_packs *= packs_per_ms;
  1227. if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep))
  1228. urb_packs = min(urb_packs, 1U << sync_ep->syncinterval);
  1229. /* decide how many packets to be used */
  1230. if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {
  1231. unsigned int minsize, maxpacks;
  1232. /* determine how small a packet can be */
  1233. minsize = (ep->freqn >> (16 - ep->datainterval))
  1234. * (frame_bits >> 3);
  1235. /* with sync from device, assume it can be 12% lower */
  1236. if (sync_ep)
  1237. minsize -= minsize >> 3;
  1238. minsize = max(minsize, 1u);
  1239. total_packs = (period_bytes + minsize - 1) / minsize;
  1240. /* we need at least two URBs for queueing */
  1241. if (total_packs < 2) {
  1242. total_packs = 2;
  1243. } else {
  1244. /* and we don't want too long a queue either */
  1245. maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  1246. total_packs = min(total_packs, maxpacks);
  1247. }
  1248. } else {
  1249. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  1250. urb_packs >>= 1;
  1251. total_packs = MAX_URBS * urb_packs;
  1252. }
  1253. ep->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  1254. if (ep->nurbs > MAX_URBS) {
  1255. /* too much... */
  1256. ep->nurbs = MAX_URBS;
  1257. total_packs = MAX_URBS * urb_packs;
  1258. } else if (ep->nurbs < 2) {
  1259. /* too little - we need at least two packets
  1260. * to ensure contiguous playback/capture
  1261. */
  1262. ep->nurbs = 2;
  1263. }
  1264. /* allocate and initialize data urbs */
  1265. for (i = 0; i < ep->nurbs; i++) {
  1266. struct snd_urb_ctx *u = &ep->urb[i];
  1267. u->index = i;
  1268. u->ep = ep;
  1269. u->packets = (i + 1) * total_packs / ep->nurbs
  1270. - i * total_packs / ep->nurbs;
  1271. u->buffer_size = maxsize * u->packets;
  1272. if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
  1273. u->packets++; /* for transfer delimiter */
  1274. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  1275. if (!u->urb)
  1276. goto out_of_memory;
  1277. u->urb->transfer_buffer =
  1278. usb_alloc_coherent(ep->chip->dev, u->buffer_size,
  1279. GFP_KERNEL, &u->urb->transfer_dma);
  1280. if (!u->urb->transfer_buffer)
  1281. goto out_of_memory;
  1282. u->urb->pipe = ep->pipe;
  1283. u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  1284. u->urb->interval = 1 << ep->datainterval;
  1285. u->urb->context = u;
  1286. u->urb->complete = snd_complete_urb;
  1287. INIT_LIST_HEAD(&u->ready_list);
  1288. }
  1289. return 0;
  1290. out_of_memory:
  1291. release_urbs(ep, 0);
  1292. return -ENOMEM;
  1293. }
  1294. static int sync_ep_set_params(struct snd_usb_endpoint *ep,
  1295. struct snd_pcm_hw_params *hw_params,
  1296. struct audioformat *fmt)
  1297. {
  1298. int i;
  1299. ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
  1300. GFP_KERNEL, &ep->sync_dma);
  1301. if (!ep->syncbuf)
  1302. return -ENOMEM;
  1303. for (i = 0; i < SYNC_URBS; i++) {
  1304. struct snd_urb_ctx *u = &ep->urb[i];
  1305. u->index = i;
  1306. u->ep = ep;
  1307. u->packets = 1;
  1308. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  1309. if (!u->urb)
  1310. goto out_of_memory;
  1311. u->urb->transfer_buffer = ep->syncbuf + i * 4;
  1312. u->urb->transfer_dma = ep->sync_dma + i * 4;
  1313. u->urb->transfer_buffer_length = 4;
  1314. u->urb->pipe = ep->pipe;
  1315. u->urb->transfer_flags = URB_ISO_ASAP |
  1316. URB_NO_TRANSFER_DMA_MAP;
  1317. u->urb->number_of_packets = 1;
  1318. u->urb->interval = 1 << ep->syncinterval;
  1319. u->urb->context = u;
  1320. u->urb->complete = snd_complete_urb;
  1321. }
  1322. ep->nurbs = SYNC_URBS;
  1323. return 0;
  1324. out_of_memory:
  1325. release_urbs(ep, 0);
  1326. return -ENOMEM;
  1327. }
  1328. int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
  1329. struct snd_pcm_hw_params *hw_params,
  1330. struct audioformat *fmt,
  1331. struct snd_usb_endpoint *sync_ep)
  1332. {
  1333. int err;
  1334. if (ep->use_count != 0) {
  1335. snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n",
  1336. ep->ep_num);
  1337. return -EBUSY;
  1338. }
  1339. /* release old buffers, if any */
  1340. release_urbs(ep, 0);
  1341. ep->datainterval = fmt->datainterval;
  1342. ep->maxpacksize = fmt->maxpacksize;
  1343. ep->fill_max = fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX;
  1344. if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)
  1345. ep->freqn = get_usb_full_speed_rate(params_rate(hw_params));
  1346. else
  1347. ep->freqn = get_usb_high_speed_rate(params_rate(hw_params));
  1348. /* calculate the frequency in 16.16 format */
  1349. ep->freqm = ep->freqn;
  1350. ep->freqshift = INT_MIN;
  1351. ep->phase = 0;
  1352. switch (ep->type) {
  1353. case SND_USB_ENDPOINT_TYPE_DATA:
  1354. err = data_ep_set_params(ep, hw_params, fmt, sync_ep);
  1355. break;
  1356. case SND_USB_ENDPOINT_TYPE_SYNC:
  1357. err = sync_ep_set_params(ep, hw_params, fmt);
  1358. break;
  1359. default:
  1360. err = -EINVAL;
  1361. }
  1362. snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",
  1363. ep->ep_num, ep->type, ep->nurbs, err);
  1364. return err;
  1365. }
  1366. int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
  1367. {
  1368. int err;
  1369. unsigned int i;
  1370. if (ep->chip->shutdown)
  1371. return -EBADFD;
  1372. /* already running? */
  1373. if (++ep->use_count != 1)
  1374. return 0;
  1375. if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags)))
  1376. return -EINVAL;
  1377. /* just to be sure */
  1378. deactivate_urbs(ep, 0, 1);
  1379. wait_clear_urbs(ep);
  1380. ep->active_mask = 0;
  1381. ep->unlink_mask = 0;
  1382. ep->phase = 0;
  1383. /*
  1384. * If this endpoint has a data endpoint as implicit feedback source,
  1385. * don't start the urbs here. Instead, mark them all as available,
  1386. * wait for the record urbs to arrive and queue from that context.
  1387. */
  1388. set_bit(EP_FLAG_RUNNING, &ep->flags);
  1389. if (snd_usb_endpoint_implict_feedback_sink(ep)) {
  1390. for (i = 0; i < ep->nurbs; i++) {
  1391. struct snd_urb_ctx *ctx = ep->urb + i;
  1392. list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
  1393. }
  1394. return 0;
  1395. }
  1396. for (i = 0; i < ep->nurbs; i++) {
  1397. struct urb *urb = ep->urb[i].urb;
  1398. if (snd_BUG_ON(!urb))
  1399. goto __error;
  1400. if (usb_pipeout(ep->pipe)) {
  1401. prepare_outbound_urb_sizes(ep, urb->context);
  1402. prepare_outbound_urb(ep, urb->context);
  1403. } else {
  1404. prepare_inbound_urb(ep, urb->context);
  1405. }
  1406. err = usb_submit_urb(urb, GFP_ATOMIC);
  1407. if (err < 0) {
  1408. snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n",
  1409. i, err, usb_error_string(err));
  1410. goto __error;
  1411. }
  1412. set_bit(i, &ep->active_mask);
  1413. }
  1414. return 0;
  1415. __error:
  1416. clear_bit(EP_FLAG_RUNNING, &ep->flags);
  1417. ep->use_count--;
  1418. deactivate_urbs(ep, 0, 0);
  1419. return -EPIPE;
  1420. }
  1421. void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep,
  1422. int force, int can_sleep, int wait)
  1423. {
  1424. if (!ep)
  1425. return;
  1426. if (snd_BUG_ON(ep->use_count == 0))
  1427. return;
  1428. if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags)))
  1429. return;
  1430. if (--ep->use_count == 0) {
  1431. deactivate_urbs(ep, force, can_sleep);
  1432. ep->data_subs = NULL;
  1433. ep->sync_slave = NULL;
  1434. ep->retire_data_urb = NULL;
  1435. ep->prepare_data_urb = NULL;
  1436. if (wait)
  1437. wait_clear_urbs(ep);
  1438. }
  1439. }
  1440. int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep)
  1441. {
  1442. if (ep->use_count != 0)
  1443. return 0;
  1444. if (!ep->chip->shutdown &&
  1445. !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) {
  1446. int ret;
  1447. ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx);
  1448. if (ret < 0) {
  1449. snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n",
  1450. __func__, ret);
  1451. clear_bit(EP_FLAG_ACTIVATED, &ep->flags);
  1452. return ret;
  1453. }
  1454. return 0;
  1455. }
  1456. return -EBUSY;
  1457. }
  1458. int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
  1459. {
  1460. if (!ep)
  1461. return -EINVAL;
  1462. if (ep->use_count != 0)
  1463. return 0;
  1464. if (!ep->chip->shutdown &&
  1465. test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) {
  1466. int ret;
  1467. ret = usb_set_interface(ep->chip->dev, ep->iface, 0);
  1468. if (ret < 0) {
  1469. snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n",
  1470. __func__, ret);
  1471. return ret;
  1472. }
  1473. return 0;
  1474. }
  1475. return -EBUSY;
  1476. }
  1477. void snd_usb_endpoint_free(struct list_head *head)
  1478. {
  1479. struct snd_usb_endpoint *ep;
  1480. ep = list_entry(head, struct snd_usb_endpoint, list);
  1481. release_urbs(ep, 1);
  1482. kfree(ep);
  1483. }
  1484. /*
  1485. * process after playback sync complete
  1486. *
  1487. * Full speed devices report feedback values in 10.14 format as samples per
  1488. * frame, high speed devices in 16.16 format as samples per microframe.
  1489. * Because the Audio Class 1 spec was written before USB 2.0, many high speed
  1490. * devices use a wrong interpretation, some others use an entirely different
  1491. * format. Therefore, we cannot predict what format any particular device uses
  1492. * and must detect it automatically.
  1493. */
  1494. void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  1495. struct snd_usb_endpoint *sender,
  1496. const struct urb *urb)
  1497. {
  1498. int shift;
  1499. unsigned int f;
  1500. unsigned long flags;
  1501. snd_BUG_ON(ep == sender);
  1502. if (snd_usb_endpoint_implict_feedback_sink(ep) &&
  1503. ep->use_count != 0) {
  1504. /* implicit feedback case */
  1505. int i, bytes = 0;
  1506. struct snd_urb_ctx *in_ctx;
  1507. struct snd_usb_packet_info *out_packet;
  1508. in_ctx = urb->context;
  1509. /* Count overall packet size */
  1510. for (i = 0; i < in_ctx->packets; i++)
  1511. if (urb->iso_frame_desc[i].status == 0)
  1512. bytes += urb->iso_frame_desc[i].actual_length;
  1513. /*
  1514. * skip empty packets. At least M-Audio's Fast Track Ultra stops
  1515. * streaming once it received a 0-byte OUT URB
  1516. */
  1517. if (bytes == 0)
  1518. return;
  1519. spin_lock_irqsave(&ep->lock, flags);
  1520. out_packet = ep->next_packet + ep->next_packet_write_pos;
  1521. /*
  1522. * Iterate through the inbound packet and prepare the lengths
  1523. * for the output packet. The OUT packet we are about to send
  1524. * will have the same amount of payload than the IN packet we
  1525. * just received.
  1526. */
  1527. out_packet->packets = in_ctx->packets;
  1528. for (i = 0; i < in_ctx->packets; i++) {
  1529. if (urb->iso_frame_desc[i].status == 0)
  1530. out_packet->packet_size[i] =
  1531. urb->iso_frame_desc[i].actual_length / ep->stride;
  1532. else
  1533. out_packet->packet_size[i] = 0;
  1534. }
  1535. ep->next_packet_write_pos++;
  1536. ep->next_packet_write_pos %= MAX_URBS;
  1537. spin_unlock_irqrestore(&ep->lock, flags);
  1538. queue_pending_output_urbs(ep);
  1539. return;
  1540. }
  1541. /* parse sync endpoint packet */
  1542. if (urb->iso_frame_desc[0].status != 0 ||
  1543. urb->iso_frame_desc[0].actual_length < 3)
  1544. return;
  1545. f = le32_to_cpup(urb->transfer_buffer);
  1546. if (urb->iso_frame_desc[0].actual_length == 3)
  1547. f &= 0x00ffffff;
  1548. else
  1549. f &= 0x0fffffff;
  1550. if (f == 0)
  1551. return;
  1552. if (unlikely(ep->freqshift == INT_MIN)) {
  1553. /*
  1554. * The first time we see a feedback value, determine its format
  1555. * by shifting it left or right until it matches the nominal
  1556. * frequency value. This assumes that the feedback does not
  1557. * differ from the nominal value more than +50% or -25%.
  1558. */
  1559. shift = 0;
  1560. while (f < ep->freqn - ep->freqn / 4) {
  1561. f <<= 1;
  1562. shift++;
  1563. }
  1564. while (f > ep->freqn + ep->freqn / 2) {
  1565. f >>= 1;
  1566. shift--;
  1567. }
  1568. ep->freqshift = shift;
  1569. } else if (ep->freqshift >= 0)
  1570. f <<= ep->freqshift;
  1571. else
  1572. f >>= -ep->freqshift;
  1573. if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
  1574. /*
  1575. * If the frequency looks valid, set it.
  1576. * This value is referred to in prepare_playback_urb().
  1577. */
  1578. spin_lock_irqsave(&ep->lock, flags);
  1579. ep->freqm = f;
  1580. spin_unlock_irqrestore(&ep->lock, flags);
  1581. } else {
  1582. /*
  1583. * Out of range; maybe the shift value is wrong.
  1584. * Reset it so that we autodetect again the next time.
  1585. */
  1586. ep->freqshift = INT_MIN;
  1587. }
  1588. }