hda_codec.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  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 <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/mutex.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include <sound/asoundef.h>
  31. #include <sound/tlv.h>
  32. #include <sound/initval.h>
  33. #include "hda_local.h"
  34. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  35. MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
  36. MODULE_LICENSE("GPL");
  37. /*
  38. * vendor / preset table
  39. */
  40. struct hda_vendor_id {
  41. unsigned int id;
  42. const char *name;
  43. };
  44. /* codec vendor labels */
  45. static struct hda_vendor_id hda_vendor_ids[] = {
  46. { 0x10ec, "Realtek" },
  47. { 0x11d4, "Analog Devices" },
  48. { 0x13f6, "C-Media" },
  49. { 0x434d, "C-Media" },
  50. { 0x8384, "SigmaTel" },
  51. {} /* terminator */
  52. };
  53. /* codec presets */
  54. #include "hda_patch.h"
  55. /**
  56. * snd_hda_codec_read - send a command and get the response
  57. * @codec: the HDA codec
  58. * @nid: NID to send the command
  59. * @direct: direct flag
  60. * @verb: the verb to send
  61. * @parm: the parameter for the verb
  62. *
  63. * Send a single command and read the corresponding response.
  64. *
  65. * Returns the obtained response value, or -1 for an error.
  66. */
  67. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
  68. unsigned int verb, unsigned int parm)
  69. {
  70. unsigned int res;
  71. mutex_lock(&codec->bus->cmd_mutex);
  72. if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
  73. res = codec->bus->ops.get_response(codec);
  74. else
  75. res = (unsigned int)-1;
  76. mutex_unlock(&codec->bus->cmd_mutex);
  77. return res;
  78. }
  79. EXPORT_SYMBOL(snd_hda_codec_read);
  80. /**
  81. * snd_hda_codec_write - send a single command without waiting for response
  82. * @codec: the HDA codec
  83. * @nid: NID to send the command
  84. * @direct: direct flag
  85. * @verb: the verb to send
  86. * @parm: the parameter for the verb
  87. *
  88. * Send a single command without waiting for response.
  89. *
  90. * Returns 0 if successful, or a negative error code.
  91. */
  92. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  93. unsigned int verb, unsigned int parm)
  94. {
  95. int err;
  96. mutex_lock(&codec->bus->cmd_mutex);
  97. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  98. mutex_unlock(&codec->bus->cmd_mutex);
  99. return err;
  100. }
  101. EXPORT_SYMBOL(snd_hda_codec_write);
  102. /**
  103. * snd_hda_sequence_write - sequence writes
  104. * @codec: the HDA codec
  105. * @seq: VERB array to send
  106. *
  107. * Send the commands sequentially from the given array.
  108. * The array must be terminated with NID=0.
  109. */
  110. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  111. {
  112. for (; seq->nid; seq++)
  113. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  114. }
  115. EXPORT_SYMBOL(snd_hda_sequence_write);
  116. /**
  117. * snd_hda_get_sub_nodes - get the range of sub nodes
  118. * @codec: the HDA codec
  119. * @nid: NID to parse
  120. * @start_id: the pointer to store the start NID
  121. *
  122. * Parse the NID and store the start NID of its sub-nodes.
  123. * Returns the number of sub-nodes.
  124. */
  125. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
  126. {
  127. unsigned int parm;
  128. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  129. *start_id = (parm >> 16) & 0x7fff;
  130. return (int)(parm & 0x7fff);
  131. }
  132. EXPORT_SYMBOL(snd_hda_get_sub_nodes);
  133. /**
  134. * snd_hda_get_connections - get connection list
  135. * @codec: the HDA codec
  136. * @nid: NID to parse
  137. * @conn_list: connection list array
  138. * @max_conns: max. number of connections to store
  139. *
  140. * Parses the connection list of the given widget and stores the list
  141. * of NIDs.
  142. *
  143. * Returns the number of connections, or a negative error code.
  144. */
  145. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  146. hda_nid_t *conn_list, int max_conns)
  147. {
  148. unsigned int parm;
  149. int i, conn_len, conns;
  150. unsigned int shift, num_elems, mask;
  151. hda_nid_t prev_nid;
  152. snd_assert(conn_list && max_conns > 0, return -EINVAL);
  153. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  154. if (parm & AC_CLIST_LONG) {
  155. /* long form */
  156. shift = 16;
  157. num_elems = 2;
  158. } else {
  159. /* short form */
  160. shift = 8;
  161. num_elems = 4;
  162. }
  163. conn_len = parm & AC_CLIST_LENGTH;
  164. mask = (1 << (shift-1)) - 1;
  165. if (! conn_len)
  166. return 0; /* no connection */
  167. if (conn_len == 1) {
  168. /* single connection */
  169. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
  170. conn_list[0] = parm & mask;
  171. return 1;
  172. }
  173. /* multi connection */
  174. conns = 0;
  175. prev_nid = 0;
  176. for (i = 0; i < conn_len; i++) {
  177. int range_val;
  178. hda_nid_t val, n;
  179. if (i % num_elems == 0)
  180. parm = snd_hda_codec_read(codec, nid, 0,
  181. AC_VERB_GET_CONNECT_LIST, i);
  182. range_val = !! (parm & (1 << (shift-1))); /* ranges */
  183. val = parm & mask;
  184. parm >>= shift;
  185. if (range_val) {
  186. /* ranges between the previous and this one */
  187. if (! prev_nid || prev_nid >= val) {
  188. snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
  189. continue;
  190. }
  191. for (n = prev_nid + 1; n <= val; n++) {
  192. if (conns >= max_conns) {
  193. snd_printk(KERN_ERR "Too many connections\n");
  194. return -EINVAL;
  195. }
  196. conn_list[conns++] = n;
  197. }
  198. } else {
  199. if (conns >= max_conns) {
  200. snd_printk(KERN_ERR "Too many connections\n");
  201. return -EINVAL;
  202. }
  203. conn_list[conns++] = val;
  204. }
  205. prev_nid = val;
  206. }
  207. return conns;
  208. }
  209. /**
  210. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  211. * @bus: the BUS
  212. * @res: unsolicited event (lower 32bit of RIRB entry)
  213. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  214. *
  215. * Adds the given event to the queue. The events are processed in
  216. * the workqueue asynchronously. Call this function in the interrupt
  217. * hanlder when RIRB receives an unsolicited event.
  218. *
  219. * Returns 0 if successful, or a negative error code.
  220. */
  221. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  222. {
  223. struct hda_bus_unsolicited *unsol;
  224. unsigned int wp;
  225. if ((unsol = bus->unsol) == NULL)
  226. return 0;
  227. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  228. unsol->wp = wp;
  229. wp <<= 1;
  230. unsol->queue[wp] = res;
  231. unsol->queue[wp + 1] = res_ex;
  232. queue_work(unsol->workq, &unsol->work);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(snd_hda_queue_unsol_event);
  236. /*
  237. * process queueud unsolicited events
  238. */
  239. static void process_unsol_events(void *data)
  240. {
  241. struct hda_bus *bus = data;
  242. struct hda_bus_unsolicited *unsol = bus->unsol;
  243. struct hda_codec *codec;
  244. unsigned int rp, caddr, res;
  245. while (unsol->rp != unsol->wp) {
  246. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  247. unsol->rp = rp;
  248. rp <<= 1;
  249. res = unsol->queue[rp];
  250. caddr = unsol->queue[rp + 1];
  251. if (! (caddr & (1 << 4))) /* no unsolicited event? */
  252. continue;
  253. codec = bus->caddr_tbl[caddr & 0x0f];
  254. if (codec && codec->patch_ops.unsol_event)
  255. codec->patch_ops.unsol_event(codec, res);
  256. }
  257. }
  258. /*
  259. * initialize unsolicited queue
  260. */
  261. static int init_unsol_queue(struct hda_bus *bus)
  262. {
  263. struct hda_bus_unsolicited *unsol;
  264. if (bus->unsol) /* already initialized */
  265. return 0;
  266. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  267. if (! unsol) {
  268. snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
  269. return -ENOMEM;
  270. }
  271. unsol->workq = create_singlethread_workqueue("hda_codec");
  272. if (! unsol->workq) {
  273. snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
  274. kfree(unsol);
  275. return -ENOMEM;
  276. }
  277. INIT_WORK(&unsol->work, process_unsol_events, bus);
  278. bus->unsol = unsol;
  279. return 0;
  280. }
  281. /*
  282. * destructor
  283. */
  284. static void snd_hda_codec_free(struct hda_codec *codec);
  285. static int snd_hda_bus_free(struct hda_bus *bus)
  286. {
  287. struct list_head *p, *n;
  288. if (! bus)
  289. return 0;
  290. if (bus->unsol) {
  291. destroy_workqueue(bus->unsol->workq);
  292. kfree(bus->unsol);
  293. }
  294. list_for_each_safe(p, n, &bus->codec_list) {
  295. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  296. snd_hda_codec_free(codec);
  297. }
  298. if (bus->ops.private_free)
  299. bus->ops.private_free(bus);
  300. kfree(bus);
  301. return 0;
  302. }
  303. static int snd_hda_bus_dev_free(struct snd_device *device)
  304. {
  305. struct hda_bus *bus = device->device_data;
  306. return snd_hda_bus_free(bus);
  307. }
  308. /**
  309. * snd_hda_bus_new - create a HDA bus
  310. * @card: the card entry
  311. * @temp: the template for hda_bus information
  312. * @busp: the pointer to store the created bus instance
  313. *
  314. * Returns 0 if successful, or a negative error code.
  315. */
  316. int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
  317. struct hda_bus **busp)
  318. {
  319. struct hda_bus *bus;
  320. int err;
  321. static struct snd_device_ops dev_ops = {
  322. .dev_free = snd_hda_bus_dev_free,
  323. };
  324. snd_assert(temp, return -EINVAL);
  325. snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
  326. if (busp)
  327. *busp = NULL;
  328. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  329. if (bus == NULL) {
  330. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  331. return -ENOMEM;
  332. }
  333. bus->card = card;
  334. bus->private_data = temp->private_data;
  335. bus->pci = temp->pci;
  336. bus->modelname = temp->modelname;
  337. bus->ops = temp->ops;
  338. mutex_init(&bus->cmd_mutex);
  339. INIT_LIST_HEAD(&bus->codec_list);
  340. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  341. snd_hda_bus_free(bus);
  342. return err;
  343. }
  344. if (busp)
  345. *busp = bus;
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(snd_hda_bus_new);
  349. /*
  350. * find a matching codec preset
  351. */
  352. static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
  353. {
  354. const struct hda_codec_preset **tbl, *preset;
  355. for (tbl = hda_preset_tables; *tbl; tbl++) {
  356. for (preset = *tbl; preset->id; preset++) {
  357. u32 mask = preset->mask;
  358. if (! mask)
  359. mask = ~0;
  360. if (preset->id == (codec->vendor_id & mask) &&
  361. (! preset->rev ||
  362. preset->rev == codec->revision_id))
  363. return preset;
  364. }
  365. }
  366. return NULL;
  367. }
  368. /*
  369. * snd_hda_get_codec_name - store the codec name
  370. */
  371. void snd_hda_get_codec_name(struct hda_codec *codec,
  372. char *name, int namelen)
  373. {
  374. const struct hda_vendor_id *c;
  375. const char *vendor = NULL;
  376. u16 vendor_id = codec->vendor_id >> 16;
  377. char tmp[16];
  378. for (c = hda_vendor_ids; c->id; c++) {
  379. if (c->id == vendor_id) {
  380. vendor = c->name;
  381. break;
  382. }
  383. }
  384. if (! vendor) {
  385. sprintf(tmp, "Generic %04x", vendor_id);
  386. vendor = tmp;
  387. }
  388. if (codec->preset && codec->preset->name)
  389. snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
  390. else
  391. snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
  392. }
  393. /*
  394. * look for an AFG and MFG nodes
  395. */
  396. static void setup_fg_nodes(struct hda_codec *codec)
  397. {
  398. int i, total_nodes;
  399. hda_nid_t nid;
  400. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  401. for (i = 0; i < total_nodes; i++, nid++) {
  402. switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
  403. case AC_GRP_AUDIO_FUNCTION:
  404. codec->afg = nid;
  405. break;
  406. case AC_GRP_MODEM_FUNCTION:
  407. codec->mfg = nid;
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. }
  414. /*
  415. * read widget caps for each widget and store in cache
  416. */
  417. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  418. {
  419. int i;
  420. hda_nid_t nid;
  421. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  422. &codec->start_nid);
  423. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  424. if (! codec->wcaps)
  425. return -ENOMEM;
  426. nid = codec->start_nid;
  427. for (i = 0; i < codec->num_nodes; i++, nid++)
  428. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  429. AC_PAR_AUDIO_WIDGET_CAP);
  430. return 0;
  431. }
  432. /*
  433. * codec destructor
  434. */
  435. static void snd_hda_codec_free(struct hda_codec *codec)
  436. {
  437. if (! codec)
  438. return;
  439. list_del(&codec->list);
  440. codec->bus->caddr_tbl[codec->addr] = NULL;
  441. if (codec->patch_ops.free)
  442. codec->patch_ops.free(codec);
  443. kfree(codec->amp_info);
  444. kfree(codec->wcaps);
  445. kfree(codec);
  446. }
  447. static void init_amp_hash(struct hda_codec *codec);
  448. /**
  449. * snd_hda_codec_new - create a HDA codec
  450. * @bus: the bus to assign
  451. * @codec_addr: the codec address
  452. * @codecp: the pointer to store the generated codec
  453. *
  454. * Returns 0 if successful, or a negative error code.
  455. */
  456. int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  457. struct hda_codec **codecp)
  458. {
  459. struct hda_codec *codec;
  460. char component[13];
  461. int err;
  462. snd_assert(bus, return -EINVAL);
  463. snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
  464. if (bus->caddr_tbl[codec_addr]) {
  465. snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
  466. return -EBUSY;
  467. }
  468. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  469. if (codec == NULL) {
  470. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  471. return -ENOMEM;
  472. }
  473. codec->bus = bus;
  474. codec->addr = codec_addr;
  475. mutex_init(&codec->spdif_mutex);
  476. init_amp_hash(codec);
  477. list_add_tail(&codec->list, &bus->codec_list);
  478. bus->caddr_tbl[codec_addr] = codec;
  479. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
  480. if (codec->vendor_id == -1)
  481. /* read again, hopefully the access method was corrected
  482. * in the last read...
  483. */
  484. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  485. AC_PAR_VENDOR_ID);
  486. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
  487. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
  488. setup_fg_nodes(codec);
  489. if (! codec->afg && ! codec->mfg) {
  490. snd_printdd("hda_codec: no AFG or MFG node found\n");
  491. snd_hda_codec_free(codec);
  492. return -ENODEV;
  493. }
  494. if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
  495. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  496. snd_hda_codec_free(codec);
  497. return -ENOMEM;
  498. }
  499. if (! codec->subsystem_id) {
  500. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  501. codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
  502. AC_VERB_GET_SUBSYSTEM_ID,
  503. 0);
  504. }
  505. codec->preset = find_codec_preset(codec);
  506. if (! *bus->card->mixername)
  507. snd_hda_get_codec_name(codec, bus->card->mixername,
  508. sizeof(bus->card->mixername));
  509. if (codec->preset && codec->preset->patch)
  510. err = codec->preset->patch(codec);
  511. else
  512. err = snd_hda_parse_generic_codec(codec);
  513. if (err < 0) {
  514. snd_hda_codec_free(codec);
  515. return err;
  516. }
  517. if (codec->patch_ops.unsol_event)
  518. init_unsol_queue(bus);
  519. snd_hda_codec_proc_new(codec);
  520. sprintf(component, "HDA:%08x", codec->vendor_id);
  521. snd_component_add(codec->bus->card, component);
  522. if (codecp)
  523. *codecp = codec;
  524. return 0;
  525. }
  526. EXPORT_SYMBOL(snd_hda_codec_new);
  527. /**
  528. * snd_hda_codec_setup_stream - set up the codec for streaming
  529. * @codec: the CODEC to set up
  530. * @nid: the NID to set up
  531. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  532. * @channel_id: channel id to pass, zero based.
  533. * @format: stream format.
  534. */
  535. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
  536. int channel_id, int format)
  537. {
  538. if (! nid)
  539. return;
  540. snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  541. nid, stream_tag, channel_id, format);
  542. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  543. (stream_tag << 4) | channel_id);
  544. msleep(1);
  545. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  546. }
  547. EXPORT_SYMBOL(snd_hda_codec_setup_stream);
  548. /*
  549. * amp access functions
  550. */
  551. /* FIXME: more better hash key? */
  552. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  553. #define INFO_AMP_CAPS (1<<0)
  554. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  555. /* initialize the hash table */
  556. static void init_amp_hash(struct hda_codec *codec)
  557. {
  558. memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
  559. codec->num_amp_entries = 0;
  560. codec->amp_info_size = 0;
  561. codec->amp_info = NULL;
  562. }
  563. /* query the hash. allocate an entry if not found. */
  564. static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  565. {
  566. u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
  567. u16 cur = codec->amp_hash[idx];
  568. struct hda_amp_info *info;
  569. while (cur != 0xffff) {
  570. info = &codec->amp_info[cur];
  571. if (info->key == key)
  572. return info;
  573. cur = info->next;
  574. }
  575. /* add a new hash entry */
  576. if (codec->num_amp_entries >= codec->amp_info_size) {
  577. /* reallocate the array */
  578. int new_size = codec->amp_info_size + 64;
  579. struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
  580. GFP_KERNEL);
  581. if (! new_info) {
  582. snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
  583. return NULL;
  584. }
  585. if (codec->amp_info) {
  586. memcpy(new_info, codec->amp_info,
  587. codec->amp_info_size * sizeof(struct hda_amp_info));
  588. kfree(codec->amp_info);
  589. }
  590. codec->amp_info_size = new_size;
  591. codec->amp_info = new_info;
  592. }
  593. cur = codec->num_amp_entries++;
  594. info = &codec->amp_info[cur];
  595. info->key = key;
  596. info->status = 0; /* not initialized yet */
  597. info->next = codec->amp_hash[idx];
  598. codec->amp_hash[idx] = cur;
  599. return info;
  600. }
  601. /*
  602. * query AMP capabilities for the given widget and direction
  603. */
  604. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  605. {
  606. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  607. if (! info)
  608. return 0;
  609. if (! (info->status & INFO_AMP_CAPS)) {
  610. if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  611. nid = codec->afg;
  612. info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
  613. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  614. info->status |= INFO_AMP_CAPS;
  615. }
  616. return info->amp_caps;
  617. }
  618. /*
  619. * read the current volume to info
  620. * if the cache exists, read the cache value.
  621. */
  622. static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  623. hda_nid_t nid, int ch, int direction, int index)
  624. {
  625. u32 val, parm;
  626. if (info->status & INFO_AMP_VOL(ch))
  627. return info->vol[ch];
  628. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  629. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  630. parm |= index;
  631. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
  632. info->vol[ch] = val & 0xff;
  633. info->status |= INFO_AMP_VOL(ch);
  634. return info->vol[ch];
  635. }
  636. /*
  637. * write the current volume in info to the h/w and update the cache
  638. */
  639. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  640. hda_nid_t nid, int ch, int direction, int index, int val)
  641. {
  642. u32 parm;
  643. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  644. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  645. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  646. parm |= val;
  647. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  648. info->vol[ch] = val;
  649. }
  650. /*
  651. * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  652. */
  653. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  654. int direction, int index)
  655. {
  656. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  657. if (! info)
  658. return 0;
  659. return get_vol_mute(codec, info, nid, ch, direction, index);
  660. }
  661. /*
  662. * update the AMP value, mask = bit mask to set, val = the value
  663. */
  664. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  665. int direction, int idx, int mask, int val)
  666. {
  667. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  668. if (! info)
  669. return 0;
  670. val &= mask;
  671. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  672. if (info->vol[ch] == val && ! codec->in_resume)
  673. return 0;
  674. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  675. return 1;
  676. }
  677. /*
  678. * AMP control callbacks
  679. */
  680. /* retrieve parameters from private_value */
  681. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  682. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  683. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  684. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  685. /* volume */
  686. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  687. {
  688. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  689. u16 nid = get_amp_nid(kcontrol);
  690. u8 chs = get_amp_channels(kcontrol);
  691. int dir = get_amp_direction(kcontrol);
  692. u32 caps;
  693. caps = query_amp_caps(codec, nid, dir);
  694. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
  695. if (! caps) {
  696. printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
  697. return -EINVAL;
  698. }
  699. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  700. uinfo->count = chs == 3 ? 2 : 1;
  701. uinfo->value.integer.min = 0;
  702. uinfo->value.integer.max = caps;
  703. return 0;
  704. }
  705. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  706. {
  707. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  708. hda_nid_t nid = get_amp_nid(kcontrol);
  709. int chs = get_amp_channels(kcontrol);
  710. int dir = get_amp_direction(kcontrol);
  711. int idx = get_amp_index(kcontrol);
  712. long *valp = ucontrol->value.integer.value;
  713. if (chs & 1)
  714. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  715. if (chs & 2)
  716. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  717. return 0;
  718. }
  719. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  720. {
  721. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  722. hda_nid_t nid = get_amp_nid(kcontrol);
  723. int chs = get_amp_channels(kcontrol);
  724. int dir = get_amp_direction(kcontrol);
  725. int idx = get_amp_index(kcontrol);
  726. long *valp = ucontrol->value.integer.value;
  727. int change = 0;
  728. if (chs & 1) {
  729. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  730. 0x7f, *valp);
  731. valp++;
  732. }
  733. if (chs & 2)
  734. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  735. 0x7f, *valp);
  736. return change;
  737. }
  738. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  739. unsigned int size, unsigned int __user *_tlv)
  740. {
  741. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  742. hda_nid_t nid = get_amp_nid(kcontrol);
  743. int dir = get_amp_direction(kcontrol);
  744. u32 caps, val1, val2;
  745. if (size < 4 * sizeof(unsigned int))
  746. return -ENOMEM;
  747. caps = query_amp_caps(codec, nid, dir);
  748. val2 = (((caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT) + 1) * 25;
  749. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  750. val1 = ((int)val1) * ((int)val2);
  751. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  752. return -EFAULT;
  753. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  754. return -EFAULT;
  755. if (put_user(val1, _tlv + 2))
  756. return -EFAULT;
  757. if (put_user(val2, _tlv + 3))
  758. return -EFAULT;
  759. return 0;
  760. }
  761. /* switch */
  762. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  763. {
  764. int chs = get_amp_channels(kcontrol);
  765. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  766. uinfo->count = chs == 3 ? 2 : 1;
  767. uinfo->value.integer.min = 0;
  768. uinfo->value.integer.max = 1;
  769. return 0;
  770. }
  771. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  772. {
  773. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  774. hda_nid_t nid = get_amp_nid(kcontrol);
  775. int chs = get_amp_channels(kcontrol);
  776. int dir = get_amp_direction(kcontrol);
  777. int idx = get_amp_index(kcontrol);
  778. long *valp = ucontrol->value.integer.value;
  779. if (chs & 1)
  780. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  781. if (chs & 2)
  782. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  783. return 0;
  784. }
  785. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  786. {
  787. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  788. hda_nid_t nid = get_amp_nid(kcontrol);
  789. int chs = get_amp_channels(kcontrol);
  790. int dir = get_amp_direction(kcontrol);
  791. int idx = get_amp_index(kcontrol);
  792. long *valp = ucontrol->value.integer.value;
  793. int change = 0;
  794. if (chs & 1) {
  795. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  796. 0x80, *valp ? 0 : 0x80);
  797. valp++;
  798. }
  799. if (chs & 2)
  800. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  801. 0x80, *valp ? 0 : 0x80);
  802. return change;
  803. }
  804. /*
  805. * bound volume controls
  806. *
  807. * bind multiple volumes (# indices, from 0)
  808. */
  809. #define AMP_VAL_IDX_SHIFT 19
  810. #define AMP_VAL_IDX_MASK (0x0f<<19)
  811. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  812. {
  813. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  814. unsigned long pval;
  815. int err;
  816. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  817. pval = kcontrol->private_value;
  818. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  819. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  820. kcontrol->private_value = pval;
  821. mutex_unlock(&codec->spdif_mutex);
  822. return err;
  823. }
  824. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  825. {
  826. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  827. unsigned long pval;
  828. int i, indices, err = 0, change = 0;
  829. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  830. pval = kcontrol->private_value;
  831. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  832. for (i = 0; i < indices; i++) {
  833. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
  834. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  835. if (err < 0)
  836. break;
  837. change |= err;
  838. }
  839. kcontrol->private_value = pval;
  840. mutex_unlock(&codec->spdif_mutex);
  841. return err < 0 ? err : change;
  842. }
  843. /*
  844. * SPDIF out controls
  845. */
  846. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  847. {
  848. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  849. uinfo->count = 1;
  850. return 0;
  851. }
  852. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  853. {
  854. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  855. IEC958_AES0_NONAUDIO |
  856. IEC958_AES0_CON_EMPHASIS_5015 |
  857. IEC958_AES0_CON_NOT_COPYRIGHT;
  858. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  859. IEC958_AES1_CON_ORIGINAL;
  860. return 0;
  861. }
  862. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  863. {
  864. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  865. IEC958_AES0_NONAUDIO |
  866. IEC958_AES0_PRO_EMPHASIS_5015;
  867. return 0;
  868. }
  869. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  870. {
  871. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  872. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  873. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  874. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  875. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  876. return 0;
  877. }
  878. /* convert from SPDIF status bits to HDA SPDIF bits
  879. * bit 0 (DigEn) is always set zero (to be filled later)
  880. */
  881. static unsigned short convert_from_spdif_status(unsigned int sbits)
  882. {
  883. unsigned short val = 0;
  884. if (sbits & IEC958_AES0_PROFESSIONAL)
  885. val |= 1 << 6;
  886. if (sbits & IEC958_AES0_NONAUDIO)
  887. val |= 1 << 5;
  888. if (sbits & IEC958_AES0_PROFESSIONAL) {
  889. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  890. val |= 1 << 3;
  891. } else {
  892. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  893. val |= 1 << 3;
  894. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  895. val |= 1 << 4;
  896. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  897. val |= 1 << 7;
  898. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  899. }
  900. return val;
  901. }
  902. /* convert to SPDIF status bits from HDA SPDIF bits
  903. */
  904. static unsigned int convert_to_spdif_status(unsigned short val)
  905. {
  906. unsigned int sbits = 0;
  907. if (val & (1 << 5))
  908. sbits |= IEC958_AES0_NONAUDIO;
  909. if (val & (1 << 6))
  910. sbits |= IEC958_AES0_PROFESSIONAL;
  911. if (sbits & IEC958_AES0_PROFESSIONAL) {
  912. if (sbits & (1 << 3))
  913. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  914. } else {
  915. if (val & (1 << 3))
  916. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  917. if (! (val & (1 << 4)))
  918. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  919. if (val & (1 << 7))
  920. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  921. sbits |= val & (0x7f << 8);
  922. }
  923. return sbits;
  924. }
  925. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  926. {
  927. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  928. hda_nid_t nid = kcontrol->private_value;
  929. unsigned short val;
  930. int change;
  931. mutex_lock(&codec->spdif_mutex);
  932. codec->spdif_status = ucontrol->value.iec958.status[0] |
  933. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  934. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  935. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  936. val = convert_from_spdif_status(codec->spdif_status);
  937. val |= codec->spdif_ctls & 1;
  938. change = codec->spdif_ctls != val;
  939. codec->spdif_ctls = val;
  940. if (change || codec->in_resume) {
  941. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  942. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  943. }
  944. mutex_unlock(&codec->spdif_mutex);
  945. return change;
  946. }
  947. static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  948. {
  949. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  950. uinfo->count = 1;
  951. uinfo->value.integer.min = 0;
  952. uinfo->value.integer.max = 1;
  953. return 0;
  954. }
  955. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  956. {
  957. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  958. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  959. return 0;
  960. }
  961. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  962. {
  963. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  964. hda_nid_t nid = kcontrol->private_value;
  965. unsigned short val;
  966. int change;
  967. mutex_lock(&codec->spdif_mutex);
  968. val = codec->spdif_ctls & ~1;
  969. if (ucontrol->value.integer.value[0])
  970. val |= 1;
  971. change = codec->spdif_ctls != val;
  972. if (change || codec->in_resume) {
  973. codec->spdif_ctls = val;
  974. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  975. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  976. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  977. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  978. }
  979. mutex_unlock(&codec->spdif_mutex);
  980. return change;
  981. }
  982. static struct snd_kcontrol_new dig_mixes[] = {
  983. {
  984. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  985. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  986. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  987. .info = snd_hda_spdif_mask_info,
  988. .get = snd_hda_spdif_cmask_get,
  989. },
  990. {
  991. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  992. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  993. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  994. .info = snd_hda_spdif_mask_info,
  995. .get = snd_hda_spdif_pmask_get,
  996. },
  997. {
  998. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  999. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1000. .info = snd_hda_spdif_mask_info,
  1001. .get = snd_hda_spdif_default_get,
  1002. .put = snd_hda_spdif_default_put,
  1003. },
  1004. {
  1005. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1006. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1007. .info = snd_hda_spdif_out_switch_info,
  1008. .get = snd_hda_spdif_out_switch_get,
  1009. .put = snd_hda_spdif_out_switch_put,
  1010. },
  1011. { } /* end */
  1012. };
  1013. /**
  1014. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1015. * @codec: the HDA codec
  1016. * @nid: audio out widget NID
  1017. *
  1018. * Creates controls related with the SPDIF output.
  1019. * Called from each patch supporting the SPDIF out.
  1020. *
  1021. * Returns 0 if successful, or a negative error code.
  1022. */
  1023. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1024. {
  1025. int err;
  1026. struct snd_kcontrol *kctl;
  1027. struct snd_kcontrol_new *dig_mix;
  1028. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1029. kctl = snd_ctl_new1(dig_mix, codec);
  1030. kctl->private_value = nid;
  1031. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1032. return err;
  1033. }
  1034. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1035. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1036. return 0;
  1037. }
  1038. /*
  1039. * SPDIF input
  1040. */
  1041. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1042. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1043. {
  1044. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1045. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1046. return 0;
  1047. }
  1048. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1049. {
  1050. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1051. hda_nid_t nid = kcontrol->private_value;
  1052. unsigned int val = !!ucontrol->value.integer.value[0];
  1053. int change;
  1054. mutex_lock(&codec->spdif_mutex);
  1055. change = codec->spdif_in_enable != val;
  1056. if (change || codec->in_resume) {
  1057. codec->spdif_in_enable = val;
  1058. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  1059. }
  1060. mutex_unlock(&codec->spdif_mutex);
  1061. return change;
  1062. }
  1063. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1064. {
  1065. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1066. hda_nid_t nid = kcontrol->private_value;
  1067. unsigned short val;
  1068. unsigned int sbits;
  1069. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1070. sbits = convert_to_spdif_status(val);
  1071. ucontrol->value.iec958.status[0] = sbits;
  1072. ucontrol->value.iec958.status[1] = sbits >> 8;
  1073. ucontrol->value.iec958.status[2] = sbits >> 16;
  1074. ucontrol->value.iec958.status[3] = sbits >> 24;
  1075. return 0;
  1076. }
  1077. static struct snd_kcontrol_new dig_in_ctls[] = {
  1078. {
  1079. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1080. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1081. .info = snd_hda_spdif_in_switch_info,
  1082. .get = snd_hda_spdif_in_switch_get,
  1083. .put = snd_hda_spdif_in_switch_put,
  1084. },
  1085. {
  1086. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1087. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1088. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1089. .info = snd_hda_spdif_mask_info,
  1090. .get = snd_hda_spdif_in_status_get,
  1091. },
  1092. { } /* end */
  1093. };
  1094. /**
  1095. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1096. * @codec: the HDA codec
  1097. * @nid: audio in widget NID
  1098. *
  1099. * Creates controls related with the SPDIF input.
  1100. * Called from each patch supporting the SPDIF in.
  1101. *
  1102. * Returns 0 if successful, or a negative error code.
  1103. */
  1104. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1105. {
  1106. int err;
  1107. struct snd_kcontrol *kctl;
  1108. struct snd_kcontrol_new *dig_mix;
  1109. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1110. kctl = snd_ctl_new1(dig_mix, codec);
  1111. kctl->private_value = nid;
  1112. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1113. return err;
  1114. }
  1115. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  1116. return 0;
  1117. }
  1118. /*
  1119. * set power state of the codec
  1120. */
  1121. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1122. unsigned int power_state)
  1123. {
  1124. hda_nid_t nid, nid_start;
  1125. int nodes;
  1126. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1127. power_state);
  1128. nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
  1129. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1130. if (get_wcaps(codec, nid) & AC_WCAP_POWER)
  1131. snd_hda_codec_write(codec, nid, 0,
  1132. AC_VERB_SET_POWER_STATE,
  1133. power_state);
  1134. }
  1135. if (power_state == AC_PWRST_D0)
  1136. msleep(10);
  1137. }
  1138. /**
  1139. * snd_hda_build_controls - build mixer controls
  1140. * @bus: the BUS
  1141. *
  1142. * Creates mixer controls for each codec included in the bus.
  1143. *
  1144. * Returns 0 if successful, otherwise a negative error code.
  1145. */
  1146. int snd_hda_build_controls(struct hda_bus *bus)
  1147. {
  1148. struct list_head *p;
  1149. /* build controls */
  1150. list_for_each(p, &bus->codec_list) {
  1151. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1152. int err;
  1153. if (! codec->patch_ops.build_controls)
  1154. continue;
  1155. err = codec->patch_ops.build_controls(codec);
  1156. if (err < 0)
  1157. return err;
  1158. }
  1159. /* initialize */
  1160. list_for_each(p, &bus->codec_list) {
  1161. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1162. int err;
  1163. hda_set_power_state(codec,
  1164. codec->afg ? codec->afg : codec->mfg,
  1165. AC_PWRST_D0);
  1166. if (! codec->patch_ops.init)
  1167. continue;
  1168. err = codec->patch_ops.init(codec);
  1169. if (err < 0)
  1170. return err;
  1171. }
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL(snd_hda_build_controls);
  1175. /*
  1176. * stream formats
  1177. */
  1178. struct hda_rate_tbl {
  1179. unsigned int hz;
  1180. unsigned int alsa_bits;
  1181. unsigned int hda_fmt;
  1182. };
  1183. static struct hda_rate_tbl rate_bits[] = {
  1184. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1185. /* autodetected value used in snd_hda_query_supported_pcm */
  1186. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1187. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1188. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1189. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1190. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1191. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1192. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1193. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1194. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1195. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1196. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1197. /* not autodetected value */
  1198. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1199. { 0 } /* terminator */
  1200. };
  1201. /**
  1202. * snd_hda_calc_stream_format - calculate format bitset
  1203. * @rate: the sample rate
  1204. * @channels: the number of channels
  1205. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1206. * @maxbps: the max. bps
  1207. *
  1208. * Calculate the format bitset from the given rate, channels and th PCM format.
  1209. *
  1210. * Return zero if invalid.
  1211. */
  1212. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1213. unsigned int channels,
  1214. unsigned int format,
  1215. unsigned int maxbps)
  1216. {
  1217. int i;
  1218. unsigned int val = 0;
  1219. for (i = 0; rate_bits[i].hz; i++)
  1220. if (rate_bits[i].hz == rate) {
  1221. val = rate_bits[i].hda_fmt;
  1222. break;
  1223. }
  1224. if (! rate_bits[i].hz) {
  1225. snd_printdd("invalid rate %d\n", rate);
  1226. return 0;
  1227. }
  1228. if (channels == 0 || channels > 8) {
  1229. snd_printdd("invalid channels %d\n", channels);
  1230. return 0;
  1231. }
  1232. val |= channels - 1;
  1233. switch (snd_pcm_format_width(format)) {
  1234. case 8: val |= 0x00; break;
  1235. case 16: val |= 0x10; break;
  1236. case 20:
  1237. case 24:
  1238. case 32:
  1239. if (maxbps >= 32)
  1240. val |= 0x40;
  1241. else if (maxbps >= 24)
  1242. val |= 0x30;
  1243. else
  1244. val |= 0x20;
  1245. break;
  1246. default:
  1247. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1248. return 0;
  1249. }
  1250. return val;
  1251. }
  1252. EXPORT_SYMBOL(snd_hda_calc_stream_format);
  1253. /**
  1254. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1255. * @codec: the HDA codec
  1256. * @nid: NID to query
  1257. * @ratesp: the pointer to store the detected rate bitflags
  1258. * @formatsp: the pointer to store the detected formats
  1259. * @bpsp: the pointer to store the detected format widths
  1260. *
  1261. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1262. * or @bsps argument is ignored.
  1263. *
  1264. * Returns 0 if successful, otherwise a negative error code.
  1265. */
  1266. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1267. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1268. {
  1269. int i;
  1270. unsigned int val, streams;
  1271. val = 0;
  1272. if (nid != codec->afg &&
  1273. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1274. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1275. if (val == -1)
  1276. return -EIO;
  1277. }
  1278. if (! val)
  1279. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1280. if (ratesp) {
  1281. u32 rates = 0;
  1282. for (i = 0; rate_bits[i].hz; i++) {
  1283. if (val & (1 << i))
  1284. rates |= rate_bits[i].alsa_bits;
  1285. }
  1286. *ratesp = rates;
  1287. }
  1288. if (formatsp || bpsp) {
  1289. u64 formats = 0;
  1290. unsigned int bps;
  1291. unsigned int wcaps;
  1292. wcaps = get_wcaps(codec, nid);
  1293. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1294. if (streams == -1)
  1295. return -EIO;
  1296. if (! streams) {
  1297. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1298. if (streams == -1)
  1299. return -EIO;
  1300. }
  1301. bps = 0;
  1302. if (streams & AC_SUPFMT_PCM) {
  1303. if (val & AC_SUPPCM_BITS_8) {
  1304. formats |= SNDRV_PCM_FMTBIT_U8;
  1305. bps = 8;
  1306. }
  1307. if (val & AC_SUPPCM_BITS_16) {
  1308. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1309. bps = 16;
  1310. }
  1311. if (wcaps & AC_WCAP_DIGITAL) {
  1312. if (val & AC_SUPPCM_BITS_32)
  1313. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1314. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1315. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1316. if (val & AC_SUPPCM_BITS_24)
  1317. bps = 24;
  1318. else if (val & AC_SUPPCM_BITS_20)
  1319. bps = 20;
  1320. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1321. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1322. if (val & AC_SUPPCM_BITS_32)
  1323. bps = 32;
  1324. else if (val & AC_SUPPCM_BITS_20)
  1325. bps = 20;
  1326. else if (val & AC_SUPPCM_BITS_24)
  1327. bps = 24;
  1328. }
  1329. }
  1330. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1331. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1332. bps = 32;
  1333. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1334. /* temporary hack: we have still no proper support
  1335. * for the direct AC3 stream...
  1336. */
  1337. formats |= SNDRV_PCM_FMTBIT_U8;
  1338. bps = 8;
  1339. }
  1340. if (formatsp)
  1341. *formatsp = formats;
  1342. if (bpsp)
  1343. *bpsp = bps;
  1344. }
  1345. return 0;
  1346. }
  1347. /**
  1348. * snd_hda_is_supported_format - check whether the given node supports the format val
  1349. *
  1350. * Returns 1 if supported, 0 if not.
  1351. */
  1352. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1353. unsigned int format)
  1354. {
  1355. int i;
  1356. unsigned int val = 0, rate, stream;
  1357. if (nid != codec->afg &&
  1358. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1359. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1360. if (val == -1)
  1361. return 0;
  1362. }
  1363. if (! val) {
  1364. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1365. if (val == -1)
  1366. return 0;
  1367. }
  1368. rate = format & 0xff00;
  1369. for (i = 0; rate_bits[i].hz; i++)
  1370. if (rate_bits[i].hda_fmt == rate) {
  1371. if (val & (1 << i))
  1372. break;
  1373. return 0;
  1374. }
  1375. if (! rate_bits[i].hz)
  1376. return 0;
  1377. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1378. if (stream == -1)
  1379. return 0;
  1380. if (! stream && nid != codec->afg)
  1381. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1382. if (! stream || stream == -1)
  1383. return 0;
  1384. if (stream & AC_SUPFMT_PCM) {
  1385. switch (format & 0xf0) {
  1386. case 0x00:
  1387. if (! (val & AC_SUPPCM_BITS_8))
  1388. return 0;
  1389. break;
  1390. case 0x10:
  1391. if (! (val & AC_SUPPCM_BITS_16))
  1392. return 0;
  1393. break;
  1394. case 0x20:
  1395. if (! (val & AC_SUPPCM_BITS_20))
  1396. return 0;
  1397. break;
  1398. case 0x30:
  1399. if (! (val & AC_SUPPCM_BITS_24))
  1400. return 0;
  1401. break;
  1402. case 0x40:
  1403. if (! (val & AC_SUPPCM_BITS_32))
  1404. return 0;
  1405. break;
  1406. default:
  1407. return 0;
  1408. }
  1409. } else {
  1410. /* FIXME: check for float32 and AC3? */
  1411. }
  1412. return 1;
  1413. }
  1414. /*
  1415. * PCM stuff
  1416. */
  1417. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1418. struct hda_codec *codec,
  1419. struct snd_pcm_substream *substream)
  1420. {
  1421. return 0;
  1422. }
  1423. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1424. struct hda_codec *codec,
  1425. unsigned int stream_tag,
  1426. unsigned int format,
  1427. struct snd_pcm_substream *substream)
  1428. {
  1429. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1430. return 0;
  1431. }
  1432. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1433. struct hda_codec *codec,
  1434. struct snd_pcm_substream *substream)
  1435. {
  1436. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1437. return 0;
  1438. }
  1439. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1440. {
  1441. if (info->nid) {
  1442. /* query support PCM information from the given NID */
  1443. if (! info->rates || ! info->formats)
  1444. snd_hda_query_supported_pcm(codec, info->nid,
  1445. info->rates ? NULL : &info->rates,
  1446. info->formats ? NULL : &info->formats,
  1447. info->maxbps ? NULL : &info->maxbps);
  1448. }
  1449. if (info->ops.open == NULL)
  1450. info->ops.open = hda_pcm_default_open_close;
  1451. if (info->ops.close == NULL)
  1452. info->ops.close = hda_pcm_default_open_close;
  1453. if (info->ops.prepare == NULL) {
  1454. snd_assert(info->nid, return -EINVAL);
  1455. info->ops.prepare = hda_pcm_default_prepare;
  1456. }
  1457. if (info->ops.cleanup == NULL) {
  1458. snd_assert(info->nid, return -EINVAL);
  1459. info->ops.cleanup = hda_pcm_default_cleanup;
  1460. }
  1461. return 0;
  1462. }
  1463. /**
  1464. * snd_hda_build_pcms - build PCM information
  1465. * @bus: the BUS
  1466. *
  1467. * Create PCM information for each codec included in the bus.
  1468. *
  1469. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1470. * codec->pcm_info properly. The array is referred by the top-level driver
  1471. * to create its PCM instances.
  1472. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1473. * callback.
  1474. *
  1475. * At least, substreams, channels_min and channels_max must be filled for
  1476. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1477. * When rates and/or formats are zero, the supported values are queried
  1478. * from the given nid. The nid is used also by the default ops.prepare
  1479. * and ops.cleanup callbacks.
  1480. *
  1481. * The driver needs to call ops.open in its open callback. Similarly,
  1482. * ops.close is supposed to be called in the close callback.
  1483. * ops.prepare should be called in the prepare or hw_params callback
  1484. * with the proper parameters for set up.
  1485. * ops.cleanup should be called in hw_free for clean up of streams.
  1486. *
  1487. * This function returns 0 if successfull, or a negative error code.
  1488. */
  1489. int snd_hda_build_pcms(struct hda_bus *bus)
  1490. {
  1491. struct list_head *p;
  1492. list_for_each(p, &bus->codec_list) {
  1493. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1494. unsigned int pcm, s;
  1495. int err;
  1496. if (! codec->patch_ops.build_pcms)
  1497. continue;
  1498. err = codec->patch_ops.build_pcms(codec);
  1499. if (err < 0)
  1500. return err;
  1501. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1502. for (s = 0; s < 2; s++) {
  1503. struct hda_pcm_stream *info;
  1504. info = &codec->pcm_info[pcm].stream[s];
  1505. if (! info->substreams)
  1506. continue;
  1507. err = set_pcm_default_values(codec, info);
  1508. if (err < 0)
  1509. return err;
  1510. }
  1511. }
  1512. }
  1513. return 0;
  1514. }
  1515. EXPORT_SYMBOL(snd_hda_build_pcms);
  1516. /**
  1517. * snd_hda_check_board_config - compare the current codec with the config table
  1518. * @codec: the HDA codec
  1519. * @tbl: configuration table, terminated by null entries
  1520. *
  1521. * Compares the modelname or PCI subsystem id of the current codec with the
  1522. * given configuration table. If a matching entry is found, returns its
  1523. * config value (supposed to be 0 or positive).
  1524. *
  1525. * If no entries are matching, the function returns a negative value.
  1526. */
  1527. int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
  1528. {
  1529. const struct hda_board_config *c;
  1530. if (codec->bus->modelname) {
  1531. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1532. if (c->modelname &&
  1533. ! strcmp(codec->bus->modelname, c->modelname)) {
  1534. snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
  1535. return c->config;
  1536. }
  1537. }
  1538. }
  1539. if (codec->bus->pci) {
  1540. u16 subsystem_vendor, subsystem_device;
  1541. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1542. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1543. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1544. if (c->pci_subvendor == subsystem_vendor &&
  1545. (! c->pci_subdevice /* all match */||
  1546. (c->pci_subdevice == subsystem_device))) {
  1547. snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
  1548. subsystem_vendor, subsystem_device, c->config);
  1549. return c->config;
  1550. }
  1551. }
  1552. }
  1553. return -1;
  1554. }
  1555. /**
  1556. * snd_hda_add_new_ctls - create controls from the array
  1557. * @codec: the HDA codec
  1558. * @knew: the array of struct snd_kcontrol_new
  1559. *
  1560. * This helper function creates and add new controls in the given array.
  1561. * The array must be terminated with an empty entry as terminator.
  1562. *
  1563. * Returns 0 if successful, or a negative error code.
  1564. */
  1565. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  1566. {
  1567. int err;
  1568. for (; knew->name; knew++) {
  1569. struct snd_kcontrol *kctl;
  1570. kctl = snd_ctl_new1(knew, codec);
  1571. if (! kctl)
  1572. return -ENOMEM;
  1573. err = snd_ctl_add(codec->bus->card, kctl);
  1574. if (err < 0) {
  1575. if (! codec->addr)
  1576. return err;
  1577. kctl = snd_ctl_new1(knew, codec);
  1578. if (! kctl)
  1579. return -ENOMEM;
  1580. kctl->id.device = codec->addr;
  1581. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1582. return err;
  1583. }
  1584. }
  1585. return 0;
  1586. }
  1587. /*
  1588. * Channel mode helper
  1589. */
  1590. int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
  1591. const struct hda_channel_mode *chmode, int num_chmodes)
  1592. {
  1593. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1594. uinfo->count = 1;
  1595. uinfo->value.enumerated.items = num_chmodes;
  1596. if (uinfo->value.enumerated.item >= num_chmodes)
  1597. uinfo->value.enumerated.item = num_chmodes - 1;
  1598. sprintf(uinfo->value.enumerated.name, "%dch",
  1599. chmode[uinfo->value.enumerated.item].channels);
  1600. return 0;
  1601. }
  1602. int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
  1603. const struct hda_channel_mode *chmode, int num_chmodes,
  1604. int max_channels)
  1605. {
  1606. int i;
  1607. for (i = 0; i < num_chmodes; i++) {
  1608. if (max_channels == chmode[i].channels) {
  1609. ucontrol->value.enumerated.item[0] = i;
  1610. break;
  1611. }
  1612. }
  1613. return 0;
  1614. }
  1615. int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
  1616. const struct hda_channel_mode *chmode, int num_chmodes,
  1617. int *max_channelsp)
  1618. {
  1619. unsigned int mode;
  1620. mode = ucontrol->value.enumerated.item[0];
  1621. snd_assert(mode < num_chmodes, return -EINVAL);
  1622. if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
  1623. return 0;
  1624. /* change the current channel setting */
  1625. *max_channelsp = chmode[mode].channels;
  1626. if (chmode[mode].sequence)
  1627. snd_hda_sequence_write(codec, chmode[mode].sequence);
  1628. return 1;
  1629. }
  1630. /*
  1631. * input MUX helper
  1632. */
  1633. int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
  1634. {
  1635. unsigned int index;
  1636. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1637. uinfo->count = 1;
  1638. uinfo->value.enumerated.items = imux->num_items;
  1639. index = uinfo->value.enumerated.item;
  1640. if (index >= imux->num_items)
  1641. index = imux->num_items - 1;
  1642. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1643. return 0;
  1644. }
  1645. int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
  1646. struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
  1647. unsigned int *cur_val)
  1648. {
  1649. unsigned int idx;
  1650. idx = ucontrol->value.enumerated.item[0];
  1651. if (idx >= imux->num_items)
  1652. idx = imux->num_items - 1;
  1653. if (*cur_val == idx && ! codec->in_resume)
  1654. return 0;
  1655. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1656. imux->items[idx].index);
  1657. *cur_val = idx;
  1658. return 1;
  1659. }
  1660. /*
  1661. * Multi-channel / digital-out PCM helper functions
  1662. */
  1663. /*
  1664. * open the digital out in the exclusive mode
  1665. */
  1666. int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
  1667. {
  1668. mutex_lock(&codec->spdif_mutex);
  1669. if (mout->dig_out_used) {
  1670. mutex_unlock(&codec->spdif_mutex);
  1671. return -EBUSY; /* already being used */
  1672. }
  1673. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1674. mutex_unlock(&codec->spdif_mutex);
  1675. return 0;
  1676. }
  1677. /*
  1678. * release the digital out
  1679. */
  1680. int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
  1681. {
  1682. mutex_lock(&codec->spdif_mutex);
  1683. mout->dig_out_used = 0;
  1684. mutex_unlock(&codec->spdif_mutex);
  1685. return 0;
  1686. }
  1687. /*
  1688. * set up more restrictions for analog out
  1689. */
  1690. int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
  1691. struct snd_pcm_substream *substream)
  1692. {
  1693. substream->runtime->hw.channels_max = mout->max_channels;
  1694. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1695. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1696. }
  1697. /*
  1698. * set up the i/o for analog out
  1699. * when the digital out is available, copy the front out to digital out, too.
  1700. */
  1701. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  1702. unsigned int stream_tag,
  1703. unsigned int format,
  1704. struct snd_pcm_substream *substream)
  1705. {
  1706. hda_nid_t *nids = mout->dac_nids;
  1707. int chs = substream->runtime->channels;
  1708. int i;
  1709. mutex_lock(&codec->spdif_mutex);
  1710. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1711. if (chs == 2 &&
  1712. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  1713. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1714. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1715. /* setup digital receiver */
  1716. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1717. stream_tag, 0, format);
  1718. } else {
  1719. mout->dig_out_used = 0;
  1720. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1721. }
  1722. }
  1723. mutex_unlock(&codec->spdif_mutex);
  1724. /* front */
  1725. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  1726. if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  1727. /* headphone out will just decode front left/right (stereo) */
  1728. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  1729. /* extra outputs copied from front */
  1730. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1731. if (mout->extra_out_nid[i])
  1732. snd_hda_codec_setup_stream(codec,
  1733. mout->extra_out_nid[i],
  1734. stream_tag, 0, format);
  1735. /* surrounds */
  1736. for (i = 1; i < mout->num_dacs; i++) {
  1737. if (chs >= (i + 1) * 2) /* independent out */
  1738. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  1739. format);
  1740. else /* copy front */
  1741. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
  1742. format);
  1743. }
  1744. return 0;
  1745. }
  1746. /*
  1747. * clean up the setting for analog out
  1748. */
  1749. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
  1750. {
  1751. hda_nid_t *nids = mout->dac_nids;
  1752. int i;
  1753. for (i = 0; i < mout->num_dacs; i++)
  1754. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1755. if (mout->hp_nid)
  1756. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1757. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1758. if (mout->extra_out_nid[i])
  1759. snd_hda_codec_setup_stream(codec,
  1760. mout->extra_out_nid[i],
  1761. 0, 0, 0);
  1762. mutex_lock(&codec->spdif_mutex);
  1763. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1764. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1765. mout->dig_out_used = 0;
  1766. }
  1767. mutex_unlock(&codec->spdif_mutex);
  1768. return 0;
  1769. }
  1770. /*
  1771. * Helper for automatic ping configuration
  1772. */
  1773. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  1774. {
  1775. for (; *list; list++)
  1776. if (*list == nid)
  1777. return 1;
  1778. return 0;
  1779. }
  1780. /*
  1781. * Parse all pin widgets and store the useful pin nids to cfg
  1782. *
  1783. * The number of line-outs or any primary output is stored in line_outs,
  1784. * and the corresponding output pins are assigned to line_out_pins[],
  1785. * in the order of front, rear, CLFE, side, ...
  1786. *
  1787. * If more extra outputs (speaker and headphone) are found, the pins are
  1788. * assisnged to hp_pin and speaker_pins[], respectively. If no line-out jack
  1789. * is detected, one of speaker of HP pins is assigned as the primary
  1790. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  1791. * if any analog output exists.
  1792. *
  1793. * The analog input pins are assigned to input_pins array.
  1794. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  1795. * respectively.
  1796. */
  1797. int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
  1798. hda_nid_t *ignore_nids)
  1799. {
  1800. hda_nid_t nid, nid_start;
  1801. int i, j, nodes;
  1802. short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
  1803. memset(cfg, 0, sizeof(*cfg));
  1804. memset(sequences, 0, sizeof(sequences));
  1805. assoc_line_out = 0;
  1806. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  1807. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1808. unsigned int wid_caps = get_wcaps(codec, nid);
  1809. unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  1810. unsigned int def_conf;
  1811. short assoc, loc;
  1812. /* read all default configuration for pin complex */
  1813. if (wid_type != AC_WID_PIN)
  1814. continue;
  1815. /* ignore the given nids (e.g. pc-beep returns error) */
  1816. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  1817. continue;
  1818. def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  1819. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  1820. continue;
  1821. loc = get_defcfg_location(def_conf);
  1822. switch (get_defcfg_device(def_conf)) {
  1823. case AC_JACK_LINE_OUT:
  1824. seq = get_defcfg_sequence(def_conf);
  1825. assoc = get_defcfg_association(def_conf);
  1826. if (! assoc)
  1827. continue;
  1828. if (! assoc_line_out)
  1829. assoc_line_out = assoc;
  1830. else if (assoc_line_out != assoc)
  1831. continue;
  1832. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  1833. continue;
  1834. cfg->line_out_pins[cfg->line_outs] = nid;
  1835. sequences[cfg->line_outs] = seq;
  1836. cfg->line_outs++;
  1837. break;
  1838. case AC_JACK_SPEAKER:
  1839. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  1840. continue;
  1841. cfg->speaker_pins[cfg->speaker_outs] = nid;
  1842. cfg->speaker_outs++;
  1843. break;
  1844. case AC_JACK_HP_OUT:
  1845. cfg->hp_pin = nid;
  1846. break;
  1847. case AC_JACK_MIC_IN:
  1848. if (loc == AC_JACK_LOC_FRONT)
  1849. cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
  1850. else
  1851. cfg->input_pins[AUTO_PIN_MIC] = nid;
  1852. break;
  1853. case AC_JACK_LINE_IN:
  1854. if (loc == AC_JACK_LOC_FRONT)
  1855. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  1856. else
  1857. cfg->input_pins[AUTO_PIN_LINE] = nid;
  1858. break;
  1859. case AC_JACK_CD:
  1860. cfg->input_pins[AUTO_PIN_CD] = nid;
  1861. break;
  1862. case AC_JACK_AUX:
  1863. cfg->input_pins[AUTO_PIN_AUX] = nid;
  1864. break;
  1865. case AC_JACK_SPDIF_OUT:
  1866. cfg->dig_out_pin = nid;
  1867. break;
  1868. case AC_JACK_SPDIF_IN:
  1869. cfg->dig_in_pin = nid;
  1870. break;
  1871. }
  1872. }
  1873. /* sort by sequence */
  1874. for (i = 0; i < cfg->line_outs; i++)
  1875. for (j = i + 1; j < cfg->line_outs; j++)
  1876. if (sequences[i] > sequences[j]) {
  1877. seq = sequences[i];
  1878. sequences[i] = sequences[j];
  1879. sequences[j] = seq;
  1880. nid = cfg->line_out_pins[i];
  1881. cfg->line_out_pins[i] = cfg->line_out_pins[j];
  1882. cfg->line_out_pins[j] = nid;
  1883. }
  1884. /* Reorder the surround channels
  1885. * ALSA sequence is front/surr/clfe/side
  1886. * HDA sequence is:
  1887. * 4-ch: front/surr => OK as it is
  1888. * 6-ch: front/clfe/surr
  1889. * 8-ch: front/clfe/side/surr
  1890. */
  1891. switch (cfg->line_outs) {
  1892. case 3:
  1893. nid = cfg->line_out_pins[1];
  1894. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  1895. cfg->line_out_pins[2] = nid;
  1896. break;
  1897. case 4:
  1898. nid = cfg->line_out_pins[1];
  1899. cfg->line_out_pins[1] = cfg->line_out_pins[3];
  1900. cfg->line_out_pins[3] = cfg->line_out_pins[2];
  1901. cfg->line_out_pins[2] = nid;
  1902. break;
  1903. }
  1904. /*
  1905. * debug prints of the parsed results
  1906. */
  1907. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  1908. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  1909. cfg->line_out_pins[2], cfg->line_out_pins[3],
  1910. cfg->line_out_pins[4]);
  1911. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  1912. cfg->speaker_outs, cfg->speaker_pins[0],
  1913. cfg->speaker_pins[1], cfg->speaker_pins[2],
  1914. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  1915. snd_printd(" hp=0x%x, dig_out=0x%x, din_in=0x%x\n",
  1916. cfg->hp_pin, cfg->dig_out_pin, cfg->dig_in_pin);
  1917. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  1918. " cd=0x%x, aux=0x%x\n",
  1919. cfg->input_pins[AUTO_PIN_MIC],
  1920. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  1921. cfg->input_pins[AUTO_PIN_LINE],
  1922. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  1923. cfg->input_pins[AUTO_PIN_CD],
  1924. cfg->input_pins[AUTO_PIN_AUX]);
  1925. /*
  1926. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  1927. * as a primary output
  1928. */
  1929. if (! cfg->line_outs) {
  1930. if (cfg->speaker_outs) {
  1931. cfg->line_outs = cfg->speaker_outs;
  1932. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  1933. sizeof(cfg->speaker_pins));
  1934. cfg->speaker_outs = 0;
  1935. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  1936. } else if (cfg->hp_pin) {
  1937. cfg->line_outs = 1;
  1938. cfg->line_out_pins[0] = cfg->hp_pin;
  1939. cfg->hp_pin = 0;
  1940. }
  1941. }
  1942. return 0;
  1943. }
  1944. /* labels for input pins */
  1945. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  1946. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  1947. };
  1948. #ifdef CONFIG_PM
  1949. /*
  1950. * power management
  1951. */
  1952. /**
  1953. * snd_hda_suspend - suspend the codecs
  1954. * @bus: the HDA bus
  1955. * @state: suspsend state
  1956. *
  1957. * Returns 0 if successful.
  1958. */
  1959. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  1960. {
  1961. struct list_head *p;
  1962. /* FIXME: should handle power widget capabilities */
  1963. list_for_each(p, &bus->codec_list) {
  1964. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1965. if (codec->patch_ops.suspend)
  1966. codec->patch_ops.suspend(codec, state);
  1967. hda_set_power_state(codec,
  1968. codec->afg ? codec->afg : codec->mfg,
  1969. AC_PWRST_D3);
  1970. }
  1971. return 0;
  1972. }
  1973. EXPORT_SYMBOL(snd_hda_suspend);
  1974. /**
  1975. * snd_hda_resume - resume the codecs
  1976. * @bus: the HDA bus
  1977. * @state: resume state
  1978. *
  1979. * Returns 0 if successful.
  1980. */
  1981. int snd_hda_resume(struct hda_bus *bus)
  1982. {
  1983. struct list_head *p;
  1984. list_for_each(p, &bus->codec_list) {
  1985. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1986. hda_set_power_state(codec,
  1987. codec->afg ? codec->afg : codec->mfg,
  1988. AC_PWRST_D0);
  1989. if (codec->patch_ops.resume)
  1990. codec->patch_ops.resume(codec);
  1991. }
  1992. return 0;
  1993. }
  1994. EXPORT_SYMBOL(snd_hda_resume);
  1995. /**
  1996. * snd_hda_resume_ctls - resume controls in the new control list
  1997. * @codec: the HDA codec
  1998. * @knew: the array of struct snd_kcontrol_new
  1999. *
  2000. * This function resumes the mixer controls in the struct snd_kcontrol_new array,
  2001. * originally for snd_hda_add_new_ctls().
  2002. * The array must be terminated with an empty entry as terminator.
  2003. */
  2004. int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2005. {
  2006. struct snd_ctl_elem_value *val;
  2007. val = kmalloc(sizeof(*val), GFP_KERNEL);
  2008. if (! val)
  2009. return -ENOMEM;
  2010. codec->in_resume = 1;
  2011. for (; knew->name; knew++) {
  2012. int i, count;
  2013. count = knew->count ? knew->count : 1;
  2014. for (i = 0; i < count; i++) {
  2015. memset(val, 0, sizeof(*val));
  2016. val->id.iface = knew->iface;
  2017. val->id.device = knew->device;
  2018. val->id.subdevice = knew->subdevice;
  2019. strcpy(val->id.name, knew->name);
  2020. val->id.index = knew->index ? knew->index : i;
  2021. /* Assume that get callback reads only from cache,
  2022. * not accessing to the real hardware
  2023. */
  2024. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  2025. continue;
  2026. snd_ctl_elem_write(codec->bus->card, NULL, val);
  2027. }
  2028. }
  2029. codec->in_resume = 0;
  2030. kfree(val);
  2031. return 0;
  2032. }
  2033. /**
  2034. * snd_hda_resume_spdif_out - resume the digital out
  2035. * @codec: the HDA codec
  2036. */
  2037. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  2038. {
  2039. return snd_hda_resume_ctls(codec, dig_mixes);
  2040. }
  2041. /**
  2042. * snd_hda_resume_spdif_in - resume the digital in
  2043. * @codec: the HDA codec
  2044. */
  2045. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  2046. {
  2047. return snd_hda_resume_ctls(codec, dig_in_ctls);
  2048. }
  2049. #endif
  2050. /*
  2051. * INIT part
  2052. */
  2053. static int __init alsa_hda_init(void)
  2054. {
  2055. return 0;
  2056. }
  2057. static void __exit alsa_hda_exit(void)
  2058. {
  2059. }
  2060. module_init(alsa_hda_init)
  2061. module_exit(alsa_hda_exit)