hdac_sysfs.c 9.6 KB

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