hdac_sysfs.c 9.9 KB

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