soc-pcm.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762
  1. /*
  2. * soc-pcm.c -- ALSA SoC PCM
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. * Copyright (C) 2010 Slimlogic Ltd.
  7. * Copyright (C) 2010 Texas Instruments Inc.
  8. *
  9. * Authors: Liam Girdwood <lrg@ti.com>
  10. * Mark Brown <broonie@opensource.wolfsonmicro.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/export.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/soc-dpcm.h>
  32. #include <sound/initval.h>
  33. #define MAX_BE_USERS 8 /* adjust if too low for everday use */
  34. /* ASoC no host IO hardware.
  35. * TODO: fine tune these values for all host less transfers.
  36. */
  37. static const struct snd_pcm_hardware no_host_hardware = {
  38. .info = SNDRV_PCM_INFO_MMAP |
  39. SNDRV_PCM_INFO_MMAP_VALID |
  40. SNDRV_PCM_INFO_INTERLEAVED |
  41. SNDRV_PCM_INFO_PAUSE |
  42. SNDRV_PCM_INFO_RESUME,
  43. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  44. SNDRV_PCM_FMTBIT_S32_LE,
  45. .period_bytes_min = PAGE_SIZE >> 2,
  46. .period_bytes_max = PAGE_SIZE >> 1,
  47. .periods_min = 2,
  48. .periods_max = 4,
  49. .buffer_bytes_max = PAGE_SIZE,
  50. };
  51. /*
  52. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  53. * are not running, paused or suspended for the specified stream direction.
  54. */
  55. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  56. struct snd_soc_pcm_runtime *be, int stream)
  57. {
  58. struct snd_soc_dpcm_params *dpcm_params;
  59. list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
  60. if (dpcm_params->fe == fe)
  61. continue;
  62. if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
  63. dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
  64. dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND)
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  70. /*
  71. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  72. * running, paused or suspended for the specified stream direction.
  73. */
  74. static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  75. struct snd_soc_pcm_runtime *be, int stream)
  76. {
  77. struct snd_soc_dpcm_params *dpcm_params;
  78. list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
  79. if (dpcm_params->fe == fe)
  80. continue;
  81. if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
  82. dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
  83. dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND ||
  84. dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE)
  85. return 0;
  86. }
  87. return 1;
  88. }
  89. static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
  90. struct snd_soc_dai *soc_dai)
  91. {
  92. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  93. int ret;
  94. if (!soc_dai->driver->symmetric_rates &&
  95. !rtd->dai_link->symmetric_rates)
  96. return 0;
  97. /* This can happen if multiple streams are starting simultaneously -
  98. * the second can need to get its constraints before the first has
  99. * picked a rate. Complain and allow the application to carry on.
  100. */
  101. if (!soc_dai->rate) {
  102. dev_warn(soc_dai->dev,
  103. "Not enforcing symmetric_rates due to race\n");
  104. return 0;
  105. }
  106. dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
  107. ret = snd_pcm_hw_constraint_minmax(substream->runtime,
  108. SNDRV_PCM_HW_PARAM_RATE,
  109. soc_dai->rate, soc_dai->rate);
  110. if (ret < 0) {
  111. dev_err(soc_dai->dev,
  112. "Unable to apply rate symmetry constraint: %d\n", ret);
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. /*
  118. * List of sample sizes that might go over the bus for parameter
  119. * application. There ought to be a wildcard sample size for things
  120. * like the DAC/ADC resolution to use but there isn't right now.
  121. */
  122. static int sample_sizes[] = {
  123. 8, 16, 24, 32,
  124. };
  125. static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
  126. struct snd_soc_dai *dai)
  127. {
  128. int ret, i, bits;
  129. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  130. bits = dai->driver->playback.sig_bits;
  131. else
  132. bits = dai->driver->capture.sig_bits;
  133. if (!bits)
  134. return;
  135. for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
  136. if (bits >= sample_sizes[i])
  137. continue;
  138. ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
  139. sample_sizes[i], bits);
  140. if (ret != 0)
  141. dev_warn(dai->dev,
  142. "Failed to set MSB %d/%d: %d\n",
  143. bits, sample_sizes[i], ret);
  144. }
  145. }
  146. /*
  147. * stream event, send event to FE and all active BEs.
  148. */
  149. int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe,
  150. int dir, const char *stream, int event)
  151. {
  152. struct snd_soc_dpcm_params *dpcm_params;
  153. snd_soc_dapm_rtd_stream_event(fe, dir, event);
  154. list_for_each_entry(dpcm_params, &fe->dpcm[dir].be_clients, list_be) {
  155. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  156. dev_dbg(be->dev, "pm: BE %s stream %s event %d dir %d\n",
  157. be->dai_link->name, stream, event, dir);
  158. snd_soc_dapm_rtd_stream_event(be, dir, event);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  164. * then initialized and any private data can be allocated. This also calls
  165. * startup for the cpu DAI, platform, machine and codec DAI.
  166. */
  167. static int soc_pcm_open(struct snd_pcm_substream *substream)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  170. struct snd_pcm_runtime *runtime = substream->runtime;
  171. struct snd_soc_platform *platform = rtd->platform;
  172. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  173. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  174. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  175. struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
  176. int ret = 0;
  177. pm_runtime_get_sync(cpu_dai->dev);
  178. pm_runtime_get_sync(codec_dai->dev);
  179. pm_runtime_get_sync(platform->dev);
  180. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  181. if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
  182. snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
  183. /* startup the audio subsystem */
  184. if (cpu_dai->driver->ops->startup) {
  185. ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
  186. if (ret < 0) {
  187. printk(KERN_ERR "asoc: can't open interface %s\n",
  188. cpu_dai->name);
  189. goto out;
  190. }
  191. }
  192. if (platform->driver->ops && platform->driver->ops->open) {
  193. ret = platform->driver->ops->open(substream);
  194. if (ret < 0) {
  195. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  196. goto platform_err;
  197. }
  198. }
  199. if (codec_dai->driver->ops->startup) {
  200. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  201. ret = codec_dai->driver->ops->startup(substream,
  202. codec_dai);
  203. if (ret < 0) {
  204. printk(KERN_ERR "asoc: can't open codec %s\n",
  205. codec_dai->name);
  206. goto codec_dai_err;
  207. }
  208. } else {
  209. if (!codec_dai->capture_active) {
  210. ret = codec_dai->driver->ops->startup(substream,
  211. codec_dai);
  212. if (ret < 0) {
  213. printk(KERN_ERR "can't open codec %s\n",
  214. codec_dai->name);
  215. goto codec_dai_err;
  216. }
  217. }
  218. }
  219. }
  220. if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
  221. ret = rtd->dai_link->ops->startup(substream);
  222. if (ret < 0) {
  223. printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
  224. goto machine_err;
  225. }
  226. }
  227. /* DSP DAI links compat checks are different */
  228. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
  229. goto dynamic;
  230. /* Check that the codec and cpu DAIs are compatible */
  231. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  232. runtime->hw.rate_min =
  233. max(codec_dai_drv->playback.rate_min,
  234. cpu_dai_drv->playback.rate_min);
  235. runtime->hw.rate_max =
  236. min(codec_dai_drv->playback.rate_max,
  237. cpu_dai_drv->playback.rate_max);
  238. runtime->hw.channels_min =
  239. max(codec_dai_drv->playback.channels_min,
  240. cpu_dai_drv->playback.channels_min);
  241. runtime->hw.channels_max =
  242. min(codec_dai_drv->playback.channels_max,
  243. cpu_dai_drv->playback.channels_max);
  244. runtime->hw.formats =
  245. codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
  246. runtime->hw.rates =
  247. codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
  248. if (codec_dai_drv->playback.rates
  249. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  250. runtime->hw.rates |= cpu_dai_drv->playback.rates;
  251. if (cpu_dai_drv->playback.rates
  252. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  253. runtime->hw.rates |= codec_dai_drv->playback.rates;
  254. } else {
  255. runtime->hw.rate_min =
  256. max(codec_dai_drv->capture.rate_min,
  257. cpu_dai_drv->capture.rate_min);
  258. runtime->hw.rate_max =
  259. min(codec_dai_drv->capture.rate_max,
  260. cpu_dai_drv->capture.rate_max);
  261. runtime->hw.channels_min =
  262. max(codec_dai_drv->capture.channels_min,
  263. cpu_dai_drv->capture.channels_min);
  264. runtime->hw.channels_max =
  265. min(codec_dai_drv->capture.channels_max,
  266. cpu_dai_drv->capture.channels_max);
  267. runtime->hw.formats =
  268. codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
  269. runtime->hw.rates =
  270. codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
  271. if (codec_dai_drv->capture.rates
  272. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  273. runtime->hw.rates |= cpu_dai_drv->capture.rates;
  274. if (cpu_dai_drv->capture.rates
  275. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  276. runtime->hw.rates |= codec_dai_drv->capture.rates;
  277. }
  278. ret = -EINVAL;
  279. snd_pcm_limit_hw_rates(runtime);
  280. if (!runtime->hw.rates) {
  281. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  282. codec_dai->name, cpu_dai->name);
  283. goto config_err;
  284. }
  285. if (!runtime->hw.formats) {
  286. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  287. codec_dai->name, cpu_dai->name);
  288. goto config_err;
  289. }
  290. if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
  291. runtime->hw.channels_min > runtime->hw.channels_max) {
  292. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  293. codec_dai->name, cpu_dai->name);
  294. goto config_err;
  295. }
  296. soc_pcm_apply_msb(substream, codec_dai);
  297. soc_pcm_apply_msb(substream, cpu_dai);
  298. /* Symmetry only applies if we've already got an active stream. */
  299. if (cpu_dai->active) {
  300. ret = soc_pcm_apply_symmetry(substream, cpu_dai);
  301. if (ret != 0)
  302. goto config_err;
  303. }
  304. if (codec_dai->active) {
  305. ret = soc_pcm_apply_symmetry(substream, codec_dai);
  306. if (ret != 0)
  307. goto config_err;
  308. }
  309. pr_debug("asoc: %s <-> %s info:\n",
  310. codec_dai->name, cpu_dai->name);
  311. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  312. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  313. runtime->hw.channels_max);
  314. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  315. runtime->hw.rate_max);
  316. dynamic:
  317. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  318. cpu_dai->playback_active++;
  319. codec_dai->playback_active++;
  320. } else {
  321. cpu_dai->capture_active++;
  322. codec_dai->capture_active++;
  323. }
  324. cpu_dai->active++;
  325. codec_dai->active++;
  326. rtd->codec->active++;
  327. mutex_unlock(&rtd->pcm_mutex);
  328. return 0;
  329. config_err:
  330. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  331. rtd->dai_link->ops->shutdown(substream);
  332. machine_err:
  333. if (codec_dai->driver->ops->shutdown)
  334. codec_dai->driver->ops->shutdown(substream, codec_dai);
  335. codec_dai_err:
  336. if (platform->driver->ops && platform->driver->ops->close)
  337. platform->driver->ops->close(substream);
  338. platform_err:
  339. if (cpu_dai->driver->ops->shutdown)
  340. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  341. out:
  342. mutex_unlock(&rtd->pcm_mutex);
  343. pm_runtime_put(platform->dev);
  344. pm_runtime_put(codec_dai->dev);
  345. pm_runtime_put(cpu_dai->dev);
  346. return ret;
  347. }
  348. /*
  349. * Power down the audio subsystem pmdown_time msecs after close is called.
  350. * This is to ensure there are no pops or clicks in between any music tracks
  351. * due to DAPM power cycling.
  352. */
  353. static void close_delayed_work(struct work_struct *work)
  354. {
  355. struct snd_soc_pcm_runtime *rtd =
  356. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  357. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  358. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  359. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  360. codec_dai->driver->playback.stream_name,
  361. codec_dai->playback_active ? "active" : "inactive",
  362. codec_dai->pop_wait ? "yes" : "no");
  363. /* are we waiting on this codec DAI stream */
  364. if (codec_dai->pop_wait == 1) {
  365. codec_dai->pop_wait = 0;
  366. snd_soc_dapm_stream_event(rtd,
  367. codec_dai->driver->playback.stream_name,
  368. SND_SOC_DAPM_STREAM_STOP);
  369. }
  370. mutex_unlock(&rtd->pcm_mutex);
  371. }
  372. /*
  373. * Called by ALSA when a PCM substream is closed. Private data can be
  374. * freed here. The cpu DAI, codec DAI, machine and platform are also
  375. * shutdown.
  376. */
  377. static int soc_pcm_close(struct snd_pcm_substream *substream)
  378. {
  379. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  380. struct snd_soc_platform *platform = rtd->platform;
  381. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  382. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  383. struct snd_soc_codec *codec = rtd->codec;
  384. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  385. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  386. cpu_dai->playback_active--;
  387. codec_dai->playback_active--;
  388. } else {
  389. cpu_dai->capture_active--;
  390. codec_dai->capture_active--;
  391. }
  392. cpu_dai->active--;
  393. codec_dai->active--;
  394. codec->active--;
  395. /* clear the corresponding DAIs rate when inactive */
  396. if (!cpu_dai->active)
  397. cpu_dai->rate = 0;
  398. if (!codec_dai->active)
  399. codec_dai->rate = 0;
  400. /* Muting the DAC suppresses artifacts caused during digital
  401. * shutdown, for example from stopping clocks.
  402. */
  403. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  404. snd_soc_dai_digital_mute(codec_dai, 1);
  405. if (cpu_dai->driver->ops->shutdown)
  406. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  407. if (codec_dai->driver->ops->shutdown) {
  408. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  409. codec_dai->driver->ops->shutdown(substream, codec_dai);
  410. } else {
  411. if (!codec_dai->capture_active)
  412. codec_dai->driver->ops->shutdown(substream,
  413. codec_dai);
  414. }
  415. }
  416. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  417. rtd->dai_link->ops->shutdown(substream);
  418. if (platform->driver->ops && platform->driver->ops->close)
  419. platform->driver->ops->close(substream);
  420. cpu_dai->runtime = NULL;
  421. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  422. if (codec->ignore_pmdown_time ||
  423. rtd->dai_link->ignore_pmdown_time ||
  424. !rtd->pmdown_time) {
  425. /* powered down playback stream now */
  426. snd_soc_dapm_stream_event(rtd,
  427. codec_dai->driver->playback.stream_name,
  428. SND_SOC_DAPM_STREAM_STOP);
  429. } else {
  430. /* start delayed pop wq here for playback streams */
  431. codec_dai->pop_wait = 1;
  432. schedule_delayed_work(&rtd->delayed_work,
  433. msecs_to_jiffies(rtd->pmdown_time));
  434. }
  435. } else {
  436. /* capture streams can be powered down now */
  437. if (!codec_dai->capture_active)
  438. snd_soc_dapm_stream_event(rtd,
  439. codec_dai->driver->capture.stream_name,
  440. SND_SOC_DAPM_STREAM_STOP);
  441. }
  442. mutex_unlock(&rtd->pcm_mutex);
  443. pm_runtime_put(platform->dev);
  444. pm_runtime_put(codec_dai->dev);
  445. pm_runtime_put(cpu_dai->dev);
  446. return 0;
  447. }
  448. /*
  449. * Called by ALSA when the PCM substream is prepared, can set format, sample
  450. * rate, etc. This function is non atomic and can be called multiple times,
  451. * it can refer to the runtime info.
  452. */
  453. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  454. {
  455. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  456. struct snd_soc_platform *platform = rtd->platform;
  457. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  458. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  459. int ret = 0;
  460. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  461. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  462. snd_soc_dapm_stream_event(rtd,
  463. codec_dai->driver->playback.stream_name,
  464. SND_SOC_DAPM_STREAM_START);
  465. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  466. ret = rtd->dai_link->ops->prepare(substream);
  467. if (ret < 0) {
  468. printk(KERN_ERR "asoc: machine prepare error\n");
  469. goto out;
  470. }
  471. }
  472. if (platform->driver->ops && platform->driver->ops->prepare) {
  473. ret = platform->driver->ops->prepare(substream);
  474. if (ret < 0) {
  475. printk(KERN_ERR "asoc: platform prepare error\n");
  476. goto out;
  477. }
  478. }
  479. if (codec_dai->driver->ops->prepare) {
  480. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  481. if (ret < 0) {
  482. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  483. goto out;
  484. }
  485. }
  486. if (cpu_dai->driver->ops->prepare) {
  487. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  488. if (ret < 0) {
  489. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  490. goto out;
  491. }
  492. }
  493. /* cancel any delayed stream shutdown that is pending */
  494. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  495. codec_dai->pop_wait) {
  496. codec_dai->pop_wait = 0;
  497. cancel_delayed_work(&rtd->delayed_work);
  498. }
  499. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  500. if (codec_dai->capture_active == 1)
  501. snd_soc_dapm_stream_event(rtd,
  502. codec_dai->driver->capture.stream_name,
  503. SND_SOC_DAPM_STREAM_START);
  504. }
  505. snd_soc_dai_digital_mute(codec_dai, 0);
  506. out:
  507. if (ret < 0 && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  508. pr_err("%s: Issue stop stream for codec_dai due to op failure %d = ret\n",
  509. __func__, ret);
  510. snd_soc_dapm_stream_event(rtd,
  511. codec_dai->driver->playback.stream_name,
  512. SND_SOC_DAPM_STREAM_STOP);
  513. }
  514. mutex_unlock(&rtd->pcm_mutex);
  515. return ret;
  516. }
  517. /*
  518. * Called by ALSA when the hardware params are set by application. This
  519. * function can also be called multiple times and can allocate buffers
  520. * (using snd_pcm_lib_* ). It's non-atomic.
  521. */
  522. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  523. struct snd_pcm_hw_params *params)
  524. {
  525. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  526. struct snd_soc_platform *platform = rtd->platform;
  527. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  528. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  529. int ret = 0;
  530. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  531. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  532. ret = rtd->dai_link->ops->hw_params(substream, params);
  533. if (ret < 0) {
  534. printk(KERN_ERR "asoc: machine hw_params failed\n");
  535. goto out;
  536. }
  537. }
  538. if (codec_dai->driver->ops->hw_params) {
  539. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  540. ret = codec_dai->driver->ops->hw_params(substream,
  541. params, codec_dai);
  542. if (ret < 0) {
  543. printk(KERN_ERR "not set codec %s hw params\n",
  544. codec_dai->name);
  545. goto codec_err;
  546. }
  547. } else {
  548. if (codec_dai->capture_active == 1) {
  549. ret = codec_dai->driver->ops->hw_params(
  550. substream, params, codec_dai);
  551. if (ret < 0) {
  552. printk(KERN_ERR "fail: %s hw params\n",
  553. codec_dai->name);
  554. goto codec_err;
  555. }
  556. }
  557. }
  558. }
  559. if (cpu_dai->driver->ops->hw_params) {
  560. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  561. if (ret < 0) {
  562. printk(KERN_ERR "asoc: interface %s hw params failed\n",
  563. cpu_dai->name);
  564. goto interface_err;
  565. }
  566. }
  567. if (platform->driver->ops && platform->driver->ops->hw_params) {
  568. ret = platform->driver->ops->hw_params(substream, params);
  569. if (ret < 0) {
  570. printk(KERN_ERR "asoc: platform %s hw params failed\n",
  571. platform->name);
  572. goto platform_err;
  573. }
  574. }
  575. /* store the rate for each DAIs */
  576. cpu_dai->rate = params_rate(params);
  577. codec_dai->rate = params_rate(params);
  578. /* malloc a page for hostless IO.
  579. * FIXME: rework with alsa-lib changes so that this malloc is not required.
  580. */
  581. if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
  582. substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
  583. substream->dma_buffer.dev.dev = rtd->dev;
  584. substream->dma_buffer.dev.dev->coherent_dma_mask = DMA_BIT_MASK(32);
  585. substream->dma_buffer.private_data = NULL;
  586. ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
  587. if (ret < 0)
  588. goto platform_err;
  589. }
  590. out:
  591. mutex_unlock(&rtd->pcm_mutex);
  592. return ret;
  593. platform_err:
  594. if (cpu_dai->driver->ops->hw_free)
  595. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  596. interface_err:
  597. if (codec_dai->driver->ops->hw_free)
  598. codec_dai->driver->ops->hw_free(substream, codec_dai);
  599. codec_err:
  600. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  601. rtd->dai_link->ops->hw_free(substream);
  602. mutex_unlock(&rtd->pcm_mutex);
  603. return ret;
  604. }
  605. /*
  606. * Frees resources allocated by hw_params, can be called multiple times
  607. */
  608. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  609. {
  610. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  611. struct snd_soc_platform *platform = rtd->platform;
  612. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  613. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  614. struct snd_soc_codec *codec = rtd->codec;
  615. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  616. /* apply codec digital mute */
  617. if (!codec->active)
  618. snd_soc_dai_digital_mute(codec_dai, 1);
  619. /* free any machine hw params */
  620. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  621. rtd->dai_link->ops->hw_free(substream);
  622. /* free any DMA resources */
  623. if (platform->driver->ops && platform->driver->ops->hw_free)
  624. platform->driver->ops->hw_free(substream);
  625. /* now free hw params for the DAIs */
  626. if (codec_dai->driver->ops->hw_free)
  627. codec_dai->driver->ops->hw_free(substream, codec_dai);
  628. if (cpu_dai->driver->ops->hw_free)
  629. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  630. if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
  631. snd_pcm_lib_free_pages(substream);
  632. mutex_unlock(&rtd->pcm_mutex);
  633. return 0;
  634. }
  635. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  636. {
  637. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  638. struct snd_soc_platform *platform = rtd->platform;
  639. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  640. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  641. int ret;
  642. if (codec_dai->driver->ops->trigger) {
  643. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  644. ret = codec_dai->driver->ops->trigger(substream,
  645. cmd, codec_dai);
  646. if (ret < 0)
  647. return ret;
  648. } else {
  649. if (codec_dai->capture_active == 1) {
  650. ret = codec_dai->driver->ops->trigger(
  651. substream, cmd, codec_dai);
  652. if (ret < 0)
  653. return ret;
  654. }
  655. }
  656. }
  657. if (platform->driver->ops && platform->driver->ops->trigger) {
  658. ret = platform->driver->ops->trigger(substream, cmd);
  659. if (ret < 0)
  660. return ret;
  661. }
  662. if (cpu_dai->driver->ops->trigger) {
  663. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  664. if (ret < 0)
  665. return ret;
  666. }
  667. return 0;
  668. }
  669. int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, int cmd)
  670. {
  671. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  672. struct snd_soc_platform *platform = rtd->platform;
  673. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  674. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  675. int ret;
  676. if (codec_dai->driver->ops->bespoke_trigger) {
  677. ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
  678. if (ret < 0)
  679. return ret;
  680. }
  681. if (platform->driver->bespoke_trigger) {
  682. ret = platform->driver->bespoke_trigger(substream, cmd);
  683. if (ret < 0)
  684. return ret;
  685. }
  686. if (cpu_dai->driver->ops->bespoke_trigger) {
  687. ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
  688. if (ret < 0)
  689. return ret;
  690. }
  691. return 0;
  692. }
  693. /*
  694. * soc level wrapper for pointer callback
  695. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  696. * the runtime->delay will be updated accordingly.
  697. */
  698. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  699. {
  700. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  701. struct snd_soc_platform *platform = rtd->platform;
  702. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  703. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  704. struct snd_pcm_runtime *runtime = substream->runtime;
  705. snd_pcm_uframes_t offset = 0;
  706. snd_pcm_sframes_t delay = 0;
  707. if (platform->driver->ops && platform->driver->ops->pointer)
  708. offset = platform->driver->ops->pointer(substream);
  709. if (cpu_dai->driver->ops->delay)
  710. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  711. if (codec_dai->driver->ops->delay)
  712. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  713. if (platform->driver->delay)
  714. delay += platform->driver->delay(substream, codec_dai);
  715. runtime->delay = delay;
  716. return offset;
  717. }
  718. static inline int be_connect(struct snd_soc_pcm_runtime *fe,
  719. struct snd_soc_pcm_runtime *be, int stream)
  720. {
  721. struct snd_soc_dpcm_params *dpcm_params;
  722. if (!fe->dpcm[stream].runtime && !fe->fe_compr) {
  723. dev_err(fe->dev, "%s no runtime\n", fe->dai_link->name);
  724. return -ENODEV;
  725. }
  726. /* only add new dpcm_paramss */
  727. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  728. if (dpcm_params->be == be && dpcm_params->fe == fe)
  729. return 0;
  730. }
  731. dpcm_params = kzalloc(sizeof(struct snd_soc_dpcm_params), GFP_KERNEL);
  732. if (!dpcm_params)
  733. return -ENOMEM;
  734. dpcm_params->be = be;
  735. dpcm_params->fe = fe;
  736. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  737. dpcm_params->state = SND_SOC_DPCM_LINK_STATE_NEW;
  738. list_add(&dpcm_params->list_be, &fe->dpcm[stream].be_clients);
  739. list_add(&dpcm_params->list_fe, &be->dpcm[stream].fe_clients);
  740. dev_dbg(fe->dev, " connected new DSP %s path %s %s %s\n",
  741. stream ? "capture" : "playback", fe->dai_link->name,
  742. stream ? "<-" : "->", be->dai_link->name);
  743. #ifdef CONFIG_DEBUG_FS
  744. dpcm_params->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  745. fe->debugfs_dpcm_root, &dpcm_params->state);
  746. #endif
  747. return 1;
  748. }
  749. static inline void be_reparent(struct snd_soc_pcm_runtime *fe,
  750. struct snd_soc_pcm_runtime *be, int stream)
  751. {
  752. struct snd_soc_dpcm_params *dpcm_params;
  753. struct snd_pcm_substream *fe_substream, *be_substream;
  754. /* reparent if BE is connected to other FEs */
  755. if (!be->dpcm[stream].users)
  756. return;
  757. be_substream = snd_soc_dpcm_get_substream(be, stream);
  758. list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
  759. if (dpcm_params->fe != fe) {
  760. dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
  761. stream ? "capture" : "playback",
  762. dpcm_params->fe->dai_link->name,
  763. stream ? "<-" : "->", dpcm_params->be->dai_link->name);
  764. fe_substream = snd_soc_dpcm_get_substream(dpcm_params->fe,
  765. stream);
  766. be_substream->runtime = fe_substream->runtime;
  767. break;
  768. }
  769. }
  770. }
  771. void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  772. {
  773. struct snd_soc_dpcm_params *dpcm_params, *d;
  774. list_for_each_entry_safe(dpcm_params, d, &fe->dpcm[stream].be_clients, list_be) {
  775. dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
  776. stream ? "capture" : "playback",
  777. dpcm_params->be->dai_link->name);
  778. if (dpcm_params->state == SND_SOC_DPCM_LINK_STATE_FREE) {
  779. dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
  780. stream ? "capture" : "playback", fe->dai_link->name,
  781. stream ? "<-" : "->", dpcm_params->be->dai_link->name);
  782. /* BEs still alive need new FE */
  783. be_reparent(fe, dpcm_params->be, stream);
  784. #ifdef CONFIG_DEBUG_FS
  785. debugfs_remove(dpcm_params->debugfs_state);
  786. #endif
  787. list_del(&dpcm_params->list_be);
  788. list_del(&dpcm_params->list_fe);
  789. kfree(dpcm_params);
  790. }
  791. }
  792. }
  793. static struct snd_soc_pcm_runtime *be_get_rtd(struct snd_soc_card *card,
  794. struct snd_soc_dapm_widget *widget)
  795. {
  796. struct snd_soc_pcm_runtime *be;
  797. int i;
  798. if (!widget->sname) {
  799. dev_err(card->dev, "widget %s has no stream\n", widget->name);
  800. return NULL;
  801. }
  802. for (i = 0; i < card->num_links; i++) {
  803. be = &card->rtd[i];
  804. if (!strcmp(widget->sname, be->dai_link->stream_name))
  805. return be;
  806. }
  807. dev_err(card->dev, "can't get BE for %s\n", widget->name);
  808. return NULL;
  809. }
  810. static struct snd_soc_dapm_widget *be_get_widget(struct snd_soc_card *card,
  811. struct snd_soc_pcm_runtime *rtd)
  812. {
  813. struct snd_soc_dapm_widget *widget;
  814. list_for_each_entry(widget, &card->widgets, list) {
  815. if (!widget->sname)
  816. continue;
  817. if (!strcmp(widget->sname, rtd->dai_link->stream_name))
  818. return widget;
  819. }
  820. dev_err(card->dev, "can't get widget for %s\n",
  821. rtd->dai_link->stream_name);
  822. return NULL;
  823. }
  824. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  825. struct snd_soc_dapm_widget *widget)
  826. {
  827. int i;
  828. for (i = 0; i < list->num_widgets; i++) {
  829. if (widget == list->widgets[i])
  830. return 1;
  831. }
  832. return 0;
  833. }
  834. int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  835. int stream, struct snd_soc_dapm_widget_list **list_)
  836. {
  837. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  838. struct snd_soc_dapm_widget_list *list;
  839. int paths;
  840. list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
  841. sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
  842. if (list == NULL)
  843. return -ENOMEM;
  844. /* get number of valid DAI paths and their widgets */
  845. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
  846. dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
  847. stream ? "capture" : "playback");
  848. *list_ = list;
  849. return paths;
  850. }
  851. static int be_prune_old(struct snd_soc_pcm_runtime *fe, int stream,
  852. struct snd_soc_dapm_widget_list **list_)
  853. {
  854. struct snd_soc_card *card = fe->card;
  855. struct snd_soc_dpcm_params *dpcm_params;
  856. struct snd_soc_dapm_widget_list *list = *list_;
  857. struct snd_soc_dapm_widget *widget;
  858. int old = 0;
  859. /* Destroy any old FE <--> BE connections */
  860. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  861. /* is there a valid widget for this BE */
  862. widget = be_get_widget(card, dpcm_params->be);
  863. if (!widget) {
  864. dev_err(fe->dev, "no widget found for %s\n",
  865. dpcm_params->be->dai_link->name);
  866. continue;
  867. }
  868. /* prune the BE if it's no longer in our active list */
  869. if (widget_in_list(list, widget))
  870. continue;
  871. dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
  872. stream ? "capture" : "playback", dpcm_params->be->dai_link->name,
  873. fe->dai_link->name);
  874. dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
  875. dpcm_params->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  876. old++;
  877. }
  878. dev_dbg(fe->dev, "found %d old BEs\n", old);
  879. return old;
  880. }
  881. static int be_add_new(struct snd_soc_pcm_runtime *fe, int stream,
  882. struct snd_soc_dapm_widget_list **list_)
  883. {
  884. struct snd_soc_card *card = fe->card;
  885. struct snd_soc_dapm_widget_list *list = *list_;
  886. enum snd_soc_dapm_type be_type;
  887. int i, new = 0, err;
  888. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  889. be_type = snd_soc_dapm_aif_out;
  890. else
  891. be_type = snd_soc_dapm_aif_in;
  892. /* Create any new FE <--> BE connections */
  893. for (i = 0; i < list->num_widgets; i++) {
  894. if (list->widgets[i]->id == be_type) {
  895. struct snd_soc_pcm_runtime *be;
  896. /* is there a valid BE rtd for this widget */
  897. be = be_get_rtd(card, list->widgets[i]);
  898. if (!be) {
  899. dev_err(fe->dev, "no BE found for %s\n",
  900. list->widgets[i]->name);
  901. continue;
  902. }
  903. /* don't connect if FE is not running */
  904. if (!fe->dpcm[stream].runtime && !fe->fe_compr)
  905. continue;
  906. /* newly connected FE and BE */
  907. err = be_connect(fe, be, stream);
  908. if (err < 0) {
  909. dev_err(fe->dev, "can't connect %s\n", list->widgets[i]->name);
  910. break;
  911. } else if (err == 0) /* already connected */
  912. continue;
  913. /* new */
  914. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  915. new++;
  916. }
  917. }
  918. dev_dbg(fe->dev, "found %d new BEs\n", new);
  919. return new;
  920. }
  921. /*
  922. * Find the corresponding BE DAIs that source or sink audio to this
  923. * FE substream.
  924. */
  925. int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  926. int stream, struct snd_soc_dapm_widget_list **list, int new)
  927. {
  928. if (new)
  929. return be_add_new(fe, stream, list);
  930. else
  931. return be_prune_old(fe, stream, list);
  932. return 0;
  933. }
  934. /*
  935. * Clear the runtime pending state of all BE's.
  936. */
  937. void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  938. {
  939. struct snd_soc_dpcm_params *dpcm_params;
  940. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
  941. dpcm_params->be->dpcm[stream].runtime_update =
  942. SND_SOC_DPCM_UPDATE_NO;
  943. }
  944. /* Unwind the BE startup */
  945. static void soc_dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, int stream)
  946. {
  947. struct snd_soc_dpcm_params *dpcm_params;
  948. /* disable any enabled and non active backends */
  949. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  950. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  951. struct snd_pcm_substream *be_substream =
  952. snd_soc_dpcm_get_substream(be, stream);
  953. if (be->dpcm[stream].users == 0)
  954. dev_err(be->dev, "no users %s at close - state %d\n",
  955. stream ? "capture" : "playback", be->dpcm[stream].state);
  956. if (--be->dpcm[stream].users != 0)
  957. continue;
  958. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  959. continue;
  960. soc_pcm_close(be_substream);
  961. be_substream->runtime = NULL;
  962. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  963. }
  964. }
  965. /* Startup all new BE */
  966. int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  967. {
  968. struct snd_soc_dpcm_params *dpcm_params;
  969. int err, count = 0;
  970. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  971. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  972. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  973. struct snd_pcm_substream *be_substream =
  974. snd_soc_dpcm_get_substream(be, stream);
  975. /* is this op for this BE ? */
  976. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  977. continue;
  978. /* first time the dpcm_params is open ? */
  979. if (be->dpcm[stream].users == MAX_BE_USERS)
  980. dev_err(be->dev, "too many users %s at open - state %d\n",
  981. stream ? "capture" : "playback", be->dpcm[stream].state);
  982. if (be->dpcm[stream].users++ != 0)
  983. continue;
  984. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  985. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  986. continue;
  987. dev_info(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
  988. be_substream->runtime = be->dpcm[stream].runtime;
  989. err = soc_pcm_open(be_substream);
  990. if (err < 0) {
  991. dev_err(be->dev, "BE open failed %d\n", err);
  992. be->dpcm[stream].users--;
  993. if (be->dpcm[stream].users < 0)
  994. dev_err(be->dev, "no users %s at unwind - state %d\n",
  995. stream ? "capture" : "playback",
  996. be->dpcm[stream].state);
  997. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  998. goto unwind;
  999. }
  1000. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1001. count++;
  1002. }
  1003. return count;
  1004. unwind:
  1005. /* disable any enabled and non active backends */
  1006. list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1007. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1008. struct snd_pcm_substream *be_substream =
  1009. snd_soc_dpcm_get_substream(be, stream);
  1010. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1011. continue;
  1012. if (be->dpcm[stream].users == 0)
  1013. dev_err(be->dev, "no users %s at close - state %d\n",
  1014. stream ? "capture" : "playback", be->dpcm[stream].state);
  1015. if (--be->dpcm[stream].users != 0)
  1016. continue;
  1017. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  1018. continue;
  1019. soc_pcm_close(be_substream);
  1020. be_substream->runtime = NULL;
  1021. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1022. }
  1023. return err;
  1024. }
  1025. void soc_dpcm_set_dynamic_runtime(struct snd_pcm_substream *substream)
  1026. {
  1027. struct snd_pcm_runtime *runtime = substream->runtime;
  1028. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1029. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1030. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  1031. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1032. runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
  1033. runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
  1034. runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
  1035. runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
  1036. runtime->hw.formats &= cpu_dai_drv->playback.formats;
  1037. runtime->hw.rates = cpu_dai_drv->playback.rates;
  1038. } else {
  1039. runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
  1040. runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
  1041. runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
  1042. runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
  1043. runtime->hw.formats &= cpu_dai_drv->capture.formats;
  1044. runtime->hw.rates = cpu_dai_drv->capture.rates;
  1045. }
  1046. }
  1047. static int soc_dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  1048. {
  1049. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1050. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  1051. int stream = fe_substream->stream, ret = 0;
  1052. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1053. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  1054. if (ret < 0) {
  1055. dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
  1056. goto be_err;
  1057. }
  1058. dev_info(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
  1059. /* start the DAI frontend */
  1060. ret = soc_pcm_open(fe_substream);
  1061. if (ret < 0) {
  1062. dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
  1063. goto unwind;
  1064. }
  1065. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1066. soc_dpcm_set_dynamic_runtime(fe_substream);
  1067. snd_pcm_limit_hw_rates(runtime);
  1068. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1069. return 0;
  1070. unwind:
  1071. soc_dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  1072. be_err:
  1073. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1074. return ret;
  1075. }
  1076. /* BE shutdown - called on DAPM sync updates (i.e. FE is already running)*/
  1077. int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1078. {
  1079. struct snd_soc_dpcm_params *dpcm_params;
  1080. /* only shutdown backends that are either sinks or sources to this frontend DAI */
  1081. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1082. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1083. struct snd_pcm_substream *be_substream =
  1084. snd_soc_dpcm_get_substream(be, stream);
  1085. /* is this op for this BE ? */
  1086. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1087. continue;
  1088. if (be->dpcm[stream].users == 0)
  1089. dev_err(be->dev, "no users %s at close - state %d\n",
  1090. stream ? "capture" : "playback", be->dpcm[stream].state);
  1091. if (--be->dpcm[stream].users != 0)
  1092. continue;
  1093. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1094. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
  1095. continue;
  1096. dev_info(be->dev, "dpcm: close BE %s\n",
  1097. dpcm_params->fe->dai_link->name);
  1098. soc_pcm_close(be_substream);
  1099. be_substream->runtime = NULL;
  1100. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1101. }
  1102. return 0;
  1103. }
  1104. /* FE +BE shutdown - called on FE PCM ops */
  1105. static int soc_dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  1106. {
  1107. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1108. int stream = substream->stream;
  1109. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1110. dev_info(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
  1111. /* now shutdown the frontend */
  1112. soc_pcm_close(substream);
  1113. /* shutdown the BEs */
  1114. dpcm_be_dai_shutdown(fe, substream->stream);
  1115. /* run the stream event for each BE */
  1116. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1117. dpcm_dapm_stream_event(fe, stream,
  1118. fe->cpu_dai->driver->playback.stream_name,
  1119. SND_SOC_DAPM_STREAM_STOP);
  1120. else
  1121. dpcm_dapm_stream_event(fe, stream,
  1122. fe->cpu_dai->driver->capture.stream_name,
  1123. SND_SOC_DAPM_STREAM_STOP);
  1124. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1125. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1126. return 0;
  1127. }
  1128. int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
  1129. {
  1130. struct snd_soc_dpcm_params *dpcm_params;
  1131. int ret;
  1132. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1133. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1134. struct snd_pcm_substream *be_substream =
  1135. snd_soc_dpcm_get_substream(be, stream);
  1136. /* is this op for this BE ? */
  1137. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1138. continue;
  1139. /* only allow hw_params() if no connected FEs are running */
  1140. if (!snd_soc_dpcm_can_be_params(fe, be, stream))
  1141. continue;
  1142. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1143. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1144. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
  1145. continue;
  1146. dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
  1147. dpcm_params->fe->dai_link->name);
  1148. /* copy params for each dpcm_params */
  1149. memcpy(&dpcm_params->hw_params, &fe->dpcm[stream].hw_params,
  1150. sizeof(struct snd_pcm_hw_params));
  1151. /* perform any hw_params fixups */
  1152. if (be->dai_link->be_hw_params_fixup) {
  1153. ret = be->dai_link->be_hw_params_fixup(be,
  1154. &dpcm_params->hw_params);
  1155. if (ret < 0) {
  1156. dev_err(be->dev,
  1157. "dpcm: hw_params BE fixup failed %d\n",
  1158. ret);
  1159. goto unwind;
  1160. }
  1161. }
  1162. ret = soc_pcm_hw_params(be_substream, &dpcm_params->hw_params);
  1163. if (ret < 0) {
  1164. dev_err(dpcm_params->be->dev, "dpcm: hw_params BE failed %d\n", ret);
  1165. goto unwind;
  1166. }
  1167. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1168. }
  1169. return 0;
  1170. unwind:
  1171. /* disable any enabled and non active backends */
  1172. list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1173. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1174. struct snd_pcm_substream *be_substream =
  1175. snd_soc_dpcm_get_substream(be, stream);
  1176. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1177. continue;
  1178. /* only allow hw_free() if no connected FEs are running */
  1179. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1180. continue;
  1181. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1182. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1183. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1184. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1185. continue;
  1186. soc_pcm_hw_free(be_substream);
  1187. }
  1188. return ret;
  1189. }
  1190. int soc_dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
  1191. struct snd_pcm_hw_params *params)
  1192. {
  1193. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1194. int ret, stream = substream->stream;
  1195. mutex_lock(&fe->card->dpcm_mutex);
  1196. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1197. memcpy(&fe->dpcm[substream->stream].hw_params, params,
  1198. sizeof(struct snd_pcm_hw_params));
  1199. ret = dpcm_be_dai_hw_params(fe, substream->stream);
  1200. if (ret < 0) {
  1201. dev_err(fe->dev,"dpcm: hw_params failed for some BEs %d\n", ret);
  1202. goto out;
  1203. }
  1204. dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
  1205. fe->dai_link->name, params_rate(params), params_channels(params),
  1206. params_format(params));
  1207. /* call hw_params on the frontend */
  1208. ret = soc_pcm_hw_params(substream, params);
  1209. if (ret < 0) {
  1210. dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
  1211. dpcm_be_dai_hw_free(fe, stream);
  1212. } else
  1213. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1214. out:
  1215. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1216. mutex_unlock(&fe->card->dpcm_mutex);
  1217. return ret;
  1218. }
  1219. static int dpcm_do_trigger(struct snd_soc_dpcm_params *dpcm_params,
  1220. struct snd_pcm_substream *substream, int cmd)
  1221. {
  1222. int ret;
  1223. dev_dbg(dpcm_params->be->dev, "dpcm: trigger BE %s cmd %d\n",
  1224. dpcm_params->fe->dai_link->name, cmd);
  1225. ret = soc_pcm_trigger(substream, cmd);
  1226. if (ret < 0)
  1227. dev_err(dpcm_params->be->dev,"dpcm: trigger BE failed %d\n", ret);
  1228. return ret;
  1229. }
  1230. int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
  1231. {
  1232. struct snd_soc_dpcm_params *dpcm_params;
  1233. int ret = 0;
  1234. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1235. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1236. struct snd_pcm_substream *be_substream =
  1237. snd_soc_dpcm_get_substream(be, stream);
  1238. /* is this op for this BE ? */
  1239. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1240. continue;
  1241. switch (cmd) {
  1242. case SNDRV_PCM_TRIGGER_START:
  1243. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1244. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1245. continue;
  1246. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1247. if (ret)
  1248. return ret;
  1249. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1250. break;
  1251. case SNDRV_PCM_TRIGGER_RESUME:
  1252. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1253. continue;
  1254. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1255. if (ret)
  1256. return ret;
  1257. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1258. break;
  1259. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1260. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  1261. continue;
  1262. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1263. if (ret)
  1264. return ret;
  1265. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1266. break;
  1267. case SNDRV_PCM_TRIGGER_STOP:
  1268. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1269. continue;
  1270. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1271. continue;
  1272. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1273. if (ret)
  1274. return ret;
  1275. be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1276. break;
  1277. case SNDRV_PCM_TRIGGER_SUSPEND:
  1278. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
  1279. continue;
  1280. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1281. continue;
  1282. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1283. if (ret)
  1284. return ret;
  1285. be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
  1286. break;
  1287. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1288. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1289. continue;
  1290. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1291. continue;
  1292. ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
  1293. if (ret)
  1294. return ret;
  1295. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1296. break;
  1297. }
  1298. }
  1299. return ret;
  1300. }
  1301. EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
  1302. int soc_dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  1303. {
  1304. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1305. int stream = substream->stream, ret;
  1306. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1307. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1308. switch (trigger) {
  1309. case SND_SOC_DPCM_TRIGGER_PRE:
  1310. /* call trigger on the frontend before the backend. */
  1311. dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
  1312. fe->dai_link->name, cmd);
  1313. ret = soc_pcm_trigger(substream, cmd);
  1314. if (ret < 0) {
  1315. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1316. goto out;
  1317. }
  1318. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1319. break;
  1320. case SND_SOC_DPCM_TRIGGER_POST:
  1321. /* call trigger on the frontend after the backend. */
  1322. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1323. if (ret < 0) {
  1324. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1325. goto out;
  1326. }
  1327. dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
  1328. fe->dai_link->name, cmd);
  1329. ret = soc_pcm_trigger(substream, cmd);
  1330. break;
  1331. case SND_SOC_DPCM_TRIGGER_BESPOKE:
  1332. /* bespoke trigger() - handles both FE and BEs */
  1333. dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
  1334. fe->dai_link->name, cmd);
  1335. ret = soc_pcm_bespoke_trigger(substream, cmd);
  1336. if (ret < 0) {
  1337. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1338. goto out;
  1339. }
  1340. break;
  1341. default:
  1342. dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
  1343. fe->dai_link->name);
  1344. ret = -EINVAL;
  1345. goto out;
  1346. }
  1347. switch (cmd) {
  1348. case SNDRV_PCM_TRIGGER_START:
  1349. case SNDRV_PCM_TRIGGER_RESUME:
  1350. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1351. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1352. break;
  1353. case SNDRV_PCM_TRIGGER_STOP:
  1354. case SNDRV_PCM_TRIGGER_SUSPEND:
  1355. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1356. break;
  1357. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1358. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1359. break;
  1360. }
  1361. out:
  1362. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1363. return ret;
  1364. }
  1365. int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
  1366. {
  1367. struct snd_soc_dpcm_params *dpcm_params;
  1368. int ret = 0;
  1369. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1370. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1371. struct snd_pcm_substream *be_substream =
  1372. snd_soc_dpcm_get_substream(be, stream);
  1373. /* is this op for this BE ? */
  1374. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1375. continue;
  1376. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1377. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1378. continue;
  1379. dev_dbg(be->dev, "dpcm: prepare BE %s\n",
  1380. dpcm_params->fe->dai_link->name);
  1381. ret = soc_pcm_prepare(be_substream);
  1382. if (ret < 0) {
  1383. dev_err(be->dev, "dpcm: backend prepare failed %d\n",
  1384. ret);
  1385. break;
  1386. }
  1387. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1388. }
  1389. return ret;
  1390. }
  1391. int soc_dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
  1392. {
  1393. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1394. int stream = substream->stream, ret = 0;
  1395. mutex_lock(&fe->card->dpcm_mutex);
  1396. dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
  1397. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1398. /* there is no point preparing this FE if there are no BEs */
  1399. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1400. dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
  1401. fe->dai_link->name);
  1402. ret = -EINVAL;
  1403. goto out;
  1404. }
  1405. ret = dpcm_be_dai_prepare(fe, substream->stream);
  1406. if (ret < 0) {
  1407. dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
  1408. fe->dai_link->name);
  1409. goto out;
  1410. }
  1411. /* call prepare on the frontend */
  1412. if (!fe->fe_compr) {
  1413. ret = soc_pcm_prepare(substream);
  1414. if (ret < 0) {
  1415. dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
  1416. fe->dai_link->name);
  1417. goto out;
  1418. }
  1419. }
  1420. /* run the stream event for each BE */
  1421. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1422. dpcm_dapm_stream_event(fe, stream,
  1423. fe->cpu_dai->driver->playback.stream_name,
  1424. SND_SOC_DAPM_STREAM_START);
  1425. else
  1426. dpcm_dapm_stream_event(fe, stream,
  1427. fe->cpu_dai->driver->capture.stream_name,
  1428. SND_SOC_DAPM_STREAM_START);
  1429. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1430. out:
  1431. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1432. mutex_unlock(&fe->card->dpcm_mutex);
  1433. return ret;
  1434. }
  1435. int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  1436. {
  1437. struct snd_soc_dpcm_params *dpcm_params;
  1438. /* only hw_params backends that are either sinks or sources
  1439. * to this frontend DAI */
  1440. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1441. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1442. struct snd_pcm_substream *be_substream =
  1443. snd_soc_dpcm_get_substream(be, stream);
  1444. /* is this op for this BE ? */
  1445. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1446. continue;
  1447. /* only free hw when no longer used - check all FEs */
  1448. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1449. continue;
  1450. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1451. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1452. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1453. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
  1454. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
  1455. !((be->dpcm[stream].state == SND_SOC_DPCM_STATE_START) &&
  1456. ((fe->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
  1457. (fe->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
  1458. (fe->dpcm[stream].state !=
  1459. SND_SOC_DPCM_STATE_SUSPEND))))
  1460. continue;
  1461. dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
  1462. dpcm_params->fe->dai_link->name);
  1463. soc_pcm_hw_free(be_substream);
  1464. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1465. }
  1466. return 0;
  1467. }
  1468. int soc_dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
  1469. {
  1470. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1471. int err, stream = substream->stream;
  1472. mutex_lock(&fe->card->dpcm_mutex);
  1473. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1474. dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
  1475. /* call hw_free on the frontend */
  1476. err = soc_pcm_hw_free(substream);
  1477. if (err < 0)
  1478. dev_err(fe->dev,"dpcm: hw_free FE %s failed\n", fe->dai_link->name);
  1479. /* only hw_params backends that are either sinks or sources
  1480. * to this frontend DAI */
  1481. err = dpcm_be_dai_hw_free(fe, stream);
  1482. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1483. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1484. mutex_unlock(&fe->card->dpcm_mutex);
  1485. return 0;
  1486. }
  1487. static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
  1488. unsigned int cmd, void *arg)
  1489. {
  1490. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1491. struct snd_soc_platform *platform = rtd->platform;
  1492. if (platform->driver->ops->ioctl)
  1493. return platform->driver->ops->ioctl(substream, cmd, arg);
  1494. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1495. }
  1496. static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1497. {
  1498. struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
  1499. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1500. int err;
  1501. dev_dbg(fe->dev, "runtime %s close on FE %s\n",
  1502. stream ? "capture" : "playback", fe->dai_link->name);
  1503. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  1504. /* call bespoke trigger - FE takes care of all BE triggers */
  1505. dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
  1506. fe->dai_link->name);
  1507. err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  1508. if (err < 0)
  1509. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
  1510. } else {
  1511. dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
  1512. fe->dai_link->name);
  1513. err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
  1514. if (err < 0)
  1515. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
  1516. }
  1517. err = dpcm_be_dai_hw_free(fe, stream);
  1518. if (err < 0)
  1519. dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
  1520. err = dpcm_be_dai_shutdown(fe, stream);
  1521. if (err < 0)
  1522. dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
  1523. /* run the stream event for each BE */
  1524. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1525. dpcm_dapm_stream_event(fe, stream,
  1526. fe->cpu_dai->driver->playback.stream_name,
  1527. SND_SOC_DAPM_STREAM_NOP);
  1528. else
  1529. dpcm_dapm_stream_event(fe, stream,
  1530. fe->cpu_dai->driver->capture.stream_name,
  1531. SND_SOC_DAPM_STREAM_NOP);
  1532. return 0;
  1533. }
  1534. static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
  1535. {
  1536. struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
  1537. struct snd_soc_dpcm_params *dpcm_params;
  1538. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1539. int ret;
  1540. dev_dbg(fe->dev, "runtime %s open on FE %s\n",
  1541. stream ? "capture" : "playback", fe->dai_link->name);
  1542. /* Only start the BE if the FE is ready */
  1543. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
  1544. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
  1545. return -EINVAL;
  1546. /* startup must always be called for new BEs */
  1547. ret = dpcm_be_dai_startup(fe, stream);
  1548. if (ret < 0) {
  1549. goto disconnect;
  1550. return ret;
  1551. }
  1552. /* keep going if FE state is > open */
  1553. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
  1554. return 0;
  1555. ret = dpcm_be_dai_hw_params(fe, stream);
  1556. if (ret < 0) {
  1557. goto close;
  1558. return ret;
  1559. }
  1560. /* keep going if FE state is > hw_params */
  1561. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
  1562. return 0;
  1563. ret = dpcm_be_dai_prepare(fe, stream);
  1564. if (ret < 0) {
  1565. goto hw_free;
  1566. return ret;
  1567. }
  1568. /* run the stream event for each BE */
  1569. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1570. dpcm_dapm_stream_event(fe, stream,
  1571. fe->cpu_dai->driver->playback.stream_name,
  1572. SND_SOC_DAPM_STREAM_NOP);
  1573. else
  1574. dpcm_dapm_stream_event(fe, stream,
  1575. fe->cpu_dai->driver->capture.stream_name,
  1576. SND_SOC_DAPM_STREAM_NOP);
  1577. /* keep going if FE state is > prepare */
  1578. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
  1579. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
  1580. return 0;
  1581. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  1582. /* call trigger on the frontend - FE takes care of all BE triggers */
  1583. dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
  1584. fe->dai_link->name);
  1585. ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
  1586. if (ret < 0) {
  1587. dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
  1588. goto hw_free;
  1589. }
  1590. } else {
  1591. dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
  1592. fe->dai_link->name);
  1593. ret = dpcm_be_dai_trigger(fe, stream,
  1594. SNDRV_PCM_TRIGGER_START);
  1595. if (ret < 0) {
  1596. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1597. goto hw_free;
  1598. }
  1599. }
  1600. return 0;
  1601. hw_free:
  1602. dpcm_be_dai_hw_free(fe, stream);
  1603. close:
  1604. dpcm_be_dai_shutdown(fe, stream);
  1605. disconnect:
  1606. /* disconnect any non started BEs */
  1607. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  1608. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1609. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1610. dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1611. }
  1612. return ret;
  1613. }
  1614. static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
  1615. {
  1616. int ret;
  1617. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1618. ret = dpcm_run_update_startup(fe, stream);
  1619. if (ret < 0)
  1620. dev_err(fe->dev, "failed to startup some BEs\n");
  1621. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1622. return ret;
  1623. }
  1624. static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
  1625. {
  1626. int ret;
  1627. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1628. ret = dpcm_run_update_shutdown(fe, stream);
  1629. if (ret < 0)
  1630. dev_err(fe->dev, "failed to shutdown some BEs\n");
  1631. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1632. return ret;
  1633. }
  1634. /* called when any mixer updates change FE -> BE the stream */
  1635. int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
  1636. {
  1637. struct snd_soc_card *card;
  1638. int i, ret = 0, old, new, paths;
  1639. if (widget->codec)
  1640. card = widget->codec->card;
  1641. else if (widget->platform)
  1642. card = widget->platform->card;
  1643. else
  1644. return -EINVAL;
  1645. mutex_lock(&card->dpcm_mutex);
  1646. for (i = 0; i < card->num_rtd; i++) {
  1647. struct snd_soc_dapm_widget_list *list;
  1648. struct snd_soc_pcm_runtime *fe = &card->rtd[i];
  1649. /* make sure link is FE */
  1650. if (!fe->dai_link->dynamic)
  1651. continue;
  1652. /* only check active links */
  1653. if (!fe->cpu_dai->active)
  1654. continue;
  1655. /* DAPM sync will call this to update DSP paths */
  1656. dev_dbg(fe->dev, "DPCM runtime update for FE %s\n", fe->dai_link->name);
  1657. /* skip if FE doesn't have playback capability */
  1658. if (!fe->cpu_dai->driver->playback.channels_min)
  1659. goto capture;
  1660. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
  1661. if (paths < 0) {
  1662. dev_warn(fe->dev, "%s no valid %s route from source to sink\n",
  1663. fe->dai_link->name, "playback");
  1664. ret = paths;
  1665. goto out;
  1666. }
  1667. /* update any new playback paths */
  1668. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
  1669. if (new) {
  1670. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1671. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1672. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1673. }
  1674. /* update any old playback paths */
  1675. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
  1676. if (old) {
  1677. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1678. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1679. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1680. }
  1681. dpcm_path_put(&list);
  1682. capture:
  1683. /* skip if FE doesn't have capture capability */
  1684. if (!fe->cpu_dai->driver->capture.channels_min)
  1685. continue;
  1686. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
  1687. if (paths < 0) {
  1688. dev_warn(fe->dev, "%s no valid %s route from source to sink\n",
  1689. fe->dai_link->name, "capture");
  1690. ret = paths;
  1691. goto out;
  1692. }
  1693. /* update any new capture paths */
  1694. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
  1695. if (new) {
  1696. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1697. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1698. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1699. }
  1700. /* update any old capture paths */
  1701. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
  1702. if (old) {
  1703. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1704. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1705. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1706. }
  1707. dpcm_path_put(&list);
  1708. }
  1709. out:
  1710. mutex_unlock(&card->dpcm_mutex);
  1711. return ret;
  1712. }
  1713. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  1714. {
  1715. struct snd_soc_dpcm_params *dpcm_params;
  1716. list_for_each_entry(dpcm_params,
  1717. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1718. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1719. struct snd_soc_dai *dai = be->codec_dai;
  1720. struct snd_soc_dai_driver *drv = dai->driver;
  1721. if (be->dai_link->ignore_suspend)
  1722. continue;
  1723. dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
  1724. if (drv->ops->digital_mute && dai->playback_active)
  1725. drv->ops->digital_mute(dai, mute);
  1726. }
  1727. return 0;
  1728. }
  1729. int soc_dpcm_be_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
  1730. {
  1731. struct snd_soc_dpcm_params *dpcm_params;
  1732. /* suspend for playback */
  1733. list_for_each_entry(dpcm_params,
  1734. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1735. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1736. struct snd_soc_dai *dai = be->cpu_dai;
  1737. struct snd_soc_dai_driver *drv = dai->driver;
  1738. if (be->dai_link->ignore_suspend)
  1739. continue;
  1740. dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
  1741. be->dai_link->name);
  1742. if (drv->suspend && !drv->ac97_control)
  1743. drv->suspend(dai);
  1744. }
  1745. /* suspend for capture */
  1746. list_for_each_entry(dpcm_params,
  1747. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1748. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1749. struct snd_soc_dai *dai = be->cpu_dai;
  1750. struct snd_soc_dai_driver *drv = dai->driver;
  1751. if (be->dai_link->ignore_suspend)
  1752. continue;
  1753. dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
  1754. be->dai_link->name);
  1755. if (drv->suspend && !drv->ac97_control)
  1756. drv->suspend(dai);
  1757. }
  1758. return 0;
  1759. }
  1760. int soc_dpcm_be_ac97_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
  1761. {
  1762. struct snd_soc_dpcm_params *dpcm_params;
  1763. /* suspend for playback */
  1764. list_for_each_entry(dpcm_params,
  1765. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1766. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1767. struct snd_soc_dai *dai = be->cpu_dai;
  1768. struct snd_soc_dai_driver *drv = dai->driver;
  1769. if (be->dai_link->ignore_suspend)
  1770. continue;
  1771. dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
  1772. be->dai_link->name);
  1773. if (drv->suspend && drv->ac97_control)
  1774. drv->suspend(dai);
  1775. }
  1776. /* suspend for capture */
  1777. list_for_each_entry(dpcm_params,
  1778. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1779. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1780. struct snd_soc_dai *dai = be->cpu_dai;
  1781. struct snd_soc_dai_driver *drv = dai->driver;
  1782. if (be->dai_link->ignore_suspend)
  1783. continue;
  1784. dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
  1785. be->dai_link->name);
  1786. if (drv->suspend && drv->ac97_control)
  1787. drv->suspend(dai);
  1788. }
  1789. return 0;
  1790. }
  1791. int soc_dpcm_be_platform_suspend(struct snd_soc_pcm_runtime *fe)
  1792. {
  1793. struct snd_soc_dpcm_params *dpcm_params;
  1794. /* suspend for playback */
  1795. list_for_each_entry(dpcm_params,
  1796. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1797. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1798. struct snd_soc_platform *platform = be->platform;
  1799. struct snd_soc_platform_driver *drv = platform->driver;
  1800. struct snd_soc_dai *dai = be->cpu_dai;
  1801. if (be->dai_link->ignore_suspend)
  1802. continue;
  1803. dev_dbg(be->dev, "pm: BE platform playback suspend %s\n",
  1804. be->dai_link->name);
  1805. if (drv->suspend && !platform->suspended) {
  1806. drv->suspend(dai);
  1807. platform->suspended = 1;
  1808. }
  1809. }
  1810. /* suspend for capture */
  1811. list_for_each_entry(dpcm_params,
  1812. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1813. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1814. struct snd_soc_platform *platform = be->platform;
  1815. struct snd_soc_platform_driver *drv = platform->driver;
  1816. struct snd_soc_dai *dai = be->cpu_dai;
  1817. if (be->dai_link->ignore_suspend)
  1818. continue;
  1819. dev_dbg(be->dev, "pm: BE platform capture suspend %s\n",
  1820. be->dai_link->name);
  1821. if (drv->suspend && !platform->suspended) {
  1822. drv->suspend(dai);
  1823. platform->suspended = 1;
  1824. }
  1825. }
  1826. return 0;
  1827. }
  1828. int soc_dpcm_fe_suspend(struct snd_soc_pcm_runtime *fe)
  1829. {
  1830. struct snd_soc_dai *dai = fe->cpu_dai;
  1831. struct snd_soc_dai_driver *dai_drv = dai->driver;
  1832. struct snd_soc_platform *platform = fe->platform;
  1833. struct snd_soc_platform_driver *plat_drv = platform->driver;
  1834. if (dai_drv->suspend && !dai_drv->ac97_control)
  1835. dai_drv->suspend(dai);
  1836. if (plat_drv->suspend && !platform->suspended) {
  1837. plat_drv->suspend(dai);
  1838. platform->suspended = 1;
  1839. }
  1840. soc_dpcm_be_cpu_dai_suspend(fe);
  1841. soc_dpcm_be_platform_suspend(fe);
  1842. return 0;
  1843. }
  1844. int soc_dpcm_be_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
  1845. {
  1846. struct snd_soc_dpcm_params *dpcm_params;
  1847. /* resume for playback */
  1848. list_for_each_entry(dpcm_params,
  1849. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1850. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1851. struct snd_soc_dai *dai = be->cpu_dai;
  1852. struct snd_soc_dai_driver *drv = dai->driver;
  1853. if (be->dai_link->ignore_suspend)
  1854. continue;
  1855. dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
  1856. be->dai_link->name);
  1857. if (drv->resume && !drv->ac97_control)
  1858. drv->resume(dai);
  1859. }
  1860. /* suspend for capture */
  1861. list_for_each_entry(dpcm_params,
  1862. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1863. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1864. struct snd_soc_dai *dai = be->cpu_dai;
  1865. struct snd_soc_dai_driver *drv = dai->driver;
  1866. if (be->dai_link->ignore_suspend)
  1867. continue;
  1868. dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
  1869. be->dai_link->name);
  1870. if (drv->resume && !drv->ac97_control)
  1871. drv->resume(dai);
  1872. }
  1873. return 0;
  1874. }
  1875. int soc_dpcm_be_ac97_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
  1876. {
  1877. struct snd_soc_dpcm_params *dpcm_params;
  1878. /* resume for playback */
  1879. list_for_each_entry(dpcm_params,
  1880. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1881. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1882. struct snd_soc_dai *dai = be->cpu_dai;
  1883. struct snd_soc_dai_driver *drv = dai->driver;
  1884. if (be->dai_link->ignore_suspend)
  1885. continue;
  1886. dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
  1887. be->dai_link->name);
  1888. if (drv->resume && drv->ac97_control)
  1889. drv->resume(dai);
  1890. }
  1891. /* suspend for capture */
  1892. list_for_each_entry(dpcm_params,
  1893. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1894. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1895. struct snd_soc_dai *dai = be->cpu_dai;
  1896. struct snd_soc_dai_driver *drv = dai->driver;
  1897. if (be->dai_link->ignore_suspend)
  1898. continue;
  1899. dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
  1900. be->dai_link->name);
  1901. if (drv->resume && drv->ac97_control)
  1902. drv->resume(dai);
  1903. }
  1904. return 0;
  1905. }
  1906. int soc_dpcm_be_platform_resume(struct snd_soc_pcm_runtime *fe)
  1907. {
  1908. struct snd_soc_dpcm_params *dpcm_params;
  1909. /* resume for playback */
  1910. list_for_each_entry(dpcm_params,
  1911. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
  1912. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1913. struct snd_soc_platform *platform = be->platform;
  1914. struct snd_soc_platform_driver *drv = platform->driver;
  1915. struct snd_soc_dai *dai = be->cpu_dai;
  1916. if (be->dai_link->ignore_suspend)
  1917. continue;
  1918. dev_dbg(be->dev, "pm: BE platform playback resume %s\n",
  1919. be->dai_link->name);
  1920. if (drv->resume && platform->suspended) {
  1921. drv->resume(dai);
  1922. platform->suspended = 0;
  1923. }
  1924. }
  1925. /* resume for capture */
  1926. list_for_each_entry(dpcm_params,
  1927. &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
  1928. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  1929. struct snd_soc_platform *platform = be->platform;
  1930. struct snd_soc_platform_driver *drv = platform->driver;
  1931. struct snd_soc_dai *dai = be->cpu_dai;
  1932. if (be->dai_link->ignore_suspend)
  1933. continue;
  1934. dev_dbg(be->dev, "pm: BE platform capture resume %s\n",
  1935. be->dai_link->name);
  1936. if (drv->resume && platform->suspended) {
  1937. drv->resume(dai);
  1938. platform->suspended = 0;
  1939. }
  1940. }
  1941. return 0;
  1942. }
  1943. int soc_dpcm_fe_resume(struct snd_soc_pcm_runtime *fe)
  1944. {
  1945. struct snd_soc_dai *dai = fe->cpu_dai;
  1946. struct snd_soc_dai_driver *dai_drv = dai->driver;
  1947. struct snd_soc_platform *platform = fe->platform;
  1948. struct snd_soc_platform_driver *plat_drv = platform->driver;
  1949. soc_dpcm_be_cpu_dai_resume(fe);
  1950. soc_dpcm_be_platform_resume(fe);
  1951. if (dai_drv->resume && !dai_drv->ac97_control)
  1952. dai_drv->resume(dai);
  1953. if (plat_drv->resume && platform->suspended) {
  1954. plat_drv->resume(dai);
  1955. platform->suspended = 0;
  1956. }
  1957. return 0;
  1958. }
  1959. /* called when opening FE stream */
  1960. int soc_dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  1961. {
  1962. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1963. struct snd_soc_dpcm_params *dpcm_params;
  1964. struct snd_soc_dapm_widget_list *list;
  1965. int ret;
  1966. int stream = fe_substream->stream;
  1967. mutex_lock(&fe->card->dpcm_mutex);
  1968. fe->dpcm[stream].runtime = fe_substream->runtime;
  1969. if (dpcm_path_get(fe, stream, &list) <= 0) {
  1970. dev_warn(fe->dev, "asoc: %s no valid %s route from source to sink\n",
  1971. fe->dai_link->name, stream ? "capture" : "playback");
  1972. mutex_unlock(&fe->card->dpcm_mutex);
  1973. return -EINVAL;
  1974. }
  1975. /* calculate valid and active FE <-> BE dpcm_paramss */
  1976. dpcm_process_paths(fe, stream, &list, 1);
  1977. ret = soc_dpcm_fe_dai_startup(fe_substream);
  1978. if (ret < 0) {
  1979. /* clean up all links */
  1980. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
  1981. dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1982. dpcm_be_disconnect(fe, stream);
  1983. fe->dpcm[stream].runtime = NULL;
  1984. }
  1985. dpcm_clear_pending_state(fe, stream);
  1986. dpcm_path_put(&list);
  1987. mutex_unlock(&fe->card->dpcm_mutex);
  1988. return ret;
  1989. }
  1990. /* called when closing FE stream */
  1991. int soc_dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  1992. {
  1993. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1994. struct snd_soc_dpcm_params *dpcm_params;
  1995. int stream = fe_substream->stream, ret;
  1996. mutex_lock(&fe->card->dpcm_mutex);
  1997. ret = soc_dpcm_fe_dai_shutdown(fe_substream);
  1998. /* mark FE's links ready to prune */
  1999. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
  2000. dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2001. dpcm_be_disconnect(fe, stream);
  2002. fe->dpcm[stream].runtime = NULL;
  2003. mutex_unlock(&fe->card->dpcm_mutex);
  2004. return ret;
  2005. }
  2006. /* create a new pcm */
  2007. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  2008. {
  2009. struct snd_soc_codec *codec = rtd->codec;
  2010. struct snd_soc_platform *platform = rtd->platform;
  2011. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  2012. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2013. struct snd_pcm_substream *substream[2];
  2014. struct snd_pcm *pcm;
  2015. char new_name[64];
  2016. int ret = 0, playback = 0, capture = 0;
  2017. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  2018. if (cpu_dai->driver->playback.channels_min)
  2019. playback = 1;
  2020. if (cpu_dai->driver->capture.channels_min)
  2021. capture = 1;
  2022. } else {
  2023. if (codec_dai->driver->playback.channels_min)
  2024. playback = 1;
  2025. if (codec_dai->driver->capture.channels_min)
  2026. capture = 1;
  2027. }
  2028. /* create the PCM */
  2029. if (rtd->dai_link->no_pcm) {
  2030. snprintf(new_name, sizeof(new_name), "(%s)",
  2031. rtd->dai_link->stream_name);
  2032. ret = snd_pcm_new_soc_be(rtd->card->snd_card, new_name, num,
  2033. playback, capture, &pcm);
  2034. } else {
  2035. if (rtd->dai_link->dynamic)
  2036. snprintf(new_name, sizeof(new_name), "%s (*)",
  2037. rtd->dai_link->stream_name);
  2038. else
  2039. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  2040. rtd->dai_link->stream_name, codec_dai->name, num);
  2041. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  2042. capture, &pcm);
  2043. }
  2044. if (ret < 0) {
  2045. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  2046. return ret;
  2047. }
  2048. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
  2049. rtd->pcm = pcm;
  2050. pcm->private_data = rtd;
  2051. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  2052. substream[SNDRV_PCM_STREAM_PLAYBACK] =
  2053. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  2054. substream[SNDRV_PCM_STREAM_CAPTURE] =
  2055. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  2056. if (rtd->dai_link->no_pcm) {
  2057. if (playback)
  2058. substream[SNDRV_PCM_STREAM_PLAYBACK]->private_data = rtd;
  2059. if (capture)
  2060. substream[SNDRV_PCM_STREAM_CAPTURE]->private_data = rtd;
  2061. goto out;
  2062. }
  2063. /* setup any hostless PCMs - i.e. no host IO is performed */
  2064. if (rtd->dai_link->no_host_mode) {
  2065. if (substream[SNDRV_PCM_STREAM_PLAYBACK]) {
  2066. substream[SNDRV_PCM_STREAM_PLAYBACK]->hw_no_buffer = 1;
  2067. snd_soc_set_runtime_hwparams(
  2068. substream[SNDRV_PCM_STREAM_PLAYBACK],
  2069. &no_host_hardware);
  2070. }
  2071. if (substream[SNDRV_PCM_STREAM_CAPTURE]) {
  2072. substream[SNDRV_PCM_STREAM_CAPTURE]->hw_no_buffer = 1;
  2073. snd_soc_set_runtime_hwparams(
  2074. substream[SNDRV_PCM_STREAM_CAPTURE],
  2075. &no_host_hardware);
  2076. }
  2077. }
  2078. /* ASoC PCM operations */
  2079. if (rtd->dai_link->dynamic) {
  2080. rtd->ops.open = soc_dpcm_fe_dai_open;
  2081. rtd->ops.hw_params = soc_dpcm_fe_dai_hw_params;
  2082. rtd->ops.prepare = soc_dpcm_fe_dai_prepare;
  2083. rtd->ops.trigger = soc_dpcm_fe_dai_trigger;
  2084. rtd->ops.hw_free = soc_dpcm_fe_dai_hw_free;
  2085. rtd->ops.close = soc_dpcm_fe_dai_close;
  2086. rtd->ops.pointer = soc_pcm_pointer;
  2087. rtd->ops.ioctl = soc_pcm_ioctl;
  2088. } else {
  2089. rtd->ops.open = soc_pcm_open;
  2090. rtd->ops.hw_params = soc_pcm_hw_params;
  2091. rtd->ops.prepare = soc_pcm_prepare;
  2092. rtd->ops.trigger = soc_pcm_trigger;
  2093. rtd->ops.hw_free = soc_pcm_hw_free;
  2094. rtd->ops.close = soc_pcm_close;
  2095. rtd->ops.pointer = soc_pcm_pointer;
  2096. rtd->ops.ioctl = soc_pcm_ioctl;
  2097. }
  2098. if (platform->driver->ops) {
  2099. rtd->ops.ack = platform->driver->ops->ack;
  2100. rtd->ops.copy = platform->driver->ops->copy;
  2101. rtd->ops.silence = platform->driver->ops->silence;
  2102. rtd->ops.page = platform->driver->ops->page;
  2103. rtd->ops.mmap = platform->driver->ops->mmap;
  2104. rtd->ops.restart = platform->driver->ops->restart;
  2105. }
  2106. if (playback)
  2107. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  2108. if (capture)
  2109. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  2110. if (platform->driver->pcm_new) {
  2111. ret = platform->driver->pcm_new(rtd);
  2112. if (ret < 0) {
  2113. pr_err("asoc: platform pcm constructor failed\n");
  2114. return ret;
  2115. }
  2116. }
  2117. pcm->private_free = platform->driver->pcm_free;
  2118. out:
  2119. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  2120. cpu_dai->name);
  2121. return ret;
  2122. }
  2123. #ifdef CONFIG_DEBUG_FS
  2124. static char *dpcm_state_string(enum snd_soc_dpcm_state state)
  2125. {
  2126. switch (state) {
  2127. case SND_SOC_DPCM_STATE_NEW:
  2128. return "new";
  2129. case SND_SOC_DPCM_STATE_OPEN:
  2130. return "open";
  2131. case SND_SOC_DPCM_STATE_HW_PARAMS:
  2132. return "hw_params";
  2133. case SND_SOC_DPCM_STATE_PREPARE:
  2134. return "prepare";
  2135. case SND_SOC_DPCM_STATE_START:
  2136. return "start";
  2137. case SND_SOC_DPCM_STATE_STOP:
  2138. return "stop";
  2139. case SND_SOC_DPCM_STATE_SUSPEND:
  2140. return "suspend";
  2141. case SND_SOC_DPCM_STATE_PAUSED:
  2142. return "paused";
  2143. case SND_SOC_DPCM_STATE_HW_FREE:
  2144. return "hw_free";
  2145. case SND_SOC_DPCM_STATE_CLOSE:
  2146. return "close";
  2147. }
  2148. return "unknown";
  2149. }
  2150. static int soc_dpcm_state_open_file(struct inode *inode, struct file *file)
  2151. {
  2152. file->private_data = inode->i_private;
  2153. return 0;
  2154. }
  2155. static ssize_t soc_dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  2156. int stream, char *buf, size_t size)
  2157. {
  2158. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  2159. struct snd_soc_dpcm_params *dpcm_params;
  2160. ssize_t offset = 0;
  2161. /* FE state */
  2162. offset += snprintf(buf + offset, size - offset,
  2163. "[%s - %s]\n", fe->dai_link->name,
  2164. stream ? "Capture" : "Playback");
  2165. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  2166. dpcm_state_string(fe->dpcm[stream].state));
  2167. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2168. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2169. offset += snprintf(buf + offset, size - offset,
  2170. "Hardware Params: "
  2171. "Format = %s, Channels = %d, Rate = %d\n",
  2172. snd_pcm_format_name(params_format(params)),
  2173. params_channels(params),
  2174. params_rate(params));
  2175. /* BEs state */
  2176. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  2177. if (list_empty(&fe->dpcm[stream].be_clients)) {
  2178. offset += snprintf(buf + offset, size - offset,
  2179. " No active DSP links\n");
  2180. goto out;
  2181. }
  2182. list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
  2183. struct snd_soc_pcm_runtime *be = dpcm_params->be;
  2184. offset += snprintf(buf + offset, size - offset,
  2185. "- %s\n", be->dai_link->name);
  2186. offset += snprintf(buf + offset, size - offset,
  2187. " State: %s\n",
  2188. dpcm_state_string(fe->dpcm[stream].state));
  2189. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2190. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2191. offset += snprintf(buf + offset, size - offset,
  2192. " Hardware Params: "
  2193. "Format = %s, Channels = %d, Rate = %d\n",
  2194. snd_pcm_format_name(params_format(params)),
  2195. params_channels(params),
  2196. params_rate(params));
  2197. }
  2198. out:
  2199. return offset;
  2200. }
  2201. static ssize_t soc_dpcm_state_read_file(struct file *file, char __user *user_buf,
  2202. size_t count, loff_t *ppos)
  2203. {
  2204. struct snd_soc_pcm_runtime *fe = file->private_data;
  2205. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  2206. char *buf;
  2207. buf = kmalloc(out_count, GFP_KERNEL);
  2208. if (!buf)
  2209. return -ENOMEM;
  2210. if (fe->cpu_dai->driver->playback.channels_min)
  2211. offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  2212. buf + offset, out_count - offset);
  2213. if (fe->cpu_dai->driver->capture.channels_min)
  2214. offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  2215. buf + offset, out_count - offset);
  2216. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  2217. kfree(buf);
  2218. return ret;
  2219. }
  2220. static const struct file_operations soc_dpcm_state_fops = {
  2221. .open = soc_dpcm_state_open_file,
  2222. .read = soc_dpcm_state_read_file,
  2223. .llseek = default_llseek,
  2224. };
  2225. int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  2226. {
  2227. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  2228. rtd->card->debugfs_card_root);
  2229. if (!rtd->debugfs_dpcm_root) {
  2230. dev_dbg(rtd->dev,
  2231. "ASoC: Failed to create dpcm debugfs directory %s\n",
  2232. rtd->dai_link->name);
  2233. return -EINVAL;
  2234. }
  2235. rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
  2236. rtd->debugfs_dpcm_root,
  2237. rtd, &soc_dpcm_state_fops);
  2238. return 0;
  2239. }
  2240. #endif