hda_codec.c 89 KB

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