hda_sysfs.c 19 KB

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