hda_hwdep.c 21 KB

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