hda_codec.c 135 KB

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