hda_codec.c 141 KB

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