hda_sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. err = snd_card_register(codec->card);
  128. error:
  129. snd_hda_power_down(codec);
  130. return err;
  131. }
  132. /*
  133. * allocate a string at most len chars, and remove the trailing EOL
  134. */
  135. static char *kstrndup_noeol(const char *src, size_t len)
  136. {
  137. char *s = kstrndup(src, len, GFP_KERNEL);
  138. char *p;
  139. if (!s)
  140. return NULL;
  141. p = strchr(s, '\n');
  142. if (p)
  143. *p = 0;
  144. return s;
  145. }
  146. #define CODEC_INFO_STORE(type, field) \
  147. static ssize_t type##_store(struct device *dev, \
  148. struct device_attribute *attr, \
  149. const char *buf, size_t count) \
  150. { \
  151. struct hda_codec *codec = dev_get_drvdata(dev); \
  152. unsigned long val; \
  153. int err = kstrtoul(buf, 0, &val); \
  154. if (err < 0) \
  155. return err; \
  156. codec->field = val; \
  157. return count; \
  158. }
  159. #define CODEC_INFO_STR_STORE(type, field) \
  160. static ssize_t type##_store(struct device *dev, \
  161. struct device_attribute *attr, \
  162. const char *buf, size_t count) \
  163. { \
  164. struct hda_codec *codec = dev_get_drvdata(dev); \
  165. char *s = kstrndup_noeol(buf, 64); \
  166. if (!s) \
  167. return -ENOMEM; \
  168. kfree(codec->field); \
  169. codec->field = s; \
  170. return count; \
  171. }
  172. CODEC_INFO_STORE(vendor_id, core.vendor_id);
  173. CODEC_INFO_STORE(subsystem_id, core.subsystem_id);
  174. CODEC_INFO_STORE(revision_id, core.revision_id);
  175. CODEC_INFO_STR_STORE(vendor_name, core.vendor_name);
  176. CODEC_INFO_STR_STORE(chip_name, core.chip_name);
  177. CODEC_INFO_STR_STORE(modelname, modelname);
  178. #define CODEC_ACTION_STORE(type) \
  179. static ssize_t type##_store(struct device *dev, \
  180. struct device_attribute *attr, \
  181. const char *buf, size_t count) \
  182. { \
  183. struct hda_codec *codec = dev_get_drvdata(dev); \
  184. int err = 0; \
  185. if (*buf) \
  186. err = type##_codec(codec); \
  187. return err < 0 ? err : count; \
  188. }
  189. CODEC_ACTION_STORE(reconfig);
  190. CODEC_ACTION_STORE(clear);
  191. static ssize_t init_verbs_show(struct device *dev,
  192. struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct hda_codec *codec = dev_get_drvdata(dev);
  196. int i, len = 0;
  197. mutex_lock(&codec->user_mutex);
  198. for (i = 0; i < codec->init_verbs.used; i++) {
  199. struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
  200. len += scnprintf(buf + len, PAGE_SIZE - len,
  201. "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. int i, len = 0;
  243. mutex_lock(&codec->user_mutex);
  244. for (i = 0; i < codec->hints.used; i++) {
  245. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  246. len += scnprintf(buf + len, PAGE_SIZE - len,
  247. "%s = %s\n", 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. int i;
  255. for (i = 0; i < codec->hints.used; i++) {
  256. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  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. #define MAX_PIN_CONFIGS 32
  342. static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
  343. {
  344. int nid, cfg, err;
  345. if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
  346. return -EINVAL;
  347. if (!nid)
  348. return -EINVAL;
  349. mutex_lock(&codec->user_mutex);
  350. err = snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
  351. mutex_unlock(&codec->user_mutex);
  352. return err;
  353. }
  354. static ssize_t user_pin_configs_store(struct device *dev,
  355. struct device_attribute *attr,
  356. const char *buf, size_t count)
  357. {
  358. struct hda_codec *codec = dev_get_drvdata(dev);
  359. int err = parse_user_pin_configs(codec, buf);
  360. if (err < 0)
  361. return err;
  362. return count;
  363. }
  364. /* sysfs attributes exposed only when CONFIG_SND_HDA_RECONFIG=y */
  365. static DEVICE_ATTR_RW(init_verbs);
  366. static DEVICE_ATTR_RW(hints);
  367. static DEVICE_ATTR_RW(user_pin_configs);
  368. static DEVICE_ATTR_WO(reconfig);
  369. static DEVICE_ATTR_WO(clear);
  370. /**
  371. * snd_hda_get_hint - Look for hint string
  372. * @codec: the HDA codec
  373. * @key: the hint key string
  374. *
  375. * Look for a hint key/value pair matching with the given key string
  376. * and returns the value string. If nothing found, returns NULL.
  377. */
  378. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  379. {
  380. struct hda_hint *hint = get_hint(codec, key);
  381. return hint ? hint->val : NULL;
  382. }
  383. EXPORT_SYMBOL_GPL(snd_hda_get_hint);
  384. /**
  385. * snd_hda_get_bool_hint - Get a boolean hint value
  386. * @codec: the HDA codec
  387. * @key: the hint key string
  388. *
  389. * Look for a hint key/value pair matching with the given key string
  390. * and returns a boolean value parsed from the value. If no matching
  391. * key is found, return a negative value.
  392. */
  393. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  394. {
  395. const char *p;
  396. int ret;
  397. mutex_lock(&codec->user_mutex);
  398. p = snd_hda_get_hint(codec, key);
  399. if (!p || !*p)
  400. ret = -ENOENT;
  401. else {
  402. switch (toupper(*p)) {
  403. case 'T': /* true */
  404. case 'Y': /* yes */
  405. case '1':
  406. ret = 1;
  407. break;
  408. default:
  409. ret = 0;
  410. break;
  411. }
  412. }
  413. mutex_unlock(&codec->user_mutex);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint);
  417. /**
  418. * snd_hda_get_int_hint - Get an integer hint value
  419. * @codec: the HDA codec
  420. * @key: the hint key string
  421. * @valp: pointer to store a value
  422. *
  423. * Look for a hint key/value pair matching with the given key string
  424. * and stores the integer value to @valp. If no matching key is found,
  425. * return a negative error code. Otherwise it returns zero.
  426. */
  427. int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
  428. {
  429. const char *p;
  430. unsigned long val;
  431. int ret;
  432. mutex_lock(&codec->user_mutex);
  433. p = snd_hda_get_hint(codec, key);
  434. if (!p)
  435. ret = -ENOENT;
  436. else if (kstrtoul(p, 0, &val))
  437. ret = -EINVAL;
  438. else {
  439. *valp = val;
  440. ret = 0;
  441. }
  442. mutex_unlock(&codec->user_mutex);
  443. return ret;
  444. }
  445. EXPORT_SYMBOL_GPL(snd_hda_get_int_hint);
  446. #endif /* CONFIG_SND_HDA_RECONFIG */
  447. /*
  448. * common sysfs attributes
  449. */
  450. #ifdef CONFIG_SND_HDA_RECONFIG
  451. #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
  452. #else
  453. #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
  454. #endif
  455. static RECONFIG_DEVICE_ATTR(vendor_id);
  456. static RECONFIG_DEVICE_ATTR(subsystem_id);
  457. static RECONFIG_DEVICE_ATTR(revision_id);
  458. static DEVICE_ATTR_RO(afg);
  459. static DEVICE_ATTR_RO(mfg);
  460. static RECONFIG_DEVICE_ATTR(vendor_name);
  461. static RECONFIG_DEVICE_ATTR(chip_name);
  462. static RECONFIG_DEVICE_ATTR(modelname);
  463. static DEVICE_ATTR_RO(init_pin_configs);
  464. static DEVICE_ATTR_RO(driver_pin_configs);
  465. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  466. /* parser mode */
  467. enum {
  468. LINE_MODE_NONE,
  469. LINE_MODE_CODEC,
  470. LINE_MODE_MODEL,
  471. LINE_MODE_PINCFG,
  472. LINE_MODE_VERB,
  473. LINE_MODE_HINT,
  474. LINE_MODE_VENDOR_ID,
  475. LINE_MODE_SUBSYSTEM_ID,
  476. LINE_MODE_REVISION_ID,
  477. LINE_MODE_CHIP_NAME,
  478. NUM_LINE_MODES,
  479. };
  480. static inline int strmatch(const char *a, const char *b)
  481. {
  482. return strncasecmp(a, b, strlen(b)) == 0;
  483. }
  484. /* parse the contents after the line "[codec]"
  485. * accept only the line with three numbers, and assign the current codec
  486. */
  487. static void parse_codec_mode(char *buf, struct hda_bus *bus,
  488. struct hda_codec **codecp)
  489. {
  490. int vendorid, subid, caddr;
  491. struct hda_codec *codec;
  492. *codecp = NULL;
  493. if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
  494. list_for_each_codec(codec, bus) {
  495. if ((vendorid <= 0 || codec->core.vendor_id == vendorid) &&
  496. (subid <= 0 || codec->core.subsystem_id == subid) &&
  497. codec->core.addr == caddr) {
  498. *codecp = codec;
  499. break;
  500. }
  501. }
  502. }
  503. }
  504. /* parse the contents after the other command tags, [pincfg], [verb],
  505. * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
  506. * just pass to the sysfs helper (only when any codec was specified)
  507. */
  508. static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
  509. struct hda_codec **codecp)
  510. {
  511. parse_user_pin_configs(*codecp, buf);
  512. }
  513. static void parse_verb_mode(char *buf, struct hda_bus *bus,
  514. struct hda_codec **codecp)
  515. {
  516. parse_init_verbs(*codecp, buf);
  517. }
  518. static void parse_hint_mode(char *buf, struct hda_bus *bus,
  519. struct hda_codec **codecp)
  520. {
  521. parse_hints(*codecp, buf);
  522. }
  523. static void parse_model_mode(char *buf, struct hda_bus *bus,
  524. struct hda_codec **codecp)
  525. {
  526. kfree((*codecp)->modelname);
  527. (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
  528. }
  529. static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
  530. struct hda_codec **codecp)
  531. {
  532. snd_hda_codec_set_name(*codecp, buf);
  533. }
  534. #define DEFINE_PARSE_ID_MODE(name) \
  535. static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
  536. struct hda_codec **codecp) \
  537. { \
  538. unsigned long val; \
  539. if (!kstrtoul(buf, 0, &val)) \
  540. (*codecp)->core.name = val; \
  541. }
  542. DEFINE_PARSE_ID_MODE(vendor_id);
  543. DEFINE_PARSE_ID_MODE(subsystem_id);
  544. DEFINE_PARSE_ID_MODE(revision_id);
  545. struct hda_patch_item {
  546. const char *tag;
  547. const char *alias;
  548. void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
  549. };
  550. static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
  551. [LINE_MODE_CODEC] = {
  552. .tag = "[codec]",
  553. .parser = parse_codec_mode,
  554. },
  555. [LINE_MODE_MODEL] = {
  556. .tag = "[model]",
  557. .parser = parse_model_mode,
  558. },
  559. [LINE_MODE_VERB] = {
  560. .tag = "[verb]",
  561. .alias = "[init_verbs]",
  562. .parser = parse_verb_mode,
  563. },
  564. [LINE_MODE_PINCFG] = {
  565. .tag = "[pincfg]",
  566. .alias = "[user_pin_configs]",
  567. .parser = parse_pincfg_mode,
  568. },
  569. [LINE_MODE_HINT] = {
  570. .tag = "[hint]",
  571. .alias = "[hints]",
  572. .parser = parse_hint_mode
  573. },
  574. [LINE_MODE_VENDOR_ID] = {
  575. .tag = "[vendor_id]",
  576. .parser = parse_vendor_id_mode,
  577. },
  578. [LINE_MODE_SUBSYSTEM_ID] = {
  579. .tag = "[subsystem_id]",
  580. .parser = parse_subsystem_id_mode,
  581. },
  582. [LINE_MODE_REVISION_ID] = {
  583. .tag = "[revision_id]",
  584. .parser = parse_revision_id_mode,
  585. },
  586. [LINE_MODE_CHIP_NAME] = {
  587. .tag = "[chip_name]",
  588. .parser = parse_chip_name_mode,
  589. },
  590. };
  591. /* check the line starting with '[' -- change the parser mode accodingly */
  592. static int parse_line_mode(char *buf, struct hda_bus *bus)
  593. {
  594. int i;
  595. for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
  596. if (!patch_items[i].tag)
  597. continue;
  598. if (strmatch(buf, patch_items[i].tag))
  599. return i;
  600. if (patch_items[i].alias && strmatch(buf, patch_items[i].alias))
  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, size_t *fw_size_p,
  612. const void **fw_data_p)
  613. {
  614. int len;
  615. size_t fw_size = *fw_size_p;
  616. const char *p = *fw_data_p;
  617. while (isspace(*p) && fw_size) {
  618. p++;
  619. fw_size--;
  620. }
  621. if (!fw_size)
  622. return 0;
  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_p = fw_size - len;
  636. *fw_data_p = p;
  637. remove_trail_spaces(buf);
  638. return 1;
  639. }
  640. /**
  641. * snd_hda_load_patch - load a "patch" firmware file and parse it
  642. * @bus: HD-audio bus
  643. * @fw_size: the firmware byte size
  644. * @fw_buf: the firmware data
  645. */
  646. int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
  647. {
  648. char buf[128];
  649. struct hda_codec *codec;
  650. int line_mode;
  651. line_mode = LINE_MODE_NONE;
  652. codec = NULL;
  653. while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) {
  654. if (!*buf || *buf == '#' || *buf == '\n')
  655. continue;
  656. if (*buf == '[')
  657. line_mode = parse_line_mode(buf, bus);
  658. else if (patch_items[line_mode].parser &&
  659. (codec || line_mode <= LINE_MODE_CODEC))
  660. patch_items[line_mode].parser(buf, bus, &codec);
  661. }
  662. return 0;
  663. }
  664. EXPORT_SYMBOL_GPL(snd_hda_load_patch);
  665. #endif /* CONFIG_SND_HDA_PATCH_LOADER */
  666. /*
  667. * sysfs entries
  668. */
  669. static struct attribute *hda_dev_attrs[] = {
  670. &dev_attr_vendor_id.attr,
  671. &dev_attr_subsystem_id.attr,
  672. &dev_attr_revision_id.attr,
  673. &dev_attr_afg.attr,
  674. &dev_attr_mfg.attr,
  675. &dev_attr_vendor_name.attr,
  676. &dev_attr_chip_name.attr,
  677. &dev_attr_modelname.attr,
  678. &dev_attr_init_pin_configs.attr,
  679. &dev_attr_driver_pin_configs.attr,
  680. #ifdef CONFIG_PM
  681. &dev_attr_power_on_acct.attr,
  682. &dev_attr_power_off_acct.attr,
  683. #endif
  684. #ifdef CONFIG_SND_HDA_RECONFIG
  685. &dev_attr_init_verbs.attr,
  686. &dev_attr_hints.attr,
  687. &dev_attr_user_pin_configs.attr,
  688. &dev_attr_reconfig.attr,
  689. &dev_attr_clear.attr,
  690. #endif
  691. NULL
  692. };
  693. static const struct attribute_group hda_dev_attr_group = {
  694. .attrs = hda_dev_attrs,
  695. };
  696. const struct attribute_group *snd_hda_dev_attr_groups[] = {
  697. &hda_dev_attr_group,
  698. NULL
  699. };
  700. void snd_hda_sysfs_init(struct hda_codec *codec)
  701. {
  702. mutex_init(&codec->user_mutex);
  703. #ifdef CONFIG_SND_HDA_RECONFIG
  704. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  705. snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
  706. snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
  707. #endif
  708. }
  709. void snd_hda_sysfs_clear(struct hda_codec *codec)
  710. {
  711. #ifdef CONFIG_SND_HDA_RECONFIG
  712. int i;
  713. /* clear init verbs */
  714. snd_array_free(&codec->init_verbs);
  715. /* clear hints */
  716. for (i = 0; i < codec->hints.used; i++) {
  717. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  718. kfree(hint->key); /* we don't need to free hint->val */
  719. }
  720. snd_array_free(&codec->hints);
  721. snd_array_free(&codec->user_pins);
  722. #endif
  723. }