hda_codec.c 62 KB

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