hda_hwdep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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_RECONFIG
  138. /*
  139. * sysfs interface
  140. */
  141. static int clear_codec(struct hda_codec *codec)
  142. {
  143. int err;
  144. err = snd_hda_codec_reset(codec);
  145. if (err < 0) {
  146. snd_printk(KERN_ERR "The codec is being used, can't free.\n");
  147. return err;
  148. }
  149. clear_hwdep_elements(codec);
  150. return 0;
  151. }
  152. static int reconfig_codec(struct hda_codec *codec)
  153. {
  154. int err;
  155. snd_hda_power_up(codec);
  156. snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
  157. err = snd_hda_codec_reset(codec);
  158. if (err < 0) {
  159. snd_printk(KERN_ERR
  160. "The codec is being used, can't reconfigure.\n");
  161. goto error;
  162. }
  163. err = snd_hda_codec_configure(codec);
  164. if (err < 0)
  165. goto error;
  166. /* rebuild PCMs */
  167. err = snd_hda_codec_build_pcms(codec);
  168. if (err < 0)
  169. goto error;
  170. /* rebuild mixers */
  171. err = snd_hda_codec_build_controls(codec);
  172. if (err < 0)
  173. goto error;
  174. err = snd_card_register(codec->bus->card);
  175. error:
  176. snd_hda_power_down(codec);
  177. return err;
  178. }
  179. /*
  180. * allocate a string at most len chars, and remove the trailing EOL
  181. */
  182. static char *kstrndup_noeol(const char *src, size_t len)
  183. {
  184. char *s = kstrndup(src, len, GFP_KERNEL);
  185. char *p;
  186. if (!s)
  187. return NULL;
  188. p = strchr(s, '\n');
  189. if (p)
  190. *p = 0;
  191. return s;
  192. }
  193. #define CODEC_INFO_SHOW(type) \
  194. static ssize_t type##_show(struct device *dev, \
  195. struct device_attribute *attr, \
  196. char *buf) \
  197. { \
  198. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  199. struct hda_codec *codec = hwdep->private_data; \
  200. return sprintf(buf, "0x%x\n", codec->type); \
  201. }
  202. #define CODEC_INFO_STR_SHOW(type) \
  203. static ssize_t type##_show(struct device *dev, \
  204. struct device_attribute *attr, \
  205. char *buf) \
  206. { \
  207. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  208. struct hda_codec *codec = hwdep->private_data; \
  209. return sprintf(buf, "%s\n", \
  210. codec->type ? codec->type : ""); \
  211. }
  212. CODEC_INFO_SHOW(vendor_id);
  213. CODEC_INFO_SHOW(subsystem_id);
  214. CODEC_INFO_SHOW(revision_id);
  215. CODEC_INFO_SHOW(afg);
  216. CODEC_INFO_SHOW(mfg);
  217. CODEC_INFO_STR_SHOW(vendor_name);
  218. CODEC_INFO_STR_SHOW(chip_name);
  219. CODEC_INFO_STR_SHOW(modelname);
  220. #define CODEC_INFO_STORE(type) \
  221. static ssize_t type##_store(struct device *dev, \
  222. struct device_attribute *attr, \
  223. const char *buf, size_t count) \
  224. { \
  225. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  226. struct hda_codec *codec = hwdep->private_data; \
  227. char *after; \
  228. codec->type = simple_strtoul(buf, &after, 0); \
  229. return count; \
  230. }
  231. #define CODEC_INFO_STR_STORE(type) \
  232. static ssize_t type##_store(struct device *dev, \
  233. struct device_attribute *attr, \
  234. const char *buf, size_t count) \
  235. { \
  236. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  237. struct hda_codec *codec = hwdep->private_data; \
  238. char *s = kstrndup_noeol(buf, 64); \
  239. if (!s) \
  240. return -ENOMEM; \
  241. kfree(codec->type); \
  242. codec->type = s; \
  243. return count; \
  244. }
  245. CODEC_INFO_STORE(vendor_id);
  246. CODEC_INFO_STORE(subsystem_id);
  247. CODEC_INFO_STORE(revision_id);
  248. CODEC_INFO_STR_STORE(vendor_name);
  249. CODEC_INFO_STR_STORE(chip_name);
  250. CODEC_INFO_STR_STORE(modelname);
  251. #define CODEC_ACTION_STORE(type) \
  252. static ssize_t type##_store(struct device *dev, \
  253. struct device_attribute *attr, \
  254. const char *buf, size_t count) \
  255. { \
  256. struct snd_hwdep *hwdep = dev_get_drvdata(dev); \
  257. struct hda_codec *codec = hwdep->private_data; \
  258. int err = 0; \
  259. if (*buf) \
  260. err = type##_codec(codec); \
  261. return err < 0 ? err : count; \
  262. }
  263. CODEC_ACTION_STORE(reconfig);
  264. CODEC_ACTION_STORE(clear);
  265. static ssize_t init_verbs_show(struct device *dev,
  266. struct device_attribute *attr,
  267. char *buf)
  268. {
  269. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  270. struct hda_codec *codec = hwdep->private_data;
  271. int i, len = 0;
  272. for (i = 0; i < codec->init_verbs.used; i++) {
  273. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  274. len += snprintf(buf + len, PAGE_SIZE - len,
  275. "0x%02x 0x%03x 0x%04x\n",
  276. v->nid, v->verb, v->param);
  277. }
  278. return len;
  279. }
  280. static int parse_init_verbs(struct hda_codec *codec, const char *buf)
  281. {
  282. struct hda_verb *v;
  283. int nid, verb, param;
  284. if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
  285. return -EINVAL;
  286. if (!nid || !verb)
  287. return -EINVAL;
  288. v = snd_array_new(&codec->init_verbs);
  289. if (!v)
  290. return -ENOMEM;
  291. v->nid = nid;
  292. v->verb = verb;
  293. v->param = param;
  294. return 0;
  295. }
  296. static ssize_t init_verbs_store(struct device *dev,
  297. struct device_attribute *attr,
  298. const char *buf, size_t count)
  299. {
  300. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  301. struct hda_codec *codec = hwdep->private_data;
  302. int err = parse_init_verbs(codec, buf);
  303. if (err < 0)
  304. return err;
  305. return count;
  306. }
  307. static ssize_t hints_show(struct device *dev,
  308. struct device_attribute *attr,
  309. char *buf)
  310. {
  311. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  312. struct hda_codec *codec = hwdep->private_data;
  313. int i, len = 0;
  314. for (i = 0; i < codec->hints.used; i++) {
  315. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  316. len += snprintf(buf + len, PAGE_SIZE - len,
  317. "%s = %s\n", hint->key, hint->val);
  318. }
  319. return len;
  320. }
  321. static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
  322. {
  323. int i;
  324. for (i = 0; i < codec->hints.used; i++) {
  325. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  326. if (!strcmp(hint->key, key))
  327. return hint;
  328. }
  329. return NULL;
  330. }
  331. static void remove_trail_spaces(char *str)
  332. {
  333. char *p;
  334. if (!*str)
  335. return;
  336. p = str + strlen(str) - 1;
  337. for (; isspace(*p); p--) {
  338. *p = 0;
  339. if (p == str)
  340. return;
  341. }
  342. }
  343. #define MAX_HINTS 1024
  344. static int parse_hints(struct hda_codec *codec, const char *buf)
  345. {
  346. char *key, *val;
  347. struct hda_hint *hint;
  348. while (isspace(*buf))
  349. buf++;
  350. if (!*buf || *buf == '#' || *buf == '\n')
  351. return 0;
  352. if (*buf == '=')
  353. return -EINVAL;
  354. key = kstrndup_noeol(buf, 1024);
  355. if (!key)
  356. return -ENOMEM;
  357. /* extract key and val */
  358. val = strchr(key, '=');
  359. if (!val) {
  360. kfree(key);
  361. return -EINVAL;
  362. }
  363. *val++ = 0;
  364. while (isspace(*val))
  365. val++;
  366. remove_trail_spaces(key);
  367. remove_trail_spaces(val);
  368. hint = get_hint(codec, key);
  369. if (hint) {
  370. /* replace */
  371. kfree(hint->key);
  372. hint->key = key;
  373. hint->val = val;
  374. return 0;
  375. }
  376. /* allocate a new hint entry */
  377. if (codec->hints.used >= MAX_HINTS)
  378. hint = NULL;
  379. else
  380. hint = snd_array_new(&codec->hints);
  381. if (!hint) {
  382. kfree(key);
  383. return -ENOMEM;
  384. }
  385. hint->key = key;
  386. hint->val = val;
  387. return 0;
  388. }
  389. static ssize_t hints_store(struct device *dev,
  390. struct device_attribute *attr,
  391. const char *buf, size_t count)
  392. {
  393. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  394. struct hda_codec *codec = hwdep->private_data;
  395. int err = parse_hints(codec, buf);
  396. if (err < 0)
  397. return err;
  398. return count;
  399. }
  400. static ssize_t pin_configs_show(struct hda_codec *codec,
  401. struct snd_array *list,
  402. char *buf)
  403. {
  404. int i, len = 0;
  405. for (i = 0; i < list->used; i++) {
  406. struct hda_pincfg *pin = snd_array_elem(list, i);
  407. len += sprintf(buf + len, "0x%02x 0x%08x\n",
  408. pin->nid, pin->cfg);
  409. }
  410. return len;
  411. }
  412. static ssize_t init_pin_configs_show(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  417. struct hda_codec *codec = hwdep->private_data;
  418. return pin_configs_show(codec, &codec->init_pins, buf);
  419. }
  420. static ssize_t user_pin_configs_show(struct device *dev,
  421. struct device_attribute *attr,
  422. char *buf)
  423. {
  424. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  425. struct hda_codec *codec = hwdep->private_data;
  426. return pin_configs_show(codec, &codec->user_pins, buf);
  427. }
  428. static ssize_t driver_pin_configs_show(struct device *dev,
  429. struct device_attribute *attr,
  430. char *buf)
  431. {
  432. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  433. struct hda_codec *codec = hwdep->private_data;
  434. return pin_configs_show(codec, &codec->driver_pins, buf);
  435. }
  436. #define MAX_PIN_CONFIGS 32
  437. static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
  438. {
  439. int nid, cfg;
  440. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  441. return -EINVAL;
  442. if (!nid)
  443. return -EINVAL;
  444. return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  445. }
  446. static ssize_t user_pin_configs_store(struct device *dev,
  447. struct device_attribute *attr,
  448. const char *buf, size_t count)
  449. {
  450. struct snd_hwdep *hwdep = dev_get_drvdata(dev);
  451. struct hda_codec *codec = hwdep->private_data;
  452. int err = parse_user_pin_configs(codec, buf);
  453. if (err < 0)
  454. return err;
  455. return count;
  456. }
  457. #define CODEC_ATTR_RW(type) \
  458. __ATTR(type, 0644, type##_show, type##_store)
  459. #define CODEC_ATTR_RO(type) \
  460. __ATTR_RO(type)
  461. #define CODEC_ATTR_WO(type) \
  462. __ATTR(type, 0200, NULL, type##_store)
  463. static struct device_attribute codec_attrs[] = {
  464. CODEC_ATTR_RW(vendor_id),
  465. CODEC_ATTR_RW(subsystem_id),
  466. CODEC_ATTR_RW(revision_id),
  467. CODEC_ATTR_RO(afg),
  468. CODEC_ATTR_RO(mfg),
  469. CODEC_ATTR_RW(vendor_name),
  470. CODEC_ATTR_RW(chip_name),
  471. CODEC_ATTR_RW(modelname),
  472. CODEC_ATTR_RW(init_verbs),
  473. CODEC_ATTR_RW(hints),
  474. CODEC_ATTR_RO(init_pin_configs),
  475. CODEC_ATTR_RW(user_pin_configs),
  476. CODEC_ATTR_RO(driver_pin_configs),
  477. CODEC_ATTR_WO(reconfig),
  478. CODEC_ATTR_WO(clear),
  479. };
  480. /*
  481. * create sysfs files on hwdep directory
  482. */
  483. int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
  484. {
  485. struct snd_hwdep *hwdep = codec->hwdep;
  486. int i;
  487. for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
  488. snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
  489. hwdep->device, &codec_attrs[i]);
  490. return 0;
  491. }
  492. /*
  493. * Look for hint string
  494. */
  495. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  496. {
  497. struct hda_hint *hint = get_hint(codec, key);
  498. return hint ? hint->val : NULL;
  499. }
  500. EXPORT_SYMBOL_HDA(snd_hda_get_hint);
  501. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  502. {
  503. const char *p = snd_hda_get_hint(codec, key);
  504. if (!p || !*p)
  505. return -ENOENT;
  506. switch (toupper(*p)) {
  507. case 'T': /* true */
  508. case 'Y': /* yes */
  509. case '1':
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);
  515. #endif /* CONFIG_SND_HDA_RECONFIG */
  516. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  517. /* parser mode */
  518. enum {
  519. LINE_MODE_NONE,
  520. LINE_MODE_CODEC,
  521. LINE_MODE_MODEL,
  522. LINE_MODE_PINCFG,
  523. LINE_MODE_VERB,
  524. LINE_MODE_HINT,
  525. NUM_LINE_MODES,
  526. };
  527. static inline int strmatch(const char *a, const char *b)
  528. {
  529. return strnicmp(a, b, strlen(b)) == 0;
  530. }
  531. /* parse the contents after the line "[codec]"
  532. * accept only the line with three numbers, and assign the current codec
  533. */
  534. static void parse_codec_mode(char *buf, struct hda_bus *bus,
  535. struct hda_codec **codecp)
  536. {
  537. unsigned int vendorid, subid, caddr;
  538. struct hda_codec *codec;
  539. *codecp = NULL;
  540. if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
  541. list_for_each_entry(codec, &bus->codec_list, list) {
  542. if (codec->addr == caddr) {
  543. *codecp = codec;
  544. break;
  545. }
  546. }
  547. }
  548. }
  549. /* parse the contents after the other command tags, [pincfg], [verb],
  550. * [hint] and [model]
  551. * just pass to the sysfs helper (only when any codec was specified)
  552. */
  553. static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
  554. struct hda_codec **codecp)
  555. {
  556. if (!*codecp)
  557. return;
  558. parse_user_pin_configs(*codecp, buf);
  559. }
  560. static void parse_verb_mode(char *buf, struct hda_bus *bus,
  561. struct hda_codec **codecp)
  562. {
  563. if (!*codecp)
  564. return;
  565. parse_init_verbs(*codecp, buf);
  566. }
  567. static void parse_hint_mode(char *buf, struct hda_bus *bus,
  568. struct hda_codec **codecp)
  569. {
  570. if (!*codecp)
  571. return;
  572. parse_hints(*codecp, buf);
  573. }
  574. static void parse_model_mode(char *buf, struct hda_bus *bus,
  575. struct hda_codec **codecp)
  576. {
  577. if (!*codecp)
  578. return;
  579. kfree((*codecp)->modelname);
  580. (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
  581. }
  582. struct hda_patch_item {
  583. const char *tag;
  584. void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
  585. };
  586. static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
  587. [LINE_MODE_CODEC] = { "[codec]", parse_codec_mode },
  588. [LINE_MODE_MODEL] = { "[model]", parse_model_mode },
  589. [LINE_MODE_VERB] = { "[verb]", parse_verb_mode },
  590. [LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode },
  591. [LINE_MODE_HINT] = { "[hint]", parse_hint_mode },
  592. };
  593. /* check the line starting with '[' -- change the parser mode accodingly */
  594. static int parse_line_mode(char *buf, struct hda_bus *bus)
  595. {
  596. int i;
  597. for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
  598. if (!patch_items[i].tag)
  599. continue;
  600. if (strmatch(buf, patch_items[i].tag))
  601. return i;
  602. }
  603. return LINE_MODE_NONE;
  604. }
  605. /* copy one line from the buffer in fw, and update the fields in fw
  606. * return zero if it reaches to the end of the buffer, or non-zero
  607. * if successfully copied a line
  608. *
  609. * the spaces at the beginning and the end of the line are stripped
  610. */
  611. static int get_line_from_fw(char *buf, int size, struct firmware *fw)
  612. {
  613. int len;
  614. const char *p = fw->data;
  615. while (isspace(*p) && fw->size) {
  616. p++;
  617. fw->size--;
  618. }
  619. if (!fw->size)
  620. return 0;
  621. if (size < fw->size)
  622. size = fw->size;
  623. for (len = 0; len < fw->size; len++) {
  624. if (!*p)
  625. break;
  626. if (*p == '\n') {
  627. p++;
  628. len++;
  629. break;
  630. }
  631. if (len < size)
  632. *buf++ = *p++;
  633. }
  634. *buf = 0;
  635. fw->size -= len;
  636. fw->data = p;
  637. remove_trail_spaces(buf);
  638. return 1;
  639. }
  640. /*
  641. * load a "patch" firmware file and parse it
  642. */
  643. int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
  644. {
  645. int err;
  646. const struct firmware *fw;
  647. struct firmware tmp;
  648. char buf[128];
  649. struct hda_codec *codec;
  650. int line_mode;
  651. struct device *dev = bus->card->dev;
  652. if (snd_BUG_ON(!dev))
  653. return -ENODEV;
  654. err = request_firmware(&fw, patch, dev);
  655. if (err < 0) {
  656. printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
  657. patch);
  658. return err;
  659. }
  660. tmp = *fw;
  661. line_mode = LINE_MODE_NONE;
  662. codec = NULL;
  663. while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
  664. if (!*buf || *buf == '#' || *buf == '\n')
  665. continue;
  666. if (*buf == '[')
  667. line_mode = parse_line_mode(buf, bus);
  668. else if (patch_items[line_mode].parser)
  669. patch_items[line_mode].parser(buf, bus, &codec);
  670. }
  671. release_firmware(fw);
  672. return 0;
  673. }
  674. EXPORT_SYMBOL_HDA(snd_hda_load_patch);
  675. #endif /* CONFIG_SND_HDA_PATCH_LOADER */