hdac_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sysfs support for HD-audio core device
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/sysfs.h>
  7. #include <linux/device.h>
  8. #include <sound/core.h>
  9. #include <sound/hdaudio.h>
  10. #include "local.h"
  11. struct hdac_widget_tree {
  12. struct kobject *root;
  13. struct kobject *afg;
  14. struct kobject **nodes;
  15. };
  16. #define CODEC_ATTR(type) \
  17. static ssize_t type##_show(struct device *dev, \
  18. struct device_attribute *attr, \
  19. char *buf) \
  20. { \
  21. struct hdac_device *codec = dev_to_hdac_dev(dev); \
  22. return sprintf(buf, "0x%x\n", codec->type); \
  23. } \
  24. static DEVICE_ATTR_RO(type)
  25. #define CODEC_ATTR_STR(type) \
  26. static ssize_t type##_show(struct device *dev, \
  27. struct device_attribute *attr, \
  28. char *buf) \
  29. { \
  30. struct hdac_device *codec = dev_to_hdac_dev(dev); \
  31. return sprintf(buf, "%s\n", \
  32. codec->type ? codec->type : ""); \
  33. } \
  34. static DEVICE_ATTR_RO(type)
  35. CODEC_ATTR(type);
  36. CODEC_ATTR(vendor_id);
  37. CODEC_ATTR(subsystem_id);
  38. CODEC_ATTR(revision_id);
  39. CODEC_ATTR(afg);
  40. CODEC_ATTR(mfg);
  41. CODEC_ATTR_STR(vendor_name);
  42. CODEC_ATTR_STR(chip_name);
  43. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  44. char *buf)
  45. {
  46. return snd_hdac_codec_modalias(dev_to_hdac_dev(dev), buf, 256);
  47. }
  48. static DEVICE_ATTR_RO(modalias);
  49. static struct attribute *hdac_dev_attrs[] = {
  50. &dev_attr_type.attr,
  51. &dev_attr_vendor_id.attr,
  52. &dev_attr_subsystem_id.attr,
  53. &dev_attr_revision_id.attr,
  54. &dev_attr_afg.attr,
  55. &dev_attr_mfg.attr,
  56. &dev_attr_vendor_name.attr,
  57. &dev_attr_chip_name.attr,
  58. &dev_attr_modalias.attr,
  59. NULL
  60. };
  61. static struct attribute_group hdac_dev_attr_group = {
  62. .attrs = hdac_dev_attrs,
  63. };
  64. const struct attribute_group *hdac_dev_attr_groups[] = {
  65. &hdac_dev_attr_group,
  66. NULL
  67. };
  68. /*
  69. * Widget tree sysfs
  70. *
  71. * This is a tree showing the attributes of each widget. It appears like
  72. * /sys/bus/hdaudioC0D0/widgets/04/caps
  73. */
  74. struct widget_attribute;
  75. struct widget_attribute {
  76. struct attribute attr;
  77. ssize_t (*show)(struct hdac_device *codec, hda_nid_t nid,
  78. struct widget_attribute *attr, char *buf);
  79. ssize_t (*store)(struct hdac_device *codec, hda_nid_t nid,
  80. struct widget_attribute *attr,
  81. const char *buf, size_t count);
  82. };
  83. static int get_codec_nid(struct kobject *kobj, struct hdac_device **codecp)
  84. {
  85. struct device *dev = kobj_to_dev(kobj->parent->parent);
  86. int nid;
  87. ssize_t ret;
  88. ret = kstrtoint(kobj->name, 16, &nid);
  89. if (ret < 0)
  90. return ret;
  91. *codecp = dev_to_hdac_dev(dev);
  92. return nid;
  93. }
  94. static ssize_t widget_attr_show(struct kobject *kobj, struct attribute *attr,
  95. char *buf)
  96. {
  97. struct widget_attribute *wid_attr =
  98. container_of(attr, struct widget_attribute, attr);
  99. struct hdac_device *codec;
  100. int nid;
  101. if (!wid_attr->show)
  102. return -EIO;
  103. nid = get_codec_nid(kobj, &codec);
  104. if (nid < 0)
  105. return nid;
  106. return wid_attr->show(codec, nid, wid_attr, buf);
  107. }
  108. static ssize_t widget_attr_store(struct kobject *kobj, struct attribute *attr,
  109. const char *buf, size_t count)
  110. {
  111. struct widget_attribute *wid_attr =
  112. container_of(attr, struct widget_attribute, attr);
  113. struct hdac_device *codec;
  114. int nid;
  115. if (!wid_attr->store)
  116. return -EIO;
  117. nid = get_codec_nid(kobj, &codec);
  118. if (nid < 0)
  119. return nid;
  120. return wid_attr->store(codec, nid, wid_attr, buf, count);
  121. }
  122. static const struct sysfs_ops widget_sysfs_ops = {
  123. .show = widget_attr_show,
  124. .store = widget_attr_store,
  125. };
  126. static void widget_release(struct kobject *kobj)
  127. {
  128. kfree(kobj);
  129. }
  130. static struct kobj_type widget_ktype = {
  131. .release = widget_release,
  132. .sysfs_ops = &widget_sysfs_ops,
  133. };
  134. #define WIDGET_ATTR_RO(_name) \
  135. struct widget_attribute wid_attr_##_name = __ATTR_RO(_name)
  136. #define WIDGET_ATTR_RW(_name) \
  137. struct widget_attribute wid_attr_##_name = __ATTR_RW(_name)
  138. static ssize_t caps_show(struct hdac_device *codec, hda_nid_t nid,
  139. struct widget_attribute *attr, char *buf)
  140. {
  141. return sprintf(buf, "0x%08x\n", get_wcaps(codec, nid));
  142. }
  143. static ssize_t pin_caps_show(struct hdac_device *codec, hda_nid_t nid,
  144. struct widget_attribute *attr, char *buf)
  145. {
  146. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  147. return 0;
  148. return sprintf(buf, "0x%08x\n",
  149. snd_hdac_read_parm(codec, nid, AC_PAR_PIN_CAP));
  150. }
  151. static ssize_t pin_cfg_show(struct hdac_device *codec, hda_nid_t nid,
  152. struct widget_attribute *attr, char *buf)
  153. {
  154. unsigned int val;
  155. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  156. return 0;
  157. if (snd_hdac_read(codec, nid, AC_VERB_GET_CONFIG_DEFAULT, 0, &val))
  158. return 0;
  159. return sprintf(buf, "0x%08x\n", val);
  160. }
  161. static bool has_pcm_cap(struct hdac_device *codec, hda_nid_t nid)
  162. {
  163. if (nid == codec->afg || nid == codec->mfg)
  164. return true;
  165. switch (get_wcaps_type(get_wcaps(codec, nid))) {
  166. case AC_WID_AUD_OUT:
  167. case AC_WID_AUD_IN:
  168. return true;
  169. default:
  170. return false;
  171. }
  172. }
  173. static ssize_t pcm_caps_show(struct hdac_device *codec, hda_nid_t nid,
  174. struct widget_attribute *attr, char *buf)
  175. {
  176. if (!has_pcm_cap(codec, nid))
  177. return 0;
  178. return sprintf(buf, "0x%08x\n",
  179. snd_hdac_read_parm(codec, nid, AC_PAR_PCM));
  180. }
  181. static ssize_t pcm_formats_show(struct hdac_device *codec, hda_nid_t nid,
  182. struct widget_attribute *attr, char *buf)
  183. {
  184. if (!has_pcm_cap(codec, nid))
  185. return 0;
  186. return sprintf(buf, "0x%08x\n",
  187. snd_hdac_read_parm(codec, nid, AC_PAR_STREAM));
  188. }
  189. static ssize_t amp_in_caps_show(struct hdac_device *codec, hda_nid_t nid,
  190. struct widget_attribute *attr, char *buf)
  191. {
  192. if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_IN_AMP))
  193. return 0;
  194. return sprintf(buf, "0x%08x\n",
  195. snd_hdac_read_parm(codec, nid, AC_PAR_AMP_IN_CAP));
  196. }
  197. static ssize_t amp_out_caps_show(struct hdac_device *codec, hda_nid_t nid,
  198. struct widget_attribute *attr, char *buf)
  199. {
  200. if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_OUT_AMP))
  201. return 0;
  202. return sprintf(buf, "0x%08x\n",
  203. snd_hdac_read_parm(codec, nid, AC_PAR_AMP_OUT_CAP));
  204. }
  205. static ssize_t power_caps_show(struct hdac_device *codec, hda_nid_t nid,
  206. struct widget_attribute *attr, char *buf)
  207. {
  208. if (nid != codec->afg && !(get_wcaps(codec, nid) & AC_WCAP_POWER))
  209. return 0;
  210. return sprintf(buf, "0x%08x\n",
  211. snd_hdac_read_parm(codec, nid, AC_PAR_POWER_STATE));
  212. }
  213. static ssize_t gpio_caps_show(struct hdac_device *codec, hda_nid_t nid,
  214. struct widget_attribute *attr, char *buf)
  215. {
  216. return sprintf(buf, "0x%08x\n",
  217. snd_hdac_read_parm(codec, nid, AC_PAR_GPIO_CAP));
  218. }
  219. static ssize_t connections_show(struct hdac_device *codec, hda_nid_t nid,
  220. struct widget_attribute *attr, char *buf)
  221. {
  222. hda_nid_t list[32];
  223. int i, nconns;
  224. ssize_t ret = 0;
  225. nconns = snd_hdac_get_connections(codec, nid, list, ARRAY_SIZE(list));
  226. if (nconns <= 0)
  227. return nconns;
  228. for (i = 0; i < nconns; i++)
  229. ret += sprintf(buf + ret, "%s0x%02x", i ? " " : "", list[i]);
  230. ret += sprintf(buf + ret, "\n");
  231. return ret;
  232. }
  233. static WIDGET_ATTR_RO(caps);
  234. static WIDGET_ATTR_RO(pin_caps);
  235. static WIDGET_ATTR_RO(pin_cfg);
  236. static WIDGET_ATTR_RO(pcm_caps);
  237. static WIDGET_ATTR_RO(pcm_formats);
  238. static WIDGET_ATTR_RO(amp_in_caps);
  239. static WIDGET_ATTR_RO(amp_out_caps);
  240. static WIDGET_ATTR_RO(power_caps);
  241. static WIDGET_ATTR_RO(gpio_caps);
  242. static WIDGET_ATTR_RO(connections);
  243. static struct attribute *widget_node_attrs[] = {
  244. &wid_attr_caps.attr,
  245. &wid_attr_pin_caps.attr,
  246. &wid_attr_pin_cfg.attr,
  247. &wid_attr_pcm_caps.attr,
  248. &wid_attr_pcm_formats.attr,
  249. &wid_attr_amp_in_caps.attr,
  250. &wid_attr_amp_out_caps.attr,
  251. &wid_attr_power_caps.attr,
  252. &wid_attr_connections.attr,
  253. NULL,
  254. };
  255. static struct attribute *widget_afg_attrs[] = {
  256. &wid_attr_pcm_caps.attr,
  257. &wid_attr_pcm_formats.attr,
  258. &wid_attr_amp_in_caps.attr,
  259. &wid_attr_amp_out_caps.attr,
  260. &wid_attr_power_caps.attr,
  261. &wid_attr_gpio_caps.attr,
  262. NULL,
  263. };
  264. static const struct attribute_group widget_node_group = {
  265. .attrs = widget_node_attrs,
  266. };
  267. static const struct attribute_group widget_afg_group = {
  268. .attrs = widget_afg_attrs,
  269. };
  270. static void free_widget_node(struct kobject *kobj,
  271. const struct attribute_group *group)
  272. {
  273. if (kobj) {
  274. sysfs_remove_group(kobj, group);
  275. kobject_put(kobj);
  276. }
  277. }
  278. static void widget_tree_free(struct hdac_device *codec)
  279. {
  280. struct hdac_widget_tree *tree = codec->widgets;
  281. struct kobject **p;
  282. if (!tree)
  283. return;
  284. free_widget_node(tree->afg, &widget_afg_group);
  285. if (tree->nodes) {
  286. for (p = tree->nodes; *p; p++)
  287. free_widget_node(*p, &widget_node_group);
  288. kfree(tree->nodes);
  289. }
  290. kobject_put(tree->root);
  291. kfree(tree);
  292. codec->widgets = NULL;
  293. }
  294. static int add_widget_node(struct kobject *parent, hda_nid_t nid,
  295. const struct attribute_group *group,
  296. struct kobject **res)
  297. {
  298. struct kobject *kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  299. int err;
  300. if (!kobj)
  301. return -ENOMEM;
  302. kobject_init(kobj, &widget_ktype);
  303. err = kobject_add(kobj, parent, "%02x", nid);
  304. if (err < 0)
  305. return err;
  306. err = sysfs_create_group(kobj, group);
  307. if (err < 0) {
  308. kobject_put(kobj);
  309. return err;
  310. }
  311. *res = kobj;
  312. return 0;
  313. }
  314. static int widget_tree_create(struct hdac_device *codec)
  315. {
  316. struct hdac_widget_tree *tree;
  317. int i, err;
  318. hda_nid_t nid;
  319. tree = codec->widgets = kzalloc(sizeof(*tree), GFP_KERNEL);
  320. if (!tree)
  321. return -ENOMEM;
  322. tree->root = kobject_create_and_add("widgets", &codec->dev.kobj);
  323. if (!tree->root)
  324. return -ENOMEM;
  325. tree->nodes = kcalloc(codec->num_nodes + 1, sizeof(*tree->nodes),
  326. GFP_KERNEL);
  327. if (!tree->nodes)
  328. return -ENOMEM;
  329. for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
  330. err = add_widget_node(tree->root, nid, &widget_node_group,
  331. &tree->nodes[i]);
  332. if (err < 0)
  333. return err;
  334. }
  335. if (codec->afg) {
  336. err = add_widget_node(tree->root, codec->afg,
  337. &widget_afg_group, &tree->afg);
  338. if (err < 0)
  339. return err;
  340. }
  341. kobject_uevent(tree->root, KOBJ_CHANGE);
  342. return 0;
  343. }
  344. /* call with codec->widget_lock held */
  345. int hda_widget_sysfs_init(struct hdac_device *codec)
  346. {
  347. int err;
  348. if (codec->widgets)
  349. return 0; /* already created */
  350. err = widget_tree_create(codec);
  351. if (err < 0) {
  352. widget_tree_free(codec);
  353. return err;
  354. }
  355. return 0;
  356. }
  357. /* call with codec->widget_lock held */
  358. void hda_widget_sysfs_exit(struct hdac_device *codec)
  359. {
  360. widget_tree_free(codec);
  361. }
  362. /* call with codec->widget_lock held */
  363. int hda_widget_sysfs_reinit(struct hdac_device *codec,
  364. hda_nid_t start_nid, int num_nodes)
  365. {
  366. struct hdac_widget_tree *tree;
  367. hda_nid_t end_nid = start_nid + num_nodes;
  368. hda_nid_t nid;
  369. int i;
  370. if (!codec->widgets)
  371. return 0;
  372. tree = kmemdup(codec->widgets, sizeof(*tree), GFP_KERNEL);
  373. if (!tree)
  374. return -ENOMEM;
  375. tree->nodes = kcalloc(num_nodes + 1, sizeof(*tree->nodes), GFP_KERNEL);
  376. if (!tree->nodes) {
  377. kfree(tree);
  378. return -ENOMEM;
  379. }
  380. /* prune non-existing nodes */
  381. for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
  382. if (nid < start_nid || nid >= end_nid)
  383. free_widget_node(codec->widgets->nodes[i],
  384. &widget_node_group);
  385. }
  386. /* add new nodes */
  387. for (i = 0, nid = start_nid; i < num_nodes; i++, nid++) {
  388. if (nid < codec->start_nid || nid >= codec->end_nid)
  389. add_widget_node(tree->root, nid, &widget_node_group,
  390. &tree->nodes[i]);
  391. else
  392. tree->nodes[i] =
  393. codec->widgets->nodes[nid - codec->start_nid];
  394. }
  395. /* replace with the new tree */
  396. kfree(codec->widgets->nodes);
  397. kfree(codec->widgets);
  398. codec->widgets = tree;
  399. kobject_uevent(tree->root, KOBJ_CHANGE);
  400. return 0;
  401. }