drm_sysfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
  3. * extra sysfs attribute from DRM. Normal drm_sysfs_class
  4. * does not allow adding attributes.
  5. *
  6. * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
  7. * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
  8. * Copyright (c) 2003-2004 IBM Corp.
  9. *
  10. * This file is released under the GPLv2
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/gfp.h>
  16. #include <linux/err.h>
  17. #include <linux/export.h>
  18. #include <drm/drm_sysfs.h>
  19. #include <drm/drmP.h>
  20. #include "drm_internal.h"
  21. #define to_drm_minor(d) dev_get_drvdata(d)
  22. #define to_drm_connector(d) dev_get_drvdata(d)
  23. static struct device_type drm_sysfs_device_minor = {
  24. .name = "drm_minor"
  25. };
  26. struct class *drm_class;
  27. static char *drm_devnode(struct device *dev, umode_t *mode)
  28. {
  29. return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
  30. }
  31. static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
  32. /**
  33. * drm_sysfs_init - initialize sysfs helpers
  34. *
  35. * This is used to create the DRM class, which is the implicit parent of any
  36. * other top-level DRM sysfs objects.
  37. *
  38. * You must call drm_sysfs_destroy() to release the allocated resources.
  39. *
  40. * Return: 0 on success, negative error code on failure.
  41. */
  42. int drm_sysfs_init(void)
  43. {
  44. int err;
  45. drm_class = class_create(THIS_MODULE, "drm");
  46. if (IS_ERR(drm_class))
  47. return PTR_ERR(drm_class);
  48. err = class_create_file(drm_class, &class_attr_version.attr);
  49. if (err) {
  50. class_destroy(drm_class);
  51. drm_class = NULL;
  52. return err;
  53. }
  54. drm_class->devnode = drm_devnode;
  55. return 0;
  56. }
  57. /**
  58. * drm_sysfs_destroy - destroys DRM class
  59. *
  60. * Destroy the DRM device class.
  61. */
  62. void drm_sysfs_destroy(void)
  63. {
  64. if (IS_ERR_OR_NULL(drm_class))
  65. return;
  66. class_remove_file(drm_class, &class_attr_version.attr);
  67. class_destroy(drm_class);
  68. drm_class = NULL;
  69. }
  70. /*
  71. * Connector properties
  72. */
  73. static ssize_t status_store(struct device *device,
  74. struct device_attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct drm_connector *connector = to_drm_connector(device);
  78. struct drm_device *dev = connector->dev;
  79. enum drm_connector_force old_force;
  80. int ret;
  81. ret = mutex_lock_interruptible(&dev->mode_config.mutex);
  82. if (ret)
  83. return ret;
  84. old_force = connector->force;
  85. if (sysfs_streq(buf, "detect"))
  86. connector->force = 0;
  87. else if (sysfs_streq(buf, "on"))
  88. connector->force = DRM_FORCE_ON;
  89. else if (sysfs_streq(buf, "on-digital"))
  90. connector->force = DRM_FORCE_ON_DIGITAL;
  91. else if (sysfs_streq(buf, "off"))
  92. connector->force = DRM_FORCE_OFF;
  93. else
  94. ret = -EINVAL;
  95. if (old_force != connector->force || !connector->force) {
  96. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
  97. connector->base.id,
  98. connector->name,
  99. old_force, connector->force);
  100. connector->funcs->fill_modes(connector,
  101. dev->mode_config.max_width,
  102. dev->mode_config.max_height);
  103. }
  104. mutex_unlock(&dev->mode_config.mutex);
  105. return ret ? ret : count;
  106. }
  107. static ssize_t status_show(struct device *device,
  108. struct device_attribute *attr,
  109. char *buf)
  110. {
  111. struct drm_connector *connector = to_drm_connector(device);
  112. enum drm_connector_status status;
  113. status = READ_ONCE(connector->status);
  114. return snprintf(buf, PAGE_SIZE, "%s\n",
  115. drm_get_connector_status_name(status));
  116. }
  117. static ssize_t dpms_show(struct device *device,
  118. struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct drm_connector *connector = to_drm_connector(device);
  122. int dpms;
  123. dpms = READ_ONCE(connector->dpms);
  124. return snprintf(buf, PAGE_SIZE, "%s\n",
  125. drm_get_dpms_name(dpms));
  126. }
  127. static ssize_t enabled_show(struct device *device,
  128. struct device_attribute *attr,
  129. char *buf)
  130. {
  131. struct drm_connector *connector = to_drm_connector(device);
  132. bool enabled;
  133. enabled = READ_ONCE(connector->encoder);
  134. return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
  135. }
  136. static ssize_t edid_show(struct file *filp, struct kobject *kobj,
  137. struct bin_attribute *attr, char *buf, loff_t off,
  138. size_t count)
  139. {
  140. struct device *connector_dev = kobj_to_dev(kobj);
  141. struct drm_connector *connector = to_drm_connector(connector_dev);
  142. unsigned char *edid;
  143. size_t size;
  144. ssize_t ret = 0;
  145. mutex_lock(&connector->dev->mode_config.mutex);
  146. if (!connector->edid_blob_ptr)
  147. goto unlock;
  148. edid = connector->edid_blob_ptr->data;
  149. size = connector->edid_blob_ptr->length;
  150. if (!edid)
  151. goto unlock;
  152. if (off >= size)
  153. goto unlock;
  154. if (off + count > size)
  155. count = size - off;
  156. memcpy(buf, edid + off, count);
  157. ret = count;
  158. unlock:
  159. mutex_unlock(&connector->dev->mode_config.mutex);
  160. return ret;
  161. }
  162. static ssize_t modes_show(struct device *device,
  163. struct device_attribute *attr,
  164. char *buf)
  165. {
  166. struct drm_connector *connector = to_drm_connector(device);
  167. struct drm_display_mode *mode;
  168. int written = 0;
  169. mutex_lock(&connector->dev->mode_config.mutex);
  170. list_for_each_entry(mode, &connector->modes, head) {
  171. written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
  172. mode->name);
  173. }
  174. mutex_unlock(&connector->dev->mode_config.mutex);
  175. return written;
  176. }
  177. static DEVICE_ATTR_RW(status);
  178. static DEVICE_ATTR_RO(enabled);
  179. static DEVICE_ATTR_RO(dpms);
  180. static DEVICE_ATTR_RO(modes);
  181. static struct attribute *connector_dev_attrs[] = {
  182. &dev_attr_status.attr,
  183. &dev_attr_enabled.attr,
  184. &dev_attr_dpms.attr,
  185. &dev_attr_modes.attr,
  186. NULL
  187. };
  188. static struct bin_attribute edid_attr = {
  189. .attr.name = "edid",
  190. .attr.mode = 0444,
  191. .size = 0,
  192. .read = edid_show,
  193. };
  194. static struct bin_attribute *connector_bin_attrs[] = {
  195. &edid_attr,
  196. NULL
  197. };
  198. static const struct attribute_group connector_dev_group = {
  199. .attrs = connector_dev_attrs,
  200. .bin_attrs = connector_bin_attrs,
  201. };
  202. static const struct attribute_group *connector_dev_groups[] = {
  203. &connector_dev_group,
  204. NULL
  205. };
  206. /**
  207. * drm_sysfs_connector_add - add a connector to sysfs
  208. * @connector: connector to add
  209. *
  210. * Create a connector device in sysfs, along with its associated connector
  211. * properties (so far, connection status, dpms, mode list & edid) and
  212. * generate a hotplug event so userspace knows there's a new connector
  213. * available.
  214. */
  215. int drm_sysfs_connector_add(struct drm_connector *connector)
  216. {
  217. struct drm_device *dev = connector->dev;
  218. if (connector->kdev)
  219. return 0;
  220. connector->kdev =
  221. device_create_with_groups(drm_class, dev->primary->kdev, 0,
  222. connector, connector_dev_groups,
  223. "card%d-%s", dev->primary->index,
  224. connector->name);
  225. DRM_DEBUG("adding \"%s\" to sysfs\n",
  226. connector->name);
  227. if (IS_ERR(connector->kdev)) {
  228. DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
  229. return PTR_ERR(connector->kdev);
  230. }
  231. /* Let userspace know we have a new connector */
  232. drm_sysfs_hotplug_event(dev);
  233. return 0;
  234. }
  235. /**
  236. * drm_sysfs_connector_remove - remove an connector device from sysfs
  237. * @connector: connector to remove
  238. *
  239. * Remove @connector and its associated attributes from sysfs. Note that
  240. * the device model core will take care of sending the "remove" uevent
  241. * at this time, so we don't need to do it.
  242. *
  243. * Note:
  244. * This routine should only be called if the connector was previously
  245. * successfully registered. If @connector hasn't been registered yet,
  246. * you'll likely see a panic somewhere deep in sysfs code when called.
  247. */
  248. void drm_sysfs_connector_remove(struct drm_connector *connector)
  249. {
  250. if (!connector->kdev)
  251. return;
  252. DRM_DEBUG("removing \"%s\" from sysfs\n",
  253. connector->name);
  254. device_unregister(connector->kdev);
  255. connector->kdev = NULL;
  256. }
  257. /**
  258. * drm_sysfs_hotplug_event - generate a DRM uevent
  259. * @dev: DRM device
  260. *
  261. * Send a uevent for the DRM device specified by @dev. Currently we only
  262. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  263. * deal with other types of events.
  264. */
  265. void drm_sysfs_hotplug_event(struct drm_device *dev)
  266. {
  267. char *event_string = "HOTPLUG=1";
  268. char *envp[] = { event_string, NULL };
  269. DRM_DEBUG("generating hotplug event\n");
  270. kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
  271. }
  272. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  273. static void drm_sysfs_release(struct device *dev)
  274. {
  275. kfree(dev);
  276. }
  277. /**
  278. * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
  279. * @minor: minor to allocate sysfs device for
  280. *
  281. * This allocates a new sysfs device for @minor and returns it. The device is
  282. * not registered nor linked. The caller has to use device_add() and
  283. * device_del() to register and unregister it.
  284. *
  285. * Note that dev_get_drvdata() on the new device will return the minor.
  286. * However, the device does not hold a ref-count to the minor nor to the
  287. * underlying drm_device. This is unproblematic as long as you access the
  288. * private data only in sysfs callbacks. device_del() disables those
  289. * synchronously, so they cannot be called after you cleanup a minor.
  290. */
  291. struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
  292. {
  293. const char *minor_str;
  294. struct device *kdev;
  295. int r;
  296. if (minor->type == DRM_MINOR_CONTROL)
  297. minor_str = "controlD%d";
  298. else if (minor->type == DRM_MINOR_RENDER)
  299. minor_str = "renderD%d";
  300. else
  301. minor_str = "card%d";
  302. kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
  303. if (!kdev)
  304. return ERR_PTR(-ENOMEM);
  305. device_initialize(kdev);
  306. kdev->devt = MKDEV(DRM_MAJOR, minor->index);
  307. kdev->class = drm_class;
  308. kdev->type = &drm_sysfs_device_minor;
  309. kdev->parent = minor->dev->dev;
  310. kdev->release = drm_sysfs_release;
  311. dev_set_drvdata(kdev, minor);
  312. r = dev_set_name(kdev, minor_str, minor->index);
  313. if (r < 0)
  314. goto err_free;
  315. return kdev;
  316. err_free:
  317. put_device(kdev);
  318. return ERR_PTR(r);
  319. }
  320. /**
  321. * drm_class_device_register - Register a struct device in the drm class.
  322. *
  323. * @dev: pointer to struct device to register.
  324. *
  325. * @dev should have all relevant members pre-filled with the exception
  326. * of the class member. In particular, the device_type member must
  327. * be set.
  328. */
  329. int drm_class_device_register(struct device *dev)
  330. {
  331. if (!drm_class || IS_ERR(drm_class))
  332. return -ENOENT;
  333. dev->class = drm_class;
  334. return device_register(dev);
  335. }
  336. EXPORT_SYMBOL_GPL(drm_class_device_register);
  337. void drm_class_device_unregister(struct device *dev)
  338. {
  339. return device_unregister(dev);
  340. }
  341. EXPORT_SYMBOL_GPL(drm_class_device_unregister);