hda_codec.c 104 KB

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