hda_codec.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370
  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 <linux/module.h>
  27. #include <sound/core.h>
  28. #include "hda_codec.h"
  29. #include <sound/asoundef.h>
  30. #include <sound/tlv.h>
  31. #include <sound/initval.h>
  32. #include <sound/jack.h>
  33. #include "hda_local.h"
  34. #include "hda_beep.h"
  35. #include "hda_jack.h"
  36. #include <sound/hda_hwdep.h>
  37. #define CREATE_TRACE_POINTS
  38. #include "hda_trace.h"
  39. /*
  40. * vendor / preset table
  41. */
  42. struct hda_vendor_id {
  43. unsigned int id;
  44. const char *name;
  45. };
  46. /* codec vendor labels */
  47. static struct hda_vendor_id hda_vendor_ids[] = {
  48. { 0x1002, "ATI" },
  49. { 0x1013, "Cirrus Logic" },
  50. { 0x1057, "Motorola" },
  51. { 0x1095, "Silicon Image" },
  52. { 0x10de, "Nvidia" },
  53. { 0x10ec, "Realtek" },
  54. { 0x1102, "Creative" },
  55. { 0x1106, "VIA" },
  56. { 0x111d, "IDT" },
  57. { 0x11c1, "LSI" },
  58. { 0x11d4, "Analog Devices" },
  59. { 0x13f6, "C-Media" },
  60. { 0x14f1, "Conexant" },
  61. { 0x17e8, "Chrontel" },
  62. { 0x1854, "LG" },
  63. { 0x1aec, "Wolfson Microelectronics" },
  64. { 0x434d, "C-Media" },
  65. { 0x8086, "Intel" },
  66. { 0x8384, "SigmaTel" },
  67. {} /* terminator */
  68. };
  69. static DEFINE_MUTEX(preset_mutex);
  70. static LIST_HEAD(hda_preset_tables);
  71. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  72. {
  73. mutex_lock(&preset_mutex);
  74. list_add_tail(&preset->list, &hda_preset_tables);
  75. mutex_unlock(&preset_mutex);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
  79. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  80. {
  81. mutex_lock(&preset_mutex);
  82. list_del(&preset->list);
  83. mutex_unlock(&preset_mutex);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
  87. #ifdef CONFIG_SND_HDA_POWER_SAVE
  88. static void hda_power_work(struct work_struct *work);
  89. static void hda_keep_power_on(struct hda_codec *codec);
  90. #define hda_codec_is_power_on(codec) ((codec)->power_on)
  91. #else
  92. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  93. #define hda_codec_is_power_on(codec) 1
  94. #endif
  95. /**
  96. * snd_hda_get_jack_location - Give a location string of the jack
  97. * @cfg: pin default config value
  98. *
  99. * Parse the pin default config value and returns the string of the
  100. * jack location, e.g. "Rear", "Front", etc.
  101. */
  102. const char *snd_hda_get_jack_location(u32 cfg)
  103. {
  104. static char *bases[7] = {
  105. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  106. };
  107. static unsigned char specials_idx[] = {
  108. 0x07, 0x08,
  109. 0x17, 0x18, 0x19,
  110. 0x37, 0x38
  111. };
  112. static char *specials[] = {
  113. "Rear Panel", "Drive Bar",
  114. "Riser", "HDMI", "ATAPI",
  115. "Mobile-In", "Mobile-Out"
  116. };
  117. int i;
  118. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  119. if ((cfg & 0x0f) < 7)
  120. return bases[cfg & 0x0f];
  121. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  122. if (cfg == specials_idx[i])
  123. return specials[i];
  124. }
  125. return "UNKNOWN";
  126. }
  127. EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
  128. /**
  129. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  130. * @cfg: pin default config value
  131. *
  132. * Parse the pin default config value and returns the string of the
  133. * jack connectivity, i.e. external or internal connection.
  134. */
  135. const char *snd_hda_get_jack_connectivity(u32 cfg)
  136. {
  137. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  138. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  139. }
  140. EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
  141. /**
  142. * snd_hda_get_jack_type - Give a type string of the jack
  143. * @cfg: pin default config value
  144. *
  145. * Parse the pin default config value and returns the string of the
  146. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  147. */
  148. const char *snd_hda_get_jack_type(u32 cfg)
  149. {
  150. static char *jack_types[16] = {
  151. "Line Out", "Speaker", "HP Out", "CD",
  152. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  153. "Line In", "Aux", "Mic", "Telephony",
  154. "SPDIF In", "Digitial In", "Reserved", "Other"
  155. };
  156. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  157. >> AC_DEFCFG_DEVICE_SHIFT];
  158. }
  159. EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
  160. /*
  161. * Compose a 32bit command word to be sent to the HD-audio controller
  162. */
  163. static inline unsigned int
  164. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  165. unsigned int verb, unsigned int parm)
  166. {
  167. u32 val;
  168. if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
  169. (verb & ~0xfff) || (parm & ~0xffff)) {
  170. printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
  171. codec->addr, direct, nid, verb, parm);
  172. return ~0;
  173. }
  174. val = (u32)codec->addr << 28;
  175. val |= (u32)direct << 27;
  176. val |= (u32)nid << 20;
  177. val |= verb << 8;
  178. val |= parm;
  179. return val;
  180. }
  181. /*
  182. * Send and receive a verb
  183. */
  184. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  185. unsigned int *res)
  186. {
  187. struct hda_bus *bus = codec->bus;
  188. int err;
  189. if (cmd == ~0)
  190. return -1;
  191. if (res)
  192. *res = -1;
  193. again:
  194. snd_hda_power_up(codec);
  195. mutex_lock(&bus->cmd_mutex);
  196. trace_hda_send_cmd(codec, cmd);
  197. err = bus->ops.command(bus, cmd);
  198. if (!err && res) {
  199. *res = bus->ops.get_response(bus, codec->addr);
  200. trace_hda_get_response(codec, *res);
  201. }
  202. mutex_unlock(&bus->cmd_mutex);
  203. snd_hda_power_down(codec);
  204. if (res && *res == -1 && bus->rirb_error) {
  205. if (bus->response_reset) {
  206. snd_printd("hda_codec: resetting BUS due to "
  207. "fatal communication error\n");
  208. trace_hda_bus_reset(bus);
  209. bus->ops.bus_reset(bus);
  210. }
  211. goto again;
  212. }
  213. /* clear reset-flag when the communication gets recovered */
  214. if (!err)
  215. bus->response_reset = 0;
  216. return err;
  217. }
  218. /**
  219. * snd_hda_codec_read - send a command and get the response
  220. * @codec: the HDA codec
  221. * @nid: NID to send the command
  222. * @direct: direct flag
  223. * @verb: the verb to send
  224. * @parm: the parameter for the verb
  225. *
  226. * Send a single command and read the corresponding response.
  227. *
  228. * Returns the obtained response value, or -1 for an error.
  229. */
  230. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  231. int direct,
  232. unsigned int verb, unsigned int parm)
  233. {
  234. unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  235. unsigned int res;
  236. if (codec_exec_verb(codec, cmd, &res))
  237. return -1;
  238. return res;
  239. }
  240. EXPORT_SYMBOL_HDA(snd_hda_codec_read);
  241. /**
  242. * snd_hda_codec_write - send a single command without waiting for response
  243. * @codec: the HDA codec
  244. * @nid: NID to send the command
  245. * @direct: direct flag
  246. * @verb: the verb to send
  247. * @parm: the parameter for the verb
  248. *
  249. * Send a single command without waiting for response.
  250. *
  251. * Returns 0 if successful, or a negative error code.
  252. */
  253. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  254. unsigned int verb, unsigned int parm)
  255. {
  256. unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  257. unsigned int res;
  258. return codec_exec_verb(codec, cmd,
  259. codec->bus->sync_write ? &res : NULL);
  260. }
  261. EXPORT_SYMBOL_HDA(snd_hda_codec_write);
  262. /**
  263. * snd_hda_sequence_write - sequence writes
  264. * @codec: the HDA codec
  265. * @seq: VERB array to send
  266. *
  267. * Send the commands sequentially from the given array.
  268. * The array must be terminated with NID=0.
  269. */
  270. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  271. {
  272. for (; seq->nid; seq++)
  273. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  274. }
  275. EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
  276. /**
  277. * snd_hda_get_sub_nodes - get the range of sub nodes
  278. * @codec: the HDA codec
  279. * @nid: NID to parse
  280. * @start_id: the pointer to store the start NID
  281. *
  282. * Parse the NID and store the start NID of its sub-nodes.
  283. * Returns the number of sub-nodes.
  284. */
  285. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  286. hda_nid_t *start_id)
  287. {
  288. unsigned int parm;
  289. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  290. if (parm == -1)
  291. return 0;
  292. *start_id = (parm >> 16) & 0x7fff;
  293. return (int)(parm & 0x7fff);
  294. }
  295. EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
  296. /* look up the cached results */
  297. static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
  298. {
  299. int i, len;
  300. for (i = 0; i < array->used; ) {
  301. hda_nid_t *p = snd_array_elem(array, i);
  302. if (nid == *p)
  303. return p;
  304. len = p[1];
  305. i += len + 2;
  306. }
  307. return NULL;
  308. }
  309. /**
  310. * snd_hda_get_conn_list - get connection list
  311. * @codec: the HDA codec
  312. * @nid: NID to parse
  313. * @listp: the pointer to store NID list
  314. *
  315. * Parses the connection list of the given widget and stores the list
  316. * of NIDs.
  317. *
  318. * Returns the number of connections, or a negative error code.
  319. */
  320. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  321. const hda_nid_t **listp)
  322. {
  323. struct snd_array *array = &codec->conn_lists;
  324. int len, err;
  325. hda_nid_t list[HDA_MAX_CONNECTIONS];
  326. hda_nid_t *p;
  327. bool added = false;
  328. again:
  329. /* if the connection-list is already cached, read it */
  330. p = lookup_conn_list(array, nid);
  331. if (p) {
  332. if (listp)
  333. *listp = p + 2;
  334. return p[1];
  335. }
  336. if (snd_BUG_ON(added))
  337. return -EINVAL;
  338. /* read the connection and add to the cache */
  339. len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
  340. if (len < 0)
  341. return len;
  342. err = snd_hda_override_conn_list(codec, nid, len, list);
  343. if (err < 0)
  344. return err;
  345. added = true;
  346. goto again;
  347. }
  348. EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
  349. /**
  350. * snd_hda_get_connections - copy connection list
  351. * @codec: the HDA codec
  352. * @nid: NID to parse
  353. * @conn_list: connection list array
  354. * @max_conns: max. number of connections to store
  355. *
  356. * Parses the connection list of the given widget and stores the list
  357. * of NIDs.
  358. *
  359. * Returns the number of connections, or a negative error code.
  360. */
  361. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  362. hda_nid_t *conn_list, int max_conns)
  363. {
  364. const hda_nid_t *list;
  365. int len = snd_hda_get_conn_list(codec, nid, &list);
  366. if (len <= 0)
  367. return len;
  368. if (len > max_conns) {
  369. snd_printk(KERN_ERR "hda_codec: "
  370. "Too many connections %d for NID 0x%x\n",
  371. len, nid);
  372. return -EINVAL;
  373. }
  374. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  375. return len;
  376. }
  377. EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  378. /**
  379. * snd_hda_get_raw_connections - copy connection list without cache
  380. * @codec: the HDA codec
  381. * @nid: NID to parse
  382. * @conn_list: connection list array
  383. * @max_conns: max. number of connections to store
  384. *
  385. * Like snd_hda_get_connections(), copy the connection list but without
  386. * checking through the connection-list cache.
  387. * Currently called only from hda_proc.c, so not exported.
  388. */
  389. int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
  390. hda_nid_t *conn_list, int max_conns)
  391. {
  392. unsigned int parm;
  393. int i, conn_len, conns;
  394. unsigned int shift, num_elems, mask;
  395. unsigned int wcaps;
  396. hda_nid_t prev_nid;
  397. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  398. return -EINVAL;
  399. wcaps = get_wcaps(codec, nid);
  400. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  401. get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
  402. return 0;
  403. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  404. if (parm & AC_CLIST_LONG) {
  405. /* long form */
  406. shift = 16;
  407. num_elems = 2;
  408. } else {
  409. /* short form */
  410. shift = 8;
  411. num_elems = 4;
  412. }
  413. conn_len = parm & AC_CLIST_LENGTH;
  414. mask = (1 << (shift-1)) - 1;
  415. if (!conn_len)
  416. return 0; /* no connection */
  417. if (conn_len == 1) {
  418. /* single connection */
  419. parm = snd_hda_codec_read(codec, nid, 0,
  420. AC_VERB_GET_CONNECT_LIST, 0);
  421. if (parm == -1 && codec->bus->rirb_error)
  422. return -EIO;
  423. conn_list[0] = parm & mask;
  424. return 1;
  425. }
  426. /* multi connection */
  427. conns = 0;
  428. prev_nid = 0;
  429. for (i = 0; i < conn_len; i++) {
  430. int range_val;
  431. hda_nid_t val, n;
  432. if (i % num_elems == 0) {
  433. parm = snd_hda_codec_read(codec, nid, 0,
  434. AC_VERB_GET_CONNECT_LIST, i);
  435. if (parm == -1 && codec->bus->rirb_error)
  436. return -EIO;
  437. }
  438. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  439. val = parm & mask;
  440. if (val == 0) {
  441. snd_printk(KERN_WARNING "hda_codec: "
  442. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  443. nid, i, parm);
  444. return 0;
  445. }
  446. parm >>= shift;
  447. if (range_val) {
  448. /* ranges between the previous and this one */
  449. if (!prev_nid || prev_nid >= val) {
  450. snd_printk(KERN_WARNING "hda_codec: "
  451. "invalid dep_range_val %x:%x\n",
  452. prev_nid, val);
  453. continue;
  454. }
  455. for (n = prev_nid + 1; n <= val; n++) {
  456. if (conns >= max_conns) {
  457. snd_printk(KERN_ERR "hda_codec: "
  458. "Too many connections %d for NID 0x%x\n",
  459. conns, nid);
  460. return -EINVAL;
  461. }
  462. conn_list[conns++] = n;
  463. }
  464. } else {
  465. if (conns >= max_conns) {
  466. snd_printk(KERN_ERR "hda_codec: "
  467. "Too many connections %d for NID 0x%x\n",
  468. conns, nid);
  469. return -EINVAL;
  470. }
  471. conn_list[conns++] = val;
  472. }
  473. prev_nid = val;
  474. }
  475. return conns;
  476. }
  477. static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
  478. {
  479. hda_nid_t *p = snd_array_new(array);
  480. if (!p)
  481. return false;
  482. *p = nid;
  483. return true;
  484. }
  485. /**
  486. * snd_hda_override_conn_list - add/modify the connection-list to cache
  487. * @codec: the HDA codec
  488. * @nid: NID to parse
  489. * @len: number of connection list entries
  490. * @list: the list of connection entries
  491. *
  492. * Add or modify the given connection-list to the cache. If the corresponding
  493. * cache already exists, invalidate it and append a new one.
  494. *
  495. * Returns zero or a negative error code.
  496. */
  497. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  498. const hda_nid_t *list)
  499. {
  500. struct snd_array *array = &codec->conn_lists;
  501. hda_nid_t *p;
  502. int i, old_used;
  503. p = lookup_conn_list(array, nid);
  504. if (p)
  505. *p = -1; /* invalidate the old entry */
  506. old_used = array->used;
  507. if (!add_conn_list(array, nid) || !add_conn_list(array, len))
  508. goto error_add;
  509. for (i = 0; i < len; i++)
  510. if (!add_conn_list(array, list[i]))
  511. goto error_add;
  512. return 0;
  513. error_add:
  514. array->used = old_used;
  515. return -ENOMEM;
  516. }
  517. EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
  518. /**
  519. * snd_hda_get_conn_index - get the connection index of the given NID
  520. * @codec: the HDA codec
  521. * @mux: NID containing the list
  522. * @nid: NID to select
  523. * @recursive: 1 when searching NID recursively, otherwise 0
  524. *
  525. * Parses the connection list of the widget @mux and checks whether the
  526. * widget @nid is present. If it is, return the connection index.
  527. * Otherwise it returns -1.
  528. */
  529. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  530. hda_nid_t nid, int recursive)
  531. {
  532. hda_nid_t conn[HDA_MAX_NUM_INPUTS];
  533. int i, nums;
  534. nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
  535. for (i = 0; i < nums; i++)
  536. if (conn[i] == nid)
  537. return i;
  538. if (!recursive)
  539. return -1;
  540. if (recursive > 5) {
  541. snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
  542. return -1;
  543. }
  544. recursive++;
  545. for (i = 0; i < nums; i++) {
  546. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  547. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  548. continue;
  549. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  550. return i;
  551. }
  552. return -1;
  553. }
  554. EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
  555. /**
  556. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  557. * @bus: the BUS
  558. * @res: unsolicited event (lower 32bit of RIRB entry)
  559. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  560. *
  561. * Adds the given event to the queue. The events are processed in
  562. * the workqueue asynchronously. Call this function in the interrupt
  563. * hanlder when RIRB receives an unsolicited event.
  564. *
  565. * Returns 0 if successful, or a negative error code.
  566. */
  567. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  568. {
  569. struct hda_bus_unsolicited *unsol;
  570. unsigned int wp;
  571. trace_hda_unsol_event(bus, res, res_ex);
  572. unsol = bus->unsol;
  573. if (!unsol)
  574. return 0;
  575. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  576. unsol->wp = wp;
  577. wp <<= 1;
  578. unsol->queue[wp] = res;
  579. unsol->queue[wp + 1] = res_ex;
  580. queue_work(bus->workq, &unsol->work);
  581. return 0;
  582. }
  583. EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
  584. /*
  585. * process queued unsolicited events
  586. */
  587. static void process_unsol_events(struct work_struct *work)
  588. {
  589. struct hda_bus_unsolicited *unsol =
  590. container_of(work, struct hda_bus_unsolicited, work);
  591. struct hda_bus *bus = unsol->bus;
  592. struct hda_codec *codec;
  593. unsigned int rp, caddr, res;
  594. while (unsol->rp != unsol->wp) {
  595. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  596. unsol->rp = rp;
  597. rp <<= 1;
  598. res = unsol->queue[rp];
  599. caddr = unsol->queue[rp + 1];
  600. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  601. continue;
  602. codec = bus->caddr_tbl[caddr & 0x0f];
  603. if (codec && codec->patch_ops.unsol_event)
  604. codec->patch_ops.unsol_event(codec, res);
  605. }
  606. }
  607. /*
  608. * initialize unsolicited queue
  609. */
  610. static int init_unsol_queue(struct hda_bus *bus)
  611. {
  612. struct hda_bus_unsolicited *unsol;
  613. if (bus->unsol) /* already initialized */
  614. return 0;
  615. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  616. if (!unsol) {
  617. snd_printk(KERN_ERR "hda_codec: "
  618. "can't allocate unsolicited queue\n");
  619. return -ENOMEM;
  620. }
  621. INIT_WORK(&unsol->work, process_unsol_events);
  622. unsol->bus = bus;
  623. bus->unsol = unsol;
  624. return 0;
  625. }
  626. /*
  627. * destructor
  628. */
  629. static void snd_hda_codec_free(struct hda_codec *codec);
  630. static int snd_hda_bus_free(struct hda_bus *bus)
  631. {
  632. struct hda_codec *codec, *n;
  633. if (!bus)
  634. return 0;
  635. if (bus->workq)
  636. flush_workqueue(bus->workq);
  637. if (bus->unsol)
  638. kfree(bus->unsol);
  639. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  640. snd_hda_codec_free(codec);
  641. }
  642. if (bus->ops.private_free)
  643. bus->ops.private_free(bus);
  644. if (bus->workq)
  645. destroy_workqueue(bus->workq);
  646. kfree(bus);
  647. return 0;
  648. }
  649. static int snd_hda_bus_dev_free(struct snd_device *device)
  650. {
  651. struct hda_bus *bus = device->device_data;
  652. bus->shutdown = 1;
  653. return snd_hda_bus_free(bus);
  654. }
  655. #ifdef CONFIG_SND_HDA_HWDEP
  656. static int snd_hda_bus_dev_register(struct snd_device *device)
  657. {
  658. struct hda_bus *bus = device->device_data;
  659. struct hda_codec *codec;
  660. list_for_each_entry(codec, &bus->codec_list, list) {
  661. snd_hda_hwdep_add_sysfs(codec);
  662. snd_hda_hwdep_add_power_sysfs(codec);
  663. }
  664. return 0;
  665. }
  666. #else
  667. #define snd_hda_bus_dev_register NULL
  668. #endif
  669. /**
  670. * snd_hda_bus_new - create a HDA bus
  671. * @card: the card entry
  672. * @temp: the template for hda_bus information
  673. * @busp: the pointer to store the created bus instance
  674. *
  675. * Returns 0 if successful, or a negative error code.
  676. */
  677. int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
  678. const struct hda_bus_template *temp,
  679. struct hda_bus **busp)
  680. {
  681. struct hda_bus *bus;
  682. int err;
  683. static struct snd_device_ops dev_ops = {
  684. .dev_register = snd_hda_bus_dev_register,
  685. .dev_free = snd_hda_bus_dev_free,
  686. };
  687. if (snd_BUG_ON(!temp))
  688. return -EINVAL;
  689. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  690. return -EINVAL;
  691. if (busp)
  692. *busp = NULL;
  693. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  694. if (bus == NULL) {
  695. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  696. return -ENOMEM;
  697. }
  698. bus->card = card;
  699. bus->private_data = temp->private_data;
  700. bus->pci = temp->pci;
  701. bus->modelname = temp->modelname;
  702. bus->power_save = temp->power_save;
  703. bus->ops = temp->ops;
  704. mutex_init(&bus->cmd_mutex);
  705. mutex_init(&bus->prepare_mutex);
  706. INIT_LIST_HEAD(&bus->codec_list);
  707. snprintf(bus->workq_name, sizeof(bus->workq_name),
  708. "hd-audio%d", card->number);
  709. bus->workq = create_singlethread_workqueue(bus->workq_name);
  710. if (!bus->workq) {
  711. snd_printk(KERN_ERR "cannot create workqueue %s\n",
  712. bus->workq_name);
  713. kfree(bus);
  714. return -ENOMEM;
  715. }
  716. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  717. if (err < 0) {
  718. snd_hda_bus_free(bus);
  719. return err;
  720. }
  721. if (busp)
  722. *busp = bus;
  723. return 0;
  724. }
  725. EXPORT_SYMBOL_HDA(snd_hda_bus_new);
  726. #ifdef CONFIG_SND_HDA_GENERIC
  727. #define is_generic_config(codec) \
  728. (codec->modelname && !strcmp(codec->modelname, "generic"))
  729. #else
  730. #define is_generic_config(codec) 0
  731. #endif
  732. #ifdef MODULE
  733. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  734. #else
  735. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  736. #endif
  737. /*
  738. * find a matching codec preset
  739. */
  740. static const struct hda_codec_preset *
  741. find_codec_preset(struct hda_codec *codec)
  742. {
  743. struct hda_codec_preset_list *tbl;
  744. const struct hda_codec_preset *preset;
  745. int mod_requested = 0;
  746. if (is_generic_config(codec))
  747. return NULL; /* use the generic parser */
  748. again:
  749. mutex_lock(&preset_mutex);
  750. list_for_each_entry(tbl, &hda_preset_tables, list) {
  751. if (!try_module_get(tbl->owner)) {
  752. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  753. continue;
  754. }
  755. for (preset = tbl->preset; preset->id; preset++) {
  756. u32 mask = preset->mask;
  757. if (preset->afg && preset->afg != codec->afg)
  758. continue;
  759. if (preset->mfg && preset->mfg != codec->mfg)
  760. continue;
  761. if (!mask)
  762. mask = ~0;
  763. if (preset->id == (codec->vendor_id & mask) &&
  764. (!preset->rev ||
  765. preset->rev == codec->revision_id)) {
  766. mutex_unlock(&preset_mutex);
  767. codec->owner = tbl->owner;
  768. return preset;
  769. }
  770. }
  771. module_put(tbl->owner);
  772. }
  773. mutex_unlock(&preset_mutex);
  774. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  775. char name[32];
  776. if (!mod_requested)
  777. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  778. codec->vendor_id);
  779. else
  780. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  781. (codec->vendor_id >> 16) & 0xffff);
  782. request_module(name);
  783. mod_requested++;
  784. goto again;
  785. }
  786. return NULL;
  787. }
  788. /*
  789. * get_codec_name - store the codec name
  790. */
  791. static int get_codec_name(struct hda_codec *codec)
  792. {
  793. const struct hda_vendor_id *c;
  794. const char *vendor = NULL;
  795. u16 vendor_id = codec->vendor_id >> 16;
  796. char tmp[16];
  797. if (codec->vendor_name)
  798. goto get_chip_name;
  799. for (c = hda_vendor_ids; c->id; c++) {
  800. if (c->id == vendor_id) {
  801. vendor = c->name;
  802. break;
  803. }
  804. }
  805. if (!vendor) {
  806. sprintf(tmp, "Generic %04x", vendor_id);
  807. vendor = tmp;
  808. }
  809. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  810. if (!codec->vendor_name)
  811. return -ENOMEM;
  812. get_chip_name:
  813. if (codec->chip_name)
  814. return 0;
  815. if (codec->preset && codec->preset->name)
  816. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  817. else {
  818. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  819. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  820. }
  821. if (!codec->chip_name)
  822. return -ENOMEM;
  823. return 0;
  824. }
  825. /*
  826. * look for an AFG and MFG nodes
  827. */
  828. static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
  829. {
  830. int i, total_nodes, function_id;
  831. hda_nid_t nid;
  832. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  833. for (i = 0; i < total_nodes; i++, nid++) {
  834. function_id = snd_hda_param_read(codec, nid,
  835. AC_PAR_FUNCTION_TYPE);
  836. switch (function_id & 0xff) {
  837. case AC_GRP_AUDIO_FUNCTION:
  838. codec->afg = nid;
  839. codec->afg_function_id = function_id & 0xff;
  840. codec->afg_unsol = (function_id >> 8) & 1;
  841. break;
  842. case AC_GRP_MODEM_FUNCTION:
  843. codec->mfg = nid;
  844. codec->mfg_function_id = function_id & 0xff;
  845. codec->mfg_unsol = (function_id >> 8) & 1;
  846. break;
  847. default:
  848. break;
  849. }
  850. }
  851. }
  852. /*
  853. * read widget caps for each widget and store in cache
  854. */
  855. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  856. {
  857. int i;
  858. hda_nid_t nid;
  859. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  860. &codec->start_nid);
  861. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  862. if (!codec->wcaps)
  863. return -ENOMEM;
  864. nid = codec->start_nid;
  865. for (i = 0; i < codec->num_nodes; i++, nid++)
  866. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  867. AC_PAR_AUDIO_WIDGET_CAP);
  868. return 0;
  869. }
  870. /* read all pin default configurations and save codec->init_pins */
  871. static int read_pin_defaults(struct hda_codec *codec)
  872. {
  873. int i;
  874. hda_nid_t nid = codec->start_nid;
  875. for (i = 0; i < codec->num_nodes; i++, nid++) {
  876. struct hda_pincfg *pin;
  877. unsigned int wcaps = get_wcaps(codec, nid);
  878. unsigned int wid_type = get_wcaps_type(wcaps);
  879. if (wid_type != AC_WID_PIN)
  880. continue;
  881. pin = snd_array_new(&codec->init_pins);
  882. if (!pin)
  883. return -ENOMEM;
  884. pin->nid = nid;
  885. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  886. AC_VERB_GET_CONFIG_DEFAULT, 0);
  887. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  888. AC_VERB_GET_PIN_WIDGET_CONTROL,
  889. 0);
  890. }
  891. return 0;
  892. }
  893. /* look up the given pin config list and return the item matching with NID */
  894. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  895. struct snd_array *array,
  896. hda_nid_t nid)
  897. {
  898. int i;
  899. for (i = 0; i < array->used; i++) {
  900. struct hda_pincfg *pin = snd_array_elem(array, i);
  901. if (pin->nid == nid)
  902. return pin;
  903. }
  904. return NULL;
  905. }
  906. /* write a config value for the given NID */
  907. static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
  908. unsigned int cfg)
  909. {
  910. int i;
  911. for (i = 0; i < 4; i++) {
  912. snd_hda_codec_write(codec, nid, 0,
  913. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
  914. cfg & 0xff);
  915. cfg >>= 8;
  916. }
  917. }
  918. /* set the current pin config value for the given NID.
  919. * the value is cached, and read via snd_hda_codec_get_pincfg()
  920. */
  921. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  922. hda_nid_t nid, unsigned int cfg)
  923. {
  924. struct hda_pincfg *pin;
  925. unsigned int oldcfg;
  926. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  927. return -EINVAL;
  928. oldcfg = snd_hda_codec_get_pincfg(codec, nid);
  929. pin = look_up_pincfg(codec, list, nid);
  930. if (!pin) {
  931. pin = snd_array_new(list);
  932. if (!pin)
  933. return -ENOMEM;
  934. pin->nid = nid;
  935. }
  936. pin->cfg = cfg;
  937. /* change only when needed; e.g. if the pincfg is already present
  938. * in user_pins[], don't write it
  939. */
  940. cfg = snd_hda_codec_get_pincfg(codec, nid);
  941. if (oldcfg != cfg)
  942. set_pincfg(codec, nid, cfg);
  943. return 0;
  944. }
  945. /**
  946. * snd_hda_codec_set_pincfg - Override a pin default configuration
  947. * @codec: the HDA codec
  948. * @nid: NID to set the pin config
  949. * @cfg: the pin default config value
  950. *
  951. * Override a pin default configuration value in the cache.
  952. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  953. * priority than the real hardware value.
  954. */
  955. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  956. hda_nid_t nid, unsigned int cfg)
  957. {
  958. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  959. }
  960. EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
  961. /**
  962. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  963. * @codec: the HDA codec
  964. * @nid: NID to get the pin config
  965. *
  966. * Get the current pin config value of the given pin NID.
  967. * If the pincfg value is cached or overridden via sysfs or driver,
  968. * returns the cached value.
  969. */
  970. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  971. {
  972. struct hda_pincfg *pin;
  973. #ifdef CONFIG_SND_HDA_HWDEP
  974. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  975. if (pin)
  976. return pin->cfg;
  977. #endif
  978. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  979. if (pin)
  980. return pin->cfg;
  981. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  982. if (pin)
  983. return pin->cfg;
  984. return 0;
  985. }
  986. EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
  987. /* restore all current pin configs */
  988. static void restore_pincfgs(struct hda_codec *codec)
  989. {
  990. int i;
  991. for (i = 0; i < codec->init_pins.used; i++) {
  992. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  993. set_pincfg(codec, pin->nid,
  994. snd_hda_codec_get_pincfg(codec, pin->nid));
  995. }
  996. }
  997. /**
  998. * snd_hda_shutup_pins - Shut up all pins
  999. * @codec: the HDA codec
  1000. *
  1001. * Clear all pin controls to shup up before suspend for avoiding click noise.
  1002. * The controls aren't cached so that they can be resumed properly.
  1003. */
  1004. void snd_hda_shutup_pins(struct hda_codec *codec)
  1005. {
  1006. int i;
  1007. /* don't shut up pins when unloading the driver; otherwise it breaks
  1008. * the default pin setup at the next load of the driver
  1009. */
  1010. if (codec->bus->shutdown)
  1011. return;
  1012. for (i = 0; i < codec->init_pins.used; i++) {
  1013. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1014. /* use read here for syncing after issuing each verb */
  1015. snd_hda_codec_read(codec, pin->nid, 0,
  1016. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  1017. }
  1018. codec->pins_shutup = 1;
  1019. }
  1020. EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
  1021. #ifdef CONFIG_PM
  1022. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  1023. static void restore_shutup_pins(struct hda_codec *codec)
  1024. {
  1025. int i;
  1026. if (!codec->pins_shutup)
  1027. return;
  1028. if (codec->bus->shutdown)
  1029. return;
  1030. for (i = 0; i < codec->init_pins.used; i++) {
  1031. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1032. snd_hda_codec_write(codec, pin->nid, 0,
  1033. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1034. pin->ctrl);
  1035. }
  1036. codec->pins_shutup = 0;
  1037. }
  1038. #endif
  1039. static void init_hda_cache(struct hda_cache_rec *cache,
  1040. unsigned int record_size);
  1041. static void free_hda_cache(struct hda_cache_rec *cache);
  1042. /* restore the initial pin cfgs and release all pincfg lists */
  1043. static void restore_init_pincfgs(struct hda_codec *codec)
  1044. {
  1045. /* first free driver_pins and user_pins, then call restore_pincfg
  1046. * so that only the values in init_pins are restored
  1047. */
  1048. snd_array_free(&codec->driver_pins);
  1049. #ifdef CONFIG_SND_HDA_HWDEP
  1050. snd_array_free(&codec->user_pins);
  1051. #endif
  1052. restore_pincfgs(codec);
  1053. snd_array_free(&codec->init_pins);
  1054. }
  1055. /*
  1056. * audio-converter setup caches
  1057. */
  1058. struct hda_cvt_setup {
  1059. hda_nid_t nid;
  1060. u8 stream_tag;
  1061. u8 channel_id;
  1062. u16 format_id;
  1063. unsigned char active; /* cvt is currently used */
  1064. unsigned char dirty; /* setups should be cleared */
  1065. };
  1066. /* get or create a cache entry for the given audio converter NID */
  1067. static struct hda_cvt_setup *
  1068. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  1069. {
  1070. struct hda_cvt_setup *p;
  1071. int i;
  1072. for (i = 0; i < codec->cvt_setups.used; i++) {
  1073. p = snd_array_elem(&codec->cvt_setups, i);
  1074. if (p->nid == nid)
  1075. return p;
  1076. }
  1077. p = snd_array_new(&codec->cvt_setups);
  1078. if (p)
  1079. p->nid = nid;
  1080. return p;
  1081. }
  1082. /*
  1083. * codec destructor
  1084. */
  1085. static void snd_hda_codec_free(struct hda_codec *codec)
  1086. {
  1087. if (!codec)
  1088. return;
  1089. restore_init_pincfgs(codec);
  1090. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1091. cancel_delayed_work(&codec->power_work);
  1092. flush_workqueue(codec->bus->workq);
  1093. #endif
  1094. list_del(&codec->list);
  1095. snd_array_free(&codec->mixers);
  1096. snd_array_free(&codec->nids);
  1097. snd_array_free(&codec->conn_lists);
  1098. snd_array_free(&codec->spdif_out);
  1099. codec->bus->caddr_tbl[codec->addr] = NULL;
  1100. if (codec->patch_ops.free)
  1101. codec->patch_ops.free(codec);
  1102. module_put(codec->owner);
  1103. free_hda_cache(&codec->amp_cache);
  1104. free_hda_cache(&codec->cmd_cache);
  1105. kfree(codec->vendor_name);
  1106. kfree(codec->chip_name);
  1107. kfree(codec->modelname);
  1108. kfree(codec->wcaps);
  1109. kfree(codec);
  1110. }
  1111. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1112. unsigned int power_state);
  1113. /**
  1114. * snd_hda_codec_new - create a HDA codec
  1115. * @bus: the bus to assign
  1116. * @codec_addr: the codec address
  1117. * @codecp: the pointer to store the generated codec
  1118. *
  1119. * Returns 0 if successful, or a negative error code.
  1120. */
  1121. int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
  1122. unsigned int codec_addr,
  1123. struct hda_codec **codecp)
  1124. {
  1125. struct hda_codec *codec;
  1126. char component[31];
  1127. int err;
  1128. if (snd_BUG_ON(!bus))
  1129. return -EINVAL;
  1130. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  1131. return -EINVAL;
  1132. if (bus->caddr_tbl[codec_addr]) {
  1133. snd_printk(KERN_ERR "hda_codec: "
  1134. "address 0x%x is already occupied\n", codec_addr);
  1135. return -EBUSY;
  1136. }
  1137. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1138. if (codec == NULL) {
  1139. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  1140. return -ENOMEM;
  1141. }
  1142. codec->bus = bus;
  1143. codec->addr = codec_addr;
  1144. mutex_init(&codec->spdif_mutex);
  1145. mutex_init(&codec->control_mutex);
  1146. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1147. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1148. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  1149. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  1150. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  1151. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  1152. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  1153. snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
  1154. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  1155. if (codec->bus->modelname) {
  1156. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  1157. if (!codec->modelname) {
  1158. snd_hda_codec_free(codec);
  1159. return -ENODEV;
  1160. }
  1161. }
  1162. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1163. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  1164. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  1165. * the caller has to power down appropriatley after initialization
  1166. * phase.
  1167. */
  1168. hda_keep_power_on(codec);
  1169. #endif
  1170. list_add_tail(&codec->list, &bus->codec_list);
  1171. bus->caddr_tbl[codec_addr] = codec;
  1172. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1173. AC_PAR_VENDOR_ID);
  1174. if (codec->vendor_id == -1)
  1175. /* read again, hopefully the access method was corrected
  1176. * in the last read...
  1177. */
  1178. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1179. AC_PAR_VENDOR_ID);
  1180. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1181. AC_PAR_SUBSYSTEM_ID);
  1182. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1183. AC_PAR_REV_ID);
  1184. setup_fg_nodes(codec);
  1185. if (!codec->afg && !codec->mfg) {
  1186. snd_printdd("hda_codec: no AFG or MFG node found\n");
  1187. err = -ENODEV;
  1188. goto error;
  1189. }
  1190. err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
  1191. if (err < 0) {
  1192. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  1193. goto error;
  1194. }
  1195. err = read_pin_defaults(codec);
  1196. if (err < 0)
  1197. goto error;
  1198. if (!codec->subsystem_id) {
  1199. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  1200. codec->subsystem_id =
  1201. snd_hda_codec_read(codec, nid, 0,
  1202. AC_VERB_GET_SUBSYSTEM_ID, 0);
  1203. }
  1204. /* power-up all before initialization */
  1205. hda_set_power_state(codec,
  1206. codec->afg ? codec->afg : codec->mfg,
  1207. AC_PWRST_D0);
  1208. snd_hda_codec_proc_new(codec);
  1209. snd_hda_create_hwdep(codec);
  1210. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  1211. codec->subsystem_id, codec->revision_id);
  1212. snd_component_add(codec->bus->card, component);
  1213. if (codecp)
  1214. *codecp = codec;
  1215. return 0;
  1216. error:
  1217. snd_hda_codec_free(codec);
  1218. return err;
  1219. }
  1220. EXPORT_SYMBOL_HDA(snd_hda_codec_new);
  1221. /**
  1222. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  1223. * @codec: the HDA codec
  1224. *
  1225. * Start parsing of the given codec tree and (re-)initialize the whole
  1226. * patch instance.
  1227. *
  1228. * Returns 0 if successful or a negative error code.
  1229. */
  1230. int snd_hda_codec_configure(struct hda_codec *codec)
  1231. {
  1232. int err;
  1233. codec->preset = find_codec_preset(codec);
  1234. if (!codec->vendor_name || !codec->chip_name) {
  1235. err = get_codec_name(codec);
  1236. if (err < 0)
  1237. return err;
  1238. }
  1239. if (is_generic_config(codec)) {
  1240. err = snd_hda_parse_generic_codec(codec);
  1241. goto patched;
  1242. }
  1243. if (codec->preset && codec->preset->patch) {
  1244. err = codec->preset->patch(codec);
  1245. goto patched;
  1246. }
  1247. /* call the default parser */
  1248. err = snd_hda_parse_generic_codec(codec);
  1249. if (err < 0)
  1250. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  1251. patched:
  1252. if (!err && codec->patch_ops.unsol_event)
  1253. err = init_unsol_queue(codec->bus);
  1254. /* audio codec should override the mixer name */
  1255. if (!err && (codec->afg || !*codec->bus->card->mixername))
  1256. snprintf(codec->bus->card->mixername,
  1257. sizeof(codec->bus->card->mixername),
  1258. "%s %s", codec->vendor_name, codec->chip_name);
  1259. return err;
  1260. }
  1261. EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
  1262. /**
  1263. * snd_hda_codec_setup_stream - set up the codec for streaming
  1264. * @codec: the CODEC to set up
  1265. * @nid: the NID to set up
  1266. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  1267. * @channel_id: channel id to pass, zero based.
  1268. * @format: stream format.
  1269. */
  1270. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  1271. u32 stream_tag,
  1272. int channel_id, int format)
  1273. {
  1274. struct hda_codec *c;
  1275. struct hda_cvt_setup *p;
  1276. unsigned int oldval, newval;
  1277. int type;
  1278. int i;
  1279. if (!nid)
  1280. return;
  1281. snd_printdd("hda_codec_setup_stream: "
  1282. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1283. nid, stream_tag, channel_id, format);
  1284. p = get_hda_cvt_setup(codec, nid);
  1285. if (!p)
  1286. return;
  1287. /* update the stream-id if changed */
  1288. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  1289. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  1290. newval = (stream_tag << 4) | channel_id;
  1291. if (oldval != newval)
  1292. snd_hda_codec_write(codec, nid, 0,
  1293. AC_VERB_SET_CHANNEL_STREAMID,
  1294. newval);
  1295. p->stream_tag = stream_tag;
  1296. p->channel_id = channel_id;
  1297. }
  1298. /* update the format-id if changed */
  1299. if (p->format_id != format) {
  1300. oldval = snd_hda_codec_read(codec, nid, 0,
  1301. AC_VERB_GET_STREAM_FORMAT, 0);
  1302. if (oldval != format) {
  1303. msleep(1);
  1304. snd_hda_codec_write(codec, nid, 0,
  1305. AC_VERB_SET_STREAM_FORMAT,
  1306. format);
  1307. }
  1308. p->format_id = format;
  1309. }
  1310. p->active = 1;
  1311. p->dirty = 0;
  1312. /* make other inactive cvts with the same stream-tag dirty */
  1313. type = get_wcaps_type(get_wcaps(codec, nid));
  1314. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1315. for (i = 0; i < c->cvt_setups.used; i++) {
  1316. p = snd_array_elem(&c->cvt_setups, i);
  1317. if (!p->active && p->stream_tag == stream_tag &&
  1318. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1319. p->dirty = 1;
  1320. }
  1321. }
  1322. }
  1323. EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
  1324. static void really_cleanup_stream(struct hda_codec *codec,
  1325. struct hda_cvt_setup *q);
  1326. /**
  1327. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1328. * @codec: the CODEC to clean up
  1329. * @nid: the NID to clean up
  1330. * @do_now: really clean up the stream instead of clearing the active flag
  1331. */
  1332. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1333. int do_now)
  1334. {
  1335. struct hda_cvt_setup *p;
  1336. if (!nid)
  1337. return;
  1338. if (codec->no_sticky_stream)
  1339. do_now = 1;
  1340. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1341. p = get_hda_cvt_setup(codec, nid);
  1342. if (p) {
  1343. /* here we just clear the active flag when do_now isn't set;
  1344. * actual clean-ups will be done later in
  1345. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1346. */
  1347. if (do_now)
  1348. really_cleanup_stream(codec, p);
  1349. else
  1350. p->active = 0;
  1351. }
  1352. }
  1353. EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
  1354. static void really_cleanup_stream(struct hda_codec *codec,
  1355. struct hda_cvt_setup *q)
  1356. {
  1357. hda_nid_t nid = q->nid;
  1358. if (q->stream_tag || q->channel_id)
  1359. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1360. if (q->format_id)
  1361. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1362. );
  1363. memset(q, 0, sizeof(*q));
  1364. q->nid = nid;
  1365. }
  1366. /* clean up the all conflicting obsolete streams */
  1367. static void purify_inactive_streams(struct hda_codec *codec)
  1368. {
  1369. struct hda_codec *c;
  1370. int i;
  1371. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1372. for (i = 0; i < c->cvt_setups.used; i++) {
  1373. struct hda_cvt_setup *p;
  1374. p = snd_array_elem(&c->cvt_setups, i);
  1375. if (p->dirty)
  1376. really_cleanup_stream(c, p);
  1377. }
  1378. }
  1379. }
  1380. #ifdef CONFIG_PM
  1381. /* clean up all streams; called from suspend */
  1382. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1383. {
  1384. int i;
  1385. for (i = 0; i < codec->cvt_setups.used; i++) {
  1386. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1387. if (p->stream_tag)
  1388. really_cleanup_stream(codec, p);
  1389. }
  1390. }
  1391. #endif
  1392. /*
  1393. * amp access functions
  1394. */
  1395. /* FIXME: more better hash key? */
  1396. #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  1397. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  1398. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  1399. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  1400. #define INFO_AMP_CAPS (1<<0)
  1401. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1402. /* initialize the hash table */
  1403. static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
  1404. unsigned int record_size)
  1405. {
  1406. memset(cache, 0, sizeof(*cache));
  1407. memset(cache->hash, 0xff, sizeof(cache->hash));
  1408. snd_array_init(&cache->buf, record_size, 64);
  1409. }
  1410. static void free_hda_cache(struct hda_cache_rec *cache)
  1411. {
  1412. snd_array_free(&cache->buf);
  1413. }
  1414. /* query the hash. allocate an entry if not found. */
  1415. static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
  1416. {
  1417. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1418. u16 cur = cache->hash[idx];
  1419. struct hda_cache_head *info;
  1420. while (cur != 0xffff) {
  1421. info = snd_array_elem(&cache->buf, cur);
  1422. if (info->key == key)
  1423. return info;
  1424. cur = info->next;
  1425. }
  1426. return NULL;
  1427. }
  1428. /* query the hash. allocate an entry if not found. */
  1429. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1430. u32 key)
  1431. {
  1432. struct hda_cache_head *info = get_hash(cache, key);
  1433. if (!info) {
  1434. u16 idx, cur;
  1435. /* add a new hash entry */
  1436. info = snd_array_new(&cache->buf);
  1437. if (!info)
  1438. return NULL;
  1439. cur = snd_array_index(&cache->buf, info);
  1440. info->key = key;
  1441. info->val = 0;
  1442. idx = key % (u16)ARRAY_SIZE(cache->hash);
  1443. info->next = cache->hash[idx];
  1444. cache->hash[idx] = cur;
  1445. }
  1446. return info;
  1447. }
  1448. /* query and allocate an amp hash entry */
  1449. static inline struct hda_amp_info *
  1450. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1451. {
  1452. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1453. }
  1454. /**
  1455. * query_amp_caps - query AMP capabilities
  1456. * @codec: the HD-auio codec
  1457. * @nid: the NID to query
  1458. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1459. *
  1460. * Query AMP capabilities for the given widget and direction.
  1461. * Returns the obtained capability bits.
  1462. *
  1463. * When cap bits have been already read, this doesn't read again but
  1464. * returns the cached value.
  1465. */
  1466. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1467. {
  1468. struct hda_amp_info *info;
  1469. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  1470. if (!info)
  1471. return 0;
  1472. if (!(info->head.val & INFO_AMP_CAPS)) {
  1473. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1474. nid = codec->afg;
  1475. info->amp_caps = snd_hda_param_read(codec, nid,
  1476. direction == HDA_OUTPUT ?
  1477. AC_PAR_AMP_OUT_CAP :
  1478. AC_PAR_AMP_IN_CAP);
  1479. if (info->amp_caps)
  1480. info->head.val |= INFO_AMP_CAPS;
  1481. }
  1482. return info->amp_caps;
  1483. }
  1484. EXPORT_SYMBOL_HDA(query_amp_caps);
  1485. /**
  1486. * snd_hda_override_amp_caps - Override the AMP capabilities
  1487. * @codec: the CODEC to clean up
  1488. * @nid: the NID to clean up
  1489. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1490. * @caps: the capability bits to set
  1491. *
  1492. * Override the cached AMP caps bits value by the given one.
  1493. * This function is useful if the driver needs to adjust the AMP ranges,
  1494. * e.g. limit to 0dB, etc.
  1495. *
  1496. * Returns zero if successful or a negative error code.
  1497. */
  1498. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1499. unsigned int caps)
  1500. {
  1501. struct hda_amp_info *info;
  1502. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  1503. if (!info)
  1504. return -EINVAL;
  1505. info->amp_caps = caps;
  1506. info->head.val |= INFO_AMP_CAPS;
  1507. return 0;
  1508. }
  1509. EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
  1510. static unsigned int
  1511. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
  1512. unsigned int (*func)(struct hda_codec *, hda_nid_t))
  1513. {
  1514. struct hda_amp_info *info;
  1515. info = get_alloc_amp_hash(codec, key);
  1516. if (!info)
  1517. return 0;
  1518. if (!info->head.val) {
  1519. info->head.val |= INFO_AMP_CAPS;
  1520. info->amp_caps = func(codec, nid);
  1521. }
  1522. return info->amp_caps;
  1523. }
  1524. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
  1525. {
  1526. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1527. }
  1528. /**
  1529. * snd_hda_query_pin_caps - Query PIN capabilities
  1530. * @codec: the HD-auio codec
  1531. * @nid: the NID to query
  1532. *
  1533. * Query PIN capabilities for the given widget.
  1534. * Returns the obtained capability bits.
  1535. *
  1536. * When cap bits have been already read, this doesn't read again but
  1537. * returns the cached value.
  1538. */
  1539. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1540. {
  1541. return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
  1542. read_pin_cap);
  1543. }
  1544. EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
  1545. /**
  1546. * snd_hda_override_pin_caps - Override the pin capabilities
  1547. * @codec: the CODEC
  1548. * @nid: the NID to override
  1549. * @caps: the capability bits to set
  1550. *
  1551. * Override the cached PIN capabilitiy bits value by the given one.
  1552. *
  1553. * Returns zero if successful or a negative error code.
  1554. */
  1555. int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
  1556. unsigned int caps)
  1557. {
  1558. struct hda_amp_info *info;
  1559. info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
  1560. if (!info)
  1561. return -ENOMEM;
  1562. info->amp_caps = caps;
  1563. info->head.val |= INFO_AMP_CAPS;
  1564. return 0;
  1565. }
  1566. EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
  1567. /*
  1568. * read the current volume to info
  1569. * if the cache exists, read the cache value.
  1570. */
  1571. static unsigned int get_vol_mute(struct hda_codec *codec,
  1572. struct hda_amp_info *info, hda_nid_t nid,
  1573. int ch, int direction, int index)
  1574. {
  1575. u32 val, parm;
  1576. if (info->head.val & INFO_AMP_VOL(ch))
  1577. return info->vol[ch];
  1578. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1579. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1580. parm |= index;
  1581. val = snd_hda_codec_read(codec, nid, 0,
  1582. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1583. info->vol[ch] = val & 0xff;
  1584. info->head.val |= INFO_AMP_VOL(ch);
  1585. return info->vol[ch];
  1586. }
  1587. /*
  1588. * write the current volume in info to the h/w and update the cache
  1589. */
  1590. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  1591. hda_nid_t nid, int ch, int direction, int index,
  1592. int val)
  1593. {
  1594. u32 parm;
  1595. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1596. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1597. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1598. parm |= val;
  1599. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1600. info->vol[ch] = val;
  1601. }
  1602. /**
  1603. * snd_hda_codec_amp_read - Read AMP value
  1604. * @codec: HD-audio codec
  1605. * @nid: NID to read the AMP value
  1606. * @ch: channel (left=0 or right=1)
  1607. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1608. * @index: the index value (only for input direction)
  1609. *
  1610. * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1611. */
  1612. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1613. int direction, int index)
  1614. {
  1615. struct hda_amp_info *info;
  1616. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1617. if (!info)
  1618. return 0;
  1619. return get_vol_mute(codec, info, nid, ch, direction, index);
  1620. }
  1621. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
  1622. /**
  1623. * snd_hda_codec_amp_update - update the AMP value
  1624. * @codec: HD-audio codec
  1625. * @nid: NID to read the AMP value
  1626. * @ch: channel (left=0 or right=1)
  1627. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1628. * @idx: the index value (only for input direction)
  1629. * @mask: bit mask to set
  1630. * @val: the bits value to set
  1631. *
  1632. * Update the AMP value with a bit mask.
  1633. * Returns 0 if the value is unchanged, 1 if changed.
  1634. */
  1635. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1636. int direction, int idx, int mask, int val)
  1637. {
  1638. struct hda_amp_info *info;
  1639. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  1640. if (!info)
  1641. return 0;
  1642. if (snd_BUG_ON(mask & ~0xff))
  1643. mask &= 0xff;
  1644. val &= mask;
  1645. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  1646. if (info->vol[ch] == val)
  1647. return 0;
  1648. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  1649. return 1;
  1650. }
  1651. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
  1652. /**
  1653. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1654. * @codec: HD-audio codec
  1655. * @nid: NID to read the AMP value
  1656. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1657. * @idx: the index value (only for input direction)
  1658. * @mask: bit mask to set
  1659. * @val: the bits value to set
  1660. *
  1661. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1662. * stereo widget with the same mask and value.
  1663. */
  1664. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1665. int direction, int idx, int mask, int val)
  1666. {
  1667. int ch, ret = 0;
  1668. if (snd_BUG_ON(mask & ~0xff))
  1669. mask &= 0xff;
  1670. for (ch = 0; ch < 2; ch++)
  1671. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1672. idx, mask, val);
  1673. return ret;
  1674. }
  1675. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
  1676. #ifdef CONFIG_PM
  1677. /**
  1678. * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
  1679. * @codec: HD-audio codec
  1680. *
  1681. * Resume the all amp commands from the cache.
  1682. */
  1683. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  1684. {
  1685. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  1686. int i;
  1687. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  1688. u32 key = buffer->head.key;
  1689. hda_nid_t nid;
  1690. unsigned int idx, dir, ch;
  1691. if (!key)
  1692. continue;
  1693. nid = key & 0xff;
  1694. idx = (key >> 16) & 0xff;
  1695. dir = (key >> 24) & 0xff;
  1696. for (ch = 0; ch < 2; ch++) {
  1697. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  1698. continue;
  1699. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  1700. buffer->vol[ch]);
  1701. }
  1702. }
  1703. }
  1704. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
  1705. #endif /* CONFIG_PM */
  1706. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  1707. unsigned int ofs)
  1708. {
  1709. u32 caps = query_amp_caps(codec, nid, dir);
  1710. /* get num steps */
  1711. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1712. if (ofs < caps)
  1713. caps -= ofs;
  1714. return caps;
  1715. }
  1716. /**
  1717. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  1718. *
  1719. * The control element is supposed to have the private_value field
  1720. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1721. */
  1722. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1723. struct snd_ctl_elem_info *uinfo)
  1724. {
  1725. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1726. u16 nid = get_amp_nid(kcontrol);
  1727. u8 chs = get_amp_channels(kcontrol);
  1728. int dir = get_amp_direction(kcontrol);
  1729. unsigned int ofs = get_amp_offset(kcontrol);
  1730. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1731. uinfo->count = chs == 3 ? 2 : 1;
  1732. uinfo->value.integer.min = 0;
  1733. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  1734. if (!uinfo->value.integer.max) {
  1735. printk(KERN_WARNING "hda_codec: "
  1736. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  1737. kcontrol->id.name);
  1738. return -EINVAL;
  1739. }
  1740. return 0;
  1741. }
  1742. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
  1743. static inline unsigned int
  1744. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1745. int ch, int dir, int idx, unsigned int ofs)
  1746. {
  1747. unsigned int val;
  1748. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1749. val &= HDA_AMP_VOLMASK;
  1750. if (val >= ofs)
  1751. val -= ofs;
  1752. else
  1753. val = 0;
  1754. return val;
  1755. }
  1756. static inline int
  1757. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1758. int ch, int dir, int idx, unsigned int ofs,
  1759. unsigned int val)
  1760. {
  1761. unsigned int maxval;
  1762. if (val > 0)
  1763. val += ofs;
  1764. /* ofs = 0: raw max value */
  1765. maxval = get_amp_max_value(codec, nid, dir, 0);
  1766. if (val > maxval)
  1767. val = maxval;
  1768. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1769. HDA_AMP_VOLMASK, val);
  1770. }
  1771. /**
  1772. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  1773. *
  1774. * The control element is supposed to have the private_value field
  1775. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1776. */
  1777. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1778. struct snd_ctl_elem_value *ucontrol)
  1779. {
  1780. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1781. hda_nid_t nid = get_amp_nid(kcontrol);
  1782. int chs = get_amp_channels(kcontrol);
  1783. int dir = get_amp_direction(kcontrol);
  1784. int idx = get_amp_index(kcontrol);
  1785. unsigned int ofs = get_amp_offset(kcontrol);
  1786. long *valp = ucontrol->value.integer.value;
  1787. if (chs & 1)
  1788. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  1789. if (chs & 2)
  1790. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  1791. return 0;
  1792. }
  1793. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
  1794. /**
  1795. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  1796. *
  1797. * The control element is supposed to have the private_value field
  1798. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1799. */
  1800. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1801. struct snd_ctl_elem_value *ucontrol)
  1802. {
  1803. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1804. hda_nid_t nid = get_amp_nid(kcontrol);
  1805. int chs = get_amp_channels(kcontrol);
  1806. int dir = get_amp_direction(kcontrol);
  1807. int idx = get_amp_index(kcontrol);
  1808. unsigned int ofs = get_amp_offset(kcontrol);
  1809. long *valp = ucontrol->value.integer.value;
  1810. int change = 0;
  1811. snd_hda_power_up(codec);
  1812. if (chs & 1) {
  1813. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  1814. valp++;
  1815. }
  1816. if (chs & 2)
  1817. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  1818. snd_hda_power_down(codec);
  1819. return change;
  1820. }
  1821. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
  1822. /**
  1823. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  1824. *
  1825. * The control element is supposed to have the private_value field
  1826. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1827. */
  1828. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1829. unsigned int size, unsigned int __user *_tlv)
  1830. {
  1831. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1832. hda_nid_t nid = get_amp_nid(kcontrol);
  1833. int dir = get_amp_direction(kcontrol);
  1834. unsigned int ofs = get_amp_offset(kcontrol);
  1835. bool min_mute = get_amp_min_mute(kcontrol);
  1836. u32 caps, val1, val2;
  1837. if (size < 4 * sizeof(unsigned int))
  1838. return -ENOMEM;
  1839. caps = query_amp_caps(codec, nid, dir);
  1840. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1841. val2 = (val2 + 1) * 25;
  1842. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1843. val1 += ofs;
  1844. val1 = ((int)val1) * ((int)val2);
  1845. if (min_mute)
  1846. val2 |= TLV_DB_SCALE_MUTE;
  1847. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1848. return -EFAULT;
  1849. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1850. return -EFAULT;
  1851. if (put_user(val1, _tlv + 2))
  1852. return -EFAULT;
  1853. if (put_user(val2, _tlv + 3))
  1854. return -EFAULT;
  1855. return 0;
  1856. }
  1857. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
  1858. /**
  1859. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  1860. * @codec: HD-audio codec
  1861. * @nid: NID of a reference widget
  1862. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1863. * @tlv: TLV data to be stored, at least 4 elements
  1864. *
  1865. * Set (static) TLV data for a virtual master volume using the AMP caps
  1866. * obtained from the reference NID.
  1867. * The volume range is recalculated as if the max volume is 0dB.
  1868. */
  1869. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1870. unsigned int *tlv)
  1871. {
  1872. u32 caps;
  1873. int nums, step;
  1874. caps = query_amp_caps(codec, nid, dir);
  1875. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1876. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1877. step = (step + 1) * 25;
  1878. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1879. tlv[1] = 2 * sizeof(unsigned int);
  1880. tlv[2] = -nums * step;
  1881. tlv[3] = step;
  1882. }
  1883. EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
  1884. /* find a mixer control element with the given name */
  1885. static struct snd_kcontrol *
  1886. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1887. const char *name, int idx)
  1888. {
  1889. struct snd_ctl_elem_id id;
  1890. memset(&id, 0, sizeof(id));
  1891. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1892. id.index = idx;
  1893. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  1894. return NULL;
  1895. strcpy(id.name, name);
  1896. return snd_ctl_find_id(codec->bus->card, &id);
  1897. }
  1898. /**
  1899. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  1900. * @codec: HD-audio codec
  1901. * @name: ctl id name string
  1902. *
  1903. * Get the control element with the given id string and IFACE_MIXER.
  1904. */
  1905. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1906. const char *name)
  1907. {
  1908. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1909. }
  1910. EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
  1911. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
  1912. {
  1913. int idx;
  1914. for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
  1915. if (!_snd_hda_find_mixer_ctl(codec, name, idx))
  1916. return idx;
  1917. }
  1918. return -EBUSY;
  1919. }
  1920. /**
  1921. * snd_hda_ctl_add - Add a control element and assign to the codec
  1922. * @codec: HD-audio codec
  1923. * @nid: corresponding NID (optional)
  1924. * @kctl: the control element to assign
  1925. *
  1926. * Add the given control element to an array inside the codec instance.
  1927. * All control elements belonging to a codec are supposed to be added
  1928. * by this function so that a proper clean-up works at the free or
  1929. * reconfiguration time.
  1930. *
  1931. * If non-zero @nid is passed, the NID is assigned to the control element.
  1932. * The assignment is shown in the codec proc file.
  1933. *
  1934. * snd_hda_ctl_add() checks the control subdev id field whether
  1935. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  1936. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  1937. * specifies if kctl->private_value is a HDA amplifier value.
  1938. */
  1939. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  1940. struct snd_kcontrol *kctl)
  1941. {
  1942. int err;
  1943. unsigned short flags = 0;
  1944. struct hda_nid_item *item;
  1945. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  1946. flags |= HDA_NID_ITEM_AMP;
  1947. if (nid == 0)
  1948. nid = get_amp_nid_(kctl->private_value);
  1949. }
  1950. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  1951. nid = kctl->id.subdevice & 0xffff;
  1952. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  1953. kctl->id.subdevice = 0;
  1954. err = snd_ctl_add(codec->bus->card, kctl);
  1955. if (err < 0)
  1956. return err;
  1957. item = snd_array_new(&codec->mixers);
  1958. if (!item)
  1959. return -ENOMEM;
  1960. item->kctl = kctl;
  1961. item->nid = nid;
  1962. item->flags = flags;
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
  1966. /**
  1967. * snd_hda_add_nid - Assign a NID to a control element
  1968. * @codec: HD-audio codec
  1969. * @nid: corresponding NID (optional)
  1970. * @kctl: the control element to assign
  1971. * @index: index to kctl
  1972. *
  1973. * Add the given control element to an array inside the codec instance.
  1974. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  1975. * NID:KCTL mapping - for example "Capture Source" selector.
  1976. */
  1977. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  1978. unsigned int index, hda_nid_t nid)
  1979. {
  1980. struct hda_nid_item *item;
  1981. if (nid > 0) {
  1982. item = snd_array_new(&codec->nids);
  1983. if (!item)
  1984. return -ENOMEM;
  1985. item->kctl = kctl;
  1986. item->index = index;
  1987. item->nid = nid;
  1988. return 0;
  1989. }
  1990. printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
  1991. kctl->id.name, kctl->id.index, index);
  1992. return -EINVAL;
  1993. }
  1994. EXPORT_SYMBOL_HDA(snd_hda_add_nid);
  1995. /**
  1996. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  1997. * @codec: HD-audio codec
  1998. */
  1999. void snd_hda_ctls_clear(struct hda_codec *codec)
  2000. {
  2001. int i;
  2002. struct hda_nid_item *items = codec->mixers.list;
  2003. for (i = 0; i < codec->mixers.used; i++)
  2004. snd_ctl_remove(codec->bus->card, items[i].kctl);
  2005. snd_array_free(&codec->mixers);
  2006. snd_array_free(&codec->nids);
  2007. }
  2008. /* pseudo device locking
  2009. * toggle card->shutdown to allow/disallow the device access (as a hack)
  2010. */
  2011. static int hda_lock_devices(struct snd_card *card)
  2012. {
  2013. spin_lock(&card->files_lock);
  2014. if (card->shutdown) {
  2015. spin_unlock(&card->files_lock);
  2016. return -EINVAL;
  2017. }
  2018. card->shutdown = 1;
  2019. spin_unlock(&card->files_lock);
  2020. return 0;
  2021. }
  2022. static void hda_unlock_devices(struct snd_card *card)
  2023. {
  2024. spin_lock(&card->files_lock);
  2025. card->shutdown = 0;
  2026. spin_unlock(&card->files_lock);
  2027. }
  2028. /**
  2029. * snd_hda_codec_reset - Clear all objects assigned to the codec
  2030. * @codec: HD-audio codec
  2031. *
  2032. * This frees the all PCM and control elements assigned to the codec, and
  2033. * clears the caches and restores the pin default configurations.
  2034. *
  2035. * When a device is being used, it returns -EBSY. If successfully freed,
  2036. * returns zero.
  2037. */
  2038. int snd_hda_codec_reset(struct hda_codec *codec)
  2039. {
  2040. struct snd_card *card = codec->bus->card;
  2041. int i, pcm;
  2042. if (hda_lock_devices(card) < 0)
  2043. return -EBUSY;
  2044. /* check whether the codec isn't used by any mixer or PCM streams */
  2045. if (!list_empty(&card->ctl_files)) {
  2046. hda_unlock_devices(card);
  2047. return -EBUSY;
  2048. }
  2049. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2050. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2051. if (!cpcm->pcm)
  2052. continue;
  2053. if (cpcm->pcm->streams[0].substream_opened ||
  2054. cpcm->pcm->streams[1].substream_opened) {
  2055. hda_unlock_devices(card);
  2056. return -EBUSY;
  2057. }
  2058. }
  2059. /* OK, let it free */
  2060. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2061. cancel_delayed_work(&codec->power_work);
  2062. flush_workqueue(codec->bus->workq);
  2063. #endif
  2064. snd_hda_ctls_clear(codec);
  2065. /* relase PCMs */
  2066. for (i = 0; i < codec->num_pcms; i++) {
  2067. if (codec->pcm_info[i].pcm) {
  2068. snd_device_free(card, codec->pcm_info[i].pcm);
  2069. clear_bit(codec->pcm_info[i].device,
  2070. codec->bus->pcm_dev_bits);
  2071. }
  2072. }
  2073. if (codec->patch_ops.free)
  2074. codec->patch_ops.free(codec);
  2075. snd_hda_jack_tbl_clear(codec);
  2076. codec->proc_widget_hook = NULL;
  2077. codec->spec = NULL;
  2078. free_hda_cache(&codec->amp_cache);
  2079. free_hda_cache(&codec->cmd_cache);
  2080. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  2081. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  2082. /* free only driver_pins so that init_pins + user_pins are restored */
  2083. snd_array_free(&codec->driver_pins);
  2084. restore_pincfgs(codec);
  2085. codec->num_pcms = 0;
  2086. codec->pcm_info = NULL;
  2087. codec->preset = NULL;
  2088. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  2089. codec->slave_dig_outs = NULL;
  2090. codec->spdif_status_reset = 0;
  2091. module_put(codec->owner);
  2092. codec->owner = NULL;
  2093. /* allow device access again */
  2094. hda_unlock_devices(card);
  2095. return 0;
  2096. }
  2097. typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
  2098. /* apply the function to all matching slave ctls in the mixer list */
  2099. static int map_slaves(struct hda_codec *codec, const char * const *slaves,
  2100. map_slave_func_t func, void *data)
  2101. {
  2102. struct hda_nid_item *items;
  2103. const char * const *s;
  2104. int i, err;
  2105. items = codec->mixers.list;
  2106. for (i = 0; i < codec->mixers.used; i++) {
  2107. struct snd_kcontrol *sctl = items[i].kctl;
  2108. if (!sctl || !sctl->id.name ||
  2109. sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  2110. continue;
  2111. for (s = slaves; *s; s++) {
  2112. if (!strcmp(sctl->id.name, *s)) {
  2113. err = func(data, sctl);
  2114. if (err)
  2115. return err;
  2116. break;
  2117. }
  2118. }
  2119. }
  2120. return 0;
  2121. }
  2122. static int check_slave_present(void *data, struct snd_kcontrol *sctl)
  2123. {
  2124. return 1;
  2125. }
  2126. /**
  2127. * snd_hda_add_vmaster - create a virtual master control and add slaves
  2128. * @codec: HD-audio codec
  2129. * @name: vmaster control name
  2130. * @tlv: TLV data (optional)
  2131. * @slaves: slave control names (optional)
  2132. *
  2133. * Create a virtual master control with the given name. The TLV data
  2134. * must be either NULL or a valid data.
  2135. *
  2136. * @slaves is a NULL-terminated array of strings, each of which is a
  2137. * slave control name. All controls with these names are assigned to
  2138. * the new virtual master control.
  2139. *
  2140. * This function returns zero if successful or a negative error code.
  2141. */
  2142. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  2143. unsigned int *tlv, const char * const *slaves)
  2144. {
  2145. struct snd_kcontrol *kctl;
  2146. int err;
  2147. err = map_slaves(codec, slaves, check_slave_present, NULL);
  2148. if (err != 1) {
  2149. snd_printdd("No slave found for %s\n", name);
  2150. return 0;
  2151. }
  2152. kctl = snd_ctl_make_virtual_master(name, tlv);
  2153. if (!kctl)
  2154. return -ENOMEM;
  2155. err = snd_hda_ctl_add(codec, 0, kctl);
  2156. if (err < 0)
  2157. return err;
  2158. err = map_slaves(codec, slaves, (map_slave_func_t)snd_ctl_add_slave,
  2159. kctl);
  2160. if (err < 0)
  2161. return err;
  2162. return 0;
  2163. }
  2164. EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
  2165. /**
  2166. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  2167. *
  2168. * The control element is supposed to have the private_value field
  2169. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2170. */
  2171. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  2172. struct snd_ctl_elem_info *uinfo)
  2173. {
  2174. int chs = get_amp_channels(kcontrol);
  2175. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2176. uinfo->count = chs == 3 ? 2 : 1;
  2177. uinfo->value.integer.min = 0;
  2178. uinfo->value.integer.max = 1;
  2179. return 0;
  2180. }
  2181. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
  2182. /**
  2183. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  2184. *
  2185. * The control element is supposed to have the private_value field
  2186. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2187. */
  2188. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  2189. struct snd_ctl_elem_value *ucontrol)
  2190. {
  2191. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2192. hda_nid_t nid = get_amp_nid(kcontrol);
  2193. int chs = get_amp_channels(kcontrol);
  2194. int dir = get_amp_direction(kcontrol);
  2195. int idx = get_amp_index(kcontrol);
  2196. long *valp = ucontrol->value.integer.value;
  2197. if (chs & 1)
  2198. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  2199. HDA_AMP_MUTE) ? 0 : 1;
  2200. if (chs & 2)
  2201. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  2202. HDA_AMP_MUTE) ? 0 : 1;
  2203. return 0;
  2204. }
  2205. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
  2206. /**
  2207. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  2208. *
  2209. * The control element is supposed to have the private_value field
  2210. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2211. */
  2212. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  2213. struct snd_ctl_elem_value *ucontrol)
  2214. {
  2215. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2216. hda_nid_t nid = get_amp_nid(kcontrol);
  2217. int chs = get_amp_channels(kcontrol);
  2218. int dir = get_amp_direction(kcontrol);
  2219. int idx = get_amp_index(kcontrol);
  2220. long *valp = ucontrol->value.integer.value;
  2221. int change = 0;
  2222. snd_hda_power_up(codec);
  2223. if (chs & 1) {
  2224. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  2225. HDA_AMP_MUTE,
  2226. *valp ? 0 : HDA_AMP_MUTE);
  2227. valp++;
  2228. }
  2229. if (chs & 2)
  2230. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  2231. HDA_AMP_MUTE,
  2232. *valp ? 0 : HDA_AMP_MUTE);
  2233. hda_call_check_power_status(codec, nid);
  2234. snd_hda_power_down(codec);
  2235. return change;
  2236. }
  2237. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
  2238. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  2239. /**
  2240. * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
  2241. *
  2242. * This function calls snd_hda_enable_beep_device(), which behaves differently
  2243. * depending on beep_mode option.
  2244. */
  2245. int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
  2246. struct snd_ctl_elem_value *ucontrol)
  2247. {
  2248. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2249. long *valp = ucontrol->value.integer.value;
  2250. snd_hda_enable_beep_device(codec, *valp);
  2251. return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2252. }
  2253. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
  2254. #endif /* CONFIG_SND_HDA_INPUT_BEEP */
  2255. /*
  2256. * bound volume controls
  2257. *
  2258. * bind multiple volumes (# indices, from 0)
  2259. */
  2260. #define AMP_VAL_IDX_SHIFT 19
  2261. #define AMP_VAL_IDX_MASK (0x0f<<19)
  2262. /**
  2263. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  2264. *
  2265. * The control element is supposed to have the private_value field
  2266. * set up via HDA_BIND_MUTE*() macros.
  2267. */
  2268. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  2269. struct snd_ctl_elem_value *ucontrol)
  2270. {
  2271. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2272. unsigned long pval;
  2273. int err;
  2274. mutex_lock(&codec->control_mutex);
  2275. pval = kcontrol->private_value;
  2276. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  2277. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  2278. kcontrol->private_value = pval;
  2279. mutex_unlock(&codec->control_mutex);
  2280. return err;
  2281. }
  2282. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
  2283. /**
  2284. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  2285. *
  2286. * The control element is supposed to have the private_value field
  2287. * set up via HDA_BIND_MUTE*() macros.
  2288. */
  2289. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  2290. struct snd_ctl_elem_value *ucontrol)
  2291. {
  2292. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2293. unsigned long pval;
  2294. int i, indices, err = 0, change = 0;
  2295. mutex_lock(&codec->control_mutex);
  2296. pval = kcontrol->private_value;
  2297. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2298. for (i = 0; i < indices; i++) {
  2299. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2300. (i << AMP_VAL_IDX_SHIFT);
  2301. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2302. if (err < 0)
  2303. break;
  2304. change |= err;
  2305. }
  2306. kcontrol->private_value = pval;
  2307. mutex_unlock(&codec->control_mutex);
  2308. return err < 0 ? err : change;
  2309. }
  2310. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
  2311. /**
  2312. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2313. *
  2314. * The control element is supposed to have the private_value field
  2315. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2316. */
  2317. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2318. struct snd_ctl_elem_info *uinfo)
  2319. {
  2320. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2321. struct hda_bind_ctls *c;
  2322. int err;
  2323. mutex_lock(&codec->control_mutex);
  2324. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2325. kcontrol->private_value = *c->values;
  2326. err = c->ops->info(kcontrol, uinfo);
  2327. kcontrol->private_value = (long)c;
  2328. mutex_unlock(&codec->control_mutex);
  2329. return err;
  2330. }
  2331. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
  2332. /**
  2333. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2334. *
  2335. * The control element is supposed to have the private_value field
  2336. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2337. */
  2338. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2339. struct snd_ctl_elem_value *ucontrol)
  2340. {
  2341. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2342. struct hda_bind_ctls *c;
  2343. int err;
  2344. mutex_lock(&codec->control_mutex);
  2345. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2346. kcontrol->private_value = *c->values;
  2347. err = c->ops->get(kcontrol, ucontrol);
  2348. kcontrol->private_value = (long)c;
  2349. mutex_unlock(&codec->control_mutex);
  2350. return err;
  2351. }
  2352. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
  2353. /**
  2354. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2355. *
  2356. * The control element is supposed to have the private_value field
  2357. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2358. */
  2359. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2360. struct snd_ctl_elem_value *ucontrol)
  2361. {
  2362. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2363. struct hda_bind_ctls *c;
  2364. unsigned long *vals;
  2365. int err = 0, change = 0;
  2366. mutex_lock(&codec->control_mutex);
  2367. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2368. for (vals = c->values; *vals; vals++) {
  2369. kcontrol->private_value = *vals;
  2370. err = c->ops->put(kcontrol, ucontrol);
  2371. if (err < 0)
  2372. break;
  2373. change |= err;
  2374. }
  2375. kcontrol->private_value = (long)c;
  2376. mutex_unlock(&codec->control_mutex);
  2377. return err < 0 ? err : change;
  2378. }
  2379. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
  2380. /**
  2381. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  2382. *
  2383. * The control element is supposed to have the private_value field
  2384. * set up via HDA_BIND_VOL() macro.
  2385. */
  2386. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2387. unsigned int size, unsigned int __user *tlv)
  2388. {
  2389. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2390. struct hda_bind_ctls *c;
  2391. int err;
  2392. mutex_lock(&codec->control_mutex);
  2393. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2394. kcontrol->private_value = *c->values;
  2395. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  2396. kcontrol->private_value = (long)c;
  2397. mutex_unlock(&codec->control_mutex);
  2398. return err;
  2399. }
  2400. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
  2401. struct hda_ctl_ops snd_hda_bind_vol = {
  2402. .info = snd_hda_mixer_amp_volume_info,
  2403. .get = snd_hda_mixer_amp_volume_get,
  2404. .put = snd_hda_mixer_amp_volume_put,
  2405. .tlv = snd_hda_mixer_amp_tlv
  2406. };
  2407. EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
  2408. struct hda_ctl_ops snd_hda_bind_sw = {
  2409. .info = snd_hda_mixer_amp_switch_info,
  2410. .get = snd_hda_mixer_amp_switch_get,
  2411. .put = snd_hda_mixer_amp_switch_put,
  2412. .tlv = snd_hda_mixer_amp_tlv
  2413. };
  2414. EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
  2415. /*
  2416. * SPDIF out controls
  2417. */
  2418. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  2419. struct snd_ctl_elem_info *uinfo)
  2420. {
  2421. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2422. uinfo->count = 1;
  2423. return 0;
  2424. }
  2425. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  2426. struct snd_ctl_elem_value *ucontrol)
  2427. {
  2428. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2429. IEC958_AES0_NONAUDIO |
  2430. IEC958_AES0_CON_EMPHASIS_5015 |
  2431. IEC958_AES0_CON_NOT_COPYRIGHT;
  2432. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  2433. IEC958_AES1_CON_ORIGINAL;
  2434. return 0;
  2435. }
  2436. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  2437. struct snd_ctl_elem_value *ucontrol)
  2438. {
  2439. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2440. IEC958_AES0_NONAUDIO |
  2441. IEC958_AES0_PRO_EMPHASIS_5015;
  2442. return 0;
  2443. }
  2444. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  2445. struct snd_ctl_elem_value *ucontrol)
  2446. {
  2447. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2448. int idx = kcontrol->private_value;
  2449. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2450. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  2451. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  2452. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  2453. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  2454. return 0;
  2455. }
  2456. /* convert from SPDIF status bits to HDA SPDIF bits
  2457. * bit 0 (DigEn) is always set zero (to be filled later)
  2458. */
  2459. static unsigned short convert_from_spdif_status(unsigned int sbits)
  2460. {
  2461. unsigned short val = 0;
  2462. if (sbits & IEC958_AES0_PROFESSIONAL)
  2463. val |= AC_DIG1_PROFESSIONAL;
  2464. if (sbits & IEC958_AES0_NONAUDIO)
  2465. val |= AC_DIG1_NONAUDIO;
  2466. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2467. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  2468. IEC958_AES0_PRO_EMPHASIS_5015)
  2469. val |= AC_DIG1_EMPHASIS;
  2470. } else {
  2471. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  2472. IEC958_AES0_CON_EMPHASIS_5015)
  2473. val |= AC_DIG1_EMPHASIS;
  2474. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  2475. val |= AC_DIG1_COPYRIGHT;
  2476. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  2477. val |= AC_DIG1_LEVEL;
  2478. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  2479. }
  2480. return val;
  2481. }
  2482. /* convert to SPDIF status bits from HDA SPDIF bits
  2483. */
  2484. static unsigned int convert_to_spdif_status(unsigned short val)
  2485. {
  2486. unsigned int sbits = 0;
  2487. if (val & AC_DIG1_NONAUDIO)
  2488. sbits |= IEC958_AES0_NONAUDIO;
  2489. if (val & AC_DIG1_PROFESSIONAL)
  2490. sbits |= IEC958_AES0_PROFESSIONAL;
  2491. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2492. if (sbits & AC_DIG1_EMPHASIS)
  2493. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  2494. } else {
  2495. if (val & AC_DIG1_EMPHASIS)
  2496. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  2497. if (!(val & AC_DIG1_COPYRIGHT))
  2498. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  2499. if (val & AC_DIG1_LEVEL)
  2500. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  2501. sbits |= val & (0x7f << 8);
  2502. }
  2503. return sbits;
  2504. }
  2505. /* set digital convert verbs both for the given NID and its slaves */
  2506. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  2507. int verb, int val)
  2508. {
  2509. const hda_nid_t *d;
  2510. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  2511. d = codec->slave_dig_outs;
  2512. if (!d)
  2513. return;
  2514. for (; *d; d++)
  2515. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  2516. }
  2517. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  2518. int dig1, int dig2)
  2519. {
  2520. if (dig1 != -1)
  2521. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  2522. if (dig2 != -1)
  2523. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  2524. }
  2525. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  2526. struct snd_ctl_elem_value *ucontrol)
  2527. {
  2528. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2529. int idx = kcontrol->private_value;
  2530. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2531. hda_nid_t nid = spdif->nid;
  2532. unsigned short val;
  2533. int change;
  2534. mutex_lock(&codec->spdif_mutex);
  2535. spdif->status = ucontrol->value.iec958.status[0] |
  2536. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  2537. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  2538. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  2539. val = convert_from_spdif_status(spdif->status);
  2540. val |= spdif->ctls & 1;
  2541. change = spdif->ctls != val;
  2542. spdif->ctls = val;
  2543. if (change && nid != (u16)-1)
  2544. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2545. mutex_unlock(&codec->spdif_mutex);
  2546. return change;
  2547. }
  2548. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  2549. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  2550. struct snd_ctl_elem_value *ucontrol)
  2551. {
  2552. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2553. int idx = kcontrol->private_value;
  2554. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2555. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  2556. return 0;
  2557. }
  2558. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  2559. int dig1, int dig2)
  2560. {
  2561. set_dig_out_convert(codec, nid, dig1, dig2);
  2562. /* unmute amp switch (if any) */
  2563. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  2564. (dig1 & AC_DIG1_ENABLE))
  2565. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  2566. HDA_AMP_MUTE, 0);
  2567. }
  2568. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  2569. struct snd_ctl_elem_value *ucontrol)
  2570. {
  2571. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2572. int idx = kcontrol->private_value;
  2573. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2574. hda_nid_t nid = spdif->nid;
  2575. unsigned short val;
  2576. int change;
  2577. mutex_lock(&codec->spdif_mutex);
  2578. val = spdif->ctls & ~AC_DIG1_ENABLE;
  2579. if (ucontrol->value.integer.value[0])
  2580. val |= AC_DIG1_ENABLE;
  2581. change = spdif->ctls != val;
  2582. spdif->ctls = val;
  2583. if (change && nid != (u16)-1)
  2584. set_spdif_ctls(codec, nid, val & 0xff, -1);
  2585. mutex_unlock(&codec->spdif_mutex);
  2586. return change;
  2587. }
  2588. static struct snd_kcontrol_new dig_mixes[] = {
  2589. {
  2590. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2591. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2592. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  2593. .info = snd_hda_spdif_mask_info,
  2594. .get = snd_hda_spdif_cmask_get,
  2595. },
  2596. {
  2597. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2598. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2599. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  2600. .info = snd_hda_spdif_mask_info,
  2601. .get = snd_hda_spdif_pmask_get,
  2602. },
  2603. {
  2604. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2605. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  2606. .info = snd_hda_spdif_mask_info,
  2607. .get = snd_hda_spdif_default_get,
  2608. .put = snd_hda_spdif_default_put,
  2609. },
  2610. {
  2611. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2612. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  2613. .info = snd_hda_spdif_out_switch_info,
  2614. .get = snd_hda_spdif_out_switch_get,
  2615. .put = snd_hda_spdif_out_switch_put,
  2616. },
  2617. { } /* end */
  2618. };
  2619. /**
  2620. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  2621. * @codec: the HDA codec
  2622. * @nid: audio out widget NID
  2623. *
  2624. * Creates controls related with the SPDIF output.
  2625. * Called from each patch supporting the SPDIF out.
  2626. *
  2627. * Returns 0 if successful, or a negative error code.
  2628. */
  2629. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
  2630. hda_nid_t associated_nid,
  2631. hda_nid_t cvt_nid)
  2632. {
  2633. int err;
  2634. struct snd_kcontrol *kctl;
  2635. struct snd_kcontrol_new *dig_mix;
  2636. int idx;
  2637. struct hda_spdif_out *spdif;
  2638. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
  2639. if (idx < 0) {
  2640. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  2641. return -EBUSY;
  2642. }
  2643. spdif = snd_array_new(&codec->spdif_out);
  2644. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2645. kctl = snd_ctl_new1(dig_mix, codec);
  2646. if (!kctl)
  2647. return -ENOMEM;
  2648. kctl->id.index = idx;
  2649. kctl->private_value = codec->spdif_out.used - 1;
  2650. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  2651. if (err < 0)
  2652. return err;
  2653. }
  2654. spdif->nid = cvt_nid;
  2655. spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
  2656. AC_VERB_GET_DIGI_CONVERT_1, 0);
  2657. spdif->status = convert_to_spdif_status(spdif->ctls);
  2658. return 0;
  2659. }
  2660. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
  2661. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  2662. hda_nid_t nid)
  2663. {
  2664. int i;
  2665. for (i = 0; i < codec->spdif_out.used; i++) {
  2666. struct hda_spdif_out *spdif =
  2667. snd_array_elem(&codec->spdif_out, i);
  2668. if (spdif->nid == nid)
  2669. return spdif;
  2670. }
  2671. return NULL;
  2672. }
  2673. EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
  2674. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  2675. {
  2676. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2677. mutex_lock(&codec->spdif_mutex);
  2678. spdif->nid = (u16)-1;
  2679. mutex_unlock(&codec->spdif_mutex);
  2680. }
  2681. EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
  2682. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  2683. {
  2684. struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
  2685. unsigned short val;
  2686. mutex_lock(&codec->spdif_mutex);
  2687. if (spdif->nid != nid) {
  2688. spdif->nid = nid;
  2689. val = spdif->ctls;
  2690. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2691. }
  2692. mutex_unlock(&codec->spdif_mutex);
  2693. }
  2694. EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
  2695. /*
  2696. * SPDIF sharing with analog output
  2697. */
  2698. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  2699. struct snd_ctl_elem_value *ucontrol)
  2700. {
  2701. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2702. ucontrol->value.integer.value[0] = mout->share_spdif;
  2703. return 0;
  2704. }
  2705. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  2706. struct snd_ctl_elem_value *ucontrol)
  2707. {
  2708. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2709. mout->share_spdif = !!ucontrol->value.integer.value[0];
  2710. return 0;
  2711. }
  2712. static struct snd_kcontrol_new spdif_share_sw = {
  2713. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2714. .name = "IEC958 Default PCM Playback Switch",
  2715. .info = snd_ctl_boolean_mono_info,
  2716. .get = spdif_share_sw_get,
  2717. .put = spdif_share_sw_put,
  2718. };
  2719. /**
  2720. * snd_hda_create_spdif_share_sw - create Default PCM switch
  2721. * @codec: the HDA codec
  2722. * @mout: multi-out instance
  2723. */
  2724. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  2725. struct hda_multi_out *mout)
  2726. {
  2727. if (!mout->dig_out_nid)
  2728. return 0;
  2729. /* ATTENTION: here mout is passed as private_data, instead of codec */
  2730. return snd_hda_ctl_add(codec, mout->dig_out_nid,
  2731. snd_ctl_new1(&spdif_share_sw, mout));
  2732. }
  2733. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
  2734. /*
  2735. * SPDIF input
  2736. */
  2737. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  2738. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  2739. struct snd_ctl_elem_value *ucontrol)
  2740. {
  2741. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2742. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  2743. return 0;
  2744. }
  2745. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  2746. struct snd_ctl_elem_value *ucontrol)
  2747. {
  2748. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2749. hda_nid_t nid = kcontrol->private_value;
  2750. unsigned int val = !!ucontrol->value.integer.value[0];
  2751. int change;
  2752. mutex_lock(&codec->spdif_mutex);
  2753. change = codec->spdif_in_enable != val;
  2754. if (change) {
  2755. codec->spdif_in_enable = val;
  2756. snd_hda_codec_write_cache(codec, nid, 0,
  2757. AC_VERB_SET_DIGI_CONVERT_1, val);
  2758. }
  2759. mutex_unlock(&codec->spdif_mutex);
  2760. return change;
  2761. }
  2762. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  2763. struct snd_ctl_elem_value *ucontrol)
  2764. {
  2765. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2766. hda_nid_t nid = kcontrol->private_value;
  2767. unsigned short val;
  2768. unsigned int sbits;
  2769. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  2770. sbits = convert_to_spdif_status(val);
  2771. ucontrol->value.iec958.status[0] = sbits;
  2772. ucontrol->value.iec958.status[1] = sbits >> 8;
  2773. ucontrol->value.iec958.status[2] = sbits >> 16;
  2774. ucontrol->value.iec958.status[3] = sbits >> 24;
  2775. return 0;
  2776. }
  2777. static struct snd_kcontrol_new dig_in_ctls[] = {
  2778. {
  2779. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2780. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  2781. .info = snd_hda_spdif_in_switch_info,
  2782. .get = snd_hda_spdif_in_switch_get,
  2783. .put = snd_hda_spdif_in_switch_put,
  2784. },
  2785. {
  2786. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2787. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2788. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  2789. .info = snd_hda_spdif_mask_info,
  2790. .get = snd_hda_spdif_in_status_get,
  2791. },
  2792. { } /* end */
  2793. };
  2794. /**
  2795. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  2796. * @codec: the HDA codec
  2797. * @nid: audio in widget NID
  2798. *
  2799. * Creates controls related with the SPDIF input.
  2800. * Called from each patch supporting the SPDIF in.
  2801. *
  2802. * Returns 0 if successful, or a negative error code.
  2803. */
  2804. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  2805. {
  2806. int err;
  2807. struct snd_kcontrol *kctl;
  2808. struct snd_kcontrol_new *dig_mix;
  2809. int idx;
  2810. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
  2811. if (idx < 0) {
  2812. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  2813. return -EBUSY;
  2814. }
  2815. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  2816. kctl = snd_ctl_new1(dig_mix, codec);
  2817. if (!kctl)
  2818. return -ENOMEM;
  2819. kctl->private_value = nid;
  2820. err = snd_hda_ctl_add(codec, nid, kctl);
  2821. if (err < 0)
  2822. return err;
  2823. }
  2824. codec->spdif_in_enable =
  2825. snd_hda_codec_read(codec, nid, 0,
  2826. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  2827. AC_DIG1_ENABLE;
  2828. return 0;
  2829. }
  2830. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
  2831. #ifdef CONFIG_PM
  2832. /*
  2833. * command cache
  2834. */
  2835. /* build a 32bit cache key with the widget id and the command parameter */
  2836. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  2837. #define get_cmd_cache_nid(key) ((key) & 0xff)
  2838. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  2839. /**
  2840. * snd_hda_codec_write_cache - send a single command with caching
  2841. * @codec: the HDA codec
  2842. * @nid: NID to send the command
  2843. * @direct: direct flag
  2844. * @verb: the verb to send
  2845. * @parm: the parameter for the verb
  2846. *
  2847. * Send a single command without waiting for response.
  2848. *
  2849. * Returns 0 if successful, or a negative error code.
  2850. */
  2851. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  2852. int direct, unsigned int verb, unsigned int parm)
  2853. {
  2854. int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
  2855. struct hda_cache_head *c;
  2856. u32 key;
  2857. if (err < 0)
  2858. return err;
  2859. /* parm may contain the verb stuff for get/set amp */
  2860. verb = verb | (parm >> 8);
  2861. parm &= 0xff;
  2862. key = build_cmd_cache_key(nid, verb);
  2863. mutex_lock(&codec->bus->cmd_mutex);
  2864. c = get_alloc_hash(&codec->cmd_cache, key);
  2865. if (c)
  2866. c->val = parm;
  2867. mutex_unlock(&codec->bus->cmd_mutex);
  2868. return 0;
  2869. }
  2870. EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
  2871. /**
  2872. * snd_hda_codec_update_cache - check cache and write the cmd only when needed
  2873. * @codec: the HDA codec
  2874. * @nid: NID to send the command
  2875. * @direct: direct flag
  2876. * @verb: the verb to send
  2877. * @parm: the parameter for the verb
  2878. *
  2879. * This function works like snd_hda_codec_write_cache(), but it doesn't send
  2880. * command if the parameter is already identical with the cached value.
  2881. * If not, it sends the command and refreshes the cache.
  2882. *
  2883. * Returns 0 if successful, or a negative error code.
  2884. */
  2885. int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
  2886. int direct, unsigned int verb, unsigned int parm)
  2887. {
  2888. struct hda_cache_head *c;
  2889. u32 key;
  2890. /* parm may contain the verb stuff for get/set amp */
  2891. verb = verb | (parm >> 8);
  2892. parm &= 0xff;
  2893. key = build_cmd_cache_key(nid, verb);
  2894. mutex_lock(&codec->bus->cmd_mutex);
  2895. c = get_hash(&codec->cmd_cache, key);
  2896. if (c && c->val == parm) {
  2897. mutex_unlock(&codec->bus->cmd_mutex);
  2898. return 0;
  2899. }
  2900. mutex_unlock(&codec->bus->cmd_mutex);
  2901. return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
  2902. }
  2903. EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
  2904. /**
  2905. * snd_hda_codec_resume_cache - Resume the all commands from the cache
  2906. * @codec: HD-audio codec
  2907. *
  2908. * Execute all verbs recorded in the command caches to resume.
  2909. */
  2910. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  2911. {
  2912. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  2913. int i;
  2914. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  2915. u32 key = buffer->key;
  2916. if (!key)
  2917. continue;
  2918. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  2919. get_cmd_cache_cmd(key), buffer->val);
  2920. }
  2921. }
  2922. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
  2923. /**
  2924. * snd_hda_sequence_write_cache - sequence writes with caching
  2925. * @codec: the HDA codec
  2926. * @seq: VERB array to send
  2927. *
  2928. * Send the commands sequentially from the given array.
  2929. * Thte commands are recorded on cache for power-save and resume.
  2930. * The array must be terminated with NID=0.
  2931. */
  2932. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  2933. const struct hda_verb *seq)
  2934. {
  2935. for (; seq->nid; seq++)
  2936. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  2937. seq->param);
  2938. }
  2939. EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
  2940. #endif /* CONFIG_PM */
  2941. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  2942. unsigned int power_state,
  2943. bool eapd_workaround)
  2944. {
  2945. hda_nid_t nid = codec->start_nid;
  2946. int i;
  2947. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2948. unsigned int wcaps = get_wcaps(codec, nid);
  2949. if (!(wcaps & AC_WCAP_POWER))
  2950. continue;
  2951. /* don't power down the widget if it controls eapd and
  2952. * EAPD_BTLENABLE is set.
  2953. */
  2954. if (eapd_workaround && power_state == AC_PWRST_D3 &&
  2955. get_wcaps_type(wcaps) == AC_WID_PIN &&
  2956. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  2957. int eapd = snd_hda_codec_read(codec, nid, 0,
  2958. AC_VERB_GET_EAPD_BTLENABLE, 0);
  2959. if (eapd & 0x02)
  2960. continue;
  2961. }
  2962. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  2963. power_state);
  2964. }
  2965. if (power_state == AC_PWRST_D0) {
  2966. unsigned long end_time;
  2967. int state;
  2968. /* wait until the codec reachs to D0 */
  2969. end_time = jiffies + msecs_to_jiffies(500);
  2970. do {
  2971. state = snd_hda_codec_read(codec, fg, 0,
  2972. AC_VERB_GET_POWER_STATE, 0);
  2973. if (state == power_state)
  2974. break;
  2975. msleep(1);
  2976. } while (time_after_eq(end_time, jiffies));
  2977. }
  2978. }
  2979. EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
  2980. /*
  2981. * set power state of the codec
  2982. */
  2983. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2984. unsigned int power_state)
  2985. {
  2986. if (codec->patch_ops.set_power_state) {
  2987. codec->patch_ops.set_power_state(codec, fg, power_state);
  2988. return;
  2989. }
  2990. /* this delay seems necessary to avoid click noise at power-down */
  2991. if (power_state == AC_PWRST_D3)
  2992. msleep(100);
  2993. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  2994. power_state);
  2995. snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
  2996. }
  2997. #ifdef CONFIG_SND_HDA_HWDEP
  2998. /* execute additional init verbs */
  2999. static void hda_exec_init_verbs(struct hda_codec *codec)
  3000. {
  3001. if (codec->init_verbs.list)
  3002. snd_hda_sequence_write(codec, codec->init_verbs.list);
  3003. }
  3004. #else
  3005. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  3006. #endif
  3007. #ifdef CONFIG_PM
  3008. /*
  3009. * call suspend and power-down; used both from PM and power-save
  3010. */
  3011. static void hda_call_codec_suspend(struct hda_codec *codec)
  3012. {
  3013. if (codec->patch_ops.suspend)
  3014. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  3015. hda_cleanup_all_streams(codec);
  3016. hda_set_power_state(codec,
  3017. codec->afg ? codec->afg : codec->mfg,
  3018. AC_PWRST_D3);
  3019. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3020. snd_hda_update_power_acct(codec);
  3021. cancel_delayed_work(&codec->power_work);
  3022. codec->power_on = 0;
  3023. codec->power_transition = 0;
  3024. codec->power_jiffies = jiffies;
  3025. #endif
  3026. }
  3027. /*
  3028. * kick up codec; used both from PM and power-save
  3029. */
  3030. static void hda_call_codec_resume(struct hda_codec *codec)
  3031. {
  3032. hda_set_power_state(codec,
  3033. codec->afg ? codec->afg : codec->mfg,
  3034. AC_PWRST_D0);
  3035. restore_pincfgs(codec); /* restore all current pin configs */
  3036. restore_shutup_pins(codec);
  3037. hda_exec_init_verbs(codec);
  3038. snd_hda_jack_set_dirty_all(codec);
  3039. if (codec->patch_ops.resume)
  3040. codec->patch_ops.resume(codec);
  3041. else {
  3042. if (codec->patch_ops.init)
  3043. codec->patch_ops.init(codec);
  3044. snd_hda_codec_resume_amp(codec);
  3045. snd_hda_codec_resume_cache(codec);
  3046. }
  3047. }
  3048. #endif /* CONFIG_PM */
  3049. /**
  3050. * snd_hda_build_controls - build mixer controls
  3051. * @bus: the BUS
  3052. *
  3053. * Creates mixer controls for each codec included in the bus.
  3054. *
  3055. * Returns 0 if successful, otherwise a negative error code.
  3056. */
  3057. int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
  3058. {
  3059. struct hda_codec *codec;
  3060. list_for_each_entry(codec, &bus->codec_list, list) {
  3061. int err = snd_hda_codec_build_controls(codec);
  3062. if (err < 0) {
  3063. printk(KERN_ERR "hda_codec: cannot build controls "
  3064. "for #%d (error %d)\n", codec->addr, err);
  3065. err = snd_hda_codec_reset(codec);
  3066. if (err < 0) {
  3067. printk(KERN_ERR
  3068. "hda_codec: cannot revert codec\n");
  3069. return err;
  3070. }
  3071. }
  3072. }
  3073. return 0;
  3074. }
  3075. EXPORT_SYMBOL_HDA(snd_hda_build_controls);
  3076. int snd_hda_codec_build_controls(struct hda_codec *codec)
  3077. {
  3078. int err = 0;
  3079. hda_exec_init_verbs(codec);
  3080. /* continue to initialize... */
  3081. if (codec->patch_ops.init)
  3082. err = codec->patch_ops.init(codec);
  3083. if (!err && codec->patch_ops.build_controls)
  3084. err = codec->patch_ops.build_controls(codec);
  3085. if (err < 0)
  3086. return err;
  3087. return 0;
  3088. }
  3089. /*
  3090. * stream formats
  3091. */
  3092. struct hda_rate_tbl {
  3093. unsigned int hz;
  3094. unsigned int alsa_bits;
  3095. unsigned int hda_fmt;
  3096. };
  3097. /* rate = base * mult / div */
  3098. #define HDA_RATE(base, mult, div) \
  3099. (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
  3100. (((div) - 1) << AC_FMT_DIV_SHIFT))
  3101. static struct hda_rate_tbl rate_bits[] = {
  3102. /* rate in Hz, ALSA rate bitmask, HDA format value */
  3103. /* autodetected value used in snd_hda_query_supported_pcm */
  3104. { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
  3105. { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
  3106. { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
  3107. { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
  3108. { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
  3109. { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
  3110. { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
  3111. { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
  3112. { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
  3113. { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
  3114. { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
  3115. #define AC_PAR_PCM_RATE_BITS 11
  3116. /* up to bits 10, 384kHZ isn't supported properly */
  3117. /* not autodetected value */
  3118. { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
  3119. { 0 } /* terminator */
  3120. };
  3121. /**
  3122. * snd_hda_calc_stream_format - calculate format bitset
  3123. * @rate: the sample rate
  3124. * @channels: the number of channels
  3125. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  3126. * @maxbps: the max. bps
  3127. *
  3128. * Calculate the format bitset from the given rate, channels and th PCM format.
  3129. *
  3130. * Return zero if invalid.
  3131. */
  3132. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  3133. unsigned int channels,
  3134. unsigned int format,
  3135. unsigned int maxbps,
  3136. unsigned short spdif_ctls)
  3137. {
  3138. int i;
  3139. unsigned int val = 0;
  3140. for (i = 0; rate_bits[i].hz; i++)
  3141. if (rate_bits[i].hz == rate) {
  3142. val = rate_bits[i].hda_fmt;
  3143. break;
  3144. }
  3145. if (!rate_bits[i].hz) {
  3146. snd_printdd("invalid rate %d\n", rate);
  3147. return 0;
  3148. }
  3149. if (channels == 0 || channels > 8) {
  3150. snd_printdd("invalid channels %d\n", channels);
  3151. return 0;
  3152. }
  3153. val |= channels - 1;
  3154. switch (snd_pcm_format_width(format)) {
  3155. case 8:
  3156. val |= AC_FMT_BITS_8;
  3157. break;
  3158. case 16:
  3159. val |= AC_FMT_BITS_16;
  3160. break;
  3161. case 20:
  3162. case 24:
  3163. case 32:
  3164. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  3165. val |= AC_FMT_BITS_32;
  3166. else if (maxbps >= 24)
  3167. val |= AC_FMT_BITS_24;
  3168. else
  3169. val |= AC_FMT_BITS_20;
  3170. break;
  3171. default:
  3172. snd_printdd("invalid format width %d\n",
  3173. snd_pcm_format_width(format));
  3174. return 0;
  3175. }
  3176. if (spdif_ctls & AC_DIG1_NONAUDIO)
  3177. val |= AC_FMT_TYPE_NON_PCM;
  3178. return val;
  3179. }
  3180. EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
  3181. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  3182. {
  3183. unsigned int val = 0;
  3184. if (nid != codec->afg &&
  3185. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  3186. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  3187. if (!val || val == -1)
  3188. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  3189. if (!val || val == -1)
  3190. return 0;
  3191. return val;
  3192. }
  3193. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  3194. {
  3195. return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
  3196. get_pcm_param);
  3197. }
  3198. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
  3199. {
  3200. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  3201. if (!streams || streams == -1)
  3202. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  3203. if (!streams || streams == -1)
  3204. return 0;
  3205. return streams;
  3206. }
  3207. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  3208. {
  3209. return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
  3210. get_stream_param);
  3211. }
  3212. /**
  3213. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  3214. * @codec: the HDA codec
  3215. * @nid: NID to query
  3216. * @ratesp: the pointer to store the detected rate bitflags
  3217. * @formatsp: the pointer to store the detected formats
  3218. * @bpsp: the pointer to store the detected format widths
  3219. *
  3220. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  3221. * or @bsps argument is ignored.
  3222. *
  3223. * Returns 0 if successful, otherwise a negative error code.
  3224. */
  3225. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  3226. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  3227. {
  3228. unsigned int i, val, wcaps;
  3229. wcaps = get_wcaps(codec, nid);
  3230. val = query_pcm_param(codec, nid);
  3231. if (ratesp) {
  3232. u32 rates = 0;
  3233. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  3234. if (val & (1 << i))
  3235. rates |= rate_bits[i].alsa_bits;
  3236. }
  3237. if (rates == 0) {
  3238. snd_printk(KERN_ERR "hda_codec: rates == 0 "
  3239. "(nid=0x%x, val=0x%x, ovrd=%i)\n",
  3240. nid, val,
  3241. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  3242. return -EIO;
  3243. }
  3244. *ratesp = rates;
  3245. }
  3246. if (formatsp || bpsp) {
  3247. u64 formats = 0;
  3248. unsigned int streams, bps;
  3249. streams = query_stream_param(codec, nid);
  3250. if (!streams)
  3251. return -EIO;
  3252. bps = 0;
  3253. if (streams & AC_SUPFMT_PCM) {
  3254. if (val & AC_SUPPCM_BITS_8) {
  3255. formats |= SNDRV_PCM_FMTBIT_U8;
  3256. bps = 8;
  3257. }
  3258. if (val & AC_SUPPCM_BITS_16) {
  3259. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  3260. bps = 16;
  3261. }
  3262. if (wcaps & AC_WCAP_DIGITAL) {
  3263. if (val & AC_SUPPCM_BITS_32)
  3264. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  3265. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  3266. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3267. if (val & AC_SUPPCM_BITS_24)
  3268. bps = 24;
  3269. else if (val & AC_SUPPCM_BITS_20)
  3270. bps = 20;
  3271. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  3272. AC_SUPPCM_BITS_32)) {
  3273. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3274. if (val & AC_SUPPCM_BITS_32)
  3275. bps = 32;
  3276. else if (val & AC_SUPPCM_BITS_24)
  3277. bps = 24;
  3278. else if (val & AC_SUPPCM_BITS_20)
  3279. bps = 20;
  3280. }
  3281. }
  3282. if (streams & AC_SUPFMT_FLOAT32) {
  3283. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  3284. if (!bps)
  3285. bps = 32;
  3286. }
  3287. if (streams == AC_SUPFMT_AC3) {
  3288. /* should be exclusive */
  3289. /* temporary hack: we have still no proper support
  3290. * for the direct AC3 stream...
  3291. */
  3292. formats |= SNDRV_PCM_FMTBIT_U8;
  3293. bps = 8;
  3294. }
  3295. if (formats == 0) {
  3296. snd_printk(KERN_ERR "hda_codec: formats == 0 "
  3297. "(nid=0x%x, val=0x%x, ovrd=%i, "
  3298. "streams=0x%x)\n",
  3299. nid, val,
  3300. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  3301. streams);
  3302. return -EIO;
  3303. }
  3304. if (formatsp)
  3305. *formatsp = formats;
  3306. if (bpsp)
  3307. *bpsp = bps;
  3308. }
  3309. return 0;
  3310. }
  3311. EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
  3312. /**
  3313. * snd_hda_is_supported_format - Check the validity of the format
  3314. * @codec: HD-audio codec
  3315. * @nid: NID to check
  3316. * @format: the HD-audio format value to check
  3317. *
  3318. * Check whether the given node supports the format value.
  3319. *
  3320. * Returns 1 if supported, 0 if not.
  3321. */
  3322. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  3323. unsigned int format)
  3324. {
  3325. int i;
  3326. unsigned int val = 0, rate, stream;
  3327. val = query_pcm_param(codec, nid);
  3328. if (!val)
  3329. return 0;
  3330. rate = format & 0xff00;
  3331. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  3332. if (rate_bits[i].hda_fmt == rate) {
  3333. if (val & (1 << i))
  3334. break;
  3335. return 0;
  3336. }
  3337. if (i >= AC_PAR_PCM_RATE_BITS)
  3338. return 0;
  3339. stream = query_stream_param(codec, nid);
  3340. if (!stream)
  3341. return 0;
  3342. if (stream & AC_SUPFMT_PCM) {
  3343. switch (format & 0xf0) {
  3344. case 0x00:
  3345. if (!(val & AC_SUPPCM_BITS_8))
  3346. return 0;
  3347. break;
  3348. case 0x10:
  3349. if (!(val & AC_SUPPCM_BITS_16))
  3350. return 0;
  3351. break;
  3352. case 0x20:
  3353. if (!(val & AC_SUPPCM_BITS_20))
  3354. return 0;
  3355. break;
  3356. case 0x30:
  3357. if (!(val & AC_SUPPCM_BITS_24))
  3358. return 0;
  3359. break;
  3360. case 0x40:
  3361. if (!(val & AC_SUPPCM_BITS_32))
  3362. return 0;
  3363. break;
  3364. default:
  3365. return 0;
  3366. }
  3367. } else {
  3368. /* FIXME: check for float32 and AC3? */
  3369. }
  3370. return 1;
  3371. }
  3372. EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
  3373. /*
  3374. * PCM stuff
  3375. */
  3376. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  3377. struct hda_codec *codec,
  3378. struct snd_pcm_substream *substream)
  3379. {
  3380. return 0;
  3381. }
  3382. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  3383. struct hda_codec *codec,
  3384. unsigned int stream_tag,
  3385. unsigned int format,
  3386. struct snd_pcm_substream *substream)
  3387. {
  3388. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3389. return 0;
  3390. }
  3391. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  3392. struct hda_codec *codec,
  3393. struct snd_pcm_substream *substream)
  3394. {
  3395. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3396. return 0;
  3397. }
  3398. static int set_pcm_default_values(struct hda_codec *codec,
  3399. struct hda_pcm_stream *info)
  3400. {
  3401. int err;
  3402. /* query support PCM information from the given NID */
  3403. if (info->nid && (!info->rates || !info->formats)) {
  3404. err = snd_hda_query_supported_pcm(codec, info->nid,
  3405. info->rates ? NULL : &info->rates,
  3406. info->formats ? NULL : &info->formats,
  3407. info->maxbps ? NULL : &info->maxbps);
  3408. if (err < 0)
  3409. return err;
  3410. }
  3411. if (info->ops.open == NULL)
  3412. info->ops.open = hda_pcm_default_open_close;
  3413. if (info->ops.close == NULL)
  3414. info->ops.close = hda_pcm_default_open_close;
  3415. if (info->ops.prepare == NULL) {
  3416. if (snd_BUG_ON(!info->nid))
  3417. return -EINVAL;
  3418. info->ops.prepare = hda_pcm_default_prepare;
  3419. }
  3420. if (info->ops.cleanup == NULL) {
  3421. if (snd_BUG_ON(!info->nid))
  3422. return -EINVAL;
  3423. info->ops.cleanup = hda_pcm_default_cleanup;
  3424. }
  3425. return 0;
  3426. }
  3427. /*
  3428. * codec prepare/cleanup entries
  3429. */
  3430. int snd_hda_codec_prepare(struct hda_codec *codec,
  3431. struct hda_pcm_stream *hinfo,
  3432. unsigned int stream,
  3433. unsigned int format,
  3434. struct snd_pcm_substream *substream)
  3435. {
  3436. int ret;
  3437. mutex_lock(&codec->bus->prepare_mutex);
  3438. ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
  3439. if (ret >= 0)
  3440. purify_inactive_streams(codec);
  3441. mutex_unlock(&codec->bus->prepare_mutex);
  3442. return ret;
  3443. }
  3444. EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
  3445. void snd_hda_codec_cleanup(struct hda_codec *codec,
  3446. struct hda_pcm_stream *hinfo,
  3447. struct snd_pcm_substream *substream)
  3448. {
  3449. mutex_lock(&codec->bus->prepare_mutex);
  3450. hinfo->ops.cleanup(hinfo, codec, substream);
  3451. mutex_unlock(&codec->bus->prepare_mutex);
  3452. }
  3453. EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
  3454. /* global */
  3455. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  3456. "Audio", "SPDIF", "HDMI", "Modem"
  3457. };
  3458. /*
  3459. * get the empty PCM device number to assign
  3460. *
  3461. * note the max device number is limited by HDA_MAX_PCMS, currently 10
  3462. */
  3463. static int get_empty_pcm_device(struct hda_bus *bus, int type)
  3464. {
  3465. /* audio device indices; not linear to keep compatibility */
  3466. static int audio_idx[HDA_PCM_NTYPES][5] = {
  3467. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  3468. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  3469. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  3470. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  3471. };
  3472. int i;
  3473. if (type >= HDA_PCM_NTYPES) {
  3474. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  3475. return -EINVAL;
  3476. }
  3477. for (i = 0; audio_idx[type][i] >= 0 ; i++)
  3478. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  3479. return audio_idx[type][i];
  3480. /* non-fixed slots starting from 10 */
  3481. for (i = 10; i < 32; i++) {
  3482. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  3483. return i;
  3484. }
  3485. snd_printk(KERN_WARNING "Too many %s devices\n",
  3486. snd_hda_pcm_type_name[type]);
  3487. return -EAGAIN;
  3488. }
  3489. /*
  3490. * attach a new PCM stream
  3491. */
  3492. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  3493. {
  3494. struct hda_bus *bus = codec->bus;
  3495. struct hda_pcm_stream *info;
  3496. int stream, err;
  3497. if (snd_BUG_ON(!pcm->name))
  3498. return -EINVAL;
  3499. for (stream = 0; stream < 2; stream++) {
  3500. info = &pcm->stream[stream];
  3501. if (info->substreams) {
  3502. err = set_pcm_default_values(codec, info);
  3503. if (err < 0)
  3504. return err;
  3505. }
  3506. }
  3507. return bus->ops.attach_pcm(bus, codec, pcm);
  3508. }
  3509. /* assign all PCMs of the given codec */
  3510. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  3511. {
  3512. unsigned int pcm;
  3513. int err;
  3514. if (!codec->num_pcms) {
  3515. if (!codec->patch_ops.build_pcms)
  3516. return 0;
  3517. err = codec->patch_ops.build_pcms(codec);
  3518. if (err < 0) {
  3519. printk(KERN_ERR "hda_codec: cannot build PCMs"
  3520. "for #%d (error %d)\n", codec->addr, err);
  3521. err = snd_hda_codec_reset(codec);
  3522. if (err < 0) {
  3523. printk(KERN_ERR
  3524. "hda_codec: cannot revert codec\n");
  3525. return err;
  3526. }
  3527. }
  3528. }
  3529. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  3530. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  3531. int dev;
  3532. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  3533. continue; /* no substreams assigned */
  3534. if (!cpcm->pcm) {
  3535. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  3536. if (dev < 0)
  3537. continue; /* no fatal error */
  3538. cpcm->device = dev;
  3539. err = snd_hda_attach_pcm(codec, cpcm);
  3540. if (err < 0) {
  3541. printk(KERN_ERR "hda_codec: cannot attach "
  3542. "PCM stream %d for codec #%d\n",
  3543. dev, codec->addr);
  3544. continue; /* no fatal error */
  3545. }
  3546. }
  3547. }
  3548. return 0;
  3549. }
  3550. /**
  3551. * snd_hda_build_pcms - build PCM information
  3552. * @bus: the BUS
  3553. *
  3554. * Create PCM information for each codec included in the bus.
  3555. *
  3556. * The build_pcms codec patch is requested to set up codec->num_pcms and
  3557. * codec->pcm_info properly. The array is referred by the top-level driver
  3558. * to create its PCM instances.
  3559. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  3560. * callback.
  3561. *
  3562. * At least, substreams, channels_min and channels_max must be filled for
  3563. * each stream. substreams = 0 indicates that the stream doesn't exist.
  3564. * When rates and/or formats are zero, the supported values are queried
  3565. * from the given nid. The nid is used also by the default ops.prepare
  3566. * and ops.cleanup callbacks.
  3567. *
  3568. * The driver needs to call ops.open in its open callback. Similarly,
  3569. * ops.close is supposed to be called in the close callback.
  3570. * ops.prepare should be called in the prepare or hw_params callback
  3571. * with the proper parameters for set up.
  3572. * ops.cleanup should be called in hw_free for clean up of streams.
  3573. *
  3574. * This function returns 0 if successful, or a negative error code.
  3575. */
  3576. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  3577. {
  3578. struct hda_codec *codec;
  3579. list_for_each_entry(codec, &bus->codec_list, list) {
  3580. int err = snd_hda_codec_build_pcms(codec);
  3581. if (err < 0)
  3582. return err;
  3583. }
  3584. return 0;
  3585. }
  3586. EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
  3587. /**
  3588. * snd_hda_check_board_config - compare the current codec with the config table
  3589. * @codec: the HDA codec
  3590. * @num_configs: number of config enums
  3591. * @models: array of model name strings
  3592. * @tbl: configuration table, terminated by null entries
  3593. *
  3594. * Compares the modelname or PCI subsystem id of the current codec with the
  3595. * given configuration table. If a matching entry is found, returns its
  3596. * config value (supposed to be 0 or positive).
  3597. *
  3598. * If no entries are matching, the function returns a negative value.
  3599. */
  3600. int snd_hda_check_board_config(struct hda_codec *codec,
  3601. int num_configs, const char * const *models,
  3602. const struct snd_pci_quirk *tbl)
  3603. {
  3604. if (codec->modelname && models) {
  3605. int i;
  3606. for (i = 0; i < num_configs; i++) {
  3607. if (models[i] &&
  3608. !strcmp(codec->modelname, models[i])) {
  3609. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  3610. "selected\n", models[i]);
  3611. return i;
  3612. }
  3613. }
  3614. }
  3615. if (!codec->bus->pci || !tbl)
  3616. return -1;
  3617. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  3618. if (!tbl)
  3619. return -1;
  3620. if (tbl->value >= 0 && tbl->value < num_configs) {
  3621. #ifdef CONFIG_SND_DEBUG_VERBOSE
  3622. char tmp[10];
  3623. const char *model = NULL;
  3624. if (models)
  3625. model = models[tbl->value];
  3626. if (!model) {
  3627. sprintf(tmp, "#%d", tbl->value);
  3628. model = tmp;
  3629. }
  3630. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  3631. "for config %x:%x (%s)\n",
  3632. model, tbl->subvendor, tbl->subdevice,
  3633. (tbl->name ? tbl->name : "Unknown device"));
  3634. #endif
  3635. return tbl->value;
  3636. }
  3637. return -1;
  3638. }
  3639. EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
  3640. /**
  3641. * snd_hda_check_board_codec_sid_config - compare the current codec
  3642. subsystem ID with the
  3643. config table
  3644. This is important for Gateway notebooks with SB450 HDA Audio
  3645. where the vendor ID of the PCI device is:
  3646. ATI Technologies Inc SB450 HDA Audio [1002:437b]
  3647. and the vendor/subvendor are found only at the codec.
  3648. * @codec: the HDA codec
  3649. * @num_configs: number of config enums
  3650. * @models: array of model name strings
  3651. * @tbl: configuration table, terminated by null entries
  3652. *
  3653. * Compares the modelname or PCI subsystem id of the current codec with the
  3654. * given configuration table. If a matching entry is found, returns its
  3655. * config value (supposed to be 0 or positive).
  3656. *
  3657. * If no entries are matching, the function returns a negative value.
  3658. */
  3659. int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
  3660. int num_configs, const char * const *models,
  3661. const struct snd_pci_quirk *tbl)
  3662. {
  3663. const struct snd_pci_quirk *q;
  3664. /* Search for codec ID */
  3665. for (q = tbl; q->subvendor; q++) {
  3666. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  3667. unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
  3668. if ((codec->subsystem_id & mask) == id)
  3669. break;
  3670. }
  3671. if (!q->subvendor)
  3672. return -1;
  3673. tbl = q;
  3674. if (tbl->value >= 0 && tbl->value < num_configs) {
  3675. #ifdef CONFIG_SND_DEBUG_VERBOSE
  3676. char tmp[10];
  3677. const char *model = NULL;
  3678. if (models)
  3679. model = models[tbl->value];
  3680. if (!model) {
  3681. sprintf(tmp, "#%d", tbl->value);
  3682. model = tmp;
  3683. }
  3684. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  3685. "for config %x:%x (%s)\n",
  3686. model, tbl->subvendor, tbl->subdevice,
  3687. (tbl->name ? tbl->name : "Unknown device"));
  3688. #endif
  3689. return tbl->value;
  3690. }
  3691. return -1;
  3692. }
  3693. EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
  3694. /**
  3695. * snd_hda_add_new_ctls - create controls from the array
  3696. * @codec: the HDA codec
  3697. * @knew: the array of struct snd_kcontrol_new
  3698. *
  3699. * This helper function creates and add new controls in the given array.
  3700. * The array must be terminated with an empty entry as terminator.
  3701. *
  3702. * Returns 0 if successful, or a negative error code.
  3703. */
  3704. int snd_hda_add_new_ctls(struct hda_codec *codec,
  3705. const struct snd_kcontrol_new *knew)
  3706. {
  3707. int err;
  3708. for (; knew->name; knew++) {
  3709. struct snd_kcontrol *kctl;
  3710. int addr = 0, idx = 0;
  3711. if (knew->iface == -1) /* skip this codec private value */
  3712. continue;
  3713. for (;;) {
  3714. kctl = snd_ctl_new1(knew, codec);
  3715. if (!kctl)
  3716. return -ENOMEM;
  3717. if (addr > 0)
  3718. kctl->id.device = addr;
  3719. if (idx > 0)
  3720. kctl->id.index = idx;
  3721. err = snd_hda_ctl_add(codec, 0, kctl);
  3722. if (!err)
  3723. break;
  3724. /* try first with another device index corresponding to
  3725. * the codec addr; if it still fails (or it's the
  3726. * primary codec), then try another control index
  3727. */
  3728. if (!addr && codec->addr)
  3729. addr = codec->addr;
  3730. else if (!idx && !knew->index) {
  3731. idx = find_empty_mixer_ctl_idx(codec,
  3732. knew->name);
  3733. if (idx <= 0)
  3734. return err;
  3735. } else
  3736. return err;
  3737. }
  3738. }
  3739. return 0;
  3740. }
  3741. EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
  3742. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3743. static void hda_power_work(struct work_struct *work)
  3744. {
  3745. struct hda_codec *codec =
  3746. container_of(work, struct hda_codec, power_work.work);
  3747. struct hda_bus *bus = codec->bus;
  3748. if (!codec->power_on || codec->power_count) {
  3749. codec->power_transition = 0;
  3750. return;
  3751. }
  3752. trace_hda_power_down(codec);
  3753. hda_call_codec_suspend(codec);
  3754. if (bus->ops.pm_notify)
  3755. bus->ops.pm_notify(bus);
  3756. }
  3757. static void hda_keep_power_on(struct hda_codec *codec)
  3758. {
  3759. codec->power_count++;
  3760. codec->power_on = 1;
  3761. codec->power_jiffies = jiffies;
  3762. }
  3763. /* update the power on/off account with the current jiffies */
  3764. void snd_hda_update_power_acct(struct hda_codec *codec)
  3765. {
  3766. unsigned long delta = jiffies - codec->power_jiffies;
  3767. if (codec->power_on)
  3768. codec->power_on_acct += delta;
  3769. else
  3770. codec->power_off_acct += delta;
  3771. codec->power_jiffies += delta;
  3772. }
  3773. /**
  3774. * snd_hda_power_up - Power-up the codec
  3775. * @codec: HD-audio codec
  3776. *
  3777. * Increment the power-up counter and power up the hardware really when
  3778. * not turned on yet.
  3779. */
  3780. void snd_hda_power_up(struct hda_codec *codec)
  3781. {
  3782. struct hda_bus *bus = codec->bus;
  3783. codec->power_count++;
  3784. if (codec->power_on || codec->power_transition)
  3785. return;
  3786. trace_hda_power_up(codec);
  3787. snd_hda_update_power_acct(codec);
  3788. codec->power_on = 1;
  3789. codec->power_jiffies = jiffies;
  3790. if (bus->ops.pm_notify)
  3791. bus->ops.pm_notify(bus);
  3792. hda_call_codec_resume(codec);
  3793. cancel_delayed_work(&codec->power_work);
  3794. codec->power_transition = 0;
  3795. }
  3796. EXPORT_SYMBOL_HDA(snd_hda_power_up);
  3797. #define power_save(codec) \
  3798. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  3799. /**
  3800. * snd_hda_power_down - Power-down the codec
  3801. * @codec: HD-audio codec
  3802. *
  3803. * Decrement the power-up counter and schedules the power-off work if
  3804. * the counter rearches to zero.
  3805. */
  3806. void snd_hda_power_down(struct hda_codec *codec)
  3807. {
  3808. --codec->power_count;
  3809. if (!codec->power_on || codec->power_count || codec->power_transition)
  3810. return;
  3811. if (power_save(codec)) {
  3812. codec->power_transition = 1; /* avoid reentrance */
  3813. queue_delayed_work(codec->bus->workq, &codec->power_work,
  3814. msecs_to_jiffies(power_save(codec) * 1000));
  3815. }
  3816. }
  3817. EXPORT_SYMBOL_HDA(snd_hda_power_down);
  3818. /**
  3819. * snd_hda_check_amp_list_power - Check the amp list and update the power
  3820. * @codec: HD-audio codec
  3821. * @check: the object containing an AMP list and the status
  3822. * @nid: NID to check / update
  3823. *
  3824. * Check whether the given NID is in the amp list. If it's in the list,
  3825. * check the current AMP status, and update the the power-status according
  3826. * to the mute status.
  3827. *
  3828. * This function is supposed to be set or called from the check_power_status
  3829. * patch ops.
  3830. */
  3831. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  3832. struct hda_loopback_check *check,
  3833. hda_nid_t nid)
  3834. {
  3835. const struct hda_amp_list *p;
  3836. int ch, v;
  3837. if (!check->amplist)
  3838. return 0;
  3839. for (p = check->amplist; p->nid; p++) {
  3840. if (p->nid == nid)
  3841. break;
  3842. }
  3843. if (!p->nid)
  3844. return 0; /* nothing changed */
  3845. for (p = check->amplist; p->nid; p++) {
  3846. for (ch = 0; ch < 2; ch++) {
  3847. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  3848. p->idx);
  3849. if (!(v & HDA_AMP_MUTE) && v > 0) {
  3850. if (!check->power_on) {
  3851. check->power_on = 1;
  3852. snd_hda_power_up(codec);
  3853. }
  3854. return 1;
  3855. }
  3856. }
  3857. }
  3858. if (check->power_on) {
  3859. check->power_on = 0;
  3860. snd_hda_power_down(codec);
  3861. }
  3862. return 0;
  3863. }
  3864. EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
  3865. #endif
  3866. /*
  3867. * Channel mode helper
  3868. */
  3869. /**
  3870. * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
  3871. */
  3872. int snd_hda_ch_mode_info(struct hda_codec *codec,
  3873. struct snd_ctl_elem_info *uinfo,
  3874. const struct hda_channel_mode *chmode,
  3875. int num_chmodes)
  3876. {
  3877. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3878. uinfo->count = 1;
  3879. uinfo->value.enumerated.items = num_chmodes;
  3880. if (uinfo->value.enumerated.item >= num_chmodes)
  3881. uinfo->value.enumerated.item = num_chmodes - 1;
  3882. sprintf(uinfo->value.enumerated.name, "%dch",
  3883. chmode[uinfo->value.enumerated.item].channels);
  3884. return 0;
  3885. }
  3886. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
  3887. /**
  3888. * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
  3889. */
  3890. int snd_hda_ch_mode_get(struct hda_codec *codec,
  3891. struct snd_ctl_elem_value *ucontrol,
  3892. const struct hda_channel_mode *chmode,
  3893. int num_chmodes,
  3894. int max_channels)
  3895. {
  3896. int i;
  3897. for (i = 0; i < num_chmodes; i++) {
  3898. if (max_channels == chmode[i].channels) {
  3899. ucontrol->value.enumerated.item[0] = i;
  3900. break;
  3901. }
  3902. }
  3903. return 0;
  3904. }
  3905. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
  3906. /**
  3907. * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
  3908. */
  3909. int snd_hda_ch_mode_put(struct hda_codec *codec,
  3910. struct snd_ctl_elem_value *ucontrol,
  3911. const struct hda_channel_mode *chmode,
  3912. int num_chmodes,
  3913. int *max_channelsp)
  3914. {
  3915. unsigned int mode;
  3916. mode = ucontrol->value.enumerated.item[0];
  3917. if (mode >= num_chmodes)
  3918. return -EINVAL;
  3919. if (*max_channelsp == chmode[mode].channels)
  3920. return 0;
  3921. /* change the current channel setting */
  3922. *max_channelsp = chmode[mode].channels;
  3923. if (chmode[mode].sequence)
  3924. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  3925. return 1;
  3926. }
  3927. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
  3928. /*
  3929. * input MUX helper
  3930. */
  3931. /**
  3932. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  3933. */
  3934. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  3935. struct snd_ctl_elem_info *uinfo)
  3936. {
  3937. unsigned int index;
  3938. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3939. uinfo->count = 1;
  3940. uinfo->value.enumerated.items = imux->num_items;
  3941. if (!imux->num_items)
  3942. return 0;
  3943. index = uinfo->value.enumerated.item;
  3944. if (index >= imux->num_items)
  3945. index = imux->num_items - 1;
  3946. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  3947. return 0;
  3948. }
  3949. EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
  3950. /**
  3951. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  3952. */
  3953. int snd_hda_input_mux_put(struct hda_codec *codec,
  3954. const struct hda_input_mux *imux,
  3955. struct snd_ctl_elem_value *ucontrol,
  3956. hda_nid_t nid,
  3957. unsigned int *cur_val)
  3958. {
  3959. unsigned int idx;
  3960. if (!imux->num_items)
  3961. return 0;
  3962. idx = ucontrol->value.enumerated.item[0];
  3963. if (idx >= imux->num_items)
  3964. idx = imux->num_items - 1;
  3965. if (*cur_val == idx)
  3966. return 0;
  3967. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  3968. imux->items[idx].index);
  3969. *cur_val = idx;
  3970. return 1;
  3971. }
  3972. EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
  3973. /*
  3974. * Multi-channel / digital-out PCM helper functions
  3975. */
  3976. /* setup SPDIF output stream */
  3977. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  3978. unsigned int stream_tag, unsigned int format)
  3979. {
  3980. struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
  3981. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  3982. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  3983. set_dig_out_convert(codec, nid,
  3984. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  3985. -1);
  3986. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  3987. if (codec->slave_dig_outs) {
  3988. const hda_nid_t *d;
  3989. for (d = codec->slave_dig_outs; *d; d++)
  3990. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  3991. format);
  3992. }
  3993. /* turn on again (if needed) */
  3994. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  3995. set_dig_out_convert(codec, nid,
  3996. spdif->ctls & 0xff, -1);
  3997. }
  3998. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  3999. {
  4000. snd_hda_codec_cleanup_stream(codec, nid);
  4001. if (codec->slave_dig_outs) {
  4002. const hda_nid_t *d;
  4003. for (d = codec->slave_dig_outs; *d; d++)
  4004. snd_hda_codec_cleanup_stream(codec, *d);
  4005. }
  4006. }
  4007. /**
  4008. * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
  4009. * @bus: HD-audio bus
  4010. */
  4011. void snd_hda_bus_reboot_notify(struct hda_bus *bus)
  4012. {
  4013. struct hda_codec *codec;
  4014. if (!bus)
  4015. return;
  4016. list_for_each_entry(codec, &bus->codec_list, list) {
  4017. if (hda_codec_is_power_on(codec) &&
  4018. codec->patch_ops.reboot_notify)
  4019. codec->patch_ops.reboot_notify(codec);
  4020. }
  4021. }
  4022. EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
  4023. /**
  4024. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  4025. */
  4026. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  4027. struct hda_multi_out *mout)
  4028. {
  4029. mutex_lock(&codec->spdif_mutex);
  4030. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  4031. /* already opened as analog dup; reset it once */
  4032. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4033. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  4034. mutex_unlock(&codec->spdif_mutex);
  4035. return 0;
  4036. }
  4037. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
  4038. /**
  4039. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  4040. */
  4041. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  4042. struct hda_multi_out *mout,
  4043. unsigned int stream_tag,
  4044. unsigned int format,
  4045. struct snd_pcm_substream *substream)
  4046. {
  4047. mutex_lock(&codec->spdif_mutex);
  4048. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  4049. mutex_unlock(&codec->spdif_mutex);
  4050. return 0;
  4051. }
  4052. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
  4053. /**
  4054. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  4055. */
  4056. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  4057. struct hda_multi_out *mout)
  4058. {
  4059. mutex_lock(&codec->spdif_mutex);
  4060. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4061. mutex_unlock(&codec->spdif_mutex);
  4062. return 0;
  4063. }
  4064. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
  4065. /**
  4066. * snd_hda_multi_out_dig_close - release the digital out stream
  4067. */
  4068. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  4069. struct hda_multi_out *mout)
  4070. {
  4071. mutex_lock(&codec->spdif_mutex);
  4072. mout->dig_out_used = 0;
  4073. mutex_unlock(&codec->spdif_mutex);
  4074. return 0;
  4075. }
  4076. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
  4077. /**
  4078. * snd_hda_multi_out_analog_open - open analog outputs
  4079. *
  4080. * Open analog outputs and set up the hw-constraints.
  4081. * If the digital outputs can be opened as slave, open the digital
  4082. * outputs, too.
  4083. */
  4084. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  4085. struct hda_multi_out *mout,
  4086. struct snd_pcm_substream *substream,
  4087. struct hda_pcm_stream *hinfo)
  4088. {
  4089. struct snd_pcm_runtime *runtime = substream->runtime;
  4090. runtime->hw.channels_max = mout->max_channels;
  4091. if (mout->dig_out_nid) {
  4092. if (!mout->analog_rates) {
  4093. mout->analog_rates = hinfo->rates;
  4094. mout->analog_formats = hinfo->formats;
  4095. mout->analog_maxbps = hinfo->maxbps;
  4096. } else {
  4097. runtime->hw.rates = mout->analog_rates;
  4098. runtime->hw.formats = mout->analog_formats;
  4099. hinfo->maxbps = mout->analog_maxbps;
  4100. }
  4101. if (!mout->spdif_rates) {
  4102. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  4103. &mout->spdif_rates,
  4104. &mout->spdif_formats,
  4105. &mout->spdif_maxbps);
  4106. }
  4107. mutex_lock(&codec->spdif_mutex);
  4108. if (mout->share_spdif) {
  4109. if ((runtime->hw.rates & mout->spdif_rates) &&
  4110. (runtime->hw.formats & mout->spdif_formats)) {
  4111. runtime->hw.rates &= mout->spdif_rates;
  4112. runtime->hw.formats &= mout->spdif_formats;
  4113. if (mout->spdif_maxbps < hinfo->maxbps)
  4114. hinfo->maxbps = mout->spdif_maxbps;
  4115. } else {
  4116. mout->share_spdif = 0;
  4117. /* FIXME: need notify? */
  4118. }
  4119. }
  4120. mutex_unlock(&codec->spdif_mutex);
  4121. }
  4122. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  4123. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  4124. }
  4125. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
  4126. /**
  4127. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  4128. *
  4129. * Set up the i/o for analog out.
  4130. * When the digital out is available, copy the front out to digital out, too.
  4131. */
  4132. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  4133. struct hda_multi_out *mout,
  4134. unsigned int stream_tag,
  4135. unsigned int format,
  4136. struct snd_pcm_substream *substream)
  4137. {
  4138. const hda_nid_t *nids = mout->dac_nids;
  4139. int chs = substream->runtime->channels;
  4140. struct hda_spdif_out *spdif =
  4141. snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  4142. int i;
  4143. mutex_lock(&codec->spdif_mutex);
  4144. if (mout->dig_out_nid && mout->share_spdif &&
  4145. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  4146. if (chs == 2 &&
  4147. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  4148. format) &&
  4149. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  4150. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  4151. setup_dig_out_stream(codec, mout->dig_out_nid,
  4152. stream_tag, format);
  4153. } else {
  4154. mout->dig_out_used = 0;
  4155. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4156. }
  4157. }
  4158. mutex_unlock(&codec->spdif_mutex);
  4159. /* front */
  4160. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  4161. 0, format);
  4162. if (!mout->no_share_stream &&
  4163. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  4164. /* headphone out will just decode front left/right (stereo) */
  4165. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  4166. 0, format);
  4167. /* extra outputs copied from front */
  4168. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  4169. if (!mout->no_share_stream && mout->hp_out_nid[i])
  4170. snd_hda_codec_setup_stream(codec,
  4171. mout->hp_out_nid[i],
  4172. stream_tag, 0, format);
  4173. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  4174. if (!mout->no_share_stream && mout->extra_out_nid[i])
  4175. snd_hda_codec_setup_stream(codec,
  4176. mout->extra_out_nid[i],
  4177. stream_tag, 0, format);
  4178. /* surrounds */
  4179. for (i = 1; i < mout->num_dacs; i++) {
  4180. if (chs >= (i + 1) * 2) /* independent out */
  4181. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  4182. i * 2, format);
  4183. else if (!mout->no_share_stream) /* copy front */
  4184. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  4185. 0, format);
  4186. }
  4187. return 0;
  4188. }
  4189. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
  4190. /**
  4191. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  4192. */
  4193. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  4194. struct hda_multi_out *mout)
  4195. {
  4196. const hda_nid_t *nids = mout->dac_nids;
  4197. int i;
  4198. for (i = 0; i < mout->num_dacs; i++)
  4199. snd_hda_codec_cleanup_stream(codec, nids[i]);
  4200. if (mout->hp_nid)
  4201. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  4202. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  4203. if (mout->hp_out_nid[i])
  4204. snd_hda_codec_cleanup_stream(codec,
  4205. mout->hp_out_nid[i]);
  4206. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  4207. if (mout->extra_out_nid[i])
  4208. snd_hda_codec_cleanup_stream(codec,
  4209. mout->extra_out_nid[i]);
  4210. mutex_lock(&codec->spdif_mutex);
  4211. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  4212. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4213. mout->dig_out_used = 0;
  4214. }
  4215. mutex_unlock(&codec->spdif_mutex);
  4216. return 0;
  4217. }
  4218. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
  4219. /*
  4220. * Helper for automatic pin configuration
  4221. */
  4222. static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
  4223. {
  4224. for (; *list; list++)
  4225. if (*list == nid)
  4226. return 1;
  4227. return 0;
  4228. }
  4229. /*
  4230. * Sort an associated group of pins according to their sequence numbers.
  4231. */
  4232. static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
  4233. int num_pins)
  4234. {
  4235. int i, j;
  4236. short seq;
  4237. hda_nid_t nid;
  4238. for (i = 0; i < num_pins; i++) {
  4239. for (j = i + 1; j < num_pins; j++) {
  4240. if (sequences[i] > sequences[j]) {
  4241. seq = sequences[i];
  4242. sequences[i] = sequences[j];
  4243. sequences[j] = seq;
  4244. nid = pins[i];
  4245. pins[i] = pins[j];
  4246. pins[j] = nid;
  4247. }
  4248. }
  4249. }
  4250. }
  4251. /* add the found input-pin to the cfg->inputs[] table */
  4252. static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
  4253. int type)
  4254. {
  4255. if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
  4256. cfg->inputs[cfg->num_inputs].pin = nid;
  4257. cfg->inputs[cfg->num_inputs].type = type;
  4258. cfg->num_inputs++;
  4259. }
  4260. }
  4261. /* sort inputs in the order of AUTO_PIN_* type */
  4262. static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
  4263. {
  4264. int i, j;
  4265. for (i = 0; i < cfg->num_inputs; i++) {
  4266. for (j = i + 1; j < cfg->num_inputs; j++) {
  4267. if (cfg->inputs[i].type > cfg->inputs[j].type) {
  4268. struct auto_pin_cfg_item tmp;
  4269. tmp = cfg->inputs[i];
  4270. cfg->inputs[i] = cfg->inputs[j];
  4271. cfg->inputs[j] = tmp;
  4272. }
  4273. }
  4274. }
  4275. }
  4276. /* Reorder the surround channels
  4277. * ALSA sequence is front/surr/clfe/side
  4278. * HDA sequence is:
  4279. * 4-ch: front/surr => OK as it is
  4280. * 6-ch: front/clfe/surr
  4281. * 8-ch: front/clfe/rear/side|fc
  4282. */
  4283. static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
  4284. {
  4285. hda_nid_t nid;
  4286. switch (nums) {
  4287. case 3:
  4288. case 4:
  4289. nid = pins[1];
  4290. pins[1] = pins[2];
  4291. pins[2] = nid;
  4292. break;
  4293. }
  4294. }
  4295. /*
  4296. * Parse all pin widgets and store the useful pin nids to cfg
  4297. *
  4298. * The number of line-outs or any primary output is stored in line_outs,
  4299. * and the corresponding output pins are assigned to line_out_pins[],
  4300. * in the order of front, rear, CLFE, side, ...
  4301. *
  4302. * If more extra outputs (speaker and headphone) are found, the pins are
  4303. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  4304. * is detected, one of speaker of HP pins is assigned as the primary
  4305. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  4306. * if any analog output exists.
  4307. *
  4308. * The analog input pins are assigned to inputs array.
  4309. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  4310. * respectively.
  4311. */
  4312. int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
  4313. struct auto_pin_cfg *cfg,
  4314. const hda_nid_t *ignore_nids,
  4315. unsigned int cond_flags)
  4316. {
  4317. hda_nid_t nid, end_nid;
  4318. short seq, assoc_line_out;
  4319. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  4320. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  4321. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  4322. int i;
  4323. memset(cfg, 0, sizeof(*cfg));
  4324. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  4325. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  4326. memset(sequences_hp, 0, sizeof(sequences_hp));
  4327. assoc_line_out = 0;
  4328. codec->ignore_misc_bit = true;
  4329. end_nid = codec->start_nid + codec->num_nodes;
  4330. for (nid = codec->start_nid; nid < end_nid; nid++) {
  4331. unsigned int wid_caps = get_wcaps(codec, nid);
  4332. unsigned int wid_type = get_wcaps_type(wid_caps);
  4333. unsigned int def_conf;
  4334. short assoc, loc, conn, dev;
  4335. /* read all default configuration for pin complex */
  4336. if (wid_type != AC_WID_PIN)
  4337. continue;
  4338. /* ignore the given nids (e.g. pc-beep returns error) */
  4339. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  4340. continue;
  4341. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  4342. if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
  4343. AC_DEFCFG_MISC_NO_PRESENCE))
  4344. codec->ignore_misc_bit = false;
  4345. conn = get_defcfg_connect(def_conf);
  4346. if (conn == AC_JACK_PORT_NONE)
  4347. continue;
  4348. loc = get_defcfg_location(def_conf);
  4349. dev = get_defcfg_device(def_conf);
  4350. /* workaround for buggy BIOS setups */
  4351. if (dev == AC_JACK_LINE_OUT) {
  4352. if (conn == AC_JACK_PORT_FIXED)
  4353. dev = AC_JACK_SPEAKER;
  4354. }
  4355. switch (dev) {
  4356. case AC_JACK_LINE_OUT:
  4357. seq = get_defcfg_sequence(def_conf);
  4358. assoc = get_defcfg_association(def_conf);
  4359. if (!(wid_caps & AC_WCAP_STEREO))
  4360. if (!cfg->mono_out_pin)
  4361. cfg->mono_out_pin = nid;
  4362. if (!assoc)
  4363. continue;
  4364. if (!assoc_line_out)
  4365. assoc_line_out = assoc;
  4366. else if (assoc_line_out != assoc)
  4367. continue;
  4368. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  4369. continue;
  4370. cfg->line_out_pins[cfg->line_outs] = nid;
  4371. sequences_line_out[cfg->line_outs] = seq;
  4372. cfg->line_outs++;
  4373. break;
  4374. case AC_JACK_SPEAKER:
  4375. seq = get_defcfg_sequence(def_conf);
  4376. assoc = get_defcfg_association(def_conf);
  4377. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  4378. continue;
  4379. cfg->speaker_pins[cfg->speaker_outs] = nid;
  4380. sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
  4381. cfg->speaker_outs++;
  4382. break;
  4383. case AC_JACK_HP_OUT:
  4384. seq = get_defcfg_sequence(def_conf);
  4385. assoc = get_defcfg_association(def_conf);
  4386. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  4387. continue;
  4388. cfg->hp_pins[cfg->hp_outs] = nid;
  4389. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  4390. cfg->hp_outs++;
  4391. break;
  4392. case AC_JACK_MIC_IN:
  4393. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
  4394. break;
  4395. case AC_JACK_LINE_IN:
  4396. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
  4397. break;
  4398. case AC_JACK_CD:
  4399. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
  4400. break;
  4401. case AC_JACK_AUX:
  4402. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
  4403. break;
  4404. case AC_JACK_SPDIF_OUT:
  4405. case AC_JACK_DIG_OTHER_OUT:
  4406. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
  4407. continue;
  4408. cfg->dig_out_pins[cfg->dig_outs] = nid;
  4409. cfg->dig_out_type[cfg->dig_outs] =
  4410. (loc == AC_JACK_LOC_HDMI) ?
  4411. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  4412. cfg->dig_outs++;
  4413. break;
  4414. case AC_JACK_SPDIF_IN:
  4415. case AC_JACK_DIG_OTHER_IN:
  4416. cfg->dig_in_pin = nid;
  4417. if (loc == AC_JACK_LOC_HDMI)
  4418. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  4419. else
  4420. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  4421. break;
  4422. }
  4423. }
  4424. /* FIX-UP:
  4425. * If no line-out is defined but multiple HPs are found,
  4426. * some of them might be the real line-outs.
  4427. */
  4428. if (!cfg->line_outs && cfg->hp_outs > 1 &&
  4429. !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
  4430. int i = 0;
  4431. while (i < cfg->hp_outs) {
  4432. /* The real HPs should have the sequence 0x0f */
  4433. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  4434. i++;
  4435. continue;
  4436. }
  4437. /* Move it to the line-out table */
  4438. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  4439. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  4440. cfg->line_outs++;
  4441. cfg->hp_outs--;
  4442. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  4443. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  4444. memmove(sequences_hp + i, sequences_hp + i + 1,
  4445. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  4446. }
  4447. memset(cfg->hp_pins + cfg->hp_outs, 0,
  4448. sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
  4449. if (!cfg->hp_outs)
  4450. cfg->line_out_type = AUTO_PIN_HP_OUT;
  4451. }
  4452. /* sort by sequence */
  4453. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  4454. cfg->line_outs);
  4455. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  4456. cfg->speaker_outs);
  4457. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  4458. cfg->hp_outs);
  4459. /*
  4460. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  4461. * as a primary output
  4462. */
  4463. if (!cfg->line_outs &&
  4464. !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
  4465. if (cfg->speaker_outs) {
  4466. cfg->line_outs = cfg->speaker_outs;
  4467. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  4468. sizeof(cfg->speaker_pins));
  4469. cfg->speaker_outs = 0;
  4470. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  4471. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  4472. } else if (cfg->hp_outs) {
  4473. cfg->line_outs = cfg->hp_outs;
  4474. memcpy(cfg->line_out_pins, cfg->hp_pins,
  4475. sizeof(cfg->hp_pins));
  4476. cfg->hp_outs = 0;
  4477. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  4478. cfg->line_out_type = AUTO_PIN_HP_OUT;
  4479. }
  4480. }
  4481. reorder_outputs(cfg->line_outs, cfg->line_out_pins);
  4482. reorder_outputs(cfg->hp_outs, cfg->hp_pins);
  4483. reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
  4484. sort_autocfg_input_pins(cfg);
  4485. /*
  4486. * debug prints of the parsed results
  4487. */
  4488. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
  4489. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  4490. cfg->line_out_pins[2], cfg->line_out_pins[3],
  4491. cfg->line_out_pins[4],
  4492. cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
  4493. (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
  4494. "speaker" : "line"));
  4495. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  4496. cfg->speaker_outs, cfg->speaker_pins[0],
  4497. cfg->speaker_pins[1], cfg->speaker_pins[2],
  4498. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  4499. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  4500. cfg->hp_outs, cfg->hp_pins[0],
  4501. cfg->hp_pins[1], cfg->hp_pins[2],
  4502. cfg->hp_pins[3], cfg->hp_pins[4]);
  4503. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  4504. if (cfg->dig_outs)
  4505. snd_printd(" dig-out=0x%x/0x%x\n",
  4506. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  4507. snd_printd(" inputs:");
  4508. for (i = 0; i < cfg->num_inputs; i++) {
  4509. snd_printd(" %s=0x%x",
  4510. hda_get_autocfg_input_label(codec, cfg, i),
  4511. cfg->inputs[i].pin);
  4512. }
  4513. snd_printd("\n");
  4514. if (cfg->dig_in_pin)
  4515. snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
  4516. return 0;
  4517. }
  4518. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
  4519. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  4520. {
  4521. unsigned int loc = get_defcfg_location(def_conf);
  4522. unsigned int conn = get_defcfg_connect(def_conf);
  4523. if (conn == AC_JACK_PORT_NONE)
  4524. return INPUT_PIN_ATTR_UNUSED;
  4525. /* Windows may claim the internal mic to be BOTH, too */
  4526. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  4527. return INPUT_PIN_ATTR_INT;
  4528. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  4529. return INPUT_PIN_ATTR_INT;
  4530. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  4531. return INPUT_PIN_ATTR_DOCK;
  4532. if (loc == AC_JACK_LOC_REAR)
  4533. return INPUT_PIN_ATTR_REAR;
  4534. if (loc == AC_JACK_LOC_FRONT)
  4535. return INPUT_PIN_ATTR_FRONT;
  4536. return INPUT_PIN_ATTR_NORMAL;
  4537. }
  4538. EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
  4539. /**
  4540. * hda_get_input_pin_label - Give a label for the given input pin
  4541. *
  4542. * When check_location is true, the function checks the pin location
  4543. * for mic and line-in pins, and set an appropriate prefix like "Front",
  4544. * "Rear", "Internal".
  4545. */
  4546. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  4547. hda_nid_t pin, bool check_location)
  4548. {
  4549. unsigned int def_conf;
  4550. static const char * const mic_names[] = {
  4551. "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
  4552. };
  4553. int attr;
  4554. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  4555. switch (get_defcfg_device(def_conf)) {
  4556. case AC_JACK_MIC_IN:
  4557. if (!check_location)
  4558. return "Mic";
  4559. attr = snd_hda_get_input_pin_attr(def_conf);
  4560. if (!attr)
  4561. return "None";
  4562. return mic_names[attr - 1];
  4563. case AC_JACK_LINE_IN:
  4564. if (!check_location)
  4565. return "Line";
  4566. attr = snd_hda_get_input_pin_attr(def_conf);
  4567. if (!attr)
  4568. return "None";
  4569. if (attr == INPUT_PIN_ATTR_DOCK)
  4570. return "Dock Line";
  4571. return "Line";
  4572. case AC_JACK_AUX:
  4573. return "Aux";
  4574. case AC_JACK_CD:
  4575. return "CD";
  4576. case AC_JACK_SPDIF_IN:
  4577. return "SPDIF In";
  4578. case AC_JACK_DIG_OTHER_IN:
  4579. return "Digital In";
  4580. default:
  4581. return "Misc";
  4582. }
  4583. }
  4584. /* Check whether the location prefix needs to be added to the label.
  4585. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  4586. * have to put "Front" prefix to each label. In such a case, returns false.
  4587. */
  4588. static int check_mic_location_need(struct hda_codec *codec,
  4589. const struct auto_pin_cfg *cfg,
  4590. int input)
  4591. {
  4592. unsigned int defc;
  4593. int i, attr, attr2;
  4594. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  4595. attr = snd_hda_get_input_pin_attr(defc);
  4596. /* for internal or docking mics, we need locations */
  4597. if (attr <= INPUT_PIN_ATTR_NORMAL)
  4598. return 1;
  4599. attr = 0;
  4600. for (i = 0; i < cfg->num_inputs; i++) {
  4601. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  4602. attr2 = snd_hda_get_input_pin_attr(defc);
  4603. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  4604. if (attr && attr != attr2)
  4605. return 1; /* different locations found */
  4606. attr = attr2;
  4607. }
  4608. }
  4609. return 0;
  4610. }
  4611. /**
  4612. * hda_get_autocfg_input_label - Get a label for the given input
  4613. *
  4614. * Get a label for the given input pin defined by the autocfg item.
  4615. * Unlike hda_get_input_pin_label(), this function checks all inputs
  4616. * defined in autocfg and avoids the redundant mic/line prefix as much as
  4617. * possible.
  4618. */
  4619. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  4620. const struct auto_pin_cfg *cfg,
  4621. int input)
  4622. {
  4623. int type = cfg->inputs[input].type;
  4624. int has_multiple_pins = 0;
  4625. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  4626. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  4627. has_multiple_pins = 1;
  4628. if (has_multiple_pins && type == AUTO_PIN_MIC)
  4629. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  4630. return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
  4631. has_multiple_pins);
  4632. }
  4633. EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
  4634. /* return the position of NID in the list, or -1 if not found */
  4635. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  4636. {
  4637. int i;
  4638. for (i = 0; i < nums; i++)
  4639. if (list[i] == nid)
  4640. return i;
  4641. return -1;
  4642. }
  4643. /* get a unique suffix or an index number */
  4644. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  4645. int num_pins, int *indexp)
  4646. {
  4647. static const char * const channel_sfx[] = {
  4648. " Front", " Surround", " CLFE", " Side"
  4649. };
  4650. int i;
  4651. i = find_idx_in_nid_list(nid, pins, num_pins);
  4652. if (i < 0)
  4653. return NULL;
  4654. if (num_pins == 1)
  4655. return "";
  4656. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  4657. if (indexp)
  4658. *indexp = i;
  4659. return "";
  4660. }
  4661. return channel_sfx[i];
  4662. }
  4663. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  4664. const struct auto_pin_cfg *cfg,
  4665. const char *name, char *label, int maxlen,
  4666. int *indexp)
  4667. {
  4668. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  4669. int attr = snd_hda_get_input_pin_attr(def_conf);
  4670. const char *pfx = "", *sfx = "";
  4671. /* handle as a speaker if it's a fixed line-out */
  4672. if (!strcmp(name, "Line-Out") && attr == INPUT_PIN_ATTR_INT)
  4673. name = "Speaker";
  4674. /* check the location */
  4675. switch (attr) {
  4676. case INPUT_PIN_ATTR_DOCK:
  4677. pfx = "Dock ";
  4678. break;
  4679. case INPUT_PIN_ATTR_FRONT:
  4680. pfx = "Front ";
  4681. break;
  4682. }
  4683. if (cfg) {
  4684. /* try to give a unique suffix if needed */
  4685. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  4686. indexp);
  4687. if (!sfx)
  4688. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  4689. indexp);
  4690. if (!sfx) {
  4691. /* don't add channel suffix for Headphone controls */
  4692. int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
  4693. cfg->hp_outs);
  4694. if (idx >= 0)
  4695. *indexp = idx;
  4696. sfx = "";
  4697. }
  4698. }
  4699. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  4700. return 1;
  4701. }
  4702. /**
  4703. * snd_hda_get_pin_label - Get a label for the given I/O pin
  4704. *
  4705. * Get a label for the given pin. This function works for both input and
  4706. * output pins. When @cfg is given as non-NULL, the function tries to get
  4707. * an optimized label using hda_get_autocfg_input_label().
  4708. *
  4709. * This function tries to give a unique label string for the pin as much as
  4710. * possible. For example, when the multiple line-outs are present, it adds
  4711. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  4712. * If no unique name with a suffix is available and @indexp is non-NULL, the
  4713. * index number is stored in the pointer.
  4714. */
  4715. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  4716. const struct auto_pin_cfg *cfg,
  4717. char *label, int maxlen, int *indexp)
  4718. {
  4719. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  4720. const char *name = NULL;
  4721. int i;
  4722. if (indexp)
  4723. *indexp = 0;
  4724. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  4725. return 0;
  4726. switch (get_defcfg_device(def_conf)) {
  4727. case AC_JACK_LINE_OUT:
  4728. return fill_audio_out_name(codec, nid, cfg, "Line-Out",
  4729. label, maxlen, indexp);
  4730. case AC_JACK_SPEAKER:
  4731. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  4732. label, maxlen, indexp);
  4733. case AC_JACK_HP_OUT:
  4734. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  4735. label, maxlen, indexp);
  4736. case AC_JACK_SPDIF_OUT:
  4737. case AC_JACK_DIG_OTHER_OUT:
  4738. if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
  4739. name = "HDMI";
  4740. else
  4741. name = "SPDIF";
  4742. if (cfg && indexp) {
  4743. i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
  4744. cfg->dig_outs);
  4745. if (i >= 0)
  4746. *indexp = i;
  4747. }
  4748. break;
  4749. default:
  4750. if (cfg) {
  4751. for (i = 0; i < cfg->num_inputs; i++) {
  4752. if (cfg->inputs[i].pin != nid)
  4753. continue;
  4754. name = hda_get_autocfg_input_label(codec, cfg, i);
  4755. if (name)
  4756. break;
  4757. }
  4758. }
  4759. if (!name)
  4760. name = hda_get_input_pin_label(codec, nid, true);
  4761. break;
  4762. }
  4763. if (!name)
  4764. return 0;
  4765. strlcpy(label, name, maxlen);
  4766. return 1;
  4767. }
  4768. EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
  4769. /**
  4770. * snd_hda_add_imux_item - Add an item to input_mux
  4771. *
  4772. * When the same label is used already in the existing items, the number
  4773. * suffix is appended to the label. This label index number is stored
  4774. * to type_idx when non-NULL pointer is given.
  4775. */
  4776. int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
  4777. int index, int *type_idx)
  4778. {
  4779. int i, label_idx = 0;
  4780. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  4781. snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
  4782. return -EINVAL;
  4783. }
  4784. for (i = 0; i < imux->num_items; i++) {
  4785. if (!strncmp(label, imux->items[i].label, strlen(label)))
  4786. label_idx++;
  4787. }
  4788. if (type_idx)
  4789. *type_idx = label_idx;
  4790. if (label_idx > 0)
  4791. snprintf(imux->items[imux->num_items].label,
  4792. sizeof(imux->items[imux->num_items].label),
  4793. "%s %d", label, label_idx);
  4794. else
  4795. strlcpy(imux->items[imux->num_items].label, label,
  4796. sizeof(imux->items[imux->num_items].label));
  4797. imux->items[imux->num_items].index = index;
  4798. imux->num_items++;
  4799. return 0;
  4800. }
  4801. EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
  4802. #ifdef CONFIG_PM
  4803. /*
  4804. * power management
  4805. */
  4806. /**
  4807. * snd_hda_suspend - suspend the codecs
  4808. * @bus: the HDA bus
  4809. *
  4810. * Returns 0 if successful.
  4811. */
  4812. int snd_hda_suspend(struct hda_bus *bus)
  4813. {
  4814. struct hda_codec *codec;
  4815. list_for_each_entry(codec, &bus->codec_list, list) {
  4816. if (hda_codec_is_power_on(codec))
  4817. hda_call_codec_suspend(codec);
  4818. if (codec->patch_ops.post_suspend)
  4819. codec->patch_ops.post_suspend(codec);
  4820. }
  4821. return 0;
  4822. }
  4823. EXPORT_SYMBOL_HDA(snd_hda_suspend);
  4824. /**
  4825. * snd_hda_resume - resume the codecs
  4826. * @bus: the HDA bus
  4827. *
  4828. * Returns 0 if successful.
  4829. *
  4830. * This function is defined only when POWER_SAVE isn't set.
  4831. * In the power-save mode, the codec is resumed dynamically.
  4832. */
  4833. int snd_hda_resume(struct hda_bus *bus)
  4834. {
  4835. struct hda_codec *codec;
  4836. list_for_each_entry(codec, &bus->codec_list, list) {
  4837. if (codec->patch_ops.pre_resume)
  4838. codec->patch_ops.pre_resume(codec);
  4839. if (snd_hda_codec_needs_resume(codec))
  4840. hda_call_codec_resume(codec);
  4841. }
  4842. return 0;
  4843. }
  4844. EXPORT_SYMBOL_HDA(snd_hda_resume);
  4845. #endif /* CONFIG_PM */
  4846. /*
  4847. * generic arrays
  4848. */
  4849. /**
  4850. * snd_array_new - get a new element from the given array
  4851. * @array: the array object
  4852. *
  4853. * Get a new element from the given array. If it exceeds the
  4854. * pre-allocated array size, re-allocate the array.
  4855. *
  4856. * Returns NULL if allocation failed.
  4857. */
  4858. void *snd_array_new(struct snd_array *array)
  4859. {
  4860. if (array->used >= array->alloced) {
  4861. int num = array->alloced + array->alloc_align;
  4862. int size = (num + 1) * array->elem_size;
  4863. int oldsize = array->alloced * array->elem_size;
  4864. void *nlist;
  4865. if (snd_BUG_ON(num >= 4096))
  4866. return NULL;
  4867. nlist = krealloc(array->list, size, GFP_KERNEL);
  4868. if (!nlist)
  4869. return NULL;
  4870. memset(nlist + oldsize, 0, size - oldsize);
  4871. array->list = nlist;
  4872. array->alloced = num;
  4873. }
  4874. return snd_array_elem(array, array->used++);
  4875. }
  4876. EXPORT_SYMBOL_HDA(snd_array_new);
  4877. /**
  4878. * snd_array_free - free the given array elements
  4879. * @array: the array object
  4880. */
  4881. void snd_array_free(struct snd_array *array)
  4882. {
  4883. kfree(array->list);
  4884. array->used = 0;
  4885. array->alloced = 0;
  4886. array->list = NULL;
  4887. }
  4888. EXPORT_SYMBOL_HDA(snd_array_free);
  4889. /**
  4890. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  4891. * @pcm: PCM caps bits
  4892. * @buf: the string buffer to write
  4893. * @buflen: the max buffer length
  4894. *
  4895. * used by hda_proc.c and hda_eld.c
  4896. */
  4897. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  4898. {
  4899. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  4900. int i, j;
  4901. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  4902. if (pcm & (AC_SUPPCM_BITS_8 << i))
  4903. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  4904. buf[j] = '\0'; /* necessary when j == 0 */
  4905. }
  4906. EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
  4907. MODULE_DESCRIPTION("HDA codec core");
  4908. MODULE_LICENSE("GPL");