hda_codec.c 141 KB

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