hda_sysfs.c 19 KB

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