hda_codec.c 142 KB

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