hda_hwdep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * HWDEP Interface for HD-audio codec
  3. *
  4. * Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This driver is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This driver is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/pci.h>
  23. #include <linux/compat.h>
  24. #include <linux/mutex.h>
  25. #include <linux/ctype.h>
  26. #include <linux/string.h>
  27. #include <linux/firmware.h>
  28. #include <linux/export.h>
  29. #include <sound/core.h>
  30. #include "hda_codec.h"
  31. #include "hda_local.h"
  32. #include <sound/hda_hwdep.h>
  33. #include <sound/minors.h>
  34. /* hint string pair */
  35. struct hda_hint {
  36. const char *key;
  37. const char *val; /* contained in the same alloc as key */
  38. };
  39. /*
  40. * write/read an out-of-bound verb
  41. */
  42. static int verb_write_ioctl(struct hda_codec *codec,
  43. struct hda_verb_ioctl __user *arg)
  44. {
  45. u32 verb, res;
  46. if (get_user(verb, &arg->verb))
  47. return -EFAULT;
  48. res = snd_hda_codec_read(codec, verb >> 24, 0,
  49. (verb >> 8) & 0xffff, verb & 0xff);
  50. if (put_user(res, &arg->res))
  51. return -EFAULT;
  52. return 0;
  53. }
  54. static int get_wcap_ioctl(struct hda_codec *codec,
  55. struct hda_verb_ioctl __user *arg)
  56. {
  57. u32 verb, res;
  58. if (get_user(verb, &arg->verb))
  59. return -EFAULT;
  60. res = get_wcaps(codec, verb >> 24);
  61. if (put_user(res, &arg->res))
  62. return -EFAULT;
  63. return 0;
  64. }
  65. /*
  66. */
  67. static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  68. unsigned int cmd, unsigned long arg)
  69. {
  70. struct hda_codec *codec = hw->private_data;
  71. void __user *argp = (void __user *)arg;
  72. switch (cmd) {
  73. case HDA_IOCTL_PVERSION:
  74. return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
  75. case HDA_IOCTL_VERB_WRITE:
  76. return verb_write_ioctl(codec, argp);
  77. case HDA_IOCTL_GET_WCAP:
  78. return get_wcap_ioctl(codec, argp);
  79. }
  80. return -ENOIOCTLCMD;
  81. }
  82. #ifdef CONFIG_COMPAT
  83. static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
  84. unsigned int cmd, unsigned long arg)
  85. {
  86. return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
  87. }
  88. #endif
  89. static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
  90. {
  91. #ifndef CONFIG_SND_DEBUG_VERBOSE
  92. if (!capable(CAP_SYS_RAWIO))
  93. return -EACCES;
  94. #endif
  95. return 0;
  96. }
  97. static void clear_hwdep_elements(struct hda_codec *codec)
  98. {
  99. int i;
  100. /* clear init verbs */
  101. snd_array_free(&codec->init_verbs);
  102. /* clear hints */
  103. for (i = 0; i < codec->hints.used; i++) {
  104. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  105. kfree(hint->key); /* we don't need to free hint->val */
  106. }
  107. snd_array_free(&codec->hints);
  108. snd_array_free(&codec->user_pins);
  109. }
  110. static void hwdep_free(struct snd_hwdep *hwdep)
  111. {
  112. clear_hwdep_elements(hwdep->private_data);
  113. }
  114. int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
  115. {
  116. char hwname[16];
  117. struct snd_hwdep *hwdep;
  118. int err;
  119. sprintf(hwname, "HDA Codec %d", codec->addr);
  120. err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
  121. if (err < 0)
  122. return err;
  123. codec->hwdep = hwdep;
  124. sprintf(hwdep->name, "HDA Codec %d", codec->addr);
  125. hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
  126. hwdep->private_data = codec;
  127. hwdep->private_free = hwdep_free;
  128. hwdep->exclusive = 1;
  129. hwdep->ops.open = hda_hwdep_open;
  130. hwdep->ops.ioctl = hda_hwdep_ioctl;
  131. #ifdef CONFIG_COMPAT
  132. hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
  133. #endif
  134. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  135. snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
  136. snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
  137. return 0;
  138. }
  139. #ifdef CONFIG_SND_HDA_POWER_SAVE
  140. static ssize_t power_on_acct_show(struct device *dev,
  141. struct device_attribute *attr,
  142. char *buf)
  143. {
  144. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  145. struct hda_codec *codec = hwdep->private_data;
  146. snd_hda_update_power_acct(codec);
  147. return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_on_acct));
  148. }
  149. static ssize_t power_off_acct_show(struct device *dev,
  150. struct device_attribute *attr,
  151. char *buf)
  152. {
  153. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  154. struct hda_codec *codec = hwdep->private_data;
  155. snd_hda_update_power_acct(codec);
  156. return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
  157. }
  158. static struct device_attribute power_attrs[] = {
  159. __ATTR_RO(power_on_acct),
  160. __ATTR_RO(power_off_acct),
  161. };
  162. int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec)
  163. {
  164. struct snd_hwdep *hwdep = codec->hwdep;
  165. int i;
  166. for (i = 0; i < ARRAY_SIZE(power_attrs); i++)
  167. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  168. hwdep->device, &power_attrs[i]);
  169. return 0;
  170. }
  171. #endif /* CONFIG_SND_HDA_POWER_SAVE */
  172. #ifdef CONFIG_SND_HDA_RECONFIG
  173. /*
  174. * sysfs interface
  175. */
  176. static int clear_codec(struct hda_codec *codec)
  177. {
  178. int err;
  179. err = snd_hda_codec_reset(codec);
  180. if (err < 0) {
  181. snd_printk(KERN_ERR "The codec is being used, can't free.\n");
  182. return err;
  183. }
  184. clear_hwdep_elements(codec);
  185. return 0;
  186. }
  187. static int reconfig_codec(struct hda_codec *codec)
  188. {
  189. int err;
  190. snd_hda_power_up(codec);
  191. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  192. err = snd_hda_codec_reset(codec);
  193. if (err < 0) {
  194. snd_printk(KERN_ERR
  195. "The codec is being used, can't reconfigure.\n");
  196. goto error;
  197. }
  198. err = snd_hda_codec_configure(codec);
  199. if (err < 0)
  200. goto error;
  201. /* rebuild PCMs */
  202. err = snd_hda_codec_build_pcms(codec);
  203. if (err < 0)
  204. goto error;
  205. /* rebuild mixers */
  206. err = snd_hda_codec_build_controls(codec);
  207. if (err < 0)
  208. goto error;
  209. err = snd_card_register(codec->bus->card);
  210. error:
  211. snd_hda_power_down(codec);
  212. return err;
  213. }
  214. /*
  215. * allocate a string at most len chars, and remove the trailing EOL
  216. */
  217. static char *kstrndup_noeol(const char *src, size_t len)
  218. {
  219. char *s = kstrndup(src, len, GFP_KERNEL);
  220. char *p;
  221. if (!s)
  222. return NULL;
  223. p = strchr(s, '\n');
  224. if (p)
  225. *p = 0;
  226. return s;
  227. }
  228. #define CODEC_INFO_SHOW(type) \
  229. static ssize_t type##_show(struct device *dev, \
  230. struct device_attribute *attr, \
  231. char *buf) \
  232. { \
  233. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  234. struct hda_codec *codec = hwdep->private_data; \
  235. return sprintf(buf, "0x%x\n", codec->type); \
  236. }
  237. #define CODEC_INFO_STR_SHOW(type) \
  238. static ssize_t type##_show(struct device *dev, \
  239. struct device_attribute *attr, \
  240. char *buf) \
  241. { \
  242. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  243. struct hda_codec *codec = hwdep->private_data; \
  244. return sprintf(buf, "%s\n", \
  245. codec->type ? codec->type : ""); \
  246. }
  247. CODEC_INFO_SHOW(vendor_id);
  248. CODEC_INFO_SHOW(subsystem_id);
  249. CODEC_INFO_SHOW(revision_id);
  250. CODEC_INFO_SHOW(afg);
  251. CODEC_INFO_SHOW(mfg);
  252. CODEC_INFO_STR_SHOW(vendor_name);
  253. CODEC_INFO_STR_SHOW(chip_name);
  254. CODEC_INFO_STR_SHOW(modelname);
  255. #define CODEC_INFO_STORE(type) \
  256. static ssize_t type##_store(struct device *dev, \
  257. struct device_attribute *attr, \
  258. const char *buf, size_t count) \
  259. { \
  260. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  261. struct hda_codec *codec = hwdep->private_data; \
  262. unsigned long val; \
  263. int err = strict_strtoul(buf, 0, &val); \
  264. if (err < 0) \
  265. return err; \
  266. codec->type = val; \
  267. return count; \
  268. }
  269. #define CODEC_INFO_STR_STORE(type) \
  270. static ssize_t type##_store(struct device *dev, \
  271. struct device_attribute *attr, \
  272. const char *buf, size_t count) \
  273. { \
  274. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  275. struct hda_codec *codec = hwdep->private_data; \
  276. char *s = kstrndup_noeol(buf, 64); \
  277. if (!s) \
  278. return -ENOMEM; \
  279. kfree(codec->type); \
  280. codec->type = s; \
  281. return count; \
  282. }
  283. CODEC_INFO_STORE(vendor_id);
  284. CODEC_INFO_STORE(subsystem_id);
  285. CODEC_INFO_STORE(revision_id);
  286. CODEC_INFO_STR_STORE(vendor_name);
  287. CODEC_INFO_STR_STORE(chip_name);
  288. CODEC_INFO_STR_STORE(modelname);
  289. #define CODEC_ACTION_STORE(type) \
  290. static ssize_t type##_store(struct device *dev, \
  291. struct device_attribute *attr, \
  292. const char *buf, size_t count) \
  293. { \
  294. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  295. struct hda_codec *codec = hwdep->private_data; \
  296. int err = 0; \
  297. if (*buf) \
  298. err = type##_codec(codec); \
  299. return err < 0 ? err : count; \
  300. }
  301. CODEC_ACTION_STORE(reconfig);
  302. CODEC_ACTION_STORE(clear);
  303. static ssize_t init_verbs_show(struct device *dev,
  304. struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  308. struct hda_codec *codec = hwdep->private_data;
  309. int i, len = 0;
  310. for (i = 0; i < codec->init_verbs.used; i++) {
  311. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  312. len += snprintf(buf + len, PAGE_SIZE - len,
  313. "0x%02x 0x%03x 0x%04x\n",
  314. v->nid, v->verb, v->param);
  315. }
  316. return len;
  317. }
  318. static int parse_init_verbs(struct hda_codec *codec, const char *buf)
  319. {
  320. struct hda_verb *v;
  321. int nid, verb, param;
  322. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  323. return -EINVAL;
  324. if (!nid || !verb)
  325. return -EINVAL;
  326. v = snd_array_new(&codec->init_verbs);
  327. if (!v)
  328. return -ENOMEM;
  329. v->nid = nid;
  330. v->verb = verb;
  331. v->param = param;
  332. return 0;
  333. }
  334. static ssize_t init_verbs_store(struct device *dev,
  335. struct device_attribute *attr,
  336. const char *buf, size_t count)
  337. {
  338. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  339. struct hda_codec *codec = hwdep->private_data;
  340. int err = parse_init_verbs(codec, buf);
  341. if (err < 0)
  342. return err;
  343. return count;
  344. }
  345. static ssize_t hints_show(struct device *dev,
  346. struct device_attribute *attr,
  347. char *buf)
  348. {
  349. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  350. struct hda_codec *codec = hwdep->private_data;
  351. int i, len = 0;
  352. for (i = 0; i < codec->hints.used; i++) {
  353. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  354. len += snprintf(buf + len, PAGE_SIZE - len,
  355. "%s = %s\n", hint->key, hint->val);
  356. }
  357. return len;
  358. }
  359. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  360. {
  361. int i;
  362. for (i = 0; i < codec->hints.used; i++) {
  363. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  364. if (!strcmp(hint->key, key))
  365. return hint;
  366. }
  367. return NULL;
  368. }
  369. static void remove_trail_spaces(char *str)
  370. {
  371. char *p;
  372. if (!*str)
  373. return;
  374. p = str + strlen(str) - 1;
  375. for (; isspace(*p); p--) {
  376. *p = 0;
  377. if (p == str)
  378. return;
  379. }
  380. }
  381. #define MAX_HINTS 1024
  382. static int parse_hints(struct hda_codec *codec, const char *buf)
  383. {
  384. char *key, *val;
  385. struct hda_hint *hint;
  386. buf = skip_spaces(buf);
  387. if (!*buf || *buf == '#' || *buf == '\n')
  388. return 0;
  389. if (*buf == '=')
  390. return -EINVAL;
  391. key = kstrndup_noeol(buf, 1024);
  392. if (!key)
  393. return -ENOMEM;
  394. /* extract key and val */
  395. val = strchr(key, '=');
  396. if (!val) {
  397. kfree(key);
  398. return -EINVAL;
  399. }
  400. *val++ = 0;
  401. val = skip_spaces(val);
  402. remove_trail_spaces(key);
  403. remove_trail_spaces(val);
  404. hint = get_hint(codec, key);
  405. if (hint) {
  406. /* replace */
  407. kfree(hint->key);
  408. hint->key = key;
  409. hint->val = val;
  410. return 0;
  411. }
  412. /* allocate a new hint entry */
  413. if (codec->hints.used >= MAX_HINTS)
  414. hint = NULL;
  415. else
  416. hint = snd_array_new(&codec->hints);
  417. if (!hint) {
  418. kfree(key);
  419. return -ENOMEM;
  420. }
  421. hint->key = key;
  422. hint->val = val;
  423. return 0;
  424. }
  425. static ssize_t hints_store(struct device *dev,
  426. struct device_attribute *attr,
  427. const char *buf, size_t count)
  428. {
  429. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  430. struct hda_codec *codec = hwdep->private_data;
  431. int err = parse_hints(codec, buf);
  432. if (err < 0)
  433. return err;
  434. return count;
  435. }
  436. static ssize_t pin_configs_show(struct hda_codec *codec,
  437. struct snd_array *list,
  438. char *buf)
  439. {
  440. int i, len = 0;
  441. for (i = 0; i < list->used; i++) {
  442. struct hda_pincfg *pin = snd_array_elem(list, i);
  443. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  444. pin->nid, pin->cfg);
  445. }
  446. return len;
  447. }
  448. static ssize_t init_pin_configs_show(struct device *dev,
  449. struct device_attribute *attr,
  450. char *buf)
  451. {
  452. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  453. struct hda_codec *codec = hwdep->private_data;
  454. return pin_configs_show(codec, &codec->init_pins, buf);
  455. }
  456. static ssize_t user_pin_configs_show(struct device *dev,
  457. struct device_attribute *attr,
  458. char *buf)
  459. {
  460. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  461. struct hda_codec *codec = hwdep->private_data;
  462. return pin_configs_show(codec, &codec->user_pins, buf);
  463. }
  464. static ssize_t driver_pin_configs_show(struct device *dev,
  465. struct device_attribute *attr,
  466. char *buf)
  467. {
  468. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  469. struct hda_codec *codec = hwdep->private_data;
  470. return pin_configs_show(codec, &codec->driver_pins, buf);
  471. }
  472. #define MAX_PIN_CONFIGS 32
  473. static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
  474. {
  475. int nid, cfg;
  476. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  477. return -EINVAL;
  478. if (!nid)
  479. return -EINVAL;
  480. return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  481. }
  482. static ssize_t user_pin_configs_store(struct device *dev,
  483. struct device_attribute *attr,
  484. const char *buf, size_t count)
  485. {
  486. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  487. struct hda_codec *codec = hwdep->private_data;
  488. int err = parse_user_pin_configs(codec, buf);
  489. if (err < 0)
  490. return err;
  491. return count;
  492. }
  493. #define CODEC_ATTR_RW(type) \
  494. __ATTR(type, 0644, type##_show, type##_store)
  495. #define CODEC_ATTR_RO(type) \
  496. __ATTR_RO(type)
  497. #define CODEC_ATTR_WO(type) \
  498. __ATTR(type, 0200, NULL, type##_store)
  499. static struct device_attribute codec_attrs[] = {
  500. CODEC_ATTR_RW(vendor_id),
  501. CODEC_ATTR_RW(subsystem_id),
  502. CODEC_ATTR_RW(revision_id),
  503. CODEC_ATTR_RO(afg),
  504. CODEC_ATTR_RO(mfg),
  505. CODEC_ATTR_RW(vendor_name),
  506. CODEC_ATTR_RW(chip_name),
  507. CODEC_ATTR_RW(modelname),
  508. CODEC_ATTR_RW(init_verbs),
  509. CODEC_ATTR_RW(hints),
  510. CODEC_ATTR_RO(init_pin_configs),
  511. CODEC_ATTR_RW(user_pin_configs),
  512. CODEC_ATTR_RO(driver_pin_configs),
  513. CODEC_ATTR_WO(reconfig),
  514. CODEC_ATTR_WO(clear),
  515. };
  516. /*
  517. * create sysfs files on hwdep directory
  518. */
  519. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  520. {
  521. struct snd_hwdep *hwdep = codec->hwdep;
  522. int i;
  523. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  524. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  525. hwdep->device, &codec_attrs[i]);
  526. return 0;
  527. }
  528. /*
  529. * Look for hint string
  530. */
  531. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  532. {
  533. struct hda_hint *hint = get_hint(codec, key);
  534. return hint ? hint->val : NULL;
  535. }
  536. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  537. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  538. {
  539. const char *p = snd_hda_get_hint(codec, key);
  540. if (!p || !*p)
  541. return -ENOENT;
  542. switch (toupper(*p)) {
  543. case 'T': /* true */
  544. case 'Y': /* yes */
  545. case '1':
  546. return 1;
  547. }
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  551. #endif /* CONFIG_SND_HDA_RECONFIG */
  552. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  553. /* parser mode */
  554. enum {
  555. LINE_MODE_NONE,
  556. LINE_MODE_CODEC,
  557. LINE_MODE_MODEL,
  558. LINE_MODE_PINCFG,
  559. LINE_MODE_VERB,
  560. LINE_MODE_HINT,
  561. LINE_MODE_VENDOR_ID,
  562. LINE_MODE_SUBSYSTEM_ID,
  563. LINE_MODE_REVISION_ID,
  564. LINE_MODE_CHIP_NAME,
  565. NUM_LINE_MODES,
  566. };
  567. static inline int strmatch(const char *a, const char *b)
  568. {
  569. return strnicmp(a, b, strlen(b)) == 0;
  570. }
  571. /* parse the contents after the line "[codec]"
  572. * accept only the line with three numbers, and assign the current codec
  573. */
  574. static void parse_codec_mode(char *buf, struct hda_bus *bus,
  575. struct hda_codec **codecp)
  576. {
  577. int vendorid, subid, caddr;
  578. struct hda_codec *codec;
  579. *codecp = NULL;
  580. if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
  581. list_for_each_entry(codec, &bus->codec_list, list) {
  582. if ((vendorid <= 0 || codec->vendor_id == vendorid) &&
  583. (subid <= 0 || codec->subsystem_id == subid) &&
  584. codec->addr == caddr) {
  585. *codecp = codec;
  586. break;
  587. }
  588. }
  589. }
  590. }
  591. /* parse the contents after the other command tags, [pincfg], [verb],
  592. * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
  593. * just pass to the sysfs helper (only when any codec was specified)
  594. */
  595. static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
  596. struct hda_codec **codecp)
  597. {
  598. parse_user_pin_configs(*codecp, buf);
  599. }
  600. static void parse_verb_mode(char *buf, struct hda_bus *bus,
  601. struct hda_codec **codecp)
  602. {
  603. parse_init_verbs(*codecp, buf);
  604. }
  605. static void parse_hint_mode(char *buf, struct hda_bus *bus,
  606. struct hda_codec **codecp)
  607. {
  608. parse_hints(*codecp, buf);
  609. }
  610. static void parse_model_mode(char *buf, struct hda_bus *bus,
  611. struct hda_codec **codecp)
  612. {
  613. kfree((*codecp)->modelname);
  614. (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
  615. }
  616. static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
  617. struct hda_codec **codecp)
  618. {
  619. kfree((*codecp)->chip_name);
  620. (*codecp)->chip_name = kstrdup(buf, GFP_KERNEL);
  621. }
  622. #define DEFINE_PARSE_ID_MODE(name) \
  623. static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
  624. struct hda_codec **codecp) \
  625. { \
  626. unsigned long val; \
  627. if (!strict_strtoul(buf, 0, &val)) \
  628. (*codecp)->name = val; \
  629. }
  630. DEFINE_PARSE_ID_MODE(vendor_id);
  631. DEFINE_PARSE_ID_MODE(subsystem_id);
  632. DEFINE_PARSE_ID_MODE(revision_id);
  633. struct hda_patch_item {
  634. const char *tag;
  635. void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
  636. int need_codec;
  637. };
  638. static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
  639. [LINE_MODE_CODEC] = { "[codec]", parse_codec_mode, 0 },
  640. [LINE_MODE_MODEL] = { "[model]", parse_model_mode, 1 },
  641. [LINE_MODE_VERB] = { "[verb]", parse_verb_mode, 1 },
  642. [LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode, 1 },
  643. [LINE_MODE_HINT] = { "[hint]", parse_hint_mode, 1 },
  644. [LINE_MODE_VENDOR_ID] = { "[vendor_id]", parse_vendor_id_mode, 1 },
  645. [LINE_MODE_SUBSYSTEM_ID] = { "[subsystem_id]", parse_subsystem_id_mode, 1 },
  646. [LINE_MODE_REVISION_ID] = { "[revision_id]", parse_revision_id_mode, 1 },
  647. [LINE_MODE_CHIP_NAME] = { "[chip_name]", parse_chip_name_mode, 1 },
  648. };
  649. /* check the line starting with '[' -- change the parser mode accodingly */
  650. static int parse_line_mode(char *buf, struct hda_bus *bus)
  651. {
  652. int i;
  653. for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
  654. if (!patch_items[i].tag)
  655. continue;
  656. if (strmatch(buf, patch_items[i].tag))
  657. return i;
  658. }
  659. return LINE_MODE_NONE;
  660. }
  661. /* copy one line from the buffer in fw, and update the fields in fw
  662. * return zero if it reaches to the end of the buffer, or non-zero
  663. * if successfully copied a line
  664. *
  665. * the spaces at the beginning and the end of the line are stripped
  666. */
  667. static int get_line_from_fw(char *buf, int size, struct firmware *fw)
  668. {
  669. int len;
  670. const char *p = fw->data;
  671. while (isspace(*p) && fw->size) {
  672. p++;
  673. fw->size--;
  674. }
  675. if (!fw->size)
  676. return 0;
  677. for (len = 0; len < fw->size; len++) {
  678. if (!*p)
  679. break;
  680. if (*p == '\n') {
  681. p++;
  682. len++;
  683. break;
  684. }
  685. if (len < size)
  686. *buf++ = *p++;
  687. }
  688. *buf = 0;
  689. fw->size -= len;
  690. fw->data = p;
  691. remove_trail_spaces(buf);
  692. return 1;
  693. }
  694. /*
  695. * load a "patch" firmware file and parse it
  696. */
  697. int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
  698. {
  699. int err;
  700. const struct firmware *fw;
  701. struct firmware tmp;
  702. char buf[128];
  703. struct hda_codec *codec;
  704. int line_mode;
  705. struct device *dev = bus->card->dev;
  706. if (snd_BUG_ON(!dev))
  707. return -ENODEV;
  708. err = request_firmware(&fw, patch, dev);
  709. if (err < 0) {
  710. printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
  711. patch);
  712. return err;
  713. }
  714. tmp = *fw;
  715. line_mode = LINE_MODE_NONE;
  716. codec = NULL;
  717. while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
  718. if (!*buf || *buf == '#' || *buf == '\n')
  719. continue;
  720. if (*buf == '[')
  721. line_mode = parse_line_mode(buf, bus);
  722. else if (patch_items[line_mode].parser &&
  723. (codec || !patch_items[line_mode].need_codec))
  724. patch_items[line_mode].parser(buf, bus, &codec);
  725. }
  726. release_firmware(fw);
  727. return 0;
  728. }
  729. EXPORT_SYMBOL_HDA(snd_hda_load_patch);
  730. #endif /* CONFIG_SND_HDA_PATCH_LOADER */