hda_sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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) \
  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->type); \
  52. }
  53. #define CODEC_INFO_STR_SHOW(type) \
  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->type ? codec->type : ""); \
  61. }
  62. CODEC_INFO_SHOW(vendor_id);
  63. CODEC_INFO_SHOW(subsystem_id);
  64. CODEC_INFO_SHOW(revision_id);
  65. CODEC_INFO_SHOW(afg);
  66. CODEC_INFO_SHOW(mfg);
  67. CODEC_INFO_STR_SHOW(vendor_name);
  68. CODEC_INFO_STR_SHOW(chip_name);
  69. CODEC_INFO_STR_SHOW(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->bus->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) \
  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->type = val; \
  165. return count; \
  166. }
  167. #define CODEC_INFO_STR_STORE(type) \
  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->type); \
  177. codec->type = s; \
  178. return count; \
  179. }
  180. CODEC_INFO_STORE(vendor_id);
  181. CODEC_INFO_STORE(subsystem_id);
  182. CODEC_INFO_STORE(revision_id);
  183. CODEC_INFO_STR_STORE(vendor_name);
  184. CODEC_INFO_STR_STORE(chip_name);
  185. CODEC_INFO_STR_STORE(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. * Look for hint string
  380. */
  381. const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
  382. {
  383. struct hda_hint *hint = get_hint(codec, key);
  384. return hint ? hint->val : NULL;
  385. }
  386. EXPORT_SYMBOL_GPL(snd_hda_get_hint);
  387. int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
  388. {
  389. const char *p;
  390. int ret;
  391. mutex_lock(&codec->user_mutex);
  392. p = snd_hda_get_hint(codec, key);
  393. if (!p || !*p)
  394. ret = -ENOENT;
  395. else {
  396. switch (toupper(*p)) {
  397. case 'T': /* true */
  398. case 'Y': /* yes */
  399. case '1':
  400. ret = 1;
  401. break;
  402. default:
  403. ret = 0;
  404. break;
  405. }
  406. }
  407. mutex_unlock(&codec->user_mutex);
  408. return ret;
  409. }
  410. EXPORT_SYMBOL_GPL(snd_hda_get_bool_hint);
  411. int snd_hda_get_int_hint(struct hda_codec *codec, const char *key, int *valp)
  412. {
  413. const char *p;
  414. unsigned long val;
  415. int ret;
  416. mutex_lock(&codec->user_mutex);
  417. p = snd_hda_get_hint(codec, key);
  418. if (!p)
  419. ret = -ENOENT;
  420. else if (kstrtoul(p, 0, &val))
  421. ret = -EINVAL;
  422. else {
  423. *valp = val;
  424. ret = 0;
  425. }
  426. mutex_unlock(&codec->user_mutex);
  427. return ret;
  428. }
  429. EXPORT_SYMBOL_GPL(snd_hda_get_int_hint);
  430. #endif /* CONFIG_SND_HDA_RECONFIG */
  431. /*
  432. * common sysfs attributes
  433. */
  434. #ifdef CONFIG_SND_HDA_RECONFIG
  435. #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RW(name)
  436. #else
  437. #define RECONFIG_DEVICE_ATTR(name) DEVICE_ATTR_RO(name)
  438. #endif
  439. static RECONFIG_DEVICE_ATTR(vendor_id);
  440. static RECONFIG_DEVICE_ATTR(subsystem_id);
  441. static RECONFIG_DEVICE_ATTR(revision_id);
  442. static DEVICE_ATTR_RO(afg);
  443. static DEVICE_ATTR_RO(mfg);
  444. static RECONFIG_DEVICE_ATTR(vendor_name);
  445. static RECONFIG_DEVICE_ATTR(chip_name);
  446. static RECONFIG_DEVICE_ATTR(modelname);
  447. static DEVICE_ATTR_RO(init_pin_configs);
  448. static DEVICE_ATTR_RO(driver_pin_configs);
  449. #ifdef CONFIG_SND_HDA_PATCH_LOADER
  450. /* parser mode */
  451. enum {
  452. LINE_MODE_NONE,
  453. LINE_MODE_CODEC,
  454. LINE_MODE_MODEL,
  455. LINE_MODE_PINCFG,
  456. LINE_MODE_VERB,
  457. LINE_MODE_HINT,
  458. LINE_MODE_VENDOR_ID,
  459. LINE_MODE_SUBSYSTEM_ID,
  460. LINE_MODE_REVISION_ID,
  461. LINE_MODE_CHIP_NAME,
  462. NUM_LINE_MODES,
  463. };
  464. static inline int strmatch(const char *a, const char *b)
  465. {
  466. return strncasecmp(a, b, strlen(b)) == 0;
  467. }
  468. /* parse the contents after the line "[codec]"
  469. * accept only the line with three numbers, and assign the current codec
  470. */
  471. static void parse_codec_mode(char *buf, struct hda_bus *bus,
  472. struct hda_codec **codecp)
  473. {
  474. int vendorid, subid, caddr;
  475. struct hda_codec *codec;
  476. *codecp = NULL;
  477. if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
  478. list_for_each_entry(codec, &bus->codec_list, list) {
  479. if ((vendorid <= 0 || codec->vendor_id == vendorid) &&
  480. (subid <= 0 || codec->subsystem_id == subid) &&
  481. codec->addr == caddr) {
  482. *codecp = codec;
  483. break;
  484. }
  485. }
  486. }
  487. }
  488. /* parse the contents after the other command tags, [pincfg], [verb],
  489. * [vendor_id], [subsystem_id], [revision_id], [chip_name], [hint] and [model]
  490. * just pass to the sysfs helper (only when any codec was specified)
  491. */
  492. static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
  493. struct hda_codec **codecp)
  494. {
  495. parse_user_pin_configs(*codecp, buf);
  496. }
  497. static void parse_verb_mode(char *buf, struct hda_bus *bus,
  498. struct hda_codec **codecp)
  499. {
  500. parse_init_verbs(*codecp, buf);
  501. }
  502. static void parse_hint_mode(char *buf, struct hda_bus *bus,
  503. struct hda_codec **codecp)
  504. {
  505. parse_hints(*codecp, buf);
  506. }
  507. static void parse_model_mode(char *buf, struct hda_bus *bus,
  508. struct hda_codec **codecp)
  509. {
  510. kfree((*codecp)->modelname);
  511. (*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
  512. }
  513. static void parse_chip_name_mode(char *buf, struct hda_bus *bus,
  514. struct hda_codec **codecp)
  515. {
  516. kfree((*codecp)->chip_name);
  517. (*codecp)->chip_name = kstrdup(buf, GFP_KERNEL);
  518. }
  519. #define DEFINE_PARSE_ID_MODE(name) \
  520. static void parse_##name##_mode(char *buf, struct hda_bus *bus, \
  521. struct hda_codec **codecp) \
  522. { \
  523. unsigned long val; \
  524. if (!kstrtoul(buf, 0, &val)) \
  525. (*codecp)->name = val; \
  526. }
  527. DEFINE_PARSE_ID_MODE(vendor_id);
  528. DEFINE_PARSE_ID_MODE(subsystem_id);
  529. DEFINE_PARSE_ID_MODE(revision_id);
  530. struct hda_patch_item {
  531. const char *tag;
  532. const char *alias;
  533. void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
  534. };
  535. static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
  536. [LINE_MODE_CODEC] = {
  537. .tag = "[codec]",
  538. .parser = parse_codec_mode,
  539. },
  540. [LINE_MODE_MODEL] = {
  541. .tag = "[model]",
  542. .parser = parse_model_mode,
  543. },
  544. [LINE_MODE_VERB] = {
  545. .tag = "[verb]",
  546. .alias = "[init_verbs]",
  547. .parser = parse_verb_mode,
  548. },
  549. [LINE_MODE_PINCFG] = {
  550. .tag = "[pincfg]",
  551. .alias = "[user_pin_configs]",
  552. .parser = parse_pincfg_mode,
  553. },
  554. [LINE_MODE_HINT] = {
  555. .tag = "[hint]",
  556. .alias = "[hints]",
  557. .parser = parse_hint_mode
  558. },
  559. [LINE_MODE_VENDOR_ID] = {
  560. .tag = "[vendor_id]",
  561. .parser = parse_vendor_id_mode,
  562. },
  563. [LINE_MODE_SUBSYSTEM_ID] = {
  564. .tag = "[subsystem_id]",
  565. .parser = parse_subsystem_id_mode,
  566. },
  567. [LINE_MODE_REVISION_ID] = {
  568. .tag = "[revision_id]",
  569. .parser = parse_revision_id_mode,
  570. },
  571. [LINE_MODE_CHIP_NAME] = {
  572. .tag = "[chip_name]",
  573. .parser = parse_chip_name_mode,
  574. },
  575. };
  576. /* check the line starting with '[' -- change the parser mode accodingly */
  577. static int parse_line_mode(char *buf, struct hda_bus *bus)
  578. {
  579. int i;
  580. for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
  581. if (!patch_items[i].tag)
  582. continue;
  583. if (strmatch(buf, patch_items[i].tag))
  584. return i;
  585. if (patch_items[i].alias && strmatch(buf, patch_items[i].alias))
  586. return i;
  587. }
  588. return LINE_MODE_NONE;
  589. }
  590. /* copy one line from the buffer in fw, and update the fields in fw
  591. * return zero if it reaches to the end of the buffer, or non-zero
  592. * if successfully copied a line
  593. *
  594. * the spaces at the beginning and the end of the line are stripped
  595. */
  596. static int get_line_from_fw(char *buf, int size, size_t *fw_size_p,
  597. const void **fw_data_p)
  598. {
  599. int len;
  600. size_t fw_size = *fw_size_p;
  601. const char *p = *fw_data_p;
  602. while (isspace(*p) && fw_size) {
  603. p++;
  604. fw_size--;
  605. }
  606. if (!fw_size)
  607. return 0;
  608. for (len = 0; len < fw_size; len++) {
  609. if (!*p)
  610. break;
  611. if (*p == '\n') {
  612. p++;
  613. len++;
  614. break;
  615. }
  616. if (len < size)
  617. *buf++ = *p++;
  618. }
  619. *buf = 0;
  620. *fw_size_p = fw_size - len;
  621. *fw_data_p = p;
  622. remove_trail_spaces(buf);
  623. return 1;
  624. }
  625. /*
  626. * load a "patch" firmware file and parse it
  627. */
  628. int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
  629. {
  630. char buf[128];
  631. struct hda_codec *codec;
  632. int line_mode;
  633. line_mode = LINE_MODE_NONE;
  634. codec = NULL;
  635. while (get_line_from_fw(buf, sizeof(buf) - 1, &fw_size, &fw_buf)) {
  636. if (!*buf || *buf == '#' || *buf == '\n')
  637. continue;
  638. if (*buf == '[')
  639. line_mode = parse_line_mode(buf, bus);
  640. else if (patch_items[line_mode].parser &&
  641. (codec || line_mode <= LINE_MODE_CODEC))
  642. patch_items[line_mode].parser(buf, bus, &codec);
  643. }
  644. return 0;
  645. }
  646. EXPORT_SYMBOL_GPL(snd_hda_load_patch);
  647. #endif /* CONFIG_SND_HDA_PATCH_LOADER */
  648. /*
  649. * sysfs entries
  650. */
  651. static struct attribute *hda_dev_attrs[] = {
  652. &dev_attr_vendor_id.attr,
  653. &dev_attr_subsystem_id.attr,
  654. &dev_attr_revision_id.attr,
  655. &dev_attr_afg.attr,
  656. &dev_attr_mfg.attr,
  657. &dev_attr_vendor_name.attr,
  658. &dev_attr_chip_name.attr,
  659. &dev_attr_modelname.attr,
  660. &dev_attr_init_pin_configs.attr,
  661. &dev_attr_driver_pin_configs.attr,
  662. #ifdef CONFIG_PM
  663. &dev_attr_power_on_acct.attr,
  664. &dev_attr_power_off_acct.attr,
  665. #endif
  666. #ifdef CONFIG_SND_HDA_RECONFIG
  667. &dev_attr_init_verbs.attr,
  668. &dev_attr_hints.attr,
  669. &dev_attr_user_pin_configs.attr,
  670. &dev_attr_reconfig.attr,
  671. &dev_attr_clear.attr,
  672. #endif
  673. NULL
  674. };
  675. static struct attribute_group hda_dev_attr_group = {
  676. .attrs = hda_dev_attrs,
  677. };
  678. const struct attribute_group *snd_hda_dev_attr_groups[] = {
  679. &hda_dev_attr_group,
  680. NULL
  681. };
  682. void snd_hda_sysfs_init(struct hda_codec *codec)
  683. {
  684. mutex_init(&codec->user_mutex);
  685. #ifdef CONFIG_SND_HDA_RECONFIG
  686. snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
  687. snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
  688. snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
  689. #endif
  690. }
  691. void snd_hda_sysfs_clear(struct hda_codec *codec)
  692. {
  693. #ifdef CONFIG_SND_HDA_RECONFIG
  694. int i;
  695. /* clear init verbs */
  696. snd_array_free(&codec->init_verbs);
  697. /* clear hints */
  698. for (i = 0; i < codec->hints.used; i++) {
  699. struct hda_hint *hint = snd_array_elem(&codec->hints, i);
  700. kfree(hint->key); /* we don't need to free hint->val */
  701. }
  702. snd_array_free(&codec->hints);
  703. snd_array_free(&codec->user_pins);
  704. #endif
  705. }