hda_hwdep.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. char *after; \
  262. codec->type = simple_strtoul(buf, &after, 0); \
  263. return count; \
  264. }
  265. #define CODEC_INFO_STR_STORE(type) \
  266. static ssize_t type##_store(struct device *dev, \
  267. struct device_attribute *attr, \
  268. const char *buf, size_t count) \
  269. { \
  270. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  271. struct hda_codec *codec = hwdep->private_data; \
  272. char *s = kstrndup_noeol(buf, 64); \
  273. if (!s) \
  274. return -ENOMEM; \
  275. kfree(codec->type); \
  276. codec->type = s; \
  277. return count; \
  278. }
  279. CODEC_INFO_STORE(vendor_id);
  280. CODEC_INFO_STORE(subsystem_id);
  281. CODEC_INFO_STORE(revision_id);
  282. CODEC_INFO_STR_STORE(vendor_name);
  283. CODEC_INFO_STR_STORE(chip_name);
  284. CODEC_INFO_STR_STORE(modelname);
  285. #define CODEC_ACTION_STORE(type) \
  286. static ssize_t type##_store(struct device *dev, \
  287. struct device_attribute *attr, \
  288. const char *buf, size_t count) \
  289. { \
  290. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  291. struct hda_codec *codec = hwdep->private_data; \
  292. int err = 0; \
  293. if (*buf) \
  294. err = type##_codec(codec); \
  295. return err < 0 ? err : count; \
  296. }
  297. CODEC_ACTION_STORE(reconfig);
  298. CODEC_ACTION_STORE(clear);
  299. static ssize_t init_verbs_show(struct device *dev,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  304. struct hda_codec *codec = hwdep->private_data;
  305. int i, len = 0;
  306. for (i = 0; i < codec->init_verbs.used; i++) {
  307. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  308. len += snprintf(buf + len, PAGE_SIZE - len,
  309. "0x%02x 0x%03x 0x%04x\n",
  310. v->nid, v->verb, v->param);
  311. }
  312. return len;
  313. }
  314. static int parse_init_verbs(struct hda_codec *codec, const char *buf)
  315. {
  316. struct hda_verb *v;
  317. int nid, verb, param;
  318. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  319. return -EINVAL;
  320. if (!nid || !verb)
  321. return -EINVAL;
  322. v = snd_array_new(&codec->init_verbs);
  323. if (!v)
  324. return -ENOMEM;
  325. v->nid = nid;
  326. v->verb = verb;
  327. v->param = param;
  328. return 0;
  329. }
  330. static ssize_t init_verbs_store(struct device *dev,
  331. struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  335. struct hda_codec *codec = hwdep->private_data;
  336. int err = parse_init_verbs(codec, buf);
  337. if (err < 0)
  338. return err;
  339. return count;
  340. }
  341. static ssize_t hints_show(struct device *dev,
  342. struct device_attribute *attr,
  343. char *buf)
  344. {
  345. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  346. struct hda_codec *codec = hwdep->private_data;
  347. int i, len = 0;
  348. for (i = 0; i < codec->hints.used; i++) {
  349. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  350. len += snprintf(buf + len, PAGE_SIZE - len,
  351. "%s = %s\n", hint->key, hint->val);
  352. }
  353. return len;
  354. }
  355. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  356. {
  357. int i;
  358. for (i = 0; i < codec->hints.used; i++) {
  359. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  360. if (!strcmp(hint->key, key))
  361. return hint;
  362. }
  363. return NULL;
  364. }
  365. static void remove_trail_spaces(char *str)
  366. {
  367. char *p;
  368. if (!*str)
  369. return;
  370. p = str + strlen(str) - 1;
  371. for (; isspace(*p); p--) {
  372. *p = 0;
  373. if (p == str)
  374. return;
  375. }
  376. }
  377. #define MAX_HINTS 1024
  378. static int parse_hints(struct hda_codec *codec, const char *buf)
  379. {
  380. char *key, *val;
  381. struct hda_hint *hint;
  382. buf = skip_spaces(buf);
  383. if (!*buf || *buf == '#' || *buf == '\n')
  384. return 0;
  385. if (*buf == '=')
  386. return -EINVAL;
  387. key = kstrndup_noeol(buf, 1024);
  388. if (!key)
  389. return -ENOMEM;
  390. /* extract key and val */
  391. val = strchr(key, '=');
  392. if (!val) {
  393. kfree(key);
  394. return -EINVAL;
  395. }
  396. *val++ = 0;
  397. val = skip_spaces(val);
  398. remove_trail_spaces(key);
  399. remove_trail_spaces(val);
  400. hint = get_hint(codec, key);
  401. if (hint) {
  402. /* replace */
  403. kfree(hint->key);
  404. hint->key = key;
  405. hint->val = val;
  406. return 0;
  407. }
  408. /* allocate a new hint entry */
  409. if (codec->hints.used >= MAX_HINTS)
  410. hint = NULL;
  411. else
  412. hint = snd_array_new(&codec->hints);
  413. if (!hint) {
  414. kfree(key);
  415. return -ENOMEM;
  416. }
  417. hint->key = key;
  418. hint->val = val;
  419. return 0;
  420. }
  421. static ssize_t hints_store(struct device *dev,
  422. struct device_attribute *attr,
  423. const char *buf, size_t count)
  424. {
  425. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  426. struct hda_codec *codec = hwdep->private_data;
  427. int err = parse_hints(codec, buf);
  428. if (err < 0)
  429. return err;
  430. return count;
  431. }
  432. static ssize_t pin_configs_show(struct hda_codec *codec,
  433. struct snd_array *list,
  434. char *buf)
  435. {
  436. int i, len = 0;
  437. for (i = 0; i < list->used; i++) {
  438. struct hda_pincfg *pin = snd_array_elem(list, i);
  439. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  440. pin->nid, pin->cfg);
  441. }
  442. return len;
  443. }
  444. static ssize_t init_pin_configs_show(struct device *dev,
  445. struct device_attribute *attr,
  446. char *buf)
  447. {
  448. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  449. struct hda_codec *codec = hwdep->private_data;
  450. return pin_configs_show(codec, &codec->init_pins, buf);
  451. }
  452. static ssize_t user_pin_configs_show(struct device *dev,
  453. struct device_attribute *attr,
  454. char *buf)
  455. {
  456. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  457. struct hda_codec *codec = hwdep->private_data;
  458. return pin_configs_show(codec, &codec->user_pins, buf);
  459. }
  460. static ssize_t driver_pin_configs_show(struct device *dev,
  461. struct device_attribute *attr,
  462. char *buf)
  463. {
  464. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  465. struct hda_codec *codec = hwdep->private_data;
  466. return pin_configs_show(codec, &codec->driver_pins, buf);
  467. }
  468. #define MAX_PIN_CONFIGS 32
  469. static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
  470. {
  471. int nid, cfg;
  472. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  473. return -EINVAL;
  474. if (!nid)
  475. return -EINVAL;
  476. return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  477. }
  478. static ssize_t user_pin_configs_store(struct device *dev,
  479. struct device_attribute *attr,
  480. const char *buf, size_t count)
  481. {
  482. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  483. struct hda_codec *codec = hwdep->private_data;
  484. int err = parse_user_pin_configs(codec, buf);
  485. if (err < 0)
  486. return err;
  487. return count;
  488. }
  489. #define CODEC_ATTR_RW(type) \
  490. __ATTR(type, 0644, type##_show, type##_store)
  491. #define CODEC_ATTR_RO(type) \
  492. __ATTR_RO(type)
  493. #define CODEC_ATTR_WO(type) \
  494. __ATTR(type, 0200, NULL, type##_store)
  495. static struct device_attribute codec_attrs[] = {
  496. CODEC_ATTR_RW(vendor_id),
  497. CODEC_ATTR_RW(subsystem_id),
  498. CODEC_ATTR_RW(revision_id),
  499. CODEC_ATTR_RO(afg),
  500. CODEC_ATTR_RO(mfg),
  501. CODEC_ATTR_RW(vendor_name),
  502. CODEC_ATTR_RW(chip_name),
  503. CODEC_ATTR_RW(modelname),
  504. CODEC_ATTR_RW(init_verbs),
  505. CODEC_ATTR_RW(hints),
  506. CODEC_ATTR_RO(init_pin_configs),
  507. CODEC_ATTR_RW(user_pin_configs),
  508. CODEC_ATTR_RO(driver_pin_configs),
  509. CODEC_ATTR_WO(reconfig),
  510. CODEC_ATTR_WO(clear),
  511. };
  512. /*
  513. * create sysfs files on hwdep directory
  514. */
  515. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  516. {
  517. struct snd_hwdep *hwdep = codec->hwdep;
  518. int i;
  519. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  520. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  521. hwdep->device, &codec_attrs[i]);
  522. return 0;
  523. }
  524. /*
  525. * Look for hint string
  526. */
  527. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  528. {
  529. struct hda_hint *hint = get_hint(codec, key);
  530. return hint ? hint->val : NULL;
  531. }
  532. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  533. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  534. {
  535. const char *p = snd_hda_get_hint(codec, key);
  536. if (!p || !*p)
  537. return -ENOENT;
  538. switch (toupper(*p)) {
  539. case 'T': /* true */
  540. case 'Y': /* yes */
  541. case '1':
  542. return 1;
  543. }
  544. return 0;
  545. }
  546. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  547. #endif /* CONFIG_SND_HDA_RECONFIG */
  548. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  549. /* parser mode */
  550. enum {
  551. LINE_MODE_NONE,
  552. LINE_MODE_CODEC,
  553. LINE_MODE_MODEL,
  554. LINE_MODE_PINCFG,
  555. LINE_MODE_VERB,
  556. LINE_MODE_HINT,
  557. NUM_LINE_MODES,
  558. };
  559. static inline int strmatch(const char *a, const char *b)
  560. {
  561. return strnicmp(a, b, strlen(b)) == 0;
  562. }
  563. /* parse the contents after the line "[codec]"
  564. * accept only the line with three numbers, and assign the current codec
  565. */
  566. static void parse_codec_mode(char *buf, struct hda_bus *bus,
  567. struct hda_codec **codecp)
  568. {
  569. unsigned int vendorid, subid, caddr;
  570. struct hda_codec *codec;
  571. *codecp = NULL;
  572. if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
  573. list_for_each_entry(codec, &bus->codec_list, list) {
  574. if (codec->addr == caddr) {
  575. *codecp = codec;
  576. break;
  577. }
  578. }
  579. }
  580. }
  581. /* parse the contents after the other command tags, [pincfg], [verb],
  582. * [hint] and [model]
  583. * just pass to the sysfs helper (only when any codec was specified)
  584. */
  585. static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
  586. struct hda_codec **codecp)
  587. {
  588. if (!*codecp)
  589. return;
  590. parse_user_pin_configs(*codecp, buf);
  591. }
  592. static void parse_verb_mode(char *buf, struct hda_bus *bus,
  593. struct hda_codec **codecp)
  594. {
  595. if (!*codecp)
  596. return;
  597. parse_init_verbs(*codecp, buf);
  598. }
  599. static void parse_hint_mode(char *buf, struct hda_bus *bus,
  600. struct hda_codec **codecp)
  601. {
  602. if (!*codecp)
  603. return;
  604. parse_hints(*codecp, buf);
  605. }
  606. static void parse_model_mode(char *buf, struct hda_bus *bus,
  607. struct hda_codec **codecp)
  608. {
  609. if (!*codecp)
  610. return;
  611. kfree((*codecp)->modelname);
  612. (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
  613. }
  614. struct hda_patch_item {
  615. const char *tag;
  616. void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
  617. };
  618. static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
  619. [LINE_MODE_CODEC] = { "[codec]", parse_codec_mode },
  620. [LINE_MODE_MODEL] = { "[model]", parse_model_mode },
  621. [LINE_MODE_VERB] = { "[verb]", parse_verb_mode },
  622. [LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode },
  623. [LINE_MODE_HINT] = { "[hint]", parse_hint_mode },
  624. };
  625. /* check the line starting with '[' -- change the parser mode accodingly */
  626. static int parse_line_mode(char *buf, struct hda_bus *bus)
  627. {
  628. int i;
  629. for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
  630. if (!patch_items[i].tag)
  631. continue;
  632. if (strmatch(buf, patch_items[i].tag))
  633. return i;
  634. }
  635. return LINE_MODE_NONE;
  636. }
  637. /* copy one line from the buffer in fw, and update the fields in fw
  638. * return zero if it reaches to the end of the buffer, or non-zero
  639. * if successfully copied a line
  640. *
  641. * the spaces at the beginning and the end of the line are stripped
  642. */
  643. static int get_line_from_fw(char *buf, int size, struct firmware *fw)
  644. {
  645. int len;
  646. const char *p = fw->data;
  647. while (isspace(*p) && fw->size) {
  648. p++;
  649. fw->size--;
  650. }
  651. if (!fw->size)
  652. return 0;
  653. if (size < fw->size)
  654. size = fw->size;
  655. for (len = 0; len < fw->size; len++) {
  656. if (!*p)
  657. break;
  658. if (*p == '\n') {
  659. p++;
  660. len++;
  661. break;
  662. }
  663. if (len < size)
  664. *buf++ = *p++;
  665. }
  666. *buf = 0;
  667. fw->size -= len;
  668. fw->data = p;
  669. remove_trail_spaces(buf);
  670. return 1;
  671. }
  672. /*
  673. * load a "patch" firmware file and parse it
  674. */
  675. int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
  676. {
  677. int err;
  678. const struct firmware *fw;
  679. struct firmware tmp;
  680. char buf[128];
  681. struct hda_codec *codec;
  682. int line_mode;
  683. struct device *dev = bus->card->dev;
  684. if (snd_BUG_ON(!dev))
  685. return -ENODEV;
  686. err = request_firmware(&fw, patch, dev);
  687. if (err < 0) {
  688. printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
  689. patch);
  690. return err;
  691. }
  692. tmp = *fw;
  693. line_mode = LINE_MODE_NONE;
  694. codec = NULL;
  695. while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
  696. if (!*buf || *buf == '#' || *buf == '\n')
  697. continue;
  698. if (*buf == '[')
  699. line_mode = parse_line_mode(buf, bus);
  700. else if (patch_items[line_mode].parser)
  701. patch_items[line_mode].parser(buf, bus, &codec);
  702. }
  703. release_firmware(fw);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_HDA(snd_hda_load_patch);
  707. #endif /* CONFIG_SND_HDA_PATCH_LOADER */