hda_codec.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/pci.h>
  25. #include <linux/mutex.h>
  26. #include <sound/core.h>
  27. #include "hda_codec.h"
  28. #include <sound/asoundef.h>
  29. #include <sound/tlv.h>
  30. #include <sound/initval.h>
  31. #include "hda_local.h"
  32. #include <sound/hda_hwdep.h>
  33. #include "hda_patch.h" /* codec presets */
  34. /*
  35. * vendor / preset table
  36. */
  37. struct hda_vendor_id {
  38. unsigned int id;
  39. const char *name;
  40. };
  41. /* codec vendor labels */
  42. static struct hda_vendor_id hda_vendor_ids[] = {
  43. { 0x1002, "ATI" },
  44. { 0x1057, "Motorola" },
  45. { 0x1095, "Silicon Image" },
  46. { 0x10ec, "Realtek" },
  47. { 0x1106, "VIA" },
  48. { 0x111d, "IDT" },
  49. { 0x11c1, "LSI" },
  50. { 0x11d4, "Analog Devices" },
  51. { 0x13f6, "C-Media" },
  52. { 0x14f1, "Conexant" },
  53. { 0x17e8, "Chrontel" },
  54. { 0x1854, "LG" },
  55. { 0x1aec, "Wolfson Microelectronics" },
  56. { 0x434d, "C-Media" },
  57. { 0x8384, "SigmaTel" },
  58. {} /* terminator */
  59. };
  60. static const struct hda_codec_preset *hda_preset_tables[] = {
  61. #ifdef CONFIG_SND_HDA_CODEC_REALTEK
  62. snd_hda_preset_realtek,
  63. #endif
  64. #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
  65. snd_hda_preset_cmedia,
  66. #endif
  67. #ifdef CONFIG_SND_HDA_CODEC_ANALOG
  68. snd_hda_preset_analog,
  69. #endif
  70. #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
  71. snd_hda_preset_sigmatel,
  72. #endif
  73. #ifdef CONFIG_SND_HDA_CODEC_SI3054
  74. snd_hda_preset_si3054,
  75. #endif
  76. #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
  77. snd_hda_preset_atihdmi,
  78. #endif
  79. #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
  80. snd_hda_preset_conexant,
  81. #endif
  82. #ifdef CONFIG_SND_HDA_CODEC_VIA
  83. snd_hda_preset_via,
  84. #endif
  85. #ifdef CONFIG_SND_HDA_CODEC_NVHDMI
  86. snd_hda_preset_nvhdmi,
  87. #endif
  88. #ifdef CONFIG_SND_HDA_CODEC_INTELHDMI
  89. snd_hda_preset_intelhdmi,
  90. #endif
  91. NULL
  92. };
  93. #ifdef CONFIG_SND_HDA_POWER_SAVE
  94. static void hda_power_work(struct work_struct *work);
  95. static void hda_keep_power_on(struct hda_codec *codec);
  96. #else
  97. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  98. #endif
  99. const char *snd_hda_get_jack_location(u32 cfg)
  100. {
  101. static char *bases[7] = {
  102. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  103. };
  104. static unsigned char specials_idx[] = {
  105. 0x07, 0x08,
  106. 0x17, 0x18, 0x19,
  107. 0x37, 0x38
  108. };
  109. static char *specials[] = {
  110. "Rear Panel", "Drive Bar",
  111. "Riser", "HDMI", "ATAPI",
  112. "Mobile-In", "Mobile-Out"
  113. };
  114. int i;
  115. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  116. if ((cfg & 0x0f) < 7)
  117. return bases[cfg & 0x0f];
  118. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  119. if (cfg == specials_idx[i])
  120. return specials[i];
  121. }
  122. return "UNKNOWN";
  123. }
  124. const char *snd_hda_get_jack_connectivity(u32 cfg)
  125. {
  126. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  127. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  128. }
  129. const char *snd_hda_get_jack_type(u32 cfg)
  130. {
  131. static char *jack_types[16] = {
  132. "Line Out", "Speaker", "HP Out", "CD",
  133. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  134. "Line In", "Aux", "Mic", "Telephony",
  135. "SPDIF In", "Digitial In", "Reserved", "Other"
  136. };
  137. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  138. >> AC_DEFCFG_DEVICE_SHIFT];
  139. }
  140. /*
  141. * Compose a 32bit command word to be sent to the HD-audio controller
  142. */
  143. static inline unsigned int
  144. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  145. unsigned int verb, unsigned int parm)
  146. {
  147. u32 val;
  148. val = (u32)(codec->addr & 0x0f) << 28;
  149. val |= (u32)direct << 27;
  150. val |= (u32)nid << 20;
  151. val |= verb << 8;
  152. val |= parm;
  153. return val;
  154. }
  155. /**
  156. * snd_hda_codec_read - send a command and get the response
  157. * @codec: the HDA codec
  158. * @nid: NID to send the command
  159. * @direct: direct flag
  160. * @verb: the verb to send
  161. * @parm: the parameter for the verb
  162. *
  163. * Send a single command and read the corresponding response.
  164. *
  165. * Returns the obtained response value, or -1 for an error.
  166. */
  167. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  168. int direct,
  169. unsigned int verb, unsigned int parm)
  170. {
  171. struct hda_bus *bus = codec->bus;
  172. unsigned int res;
  173. res = make_codec_cmd(codec, nid, direct, verb, parm);
  174. snd_hda_power_up(codec);
  175. mutex_lock(&bus->cmd_mutex);
  176. if (!bus->ops.command(bus, res))
  177. res = bus->ops.get_response(bus);
  178. else
  179. res = (unsigned int)-1;
  180. mutex_unlock(&bus->cmd_mutex);
  181. snd_hda_power_down(codec);
  182. return res;
  183. }
  184. /**
  185. * snd_hda_codec_write - send a single command without waiting for response
  186. * @codec: the HDA codec
  187. * @nid: NID to send the command
  188. * @direct: direct flag
  189. * @verb: the verb to send
  190. * @parm: the parameter for the verb
  191. *
  192. * Send a single command without waiting for response.
  193. *
  194. * Returns 0 if successful, or a negative error code.
  195. */
  196. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  197. unsigned int verb, unsigned int parm)
  198. {
  199. struct hda_bus *bus = codec->bus;
  200. unsigned int res;
  201. int err;
  202. res = make_codec_cmd(codec, nid, direct, verb, parm);
  203. snd_hda_power_up(codec);
  204. mutex_lock(&bus->cmd_mutex);
  205. err = bus->ops.command(bus, res);
  206. mutex_unlock(&bus->cmd_mutex);
  207. snd_hda_power_down(codec);
  208. return err;
  209. }
  210. /**
  211. * snd_hda_sequence_write - sequence writes
  212. * @codec: the HDA codec
  213. * @seq: VERB array to send
  214. *
  215. * Send the commands sequentially from the given array.
  216. * The array must be terminated with NID=0.
  217. */
  218. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  219. {
  220. for (; seq->nid; seq++)
  221. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  222. }
  223. /**
  224. * snd_hda_get_sub_nodes - get the range of sub nodes
  225. * @codec: the HDA codec
  226. * @nid: NID to parse
  227. * @start_id: the pointer to store the start NID
  228. *
  229. * Parse the NID and store the start NID of its sub-nodes.
  230. * Returns the number of sub-nodes.
  231. */
  232. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  233. hda_nid_t *start_id)
  234. {
  235. unsigned int parm;
  236. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  237. if (parm == -1)
  238. return 0;
  239. *start_id = (parm >> 16) & 0x7fff;
  240. return (int)(parm & 0x7fff);
  241. }
  242. /**
  243. * snd_hda_get_connections - get connection list
  244. * @codec: the HDA codec
  245. * @nid: NID to parse
  246. * @conn_list: connection list array
  247. * @max_conns: max. number of connections to store
  248. *
  249. * Parses the connection list of the given widget and stores the list
  250. * of NIDs.
  251. *
  252. * Returns the number of connections, or a negative error code.
  253. */
  254. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  255. hda_nid_t *conn_list, int max_conns)
  256. {
  257. unsigned int parm;
  258. int i, conn_len, conns;
  259. unsigned int shift, num_elems, mask;
  260. hda_nid_t prev_nid;
  261. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  262. return -EINVAL;
  263. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  264. if (parm & AC_CLIST_LONG) {
  265. /* long form */
  266. shift = 16;
  267. num_elems = 2;
  268. } else {
  269. /* short form */
  270. shift = 8;
  271. num_elems = 4;
  272. }
  273. conn_len = parm & AC_CLIST_LENGTH;
  274. mask = (1 << (shift-1)) - 1;
  275. if (!conn_len)
  276. return 0; /* no connection */
  277. if (conn_len == 1) {
  278. /* single connection */
  279. parm = snd_hda_codec_read(codec, nid, 0,
  280. AC_VERB_GET_CONNECT_LIST, 0);
  281. conn_list[0] = parm & mask;
  282. return 1;
  283. }
  284. /* multi connection */
  285. conns = 0;
  286. prev_nid = 0;
  287. for (i = 0; i < conn_len; i++) {
  288. int range_val;
  289. hda_nid_t val, n;
  290. if (i % num_elems == 0)
  291. parm = snd_hda_codec_read(codec, nid, 0,
  292. AC_VERB_GET_CONNECT_LIST, i);
  293. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  294. val = parm & mask;
  295. parm >>= shift;
  296. if (range_val) {
  297. /* ranges between the previous and this one */
  298. if (!prev_nid || prev_nid >= val) {
  299. snd_printk(KERN_WARNING "hda_codec: "
  300. "invalid dep_range_val %x:%x\n",
  301. prev_nid, val);
  302. continue;
  303. }
  304. for (n = prev_nid + 1; n <= val; n++) {
  305. if (conns >= max_conns) {
  306. snd_printk(KERN_ERR
  307. "Too many connections\n");
  308. return -EINVAL;
  309. }
  310. conn_list[conns++] = n;
  311. }
  312. } else {
  313. if (conns >= max_conns) {
  314. snd_printk(KERN_ERR "Too many connections\n");
  315. return -EINVAL;
  316. }
  317. conn_list[conns++] = val;
  318. }
  319. prev_nid = val;
  320. }
  321. return conns;
  322. }
  323. /**
  324. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  325. * @bus: the BUS
  326. * @res: unsolicited event (lower 32bit of RIRB entry)
  327. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  328. *
  329. * Adds the given event to the queue. The events are processed in
  330. * the workqueue asynchronously. Call this function in the interrupt
  331. * hanlder when RIRB receives an unsolicited event.
  332. *
  333. * Returns 0 if successful, or a negative error code.
  334. */
  335. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  336. {
  337. struct hda_bus_unsolicited *unsol;
  338. unsigned int wp;
  339. unsol = bus->unsol;
  340. if (!unsol)
  341. return 0;
  342. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  343. unsol->wp = wp;
  344. wp <<= 1;
  345. unsol->queue[wp] = res;
  346. unsol->queue[wp + 1] = res_ex;
  347. schedule_work(&unsol->work);
  348. return 0;
  349. }
  350. /*
  351. * process queued unsolicited events
  352. */
  353. static void process_unsol_events(struct work_struct *work)
  354. {
  355. struct hda_bus_unsolicited *unsol =
  356. container_of(work, struct hda_bus_unsolicited, work);
  357. struct hda_bus *bus = unsol->bus;
  358. struct hda_codec *codec;
  359. unsigned int rp, caddr, res;
  360. while (unsol->rp != unsol->wp) {
  361. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  362. unsol->rp = rp;
  363. rp <<= 1;
  364. res = unsol->queue[rp];
  365. caddr = unsol->queue[rp + 1];
  366. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  367. continue;
  368. codec = bus->caddr_tbl[caddr & 0x0f];
  369. if (codec && codec->patch_ops.unsol_event)
  370. codec->patch_ops.unsol_event(codec, res);
  371. }
  372. }
  373. /*
  374. * initialize unsolicited queue
  375. */
  376. static int init_unsol_queue(struct hda_bus *bus)
  377. {
  378. struct hda_bus_unsolicited *unsol;
  379. if (bus->unsol) /* already initialized */
  380. return 0;
  381. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  382. if (!unsol) {
  383. snd_printk(KERN_ERR "hda_codec: "
  384. "can't allocate unsolicited queue\n");
  385. return -ENOMEM;
  386. }
  387. INIT_WORK(&unsol->work, process_unsol_events);
  388. unsol->bus = bus;
  389. bus->unsol = unsol;
  390. return 0;
  391. }
  392. /*
  393. * destructor
  394. */
  395. static void snd_hda_codec_free(struct hda_codec *codec);
  396. static int snd_hda_bus_free(struct hda_bus *bus)
  397. {
  398. struct hda_codec *codec, *n;
  399. if (!bus)
  400. return 0;
  401. if (bus->unsol) {
  402. flush_scheduled_work();
  403. kfree(bus->unsol);
  404. }
  405. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  406. snd_hda_codec_free(codec);
  407. }
  408. if (bus->ops.private_free)
  409. bus->ops.private_free(bus);
  410. kfree(bus);
  411. return 0;
  412. }
  413. static int snd_hda_bus_dev_free(struct snd_device *device)
  414. {
  415. struct hda_bus *bus = device->device_data;
  416. bus->shutdown = 1;
  417. return snd_hda_bus_free(bus);
  418. }
  419. #ifdef CONFIG_SND_HDA_HWDEP
  420. static int snd_hda_bus_dev_register(struct snd_device *device)
  421. {
  422. struct hda_bus *bus = device->device_data;
  423. struct hda_codec *codec;
  424. list_for_each_entry(codec, &bus->codec_list, list) {
  425. snd_hda_hwdep_add_sysfs(codec);
  426. }
  427. return 0;
  428. }
  429. #else
  430. #define snd_hda_bus_dev_register NULL
  431. #endif
  432. /**
  433. * snd_hda_bus_new - create a HDA bus
  434. * @card: the card entry
  435. * @temp: the template for hda_bus information
  436. * @busp: the pointer to store the created bus instance
  437. *
  438. * Returns 0 if successful, or a negative error code.
  439. */
  440. int __devinit snd_hda_bus_new(struct snd_card *card,
  441. const struct hda_bus_template *temp,
  442. struct hda_bus **busp)
  443. {
  444. struct hda_bus *bus;
  445. int err;
  446. static struct snd_device_ops dev_ops = {
  447. .dev_register = snd_hda_bus_dev_register,
  448. .dev_free = snd_hda_bus_dev_free,
  449. };
  450. if (snd_BUG_ON(!temp))
  451. return -EINVAL;
  452. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  453. return -EINVAL;
  454. if (busp)
  455. *busp = NULL;
  456. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  457. if (bus == NULL) {
  458. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  459. return -ENOMEM;
  460. }
  461. bus->card = card;
  462. bus->private_data = temp->private_data;
  463. bus->pci = temp->pci;
  464. bus->modelname = temp->modelname;
  465. bus->power_save = temp->power_save;
  466. bus->ops = temp->ops;
  467. mutex_init(&bus->cmd_mutex);
  468. INIT_LIST_HEAD(&bus->codec_list);
  469. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  470. if (err < 0) {
  471. snd_hda_bus_free(bus);
  472. return err;
  473. }
  474. if (busp)
  475. *busp = bus;
  476. return 0;
  477. }
  478. #ifdef CONFIG_SND_HDA_GENERIC
  479. #define is_generic_config(codec) \
  480. (codec->modelname && !strcmp(codec->modelname, "generic"))
  481. #else
  482. #define is_generic_config(codec) 0
  483. #endif
  484. /*
  485. * find a matching codec preset
  486. */
  487. static const struct hda_codec_preset *
  488. find_codec_preset(struct hda_codec *codec)
  489. {
  490. const struct hda_codec_preset **tbl, *preset;
  491. if (is_generic_config(codec))
  492. return NULL; /* use the generic parser */
  493. for (tbl = hda_preset_tables; *tbl; tbl++) {
  494. for (preset = *tbl; preset->id; preset++) {
  495. u32 mask = preset->mask;
  496. if (preset->afg && preset->afg != codec->afg)
  497. continue;
  498. if (preset->mfg && preset->mfg != codec->mfg)
  499. continue;
  500. if (!mask)
  501. mask = ~0;
  502. if (preset->id == (codec->vendor_id & mask) &&
  503. (!preset->rev ||
  504. preset->rev == codec->revision_id))
  505. return preset;
  506. }
  507. }
  508. return NULL;
  509. }
  510. /*
  511. * get_codec_name - store the codec name
  512. */
  513. static int get_codec_name(struct hda_codec *codec)
  514. {
  515. const struct hda_vendor_id *c;
  516. const char *vendor = NULL;
  517. u16 vendor_id = codec->vendor_id >> 16;
  518. char tmp[16], name[32];
  519. for (c = hda_vendor_ids; c->id; c++) {
  520. if (c->id == vendor_id) {
  521. vendor = c->name;
  522. break;
  523. }
  524. }
  525. if (!vendor) {
  526. sprintf(tmp, "Generic %04x", vendor_id);
  527. vendor = tmp;
  528. }
  529. if (codec->preset && codec->preset->name)
  530. snprintf(name, sizeof(name), "%s %s", vendor,
  531. codec->preset->name);
  532. else
  533. snprintf(name, sizeof(name), "%s ID %x", vendor,
  534. codec->vendor_id & 0xffff);
  535. codec->name = kstrdup(name, GFP_KERNEL);
  536. if (!codec->name)
  537. return -ENOMEM;
  538. return 0;
  539. }
  540. /*
  541. * look for an AFG and MFG nodes
  542. */
  543. static void __devinit setup_fg_nodes(struct hda_codec *codec)
  544. {
  545. int i, total_nodes;
  546. hda_nid_t nid;
  547. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  548. for (i = 0; i < total_nodes; i++, nid++) {
  549. unsigned int func;
  550. func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
  551. switch (func & 0xff) {
  552. case AC_GRP_AUDIO_FUNCTION:
  553. codec->afg = nid;
  554. break;
  555. case AC_GRP_MODEM_FUNCTION:
  556. codec->mfg = nid;
  557. break;
  558. default:
  559. break;
  560. }
  561. }
  562. }
  563. /*
  564. * read widget caps for each widget and store in cache
  565. */
  566. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  567. {
  568. int i;
  569. hda_nid_t nid;
  570. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  571. &codec->start_nid);
  572. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  573. if (!codec->wcaps)
  574. return -ENOMEM;
  575. nid = codec->start_nid;
  576. for (i = 0; i < codec->num_nodes; i++, nid++)
  577. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  578. AC_PAR_AUDIO_WIDGET_CAP);
  579. return 0;
  580. }
  581. static void init_hda_cache(struct hda_cache_rec *cache,
  582. unsigned int record_size);
  583. static void free_hda_cache(struct hda_cache_rec *cache);
  584. /*
  585. * codec destructor
  586. */
  587. static void snd_hda_codec_free(struct hda_codec *codec)
  588. {
  589. if (!codec)
  590. return;
  591. #ifdef CONFIG_SND_HDA_POWER_SAVE
  592. cancel_delayed_work(&codec->power_work);
  593. flush_scheduled_work();
  594. #endif
  595. list_del(&codec->list);
  596. snd_array_free(&codec->mixers);
  597. codec->bus->caddr_tbl[codec->addr] = NULL;
  598. if (codec->patch_ops.free)
  599. codec->patch_ops.free(codec);
  600. free_hda_cache(&codec->amp_cache);
  601. free_hda_cache(&codec->cmd_cache);
  602. kfree(codec->name);
  603. kfree(codec->modelname);
  604. kfree(codec->wcaps);
  605. kfree(codec);
  606. }
  607. /**
  608. * snd_hda_codec_new - create a HDA codec
  609. * @bus: the bus to assign
  610. * @codec_addr: the codec address
  611. * @codecp: the pointer to store the generated codec
  612. *
  613. * Returns 0 if successful, or a negative error code.
  614. */
  615. int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  616. struct hda_codec **codecp)
  617. {
  618. struct hda_codec *codec;
  619. char component[31];
  620. int err;
  621. if (snd_BUG_ON(!bus))
  622. return -EINVAL;
  623. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  624. return -EINVAL;
  625. if (bus->caddr_tbl[codec_addr]) {
  626. snd_printk(KERN_ERR "hda_codec: "
  627. "address 0x%x is already occupied\n", codec_addr);
  628. return -EBUSY;
  629. }
  630. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  631. if (codec == NULL) {
  632. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  633. return -ENOMEM;
  634. }
  635. codec->bus = bus;
  636. codec->addr = codec_addr;
  637. mutex_init(&codec->spdif_mutex);
  638. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  639. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  640. snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
  641. if (codec->bus->modelname) {
  642. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  643. if (!codec->modelname) {
  644. snd_hda_codec_free(codec);
  645. return -ENODEV;
  646. }
  647. }
  648. #ifdef CONFIG_SND_HDA_POWER_SAVE
  649. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  650. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  651. * the caller has to power down appropriatley after initialization
  652. * phase.
  653. */
  654. hda_keep_power_on(codec);
  655. #endif
  656. list_add_tail(&codec->list, &bus->codec_list);
  657. bus->caddr_tbl[codec_addr] = codec;
  658. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  659. AC_PAR_VENDOR_ID);
  660. if (codec->vendor_id == -1)
  661. /* read again, hopefully the access method was corrected
  662. * in the last read...
  663. */
  664. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  665. AC_PAR_VENDOR_ID);
  666. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  667. AC_PAR_SUBSYSTEM_ID);
  668. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  669. AC_PAR_REV_ID);
  670. setup_fg_nodes(codec);
  671. if (!codec->afg && !codec->mfg) {
  672. snd_printdd("hda_codec: no AFG or MFG node found\n");
  673. snd_hda_codec_free(codec);
  674. return -ENODEV;
  675. }
  676. if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
  677. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  678. snd_hda_codec_free(codec);
  679. return -ENOMEM;
  680. }
  681. if (!codec->subsystem_id) {
  682. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  683. codec->subsystem_id =
  684. snd_hda_codec_read(codec, nid, 0,
  685. AC_VERB_GET_SUBSYSTEM_ID, 0);
  686. }
  687. if (bus->modelname)
  688. codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
  689. err = snd_hda_codec_configure(codec);
  690. if (err < 0) {
  691. snd_hda_codec_free(codec);
  692. return err;
  693. }
  694. snd_hda_codec_proc_new(codec);
  695. snd_hda_create_hwdep(codec);
  696. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  697. codec->subsystem_id, codec->revision_id);
  698. snd_component_add(codec->bus->card, component);
  699. if (codecp)
  700. *codecp = codec;
  701. return 0;
  702. }
  703. int snd_hda_codec_configure(struct hda_codec *codec)
  704. {
  705. int err;
  706. codec->preset = find_codec_preset(codec);
  707. if (!codec->name) {
  708. err = get_codec_name(codec);
  709. if (err < 0)
  710. return err;
  711. }
  712. /* audio codec should override the mixer name */
  713. if (codec->afg || !*codec->bus->card->mixername)
  714. strlcpy(codec->bus->card->mixername, codec->name,
  715. sizeof(codec->bus->card->mixername));
  716. if (is_generic_config(codec)) {
  717. err = snd_hda_parse_generic_codec(codec);
  718. goto patched;
  719. }
  720. if (codec->preset && codec->preset->patch) {
  721. err = codec->preset->patch(codec);
  722. goto patched;
  723. }
  724. /* call the default parser */
  725. err = snd_hda_parse_generic_codec(codec);
  726. if (err < 0)
  727. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  728. patched:
  729. if (!err && codec->patch_ops.unsol_event)
  730. err = init_unsol_queue(codec->bus);
  731. return err;
  732. }
  733. /**
  734. * snd_hda_codec_setup_stream - set up the codec for streaming
  735. * @codec: the CODEC to set up
  736. * @nid: the NID to set up
  737. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  738. * @channel_id: channel id to pass, zero based.
  739. * @format: stream format.
  740. */
  741. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  742. u32 stream_tag,
  743. int channel_id, int format)
  744. {
  745. if (!nid)
  746. return;
  747. snd_printdd("hda_codec_setup_stream: "
  748. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  749. nid, stream_tag, channel_id, format);
  750. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  751. (stream_tag << 4) | channel_id);
  752. msleep(1);
  753. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  754. }
  755. void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  756. {
  757. if (!nid)
  758. return;
  759. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  760. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  761. #if 0 /* keep the format */
  762. msleep(1);
  763. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  764. #endif
  765. }
  766. /*
  767. * amp access functions
  768. */
  769. /* FIXME: more better hash key? */
  770. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  771. #define INFO_AMP_CAPS (1<<0)
  772. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  773. /* initialize the hash table */
  774. static void __devinit init_hda_cache(struct hda_cache_rec *cache,
  775. unsigned int record_size)
  776. {
  777. memset(cache, 0, sizeof(*cache));
  778. memset(cache->hash, 0xff, sizeof(cache->hash));
  779. snd_array_init(&cache->buf, record_size, 64);
  780. }
  781. static void free_hda_cache(struct hda_cache_rec *cache)
  782. {
  783. snd_array_free(&cache->buf);
  784. }
  785. /* query the hash. allocate an entry if not found. */
  786. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  787. u32 key)
  788. {
  789. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  790. u16 cur = cache->hash[idx];
  791. struct hda_cache_head *info;
  792. while (cur != 0xffff) {
  793. info = snd_array_elem(&cache->buf, cur);
  794. if (info->key == key)
  795. return info;
  796. cur = info->next;
  797. }
  798. /* add a new hash entry */
  799. info = snd_array_new(&cache->buf);
  800. if (!info)
  801. return NULL;
  802. cur = snd_array_index(&cache->buf, info);
  803. info->key = key;
  804. info->val = 0;
  805. info->next = cache->hash[idx];
  806. cache->hash[idx] = cur;
  807. return info;
  808. }
  809. /* query and allocate an amp hash entry */
  810. static inline struct hda_amp_info *
  811. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  812. {
  813. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  814. }
  815. /*
  816. * query AMP capabilities for the given widget and direction
  817. */
  818. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  819. {
  820. struct hda_amp_info *info;
  821. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  822. if (!info)
  823. return 0;
  824. if (!(info->head.val & INFO_AMP_CAPS)) {
  825. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  826. nid = codec->afg;
  827. info->amp_caps = snd_hda_param_read(codec, nid,
  828. direction == HDA_OUTPUT ?
  829. AC_PAR_AMP_OUT_CAP :
  830. AC_PAR_AMP_IN_CAP);
  831. if (info->amp_caps)
  832. info->head.val |= INFO_AMP_CAPS;
  833. }
  834. return info->amp_caps;
  835. }
  836. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  837. unsigned int caps)
  838. {
  839. struct hda_amp_info *info;
  840. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  841. if (!info)
  842. return -EINVAL;
  843. info->amp_caps = caps;
  844. info->head.val |= INFO_AMP_CAPS;
  845. return 0;
  846. }
  847. /*
  848. * read the current volume to info
  849. * if the cache exists, read the cache value.
  850. */
  851. static unsigned int get_vol_mute(struct hda_codec *codec,
  852. struct hda_amp_info *info, hda_nid_t nid,
  853. int ch, int direction, int index)
  854. {
  855. u32 val, parm;
  856. if (info->head.val & INFO_AMP_VOL(ch))
  857. return info->vol[ch];
  858. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  859. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  860. parm |= index;
  861. val = snd_hda_codec_read(codec, nid, 0,
  862. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  863. info->vol[ch] = val & 0xff;
  864. info->head.val |= INFO_AMP_VOL(ch);
  865. return info->vol[ch];
  866. }
  867. /*
  868. * write the current volume in info to the h/w and update the cache
  869. */
  870. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  871. hda_nid_t nid, int ch, int direction, int index,
  872. int val)
  873. {
  874. u32 parm;
  875. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  876. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  877. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  878. parm |= val;
  879. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  880. info->vol[ch] = val;
  881. }
  882. /*
  883. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  884. */
  885. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  886. int direction, int index)
  887. {
  888. struct hda_amp_info *info;
  889. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  890. if (!info)
  891. return 0;
  892. return get_vol_mute(codec, info, nid, ch, direction, index);
  893. }
  894. /*
  895. * update the AMP value, mask = bit mask to set, val = the value
  896. */
  897. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  898. int direction, int idx, int mask, int val)
  899. {
  900. struct hda_amp_info *info;
  901. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  902. if (!info)
  903. return 0;
  904. val &= mask;
  905. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  906. if (info->vol[ch] == val)
  907. return 0;
  908. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  909. return 1;
  910. }
  911. /*
  912. * update the AMP stereo with the same mask and value
  913. */
  914. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  915. int direction, int idx, int mask, int val)
  916. {
  917. int ch, ret = 0;
  918. for (ch = 0; ch < 2; ch++)
  919. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  920. idx, mask, val);
  921. return ret;
  922. }
  923. #ifdef SND_HDA_NEEDS_RESUME
  924. /* resume the all amp commands from the cache */
  925. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  926. {
  927. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  928. int i;
  929. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  930. u32 key = buffer->head.key;
  931. hda_nid_t nid;
  932. unsigned int idx, dir, ch;
  933. if (!key)
  934. continue;
  935. nid = key & 0xff;
  936. idx = (key >> 16) & 0xff;
  937. dir = (key >> 24) & 0xff;
  938. for (ch = 0; ch < 2; ch++) {
  939. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  940. continue;
  941. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  942. buffer->vol[ch]);
  943. }
  944. }
  945. }
  946. #endif /* SND_HDA_NEEDS_RESUME */
  947. /* volume */
  948. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  949. struct snd_ctl_elem_info *uinfo)
  950. {
  951. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  952. u16 nid = get_amp_nid(kcontrol);
  953. u8 chs = get_amp_channels(kcontrol);
  954. int dir = get_amp_direction(kcontrol);
  955. u32 caps;
  956. caps = query_amp_caps(codec, nid, dir);
  957. /* num steps */
  958. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  959. if (!caps) {
  960. printk(KERN_WARNING "hda_codec: "
  961. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  962. kcontrol->id.name);
  963. return -EINVAL;
  964. }
  965. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  966. uinfo->count = chs == 3 ? 2 : 1;
  967. uinfo->value.integer.min = 0;
  968. uinfo->value.integer.max = caps;
  969. return 0;
  970. }
  971. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  972. struct snd_ctl_elem_value *ucontrol)
  973. {
  974. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  975. hda_nid_t nid = get_amp_nid(kcontrol);
  976. int chs = get_amp_channels(kcontrol);
  977. int dir = get_amp_direction(kcontrol);
  978. int idx = get_amp_index(kcontrol);
  979. long *valp = ucontrol->value.integer.value;
  980. if (chs & 1)
  981. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
  982. & HDA_AMP_VOLMASK;
  983. if (chs & 2)
  984. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
  985. & HDA_AMP_VOLMASK;
  986. return 0;
  987. }
  988. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  989. struct snd_ctl_elem_value *ucontrol)
  990. {
  991. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  992. hda_nid_t nid = get_amp_nid(kcontrol);
  993. int chs = get_amp_channels(kcontrol);
  994. int dir = get_amp_direction(kcontrol);
  995. int idx = get_amp_index(kcontrol);
  996. long *valp = ucontrol->value.integer.value;
  997. int change = 0;
  998. snd_hda_power_up(codec);
  999. if (chs & 1) {
  1000. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1001. 0x7f, *valp);
  1002. valp++;
  1003. }
  1004. if (chs & 2)
  1005. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1006. 0x7f, *valp);
  1007. snd_hda_power_down(codec);
  1008. return change;
  1009. }
  1010. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1011. unsigned int size, unsigned int __user *_tlv)
  1012. {
  1013. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1014. hda_nid_t nid = get_amp_nid(kcontrol);
  1015. int dir = get_amp_direction(kcontrol);
  1016. u32 caps, val1, val2;
  1017. if (size < 4 * sizeof(unsigned int))
  1018. return -ENOMEM;
  1019. caps = query_amp_caps(codec, nid, dir);
  1020. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1021. val2 = (val2 + 1) * 25;
  1022. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1023. val1 = ((int)val1) * ((int)val2);
  1024. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1025. return -EFAULT;
  1026. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1027. return -EFAULT;
  1028. if (put_user(val1, _tlv + 2))
  1029. return -EFAULT;
  1030. if (put_user(val2, _tlv + 3))
  1031. return -EFAULT;
  1032. return 0;
  1033. }
  1034. /*
  1035. * set (static) TLV for virtual master volume; recalculated as max 0dB
  1036. */
  1037. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1038. unsigned int *tlv)
  1039. {
  1040. u32 caps;
  1041. int nums, step;
  1042. caps = query_amp_caps(codec, nid, dir);
  1043. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1044. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1045. step = (step + 1) * 25;
  1046. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1047. tlv[1] = 2 * sizeof(unsigned int);
  1048. tlv[2] = -nums * step;
  1049. tlv[3] = step;
  1050. }
  1051. /* find a mixer control element with the given name */
  1052. static struct snd_kcontrol *
  1053. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1054. const char *name, int idx)
  1055. {
  1056. struct snd_ctl_elem_id id;
  1057. memset(&id, 0, sizeof(id));
  1058. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1059. id.index = idx;
  1060. strcpy(id.name, name);
  1061. return snd_ctl_find_id(codec->bus->card, &id);
  1062. }
  1063. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1064. const char *name)
  1065. {
  1066. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1067. }
  1068. /* Add a control element and assign to the codec */
  1069. int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
  1070. {
  1071. int err;
  1072. struct snd_kcontrol **knewp;
  1073. err = snd_ctl_add(codec->bus->card, kctl);
  1074. if (err < 0)
  1075. return err;
  1076. knewp = snd_array_new(&codec->mixers);
  1077. if (!knewp)
  1078. return -ENOMEM;
  1079. *knewp = kctl;
  1080. return 0;
  1081. }
  1082. /* Clear all controls assigned to the given codec */
  1083. void snd_hda_ctls_clear(struct hda_codec *codec)
  1084. {
  1085. int i;
  1086. struct snd_kcontrol **kctls = codec->mixers.list;
  1087. for (i = 0; i < codec->mixers.used; i++)
  1088. snd_ctl_remove(codec->bus->card, kctls[i]);
  1089. snd_array_free(&codec->mixers);
  1090. }
  1091. void snd_hda_codec_reset(struct hda_codec *codec)
  1092. {
  1093. int i;
  1094. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1095. cancel_delayed_work(&codec->power_work);
  1096. flush_scheduled_work();
  1097. #endif
  1098. snd_hda_ctls_clear(codec);
  1099. /* relase PCMs */
  1100. for (i = 0; i < codec->num_pcms; i++) {
  1101. if (codec->pcm_info[i].pcm)
  1102. snd_device_free(codec->bus->card,
  1103. codec->pcm_info[i].pcm);
  1104. }
  1105. if (codec->patch_ops.free)
  1106. codec->patch_ops.free(codec);
  1107. codec->spec = NULL;
  1108. free_hda_cache(&codec->amp_cache);
  1109. free_hda_cache(&codec->cmd_cache);
  1110. codec->num_pcms = 0;
  1111. codec->pcm_info = NULL;
  1112. codec->preset = NULL;
  1113. }
  1114. /* create a virtual master control and add slaves */
  1115. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1116. unsigned int *tlv, const char **slaves)
  1117. {
  1118. struct snd_kcontrol *kctl;
  1119. const char **s;
  1120. int err;
  1121. for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
  1122. ;
  1123. if (!*s) {
  1124. snd_printdd("No slave found for %s\n", name);
  1125. return 0;
  1126. }
  1127. kctl = snd_ctl_make_virtual_master(name, tlv);
  1128. if (!kctl)
  1129. return -ENOMEM;
  1130. err = snd_hda_ctl_add(codec, kctl);
  1131. if (err < 0)
  1132. return err;
  1133. for (s = slaves; *s; s++) {
  1134. struct snd_kcontrol *sctl;
  1135. sctl = snd_hda_find_mixer_ctl(codec, *s);
  1136. if (!sctl) {
  1137. snd_printdd("Cannot find slave %s, skipped\n", *s);
  1138. continue;
  1139. }
  1140. err = snd_ctl_add_slave(kctl, sctl);
  1141. if (err < 0)
  1142. return err;
  1143. }
  1144. return 0;
  1145. }
  1146. /* switch */
  1147. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1148. struct snd_ctl_elem_info *uinfo)
  1149. {
  1150. int chs = get_amp_channels(kcontrol);
  1151. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1152. uinfo->count = chs == 3 ? 2 : 1;
  1153. uinfo->value.integer.min = 0;
  1154. uinfo->value.integer.max = 1;
  1155. return 0;
  1156. }
  1157. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1158. struct snd_ctl_elem_value *ucontrol)
  1159. {
  1160. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1161. hda_nid_t nid = get_amp_nid(kcontrol);
  1162. int chs = get_amp_channels(kcontrol);
  1163. int dir = get_amp_direction(kcontrol);
  1164. int idx = get_amp_index(kcontrol);
  1165. long *valp = ucontrol->value.integer.value;
  1166. if (chs & 1)
  1167. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1168. HDA_AMP_MUTE) ? 0 : 1;
  1169. if (chs & 2)
  1170. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1171. HDA_AMP_MUTE) ? 0 : 1;
  1172. return 0;
  1173. }
  1174. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1175. struct snd_ctl_elem_value *ucontrol)
  1176. {
  1177. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1178. hda_nid_t nid = get_amp_nid(kcontrol);
  1179. int chs = get_amp_channels(kcontrol);
  1180. int dir = get_amp_direction(kcontrol);
  1181. int idx = get_amp_index(kcontrol);
  1182. long *valp = ucontrol->value.integer.value;
  1183. int change = 0;
  1184. snd_hda_power_up(codec);
  1185. if (chs & 1) {
  1186. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1187. HDA_AMP_MUTE,
  1188. *valp ? 0 : HDA_AMP_MUTE);
  1189. valp++;
  1190. }
  1191. if (chs & 2)
  1192. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1193. HDA_AMP_MUTE,
  1194. *valp ? 0 : HDA_AMP_MUTE);
  1195. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1196. if (codec->patch_ops.check_power_status)
  1197. codec->patch_ops.check_power_status(codec, nid);
  1198. #endif
  1199. snd_hda_power_down(codec);
  1200. return change;
  1201. }
  1202. /*
  1203. * bound volume controls
  1204. *
  1205. * bind multiple volumes (# indices, from 0)
  1206. */
  1207. #define AMP_VAL_IDX_SHIFT 19
  1208. #define AMP_VAL_IDX_MASK (0x0f<<19)
  1209. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  1210. struct snd_ctl_elem_value *ucontrol)
  1211. {
  1212. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1213. unsigned long pval;
  1214. int err;
  1215. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1216. pval = kcontrol->private_value;
  1217. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  1218. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  1219. kcontrol->private_value = pval;
  1220. mutex_unlock(&codec->spdif_mutex);
  1221. return err;
  1222. }
  1223. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  1224. struct snd_ctl_elem_value *ucontrol)
  1225. {
  1226. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1227. unsigned long pval;
  1228. int i, indices, err = 0, change = 0;
  1229. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1230. pval = kcontrol->private_value;
  1231. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  1232. for (i = 0; i < indices; i++) {
  1233. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  1234. (i << AMP_VAL_IDX_SHIFT);
  1235. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  1236. if (err < 0)
  1237. break;
  1238. change |= err;
  1239. }
  1240. kcontrol->private_value = pval;
  1241. mutex_unlock(&codec->spdif_mutex);
  1242. return err < 0 ? err : change;
  1243. }
  1244. /*
  1245. * generic bound volume/swtich controls
  1246. */
  1247. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  1248. struct snd_ctl_elem_info *uinfo)
  1249. {
  1250. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1251. struct hda_bind_ctls *c;
  1252. int err;
  1253. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1254. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1255. kcontrol->private_value = *c->values;
  1256. err = c->ops->info(kcontrol, uinfo);
  1257. kcontrol->private_value = (long)c;
  1258. mutex_unlock(&codec->spdif_mutex);
  1259. return err;
  1260. }
  1261. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  1262. struct snd_ctl_elem_value *ucontrol)
  1263. {
  1264. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1265. struct hda_bind_ctls *c;
  1266. int err;
  1267. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1268. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1269. kcontrol->private_value = *c->values;
  1270. err = c->ops->get(kcontrol, ucontrol);
  1271. kcontrol->private_value = (long)c;
  1272. mutex_unlock(&codec->spdif_mutex);
  1273. return err;
  1274. }
  1275. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  1276. struct snd_ctl_elem_value *ucontrol)
  1277. {
  1278. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1279. struct hda_bind_ctls *c;
  1280. unsigned long *vals;
  1281. int err = 0, change = 0;
  1282. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1283. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1284. for (vals = c->values; *vals; vals++) {
  1285. kcontrol->private_value = *vals;
  1286. err = c->ops->put(kcontrol, ucontrol);
  1287. if (err < 0)
  1288. break;
  1289. change |= err;
  1290. }
  1291. kcontrol->private_value = (long)c;
  1292. mutex_unlock(&codec->spdif_mutex);
  1293. return err < 0 ? err : change;
  1294. }
  1295. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1296. unsigned int size, unsigned int __user *tlv)
  1297. {
  1298. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1299. struct hda_bind_ctls *c;
  1300. int err;
  1301. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  1302. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1303. kcontrol->private_value = *c->values;
  1304. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  1305. kcontrol->private_value = (long)c;
  1306. mutex_unlock(&codec->spdif_mutex);
  1307. return err;
  1308. }
  1309. struct hda_ctl_ops snd_hda_bind_vol = {
  1310. .info = snd_hda_mixer_amp_volume_info,
  1311. .get = snd_hda_mixer_amp_volume_get,
  1312. .put = snd_hda_mixer_amp_volume_put,
  1313. .tlv = snd_hda_mixer_amp_tlv
  1314. };
  1315. struct hda_ctl_ops snd_hda_bind_sw = {
  1316. .info = snd_hda_mixer_amp_switch_info,
  1317. .get = snd_hda_mixer_amp_switch_get,
  1318. .put = snd_hda_mixer_amp_switch_put,
  1319. .tlv = snd_hda_mixer_amp_tlv
  1320. };
  1321. /*
  1322. * SPDIF out controls
  1323. */
  1324. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1325. struct snd_ctl_elem_info *uinfo)
  1326. {
  1327. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1328. uinfo->count = 1;
  1329. return 0;
  1330. }
  1331. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1332. struct snd_ctl_elem_value *ucontrol)
  1333. {
  1334. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1335. IEC958_AES0_NONAUDIO |
  1336. IEC958_AES0_CON_EMPHASIS_5015 |
  1337. IEC958_AES0_CON_NOT_COPYRIGHT;
  1338. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1339. IEC958_AES1_CON_ORIGINAL;
  1340. return 0;
  1341. }
  1342. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1343. struct snd_ctl_elem_value *ucontrol)
  1344. {
  1345. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1346. IEC958_AES0_NONAUDIO |
  1347. IEC958_AES0_PRO_EMPHASIS_5015;
  1348. return 0;
  1349. }
  1350. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1351. struct snd_ctl_elem_value *ucontrol)
  1352. {
  1353. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1354. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  1355. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  1356. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  1357. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  1358. return 0;
  1359. }
  1360. /* convert from SPDIF status bits to HDA SPDIF bits
  1361. * bit 0 (DigEn) is always set zero (to be filled later)
  1362. */
  1363. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1364. {
  1365. unsigned short val = 0;
  1366. if (sbits & IEC958_AES0_PROFESSIONAL)
  1367. val |= AC_DIG1_PROFESSIONAL;
  1368. if (sbits & IEC958_AES0_NONAUDIO)
  1369. val |= AC_DIG1_NONAUDIO;
  1370. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1371. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1372. IEC958_AES0_PRO_EMPHASIS_5015)
  1373. val |= AC_DIG1_EMPHASIS;
  1374. } else {
  1375. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1376. IEC958_AES0_CON_EMPHASIS_5015)
  1377. val |= AC_DIG1_EMPHASIS;
  1378. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1379. val |= AC_DIG1_COPYRIGHT;
  1380. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1381. val |= AC_DIG1_LEVEL;
  1382. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1383. }
  1384. return val;
  1385. }
  1386. /* convert to SPDIF status bits from HDA SPDIF bits
  1387. */
  1388. static unsigned int convert_to_spdif_status(unsigned short val)
  1389. {
  1390. unsigned int sbits = 0;
  1391. if (val & AC_DIG1_NONAUDIO)
  1392. sbits |= IEC958_AES0_NONAUDIO;
  1393. if (val & AC_DIG1_PROFESSIONAL)
  1394. sbits |= IEC958_AES0_PROFESSIONAL;
  1395. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1396. if (sbits & AC_DIG1_EMPHASIS)
  1397. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1398. } else {
  1399. if (val & AC_DIG1_EMPHASIS)
  1400. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1401. if (!(val & AC_DIG1_COPYRIGHT))
  1402. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1403. if (val & AC_DIG1_LEVEL)
  1404. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1405. sbits |= val & (0x7f << 8);
  1406. }
  1407. return sbits;
  1408. }
  1409. /* set digital convert verbs both for the given NID and its slaves */
  1410. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  1411. int verb, int val)
  1412. {
  1413. hda_nid_t *d;
  1414. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  1415. d = codec->slave_dig_outs;
  1416. if (!d)
  1417. return;
  1418. for (; *d; d++)
  1419. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  1420. }
  1421. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  1422. int dig1, int dig2)
  1423. {
  1424. if (dig1 != -1)
  1425. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  1426. if (dig2 != -1)
  1427. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  1428. }
  1429. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  1430. struct snd_ctl_elem_value *ucontrol)
  1431. {
  1432. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1433. hda_nid_t nid = kcontrol->private_value;
  1434. unsigned short val;
  1435. int change;
  1436. mutex_lock(&codec->spdif_mutex);
  1437. codec->spdif_status = ucontrol->value.iec958.status[0] |
  1438. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  1439. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  1440. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  1441. val = convert_from_spdif_status(codec->spdif_status);
  1442. val |= codec->spdif_ctls & 1;
  1443. change = codec->spdif_ctls != val;
  1444. codec->spdif_ctls = val;
  1445. if (change)
  1446. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  1447. mutex_unlock(&codec->spdif_mutex);
  1448. return change;
  1449. }
  1450. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1451. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1452. struct snd_ctl_elem_value *ucontrol)
  1453. {
  1454. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1455. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1456. return 0;
  1457. }
  1458. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1459. struct snd_ctl_elem_value *ucontrol)
  1460. {
  1461. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1462. hda_nid_t nid = kcontrol->private_value;
  1463. unsigned short val;
  1464. int change;
  1465. mutex_lock(&codec->spdif_mutex);
  1466. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1467. if (ucontrol->value.integer.value[0])
  1468. val |= AC_DIG1_ENABLE;
  1469. change = codec->spdif_ctls != val;
  1470. if (change) {
  1471. codec->spdif_ctls = val;
  1472. set_dig_out_convert(codec, nid, val & 0xff, -1);
  1473. /* unmute amp switch (if any) */
  1474. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1475. (val & AC_DIG1_ENABLE))
  1476. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1477. HDA_AMP_MUTE, 0);
  1478. }
  1479. mutex_unlock(&codec->spdif_mutex);
  1480. return change;
  1481. }
  1482. static struct snd_kcontrol_new dig_mixes[] = {
  1483. {
  1484. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1485. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1486. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1487. .info = snd_hda_spdif_mask_info,
  1488. .get = snd_hda_spdif_cmask_get,
  1489. },
  1490. {
  1491. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1492. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1493. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1494. .info = snd_hda_spdif_mask_info,
  1495. .get = snd_hda_spdif_pmask_get,
  1496. },
  1497. {
  1498. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1499. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1500. .info = snd_hda_spdif_mask_info,
  1501. .get = snd_hda_spdif_default_get,
  1502. .put = snd_hda_spdif_default_put,
  1503. },
  1504. {
  1505. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1506. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1507. .info = snd_hda_spdif_out_switch_info,
  1508. .get = snd_hda_spdif_out_switch_get,
  1509. .put = snd_hda_spdif_out_switch_put,
  1510. },
  1511. { } /* end */
  1512. };
  1513. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  1514. /**
  1515. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1516. * @codec: the HDA codec
  1517. * @nid: audio out widget NID
  1518. *
  1519. * Creates controls related with the SPDIF output.
  1520. * Called from each patch supporting the SPDIF out.
  1521. *
  1522. * Returns 0 if successful, or a negative error code.
  1523. */
  1524. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1525. {
  1526. int err;
  1527. struct snd_kcontrol *kctl;
  1528. struct snd_kcontrol_new *dig_mix;
  1529. int idx;
  1530. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1531. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  1532. idx))
  1533. break;
  1534. }
  1535. if (idx >= SPDIF_MAX_IDX) {
  1536. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  1537. return -EBUSY;
  1538. }
  1539. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1540. kctl = snd_ctl_new1(dig_mix, codec);
  1541. if (!kctl)
  1542. return -ENOMEM;
  1543. kctl->id.index = idx;
  1544. kctl->private_value = nid;
  1545. err = snd_hda_ctl_add(codec, kctl);
  1546. if (err < 0)
  1547. return err;
  1548. }
  1549. codec->spdif_ctls =
  1550. snd_hda_codec_read(codec, nid, 0,
  1551. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1552. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1553. return 0;
  1554. }
  1555. /*
  1556. * SPDIF sharing with analog output
  1557. */
  1558. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  1559. struct snd_ctl_elem_value *ucontrol)
  1560. {
  1561. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1562. ucontrol->value.integer.value[0] = mout->share_spdif;
  1563. return 0;
  1564. }
  1565. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  1566. struct snd_ctl_elem_value *ucontrol)
  1567. {
  1568. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1569. mout->share_spdif = !!ucontrol->value.integer.value[0];
  1570. return 0;
  1571. }
  1572. static struct snd_kcontrol_new spdif_share_sw = {
  1573. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1574. .name = "IEC958 Default PCM Playback Switch",
  1575. .info = snd_ctl_boolean_mono_info,
  1576. .get = spdif_share_sw_get,
  1577. .put = spdif_share_sw_put,
  1578. };
  1579. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  1580. struct hda_multi_out *mout)
  1581. {
  1582. if (!mout->dig_out_nid)
  1583. return 0;
  1584. /* ATTENTION: here mout is passed as private_data, instead of codec */
  1585. return snd_hda_ctl_add(codec,
  1586. snd_ctl_new1(&spdif_share_sw, mout));
  1587. }
  1588. /*
  1589. * SPDIF input
  1590. */
  1591. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1592. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1593. struct snd_ctl_elem_value *ucontrol)
  1594. {
  1595. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1596. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1597. return 0;
  1598. }
  1599. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1600. struct snd_ctl_elem_value *ucontrol)
  1601. {
  1602. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1603. hda_nid_t nid = kcontrol->private_value;
  1604. unsigned int val = !!ucontrol->value.integer.value[0];
  1605. int change;
  1606. mutex_lock(&codec->spdif_mutex);
  1607. change = codec->spdif_in_enable != val;
  1608. if (change) {
  1609. codec->spdif_in_enable = val;
  1610. snd_hda_codec_write_cache(codec, nid, 0,
  1611. AC_VERB_SET_DIGI_CONVERT_1, val);
  1612. }
  1613. mutex_unlock(&codec->spdif_mutex);
  1614. return change;
  1615. }
  1616. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1617. struct snd_ctl_elem_value *ucontrol)
  1618. {
  1619. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1620. hda_nid_t nid = kcontrol->private_value;
  1621. unsigned short val;
  1622. unsigned int sbits;
  1623. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1624. sbits = convert_to_spdif_status(val);
  1625. ucontrol->value.iec958.status[0] = sbits;
  1626. ucontrol->value.iec958.status[1] = sbits >> 8;
  1627. ucontrol->value.iec958.status[2] = sbits >> 16;
  1628. ucontrol->value.iec958.status[3] = sbits >> 24;
  1629. return 0;
  1630. }
  1631. static struct snd_kcontrol_new dig_in_ctls[] = {
  1632. {
  1633. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1634. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1635. .info = snd_hda_spdif_in_switch_info,
  1636. .get = snd_hda_spdif_in_switch_get,
  1637. .put = snd_hda_spdif_in_switch_put,
  1638. },
  1639. {
  1640. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1641. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1642. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1643. .info = snd_hda_spdif_mask_info,
  1644. .get = snd_hda_spdif_in_status_get,
  1645. },
  1646. { } /* end */
  1647. };
  1648. /**
  1649. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1650. * @codec: the HDA codec
  1651. * @nid: audio in widget NID
  1652. *
  1653. * Creates controls related with the SPDIF input.
  1654. * Called from each patch supporting the SPDIF in.
  1655. *
  1656. * Returns 0 if successful, or a negative error code.
  1657. */
  1658. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1659. {
  1660. int err;
  1661. struct snd_kcontrol *kctl;
  1662. struct snd_kcontrol_new *dig_mix;
  1663. int idx;
  1664. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1665. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  1666. idx))
  1667. break;
  1668. }
  1669. if (idx >= SPDIF_MAX_IDX) {
  1670. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  1671. return -EBUSY;
  1672. }
  1673. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1674. kctl = snd_ctl_new1(dig_mix, codec);
  1675. kctl->private_value = nid;
  1676. err = snd_hda_ctl_add(codec, kctl);
  1677. if (err < 0)
  1678. return err;
  1679. }
  1680. codec->spdif_in_enable =
  1681. snd_hda_codec_read(codec, nid, 0,
  1682. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  1683. AC_DIG1_ENABLE;
  1684. return 0;
  1685. }
  1686. #ifdef SND_HDA_NEEDS_RESUME
  1687. /*
  1688. * command cache
  1689. */
  1690. /* build a 32bit cache key with the widget id and the command parameter */
  1691. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  1692. #define get_cmd_cache_nid(key) ((key) & 0xff)
  1693. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  1694. /**
  1695. * snd_hda_codec_write_cache - send a single command with caching
  1696. * @codec: the HDA codec
  1697. * @nid: NID to send the command
  1698. * @direct: direct flag
  1699. * @verb: the verb to send
  1700. * @parm: the parameter for the verb
  1701. *
  1702. * Send a single command without waiting for response.
  1703. *
  1704. * Returns 0 if successful, or a negative error code.
  1705. */
  1706. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  1707. int direct, unsigned int verb, unsigned int parm)
  1708. {
  1709. struct hda_bus *bus = codec->bus;
  1710. unsigned int res;
  1711. int err;
  1712. res = make_codec_cmd(codec, nid, direct, verb, parm);
  1713. snd_hda_power_up(codec);
  1714. mutex_lock(&bus->cmd_mutex);
  1715. err = bus->ops.command(bus, res);
  1716. if (!err) {
  1717. struct hda_cache_head *c;
  1718. u32 key = build_cmd_cache_key(nid, verb);
  1719. c = get_alloc_hash(&codec->cmd_cache, key);
  1720. if (c)
  1721. c->val = parm;
  1722. }
  1723. mutex_unlock(&bus->cmd_mutex);
  1724. snd_hda_power_down(codec);
  1725. return err;
  1726. }
  1727. /* resume the all commands from the cache */
  1728. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  1729. {
  1730. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  1731. int i;
  1732. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  1733. u32 key = buffer->key;
  1734. if (!key)
  1735. continue;
  1736. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  1737. get_cmd_cache_cmd(key), buffer->val);
  1738. }
  1739. }
  1740. /**
  1741. * snd_hda_sequence_write_cache - sequence writes with caching
  1742. * @codec: the HDA codec
  1743. * @seq: VERB array to send
  1744. *
  1745. * Send the commands sequentially from the given array.
  1746. * Thte commands are recorded on cache for power-save and resume.
  1747. * The array must be terminated with NID=0.
  1748. */
  1749. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  1750. const struct hda_verb *seq)
  1751. {
  1752. for (; seq->nid; seq++)
  1753. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  1754. seq->param);
  1755. }
  1756. #endif /* SND_HDA_NEEDS_RESUME */
  1757. /*
  1758. * set power state of the codec
  1759. */
  1760. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1761. unsigned int power_state)
  1762. {
  1763. hda_nid_t nid;
  1764. int i;
  1765. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1766. power_state);
  1767. msleep(10); /* partial workaround for "azx_get_response timeout" */
  1768. nid = codec->start_nid;
  1769. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1770. unsigned int wcaps = get_wcaps(codec, nid);
  1771. if (wcaps & AC_WCAP_POWER) {
  1772. unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
  1773. AC_WCAP_TYPE_SHIFT;
  1774. if (wid_type == AC_WID_PIN) {
  1775. unsigned int pincap;
  1776. /*
  1777. * don't power down the widget if it controls
  1778. * eapd and EAPD_BTLENABLE is set.
  1779. */
  1780. pincap = snd_hda_param_read(codec, nid,
  1781. AC_PAR_PIN_CAP);
  1782. if (pincap & AC_PINCAP_EAPD) {
  1783. int eapd = snd_hda_codec_read(codec,
  1784. nid, 0,
  1785. AC_VERB_GET_EAPD_BTLENABLE, 0);
  1786. eapd &= 0x02;
  1787. if (power_state == AC_PWRST_D3 && eapd)
  1788. continue;
  1789. }
  1790. }
  1791. snd_hda_codec_write(codec, nid, 0,
  1792. AC_VERB_SET_POWER_STATE,
  1793. power_state);
  1794. }
  1795. }
  1796. if (power_state == AC_PWRST_D0) {
  1797. unsigned long end_time;
  1798. int state;
  1799. msleep(10);
  1800. /* wait until the codec reachs to D0 */
  1801. end_time = jiffies + msecs_to_jiffies(500);
  1802. do {
  1803. state = snd_hda_codec_read(codec, fg, 0,
  1804. AC_VERB_GET_POWER_STATE, 0);
  1805. if (state == power_state)
  1806. break;
  1807. msleep(1);
  1808. } while (time_after_eq(end_time, jiffies));
  1809. }
  1810. }
  1811. #ifdef CONFIG_SND_HDA_HWDEP
  1812. /* execute additional init verbs */
  1813. static void hda_exec_init_verbs(struct hda_codec *codec)
  1814. {
  1815. if (codec->init_verbs.list)
  1816. snd_hda_sequence_write(codec, codec->init_verbs.list);
  1817. }
  1818. #else
  1819. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  1820. #endif
  1821. #ifdef SND_HDA_NEEDS_RESUME
  1822. /*
  1823. * call suspend and power-down; used both from PM and power-save
  1824. */
  1825. static void hda_call_codec_suspend(struct hda_codec *codec)
  1826. {
  1827. if (codec->patch_ops.suspend)
  1828. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  1829. hda_set_power_state(codec,
  1830. codec->afg ? codec->afg : codec->mfg,
  1831. AC_PWRST_D3);
  1832. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1833. cancel_delayed_work(&codec->power_work);
  1834. codec->power_on = 0;
  1835. codec->power_transition = 0;
  1836. #endif
  1837. }
  1838. /*
  1839. * kick up codec; used both from PM and power-save
  1840. */
  1841. static void hda_call_codec_resume(struct hda_codec *codec)
  1842. {
  1843. hda_set_power_state(codec,
  1844. codec->afg ? codec->afg : codec->mfg,
  1845. AC_PWRST_D0);
  1846. hda_exec_init_verbs(codec);
  1847. if (codec->patch_ops.resume)
  1848. codec->patch_ops.resume(codec);
  1849. else {
  1850. if (codec->patch_ops.init)
  1851. codec->patch_ops.init(codec);
  1852. snd_hda_codec_resume_amp(codec);
  1853. snd_hda_codec_resume_cache(codec);
  1854. }
  1855. }
  1856. #endif /* SND_HDA_NEEDS_RESUME */
  1857. /**
  1858. * snd_hda_build_controls - build mixer controls
  1859. * @bus: the BUS
  1860. *
  1861. * Creates mixer controls for each codec included in the bus.
  1862. *
  1863. * Returns 0 if successful, otherwise a negative error code.
  1864. */
  1865. int __devinit snd_hda_build_controls(struct hda_bus *bus)
  1866. {
  1867. struct hda_codec *codec;
  1868. list_for_each_entry(codec, &bus->codec_list, list) {
  1869. int err = snd_hda_codec_build_controls(codec);
  1870. if (err < 0)
  1871. return err;
  1872. }
  1873. return 0;
  1874. }
  1875. int snd_hda_codec_build_controls(struct hda_codec *codec)
  1876. {
  1877. int err = 0;
  1878. /* fake as if already powered-on */
  1879. hda_keep_power_on(codec);
  1880. /* then fire up */
  1881. hda_set_power_state(codec,
  1882. codec->afg ? codec->afg : codec->mfg,
  1883. AC_PWRST_D0);
  1884. hda_exec_init_verbs(codec);
  1885. /* continue to initialize... */
  1886. if (codec->patch_ops.init)
  1887. err = codec->patch_ops.init(codec);
  1888. if (!err && codec->patch_ops.build_controls)
  1889. err = codec->patch_ops.build_controls(codec);
  1890. snd_hda_power_down(codec);
  1891. if (err < 0)
  1892. return err;
  1893. return 0;
  1894. }
  1895. /*
  1896. * stream formats
  1897. */
  1898. struct hda_rate_tbl {
  1899. unsigned int hz;
  1900. unsigned int alsa_bits;
  1901. unsigned int hda_fmt;
  1902. };
  1903. static struct hda_rate_tbl rate_bits[] = {
  1904. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1905. /* autodetected value used in snd_hda_query_supported_pcm */
  1906. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1907. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1908. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1909. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1910. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1911. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1912. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1913. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1914. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1915. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1916. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1917. #define AC_PAR_PCM_RATE_BITS 11
  1918. /* up to bits 10, 384kHZ isn't supported properly */
  1919. /* not autodetected value */
  1920. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1921. { 0 } /* terminator */
  1922. };
  1923. /**
  1924. * snd_hda_calc_stream_format - calculate format bitset
  1925. * @rate: the sample rate
  1926. * @channels: the number of channels
  1927. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1928. * @maxbps: the max. bps
  1929. *
  1930. * Calculate the format bitset from the given rate, channels and th PCM format.
  1931. *
  1932. * Return zero if invalid.
  1933. */
  1934. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1935. unsigned int channels,
  1936. unsigned int format,
  1937. unsigned int maxbps)
  1938. {
  1939. int i;
  1940. unsigned int val = 0;
  1941. for (i = 0; rate_bits[i].hz; i++)
  1942. if (rate_bits[i].hz == rate) {
  1943. val = rate_bits[i].hda_fmt;
  1944. break;
  1945. }
  1946. if (!rate_bits[i].hz) {
  1947. snd_printdd("invalid rate %d\n", rate);
  1948. return 0;
  1949. }
  1950. if (channels == 0 || channels > 8) {
  1951. snd_printdd("invalid channels %d\n", channels);
  1952. return 0;
  1953. }
  1954. val |= channels - 1;
  1955. switch (snd_pcm_format_width(format)) {
  1956. case 8: val |= 0x00; break;
  1957. case 16: val |= 0x10; break;
  1958. case 20:
  1959. case 24:
  1960. case 32:
  1961. if (maxbps >= 32)
  1962. val |= 0x40;
  1963. else if (maxbps >= 24)
  1964. val |= 0x30;
  1965. else
  1966. val |= 0x20;
  1967. break;
  1968. default:
  1969. snd_printdd("invalid format width %d\n",
  1970. snd_pcm_format_width(format));
  1971. return 0;
  1972. }
  1973. return val;
  1974. }
  1975. /**
  1976. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1977. * @codec: the HDA codec
  1978. * @nid: NID to query
  1979. * @ratesp: the pointer to store the detected rate bitflags
  1980. * @formatsp: the pointer to store the detected formats
  1981. * @bpsp: the pointer to store the detected format widths
  1982. *
  1983. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1984. * or @bsps argument is ignored.
  1985. *
  1986. * Returns 0 if successful, otherwise a negative error code.
  1987. */
  1988. static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1989. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1990. {
  1991. int i;
  1992. unsigned int val, streams;
  1993. val = 0;
  1994. if (nid != codec->afg &&
  1995. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1996. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1997. if (val == -1)
  1998. return -EIO;
  1999. }
  2000. if (!val)
  2001. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2002. if (ratesp) {
  2003. u32 rates = 0;
  2004. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  2005. if (val & (1 << i))
  2006. rates |= rate_bits[i].alsa_bits;
  2007. }
  2008. *ratesp = rates;
  2009. }
  2010. if (formatsp || bpsp) {
  2011. u64 formats = 0;
  2012. unsigned int bps;
  2013. unsigned int wcaps;
  2014. wcaps = get_wcaps(codec, nid);
  2015. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2016. if (streams == -1)
  2017. return -EIO;
  2018. if (!streams) {
  2019. streams = snd_hda_param_read(codec, codec->afg,
  2020. AC_PAR_STREAM);
  2021. if (streams == -1)
  2022. return -EIO;
  2023. }
  2024. bps = 0;
  2025. if (streams & AC_SUPFMT_PCM) {
  2026. if (val & AC_SUPPCM_BITS_8) {
  2027. formats |= SNDRV_PCM_FMTBIT_U8;
  2028. bps = 8;
  2029. }
  2030. if (val & AC_SUPPCM_BITS_16) {
  2031. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  2032. bps = 16;
  2033. }
  2034. if (wcaps & AC_WCAP_DIGITAL) {
  2035. if (val & AC_SUPPCM_BITS_32)
  2036. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  2037. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  2038. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2039. if (val & AC_SUPPCM_BITS_24)
  2040. bps = 24;
  2041. else if (val & AC_SUPPCM_BITS_20)
  2042. bps = 20;
  2043. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  2044. AC_SUPPCM_BITS_32)) {
  2045. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2046. if (val & AC_SUPPCM_BITS_32)
  2047. bps = 32;
  2048. else if (val & AC_SUPPCM_BITS_24)
  2049. bps = 24;
  2050. else if (val & AC_SUPPCM_BITS_20)
  2051. bps = 20;
  2052. }
  2053. }
  2054. else if (streams == AC_SUPFMT_FLOAT32) {
  2055. /* should be exclusive */
  2056. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  2057. bps = 32;
  2058. } else if (streams == AC_SUPFMT_AC3) {
  2059. /* should be exclusive */
  2060. /* temporary hack: we have still no proper support
  2061. * for the direct AC3 stream...
  2062. */
  2063. formats |= SNDRV_PCM_FMTBIT_U8;
  2064. bps = 8;
  2065. }
  2066. if (formatsp)
  2067. *formatsp = formats;
  2068. if (bpsp)
  2069. *bpsp = bps;
  2070. }
  2071. return 0;
  2072. }
  2073. /**
  2074. * snd_hda_is_supported_format - check whether the given node supports
  2075. * the format val
  2076. *
  2077. * Returns 1 if supported, 0 if not.
  2078. */
  2079. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  2080. unsigned int format)
  2081. {
  2082. int i;
  2083. unsigned int val = 0, rate, stream;
  2084. if (nid != codec->afg &&
  2085. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  2086. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2087. if (val == -1)
  2088. return 0;
  2089. }
  2090. if (!val) {
  2091. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2092. if (val == -1)
  2093. return 0;
  2094. }
  2095. rate = format & 0xff00;
  2096. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  2097. if (rate_bits[i].hda_fmt == rate) {
  2098. if (val & (1 << i))
  2099. break;
  2100. return 0;
  2101. }
  2102. if (i >= AC_PAR_PCM_RATE_BITS)
  2103. return 0;
  2104. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2105. if (stream == -1)
  2106. return 0;
  2107. if (!stream && nid != codec->afg)
  2108. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  2109. if (!stream || stream == -1)
  2110. return 0;
  2111. if (stream & AC_SUPFMT_PCM) {
  2112. switch (format & 0xf0) {
  2113. case 0x00:
  2114. if (!(val & AC_SUPPCM_BITS_8))
  2115. return 0;
  2116. break;
  2117. case 0x10:
  2118. if (!(val & AC_SUPPCM_BITS_16))
  2119. return 0;
  2120. break;
  2121. case 0x20:
  2122. if (!(val & AC_SUPPCM_BITS_20))
  2123. return 0;
  2124. break;
  2125. case 0x30:
  2126. if (!(val & AC_SUPPCM_BITS_24))
  2127. return 0;
  2128. break;
  2129. case 0x40:
  2130. if (!(val & AC_SUPPCM_BITS_32))
  2131. return 0;
  2132. break;
  2133. default:
  2134. return 0;
  2135. }
  2136. } else {
  2137. /* FIXME: check for float32 and AC3? */
  2138. }
  2139. return 1;
  2140. }
  2141. /*
  2142. * PCM stuff
  2143. */
  2144. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2145. struct hda_codec *codec,
  2146. struct snd_pcm_substream *substream)
  2147. {
  2148. return 0;
  2149. }
  2150. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2151. struct hda_codec *codec,
  2152. unsigned int stream_tag,
  2153. unsigned int format,
  2154. struct snd_pcm_substream *substream)
  2155. {
  2156. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2157. return 0;
  2158. }
  2159. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2160. struct hda_codec *codec,
  2161. struct snd_pcm_substream *substream)
  2162. {
  2163. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2164. return 0;
  2165. }
  2166. static int set_pcm_default_values(struct hda_codec *codec,
  2167. struct hda_pcm_stream *info)
  2168. {
  2169. /* query support PCM information from the given NID */
  2170. if (info->nid && (!info->rates || !info->formats)) {
  2171. snd_hda_query_supported_pcm(codec, info->nid,
  2172. info->rates ? NULL : &info->rates,
  2173. info->formats ? NULL : &info->formats,
  2174. info->maxbps ? NULL : &info->maxbps);
  2175. }
  2176. if (info->ops.open == NULL)
  2177. info->ops.open = hda_pcm_default_open_close;
  2178. if (info->ops.close == NULL)
  2179. info->ops.close = hda_pcm_default_open_close;
  2180. if (info->ops.prepare == NULL) {
  2181. if (snd_BUG_ON(!info->nid))
  2182. return -EINVAL;
  2183. info->ops.prepare = hda_pcm_default_prepare;
  2184. }
  2185. if (info->ops.cleanup == NULL) {
  2186. if (snd_BUG_ON(!info->nid))
  2187. return -EINVAL;
  2188. info->ops.cleanup = hda_pcm_default_cleanup;
  2189. }
  2190. return 0;
  2191. }
  2192. /*
  2193. * attach a new PCM stream
  2194. */
  2195. static int __devinit
  2196. snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  2197. {
  2198. struct hda_bus *bus = codec->bus;
  2199. struct hda_pcm_stream *info;
  2200. int stream, err;
  2201. if (snd_BUG_ON(!pcm->name))
  2202. return -EINVAL;
  2203. for (stream = 0; stream < 2; stream++) {
  2204. info = &pcm->stream[stream];
  2205. if (info->substreams) {
  2206. err = set_pcm_default_values(codec, info);
  2207. if (err < 0)
  2208. return err;
  2209. }
  2210. }
  2211. return bus->ops.attach_pcm(bus, codec, pcm);
  2212. }
  2213. /**
  2214. * snd_hda_build_pcms - build PCM information
  2215. * @bus: the BUS
  2216. *
  2217. * Create PCM information for each codec included in the bus.
  2218. *
  2219. * The build_pcms codec patch is requested to set up codec->num_pcms and
  2220. * codec->pcm_info properly. The array is referred by the top-level driver
  2221. * to create its PCM instances.
  2222. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  2223. * callback.
  2224. *
  2225. * At least, substreams, channels_min and channels_max must be filled for
  2226. * each stream. substreams = 0 indicates that the stream doesn't exist.
  2227. * When rates and/or formats are zero, the supported values are queried
  2228. * from the given nid. The nid is used also by the default ops.prepare
  2229. * and ops.cleanup callbacks.
  2230. *
  2231. * The driver needs to call ops.open in its open callback. Similarly,
  2232. * ops.close is supposed to be called in the close callback.
  2233. * ops.prepare should be called in the prepare or hw_params callback
  2234. * with the proper parameters for set up.
  2235. * ops.cleanup should be called in hw_free for clean up of streams.
  2236. *
  2237. * This function returns 0 if successfull, or a negative error code.
  2238. */
  2239. int snd_hda_build_pcms(struct hda_bus *bus)
  2240. {
  2241. static const char *dev_name[HDA_PCM_NTYPES] = {
  2242. "Audio", "SPDIF", "HDMI", "Modem"
  2243. };
  2244. /* starting device index for each PCM type */
  2245. static int dev_idx[HDA_PCM_NTYPES] = {
  2246. [HDA_PCM_TYPE_AUDIO] = 0,
  2247. [HDA_PCM_TYPE_SPDIF] = 1,
  2248. [HDA_PCM_TYPE_HDMI] = 3,
  2249. [HDA_PCM_TYPE_MODEM] = 6
  2250. };
  2251. /* normal audio device indices; not linear to keep compatibility */
  2252. static int audio_idx[4] = { 0, 2, 4, 5 };
  2253. struct hda_codec *codec;
  2254. int num_devs[HDA_PCM_NTYPES];
  2255. memset(num_devs, 0, sizeof(num_devs));
  2256. list_for_each_entry(codec, &bus->codec_list, list) {
  2257. unsigned int pcm;
  2258. int err;
  2259. if (!codec->num_pcms) {
  2260. if (!codec->patch_ops.build_pcms)
  2261. continue;
  2262. err = codec->patch_ops.build_pcms(codec);
  2263. if (err < 0)
  2264. return err;
  2265. }
  2266. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2267. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2268. int type = cpcm->pcm_type;
  2269. int dev;
  2270. if (!cpcm->stream[0].substreams &&
  2271. !cpcm->stream[1].substreams)
  2272. continue; /* no substreams assigned */
  2273. switch (type) {
  2274. case HDA_PCM_TYPE_AUDIO:
  2275. if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
  2276. snd_printk(KERN_WARNING
  2277. "Too many audio devices\n");
  2278. continue;
  2279. }
  2280. dev = audio_idx[num_devs[type]];
  2281. break;
  2282. case HDA_PCM_TYPE_SPDIF:
  2283. case HDA_PCM_TYPE_HDMI:
  2284. case HDA_PCM_TYPE_MODEM:
  2285. if (num_devs[type]) {
  2286. snd_printk(KERN_WARNING
  2287. "%s already defined\n",
  2288. dev_name[type]);
  2289. continue;
  2290. }
  2291. dev = dev_idx[type];
  2292. break;
  2293. default:
  2294. snd_printk(KERN_WARNING
  2295. "Invalid PCM type %d\n", type);
  2296. continue;
  2297. }
  2298. num_devs[type]++;
  2299. if (!cpcm->pcm) {
  2300. cpcm->device = dev;
  2301. err = snd_hda_attach_pcm(codec, cpcm);
  2302. if (err < 0)
  2303. return err;
  2304. }
  2305. }
  2306. }
  2307. return 0;
  2308. }
  2309. /**
  2310. * snd_hda_check_board_config - compare the current codec with the config table
  2311. * @codec: the HDA codec
  2312. * @num_configs: number of config enums
  2313. * @models: array of model name strings
  2314. * @tbl: configuration table, terminated by null entries
  2315. *
  2316. * Compares the modelname or PCI subsystem id of the current codec with the
  2317. * given configuration table. If a matching entry is found, returns its
  2318. * config value (supposed to be 0 or positive).
  2319. *
  2320. * If no entries are matching, the function returns a negative value.
  2321. */
  2322. int snd_hda_check_board_config(struct hda_codec *codec,
  2323. int num_configs, const char **models,
  2324. const struct snd_pci_quirk *tbl)
  2325. {
  2326. if (codec->modelname && models) {
  2327. int i;
  2328. for (i = 0; i < num_configs; i++) {
  2329. if (models[i] &&
  2330. !strcmp(codec->modelname, models[i])) {
  2331. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  2332. "selected\n", models[i]);
  2333. return i;
  2334. }
  2335. }
  2336. }
  2337. if (!codec->bus->pci || !tbl)
  2338. return -1;
  2339. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  2340. if (!tbl)
  2341. return -1;
  2342. if (tbl->value >= 0 && tbl->value < num_configs) {
  2343. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2344. char tmp[10];
  2345. const char *model = NULL;
  2346. if (models)
  2347. model = models[tbl->value];
  2348. if (!model) {
  2349. sprintf(tmp, "#%d", tbl->value);
  2350. model = tmp;
  2351. }
  2352. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2353. "for config %x:%x (%s)\n",
  2354. model, tbl->subvendor, tbl->subdevice,
  2355. (tbl->name ? tbl->name : "Unknown device"));
  2356. #endif
  2357. return tbl->value;
  2358. }
  2359. return -1;
  2360. }
  2361. /**
  2362. * snd_hda_add_new_ctls - create controls from the array
  2363. * @codec: the HDA codec
  2364. * @knew: the array of struct snd_kcontrol_new
  2365. *
  2366. * This helper function creates and add new controls in the given array.
  2367. * The array must be terminated with an empty entry as terminator.
  2368. *
  2369. * Returns 0 if successful, or a negative error code.
  2370. */
  2371. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2372. {
  2373. int err;
  2374. for (; knew->name; knew++) {
  2375. struct snd_kcontrol *kctl;
  2376. kctl = snd_ctl_new1(knew, codec);
  2377. if (!kctl)
  2378. return -ENOMEM;
  2379. err = snd_hda_ctl_add(codec, kctl);
  2380. if (err < 0) {
  2381. if (!codec->addr)
  2382. return err;
  2383. kctl = snd_ctl_new1(knew, codec);
  2384. if (!kctl)
  2385. return -ENOMEM;
  2386. kctl->id.device = codec->addr;
  2387. err = snd_hda_ctl_add(codec, kctl);
  2388. if (err < 0)
  2389. return err;
  2390. }
  2391. }
  2392. return 0;
  2393. }
  2394. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2395. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2396. unsigned int power_state);
  2397. static void hda_power_work(struct work_struct *work)
  2398. {
  2399. struct hda_codec *codec =
  2400. container_of(work, struct hda_codec, power_work.work);
  2401. struct hda_bus *bus = codec->bus;
  2402. if (!codec->power_on || codec->power_count) {
  2403. codec->power_transition = 0;
  2404. return;
  2405. }
  2406. hda_call_codec_suspend(codec);
  2407. if (bus->ops.pm_notify)
  2408. bus->ops.pm_notify(bus);
  2409. }
  2410. static void hda_keep_power_on(struct hda_codec *codec)
  2411. {
  2412. codec->power_count++;
  2413. codec->power_on = 1;
  2414. }
  2415. void snd_hda_power_up(struct hda_codec *codec)
  2416. {
  2417. struct hda_bus *bus = codec->bus;
  2418. codec->power_count++;
  2419. if (codec->power_on || codec->power_transition)
  2420. return;
  2421. codec->power_on = 1;
  2422. if (bus->ops.pm_notify)
  2423. bus->ops.pm_notify(bus);
  2424. hda_call_codec_resume(codec);
  2425. cancel_delayed_work(&codec->power_work);
  2426. codec->power_transition = 0;
  2427. }
  2428. #define power_save(codec) \
  2429. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2430. void snd_hda_power_down(struct hda_codec *codec)
  2431. {
  2432. --codec->power_count;
  2433. if (!codec->power_on || codec->power_count || codec->power_transition)
  2434. return;
  2435. if (power_save(codec)) {
  2436. codec->power_transition = 1; /* avoid reentrance */
  2437. schedule_delayed_work(&codec->power_work,
  2438. msecs_to_jiffies(power_save(codec) * 1000));
  2439. }
  2440. }
  2441. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2442. struct hda_loopback_check *check,
  2443. hda_nid_t nid)
  2444. {
  2445. struct hda_amp_list *p;
  2446. int ch, v;
  2447. if (!check->amplist)
  2448. return 0;
  2449. for (p = check->amplist; p->nid; p++) {
  2450. if (p->nid == nid)
  2451. break;
  2452. }
  2453. if (!p->nid)
  2454. return 0; /* nothing changed */
  2455. for (p = check->amplist; p->nid; p++) {
  2456. for (ch = 0; ch < 2; ch++) {
  2457. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2458. p->idx);
  2459. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2460. if (!check->power_on) {
  2461. check->power_on = 1;
  2462. snd_hda_power_up(codec);
  2463. }
  2464. return 1;
  2465. }
  2466. }
  2467. }
  2468. if (check->power_on) {
  2469. check->power_on = 0;
  2470. snd_hda_power_down(codec);
  2471. }
  2472. return 0;
  2473. }
  2474. #endif
  2475. /*
  2476. * Channel mode helper
  2477. */
  2478. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2479. struct snd_ctl_elem_info *uinfo,
  2480. const struct hda_channel_mode *chmode,
  2481. int num_chmodes)
  2482. {
  2483. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2484. uinfo->count = 1;
  2485. uinfo->value.enumerated.items = num_chmodes;
  2486. if (uinfo->value.enumerated.item >= num_chmodes)
  2487. uinfo->value.enumerated.item = num_chmodes - 1;
  2488. sprintf(uinfo->value.enumerated.name, "%dch",
  2489. chmode[uinfo->value.enumerated.item].channels);
  2490. return 0;
  2491. }
  2492. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2493. struct snd_ctl_elem_value *ucontrol,
  2494. const struct hda_channel_mode *chmode,
  2495. int num_chmodes,
  2496. int max_channels)
  2497. {
  2498. int i;
  2499. for (i = 0; i < num_chmodes; i++) {
  2500. if (max_channels == chmode[i].channels) {
  2501. ucontrol->value.enumerated.item[0] = i;
  2502. break;
  2503. }
  2504. }
  2505. return 0;
  2506. }
  2507. int snd_hda_ch_mode_put(struct hda_codec *codec,
  2508. struct snd_ctl_elem_value *ucontrol,
  2509. const struct hda_channel_mode *chmode,
  2510. int num_chmodes,
  2511. int *max_channelsp)
  2512. {
  2513. unsigned int mode;
  2514. mode = ucontrol->value.enumerated.item[0];
  2515. if (mode >= num_chmodes)
  2516. return -EINVAL;
  2517. if (*max_channelsp == chmode[mode].channels)
  2518. return 0;
  2519. /* change the current channel setting */
  2520. *max_channelsp = chmode[mode].channels;
  2521. if (chmode[mode].sequence)
  2522. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  2523. return 1;
  2524. }
  2525. /*
  2526. * input MUX helper
  2527. */
  2528. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  2529. struct snd_ctl_elem_info *uinfo)
  2530. {
  2531. unsigned int index;
  2532. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2533. uinfo->count = 1;
  2534. uinfo->value.enumerated.items = imux->num_items;
  2535. if (!imux->num_items)
  2536. return 0;
  2537. index = uinfo->value.enumerated.item;
  2538. if (index >= imux->num_items)
  2539. index = imux->num_items - 1;
  2540. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  2541. return 0;
  2542. }
  2543. int snd_hda_input_mux_put(struct hda_codec *codec,
  2544. const struct hda_input_mux *imux,
  2545. struct snd_ctl_elem_value *ucontrol,
  2546. hda_nid_t nid,
  2547. unsigned int *cur_val)
  2548. {
  2549. unsigned int idx;
  2550. if (!imux->num_items)
  2551. return 0;
  2552. idx = ucontrol->value.enumerated.item[0];
  2553. if (idx >= imux->num_items)
  2554. idx = imux->num_items - 1;
  2555. if (*cur_val == idx)
  2556. return 0;
  2557. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  2558. imux->items[idx].index);
  2559. *cur_val = idx;
  2560. return 1;
  2561. }
  2562. /*
  2563. * Multi-channel / digital-out PCM helper functions
  2564. */
  2565. /* setup SPDIF output stream */
  2566. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  2567. unsigned int stream_tag, unsigned int format)
  2568. {
  2569. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2570. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2571. set_dig_out_convert(codec, nid,
  2572. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
  2573. -1);
  2574. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  2575. if (codec->slave_dig_outs) {
  2576. hda_nid_t *d;
  2577. for (d = codec->slave_dig_outs; *d; d++)
  2578. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  2579. format);
  2580. }
  2581. /* turn on again (if needed) */
  2582. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  2583. set_dig_out_convert(codec, nid,
  2584. codec->spdif_ctls & 0xff, -1);
  2585. }
  2586. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  2587. {
  2588. snd_hda_codec_cleanup_stream(codec, nid);
  2589. if (codec->slave_dig_outs) {
  2590. hda_nid_t *d;
  2591. for (d = codec->slave_dig_outs; *d; d++)
  2592. snd_hda_codec_cleanup_stream(codec, *d);
  2593. }
  2594. }
  2595. /*
  2596. * open the digital out in the exclusive mode
  2597. */
  2598. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  2599. struct hda_multi_out *mout)
  2600. {
  2601. mutex_lock(&codec->spdif_mutex);
  2602. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  2603. /* already opened as analog dup; reset it once */
  2604. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2605. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  2606. mutex_unlock(&codec->spdif_mutex);
  2607. return 0;
  2608. }
  2609. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  2610. struct hda_multi_out *mout,
  2611. unsigned int stream_tag,
  2612. unsigned int format,
  2613. struct snd_pcm_substream *substream)
  2614. {
  2615. mutex_lock(&codec->spdif_mutex);
  2616. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  2617. mutex_unlock(&codec->spdif_mutex);
  2618. return 0;
  2619. }
  2620. /*
  2621. * release the digital out
  2622. */
  2623. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  2624. struct hda_multi_out *mout)
  2625. {
  2626. mutex_lock(&codec->spdif_mutex);
  2627. mout->dig_out_used = 0;
  2628. mutex_unlock(&codec->spdif_mutex);
  2629. return 0;
  2630. }
  2631. /*
  2632. * set up more restrictions for analog out
  2633. */
  2634. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  2635. struct hda_multi_out *mout,
  2636. struct snd_pcm_substream *substream,
  2637. struct hda_pcm_stream *hinfo)
  2638. {
  2639. struct snd_pcm_runtime *runtime = substream->runtime;
  2640. runtime->hw.channels_max = mout->max_channels;
  2641. if (mout->dig_out_nid) {
  2642. if (!mout->analog_rates) {
  2643. mout->analog_rates = hinfo->rates;
  2644. mout->analog_formats = hinfo->formats;
  2645. mout->analog_maxbps = hinfo->maxbps;
  2646. } else {
  2647. runtime->hw.rates = mout->analog_rates;
  2648. runtime->hw.formats = mout->analog_formats;
  2649. hinfo->maxbps = mout->analog_maxbps;
  2650. }
  2651. if (!mout->spdif_rates) {
  2652. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  2653. &mout->spdif_rates,
  2654. &mout->spdif_formats,
  2655. &mout->spdif_maxbps);
  2656. }
  2657. mutex_lock(&codec->spdif_mutex);
  2658. if (mout->share_spdif) {
  2659. runtime->hw.rates &= mout->spdif_rates;
  2660. runtime->hw.formats &= mout->spdif_formats;
  2661. if (mout->spdif_maxbps < hinfo->maxbps)
  2662. hinfo->maxbps = mout->spdif_maxbps;
  2663. }
  2664. mutex_unlock(&codec->spdif_mutex);
  2665. }
  2666. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  2667. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2668. }
  2669. /*
  2670. * set up the i/o for analog out
  2671. * when the digital out is available, copy the front out to digital out, too.
  2672. */
  2673. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  2674. struct hda_multi_out *mout,
  2675. unsigned int stream_tag,
  2676. unsigned int format,
  2677. struct snd_pcm_substream *substream)
  2678. {
  2679. hda_nid_t *nids = mout->dac_nids;
  2680. int chs = substream->runtime->channels;
  2681. int i;
  2682. mutex_lock(&codec->spdif_mutex);
  2683. if (mout->dig_out_nid && mout->share_spdif &&
  2684. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  2685. if (chs == 2 &&
  2686. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  2687. format) &&
  2688. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  2689. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  2690. setup_dig_out_stream(codec, mout->dig_out_nid,
  2691. stream_tag, format);
  2692. } else {
  2693. mout->dig_out_used = 0;
  2694. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2695. }
  2696. }
  2697. mutex_unlock(&codec->spdif_mutex);
  2698. /* front */
  2699. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  2700. 0, format);
  2701. if (!mout->no_share_stream &&
  2702. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  2703. /* headphone out will just decode front left/right (stereo) */
  2704. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  2705. 0, format);
  2706. /* extra outputs copied from front */
  2707. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2708. if (!mout->no_share_stream && mout->extra_out_nid[i])
  2709. snd_hda_codec_setup_stream(codec,
  2710. mout->extra_out_nid[i],
  2711. stream_tag, 0, format);
  2712. /* surrounds */
  2713. for (i = 1; i < mout->num_dacs; i++) {
  2714. if (chs >= (i + 1) * 2) /* independent out */
  2715. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2716. i * 2, format);
  2717. else if (!mout->no_share_stream) /* copy front */
  2718. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  2719. 0, format);
  2720. }
  2721. return 0;
  2722. }
  2723. /*
  2724. * clean up the setting for analog out
  2725. */
  2726. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  2727. struct hda_multi_out *mout)
  2728. {
  2729. hda_nid_t *nids = mout->dac_nids;
  2730. int i;
  2731. for (i = 0; i < mout->num_dacs; i++)
  2732. snd_hda_codec_cleanup_stream(codec, nids[i]);
  2733. if (mout->hp_nid)
  2734. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  2735. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  2736. if (mout->extra_out_nid[i])
  2737. snd_hda_codec_cleanup_stream(codec,
  2738. mout->extra_out_nid[i]);
  2739. mutex_lock(&codec->spdif_mutex);
  2740. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  2741. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  2742. mout->dig_out_used = 0;
  2743. }
  2744. mutex_unlock(&codec->spdif_mutex);
  2745. return 0;
  2746. }
  2747. /*
  2748. * Helper for automatic pin configuration
  2749. */
  2750. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  2751. {
  2752. for (; *list; list++)
  2753. if (*list == nid)
  2754. return 1;
  2755. return 0;
  2756. }
  2757. /*
  2758. * Sort an associated group of pins according to their sequence numbers.
  2759. */
  2760. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  2761. int num_pins)
  2762. {
  2763. int i, j;
  2764. short seq;
  2765. hda_nid_t nid;
  2766. for (i = 0; i < num_pins; i++) {
  2767. for (j = i + 1; j < num_pins; j++) {
  2768. if (sequences[i] > sequences[j]) {
  2769. seq = sequences[i];
  2770. sequences[i] = sequences[j];
  2771. sequences[j] = seq;
  2772. nid = pins[i];
  2773. pins[i] = pins[j];
  2774. pins[j] = nid;
  2775. }
  2776. }
  2777. }
  2778. }
  2779. /*
  2780. * Parse all pin widgets and store the useful pin nids to cfg
  2781. *
  2782. * The number of line-outs or any primary output is stored in line_outs,
  2783. * and the corresponding output pins are assigned to line_out_pins[],
  2784. * in the order of front, rear, CLFE, side, ...
  2785. *
  2786. * If more extra outputs (speaker and headphone) are found, the pins are
  2787. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  2788. * is detected, one of speaker of HP pins is assigned as the primary
  2789. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  2790. * if any analog output exists.
  2791. *
  2792. * The analog input pins are assigned to input_pins array.
  2793. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  2794. * respectively.
  2795. */
  2796. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  2797. struct auto_pin_cfg *cfg,
  2798. hda_nid_t *ignore_nids)
  2799. {
  2800. hda_nid_t nid, end_nid;
  2801. short seq, assoc_line_out, assoc_speaker;
  2802. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  2803. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  2804. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  2805. memset(cfg, 0, sizeof(*cfg));
  2806. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  2807. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  2808. memset(sequences_hp, 0, sizeof(sequences_hp));
  2809. assoc_line_out = assoc_speaker = 0;
  2810. end_nid = codec->start_nid + codec->num_nodes;
  2811. for (nid = codec->start_nid; nid < end_nid; nid++) {
  2812. unsigned int wid_caps = get_wcaps(codec, nid);
  2813. unsigned int wid_type =
  2814. (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  2815. unsigned int def_conf;
  2816. short assoc, loc;
  2817. /* read all default configuration for pin complex */
  2818. if (wid_type != AC_WID_PIN)
  2819. continue;
  2820. /* ignore the given nids (e.g. pc-beep returns error) */
  2821. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  2822. continue;
  2823. def_conf = snd_hda_codec_read(codec, nid, 0,
  2824. AC_VERB_GET_CONFIG_DEFAULT, 0);
  2825. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  2826. continue;
  2827. loc = get_defcfg_location(def_conf);
  2828. switch (get_defcfg_device(def_conf)) {
  2829. case AC_JACK_LINE_OUT:
  2830. seq = get_defcfg_sequence(def_conf);
  2831. assoc = get_defcfg_association(def_conf);
  2832. if (!(wid_caps & AC_WCAP_STEREO))
  2833. if (!cfg->mono_out_pin)
  2834. cfg->mono_out_pin = nid;
  2835. if (!assoc)
  2836. continue;
  2837. if (!assoc_line_out)
  2838. assoc_line_out = assoc;
  2839. else if (assoc_line_out != assoc)
  2840. continue;
  2841. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  2842. continue;
  2843. cfg->line_out_pins[cfg->line_outs] = nid;
  2844. sequences_line_out[cfg->line_outs] = seq;
  2845. cfg->line_outs++;
  2846. break;
  2847. case AC_JACK_SPEAKER:
  2848. seq = get_defcfg_sequence(def_conf);
  2849. assoc = get_defcfg_association(def_conf);
  2850. if (! assoc)
  2851. continue;
  2852. if (! assoc_speaker)
  2853. assoc_speaker = assoc;
  2854. else if (assoc_speaker != assoc)
  2855. continue;
  2856. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  2857. continue;
  2858. cfg->speaker_pins[cfg->speaker_outs] = nid;
  2859. sequences_speaker[cfg->speaker_outs] = seq;
  2860. cfg->speaker_outs++;
  2861. break;
  2862. case AC_JACK_HP_OUT:
  2863. seq = get_defcfg_sequence(def_conf);
  2864. assoc = get_defcfg_association(def_conf);
  2865. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  2866. continue;
  2867. cfg->hp_pins[cfg->hp_outs] = nid;
  2868. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  2869. cfg->hp_outs++;
  2870. break;
  2871. case AC_JACK_MIC_IN: {
  2872. int preferred, alt;
  2873. if (loc == AC_JACK_LOC_FRONT) {
  2874. preferred = AUTO_PIN_FRONT_MIC;
  2875. alt = AUTO_PIN_MIC;
  2876. } else {
  2877. preferred = AUTO_PIN_MIC;
  2878. alt = AUTO_PIN_FRONT_MIC;
  2879. }
  2880. if (!cfg->input_pins[preferred])
  2881. cfg->input_pins[preferred] = nid;
  2882. else if (!cfg->input_pins[alt])
  2883. cfg->input_pins[alt] = nid;
  2884. break;
  2885. }
  2886. case AC_JACK_LINE_IN:
  2887. if (loc == AC_JACK_LOC_FRONT)
  2888. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  2889. else
  2890. cfg->input_pins[AUTO_PIN_LINE] = nid;
  2891. break;
  2892. case AC_JACK_CD:
  2893. cfg->input_pins[AUTO_PIN_CD] = nid;
  2894. break;
  2895. case AC_JACK_AUX:
  2896. cfg->input_pins[AUTO_PIN_AUX] = nid;
  2897. break;
  2898. case AC_JACK_SPDIF_OUT:
  2899. cfg->dig_out_pin = nid;
  2900. break;
  2901. case AC_JACK_SPDIF_IN:
  2902. cfg->dig_in_pin = nid;
  2903. break;
  2904. }
  2905. }
  2906. /* FIX-UP:
  2907. * If no line-out is defined but multiple HPs are found,
  2908. * some of them might be the real line-outs.
  2909. */
  2910. if (!cfg->line_outs && cfg->hp_outs > 1) {
  2911. int i = 0;
  2912. while (i < cfg->hp_outs) {
  2913. /* The real HPs should have the sequence 0x0f */
  2914. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  2915. i++;
  2916. continue;
  2917. }
  2918. /* Move it to the line-out table */
  2919. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  2920. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  2921. cfg->line_outs++;
  2922. cfg->hp_outs--;
  2923. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  2924. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  2925. memmove(sequences_hp + i - 1, sequences_hp + i,
  2926. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  2927. }
  2928. }
  2929. /* sort by sequence */
  2930. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  2931. cfg->line_outs);
  2932. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  2933. cfg->speaker_outs);
  2934. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  2935. cfg->hp_outs);
  2936. /* if we have only one mic, make it AUTO_PIN_MIC */
  2937. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  2938. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  2939. cfg->input_pins[AUTO_PIN_MIC] =
  2940. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  2941. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  2942. }
  2943. /* ditto for line-in */
  2944. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  2945. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  2946. cfg->input_pins[AUTO_PIN_LINE] =
  2947. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  2948. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  2949. }
  2950. /*
  2951. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  2952. * as a primary output
  2953. */
  2954. if (!cfg->line_outs) {
  2955. if (cfg->speaker_outs) {
  2956. cfg->line_outs = cfg->speaker_outs;
  2957. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  2958. sizeof(cfg->speaker_pins));
  2959. cfg->speaker_outs = 0;
  2960. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  2961. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  2962. } else if (cfg->hp_outs) {
  2963. cfg->line_outs = cfg->hp_outs;
  2964. memcpy(cfg->line_out_pins, cfg->hp_pins,
  2965. sizeof(cfg->hp_pins));
  2966. cfg->hp_outs = 0;
  2967. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  2968. cfg->line_out_type = AUTO_PIN_HP_OUT;
  2969. }
  2970. }
  2971. /* Reorder the surround channels
  2972. * ALSA sequence is front/surr/clfe/side
  2973. * HDA sequence is:
  2974. * 4-ch: front/surr => OK as it is
  2975. * 6-ch: front/clfe/surr
  2976. * 8-ch: front/clfe/rear/side|fc
  2977. */
  2978. switch (cfg->line_outs) {
  2979. case 3:
  2980. case 4:
  2981. nid = cfg->line_out_pins[1];
  2982. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  2983. cfg->line_out_pins[2] = nid;
  2984. break;
  2985. }
  2986. /*
  2987. * debug prints of the parsed results
  2988. */
  2989. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2990. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  2991. cfg->line_out_pins[2], cfg->line_out_pins[3],
  2992. cfg->line_out_pins[4]);
  2993. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2994. cfg->speaker_outs, cfg->speaker_pins[0],
  2995. cfg->speaker_pins[1], cfg->speaker_pins[2],
  2996. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  2997. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  2998. cfg->hp_outs, cfg->hp_pins[0],
  2999. cfg->hp_pins[1], cfg->hp_pins[2],
  3000. cfg->hp_pins[3], cfg->hp_pins[4]);
  3001. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  3002. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  3003. " cd=0x%x, aux=0x%x\n",
  3004. cfg->input_pins[AUTO_PIN_MIC],
  3005. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  3006. cfg->input_pins[AUTO_PIN_LINE],
  3007. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  3008. cfg->input_pins[AUTO_PIN_CD],
  3009. cfg->input_pins[AUTO_PIN_AUX]);
  3010. return 0;
  3011. }
  3012. /* labels for input pins */
  3013. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  3014. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  3015. };
  3016. #ifdef CONFIG_PM
  3017. /*
  3018. * power management
  3019. */
  3020. /**
  3021. * snd_hda_suspend - suspend the codecs
  3022. * @bus: the HDA bus
  3023. * @state: suspsend state
  3024. *
  3025. * Returns 0 if successful.
  3026. */
  3027. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  3028. {
  3029. struct hda_codec *codec;
  3030. list_for_each_entry(codec, &bus->codec_list, list) {
  3031. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3032. if (!codec->power_on)
  3033. continue;
  3034. #endif
  3035. hda_call_codec_suspend(codec);
  3036. }
  3037. return 0;
  3038. }
  3039. /**
  3040. * snd_hda_resume - resume the codecs
  3041. * @bus: the HDA bus
  3042. *
  3043. * Returns 0 if successful.
  3044. *
  3045. * This fucntion is defined only when POWER_SAVE isn't set.
  3046. * In the power-save mode, the codec is resumed dynamically.
  3047. */
  3048. int snd_hda_resume(struct hda_bus *bus)
  3049. {
  3050. struct hda_codec *codec;
  3051. list_for_each_entry(codec, &bus->codec_list, list) {
  3052. if (snd_hda_codec_needs_resume(codec))
  3053. hda_call_codec_resume(codec);
  3054. }
  3055. return 0;
  3056. }
  3057. #endif
  3058. /*
  3059. * generic arrays
  3060. */
  3061. /* get a new element from the given array
  3062. * if it exceeds the pre-allocated array size, re-allocate the array
  3063. */
  3064. void *snd_array_new(struct snd_array *array)
  3065. {
  3066. if (array->used >= array->alloced) {
  3067. int num = array->alloced + array->alloc_align;
  3068. void *nlist;
  3069. if (snd_BUG_ON(num >= 4096))
  3070. return NULL;
  3071. nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
  3072. if (!nlist)
  3073. return NULL;
  3074. if (array->list) {
  3075. memcpy(nlist, array->list,
  3076. array->elem_size * array->alloced);
  3077. kfree(array->list);
  3078. }
  3079. array->list = nlist;
  3080. array->alloced = num;
  3081. }
  3082. return snd_array_elem(array, array->used++);
  3083. }
  3084. /* free the given array elements */
  3085. void snd_array_free(struct snd_array *array)
  3086. {
  3087. kfree(array->list);
  3088. array->used = 0;
  3089. array->alloced = 0;
  3090. array->list = NULL;
  3091. }
  3092. /*
  3093. * used by hda_proc.c and hda_eld.c
  3094. */
  3095. void snd_print_pcm_rates(int pcm, char *buf, int buflen)
  3096. {
  3097. static unsigned int rates[] = {
  3098. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  3099. 96000, 176400, 192000, 384000
  3100. };
  3101. int i, j;
  3102. for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
  3103. if (pcm & (1 << i))
  3104. j += snprintf(buf + j, buflen - j, " %d", rates[i]);
  3105. buf[j] = '\0'; /* necessary when j == 0 */
  3106. }
  3107. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3108. {
  3109. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3110. int i, j;
  3111. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3112. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3113. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  3114. buf[j] = '\0'; /* necessary when j == 0 */
  3115. }