hda_codec.c 62 KB

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