hda_codec.c 136 KB

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