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