hda_codec.c 137 KB

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