hda_codec.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036
  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 "hda_local.h"
  32. #include <sound/hda_hwdep.h>
  33. /*
  34. * vendor / preset table
  35. */
  36. struct hda_vendor_id {
  37. unsigned int id;
  38. const char *name;
  39. };
  40. /* codec vendor labels */
  41. static struct hda_vendor_id hda_vendor_ids[] = {
  42. { 0x1002, "ATI" },
  43. { 0x1013, "Cirrus Logic" },
  44. { 0x1057, "Motorola" },
  45. { 0x1095, "Silicon Image" },
  46. { 0x10de, "Nvidia" },
  47. { 0x10ec, "Realtek" },
  48. { 0x1102, "Creative" },
  49. { 0x1106, "VIA" },
  50. { 0x111d, "IDT" },
  51. { 0x11c1, "LSI" },
  52. { 0x11d4, "Analog Devices" },
  53. { 0x13f6, "C-Media" },
  54. { 0x14f1, "Conexant" },
  55. { 0x17e8, "Chrontel" },
  56. { 0x1854, "LG" },
  57. { 0x1aec, "Wolfson Microelectronics" },
  58. { 0x434d, "C-Media" },
  59. { 0x8086, "Intel" },
  60. { 0x8384, "SigmaTel" },
  61. {} /* terminator */
  62. };
  63. static DEFINE_MUTEX(preset_mutex);
  64. static LIST_HEAD(hda_preset_tables);
  65. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  66. {
  67. mutex_lock(&preset_mutex);
  68. list_add_tail(&preset->list, &hda_preset_tables);
  69. mutex_unlock(&preset_mutex);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
  73. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  74. {
  75. mutex_lock(&preset_mutex);
  76. list_del(&preset->list);
  77. mutex_unlock(&preset_mutex);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
  81. #ifdef CONFIG_SND_HDA_POWER_SAVE
  82. static void hda_power_work(struct work_struct *work);
  83. static void hda_keep_power_on(struct hda_codec *codec);
  84. #else
  85. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  86. #endif
  87. const char *snd_hda_get_jack_location(u32 cfg)
  88. {
  89. static char *bases[7] = {
  90. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  91. };
  92. static unsigned char specials_idx[] = {
  93. 0x07, 0x08,
  94. 0x17, 0x18, 0x19,
  95. 0x37, 0x38
  96. };
  97. static char *specials[] = {
  98. "Rear Panel", "Drive Bar",
  99. "Riser", "HDMI", "ATAPI",
  100. "Mobile-In", "Mobile-Out"
  101. };
  102. int i;
  103. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  104. if ((cfg & 0x0f) < 7)
  105. return bases[cfg & 0x0f];
  106. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  107. if (cfg == specials_idx[i])
  108. return specials[i];
  109. }
  110. return "UNKNOWN";
  111. }
  112. EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
  113. const char *snd_hda_get_jack_connectivity(u32 cfg)
  114. {
  115. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  116. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  117. }
  118. EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
  119. const char *snd_hda_get_jack_type(u32 cfg)
  120. {
  121. static char *jack_types[16] = {
  122. "Line Out", "Speaker", "HP Out", "CD",
  123. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  124. "Line In", "Aux", "Mic", "Telephony",
  125. "SPDIF In", "Digitial In", "Reserved", "Other"
  126. };
  127. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  128. >> AC_DEFCFG_DEVICE_SHIFT];
  129. }
  130. EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
  131. /*
  132. * Compose a 32bit command word to be sent to the HD-audio controller
  133. */
  134. static inline unsigned int
  135. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  136. unsigned int verb, unsigned int parm)
  137. {
  138. u32 val;
  139. if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
  140. (verb & ~0xfff) || (parm & ~0xffff)) {
  141. printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
  142. codec->addr, direct, nid, verb, parm);
  143. return ~0;
  144. }
  145. val = (u32)codec->addr << 28;
  146. val |= (u32)direct << 27;
  147. val |= (u32)nid << 20;
  148. val |= verb << 8;
  149. val |= parm;
  150. return val;
  151. }
  152. /*
  153. * Send and receive a verb
  154. */
  155. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  156. unsigned int *res)
  157. {
  158. struct hda_bus *bus = codec->bus;
  159. int err;
  160. if (cmd == ~0)
  161. return -1;
  162. if (res)
  163. *res = -1;
  164. again:
  165. snd_hda_power_up(codec);
  166. mutex_lock(&bus->cmd_mutex);
  167. err = bus->ops.command(bus, cmd);
  168. if (!err && res)
  169. *res = bus->ops.get_response(bus, codec->addr);
  170. mutex_unlock(&bus->cmd_mutex);
  171. snd_hda_power_down(codec);
  172. if (res && *res == -1 && bus->rirb_error) {
  173. if (bus->response_reset) {
  174. snd_printd("hda_codec: resetting BUS due to "
  175. "fatal communication error\n");
  176. bus->ops.bus_reset(bus);
  177. }
  178. goto again;
  179. }
  180. /* clear reset-flag when the communication gets recovered */
  181. if (!err)
  182. bus->response_reset = 0;
  183. return err;
  184. }
  185. /**
  186. * snd_hda_codec_read - send a command and get the response
  187. * @codec: the HDA codec
  188. * @nid: NID to send the command
  189. * @direct: direct flag
  190. * @verb: the verb to send
  191. * @parm: the parameter for the verb
  192. *
  193. * Send a single command and read the corresponding response.
  194. *
  195. * Returns the obtained response value, or -1 for an error.
  196. */
  197. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  198. int direct,
  199. unsigned int verb, unsigned int parm)
  200. {
  201. unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  202. unsigned int res;
  203. codec_exec_verb(codec, cmd, &res);
  204. return res;
  205. }
  206. EXPORT_SYMBOL_HDA(snd_hda_codec_read);
  207. /**
  208. * snd_hda_codec_write - send a single command without waiting for response
  209. * @codec: the HDA codec
  210. * @nid: NID to send the command
  211. * @direct: direct flag
  212. * @verb: the verb to send
  213. * @parm: the parameter for the verb
  214. *
  215. * Send a single command without waiting for response.
  216. *
  217. * Returns 0 if successful, or a negative error code.
  218. */
  219. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  220. unsigned int verb, unsigned int parm)
  221. {
  222. unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  223. unsigned int res;
  224. return codec_exec_verb(codec, cmd,
  225. codec->bus->sync_write ? &res : NULL);
  226. }
  227. EXPORT_SYMBOL_HDA(snd_hda_codec_write);
  228. /**
  229. * snd_hda_sequence_write - sequence writes
  230. * @codec: the HDA codec
  231. * @seq: VERB array to send
  232. *
  233. * Send the commands sequentially from the given array.
  234. * The array must be terminated with NID=0.
  235. */
  236. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  237. {
  238. for (; seq->nid; seq++)
  239. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  240. }
  241. EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
  242. /**
  243. * snd_hda_get_sub_nodes - get the range of sub nodes
  244. * @codec: the HDA codec
  245. * @nid: NID to parse
  246. * @start_id: the pointer to store the start NID
  247. *
  248. * Parse the NID and store the start NID of its sub-nodes.
  249. * Returns the number of sub-nodes.
  250. */
  251. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  252. hda_nid_t *start_id)
  253. {
  254. unsigned int parm;
  255. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  256. if (parm == -1)
  257. return 0;
  258. *start_id = (parm >> 16) & 0x7fff;
  259. return (int)(parm & 0x7fff);
  260. }
  261. EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
  262. /**
  263. * snd_hda_get_connections - get connection list
  264. * @codec: the HDA codec
  265. * @nid: NID to parse
  266. * @conn_list: connection list array
  267. * @max_conns: max. number of connections to store
  268. *
  269. * Parses the connection list of the given widget and stores the list
  270. * of NIDs.
  271. *
  272. * Returns the number of connections, or a negative error code.
  273. */
  274. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  275. hda_nid_t *conn_list, int max_conns)
  276. {
  277. unsigned int parm;
  278. int i, conn_len, conns;
  279. unsigned int shift, num_elems, mask;
  280. unsigned int wcaps;
  281. hda_nid_t prev_nid;
  282. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  283. return -EINVAL;
  284. wcaps = get_wcaps(codec, nid);
  285. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  286. get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
  287. snd_printk(KERN_WARNING "hda_codec: "
  288. "connection list not available for 0x%x\n", nid);
  289. return -EINVAL;
  290. }
  291. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  292. if (parm & AC_CLIST_LONG) {
  293. /* long form */
  294. shift = 16;
  295. num_elems = 2;
  296. } else {
  297. /* short form */
  298. shift = 8;
  299. num_elems = 4;
  300. }
  301. conn_len = parm & AC_CLIST_LENGTH;
  302. mask = (1 << (shift-1)) - 1;
  303. if (!conn_len)
  304. return 0; /* no connection */
  305. if (conn_len == 1) {
  306. /* single connection */
  307. parm = snd_hda_codec_read(codec, nid, 0,
  308. AC_VERB_GET_CONNECT_LIST, 0);
  309. if (parm == -1 && codec->bus->rirb_error)
  310. return -EIO;
  311. conn_list[0] = parm & mask;
  312. return 1;
  313. }
  314. /* multi connection */
  315. conns = 0;
  316. prev_nid = 0;
  317. for (i = 0; i < conn_len; i++) {
  318. int range_val;
  319. hda_nid_t val, n;
  320. if (i % num_elems == 0) {
  321. parm = snd_hda_codec_read(codec, nid, 0,
  322. AC_VERB_GET_CONNECT_LIST, i);
  323. if (parm == -1 && codec->bus->rirb_error)
  324. return -EIO;
  325. }
  326. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  327. val = parm & mask;
  328. if (val == 0) {
  329. snd_printk(KERN_WARNING "hda_codec: "
  330. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  331. nid, i, parm);
  332. return 0;
  333. }
  334. parm >>= shift;
  335. if (range_val) {
  336. /* ranges between the previous and this one */
  337. if (!prev_nid || prev_nid >= val) {
  338. snd_printk(KERN_WARNING "hda_codec: "
  339. "invalid dep_range_val %x:%x\n",
  340. prev_nid, val);
  341. continue;
  342. }
  343. for (n = prev_nid + 1; n <= val; n++) {
  344. if (conns >= max_conns) {
  345. snd_printk(KERN_ERR
  346. "Too many connections\n");
  347. return -EINVAL;
  348. }
  349. conn_list[conns++] = n;
  350. }
  351. } else {
  352. if (conns >= max_conns) {
  353. snd_printk(KERN_ERR "Too many connections\n");
  354. return -EINVAL;
  355. }
  356. conn_list[conns++] = val;
  357. }
  358. prev_nid = val;
  359. }
  360. return conns;
  361. }
  362. EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  363. /**
  364. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  365. * @bus: the BUS
  366. * @res: unsolicited event (lower 32bit of RIRB entry)
  367. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  368. *
  369. * Adds the given event to the queue. The events are processed in
  370. * the workqueue asynchronously. Call this function in the interrupt
  371. * hanlder when RIRB receives an unsolicited event.
  372. *
  373. * Returns 0 if successful, or a negative error code.
  374. */
  375. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  376. {
  377. struct hda_bus_unsolicited *unsol;
  378. unsigned int wp;
  379. unsol = bus->unsol;
  380. if (!unsol)
  381. return 0;
  382. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  383. unsol->wp = wp;
  384. wp <<= 1;
  385. unsol->queue[wp] = res;
  386. unsol->queue[wp + 1] = res_ex;
  387. queue_work(bus->workq, &unsol->work);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
  391. /*
  392. * process queued unsolicited events
  393. */
  394. static void process_unsol_events(struct work_struct *work)
  395. {
  396. struct hda_bus_unsolicited *unsol =
  397. container_of(work, struct hda_bus_unsolicited, work);
  398. struct hda_bus *bus = unsol->bus;
  399. struct hda_codec *codec;
  400. unsigned int rp, caddr, res;
  401. while (unsol->rp != unsol->wp) {
  402. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  403. unsol->rp = rp;
  404. rp <<= 1;
  405. res = unsol->queue[rp];
  406. caddr = unsol->queue[rp + 1];
  407. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  408. continue;
  409. codec = bus->caddr_tbl[caddr & 0x0f];
  410. if (codec && codec->patch_ops.unsol_event)
  411. codec->patch_ops.unsol_event(codec, res);
  412. }
  413. }
  414. /*
  415. * initialize unsolicited queue
  416. */
  417. static int init_unsol_queue(struct hda_bus *bus)
  418. {
  419. struct hda_bus_unsolicited *unsol;
  420. if (bus->unsol) /* already initialized */
  421. return 0;
  422. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  423. if (!unsol) {
  424. snd_printk(KERN_ERR "hda_codec: "
  425. "can't allocate unsolicited queue\n");
  426. return -ENOMEM;
  427. }
  428. INIT_WORK(&unsol->work, process_unsol_events);
  429. unsol->bus = bus;
  430. bus->unsol = unsol;
  431. return 0;
  432. }
  433. /*
  434. * destructor
  435. */
  436. static void snd_hda_codec_free(struct hda_codec *codec);
  437. static int snd_hda_bus_free(struct hda_bus *bus)
  438. {
  439. struct hda_codec *codec, *n;
  440. if (!bus)
  441. return 0;
  442. if (bus->workq)
  443. flush_workqueue(bus->workq);
  444. if (bus->unsol)
  445. kfree(bus->unsol);
  446. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  447. snd_hda_codec_free(codec);
  448. }
  449. if (bus->ops.private_free)
  450. bus->ops.private_free(bus);
  451. if (bus->workq)
  452. destroy_workqueue(bus->workq);
  453. kfree(bus);
  454. return 0;
  455. }
  456. static int snd_hda_bus_dev_free(struct snd_device *device)
  457. {
  458. struct hda_bus *bus = device->device_data;
  459. bus->shutdown = 1;
  460. return snd_hda_bus_free(bus);
  461. }
  462. #ifdef CONFIG_SND_HDA_HWDEP
  463. static int snd_hda_bus_dev_register(struct snd_device *device)
  464. {
  465. struct hda_bus *bus = device->device_data;
  466. struct hda_codec *codec;
  467. list_for_each_entry(codec, &bus->codec_list, list) {
  468. snd_hda_hwdep_add_sysfs(codec);
  469. }
  470. return 0;
  471. }
  472. #else
  473. #define snd_hda_bus_dev_register NULL
  474. #endif
  475. /**
  476. * snd_hda_bus_new - create a HDA bus
  477. * @card: the card entry
  478. * @temp: the template for hda_bus information
  479. * @busp: the pointer to store the created bus instance
  480. *
  481. * Returns 0 if successful, or a negative error code.
  482. */
  483. int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
  484. const struct hda_bus_template *temp,
  485. struct hda_bus **busp)
  486. {
  487. struct hda_bus *bus;
  488. int err;
  489. static struct snd_device_ops dev_ops = {
  490. .dev_register = snd_hda_bus_dev_register,
  491. .dev_free = snd_hda_bus_dev_free,
  492. };
  493. if (snd_BUG_ON(!temp))
  494. return -EINVAL;
  495. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  496. return -EINVAL;
  497. if (busp)
  498. *busp = NULL;
  499. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  500. if (bus == NULL) {
  501. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  502. return -ENOMEM;
  503. }
  504. bus->card = card;
  505. bus->private_data = temp->private_data;
  506. bus->pci = temp->pci;
  507. bus->modelname = temp->modelname;
  508. bus->power_save = temp->power_save;
  509. bus->ops = temp->ops;
  510. mutex_init(&bus->cmd_mutex);
  511. INIT_LIST_HEAD(&bus->codec_list);
  512. snprintf(bus->workq_name, sizeof(bus->workq_name),
  513. "hd-audio%d", card->number);
  514. bus->workq = create_singlethread_workqueue(bus->workq_name);
  515. if (!bus->workq) {
  516. snd_printk(KERN_ERR "cannot create workqueue %s\n",
  517. bus->workq_name);
  518. kfree(bus);
  519. return -ENOMEM;
  520. }
  521. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  522. if (err < 0) {
  523. snd_hda_bus_free(bus);
  524. return err;
  525. }
  526. if (busp)
  527. *busp = bus;
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_HDA(snd_hda_bus_new);
  531. #ifdef CONFIG_SND_HDA_GENERIC
  532. #define is_generic_config(codec) \
  533. (codec->modelname && !strcmp(codec->modelname, "generic"))
  534. #else
  535. #define is_generic_config(codec) 0
  536. #endif
  537. #ifdef MODULE
  538. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  539. #else
  540. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  541. #endif
  542. /*
  543. * find a matching codec preset
  544. */
  545. static const struct hda_codec_preset *
  546. find_codec_preset(struct hda_codec *codec)
  547. {
  548. struct hda_codec_preset_list *tbl;
  549. const struct hda_codec_preset *preset;
  550. int mod_requested = 0;
  551. if (is_generic_config(codec))
  552. return NULL; /* use the generic parser */
  553. again:
  554. mutex_lock(&preset_mutex);
  555. list_for_each_entry(tbl, &hda_preset_tables, list) {
  556. if (!try_module_get(tbl->owner)) {
  557. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  558. continue;
  559. }
  560. for (preset = tbl->preset; preset->id; preset++) {
  561. u32 mask = preset->mask;
  562. if (preset->afg && preset->afg != codec->afg)
  563. continue;
  564. if (preset->mfg && preset->mfg != codec->mfg)
  565. continue;
  566. if (!mask)
  567. mask = ~0;
  568. if (preset->id == (codec->vendor_id & mask) &&
  569. (!preset->rev ||
  570. preset->rev == codec->revision_id)) {
  571. mutex_unlock(&preset_mutex);
  572. codec->owner = tbl->owner;
  573. return preset;
  574. }
  575. }
  576. module_put(tbl->owner);
  577. }
  578. mutex_unlock(&preset_mutex);
  579. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  580. char name[32];
  581. if (!mod_requested)
  582. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  583. codec->vendor_id);
  584. else
  585. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  586. (codec->vendor_id >> 16) & 0xffff);
  587. request_module(name);
  588. mod_requested++;
  589. goto again;
  590. }
  591. return NULL;
  592. }
  593. /*
  594. * get_codec_name - store the codec name
  595. */
  596. static int get_codec_name(struct hda_codec *codec)
  597. {
  598. const struct hda_vendor_id *c;
  599. const char *vendor = NULL;
  600. u16 vendor_id = codec->vendor_id >> 16;
  601. char tmp[16];
  602. if (codec->vendor_name)
  603. goto get_chip_name;
  604. for (c = hda_vendor_ids; c->id; c++) {
  605. if (c->id == vendor_id) {
  606. vendor = c->name;
  607. break;
  608. }
  609. }
  610. if (!vendor) {
  611. sprintf(tmp, "Generic %04x", vendor_id);
  612. vendor = tmp;
  613. }
  614. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  615. if (!codec->vendor_name)
  616. return -ENOMEM;
  617. get_chip_name:
  618. if (codec->chip_name)
  619. return 0;
  620. if (codec->preset && codec->preset->name)
  621. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  622. else {
  623. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  624. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  625. }
  626. if (!codec->chip_name)
  627. return -ENOMEM;
  628. return 0;
  629. }
  630. /*
  631. * look for an AFG and MFG nodes
  632. */
  633. static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
  634. {
  635. int i, total_nodes, function_id;
  636. hda_nid_t nid;
  637. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  638. for (i = 0; i < total_nodes; i++, nid++) {
  639. function_id = snd_hda_param_read(codec, nid,
  640. AC_PAR_FUNCTION_TYPE) & 0xff;
  641. switch (function_id) {
  642. case AC_GRP_AUDIO_FUNCTION:
  643. codec->afg = nid;
  644. codec->function_id = function_id;
  645. break;
  646. case AC_GRP_MODEM_FUNCTION:
  647. codec->mfg = nid;
  648. codec->function_id = function_id;
  649. break;
  650. default:
  651. break;
  652. }
  653. }
  654. }
  655. /*
  656. * read widget caps for each widget and store in cache
  657. */
  658. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  659. {
  660. int i;
  661. hda_nid_t nid;
  662. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  663. &codec->start_nid);
  664. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  665. if (!codec->wcaps)
  666. return -ENOMEM;
  667. nid = codec->start_nid;
  668. for (i = 0; i < codec->num_nodes; i++, nid++)
  669. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  670. AC_PAR_AUDIO_WIDGET_CAP);
  671. return 0;
  672. }
  673. /* read all pin default configurations and save codec->init_pins */
  674. static int read_pin_defaults(struct hda_codec *codec)
  675. {
  676. int i;
  677. hda_nid_t nid = codec->start_nid;
  678. for (i = 0; i < codec->num_nodes; i++, nid++) {
  679. struct hda_pincfg *pin;
  680. unsigned int wcaps = get_wcaps(codec, nid);
  681. unsigned int wid_type = get_wcaps_type(wcaps);
  682. if (wid_type != AC_WID_PIN)
  683. continue;
  684. pin = snd_array_new(&codec->init_pins);
  685. if (!pin)
  686. return -ENOMEM;
  687. pin->nid = nid;
  688. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  689. AC_VERB_GET_CONFIG_DEFAULT, 0);
  690. }
  691. return 0;
  692. }
  693. /* look up the given pin config list and return the item matching with NID */
  694. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  695. struct snd_array *array,
  696. hda_nid_t nid)
  697. {
  698. int i;
  699. for (i = 0; i < array->used; i++) {
  700. struct hda_pincfg *pin = snd_array_elem(array, i);
  701. if (pin->nid == nid)
  702. return pin;
  703. }
  704. return NULL;
  705. }
  706. /* write a config value for the given NID */
  707. static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
  708. unsigned int cfg)
  709. {
  710. int i;
  711. for (i = 0; i < 4; i++) {
  712. snd_hda_codec_write(codec, nid, 0,
  713. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
  714. cfg & 0xff);
  715. cfg >>= 8;
  716. }
  717. }
  718. /* set the current pin config value for the given NID.
  719. * the value is cached, and read via snd_hda_codec_get_pincfg()
  720. */
  721. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  722. hda_nid_t nid, unsigned int cfg)
  723. {
  724. struct hda_pincfg *pin;
  725. unsigned int oldcfg;
  726. oldcfg = snd_hda_codec_get_pincfg(codec, nid);
  727. pin = look_up_pincfg(codec, list, nid);
  728. if (!pin) {
  729. pin = snd_array_new(list);
  730. if (!pin)
  731. return -ENOMEM;
  732. pin->nid = nid;
  733. }
  734. pin->cfg = cfg;
  735. /* change only when needed; e.g. if the pincfg is already present
  736. * in user_pins[], don't write it
  737. */
  738. cfg = snd_hda_codec_get_pincfg(codec, nid);
  739. if (oldcfg != cfg)
  740. set_pincfg(codec, nid, cfg);
  741. return 0;
  742. }
  743. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  744. hda_nid_t nid, unsigned int cfg)
  745. {
  746. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  747. }
  748. EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
  749. /* get the current pin config value of the given pin NID */
  750. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  751. {
  752. struct hda_pincfg *pin;
  753. #ifdef CONFIG_SND_HDA_HWDEP
  754. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  755. if (pin)
  756. return pin->cfg;
  757. #endif
  758. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  759. if (pin)
  760. return pin->cfg;
  761. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  762. if (pin)
  763. return pin->cfg;
  764. return 0;
  765. }
  766. EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
  767. /* restore all current pin configs */
  768. static void restore_pincfgs(struct hda_codec *codec)
  769. {
  770. int i;
  771. for (i = 0; i < codec->init_pins.used; i++) {
  772. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  773. set_pincfg(codec, pin->nid,
  774. snd_hda_codec_get_pincfg(codec, pin->nid));
  775. }
  776. }
  777. static void init_hda_cache(struct hda_cache_rec *cache,
  778. unsigned int record_size);
  779. static void free_hda_cache(struct hda_cache_rec *cache);
  780. /* restore the initial pin cfgs and release all pincfg lists */
  781. static void restore_init_pincfgs(struct hda_codec *codec)
  782. {
  783. /* first free driver_pins and user_pins, then call restore_pincfg
  784. * so that only the values in init_pins are restored
  785. */
  786. snd_array_free(&codec->driver_pins);
  787. #ifdef CONFIG_SND_HDA_HWDEP
  788. snd_array_free(&codec->user_pins);
  789. #endif
  790. restore_pincfgs(codec);
  791. snd_array_free(&codec->init_pins);
  792. }
  793. /*
  794. * codec destructor
  795. */
  796. static void snd_hda_codec_free(struct hda_codec *codec)
  797. {
  798. if (!codec)
  799. return;
  800. restore_init_pincfgs(codec);
  801. #ifdef CONFIG_SND_HDA_POWER_SAVE
  802. cancel_delayed_work(&codec->power_work);
  803. flush_workqueue(codec->bus->workq);
  804. #endif
  805. list_del(&codec->list);
  806. snd_array_free(&codec->mixers);
  807. codec->bus->caddr_tbl[codec->addr] = NULL;
  808. if (codec->patch_ops.free)
  809. codec->patch_ops.free(codec);
  810. module_put(codec->owner);
  811. free_hda_cache(&codec->amp_cache);
  812. free_hda_cache(&codec->cmd_cache);
  813. kfree(codec->vendor_name);
  814. kfree(codec->chip_name);
  815. kfree(codec->modelname);
  816. kfree(codec->wcaps);
  817. kfree(codec);
  818. }
  819. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  820. unsigned int power_state);
  821. /**
  822. * snd_hda_codec_new - create a HDA codec
  823. * @bus: the bus to assign
  824. * @codec_addr: the codec address
  825. * @codecp: the pointer to store the generated codec
  826. *
  827. * Returns 0 if successful, or a negative error code.
  828. */
  829. int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  830. struct hda_codec **codecp)
  831. {
  832. struct hda_codec *codec;
  833. char component[31];
  834. int err;
  835. if (snd_BUG_ON(!bus))
  836. return -EINVAL;
  837. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  838. return -EINVAL;
  839. if (bus->caddr_tbl[codec_addr]) {
  840. snd_printk(KERN_ERR "hda_codec: "
  841. "address 0x%x is already occupied\n", codec_addr);
  842. return -EBUSY;
  843. }
  844. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  845. if (codec == NULL) {
  846. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  847. return -ENOMEM;
  848. }
  849. codec->bus = bus;
  850. codec->addr = codec_addr;
  851. mutex_init(&codec->spdif_mutex);
  852. mutex_init(&codec->control_mutex);
  853. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  854. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  855. snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
  856. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  857. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  858. if (codec->bus->modelname) {
  859. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  860. if (!codec->modelname) {
  861. snd_hda_codec_free(codec);
  862. return -ENODEV;
  863. }
  864. }
  865. #ifdef CONFIG_SND_HDA_POWER_SAVE
  866. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  867. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  868. * the caller has to power down appropriatley after initialization
  869. * phase.
  870. */
  871. hda_keep_power_on(codec);
  872. #endif
  873. list_add_tail(&codec->list, &bus->codec_list);
  874. bus->caddr_tbl[codec_addr] = codec;
  875. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  876. AC_PAR_VENDOR_ID);
  877. if (codec->vendor_id == -1)
  878. /* read again, hopefully the access method was corrected
  879. * in the last read...
  880. */
  881. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  882. AC_PAR_VENDOR_ID);
  883. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  884. AC_PAR_SUBSYSTEM_ID);
  885. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  886. AC_PAR_REV_ID);
  887. setup_fg_nodes(codec);
  888. if (!codec->afg && !codec->mfg) {
  889. snd_printdd("hda_codec: no AFG or MFG node found\n");
  890. err = -ENODEV;
  891. goto error;
  892. }
  893. err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
  894. if (err < 0) {
  895. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  896. goto error;
  897. }
  898. err = read_pin_defaults(codec);
  899. if (err < 0)
  900. goto error;
  901. if (!codec->subsystem_id) {
  902. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  903. codec->subsystem_id =
  904. snd_hda_codec_read(codec, nid, 0,
  905. AC_VERB_GET_SUBSYSTEM_ID, 0);
  906. }
  907. /* power-up all before initialization */
  908. hda_set_power_state(codec,
  909. codec->afg ? codec->afg : codec->mfg,
  910. AC_PWRST_D0);
  911. snd_hda_codec_proc_new(codec);
  912. snd_hda_create_hwdep(codec);
  913. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  914. codec->subsystem_id, codec->revision_id);
  915. snd_component_add(codec->bus->card, component);
  916. if (codecp)
  917. *codecp = codec;
  918. return 0;
  919. error:
  920. snd_hda_codec_free(codec);
  921. return err;
  922. }
  923. EXPORT_SYMBOL_HDA(snd_hda_codec_new);
  924. int snd_hda_codec_configure(struct hda_codec *codec)
  925. {
  926. int err;
  927. codec->preset = find_codec_preset(codec);
  928. if (!codec->vendor_name || !codec->chip_name) {
  929. err = get_codec_name(codec);
  930. if (err < 0)
  931. return err;
  932. }
  933. /* audio codec should override the mixer name */
  934. if (codec->afg || !*codec->bus->card->mixername)
  935. snprintf(codec->bus->card->mixername,
  936. sizeof(codec->bus->card->mixername),
  937. "%s %s", codec->vendor_name, codec->chip_name);
  938. if (is_generic_config(codec)) {
  939. err = snd_hda_parse_generic_codec(codec);
  940. goto patched;
  941. }
  942. if (codec->preset && codec->preset->patch) {
  943. err = codec->preset->patch(codec);
  944. goto patched;
  945. }
  946. /* call the default parser */
  947. err = snd_hda_parse_generic_codec(codec);
  948. if (err < 0)
  949. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  950. patched:
  951. if (!err && codec->patch_ops.unsol_event)
  952. err = init_unsol_queue(codec->bus);
  953. return err;
  954. }
  955. EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
  956. /**
  957. * snd_hda_codec_setup_stream - set up the codec for streaming
  958. * @codec: the CODEC to set up
  959. * @nid: the NID to set up
  960. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  961. * @channel_id: channel id to pass, zero based.
  962. * @format: stream format.
  963. */
  964. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  965. u32 stream_tag,
  966. int channel_id, int format)
  967. {
  968. if (!nid)
  969. return;
  970. snd_printdd("hda_codec_setup_stream: "
  971. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  972. nid, stream_tag, channel_id, format);
  973. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  974. (stream_tag << 4) | channel_id);
  975. msleep(1);
  976. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  977. }
  978. EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
  979. void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  980. {
  981. if (!nid)
  982. return;
  983. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  984. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  985. #if 0 /* keep the format */
  986. msleep(1);
  987. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  988. #endif
  989. }
  990. EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
  991. /*
  992. * amp access functions
  993. */
  994. /* FIXME: more better hash key? */
  995. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  996. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  997. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  998. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  999. #define INFO_AMP_CAPS (1<<0)
  1000. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1001. /* initialize the hash table */
  1002. static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
  1003. unsigned int record_size)
  1004. {
  1005. memset(cache, 0, sizeof(*cache));
  1006. memset(cache->hash, 0xff, sizeof(cache->hash));
  1007. snd_array_init(&cache->buf, record_size, 64);
  1008. }
  1009. static void free_hda_cache(struct hda_cache_rec *cache)
  1010. {
  1011. snd_array_free(&cache->buf);
  1012. }
  1013. /* query the hash. allocate an entry if not found. */
  1014. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1015. u32 key)
  1016. {
  1017. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1018. u16 cur = cache->hash[idx];
  1019. struct hda_cache_head *info;
  1020. while (cur != 0xffff) {
  1021. info = snd_array_elem(&cache->buf, cur);
  1022. if (info->key == key)
  1023. return info;
  1024. cur = info->next;
  1025. }
  1026. /* add a new hash entry */
  1027. info = snd_array_new(&cache->buf);
  1028. if (!info)
  1029. return NULL;
  1030. cur = snd_array_index(&cache->buf, info);
  1031. info->key = key;
  1032. info->val = 0;
  1033. info->next = cache->hash[idx];
  1034. cache->hash[idx] = cur;
  1035. return info;
  1036. }
  1037. /* query and allocate an amp hash entry */
  1038. static inline struct hda_amp_info *
  1039. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1040. {
  1041. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1042. }
  1043. /*
  1044. * query AMP capabilities for the given widget and direction
  1045. */
  1046. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1047. {
  1048. struct hda_amp_info *info;
  1049. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  1050. if (!info)
  1051. return 0;
  1052. if (!(info->head.val & INFO_AMP_CAPS)) {
  1053. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1054. nid = codec->afg;
  1055. info->amp_caps = snd_hda_param_read(codec, nid,
  1056. direction == HDA_OUTPUT ?
  1057. AC_PAR_AMP_OUT_CAP :
  1058. AC_PAR_AMP_IN_CAP);
  1059. if (info->amp_caps)
  1060. info->head.val |= INFO_AMP_CAPS;
  1061. }
  1062. return info->amp_caps;
  1063. }
  1064. EXPORT_SYMBOL_HDA(query_amp_caps);
  1065. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1066. unsigned int caps)
  1067. {
  1068. struct hda_amp_info *info;
  1069. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  1070. if (!info)
  1071. return -EINVAL;
  1072. info->amp_caps = caps;
  1073. info->head.val |= INFO_AMP_CAPS;
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
  1077. static unsigned int
  1078. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
  1079. unsigned int (*func)(struct hda_codec *, hda_nid_t))
  1080. {
  1081. struct hda_amp_info *info;
  1082. info = get_alloc_amp_hash(codec, key);
  1083. if (!info)
  1084. return 0;
  1085. if (!info->head.val) {
  1086. info->head.val |= INFO_AMP_CAPS;
  1087. info->amp_caps = func(codec, nid);
  1088. }
  1089. return info->amp_caps;
  1090. }
  1091. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
  1092. {
  1093. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1094. }
  1095. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1096. {
  1097. return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
  1098. read_pin_cap);
  1099. }
  1100. EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
  1101. /*
  1102. * read the current volume to info
  1103. * if the cache exists, read the cache value.
  1104. */
  1105. static unsigned int get_vol_mute(struct hda_codec *codec,
  1106. struct hda_amp_info *info, hda_nid_t nid,
  1107. int ch, int direction, int index)
  1108. {
  1109. u32 val, parm;
  1110. if (info->head.val & INFO_AMP_VOL(ch))
  1111. return info->vol[ch];
  1112. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1113. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1114. parm |= index;
  1115. val = snd_hda_codec_read(codec, nid, 0,
  1116. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1117. info->vol[ch] = val & 0xff;
  1118. info->head.val |= INFO_AMP_VOL(ch);
  1119. return info->vol[ch];
  1120. }
  1121. /*
  1122. * write the current volume in info to the h/w and update the cache
  1123. */
  1124. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  1125. hda_nid_t nid, int ch, int direction, int index,
  1126. int val)
  1127. {
  1128. u32 parm;
  1129. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1130. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1131. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1132. parm |= val;
  1133. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1134. info->vol[ch] = val;
  1135. }
  1136. /*
  1137. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1138. */
  1139. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1140. int direction, int index)
  1141. {
  1142. struct hda_amp_info *info;
  1143. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1144. if (!info)
  1145. return 0;
  1146. return get_vol_mute(codec, info, nid, ch, direction, index);
  1147. }
  1148. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
  1149. /*
  1150. * update the AMP value, mask = bit mask to set, val = the value
  1151. */
  1152. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1153. int direction, int idx, int mask, int val)
  1154. {
  1155. struct hda_amp_info *info;
  1156. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  1157. if (!info)
  1158. return 0;
  1159. val &= mask;
  1160. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  1161. if (info->vol[ch] == val)
  1162. return 0;
  1163. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  1164. return 1;
  1165. }
  1166. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
  1167. /*
  1168. * update the AMP stereo with the same mask and value
  1169. */
  1170. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1171. int direction, int idx, int mask, int val)
  1172. {
  1173. int ch, ret = 0;
  1174. for (ch = 0; ch < 2; ch++)
  1175. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1176. idx, mask, val);
  1177. return ret;
  1178. }
  1179. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
  1180. #ifdef SND_HDA_NEEDS_RESUME
  1181. /* resume the all amp commands from the cache */
  1182. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  1183. {
  1184. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  1185. int i;
  1186. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  1187. u32 key = buffer->head.key;
  1188. hda_nid_t nid;
  1189. unsigned int idx, dir, ch;
  1190. if (!key)
  1191. continue;
  1192. nid = key & 0xff;
  1193. idx = (key >> 16) & 0xff;
  1194. dir = (key >> 24) & 0xff;
  1195. for (ch = 0; ch < 2; ch++) {
  1196. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  1197. continue;
  1198. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  1199. buffer->vol[ch]);
  1200. }
  1201. }
  1202. }
  1203. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
  1204. #endif /* SND_HDA_NEEDS_RESUME */
  1205. /* volume */
  1206. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1207. struct snd_ctl_elem_info *uinfo)
  1208. {
  1209. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1210. u16 nid = get_amp_nid(kcontrol);
  1211. u8 chs = get_amp_channels(kcontrol);
  1212. int dir = get_amp_direction(kcontrol);
  1213. unsigned int ofs = get_amp_offset(kcontrol);
  1214. u32 caps;
  1215. caps = query_amp_caps(codec, nid, dir);
  1216. /* num steps */
  1217. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1218. if (!caps) {
  1219. printk(KERN_WARNING "hda_codec: "
  1220. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  1221. kcontrol->id.name);
  1222. return -EINVAL;
  1223. }
  1224. if (ofs < caps)
  1225. caps -= ofs;
  1226. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1227. uinfo->count = chs == 3 ? 2 : 1;
  1228. uinfo->value.integer.min = 0;
  1229. uinfo->value.integer.max = caps;
  1230. return 0;
  1231. }
  1232. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
  1233. static inline unsigned int
  1234. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1235. int ch, int dir, int idx, unsigned int ofs)
  1236. {
  1237. unsigned int val;
  1238. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1239. val &= HDA_AMP_VOLMASK;
  1240. if (val >= ofs)
  1241. val -= ofs;
  1242. else
  1243. val = 0;
  1244. return val;
  1245. }
  1246. static inline int
  1247. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1248. int ch, int dir, int idx, unsigned int ofs,
  1249. unsigned int val)
  1250. {
  1251. if (val > 0)
  1252. val += ofs;
  1253. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1254. HDA_AMP_VOLMASK, val);
  1255. }
  1256. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1257. struct snd_ctl_elem_value *ucontrol)
  1258. {
  1259. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1260. hda_nid_t nid = get_amp_nid(kcontrol);
  1261. int chs = get_amp_channels(kcontrol);
  1262. int dir = get_amp_direction(kcontrol);
  1263. int idx = get_amp_index(kcontrol);
  1264. unsigned int ofs = get_amp_offset(kcontrol);
  1265. long *valp = ucontrol->value.integer.value;
  1266. if (chs & 1)
  1267. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  1268. if (chs & 2)
  1269. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  1270. return 0;
  1271. }
  1272. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
  1273. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1274. struct snd_ctl_elem_value *ucontrol)
  1275. {
  1276. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1277. hda_nid_t nid = get_amp_nid(kcontrol);
  1278. int chs = get_amp_channels(kcontrol);
  1279. int dir = get_amp_direction(kcontrol);
  1280. int idx = get_amp_index(kcontrol);
  1281. unsigned int ofs = get_amp_offset(kcontrol);
  1282. long *valp = ucontrol->value.integer.value;
  1283. int change = 0;
  1284. snd_hda_power_up(codec);
  1285. if (chs & 1) {
  1286. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  1287. valp++;
  1288. }
  1289. if (chs & 2)
  1290. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  1291. snd_hda_power_down(codec);
  1292. return change;
  1293. }
  1294. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
  1295. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1296. unsigned int size, unsigned int __user *_tlv)
  1297. {
  1298. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1299. hda_nid_t nid = get_amp_nid(kcontrol);
  1300. int dir = get_amp_direction(kcontrol);
  1301. unsigned int ofs = get_amp_offset(kcontrol);
  1302. u32 caps, val1, val2;
  1303. if (size < 4 * sizeof(unsigned int))
  1304. return -ENOMEM;
  1305. caps = query_amp_caps(codec, nid, dir);
  1306. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1307. val2 = (val2 + 1) * 25;
  1308. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1309. val1 += ofs;
  1310. val1 = ((int)val1) * ((int)val2);
  1311. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1312. return -EFAULT;
  1313. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1314. return -EFAULT;
  1315. if (put_user(val1, _tlv + 2))
  1316. return -EFAULT;
  1317. if (put_user(val2, _tlv + 3))
  1318. return -EFAULT;
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
  1322. /*
  1323. * set (static) TLV for virtual master volume; recalculated as max 0dB
  1324. */
  1325. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1326. unsigned int *tlv)
  1327. {
  1328. u32 caps;
  1329. int nums, step;
  1330. caps = query_amp_caps(codec, nid, dir);
  1331. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1332. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1333. step = (step + 1) * 25;
  1334. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1335. tlv[1] = 2 * sizeof(unsigned int);
  1336. tlv[2] = -nums * step;
  1337. tlv[3] = step;
  1338. }
  1339. EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
  1340. /* find a mixer control element with the given name */
  1341. static struct snd_kcontrol *
  1342. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1343. const char *name, int idx)
  1344. {
  1345. struct snd_ctl_elem_id id;
  1346. memset(&id, 0, sizeof(id));
  1347. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1348. id.index = idx;
  1349. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  1350. return NULL;
  1351. strcpy(id.name, name);
  1352. return snd_ctl_find_id(codec->bus->card, &id);
  1353. }
  1354. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1355. const char *name)
  1356. {
  1357. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1358. }
  1359. EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
  1360. /* Add a control element and assign to the codec */
  1361. int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
  1362. {
  1363. int err;
  1364. struct snd_kcontrol **knewp;
  1365. err = snd_ctl_add(codec->bus->card, kctl);
  1366. if (err < 0)
  1367. return err;
  1368. knewp = snd_array_new(&codec->mixers);
  1369. if (!knewp)
  1370. return -ENOMEM;
  1371. *knewp = kctl;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
  1375. /* Clear all controls assigned to the given codec */
  1376. void snd_hda_ctls_clear(struct hda_codec *codec)
  1377. {
  1378. int i;
  1379. struct snd_kcontrol **kctls = codec->mixers.list;
  1380. for (i = 0; i < codec->mixers.used; i++)
  1381. snd_ctl_remove(codec->bus->card, kctls[i]);
  1382. snd_array_free(&codec->mixers);
  1383. }
  1384. /* pseudo device locking
  1385. * toggle card->shutdown to allow/disallow the device access (as a hack)
  1386. */
  1387. static int hda_lock_devices(struct snd_card *card)
  1388. {
  1389. spin_lock(&card->files_lock);
  1390. if (card->shutdown) {
  1391. spin_unlock(&card->files_lock);
  1392. return -EINVAL;
  1393. }
  1394. card->shutdown = 1;
  1395. spin_unlock(&card->files_lock);
  1396. return 0;
  1397. }
  1398. static void hda_unlock_devices(struct snd_card *card)
  1399. {
  1400. spin_lock(&card->files_lock);
  1401. card->shutdown = 0;
  1402. spin_unlock(&card->files_lock);
  1403. }
  1404. int snd_hda_codec_reset(struct hda_codec *codec)
  1405. {
  1406. struct snd_card *card = codec->bus->card;
  1407. int i, pcm;
  1408. if (hda_lock_devices(card) < 0)
  1409. return -EBUSY;
  1410. /* check whether the codec isn't used by any mixer or PCM streams */
  1411. if (!list_empty(&card->ctl_files)) {
  1412. hda_unlock_devices(card);
  1413. return -EBUSY;
  1414. }
  1415. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1416. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  1417. if (!cpcm->pcm)
  1418. continue;
  1419. if (cpcm->pcm->streams[0].substream_opened ||
  1420. cpcm->pcm->streams[1].substream_opened) {
  1421. hda_unlock_devices(card);
  1422. return -EBUSY;
  1423. }
  1424. }
  1425. /* OK, let it free */
  1426. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1427. cancel_delayed_work(&codec->power_work);
  1428. flush_workqueue(codec->bus->workq);
  1429. #endif
  1430. snd_hda_ctls_clear(codec);
  1431. /* relase PCMs */
  1432. for (i = 0; i < codec->num_pcms; i++) {
  1433. if (codec->pcm_info[i].pcm) {
  1434. snd_device_free(card, codec->pcm_info[i].pcm);
  1435. clear_bit(codec->pcm_info[i].device,
  1436. codec->bus->pcm_dev_bits);
  1437. }
  1438. }
  1439. if (codec->patch_ops.free)
  1440. codec->patch_ops.free(codec);
  1441. codec->proc_widget_hook = NULL;
  1442. codec->spec = NULL;
  1443. free_hda_cache(&codec->amp_cache);
  1444. free_hda_cache(&codec->cmd_cache);
  1445. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1446. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1447. /* free only driver_pins so that init_pins + user_pins are restored */
  1448. snd_array_free(&codec->driver_pins);
  1449. restore_pincfgs(codec);
  1450. codec->num_pcms = 0;
  1451. codec->pcm_info = NULL;
  1452. codec->preset = NULL;
  1453. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  1454. codec->slave_dig_outs = NULL;
  1455. codec->spdif_status_reset = 0;
  1456. module_put(codec->owner);
  1457. codec->owner = NULL;
  1458. /* allow device access again */
  1459. hda_unlock_devices(card);
  1460. return 0;
  1461. }
  1462. /* create a virtual master control and add slaves */
  1463. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1464. unsigned int *tlv, const char **slaves)
  1465. {
  1466. struct snd_kcontrol *kctl;
  1467. const char **s;
  1468. int err;
  1469. for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
  1470. ;
  1471. if (!*s) {
  1472. snd_printdd("No slave found for %s\n", name);
  1473. return 0;
  1474. }
  1475. kctl = snd_ctl_make_virtual_master(name, tlv);
  1476. if (!kctl)
  1477. return -ENOMEM;
  1478. err = snd_hda_ctl_add(codec, kctl);
  1479. if (err < 0)
  1480. return err;
  1481. for (s = slaves; *s; s++) {
  1482. struct snd_kcontrol *sctl;
  1483. int i = 0;
  1484. for (;;) {
  1485. sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
  1486. if (!sctl) {
  1487. if (!i)
  1488. snd_printdd("Cannot find slave %s, "
  1489. "skipped\n", *s);
  1490. break;
  1491. }
  1492. err = snd_ctl_add_slave(kctl, sctl);
  1493. if (err < 0)
  1494. return err;
  1495. i++;
  1496. }
  1497. }
  1498. return 0;
  1499. }
  1500. EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
  1501. /* switch */
  1502. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1503. struct snd_ctl_elem_info *uinfo)
  1504. {
  1505. int chs = get_amp_channels(kcontrol);
  1506. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1507. uinfo->count = chs == 3 ? 2 : 1;
  1508. uinfo->value.integer.min = 0;
  1509. uinfo->value.integer.max = 1;
  1510. return 0;
  1511. }
  1512. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
  1513. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1514. struct snd_ctl_elem_value *ucontrol)
  1515. {
  1516. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1517. hda_nid_t nid = get_amp_nid(kcontrol);
  1518. int chs = get_amp_channels(kcontrol);
  1519. int dir = get_amp_direction(kcontrol);
  1520. int idx = get_amp_index(kcontrol);
  1521. long *valp = ucontrol->value.integer.value;
  1522. if (chs & 1)
  1523. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1524. HDA_AMP_MUTE) ? 0 : 1;
  1525. if (chs & 2)
  1526. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1527. HDA_AMP_MUTE) ? 0 : 1;
  1528. return 0;
  1529. }
  1530. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
  1531. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1532. struct snd_ctl_elem_value *ucontrol)
  1533. {
  1534. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1535. hda_nid_t nid = get_amp_nid(kcontrol);
  1536. int chs = get_amp_channels(kcontrol);
  1537. int dir = get_amp_direction(kcontrol);
  1538. int idx = get_amp_index(kcontrol);
  1539. long *valp = ucontrol->value.integer.value;
  1540. int change = 0;
  1541. snd_hda_power_up(codec);
  1542. if (chs & 1) {
  1543. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1544. HDA_AMP_MUTE,
  1545. *valp ? 0 : HDA_AMP_MUTE);
  1546. valp++;
  1547. }
  1548. if (chs & 2)
  1549. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1550. HDA_AMP_MUTE,
  1551. *valp ? 0 : HDA_AMP_MUTE);
  1552. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1553. if (codec->patch_ops.check_power_status)
  1554. codec->patch_ops.check_power_status(codec, nid);
  1555. #endif
  1556. snd_hda_power_down(codec);
  1557. return change;
  1558. }
  1559. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
  1560. /*
  1561. * bound volume controls
  1562. *
  1563. * bind multiple volumes (# indices, from 0)
  1564. */
  1565. #define AMP_VAL_IDX_SHIFT 19
  1566. #define AMP_VAL_IDX_MASK (0x0f<<19)
  1567. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  1568. struct snd_ctl_elem_value *ucontrol)
  1569. {
  1570. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1571. unsigned long pval;
  1572. int err;
  1573. mutex_lock(&codec->control_mutex);
  1574. pval = kcontrol->private_value;
  1575. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  1576. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  1577. kcontrol->private_value = pval;
  1578. mutex_unlock(&codec->control_mutex);
  1579. return err;
  1580. }
  1581. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
  1582. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  1583. struct snd_ctl_elem_value *ucontrol)
  1584. {
  1585. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1586. unsigned long pval;
  1587. int i, indices, err = 0, change = 0;
  1588. mutex_lock(&codec->control_mutex);
  1589. pval = kcontrol->private_value;
  1590. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  1591. for (i = 0; i < indices; i++) {
  1592. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  1593. (i << AMP_VAL_IDX_SHIFT);
  1594. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  1595. if (err < 0)
  1596. break;
  1597. change |= err;
  1598. }
  1599. kcontrol->private_value = pval;
  1600. mutex_unlock(&codec->control_mutex);
  1601. return err < 0 ? err : change;
  1602. }
  1603. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
  1604. /*
  1605. * generic bound volume/swtich controls
  1606. */
  1607. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  1608. struct snd_ctl_elem_info *uinfo)
  1609. {
  1610. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1611. struct hda_bind_ctls *c;
  1612. int err;
  1613. mutex_lock(&codec->control_mutex);
  1614. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1615. kcontrol->private_value = *c->values;
  1616. err = c->ops->info(kcontrol, uinfo);
  1617. kcontrol->private_value = (long)c;
  1618. mutex_unlock(&codec->control_mutex);
  1619. return err;
  1620. }
  1621. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
  1622. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  1623. struct snd_ctl_elem_value *ucontrol)
  1624. {
  1625. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1626. struct hda_bind_ctls *c;
  1627. int err;
  1628. mutex_lock(&codec->control_mutex);
  1629. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1630. kcontrol->private_value = *c->values;
  1631. err = c->ops->get(kcontrol, ucontrol);
  1632. kcontrol->private_value = (long)c;
  1633. mutex_unlock(&codec->control_mutex);
  1634. return err;
  1635. }
  1636. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
  1637. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  1638. struct snd_ctl_elem_value *ucontrol)
  1639. {
  1640. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1641. struct hda_bind_ctls *c;
  1642. unsigned long *vals;
  1643. int err = 0, change = 0;
  1644. mutex_lock(&codec->control_mutex);
  1645. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1646. for (vals = c->values; *vals; vals++) {
  1647. kcontrol->private_value = *vals;
  1648. err = c->ops->put(kcontrol, ucontrol);
  1649. if (err < 0)
  1650. break;
  1651. change |= err;
  1652. }
  1653. kcontrol->private_value = (long)c;
  1654. mutex_unlock(&codec->control_mutex);
  1655. return err < 0 ? err : change;
  1656. }
  1657. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
  1658. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1659. unsigned int size, unsigned int __user *tlv)
  1660. {
  1661. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1662. struct hda_bind_ctls *c;
  1663. int err;
  1664. mutex_lock(&codec->control_mutex);
  1665. c = (struct hda_bind_ctls *)kcontrol->private_value;
  1666. kcontrol->private_value = *c->values;
  1667. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  1668. kcontrol->private_value = (long)c;
  1669. mutex_unlock(&codec->control_mutex);
  1670. return err;
  1671. }
  1672. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
  1673. struct hda_ctl_ops snd_hda_bind_vol = {
  1674. .info = snd_hda_mixer_amp_volume_info,
  1675. .get = snd_hda_mixer_amp_volume_get,
  1676. .put = snd_hda_mixer_amp_volume_put,
  1677. .tlv = snd_hda_mixer_amp_tlv
  1678. };
  1679. EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
  1680. struct hda_ctl_ops snd_hda_bind_sw = {
  1681. .info = snd_hda_mixer_amp_switch_info,
  1682. .get = snd_hda_mixer_amp_switch_get,
  1683. .put = snd_hda_mixer_amp_switch_put,
  1684. .tlv = snd_hda_mixer_amp_tlv
  1685. };
  1686. EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
  1687. /*
  1688. * SPDIF out controls
  1689. */
  1690. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1691. struct snd_ctl_elem_info *uinfo)
  1692. {
  1693. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1694. uinfo->count = 1;
  1695. return 0;
  1696. }
  1697. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1698. struct snd_ctl_elem_value *ucontrol)
  1699. {
  1700. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1701. IEC958_AES0_NONAUDIO |
  1702. IEC958_AES0_CON_EMPHASIS_5015 |
  1703. IEC958_AES0_CON_NOT_COPYRIGHT;
  1704. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1705. IEC958_AES1_CON_ORIGINAL;
  1706. return 0;
  1707. }
  1708. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1709. struct snd_ctl_elem_value *ucontrol)
  1710. {
  1711. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1712. IEC958_AES0_NONAUDIO |
  1713. IEC958_AES0_PRO_EMPHASIS_5015;
  1714. return 0;
  1715. }
  1716. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1717. struct snd_ctl_elem_value *ucontrol)
  1718. {
  1719. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1720. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  1721. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  1722. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  1723. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  1724. return 0;
  1725. }
  1726. /* convert from SPDIF status bits to HDA SPDIF bits
  1727. * bit 0 (DigEn) is always set zero (to be filled later)
  1728. */
  1729. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1730. {
  1731. unsigned short val = 0;
  1732. if (sbits & IEC958_AES0_PROFESSIONAL)
  1733. val |= AC_DIG1_PROFESSIONAL;
  1734. if (sbits & IEC958_AES0_NONAUDIO)
  1735. val |= AC_DIG1_NONAUDIO;
  1736. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1737. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1738. IEC958_AES0_PRO_EMPHASIS_5015)
  1739. val |= AC_DIG1_EMPHASIS;
  1740. } else {
  1741. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1742. IEC958_AES0_CON_EMPHASIS_5015)
  1743. val |= AC_DIG1_EMPHASIS;
  1744. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1745. val |= AC_DIG1_COPYRIGHT;
  1746. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1747. val |= AC_DIG1_LEVEL;
  1748. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1749. }
  1750. return val;
  1751. }
  1752. /* convert to SPDIF status bits from HDA SPDIF bits
  1753. */
  1754. static unsigned int convert_to_spdif_status(unsigned short val)
  1755. {
  1756. unsigned int sbits = 0;
  1757. if (val & AC_DIG1_NONAUDIO)
  1758. sbits |= IEC958_AES0_NONAUDIO;
  1759. if (val & AC_DIG1_PROFESSIONAL)
  1760. sbits |= IEC958_AES0_PROFESSIONAL;
  1761. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1762. if (sbits & AC_DIG1_EMPHASIS)
  1763. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1764. } else {
  1765. if (val & AC_DIG1_EMPHASIS)
  1766. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1767. if (!(val & AC_DIG1_COPYRIGHT))
  1768. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1769. if (val & AC_DIG1_LEVEL)
  1770. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1771. sbits |= val & (0x7f << 8);
  1772. }
  1773. return sbits;
  1774. }
  1775. /* set digital convert verbs both for the given NID and its slaves */
  1776. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  1777. int verb, int val)
  1778. {
  1779. hda_nid_t *d;
  1780. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  1781. d = codec->slave_dig_outs;
  1782. if (!d)
  1783. return;
  1784. for (; *d; d++)
  1785. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  1786. }
  1787. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  1788. int dig1, int dig2)
  1789. {
  1790. if (dig1 != -1)
  1791. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  1792. if (dig2 != -1)
  1793. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  1794. }
  1795. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  1796. struct snd_ctl_elem_value *ucontrol)
  1797. {
  1798. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1799. hda_nid_t nid = kcontrol->private_value;
  1800. unsigned short val;
  1801. int change;
  1802. mutex_lock(&codec->spdif_mutex);
  1803. codec->spdif_status = ucontrol->value.iec958.status[0] |
  1804. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  1805. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  1806. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  1807. val = convert_from_spdif_status(codec->spdif_status);
  1808. val |= codec->spdif_ctls & 1;
  1809. change = codec->spdif_ctls != val;
  1810. codec->spdif_ctls = val;
  1811. if (change)
  1812. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  1813. mutex_unlock(&codec->spdif_mutex);
  1814. return change;
  1815. }
  1816. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  1817. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  1818. struct snd_ctl_elem_value *ucontrol)
  1819. {
  1820. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1821. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  1822. return 0;
  1823. }
  1824. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  1825. struct snd_ctl_elem_value *ucontrol)
  1826. {
  1827. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1828. hda_nid_t nid = kcontrol->private_value;
  1829. unsigned short val;
  1830. int change;
  1831. mutex_lock(&codec->spdif_mutex);
  1832. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  1833. if (ucontrol->value.integer.value[0])
  1834. val |= AC_DIG1_ENABLE;
  1835. change = codec->spdif_ctls != val;
  1836. if (change) {
  1837. codec->spdif_ctls = val;
  1838. set_dig_out_convert(codec, nid, val & 0xff, -1);
  1839. /* unmute amp switch (if any) */
  1840. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  1841. (val & AC_DIG1_ENABLE))
  1842. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  1843. HDA_AMP_MUTE, 0);
  1844. }
  1845. mutex_unlock(&codec->spdif_mutex);
  1846. return change;
  1847. }
  1848. static struct snd_kcontrol_new dig_mixes[] = {
  1849. {
  1850. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1851. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1852. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  1853. .info = snd_hda_spdif_mask_info,
  1854. .get = snd_hda_spdif_cmask_get,
  1855. },
  1856. {
  1857. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1858. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1859. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1860. .info = snd_hda_spdif_mask_info,
  1861. .get = snd_hda_spdif_pmask_get,
  1862. },
  1863. {
  1864. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1865. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1866. .info = snd_hda_spdif_mask_info,
  1867. .get = snd_hda_spdif_default_get,
  1868. .put = snd_hda_spdif_default_put,
  1869. },
  1870. {
  1871. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1872. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1873. .info = snd_hda_spdif_out_switch_info,
  1874. .get = snd_hda_spdif_out_switch_get,
  1875. .put = snd_hda_spdif_out_switch_put,
  1876. },
  1877. { } /* end */
  1878. };
  1879. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  1880. /**
  1881. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1882. * @codec: the HDA codec
  1883. * @nid: audio out widget NID
  1884. *
  1885. * Creates controls related with the SPDIF output.
  1886. * Called from each patch supporting the SPDIF out.
  1887. *
  1888. * Returns 0 if successful, or a negative error code.
  1889. */
  1890. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1891. {
  1892. int err;
  1893. struct snd_kcontrol *kctl;
  1894. struct snd_kcontrol_new *dig_mix;
  1895. int idx;
  1896. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  1897. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  1898. idx))
  1899. break;
  1900. }
  1901. if (idx >= SPDIF_MAX_IDX) {
  1902. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  1903. return -EBUSY;
  1904. }
  1905. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1906. kctl = snd_ctl_new1(dig_mix, codec);
  1907. if (!kctl)
  1908. return -ENOMEM;
  1909. kctl->id.index = idx;
  1910. kctl->private_value = nid;
  1911. err = snd_hda_ctl_add(codec, kctl);
  1912. if (err < 0)
  1913. return err;
  1914. }
  1915. codec->spdif_ctls =
  1916. snd_hda_codec_read(codec, nid, 0,
  1917. AC_VERB_GET_DIGI_CONVERT_1, 0);
  1918. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1919. return 0;
  1920. }
  1921. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
  1922. /*
  1923. * SPDIF sharing with analog output
  1924. */
  1925. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  1926. struct snd_ctl_elem_value *ucontrol)
  1927. {
  1928. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1929. ucontrol->value.integer.value[0] = mout->share_spdif;
  1930. return 0;
  1931. }
  1932. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  1933. struct snd_ctl_elem_value *ucontrol)
  1934. {
  1935. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  1936. mout->share_spdif = !!ucontrol->value.integer.value[0];
  1937. return 0;
  1938. }
  1939. static struct snd_kcontrol_new spdif_share_sw = {
  1940. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1941. .name = "IEC958 Default PCM Playback Switch",
  1942. .info = snd_ctl_boolean_mono_info,
  1943. .get = spdif_share_sw_get,
  1944. .put = spdif_share_sw_put,
  1945. };
  1946. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  1947. struct hda_multi_out *mout)
  1948. {
  1949. if (!mout->dig_out_nid)
  1950. return 0;
  1951. /* ATTENTION: here mout is passed as private_data, instead of codec */
  1952. return snd_hda_ctl_add(codec,
  1953. snd_ctl_new1(&spdif_share_sw, mout));
  1954. }
  1955. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
  1956. /*
  1957. * SPDIF input
  1958. */
  1959. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1960. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  1961. struct snd_ctl_elem_value *ucontrol)
  1962. {
  1963. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1964. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1965. return 0;
  1966. }
  1967. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  1968. struct snd_ctl_elem_value *ucontrol)
  1969. {
  1970. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1971. hda_nid_t nid = kcontrol->private_value;
  1972. unsigned int val = !!ucontrol->value.integer.value[0];
  1973. int change;
  1974. mutex_lock(&codec->spdif_mutex);
  1975. change = codec->spdif_in_enable != val;
  1976. if (change) {
  1977. codec->spdif_in_enable = val;
  1978. snd_hda_codec_write_cache(codec, nid, 0,
  1979. AC_VERB_SET_DIGI_CONVERT_1, val);
  1980. }
  1981. mutex_unlock(&codec->spdif_mutex);
  1982. return change;
  1983. }
  1984. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  1985. struct snd_ctl_elem_value *ucontrol)
  1986. {
  1987. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1988. hda_nid_t nid = kcontrol->private_value;
  1989. unsigned short val;
  1990. unsigned int sbits;
  1991. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  1992. sbits = convert_to_spdif_status(val);
  1993. ucontrol->value.iec958.status[0] = sbits;
  1994. ucontrol->value.iec958.status[1] = sbits >> 8;
  1995. ucontrol->value.iec958.status[2] = sbits >> 16;
  1996. ucontrol->value.iec958.status[3] = sbits >> 24;
  1997. return 0;
  1998. }
  1999. static struct snd_kcontrol_new dig_in_ctls[] = {
  2000. {
  2001. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2002. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  2003. .info = snd_hda_spdif_in_switch_info,
  2004. .get = snd_hda_spdif_in_switch_get,
  2005. .put = snd_hda_spdif_in_switch_put,
  2006. },
  2007. {
  2008. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2009. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2010. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  2011. .info = snd_hda_spdif_mask_info,
  2012. .get = snd_hda_spdif_in_status_get,
  2013. },
  2014. { } /* end */
  2015. };
  2016. /**
  2017. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  2018. * @codec: the HDA codec
  2019. * @nid: audio in widget NID
  2020. *
  2021. * Creates controls related with the SPDIF input.
  2022. * Called from each patch supporting the SPDIF in.
  2023. *
  2024. * Returns 0 if successful, or a negative error code.
  2025. */
  2026. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  2027. {
  2028. int err;
  2029. struct snd_kcontrol *kctl;
  2030. struct snd_kcontrol_new *dig_mix;
  2031. int idx;
  2032. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  2033. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  2034. idx))
  2035. break;
  2036. }
  2037. if (idx >= SPDIF_MAX_IDX) {
  2038. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  2039. return -EBUSY;
  2040. }
  2041. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  2042. kctl = snd_ctl_new1(dig_mix, codec);
  2043. if (!kctl)
  2044. return -ENOMEM;
  2045. kctl->private_value = nid;
  2046. err = snd_hda_ctl_add(codec, kctl);
  2047. if (err < 0)
  2048. return err;
  2049. }
  2050. codec->spdif_in_enable =
  2051. snd_hda_codec_read(codec, nid, 0,
  2052. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  2053. AC_DIG1_ENABLE;
  2054. return 0;
  2055. }
  2056. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
  2057. #ifdef SND_HDA_NEEDS_RESUME
  2058. /*
  2059. * command cache
  2060. */
  2061. /* build a 32bit cache key with the widget id and the command parameter */
  2062. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  2063. #define get_cmd_cache_nid(key) ((key) & 0xff)
  2064. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  2065. /**
  2066. * snd_hda_codec_write_cache - send a single command with caching
  2067. * @codec: the HDA codec
  2068. * @nid: NID to send the command
  2069. * @direct: direct flag
  2070. * @verb: the verb to send
  2071. * @parm: the parameter for the verb
  2072. *
  2073. * Send a single command without waiting for response.
  2074. *
  2075. * Returns 0 if successful, or a negative error code.
  2076. */
  2077. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  2078. int direct, unsigned int verb, unsigned int parm)
  2079. {
  2080. int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
  2081. struct hda_cache_head *c;
  2082. u32 key;
  2083. if (err < 0)
  2084. return err;
  2085. /* parm may contain the verb stuff for get/set amp */
  2086. verb = verb | (parm >> 8);
  2087. parm &= 0xff;
  2088. key = build_cmd_cache_key(nid, verb);
  2089. mutex_lock(&codec->bus->cmd_mutex);
  2090. c = get_alloc_hash(&codec->cmd_cache, key);
  2091. if (c)
  2092. c->val = parm;
  2093. mutex_unlock(&codec->bus->cmd_mutex);
  2094. return 0;
  2095. }
  2096. EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
  2097. /* resume the all commands from the cache */
  2098. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  2099. {
  2100. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  2101. int i;
  2102. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  2103. u32 key = buffer->key;
  2104. if (!key)
  2105. continue;
  2106. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  2107. get_cmd_cache_cmd(key), buffer->val);
  2108. }
  2109. }
  2110. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
  2111. /**
  2112. * snd_hda_sequence_write_cache - sequence writes with caching
  2113. * @codec: the HDA codec
  2114. * @seq: VERB array to send
  2115. *
  2116. * Send the commands sequentially from the given array.
  2117. * Thte commands are recorded on cache for power-save and resume.
  2118. * The array must be terminated with NID=0.
  2119. */
  2120. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  2121. const struct hda_verb *seq)
  2122. {
  2123. for (; seq->nid; seq++)
  2124. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  2125. seq->param);
  2126. }
  2127. EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
  2128. #endif /* SND_HDA_NEEDS_RESUME */
  2129. /*
  2130. * set power state of the codec
  2131. */
  2132. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2133. unsigned int power_state)
  2134. {
  2135. hda_nid_t nid;
  2136. int i;
  2137. /* this delay seems necessary to avoid click noise at power-down */
  2138. if (power_state == AC_PWRST_D3)
  2139. msleep(100);
  2140. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  2141. power_state);
  2142. /* partial workaround for "azx_get_response timeout" */
  2143. if (power_state == AC_PWRST_D0)
  2144. msleep(10);
  2145. nid = codec->start_nid;
  2146. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2147. unsigned int wcaps = get_wcaps(codec, nid);
  2148. if (wcaps & AC_WCAP_POWER) {
  2149. unsigned int wid_type = get_wcaps_type(wcaps);
  2150. if (power_state == AC_PWRST_D3 &&
  2151. wid_type == AC_WID_PIN) {
  2152. unsigned int pincap;
  2153. /*
  2154. * don't power down the widget if it controls
  2155. * eapd and EAPD_BTLENABLE is set.
  2156. */
  2157. pincap = snd_hda_query_pin_caps(codec, nid);
  2158. if (pincap & AC_PINCAP_EAPD) {
  2159. int eapd = snd_hda_codec_read(codec,
  2160. nid, 0,
  2161. AC_VERB_GET_EAPD_BTLENABLE, 0);
  2162. eapd &= 0x02;
  2163. if (eapd)
  2164. continue;
  2165. }
  2166. }
  2167. snd_hda_codec_write(codec, nid, 0,
  2168. AC_VERB_SET_POWER_STATE,
  2169. power_state);
  2170. }
  2171. }
  2172. if (power_state == AC_PWRST_D0) {
  2173. unsigned long end_time;
  2174. int state;
  2175. msleep(10);
  2176. /* wait until the codec reachs to D0 */
  2177. end_time = jiffies + msecs_to_jiffies(500);
  2178. do {
  2179. state = snd_hda_codec_read(codec, fg, 0,
  2180. AC_VERB_GET_POWER_STATE, 0);
  2181. if (state == power_state)
  2182. break;
  2183. msleep(1);
  2184. } while (time_after_eq(end_time, jiffies));
  2185. }
  2186. }
  2187. #ifdef CONFIG_SND_HDA_HWDEP
  2188. /* execute additional init verbs */
  2189. static void hda_exec_init_verbs(struct hda_codec *codec)
  2190. {
  2191. if (codec->init_verbs.list)
  2192. snd_hda_sequence_write(codec, codec->init_verbs.list);
  2193. }
  2194. #else
  2195. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  2196. #endif
  2197. #ifdef SND_HDA_NEEDS_RESUME
  2198. /*
  2199. * call suspend and power-down; used both from PM and power-save
  2200. */
  2201. static void hda_call_codec_suspend(struct hda_codec *codec)
  2202. {
  2203. if (codec->patch_ops.suspend)
  2204. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  2205. hda_set_power_state(codec,
  2206. codec->afg ? codec->afg : codec->mfg,
  2207. AC_PWRST_D3);
  2208. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2209. cancel_delayed_work(&codec->power_work);
  2210. codec->power_on = 0;
  2211. codec->power_transition = 0;
  2212. #endif
  2213. }
  2214. /*
  2215. * kick up codec; used both from PM and power-save
  2216. */
  2217. static void hda_call_codec_resume(struct hda_codec *codec)
  2218. {
  2219. hda_set_power_state(codec,
  2220. codec->afg ? codec->afg : codec->mfg,
  2221. AC_PWRST_D0);
  2222. restore_pincfgs(codec); /* restore all current pin configs */
  2223. hda_exec_init_verbs(codec);
  2224. if (codec->patch_ops.resume)
  2225. codec->patch_ops.resume(codec);
  2226. else {
  2227. if (codec->patch_ops.init)
  2228. codec->patch_ops.init(codec);
  2229. snd_hda_codec_resume_amp(codec);
  2230. snd_hda_codec_resume_cache(codec);
  2231. }
  2232. }
  2233. #endif /* SND_HDA_NEEDS_RESUME */
  2234. /**
  2235. * snd_hda_build_controls - build mixer controls
  2236. * @bus: the BUS
  2237. *
  2238. * Creates mixer controls for each codec included in the bus.
  2239. *
  2240. * Returns 0 if successful, otherwise a negative error code.
  2241. */
  2242. int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
  2243. {
  2244. struct hda_codec *codec;
  2245. list_for_each_entry(codec, &bus->codec_list, list) {
  2246. int err = snd_hda_codec_build_controls(codec);
  2247. if (err < 0) {
  2248. printk(KERN_ERR "hda_codec: cannot build controls"
  2249. "for #%d (error %d)\n", codec->addr, err);
  2250. err = snd_hda_codec_reset(codec);
  2251. if (err < 0) {
  2252. printk(KERN_ERR
  2253. "hda_codec: cannot revert codec\n");
  2254. return err;
  2255. }
  2256. }
  2257. }
  2258. return 0;
  2259. }
  2260. EXPORT_SYMBOL_HDA(snd_hda_build_controls);
  2261. int snd_hda_codec_build_controls(struct hda_codec *codec)
  2262. {
  2263. int err = 0;
  2264. hda_exec_init_verbs(codec);
  2265. /* continue to initialize... */
  2266. if (codec->patch_ops.init)
  2267. err = codec->patch_ops.init(codec);
  2268. if (!err && codec->patch_ops.build_controls)
  2269. err = codec->patch_ops.build_controls(codec);
  2270. if (err < 0)
  2271. return err;
  2272. return 0;
  2273. }
  2274. /*
  2275. * stream formats
  2276. */
  2277. struct hda_rate_tbl {
  2278. unsigned int hz;
  2279. unsigned int alsa_bits;
  2280. unsigned int hda_fmt;
  2281. };
  2282. static struct hda_rate_tbl rate_bits[] = {
  2283. /* rate in Hz, ALSA rate bitmask, HDA format value */
  2284. /* autodetected value used in snd_hda_query_supported_pcm */
  2285. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  2286. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  2287. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  2288. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  2289. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  2290. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  2291. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  2292. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  2293. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  2294. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  2295. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  2296. #define AC_PAR_PCM_RATE_BITS 11
  2297. /* up to bits 10, 384kHZ isn't supported properly */
  2298. /* not autodetected value */
  2299. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  2300. { 0 } /* terminator */
  2301. };
  2302. /**
  2303. * snd_hda_calc_stream_format - calculate format bitset
  2304. * @rate: the sample rate
  2305. * @channels: the number of channels
  2306. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  2307. * @maxbps: the max. bps
  2308. *
  2309. * Calculate the format bitset from the given rate, channels and th PCM format.
  2310. *
  2311. * Return zero if invalid.
  2312. */
  2313. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  2314. unsigned int channels,
  2315. unsigned int format,
  2316. unsigned int maxbps)
  2317. {
  2318. int i;
  2319. unsigned int val = 0;
  2320. for (i = 0; rate_bits[i].hz; i++)
  2321. if (rate_bits[i].hz == rate) {
  2322. val = rate_bits[i].hda_fmt;
  2323. break;
  2324. }
  2325. if (!rate_bits[i].hz) {
  2326. snd_printdd("invalid rate %d\n", rate);
  2327. return 0;
  2328. }
  2329. if (channels == 0 || channels > 8) {
  2330. snd_printdd("invalid channels %d\n", channels);
  2331. return 0;
  2332. }
  2333. val |= channels - 1;
  2334. switch (snd_pcm_format_width(format)) {
  2335. case 8: val |= 0x00; break;
  2336. case 16: val |= 0x10; break;
  2337. case 20:
  2338. case 24:
  2339. case 32:
  2340. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  2341. val |= 0x40;
  2342. else if (maxbps >= 24)
  2343. val |= 0x30;
  2344. else
  2345. val |= 0x20;
  2346. break;
  2347. default:
  2348. snd_printdd("invalid format width %d\n",
  2349. snd_pcm_format_width(format));
  2350. return 0;
  2351. }
  2352. return val;
  2353. }
  2354. EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
  2355. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  2356. {
  2357. unsigned int val = 0;
  2358. if (nid != codec->afg &&
  2359. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  2360. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2361. if (!val || val == -1)
  2362. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2363. if (!val || val == -1)
  2364. return 0;
  2365. return val;
  2366. }
  2367. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  2368. {
  2369. return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
  2370. get_pcm_param);
  2371. }
  2372. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
  2373. {
  2374. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2375. if (!streams || streams == -1)
  2376. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  2377. if (!streams || streams == -1)
  2378. return 0;
  2379. return streams;
  2380. }
  2381. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  2382. {
  2383. return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
  2384. get_stream_param);
  2385. }
  2386. /**
  2387. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  2388. * @codec: the HDA codec
  2389. * @nid: NID to query
  2390. * @ratesp: the pointer to store the detected rate bitflags
  2391. * @formatsp: the pointer to store the detected formats
  2392. * @bpsp: the pointer to store the detected format widths
  2393. *
  2394. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  2395. * or @bsps argument is ignored.
  2396. *
  2397. * Returns 0 if successful, otherwise a negative error code.
  2398. */
  2399. static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  2400. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  2401. {
  2402. unsigned int i, val, wcaps;
  2403. wcaps = get_wcaps(codec, nid);
  2404. val = query_pcm_param(codec, nid);
  2405. if (ratesp) {
  2406. u32 rates = 0;
  2407. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  2408. if (val & (1 << i))
  2409. rates |= rate_bits[i].alsa_bits;
  2410. }
  2411. if (rates == 0) {
  2412. snd_printk(KERN_ERR "hda_codec: rates == 0 "
  2413. "(nid=0x%x, val=0x%x, ovrd=%i)\n",
  2414. nid, val,
  2415. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  2416. return -EIO;
  2417. }
  2418. *ratesp = rates;
  2419. }
  2420. if (formatsp || bpsp) {
  2421. u64 formats = 0;
  2422. unsigned int streams, bps;
  2423. streams = query_stream_param(codec, nid);
  2424. if (!streams)
  2425. return -EIO;
  2426. bps = 0;
  2427. if (streams & AC_SUPFMT_PCM) {
  2428. if (val & AC_SUPPCM_BITS_8) {
  2429. formats |= SNDRV_PCM_FMTBIT_U8;
  2430. bps = 8;
  2431. }
  2432. if (val & AC_SUPPCM_BITS_16) {
  2433. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  2434. bps = 16;
  2435. }
  2436. if (wcaps & AC_WCAP_DIGITAL) {
  2437. if (val & AC_SUPPCM_BITS_32)
  2438. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  2439. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  2440. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2441. if (val & AC_SUPPCM_BITS_24)
  2442. bps = 24;
  2443. else if (val & AC_SUPPCM_BITS_20)
  2444. bps = 20;
  2445. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  2446. AC_SUPPCM_BITS_32)) {
  2447. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  2448. if (val & AC_SUPPCM_BITS_32)
  2449. bps = 32;
  2450. else if (val & AC_SUPPCM_BITS_24)
  2451. bps = 24;
  2452. else if (val & AC_SUPPCM_BITS_20)
  2453. bps = 20;
  2454. }
  2455. }
  2456. if (streams & AC_SUPFMT_FLOAT32) {
  2457. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  2458. if (!bps)
  2459. bps = 32;
  2460. }
  2461. if (streams == AC_SUPFMT_AC3) {
  2462. /* should be exclusive */
  2463. /* temporary hack: we have still no proper support
  2464. * for the direct AC3 stream...
  2465. */
  2466. formats |= SNDRV_PCM_FMTBIT_U8;
  2467. bps = 8;
  2468. }
  2469. if (formats == 0) {
  2470. snd_printk(KERN_ERR "hda_codec: formats == 0 "
  2471. "(nid=0x%x, val=0x%x, ovrd=%i, "
  2472. "streams=0x%x)\n",
  2473. nid, val,
  2474. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  2475. streams);
  2476. return -EIO;
  2477. }
  2478. if (formatsp)
  2479. *formatsp = formats;
  2480. if (bpsp)
  2481. *bpsp = bps;
  2482. }
  2483. return 0;
  2484. }
  2485. /**
  2486. * snd_hda_is_supported_format - check whether the given node supports
  2487. * the format val
  2488. *
  2489. * Returns 1 if supported, 0 if not.
  2490. */
  2491. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  2492. unsigned int format)
  2493. {
  2494. int i;
  2495. unsigned int val = 0, rate, stream;
  2496. val = query_pcm_param(codec, nid);
  2497. if (!val)
  2498. return 0;
  2499. rate = format & 0xff00;
  2500. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  2501. if (rate_bits[i].hda_fmt == rate) {
  2502. if (val & (1 << i))
  2503. break;
  2504. return 0;
  2505. }
  2506. if (i >= AC_PAR_PCM_RATE_BITS)
  2507. return 0;
  2508. stream = query_stream_param(codec, nid);
  2509. if (!stream)
  2510. return 0;
  2511. if (stream & AC_SUPFMT_PCM) {
  2512. switch (format & 0xf0) {
  2513. case 0x00:
  2514. if (!(val & AC_SUPPCM_BITS_8))
  2515. return 0;
  2516. break;
  2517. case 0x10:
  2518. if (!(val & AC_SUPPCM_BITS_16))
  2519. return 0;
  2520. break;
  2521. case 0x20:
  2522. if (!(val & AC_SUPPCM_BITS_20))
  2523. return 0;
  2524. break;
  2525. case 0x30:
  2526. if (!(val & AC_SUPPCM_BITS_24))
  2527. return 0;
  2528. break;
  2529. case 0x40:
  2530. if (!(val & AC_SUPPCM_BITS_32))
  2531. return 0;
  2532. break;
  2533. default:
  2534. return 0;
  2535. }
  2536. } else {
  2537. /* FIXME: check for float32 and AC3? */
  2538. }
  2539. return 1;
  2540. }
  2541. EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
  2542. /*
  2543. * PCM stuff
  2544. */
  2545. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2546. struct hda_codec *codec,
  2547. struct snd_pcm_substream *substream)
  2548. {
  2549. return 0;
  2550. }
  2551. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2552. struct hda_codec *codec,
  2553. unsigned int stream_tag,
  2554. unsigned int format,
  2555. struct snd_pcm_substream *substream)
  2556. {
  2557. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2558. return 0;
  2559. }
  2560. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2561. struct hda_codec *codec,
  2562. struct snd_pcm_substream *substream)
  2563. {
  2564. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2565. return 0;
  2566. }
  2567. static int set_pcm_default_values(struct hda_codec *codec,
  2568. struct hda_pcm_stream *info)
  2569. {
  2570. int err;
  2571. /* query support PCM information from the given NID */
  2572. if (info->nid && (!info->rates || !info->formats)) {
  2573. err = snd_hda_query_supported_pcm(codec, info->nid,
  2574. info->rates ? NULL : &info->rates,
  2575. info->formats ? NULL : &info->formats,
  2576. info->maxbps ? NULL : &info->maxbps);
  2577. if (err < 0)
  2578. return err;
  2579. }
  2580. if (info->ops.open == NULL)
  2581. info->ops.open = hda_pcm_default_open_close;
  2582. if (info->ops.close == NULL)
  2583. info->ops.close = hda_pcm_default_open_close;
  2584. if (info->ops.prepare == NULL) {
  2585. if (snd_BUG_ON(!info->nid))
  2586. return -EINVAL;
  2587. info->ops.prepare = hda_pcm_default_prepare;
  2588. }
  2589. if (info->ops.cleanup == NULL) {
  2590. if (snd_BUG_ON(!info->nid))
  2591. return -EINVAL;
  2592. info->ops.cleanup = hda_pcm_default_cleanup;
  2593. }
  2594. return 0;
  2595. }
  2596. /*
  2597. * get the empty PCM device number to assign
  2598. */
  2599. static int get_empty_pcm_device(struct hda_bus *bus, int type)
  2600. {
  2601. static const char *dev_name[HDA_PCM_NTYPES] = {
  2602. "Audio", "SPDIF", "HDMI", "Modem"
  2603. };
  2604. /* starting device index for each PCM type */
  2605. static int dev_idx[HDA_PCM_NTYPES] = {
  2606. [HDA_PCM_TYPE_AUDIO] = 0,
  2607. [HDA_PCM_TYPE_SPDIF] = 1,
  2608. [HDA_PCM_TYPE_HDMI] = 3,
  2609. [HDA_PCM_TYPE_MODEM] = 6
  2610. };
  2611. /* normal audio device indices; not linear to keep compatibility */
  2612. static int audio_idx[4] = { 0, 2, 4, 5 };
  2613. int i, dev;
  2614. switch (type) {
  2615. case HDA_PCM_TYPE_AUDIO:
  2616. for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
  2617. dev = audio_idx[i];
  2618. if (!test_bit(dev, bus->pcm_dev_bits))
  2619. goto ok;
  2620. }
  2621. snd_printk(KERN_WARNING "Too many audio devices\n");
  2622. return -EAGAIN;
  2623. case HDA_PCM_TYPE_SPDIF:
  2624. case HDA_PCM_TYPE_HDMI:
  2625. case HDA_PCM_TYPE_MODEM:
  2626. dev = dev_idx[type];
  2627. if (test_bit(dev, bus->pcm_dev_bits)) {
  2628. snd_printk(KERN_WARNING "%s already defined\n",
  2629. dev_name[type]);
  2630. return -EAGAIN;
  2631. }
  2632. break;
  2633. default:
  2634. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  2635. return -EINVAL;
  2636. }
  2637. ok:
  2638. set_bit(dev, bus->pcm_dev_bits);
  2639. return dev;
  2640. }
  2641. /*
  2642. * attach a new PCM stream
  2643. */
  2644. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  2645. {
  2646. struct hda_bus *bus = codec->bus;
  2647. struct hda_pcm_stream *info;
  2648. int stream, err;
  2649. if (snd_BUG_ON(!pcm->name))
  2650. return -EINVAL;
  2651. for (stream = 0; stream < 2; stream++) {
  2652. info = &pcm->stream[stream];
  2653. if (info->substreams) {
  2654. err = set_pcm_default_values(codec, info);
  2655. if (err < 0)
  2656. return err;
  2657. }
  2658. }
  2659. return bus->ops.attach_pcm(bus, codec, pcm);
  2660. }
  2661. /* assign all PCMs of the given codec */
  2662. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  2663. {
  2664. unsigned int pcm;
  2665. int err;
  2666. if (!codec->num_pcms) {
  2667. if (!codec->patch_ops.build_pcms)
  2668. return 0;
  2669. err = codec->patch_ops.build_pcms(codec);
  2670. if (err < 0) {
  2671. printk(KERN_ERR "hda_codec: cannot build PCMs"
  2672. "for #%d (error %d)\n", codec->addr, err);
  2673. err = snd_hda_codec_reset(codec);
  2674. if (err < 0) {
  2675. printk(KERN_ERR
  2676. "hda_codec: cannot revert codec\n");
  2677. return err;
  2678. }
  2679. }
  2680. }
  2681. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2682. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2683. int dev;
  2684. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  2685. continue; /* no substreams assigned */
  2686. if (!cpcm->pcm) {
  2687. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  2688. if (dev < 0)
  2689. continue; /* no fatal error */
  2690. cpcm->device = dev;
  2691. err = snd_hda_attach_pcm(codec, cpcm);
  2692. if (err < 0) {
  2693. printk(KERN_ERR "hda_codec: cannot attach "
  2694. "PCM stream %d for codec #%d\n",
  2695. dev, codec->addr);
  2696. continue; /* no fatal error */
  2697. }
  2698. }
  2699. }
  2700. return 0;
  2701. }
  2702. /**
  2703. * snd_hda_build_pcms - build PCM information
  2704. * @bus: the BUS
  2705. *
  2706. * Create PCM information for each codec included in the bus.
  2707. *
  2708. * The build_pcms codec patch is requested to set up codec->num_pcms and
  2709. * codec->pcm_info properly. The array is referred by the top-level driver
  2710. * to create its PCM instances.
  2711. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  2712. * callback.
  2713. *
  2714. * At least, substreams, channels_min and channels_max must be filled for
  2715. * each stream. substreams = 0 indicates that the stream doesn't exist.
  2716. * When rates and/or formats are zero, the supported values are queried
  2717. * from the given nid. The nid is used also by the default ops.prepare
  2718. * and ops.cleanup callbacks.
  2719. *
  2720. * The driver needs to call ops.open in its open callback. Similarly,
  2721. * ops.close is supposed to be called in the close callback.
  2722. * ops.prepare should be called in the prepare or hw_params callback
  2723. * with the proper parameters for set up.
  2724. * ops.cleanup should be called in hw_free for clean up of streams.
  2725. *
  2726. * This function returns 0 if successfull, or a negative error code.
  2727. */
  2728. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  2729. {
  2730. struct hda_codec *codec;
  2731. list_for_each_entry(codec, &bus->codec_list, list) {
  2732. int err = snd_hda_codec_build_pcms(codec);
  2733. if (err < 0)
  2734. return err;
  2735. }
  2736. return 0;
  2737. }
  2738. EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
  2739. /**
  2740. * snd_hda_check_board_config - compare the current codec with the config table
  2741. * @codec: the HDA codec
  2742. * @num_configs: number of config enums
  2743. * @models: array of model name strings
  2744. * @tbl: configuration table, terminated by null entries
  2745. *
  2746. * Compares the modelname or PCI subsystem id of the current codec with the
  2747. * given configuration table. If a matching entry is found, returns its
  2748. * config value (supposed to be 0 or positive).
  2749. *
  2750. * If no entries are matching, the function returns a negative value.
  2751. */
  2752. int snd_hda_check_board_config(struct hda_codec *codec,
  2753. int num_configs, const char **models,
  2754. const struct snd_pci_quirk *tbl)
  2755. {
  2756. if (codec->modelname && models) {
  2757. int i;
  2758. for (i = 0; i < num_configs; i++) {
  2759. if (models[i] &&
  2760. !strcmp(codec->modelname, models[i])) {
  2761. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  2762. "selected\n", models[i]);
  2763. return i;
  2764. }
  2765. }
  2766. }
  2767. if (!codec->bus->pci || !tbl)
  2768. return -1;
  2769. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  2770. if (!tbl)
  2771. return -1;
  2772. if (tbl->value >= 0 && tbl->value < num_configs) {
  2773. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2774. char tmp[10];
  2775. const char *model = NULL;
  2776. if (models)
  2777. model = models[tbl->value];
  2778. if (!model) {
  2779. sprintf(tmp, "#%d", tbl->value);
  2780. model = tmp;
  2781. }
  2782. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2783. "for config %x:%x (%s)\n",
  2784. model, tbl->subvendor, tbl->subdevice,
  2785. (tbl->name ? tbl->name : "Unknown device"));
  2786. #endif
  2787. return tbl->value;
  2788. }
  2789. return -1;
  2790. }
  2791. EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
  2792. /**
  2793. * snd_hda_check_board_codec_sid_config - compare the current codec
  2794. subsystem ID with the
  2795. config table
  2796. This is important for Gateway notebooks with SB450 HDA Audio
  2797. where the vendor ID of the PCI device is:
  2798. ATI Technologies Inc SB450 HDA Audio [1002:437b]
  2799. and the vendor/subvendor are found only at the codec.
  2800. * @codec: the HDA codec
  2801. * @num_configs: number of config enums
  2802. * @models: array of model name strings
  2803. * @tbl: configuration table, terminated by null entries
  2804. *
  2805. * Compares the modelname or PCI subsystem id of the current codec with the
  2806. * given configuration table. If a matching entry is found, returns its
  2807. * config value (supposed to be 0 or positive).
  2808. *
  2809. * If no entries are matching, the function returns a negative value.
  2810. */
  2811. int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
  2812. int num_configs, const char **models,
  2813. const struct snd_pci_quirk *tbl)
  2814. {
  2815. const struct snd_pci_quirk *q;
  2816. /* Search for codec ID */
  2817. for (q = tbl; q->subvendor; q++) {
  2818. unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
  2819. if (vendorid == codec->subsystem_id)
  2820. break;
  2821. }
  2822. if (!q->subvendor)
  2823. return -1;
  2824. tbl = q;
  2825. if (tbl->value >= 0 && tbl->value < num_configs) {
  2826. #ifdef CONFIG_SND_DEBUG_VERBOSE
  2827. char tmp[10];
  2828. const char *model = NULL;
  2829. if (models)
  2830. model = models[tbl->value];
  2831. if (!model) {
  2832. sprintf(tmp, "#%d", tbl->value);
  2833. model = tmp;
  2834. }
  2835. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  2836. "for config %x:%x (%s)\n",
  2837. model, tbl->subvendor, tbl->subdevice,
  2838. (tbl->name ? tbl->name : "Unknown device"));
  2839. #endif
  2840. return tbl->value;
  2841. }
  2842. return -1;
  2843. }
  2844. EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
  2845. /**
  2846. * snd_hda_add_new_ctls - create controls from the array
  2847. * @codec: the HDA codec
  2848. * @knew: the array of struct snd_kcontrol_new
  2849. *
  2850. * This helper function creates and add new controls in the given array.
  2851. * The array must be terminated with an empty entry as terminator.
  2852. *
  2853. * Returns 0 if successful, or a negative error code.
  2854. */
  2855. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2856. {
  2857. int err;
  2858. for (; knew->name; knew++) {
  2859. struct snd_kcontrol *kctl;
  2860. kctl = snd_ctl_new1(knew, codec);
  2861. if (!kctl)
  2862. return -ENOMEM;
  2863. err = snd_hda_ctl_add(codec, kctl);
  2864. if (err < 0) {
  2865. if (!codec->addr)
  2866. return err;
  2867. kctl = snd_ctl_new1(knew, codec);
  2868. if (!kctl)
  2869. return -ENOMEM;
  2870. kctl->id.device = codec->addr;
  2871. err = snd_hda_ctl_add(codec, kctl);
  2872. if (err < 0)
  2873. return err;
  2874. }
  2875. }
  2876. return 0;
  2877. }
  2878. EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
  2879. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2880. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2881. unsigned int power_state);
  2882. static void hda_power_work(struct work_struct *work)
  2883. {
  2884. struct hda_codec *codec =
  2885. container_of(work, struct hda_codec, power_work.work);
  2886. struct hda_bus *bus = codec->bus;
  2887. if (!codec->power_on || codec->power_count) {
  2888. codec->power_transition = 0;
  2889. return;
  2890. }
  2891. hda_call_codec_suspend(codec);
  2892. if (bus->ops.pm_notify)
  2893. bus->ops.pm_notify(bus);
  2894. }
  2895. static void hda_keep_power_on(struct hda_codec *codec)
  2896. {
  2897. codec->power_count++;
  2898. codec->power_on = 1;
  2899. }
  2900. void snd_hda_power_up(struct hda_codec *codec)
  2901. {
  2902. struct hda_bus *bus = codec->bus;
  2903. codec->power_count++;
  2904. if (codec->power_on || codec->power_transition)
  2905. return;
  2906. codec->power_on = 1;
  2907. if (bus->ops.pm_notify)
  2908. bus->ops.pm_notify(bus);
  2909. hda_call_codec_resume(codec);
  2910. cancel_delayed_work(&codec->power_work);
  2911. codec->power_transition = 0;
  2912. }
  2913. EXPORT_SYMBOL_HDA(snd_hda_power_up);
  2914. #define power_save(codec) \
  2915. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2916. #define power_save(codec) \
  2917. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  2918. void snd_hda_power_down(struct hda_codec *codec)
  2919. {
  2920. --codec->power_count;
  2921. if (!codec->power_on || codec->power_count || codec->power_transition)
  2922. return;
  2923. if (power_save(codec)) {
  2924. codec->power_transition = 1; /* avoid reentrance */
  2925. queue_delayed_work(codec->bus->workq, &codec->power_work,
  2926. msecs_to_jiffies(power_save(codec) * 1000));
  2927. }
  2928. }
  2929. EXPORT_SYMBOL_HDA(snd_hda_power_down);
  2930. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  2931. struct hda_loopback_check *check,
  2932. hda_nid_t nid)
  2933. {
  2934. struct hda_amp_list *p;
  2935. int ch, v;
  2936. if (!check->amplist)
  2937. return 0;
  2938. for (p = check->amplist; p->nid; p++) {
  2939. if (p->nid == nid)
  2940. break;
  2941. }
  2942. if (!p->nid)
  2943. return 0; /* nothing changed */
  2944. for (p = check->amplist; p->nid; p++) {
  2945. for (ch = 0; ch < 2; ch++) {
  2946. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  2947. p->idx);
  2948. if (!(v & HDA_AMP_MUTE) && v > 0) {
  2949. if (!check->power_on) {
  2950. check->power_on = 1;
  2951. snd_hda_power_up(codec);
  2952. }
  2953. return 1;
  2954. }
  2955. }
  2956. }
  2957. if (check->power_on) {
  2958. check->power_on = 0;
  2959. snd_hda_power_down(codec);
  2960. }
  2961. return 0;
  2962. }
  2963. EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
  2964. #endif
  2965. /*
  2966. * Channel mode helper
  2967. */
  2968. int snd_hda_ch_mode_info(struct hda_codec *codec,
  2969. struct snd_ctl_elem_info *uinfo,
  2970. const struct hda_channel_mode *chmode,
  2971. int num_chmodes)
  2972. {
  2973. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2974. uinfo->count = 1;
  2975. uinfo->value.enumerated.items = num_chmodes;
  2976. if (uinfo->value.enumerated.item >= num_chmodes)
  2977. uinfo->value.enumerated.item = num_chmodes - 1;
  2978. sprintf(uinfo->value.enumerated.name, "%dch",
  2979. chmode[uinfo->value.enumerated.item].channels);
  2980. return 0;
  2981. }
  2982. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
  2983. int snd_hda_ch_mode_get(struct hda_codec *codec,
  2984. struct snd_ctl_elem_value *ucontrol,
  2985. const struct hda_channel_mode *chmode,
  2986. int num_chmodes,
  2987. int max_channels)
  2988. {
  2989. int i;
  2990. for (i = 0; i < num_chmodes; i++) {
  2991. if (max_channels == chmode[i].channels) {
  2992. ucontrol->value.enumerated.item[0] = i;
  2993. break;
  2994. }
  2995. }
  2996. return 0;
  2997. }
  2998. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
  2999. int snd_hda_ch_mode_put(struct hda_codec *codec,
  3000. struct snd_ctl_elem_value *ucontrol,
  3001. const struct hda_channel_mode *chmode,
  3002. int num_chmodes,
  3003. int *max_channelsp)
  3004. {
  3005. unsigned int mode;
  3006. mode = ucontrol->value.enumerated.item[0];
  3007. if (mode >= num_chmodes)
  3008. return -EINVAL;
  3009. if (*max_channelsp == chmode[mode].channels)
  3010. return 0;
  3011. /* change the current channel setting */
  3012. *max_channelsp = chmode[mode].channels;
  3013. if (chmode[mode].sequence)
  3014. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  3015. return 1;
  3016. }
  3017. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
  3018. /*
  3019. * input MUX helper
  3020. */
  3021. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  3022. struct snd_ctl_elem_info *uinfo)
  3023. {
  3024. unsigned int index;
  3025. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3026. uinfo->count = 1;
  3027. uinfo->value.enumerated.items = imux->num_items;
  3028. if (!imux->num_items)
  3029. return 0;
  3030. index = uinfo->value.enumerated.item;
  3031. if (index >= imux->num_items)
  3032. index = imux->num_items - 1;
  3033. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  3034. return 0;
  3035. }
  3036. EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
  3037. int snd_hda_input_mux_put(struct hda_codec *codec,
  3038. const struct hda_input_mux *imux,
  3039. struct snd_ctl_elem_value *ucontrol,
  3040. hda_nid_t nid,
  3041. unsigned int *cur_val)
  3042. {
  3043. unsigned int idx;
  3044. if (!imux->num_items)
  3045. return 0;
  3046. idx = ucontrol->value.enumerated.item[0];
  3047. if (idx >= imux->num_items)
  3048. idx = imux->num_items - 1;
  3049. if (*cur_val == idx)
  3050. return 0;
  3051. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  3052. imux->items[idx].index);
  3053. *cur_val = idx;
  3054. return 1;
  3055. }
  3056. EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
  3057. /*
  3058. * Multi-channel / digital-out PCM helper functions
  3059. */
  3060. /* setup SPDIF output stream */
  3061. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  3062. unsigned int stream_tag, unsigned int format)
  3063. {
  3064. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  3065. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  3066. set_dig_out_convert(codec, nid,
  3067. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
  3068. -1);
  3069. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  3070. if (codec->slave_dig_outs) {
  3071. hda_nid_t *d;
  3072. for (d = codec->slave_dig_outs; *d; d++)
  3073. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  3074. format);
  3075. }
  3076. /* turn on again (if needed) */
  3077. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  3078. set_dig_out_convert(codec, nid,
  3079. codec->spdif_ctls & 0xff, -1);
  3080. }
  3081. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  3082. {
  3083. snd_hda_codec_cleanup_stream(codec, nid);
  3084. if (codec->slave_dig_outs) {
  3085. hda_nid_t *d;
  3086. for (d = codec->slave_dig_outs; *d; d++)
  3087. snd_hda_codec_cleanup_stream(codec, *d);
  3088. }
  3089. }
  3090. /*
  3091. * open the digital out in the exclusive mode
  3092. */
  3093. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  3094. struct hda_multi_out *mout)
  3095. {
  3096. mutex_lock(&codec->spdif_mutex);
  3097. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  3098. /* already opened as analog dup; reset it once */
  3099. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3100. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  3101. mutex_unlock(&codec->spdif_mutex);
  3102. return 0;
  3103. }
  3104. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
  3105. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  3106. struct hda_multi_out *mout,
  3107. unsigned int stream_tag,
  3108. unsigned int format,
  3109. struct snd_pcm_substream *substream)
  3110. {
  3111. mutex_lock(&codec->spdif_mutex);
  3112. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  3113. mutex_unlock(&codec->spdif_mutex);
  3114. return 0;
  3115. }
  3116. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
  3117. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  3118. struct hda_multi_out *mout)
  3119. {
  3120. mutex_lock(&codec->spdif_mutex);
  3121. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3122. mutex_unlock(&codec->spdif_mutex);
  3123. return 0;
  3124. }
  3125. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
  3126. /*
  3127. * release the digital out
  3128. */
  3129. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  3130. struct hda_multi_out *mout)
  3131. {
  3132. mutex_lock(&codec->spdif_mutex);
  3133. mout->dig_out_used = 0;
  3134. mutex_unlock(&codec->spdif_mutex);
  3135. return 0;
  3136. }
  3137. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
  3138. /*
  3139. * set up more restrictions for analog out
  3140. */
  3141. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  3142. struct hda_multi_out *mout,
  3143. struct snd_pcm_substream *substream,
  3144. struct hda_pcm_stream *hinfo)
  3145. {
  3146. struct snd_pcm_runtime *runtime = substream->runtime;
  3147. runtime->hw.channels_max = mout->max_channels;
  3148. if (mout->dig_out_nid) {
  3149. if (!mout->analog_rates) {
  3150. mout->analog_rates = hinfo->rates;
  3151. mout->analog_formats = hinfo->formats;
  3152. mout->analog_maxbps = hinfo->maxbps;
  3153. } else {
  3154. runtime->hw.rates = mout->analog_rates;
  3155. runtime->hw.formats = mout->analog_formats;
  3156. hinfo->maxbps = mout->analog_maxbps;
  3157. }
  3158. if (!mout->spdif_rates) {
  3159. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  3160. &mout->spdif_rates,
  3161. &mout->spdif_formats,
  3162. &mout->spdif_maxbps);
  3163. }
  3164. mutex_lock(&codec->spdif_mutex);
  3165. if (mout->share_spdif) {
  3166. if ((runtime->hw.rates & mout->spdif_rates) &&
  3167. (runtime->hw.formats & mout->spdif_formats)) {
  3168. runtime->hw.rates &= mout->spdif_rates;
  3169. runtime->hw.formats &= mout->spdif_formats;
  3170. if (mout->spdif_maxbps < hinfo->maxbps)
  3171. hinfo->maxbps = mout->spdif_maxbps;
  3172. } else {
  3173. mout->share_spdif = 0;
  3174. /* FIXME: need notify? */
  3175. }
  3176. }
  3177. mutex_unlock(&codec->spdif_mutex);
  3178. }
  3179. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  3180. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  3181. }
  3182. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
  3183. /*
  3184. * set up the i/o for analog out
  3185. * when the digital out is available, copy the front out to digital out, too.
  3186. */
  3187. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  3188. struct hda_multi_out *mout,
  3189. unsigned int stream_tag,
  3190. unsigned int format,
  3191. struct snd_pcm_substream *substream)
  3192. {
  3193. hda_nid_t *nids = mout->dac_nids;
  3194. int chs = substream->runtime->channels;
  3195. int i;
  3196. mutex_lock(&codec->spdif_mutex);
  3197. if (mout->dig_out_nid && mout->share_spdif &&
  3198. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  3199. if (chs == 2 &&
  3200. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  3201. format) &&
  3202. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  3203. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  3204. setup_dig_out_stream(codec, mout->dig_out_nid,
  3205. stream_tag, format);
  3206. } else {
  3207. mout->dig_out_used = 0;
  3208. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3209. }
  3210. }
  3211. mutex_unlock(&codec->spdif_mutex);
  3212. /* front */
  3213. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  3214. 0, format);
  3215. if (!mout->no_share_stream &&
  3216. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  3217. /* headphone out will just decode front left/right (stereo) */
  3218. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  3219. 0, format);
  3220. /* extra outputs copied from front */
  3221. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3222. if (!mout->no_share_stream && mout->extra_out_nid[i])
  3223. snd_hda_codec_setup_stream(codec,
  3224. mout->extra_out_nid[i],
  3225. stream_tag, 0, format);
  3226. /* surrounds */
  3227. for (i = 1; i < mout->num_dacs; i++) {
  3228. if (chs >= (i + 1) * 2) /* independent out */
  3229. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3230. i * 2, format);
  3231. else if (!mout->no_share_stream) /* copy front */
  3232. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3233. 0, format);
  3234. }
  3235. return 0;
  3236. }
  3237. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
  3238. /*
  3239. * clean up the setting for analog out
  3240. */
  3241. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  3242. struct hda_multi_out *mout)
  3243. {
  3244. hda_nid_t *nids = mout->dac_nids;
  3245. int i;
  3246. for (i = 0; i < mout->num_dacs; i++)
  3247. snd_hda_codec_cleanup_stream(codec, nids[i]);
  3248. if (mout->hp_nid)
  3249. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  3250. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3251. if (mout->extra_out_nid[i])
  3252. snd_hda_codec_cleanup_stream(codec,
  3253. mout->extra_out_nid[i]);
  3254. mutex_lock(&codec->spdif_mutex);
  3255. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  3256. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3257. mout->dig_out_used = 0;
  3258. }
  3259. mutex_unlock(&codec->spdif_mutex);
  3260. return 0;
  3261. }
  3262. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
  3263. /*
  3264. * Helper for automatic pin configuration
  3265. */
  3266. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  3267. {
  3268. for (; *list; list++)
  3269. if (*list == nid)
  3270. return 1;
  3271. return 0;
  3272. }
  3273. /*
  3274. * Sort an associated group of pins according to their sequence numbers.
  3275. */
  3276. static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
  3277. int num_pins)
  3278. {
  3279. int i, j;
  3280. short seq;
  3281. hda_nid_t nid;
  3282. for (i = 0; i < num_pins; i++) {
  3283. for (j = i + 1; j < num_pins; j++) {
  3284. if (sequences[i] > sequences[j]) {
  3285. seq = sequences[i];
  3286. sequences[i] = sequences[j];
  3287. sequences[j] = seq;
  3288. nid = pins[i];
  3289. pins[i] = pins[j];
  3290. pins[j] = nid;
  3291. }
  3292. }
  3293. }
  3294. }
  3295. /*
  3296. * Parse all pin widgets and store the useful pin nids to cfg
  3297. *
  3298. * The number of line-outs or any primary output is stored in line_outs,
  3299. * and the corresponding output pins are assigned to line_out_pins[],
  3300. * in the order of front, rear, CLFE, side, ...
  3301. *
  3302. * If more extra outputs (speaker and headphone) are found, the pins are
  3303. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  3304. * is detected, one of speaker of HP pins is assigned as the primary
  3305. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  3306. * if any analog output exists.
  3307. *
  3308. * The analog input pins are assigned to input_pins array.
  3309. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  3310. * respectively.
  3311. */
  3312. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  3313. struct auto_pin_cfg *cfg,
  3314. hda_nid_t *ignore_nids)
  3315. {
  3316. hda_nid_t nid, end_nid;
  3317. short seq, assoc_line_out, assoc_speaker;
  3318. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  3319. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  3320. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  3321. memset(cfg, 0, sizeof(*cfg));
  3322. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  3323. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  3324. memset(sequences_hp, 0, sizeof(sequences_hp));
  3325. assoc_line_out = assoc_speaker = 0;
  3326. end_nid = codec->start_nid + codec->num_nodes;
  3327. for (nid = codec->start_nid; nid < end_nid; nid++) {
  3328. unsigned int wid_caps = get_wcaps(codec, nid);
  3329. unsigned int wid_type = get_wcaps_type(wid_caps);
  3330. unsigned int def_conf;
  3331. short assoc, loc;
  3332. /* read all default configuration for pin complex */
  3333. if (wid_type != AC_WID_PIN)
  3334. continue;
  3335. /* ignore the given nids (e.g. pc-beep returns error) */
  3336. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  3337. continue;
  3338. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  3339. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  3340. continue;
  3341. loc = get_defcfg_location(def_conf);
  3342. switch (get_defcfg_device(def_conf)) {
  3343. case AC_JACK_LINE_OUT:
  3344. seq = get_defcfg_sequence(def_conf);
  3345. assoc = get_defcfg_association(def_conf);
  3346. if (!(wid_caps & AC_WCAP_STEREO))
  3347. if (!cfg->mono_out_pin)
  3348. cfg->mono_out_pin = nid;
  3349. if (!assoc)
  3350. continue;
  3351. if (!assoc_line_out)
  3352. assoc_line_out = assoc;
  3353. else if (assoc_line_out != assoc)
  3354. continue;
  3355. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  3356. continue;
  3357. cfg->line_out_pins[cfg->line_outs] = nid;
  3358. sequences_line_out[cfg->line_outs] = seq;
  3359. cfg->line_outs++;
  3360. break;
  3361. case AC_JACK_SPEAKER:
  3362. seq = get_defcfg_sequence(def_conf);
  3363. assoc = get_defcfg_association(def_conf);
  3364. if (! assoc)
  3365. continue;
  3366. if (! assoc_speaker)
  3367. assoc_speaker = assoc;
  3368. else if (assoc_speaker != assoc)
  3369. continue;
  3370. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  3371. continue;
  3372. cfg->speaker_pins[cfg->speaker_outs] = nid;
  3373. sequences_speaker[cfg->speaker_outs] = seq;
  3374. cfg->speaker_outs++;
  3375. break;
  3376. case AC_JACK_HP_OUT:
  3377. seq = get_defcfg_sequence(def_conf);
  3378. assoc = get_defcfg_association(def_conf);
  3379. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  3380. continue;
  3381. cfg->hp_pins[cfg->hp_outs] = nid;
  3382. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  3383. cfg->hp_outs++;
  3384. break;
  3385. case AC_JACK_MIC_IN: {
  3386. int preferred, alt;
  3387. if (loc == AC_JACK_LOC_FRONT) {
  3388. preferred = AUTO_PIN_FRONT_MIC;
  3389. alt = AUTO_PIN_MIC;
  3390. } else {
  3391. preferred = AUTO_PIN_MIC;
  3392. alt = AUTO_PIN_FRONT_MIC;
  3393. }
  3394. if (!cfg->input_pins[preferred])
  3395. cfg->input_pins[preferred] = nid;
  3396. else if (!cfg->input_pins[alt])
  3397. cfg->input_pins[alt] = nid;
  3398. break;
  3399. }
  3400. case AC_JACK_LINE_IN:
  3401. if (loc == AC_JACK_LOC_FRONT)
  3402. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  3403. else
  3404. cfg->input_pins[AUTO_PIN_LINE] = nid;
  3405. break;
  3406. case AC_JACK_CD:
  3407. cfg->input_pins[AUTO_PIN_CD] = nid;
  3408. break;
  3409. case AC_JACK_AUX:
  3410. cfg->input_pins[AUTO_PIN_AUX] = nid;
  3411. break;
  3412. case AC_JACK_SPDIF_OUT:
  3413. case AC_JACK_DIG_OTHER_OUT:
  3414. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
  3415. continue;
  3416. cfg->dig_out_pins[cfg->dig_outs] = nid;
  3417. cfg->dig_out_type[cfg->dig_outs] =
  3418. (loc == AC_JACK_LOC_HDMI) ?
  3419. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  3420. cfg->dig_outs++;
  3421. break;
  3422. case AC_JACK_SPDIF_IN:
  3423. case AC_JACK_DIG_OTHER_IN:
  3424. cfg->dig_in_pin = nid;
  3425. if (loc == AC_JACK_LOC_HDMI)
  3426. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  3427. else
  3428. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  3429. break;
  3430. }
  3431. }
  3432. /* FIX-UP:
  3433. * If no line-out is defined but multiple HPs are found,
  3434. * some of them might be the real line-outs.
  3435. */
  3436. if (!cfg->line_outs && cfg->hp_outs > 1) {
  3437. int i = 0;
  3438. while (i < cfg->hp_outs) {
  3439. /* The real HPs should have the sequence 0x0f */
  3440. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  3441. i++;
  3442. continue;
  3443. }
  3444. /* Move it to the line-out table */
  3445. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  3446. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  3447. cfg->line_outs++;
  3448. cfg->hp_outs--;
  3449. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  3450. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  3451. memmove(sequences_hp + i - 1, sequences_hp + i,
  3452. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  3453. }
  3454. }
  3455. /* sort by sequence */
  3456. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  3457. cfg->line_outs);
  3458. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  3459. cfg->speaker_outs);
  3460. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  3461. cfg->hp_outs);
  3462. /* if we have only one mic, make it AUTO_PIN_MIC */
  3463. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  3464. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  3465. cfg->input_pins[AUTO_PIN_MIC] =
  3466. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  3467. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  3468. }
  3469. /* ditto for line-in */
  3470. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  3471. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  3472. cfg->input_pins[AUTO_PIN_LINE] =
  3473. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  3474. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  3475. }
  3476. /*
  3477. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  3478. * as a primary output
  3479. */
  3480. if (!cfg->line_outs) {
  3481. if (cfg->speaker_outs) {
  3482. cfg->line_outs = cfg->speaker_outs;
  3483. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  3484. sizeof(cfg->speaker_pins));
  3485. cfg->speaker_outs = 0;
  3486. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  3487. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  3488. } else if (cfg->hp_outs) {
  3489. cfg->line_outs = cfg->hp_outs;
  3490. memcpy(cfg->line_out_pins, cfg->hp_pins,
  3491. sizeof(cfg->hp_pins));
  3492. cfg->hp_outs = 0;
  3493. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  3494. cfg->line_out_type = AUTO_PIN_HP_OUT;
  3495. }
  3496. }
  3497. /* Reorder the surround channels
  3498. * ALSA sequence is front/surr/clfe/side
  3499. * HDA sequence is:
  3500. * 4-ch: front/surr => OK as it is
  3501. * 6-ch: front/clfe/surr
  3502. * 8-ch: front/clfe/rear/side|fc
  3503. */
  3504. switch (cfg->line_outs) {
  3505. case 3:
  3506. case 4:
  3507. nid = cfg->line_out_pins[1];
  3508. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  3509. cfg->line_out_pins[2] = nid;
  3510. break;
  3511. }
  3512. /*
  3513. * debug prints of the parsed results
  3514. */
  3515. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3516. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  3517. cfg->line_out_pins[2], cfg->line_out_pins[3],
  3518. cfg->line_out_pins[4]);
  3519. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3520. cfg->speaker_outs, cfg->speaker_pins[0],
  3521. cfg->speaker_pins[1], cfg->speaker_pins[2],
  3522. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  3523. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  3524. cfg->hp_outs, cfg->hp_pins[0],
  3525. cfg->hp_pins[1], cfg->hp_pins[2],
  3526. cfg->hp_pins[3], cfg->hp_pins[4]);
  3527. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  3528. if (cfg->dig_outs)
  3529. snd_printd(" dig-out=0x%x/0x%x\n",
  3530. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  3531. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  3532. " cd=0x%x, aux=0x%x\n",
  3533. cfg->input_pins[AUTO_PIN_MIC],
  3534. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  3535. cfg->input_pins[AUTO_PIN_LINE],
  3536. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  3537. cfg->input_pins[AUTO_PIN_CD],
  3538. cfg->input_pins[AUTO_PIN_AUX]);
  3539. if (cfg->dig_in_pin)
  3540. snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
  3541. return 0;
  3542. }
  3543. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
  3544. /* labels for input pins */
  3545. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  3546. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  3547. };
  3548. EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
  3549. #ifdef CONFIG_PM
  3550. /*
  3551. * power management
  3552. */
  3553. /**
  3554. * snd_hda_suspend - suspend the codecs
  3555. * @bus: the HDA bus
  3556. *
  3557. * Returns 0 if successful.
  3558. */
  3559. int snd_hda_suspend(struct hda_bus *bus)
  3560. {
  3561. struct hda_codec *codec;
  3562. list_for_each_entry(codec, &bus->codec_list, list) {
  3563. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3564. if (!codec->power_on)
  3565. continue;
  3566. #endif
  3567. hda_call_codec_suspend(codec);
  3568. }
  3569. return 0;
  3570. }
  3571. EXPORT_SYMBOL_HDA(snd_hda_suspend);
  3572. /**
  3573. * snd_hda_resume - resume the codecs
  3574. * @bus: the HDA bus
  3575. *
  3576. * Returns 0 if successful.
  3577. *
  3578. * This fucntion is defined only when POWER_SAVE isn't set.
  3579. * In the power-save mode, the codec is resumed dynamically.
  3580. */
  3581. int snd_hda_resume(struct hda_bus *bus)
  3582. {
  3583. struct hda_codec *codec;
  3584. list_for_each_entry(codec, &bus->codec_list, list) {
  3585. if (snd_hda_codec_needs_resume(codec))
  3586. hda_call_codec_resume(codec);
  3587. }
  3588. return 0;
  3589. }
  3590. EXPORT_SYMBOL_HDA(snd_hda_resume);
  3591. #endif /* CONFIG_PM */
  3592. /*
  3593. * generic arrays
  3594. */
  3595. /* get a new element from the given array
  3596. * if it exceeds the pre-allocated array size, re-allocate the array
  3597. */
  3598. void *snd_array_new(struct snd_array *array)
  3599. {
  3600. if (array->used >= array->alloced) {
  3601. int num = array->alloced + array->alloc_align;
  3602. void *nlist;
  3603. if (snd_BUG_ON(num >= 4096))
  3604. return NULL;
  3605. nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
  3606. if (!nlist)
  3607. return NULL;
  3608. if (array->list) {
  3609. memcpy(nlist, array->list,
  3610. array->elem_size * array->alloced);
  3611. kfree(array->list);
  3612. }
  3613. array->list = nlist;
  3614. array->alloced = num;
  3615. }
  3616. return snd_array_elem(array, array->used++);
  3617. }
  3618. EXPORT_SYMBOL_HDA(snd_array_new);
  3619. /* free the given array elements */
  3620. void snd_array_free(struct snd_array *array)
  3621. {
  3622. kfree(array->list);
  3623. array->used = 0;
  3624. array->alloced = 0;
  3625. array->list = NULL;
  3626. }
  3627. EXPORT_SYMBOL_HDA(snd_array_free);
  3628. /*
  3629. * used by hda_proc.c and hda_eld.c
  3630. */
  3631. void snd_print_pcm_rates(int pcm, char *buf, int buflen)
  3632. {
  3633. static unsigned int rates[] = {
  3634. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  3635. 96000, 176400, 192000, 384000
  3636. };
  3637. int i, j;
  3638. for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
  3639. if (pcm & (1 << i))
  3640. j += snprintf(buf + j, buflen - j, " %d", rates[i]);
  3641. buf[j] = '\0'; /* necessary when j == 0 */
  3642. }
  3643. EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
  3644. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3645. {
  3646. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3647. int i, j;
  3648. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3649. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3650. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  3651. buf[j] = '\0'; /* necessary when j == 0 */
  3652. }
  3653. EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
  3654. MODULE_DESCRIPTION("HDA codec core");
  3655. MODULE_LICENSE("GPL");