hda_codec.c 103 KB

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