drm_sysfs.c 10 KB

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