hda_codec.c 149 KB

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