hda_codec.c 135 KB

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