hda_hwdep.c 19 KB

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