hda_codec.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  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 (caps & AC_AMPCAP_MUTE)
  752. val2 |= 0x10000;
  753. if ((val2 & 0x10000) == 0 && dir == HDA_OUTPUT) {
  754. caps = query_amp_caps(codec, nid, HDA_INPUT);
  755. if (caps & AC_AMPCAP_MUTE)
  756. val2 |= 0x10000;
  757. }
  758. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  759. return -EFAULT;
  760. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  761. return -EFAULT;
  762. if (put_user(val1, _tlv + 2))
  763. return -EFAULT;
  764. if (put_user(val2, _tlv + 3))
  765. return -EFAULT;
  766. return 0;
  767. }
  768. /* switch */
  769. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  770. {
  771. int chs = get_amp_channels(kcontrol);
  772. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  773. uinfo->count = chs == 3 ? 2 : 1;
  774. uinfo->value.integer.min = 0;
  775. uinfo->value.integer.max = 1;
  776. return 0;
  777. }
  778. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  779. {
  780. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  781. hda_nid_t nid = get_amp_nid(kcontrol);
  782. int chs = get_amp_channels(kcontrol);
  783. int dir = get_amp_direction(kcontrol);
  784. int idx = get_amp_index(kcontrol);
  785. long *valp = ucontrol->value.integer.value;
  786. if (chs & 1)
  787. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  788. if (chs & 2)
  789. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  790. return 0;
  791. }
  792. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  793. {
  794. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  795. hda_nid_t nid = get_amp_nid(kcontrol);
  796. int chs = get_amp_channels(kcontrol);
  797. int dir = get_amp_direction(kcontrol);
  798. int idx = get_amp_index(kcontrol);
  799. long *valp = ucontrol->value.integer.value;
  800. int change = 0;
  801. if (chs & 1) {
  802. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  803. 0x80, *valp ? 0 : 0x80);
  804. valp++;
  805. }
  806. if (chs & 2)
  807. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  808. 0x80, *valp ? 0 : 0x80);
  809. return change;
  810. }
  811. /*
  812. * bound volume controls
  813. *
  814. * bind multiple volumes (# indices, from 0)
  815. */
  816. #define AMP_VAL_IDX_SHIFT 19
  817. #define AMP_VAL_IDX_MASK (0x0f<<19)
  818. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  819. {
  820. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  821. unsigned long pval;
  822. int err;
  823. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  824. pval = kcontrol->private_value;
  825. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  826. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  827. kcontrol->private_value = pval;
  828. mutex_unlock(&codec->spdif_mutex);
  829. return err;
  830. }
  831. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  832. {
  833. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  834. unsigned long pval;
  835. int i, indices, err = 0, change = 0;
  836. mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
  837. pval = kcontrol->private_value;
  838. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  839. for (i = 0; i < indices; i++) {
  840. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
  841. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  842. if (err < 0)
  843. break;
  844. change |= err;
  845. }
  846. kcontrol->private_value = pval;
  847. mutex_unlock(&codec->spdif_mutex);
  848. return err < 0 ? err : change;
  849. }
  850. /*
  851. * SPDIF out controls
  852. */
  853. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  854. {
  855. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  856. uinfo->count = 1;
  857. return 0;
  858. }
  859. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  860. {
  861. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  862. IEC958_AES0_NONAUDIO |
  863. IEC958_AES0_CON_EMPHASIS_5015 |
  864. IEC958_AES0_CON_NOT_COPYRIGHT;
  865. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  866. IEC958_AES1_CON_ORIGINAL;
  867. return 0;
  868. }
  869. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  870. {
  871. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  872. IEC958_AES0_NONAUDIO |
  873. IEC958_AES0_PRO_EMPHASIS_5015;
  874. return 0;
  875. }
  876. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  877. {
  878. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  879. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  880. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  881. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  882. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  883. return 0;
  884. }
  885. /* convert from SPDIF status bits to HDA SPDIF bits
  886. * bit 0 (DigEn) is always set zero (to be filled later)
  887. */
  888. static unsigned short convert_from_spdif_status(unsigned int sbits)
  889. {
  890. unsigned short val = 0;
  891. if (sbits & IEC958_AES0_PROFESSIONAL)
  892. val |= 1 << 6;
  893. if (sbits & IEC958_AES0_NONAUDIO)
  894. val |= 1 << 5;
  895. if (sbits & IEC958_AES0_PROFESSIONAL) {
  896. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  897. val |= 1 << 3;
  898. } else {
  899. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  900. val |= 1 << 3;
  901. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  902. val |= 1 << 4;
  903. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  904. val |= 1 << 7;
  905. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  906. }
  907. return val;
  908. }
  909. /* convert to SPDIF status bits from HDA SPDIF bits
  910. */
  911. static unsigned int convert_to_spdif_status(unsigned short val)
  912. {
  913. unsigned int sbits = 0;
  914. if (val & (1 << 5))
  915. sbits |= IEC958_AES0_NONAUDIO;
  916. if (val & (1 << 6))
  917. sbits |= IEC958_AES0_PROFESSIONAL;
  918. if (sbits & IEC958_AES0_PROFESSIONAL) {
  919. if (sbits & (1 << 3))
  920. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  921. } else {
  922. if (val & (1 << 3))
  923. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  924. if (! (val & (1 << 4)))
  925. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  926. if (val & (1 << 7))
  927. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  928. sbits |= val & (0x7f << 8);
  929. }
  930. return sbits;
  931. }
  932. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  933. {
  934. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  935. hda_nid_t nid = kcontrol->private_value;
  936. unsigned short val;
  937. int change;
  938. mutex_lock(&codec->spdif_mutex);
  939. codec->spdif_status = ucontrol->value.iec958.status[0] |
  940. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  941. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  942. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  943. val = convert_from_spdif_status(codec->spdif_status);
  944. val |= codec->spdif_ctls & 1;
  945. change = codec->spdif_ctls != val;
  946. codec->spdif_ctls = val;
  947. if (change || codec->in_resume) {
  948. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  949. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  950. }
  951. mutex_unlock(&codec->spdif_mutex);
  952. return change;
  953. }
  954. static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  955. {
  956. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  957. uinfo->count = 1;
  958. uinfo->value.integer.min = 0;
  959. uinfo->value.integer.max = 1;
  960. return 0;
  961. }
  962. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  963. {
  964. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  965. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  966. return 0;
  967. }
  968. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  969. {
  970. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  971. hda_nid_t nid = kcontrol->private_value;
  972. unsigned short val;
  973. int change;
  974. mutex_lock(&codec->spdif_mutex);
  975. val = codec->spdif_ctls & ~1;
  976. if (ucontrol->value.integer.value[0])
  977. val |= 1;
  978. change = codec->spdif_ctls != val;
  979. if (change || codec->in_resume) {
  980. codec->spdif_ctls = val;
  981. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  982. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  983. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  984. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  985. }
  986. mutex_unlock(&codec->spdif_mutex);
  987. return change;
  988. }
  989. static struct snd_kcontrol_new dig_mixes[] = {
  990. {
  991. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  992. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  993. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  994. .info = snd_hda_spdif_mask_info,
  995. .get = snd_hda_spdif_cmask_get,
  996. },
  997. {
  998. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  999. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1000. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  1001. .info = snd_hda_spdif_mask_info,
  1002. .get = snd_hda_spdif_pmask_get,
  1003. },
  1004. {
  1005. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1006. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1007. .info = snd_hda_spdif_mask_info,
  1008. .get = snd_hda_spdif_default_get,
  1009. .put = snd_hda_spdif_default_put,
  1010. },
  1011. {
  1012. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1013. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1014. .info = snd_hda_spdif_out_switch_info,
  1015. .get = snd_hda_spdif_out_switch_get,
  1016. .put = snd_hda_spdif_out_switch_put,
  1017. },
  1018. { } /* end */
  1019. };
  1020. /**
  1021. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  1022. * @codec: the HDA codec
  1023. * @nid: audio out widget NID
  1024. *
  1025. * Creates controls related with the SPDIF output.
  1026. * Called from each patch supporting the SPDIF out.
  1027. *
  1028. * Returns 0 if successful, or a negative error code.
  1029. */
  1030. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  1031. {
  1032. int err;
  1033. struct snd_kcontrol *kctl;
  1034. struct snd_kcontrol_new *dig_mix;
  1035. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  1036. kctl = snd_ctl_new1(dig_mix, codec);
  1037. kctl->private_value = nid;
  1038. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1039. return err;
  1040. }
  1041. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1042. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  1043. return 0;
  1044. }
  1045. /*
  1046. * SPDIF input
  1047. */
  1048. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  1049. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1050. {
  1051. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1052. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  1053. return 0;
  1054. }
  1055. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1056. {
  1057. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1058. hda_nid_t nid = kcontrol->private_value;
  1059. unsigned int val = !!ucontrol->value.integer.value[0];
  1060. int change;
  1061. mutex_lock(&codec->spdif_mutex);
  1062. change = codec->spdif_in_enable != val;
  1063. if (change || codec->in_resume) {
  1064. codec->spdif_in_enable = val;
  1065. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  1066. }
  1067. mutex_unlock(&codec->spdif_mutex);
  1068. return change;
  1069. }
  1070. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1071. {
  1072. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1073. hda_nid_t nid = kcontrol->private_value;
  1074. unsigned short val;
  1075. unsigned int sbits;
  1076. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  1077. sbits = convert_to_spdif_status(val);
  1078. ucontrol->value.iec958.status[0] = sbits;
  1079. ucontrol->value.iec958.status[1] = sbits >> 8;
  1080. ucontrol->value.iec958.status[2] = sbits >> 16;
  1081. ucontrol->value.iec958.status[3] = sbits >> 24;
  1082. return 0;
  1083. }
  1084. static struct snd_kcontrol_new dig_in_ctls[] = {
  1085. {
  1086. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1087. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  1088. .info = snd_hda_spdif_in_switch_info,
  1089. .get = snd_hda_spdif_in_switch_get,
  1090. .put = snd_hda_spdif_in_switch_put,
  1091. },
  1092. {
  1093. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1094. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1095. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  1096. .info = snd_hda_spdif_mask_info,
  1097. .get = snd_hda_spdif_in_status_get,
  1098. },
  1099. { } /* end */
  1100. };
  1101. /**
  1102. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  1103. * @codec: the HDA codec
  1104. * @nid: audio in widget NID
  1105. *
  1106. * Creates controls related with the SPDIF input.
  1107. * Called from each patch supporting the SPDIF in.
  1108. *
  1109. * Returns 0 if successful, or a negative error code.
  1110. */
  1111. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  1112. {
  1113. int err;
  1114. struct snd_kcontrol *kctl;
  1115. struct snd_kcontrol_new *dig_mix;
  1116. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  1117. kctl = snd_ctl_new1(dig_mix, codec);
  1118. kctl->private_value = nid;
  1119. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1120. return err;
  1121. }
  1122. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  1123. return 0;
  1124. }
  1125. /*
  1126. * set power state of the codec
  1127. */
  1128. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1129. unsigned int power_state)
  1130. {
  1131. hda_nid_t nid, nid_start;
  1132. int nodes;
  1133. snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  1134. power_state);
  1135. nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
  1136. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1137. if (get_wcaps(codec, nid) & AC_WCAP_POWER)
  1138. snd_hda_codec_write(codec, nid, 0,
  1139. AC_VERB_SET_POWER_STATE,
  1140. power_state);
  1141. }
  1142. if (power_state == AC_PWRST_D0)
  1143. msleep(10);
  1144. }
  1145. /**
  1146. * snd_hda_build_controls - build mixer controls
  1147. * @bus: the BUS
  1148. *
  1149. * Creates mixer controls for each codec included in the bus.
  1150. *
  1151. * Returns 0 if successful, otherwise a negative error code.
  1152. */
  1153. int snd_hda_build_controls(struct hda_bus *bus)
  1154. {
  1155. struct list_head *p;
  1156. /* build controls */
  1157. list_for_each(p, &bus->codec_list) {
  1158. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1159. int err;
  1160. if (! codec->patch_ops.build_controls)
  1161. continue;
  1162. err = codec->patch_ops.build_controls(codec);
  1163. if (err < 0)
  1164. return err;
  1165. }
  1166. /* initialize */
  1167. list_for_each(p, &bus->codec_list) {
  1168. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1169. int err;
  1170. hda_set_power_state(codec,
  1171. codec->afg ? codec->afg : codec->mfg,
  1172. AC_PWRST_D0);
  1173. if (! codec->patch_ops.init)
  1174. continue;
  1175. err = codec->patch_ops.init(codec);
  1176. if (err < 0)
  1177. return err;
  1178. }
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL(snd_hda_build_controls);
  1182. /*
  1183. * stream formats
  1184. */
  1185. struct hda_rate_tbl {
  1186. unsigned int hz;
  1187. unsigned int alsa_bits;
  1188. unsigned int hda_fmt;
  1189. };
  1190. static struct hda_rate_tbl rate_bits[] = {
  1191. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1192. /* autodetected value used in snd_hda_query_supported_pcm */
  1193. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1194. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1195. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1196. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1197. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1198. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1199. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1200. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1201. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1202. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1203. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1204. /* not autodetected value */
  1205. { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
  1206. { 0 } /* terminator */
  1207. };
  1208. /**
  1209. * snd_hda_calc_stream_format - calculate format bitset
  1210. * @rate: the sample rate
  1211. * @channels: the number of channels
  1212. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1213. * @maxbps: the max. bps
  1214. *
  1215. * Calculate the format bitset from the given rate, channels and th PCM format.
  1216. *
  1217. * Return zero if invalid.
  1218. */
  1219. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1220. unsigned int channels,
  1221. unsigned int format,
  1222. unsigned int maxbps)
  1223. {
  1224. int i;
  1225. unsigned int val = 0;
  1226. for (i = 0; rate_bits[i].hz; i++)
  1227. if (rate_bits[i].hz == rate) {
  1228. val = rate_bits[i].hda_fmt;
  1229. break;
  1230. }
  1231. if (! rate_bits[i].hz) {
  1232. snd_printdd("invalid rate %d\n", rate);
  1233. return 0;
  1234. }
  1235. if (channels == 0 || channels > 8) {
  1236. snd_printdd("invalid channels %d\n", channels);
  1237. return 0;
  1238. }
  1239. val |= channels - 1;
  1240. switch (snd_pcm_format_width(format)) {
  1241. case 8: val |= 0x00; break;
  1242. case 16: val |= 0x10; break;
  1243. case 20:
  1244. case 24:
  1245. case 32:
  1246. if (maxbps >= 32)
  1247. val |= 0x40;
  1248. else if (maxbps >= 24)
  1249. val |= 0x30;
  1250. else
  1251. val |= 0x20;
  1252. break;
  1253. default:
  1254. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1255. return 0;
  1256. }
  1257. return val;
  1258. }
  1259. EXPORT_SYMBOL(snd_hda_calc_stream_format);
  1260. /**
  1261. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1262. * @codec: the HDA codec
  1263. * @nid: NID to query
  1264. * @ratesp: the pointer to store the detected rate bitflags
  1265. * @formatsp: the pointer to store the detected formats
  1266. * @bpsp: the pointer to store the detected format widths
  1267. *
  1268. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1269. * or @bsps argument is ignored.
  1270. *
  1271. * Returns 0 if successful, otherwise a negative error code.
  1272. */
  1273. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1274. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1275. {
  1276. int i;
  1277. unsigned int val, streams;
  1278. val = 0;
  1279. if (nid != codec->afg &&
  1280. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1281. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1282. if (val == -1)
  1283. return -EIO;
  1284. }
  1285. if (! val)
  1286. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1287. if (ratesp) {
  1288. u32 rates = 0;
  1289. for (i = 0; rate_bits[i].hz; i++) {
  1290. if (val & (1 << i))
  1291. rates |= rate_bits[i].alsa_bits;
  1292. }
  1293. *ratesp = rates;
  1294. }
  1295. if (formatsp || bpsp) {
  1296. u64 formats = 0;
  1297. unsigned int bps;
  1298. unsigned int wcaps;
  1299. wcaps = get_wcaps(codec, nid);
  1300. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1301. if (streams == -1)
  1302. return -EIO;
  1303. if (! streams) {
  1304. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1305. if (streams == -1)
  1306. return -EIO;
  1307. }
  1308. bps = 0;
  1309. if (streams & AC_SUPFMT_PCM) {
  1310. if (val & AC_SUPPCM_BITS_8) {
  1311. formats |= SNDRV_PCM_FMTBIT_U8;
  1312. bps = 8;
  1313. }
  1314. if (val & AC_SUPPCM_BITS_16) {
  1315. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1316. bps = 16;
  1317. }
  1318. if (wcaps & AC_WCAP_DIGITAL) {
  1319. if (val & AC_SUPPCM_BITS_32)
  1320. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1321. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1322. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1323. if (val & AC_SUPPCM_BITS_24)
  1324. bps = 24;
  1325. else if (val & AC_SUPPCM_BITS_20)
  1326. bps = 20;
  1327. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1328. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1329. if (val & AC_SUPPCM_BITS_32)
  1330. bps = 32;
  1331. else if (val & AC_SUPPCM_BITS_20)
  1332. bps = 20;
  1333. else if (val & AC_SUPPCM_BITS_24)
  1334. bps = 24;
  1335. }
  1336. }
  1337. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1338. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1339. bps = 32;
  1340. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1341. /* temporary hack: we have still no proper support
  1342. * for the direct AC3 stream...
  1343. */
  1344. formats |= SNDRV_PCM_FMTBIT_U8;
  1345. bps = 8;
  1346. }
  1347. if (formatsp)
  1348. *formatsp = formats;
  1349. if (bpsp)
  1350. *bpsp = bps;
  1351. }
  1352. return 0;
  1353. }
  1354. /**
  1355. * snd_hda_is_supported_format - check whether the given node supports the format val
  1356. *
  1357. * Returns 1 if supported, 0 if not.
  1358. */
  1359. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1360. unsigned int format)
  1361. {
  1362. int i;
  1363. unsigned int val = 0, rate, stream;
  1364. if (nid != codec->afg &&
  1365. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
  1366. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1367. if (val == -1)
  1368. return 0;
  1369. }
  1370. if (! val) {
  1371. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1372. if (val == -1)
  1373. return 0;
  1374. }
  1375. rate = format & 0xff00;
  1376. for (i = 0; rate_bits[i].hz; i++)
  1377. if (rate_bits[i].hda_fmt == rate) {
  1378. if (val & (1 << i))
  1379. break;
  1380. return 0;
  1381. }
  1382. if (! rate_bits[i].hz)
  1383. return 0;
  1384. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1385. if (stream == -1)
  1386. return 0;
  1387. if (! stream && nid != codec->afg)
  1388. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1389. if (! stream || stream == -1)
  1390. return 0;
  1391. if (stream & AC_SUPFMT_PCM) {
  1392. switch (format & 0xf0) {
  1393. case 0x00:
  1394. if (! (val & AC_SUPPCM_BITS_8))
  1395. return 0;
  1396. break;
  1397. case 0x10:
  1398. if (! (val & AC_SUPPCM_BITS_16))
  1399. return 0;
  1400. break;
  1401. case 0x20:
  1402. if (! (val & AC_SUPPCM_BITS_20))
  1403. return 0;
  1404. break;
  1405. case 0x30:
  1406. if (! (val & AC_SUPPCM_BITS_24))
  1407. return 0;
  1408. break;
  1409. case 0x40:
  1410. if (! (val & AC_SUPPCM_BITS_32))
  1411. return 0;
  1412. break;
  1413. default:
  1414. return 0;
  1415. }
  1416. } else {
  1417. /* FIXME: check for float32 and AC3? */
  1418. }
  1419. return 1;
  1420. }
  1421. /*
  1422. * PCM stuff
  1423. */
  1424. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1425. struct hda_codec *codec,
  1426. struct snd_pcm_substream *substream)
  1427. {
  1428. return 0;
  1429. }
  1430. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1431. struct hda_codec *codec,
  1432. unsigned int stream_tag,
  1433. unsigned int format,
  1434. struct snd_pcm_substream *substream)
  1435. {
  1436. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1437. return 0;
  1438. }
  1439. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1440. struct hda_codec *codec,
  1441. struct snd_pcm_substream *substream)
  1442. {
  1443. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1444. return 0;
  1445. }
  1446. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1447. {
  1448. if (info->nid) {
  1449. /* query support PCM information from the given NID */
  1450. if (! info->rates || ! info->formats)
  1451. snd_hda_query_supported_pcm(codec, info->nid,
  1452. info->rates ? NULL : &info->rates,
  1453. info->formats ? NULL : &info->formats,
  1454. info->maxbps ? NULL : &info->maxbps);
  1455. }
  1456. if (info->ops.open == NULL)
  1457. info->ops.open = hda_pcm_default_open_close;
  1458. if (info->ops.close == NULL)
  1459. info->ops.close = hda_pcm_default_open_close;
  1460. if (info->ops.prepare == NULL) {
  1461. snd_assert(info->nid, return -EINVAL);
  1462. info->ops.prepare = hda_pcm_default_prepare;
  1463. }
  1464. if (info->ops.cleanup == NULL) {
  1465. snd_assert(info->nid, return -EINVAL);
  1466. info->ops.cleanup = hda_pcm_default_cleanup;
  1467. }
  1468. return 0;
  1469. }
  1470. /**
  1471. * snd_hda_build_pcms - build PCM information
  1472. * @bus: the BUS
  1473. *
  1474. * Create PCM information for each codec included in the bus.
  1475. *
  1476. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1477. * codec->pcm_info properly. The array is referred by the top-level driver
  1478. * to create its PCM instances.
  1479. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1480. * callback.
  1481. *
  1482. * At least, substreams, channels_min and channels_max must be filled for
  1483. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1484. * When rates and/or formats are zero, the supported values are queried
  1485. * from the given nid. The nid is used also by the default ops.prepare
  1486. * and ops.cleanup callbacks.
  1487. *
  1488. * The driver needs to call ops.open in its open callback. Similarly,
  1489. * ops.close is supposed to be called in the close callback.
  1490. * ops.prepare should be called in the prepare or hw_params callback
  1491. * with the proper parameters for set up.
  1492. * ops.cleanup should be called in hw_free for clean up of streams.
  1493. *
  1494. * This function returns 0 if successfull, or a negative error code.
  1495. */
  1496. int snd_hda_build_pcms(struct hda_bus *bus)
  1497. {
  1498. struct list_head *p;
  1499. list_for_each(p, &bus->codec_list) {
  1500. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1501. unsigned int pcm, s;
  1502. int err;
  1503. if (! codec->patch_ops.build_pcms)
  1504. continue;
  1505. err = codec->patch_ops.build_pcms(codec);
  1506. if (err < 0)
  1507. return err;
  1508. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1509. for (s = 0; s < 2; s++) {
  1510. struct hda_pcm_stream *info;
  1511. info = &codec->pcm_info[pcm].stream[s];
  1512. if (! info->substreams)
  1513. continue;
  1514. err = set_pcm_default_values(codec, info);
  1515. if (err < 0)
  1516. return err;
  1517. }
  1518. }
  1519. }
  1520. return 0;
  1521. }
  1522. EXPORT_SYMBOL(snd_hda_build_pcms);
  1523. /**
  1524. * snd_hda_check_board_config - compare the current codec with the config table
  1525. * @codec: the HDA codec
  1526. * @tbl: configuration table, terminated by null entries
  1527. *
  1528. * Compares the modelname or PCI subsystem id of the current codec with the
  1529. * given configuration table. If a matching entry is found, returns its
  1530. * config value (supposed to be 0 or positive).
  1531. *
  1532. * If no entries are matching, the function returns a negative value.
  1533. */
  1534. int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
  1535. {
  1536. const struct hda_board_config *c;
  1537. if (codec->bus->modelname) {
  1538. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1539. if (c->modelname &&
  1540. ! strcmp(codec->bus->modelname, c->modelname)) {
  1541. snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
  1542. return c->config;
  1543. }
  1544. }
  1545. }
  1546. if (codec->bus->pci) {
  1547. u16 subsystem_vendor, subsystem_device;
  1548. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1549. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1550. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1551. if (c->pci_subvendor == subsystem_vendor &&
  1552. (! c->pci_subdevice /* all match */||
  1553. (c->pci_subdevice == subsystem_device))) {
  1554. snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
  1555. subsystem_vendor, subsystem_device, c->config);
  1556. return c->config;
  1557. }
  1558. }
  1559. }
  1560. return -1;
  1561. }
  1562. /**
  1563. * snd_hda_add_new_ctls - create controls from the array
  1564. * @codec: the HDA codec
  1565. * @knew: the array of struct snd_kcontrol_new
  1566. *
  1567. * This helper function creates and add new controls in the given array.
  1568. * The array must be terminated with an empty entry as terminator.
  1569. *
  1570. * Returns 0 if successful, or a negative error code.
  1571. */
  1572. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  1573. {
  1574. int err;
  1575. for (; knew->name; knew++) {
  1576. struct snd_kcontrol *kctl;
  1577. kctl = snd_ctl_new1(knew, codec);
  1578. if (! kctl)
  1579. return -ENOMEM;
  1580. err = snd_ctl_add(codec->bus->card, kctl);
  1581. if (err < 0) {
  1582. if (! codec->addr)
  1583. return err;
  1584. kctl = snd_ctl_new1(knew, codec);
  1585. if (! kctl)
  1586. return -ENOMEM;
  1587. kctl->id.device = codec->addr;
  1588. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  1589. return err;
  1590. }
  1591. }
  1592. return 0;
  1593. }
  1594. /*
  1595. * Channel mode helper
  1596. */
  1597. int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
  1598. const struct hda_channel_mode *chmode, int num_chmodes)
  1599. {
  1600. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1601. uinfo->count = 1;
  1602. uinfo->value.enumerated.items = num_chmodes;
  1603. if (uinfo->value.enumerated.item >= num_chmodes)
  1604. uinfo->value.enumerated.item = num_chmodes - 1;
  1605. sprintf(uinfo->value.enumerated.name, "%dch",
  1606. chmode[uinfo->value.enumerated.item].channels);
  1607. return 0;
  1608. }
  1609. int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
  1610. const struct hda_channel_mode *chmode, int num_chmodes,
  1611. int max_channels)
  1612. {
  1613. int i;
  1614. for (i = 0; i < num_chmodes; i++) {
  1615. if (max_channels == chmode[i].channels) {
  1616. ucontrol->value.enumerated.item[0] = i;
  1617. break;
  1618. }
  1619. }
  1620. return 0;
  1621. }
  1622. int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
  1623. const struct hda_channel_mode *chmode, int num_chmodes,
  1624. int *max_channelsp)
  1625. {
  1626. unsigned int mode;
  1627. mode = ucontrol->value.enumerated.item[0];
  1628. snd_assert(mode < num_chmodes, return -EINVAL);
  1629. if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
  1630. return 0;
  1631. /* change the current channel setting */
  1632. *max_channelsp = chmode[mode].channels;
  1633. if (chmode[mode].sequence)
  1634. snd_hda_sequence_write(codec, chmode[mode].sequence);
  1635. return 1;
  1636. }
  1637. /*
  1638. * input MUX helper
  1639. */
  1640. int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
  1641. {
  1642. unsigned int index;
  1643. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1644. uinfo->count = 1;
  1645. uinfo->value.enumerated.items = imux->num_items;
  1646. index = uinfo->value.enumerated.item;
  1647. if (index >= imux->num_items)
  1648. index = imux->num_items - 1;
  1649. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1650. return 0;
  1651. }
  1652. int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
  1653. struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
  1654. unsigned int *cur_val)
  1655. {
  1656. unsigned int idx;
  1657. idx = ucontrol->value.enumerated.item[0];
  1658. if (idx >= imux->num_items)
  1659. idx = imux->num_items - 1;
  1660. if (*cur_val == idx && ! codec->in_resume)
  1661. return 0;
  1662. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1663. imux->items[idx].index);
  1664. *cur_val = idx;
  1665. return 1;
  1666. }
  1667. /*
  1668. * Multi-channel / digital-out PCM helper functions
  1669. */
  1670. /*
  1671. * open the digital out in the exclusive mode
  1672. */
  1673. int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
  1674. {
  1675. mutex_lock(&codec->spdif_mutex);
  1676. if (mout->dig_out_used) {
  1677. mutex_unlock(&codec->spdif_mutex);
  1678. return -EBUSY; /* already being used */
  1679. }
  1680. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1681. mutex_unlock(&codec->spdif_mutex);
  1682. return 0;
  1683. }
  1684. /*
  1685. * release the digital out
  1686. */
  1687. int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
  1688. {
  1689. mutex_lock(&codec->spdif_mutex);
  1690. mout->dig_out_used = 0;
  1691. mutex_unlock(&codec->spdif_mutex);
  1692. return 0;
  1693. }
  1694. /*
  1695. * set up more restrictions for analog out
  1696. */
  1697. int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
  1698. struct snd_pcm_substream *substream)
  1699. {
  1700. substream->runtime->hw.channels_max = mout->max_channels;
  1701. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1702. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1703. }
  1704. /*
  1705. * set up the i/o for analog out
  1706. * when the digital out is available, copy the front out to digital out, too.
  1707. */
  1708. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  1709. unsigned int stream_tag,
  1710. unsigned int format,
  1711. struct snd_pcm_substream *substream)
  1712. {
  1713. hda_nid_t *nids = mout->dac_nids;
  1714. int chs = substream->runtime->channels;
  1715. int i;
  1716. mutex_lock(&codec->spdif_mutex);
  1717. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1718. if (chs == 2 &&
  1719. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  1720. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1721. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1722. /* setup digital receiver */
  1723. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1724. stream_tag, 0, format);
  1725. } else {
  1726. mout->dig_out_used = 0;
  1727. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1728. }
  1729. }
  1730. mutex_unlock(&codec->spdif_mutex);
  1731. /* front */
  1732. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  1733. if (mout->hp_nid)
  1734. /* headphone out will just decode front left/right (stereo) */
  1735. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  1736. /* extra outputs copied from front */
  1737. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1738. if (mout->extra_out_nid[i])
  1739. snd_hda_codec_setup_stream(codec,
  1740. mout->extra_out_nid[i],
  1741. stream_tag, 0, format);
  1742. /* surrounds */
  1743. for (i = 1; i < mout->num_dacs; i++) {
  1744. if (chs >= (i + 1) * 2) /* independent out */
  1745. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  1746. format);
  1747. else /* copy front */
  1748. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
  1749. format);
  1750. }
  1751. return 0;
  1752. }
  1753. /*
  1754. * clean up the setting for analog out
  1755. */
  1756. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
  1757. {
  1758. hda_nid_t *nids = mout->dac_nids;
  1759. int i;
  1760. for (i = 0; i < mout->num_dacs; i++)
  1761. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1762. if (mout->hp_nid)
  1763. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1764. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  1765. if (mout->extra_out_nid[i])
  1766. snd_hda_codec_setup_stream(codec,
  1767. mout->extra_out_nid[i],
  1768. 0, 0, 0);
  1769. mutex_lock(&codec->spdif_mutex);
  1770. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1771. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1772. mout->dig_out_used = 0;
  1773. }
  1774. mutex_unlock(&codec->spdif_mutex);
  1775. return 0;
  1776. }
  1777. /*
  1778. * Helper for automatic ping configuration
  1779. */
  1780. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  1781. {
  1782. for (; *list; list++)
  1783. if (*list == nid)
  1784. return 1;
  1785. return 0;
  1786. }
  1787. /*
  1788. * Parse all pin widgets and store the useful pin nids to cfg
  1789. *
  1790. * The number of line-outs or any primary output is stored in line_outs,
  1791. * and the corresponding output pins are assigned to line_out_pins[],
  1792. * in the order of front, rear, CLFE, side, ...
  1793. *
  1794. * If more extra outputs (speaker and headphone) are found, the pins are
  1795. * assisnged to hp_pin and speaker_pins[], respectively. If no line-out jack
  1796. * is detected, one of speaker of HP pins is assigned as the primary
  1797. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  1798. * if any analog output exists.
  1799. *
  1800. * The analog input pins are assigned to input_pins array.
  1801. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  1802. * respectively.
  1803. */
  1804. int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
  1805. hda_nid_t *ignore_nids)
  1806. {
  1807. hda_nid_t nid, nid_start;
  1808. int i, j, nodes;
  1809. short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
  1810. memset(cfg, 0, sizeof(*cfg));
  1811. memset(sequences, 0, sizeof(sequences));
  1812. assoc_line_out = 0;
  1813. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  1814. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  1815. unsigned int wid_caps = get_wcaps(codec, nid);
  1816. unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  1817. unsigned int def_conf;
  1818. short assoc, loc;
  1819. /* read all default configuration for pin complex */
  1820. if (wid_type != AC_WID_PIN)
  1821. continue;
  1822. /* ignore the given nids (e.g. pc-beep returns error) */
  1823. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  1824. continue;
  1825. def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  1826. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  1827. continue;
  1828. loc = get_defcfg_location(def_conf);
  1829. switch (get_defcfg_device(def_conf)) {
  1830. case AC_JACK_LINE_OUT:
  1831. seq = get_defcfg_sequence(def_conf);
  1832. assoc = get_defcfg_association(def_conf);
  1833. if (! assoc)
  1834. continue;
  1835. if (! assoc_line_out)
  1836. assoc_line_out = assoc;
  1837. else if (assoc_line_out != assoc)
  1838. continue;
  1839. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  1840. continue;
  1841. cfg->line_out_pins[cfg->line_outs] = nid;
  1842. sequences[cfg->line_outs] = seq;
  1843. cfg->line_outs++;
  1844. break;
  1845. case AC_JACK_SPEAKER:
  1846. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  1847. continue;
  1848. cfg->speaker_pins[cfg->speaker_outs] = nid;
  1849. cfg->speaker_outs++;
  1850. break;
  1851. case AC_JACK_HP_OUT:
  1852. cfg->hp_pin = nid;
  1853. break;
  1854. case AC_JACK_MIC_IN:
  1855. if (loc == AC_JACK_LOC_FRONT)
  1856. cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
  1857. else
  1858. cfg->input_pins[AUTO_PIN_MIC] = nid;
  1859. break;
  1860. case AC_JACK_LINE_IN:
  1861. if (loc == AC_JACK_LOC_FRONT)
  1862. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  1863. else
  1864. cfg->input_pins[AUTO_PIN_LINE] = nid;
  1865. break;
  1866. case AC_JACK_CD:
  1867. cfg->input_pins[AUTO_PIN_CD] = nid;
  1868. break;
  1869. case AC_JACK_AUX:
  1870. cfg->input_pins[AUTO_PIN_AUX] = nid;
  1871. break;
  1872. case AC_JACK_SPDIF_OUT:
  1873. cfg->dig_out_pin = nid;
  1874. break;
  1875. case AC_JACK_SPDIF_IN:
  1876. cfg->dig_in_pin = nid;
  1877. break;
  1878. }
  1879. }
  1880. /* sort by sequence */
  1881. for (i = 0; i < cfg->line_outs; i++)
  1882. for (j = i + 1; j < cfg->line_outs; j++)
  1883. if (sequences[i] > sequences[j]) {
  1884. seq = sequences[i];
  1885. sequences[i] = sequences[j];
  1886. sequences[j] = seq;
  1887. nid = cfg->line_out_pins[i];
  1888. cfg->line_out_pins[i] = cfg->line_out_pins[j];
  1889. cfg->line_out_pins[j] = nid;
  1890. }
  1891. /* Reorder the surround channels
  1892. * ALSA sequence is front/surr/clfe/side
  1893. * HDA sequence is:
  1894. * 4-ch: front/surr => OK as it is
  1895. * 6-ch: front/clfe/surr
  1896. * 8-ch: front/clfe/side/surr
  1897. */
  1898. switch (cfg->line_outs) {
  1899. case 3:
  1900. nid = cfg->line_out_pins[1];
  1901. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  1902. cfg->line_out_pins[2] = nid;
  1903. break;
  1904. case 4:
  1905. nid = cfg->line_out_pins[1];
  1906. cfg->line_out_pins[1] = cfg->line_out_pins[3];
  1907. cfg->line_out_pins[3] = cfg->line_out_pins[2];
  1908. cfg->line_out_pins[2] = nid;
  1909. break;
  1910. }
  1911. /*
  1912. * debug prints of the parsed results
  1913. */
  1914. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  1915. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  1916. cfg->line_out_pins[2], cfg->line_out_pins[3],
  1917. cfg->line_out_pins[4]);
  1918. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  1919. cfg->speaker_outs, cfg->speaker_pins[0],
  1920. cfg->speaker_pins[1], cfg->speaker_pins[2],
  1921. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  1922. snd_printd(" hp=0x%x, dig_out=0x%x, din_in=0x%x\n",
  1923. cfg->hp_pin, cfg->dig_out_pin, cfg->dig_in_pin);
  1924. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  1925. " cd=0x%x, aux=0x%x\n",
  1926. cfg->input_pins[AUTO_PIN_MIC],
  1927. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  1928. cfg->input_pins[AUTO_PIN_LINE],
  1929. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  1930. cfg->input_pins[AUTO_PIN_CD],
  1931. cfg->input_pins[AUTO_PIN_AUX]);
  1932. /*
  1933. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  1934. * as a primary output
  1935. */
  1936. if (! cfg->line_outs) {
  1937. if (cfg->speaker_outs) {
  1938. cfg->line_outs = cfg->speaker_outs;
  1939. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  1940. sizeof(cfg->speaker_pins));
  1941. cfg->speaker_outs = 0;
  1942. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  1943. } else if (cfg->hp_pin) {
  1944. cfg->line_outs = 1;
  1945. cfg->line_out_pins[0] = cfg->hp_pin;
  1946. cfg->hp_pin = 0;
  1947. }
  1948. }
  1949. return 0;
  1950. }
  1951. /* labels for input pins */
  1952. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  1953. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  1954. };
  1955. #ifdef CONFIG_PM
  1956. /*
  1957. * power management
  1958. */
  1959. /**
  1960. * snd_hda_suspend - suspend the codecs
  1961. * @bus: the HDA bus
  1962. * @state: suspsend state
  1963. *
  1964. * Returns 0 if successful.
  1965. */
  1966. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  1967. {
  1968. struct list_head *p;
  1969. /* FIXME: should handle power widget capabilities */
  1970. list_for_each(p, &bus->codec_list) {
  1971. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1972. if (codec->patch_ops.suspend)
  1973. codec->patch_ops.suspend(codec, state);
  1974. hda_set_power_state(codec,
  1975. codec->afg ? codec->afg : codec->mfg,
  1976. AC_PWRST_D3);
  1977. }
  1978. return 0;
  1979. }
  1980. EXPORT_SYMBOL(snd_hda_suspend);
  1981. /**
  1982. * snd_hda_resume - resume the codecs
  1983. * @bus: the HDA bus
  1984. * @state: resume state
  1985. *
  1986. * Returns 0 if successful.
  1987. */
  1988. int snd_hda_resume(struct hda_bus *bus)
  1989. {
  1990. struct list_head *p;
  1991. list_for_each(p, &bus->codec_list) {
  1992. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1993. hda_set_power_state(codec,
  1994. codec->afg ? codec->afg : codec->mfg,
  1995. AC_PWRST_D0);
  1996. if (codec->patch_ops.resume)
  1997. codec->patch_ops.resume(codec);
  1998. }
  1999. return 0;
  2000. }
  2001. EXPORT_SYMBOL(snd_hda_resume);
  2002. /**
  2003. * snd_hda_resume_ctls - resume controls in the new control list
  2004. * @codec: the HDA codec
  2005. * @knew: the array of struct snd_kcontrol_new
  2006. *
  2007. * This function resumes the mixer controls in the struct snd_kcontrol_new array,
  2008. * originally for snd_hda_add_new_ctls().
  2009. * The array must be terminated with an empty entry as terminator.
  2010. */
  2011. int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  2012. {
  2013. struct snd_ctl_elem_value *val;
  2014. val = kmalloc(sizeof(*val), GFP_KERNEL);
  2015. if (! val)
  2016. return -ENOMEM;
  2017. codec->in_resume = 1;
  2018. for (; knew->name; knew++) {
  2019. int i, count;
  2020. count = knew->count ? knew->count : 1;
  2021. for (i = 0; i < count; i++) {
  2022. memset(val, 0, sizeof(*val));
  2023. val->id.iface = knew->iface;
  2024. val->id.device = knew->device;
  2025. val->id.subdevice = knew->subdevice;
  2026. strcpy(val->id.name, knew->name);
  2027. val->id.index = knew->index ? knew->index : i;
  2028. /* Assume that get callback reads only from cache,
  2029. * not accessing to the real hardware
  2030. */
  2031. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  2032. continue;
  2033. snd_ctl_elem_write(codec->bus->card, NULL, val);
  2034. }
  2035. }
  2036. codec->in_resume = 0;
  2037. kfree(val);
  2038. return 0;
  2039. }
  2040. /**
  2041. * snd_hda_resume_spdif_out - resume the digital out
  2042. * @codec: the HDA codec
  2043. */
  2044. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  2045. {
  2046. return snd_hda_resume_ctls(codec, dig_mixes);
  2047. }
  2048. /**
  2049. * snd_hda_resume_spdif_in - resume the digital in
  2050. * @codec: the HDA codec
  2051. */
  2052. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  2053. {
  2054. return snd_hda_resume_ctls(codec, dig_in_ctls);
  2055. }
  2056. #endif
  2057. /*
  2058. * INIT part
  2059. */
  2060. static int __init alsa_hda_init(void)
  2061. {
  2062. return 0;
  2063. }
  2064. static void __exit alsa_hda_exit(void)
  2065. {
  2066. }
  2067. module_init(alsa_hda_init)
  2068. module_exit(alsa_hda_exit)