drm_sysfs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/err.h>
  16. #include "drm_core.h"
  17. #include "drmP.h"
  18. #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
  19. /**
  20. * drm_sysfs_suspend - DRM class suspend hook
  21. * @dev: Linux device to suspend
  22. * @state: power state to enter
  23. *
  24. * Just figures out what the actual struct drm_device associated with
  25. * @dev is and calls its suspend hook, if present.
  26. */
  27. static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
  28. {
  29. struct drm_minor *drm_minor = to_drm_minor(dev);
  30. struct drm_device *drm_dev = drm_minor->dev;
  31. if (drm_dev->driver->suspend)
  32. return drm_dev->driver->suspend(drm_dev, state);
  33. return 0;
  34. }
  35. /**
  36. * drm_sysfs_resume - DRM class resume hook
  37. * @dev: Linux device to resume
  38. *
  39. * Just figures out what the actual struct drm_device associated with
  40. * @dev is and calls its resume hook, if present.
  41. */
  42. static int drm_sysfs_resume(struct device *dev)
  43. {
  44. struct drm_minor *drm_minor = to_drm_minor(dev);
  45. struct drm_device *drm_dev = drm_minor->dev;
  46. if (drm_dev->driver->resume)
  47. return drm_dev->driver->resume(drm_dev);
  48. return 0;
  49. }
  50. /* Display the version of drm_core. This doesn't work right in current design */
  51. static ssize_t version_show(struct class *dev, char *buf)
  52. {
  53. return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
  54. CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  55. }
  56. static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
  57. /**
  58. * drm_sysfs_create - create a struct drm_sysfs_class structure
  59. * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
  60. * @name: pointer to a string for the name of this class.
  61. *
  62. * This is used to create DRM class pointer that can then be used
  63. * in calls to drm_sysfs_device_add().
  64. *
  65. * Note, the pointer created here is to be destroyed when finished by making a
  66. * call to drm_sysfs_destroy().
  67. */
  68. struct class *drm_sysfs_create(struct module *owner, char *name)
  69. {
  70. struct class *class;
  71. int err;
  72. class = class_create(owner, name);
  73. if (IS_ERR(class)) {
  74. err = PTR_ERR(class);
  75. goto err_out;
  76. }
  77. class->suspend = drm_sysfs_suspend;
  78. class->resume = drm_sysfs_resume;
  79. err = class_create_file(class, &class_attr_version);
  80. if (err)
  81. goto err_out_class;
  82. return class;
  83. err_out_class:
  84. class_destroy(class);
  85. err_out:
  86. return ERR_PTR(err);
  87. }
  88. /**
  89. * drm_sysfs_destroy - destroys DRM class
  90. *
  91. * Destroy the DRM device class.
  92. */
  93. void drm_sysfs_destroy(void)
  94. {
  95. if ((drm_class == NULL) || (IS_ERR(drm_class)))
  96. return;
  97. class_remove_file(drm_class, &class_attr_version);
  98. class_destroy(drm_class);
  99. }
  100. static ssize_t show_dri(struct device *device, struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct drm_minor *drm_minor = to_drm_minor(device);
  104. struct drm_device *drm_dev = drm_minor->dev;
  105. if (drm_dev->driver->dri_library_name)
  106. return drm_dev->driver->dri_library_name(drm_dev, buf);
  107. return snprintf(buf, PAGE_SIZE, "%s\n", drm_dev->driver->pci_driver.name);
  108. }
  109. static struct device_attribute device_attrs[] = {
  110. __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
  111. };
  112. /**
  113. * drm_sysfs_device_release - do nothing
  114. * @dev: Linux device
  115. *
  116. * Normally, this would free the DRM device associated with @dev, along
  117. * with cleaning up any other stuff. But we do that in the DRM core, so
  118. * this function can just return and hope that the core does its job.
  119. */
  120. static void drm_sysfs_device_release(struct device *dev)
  121. {
  122. return;
  123. }
  124. /**
  125. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  126. * @dev: DRM device to be added
  127. * @head: DRM head in question
  128. *
  129. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  130. * as the parent for the Linux device, and make sure it has a file containing
  131. * the driver we're using (for userspace compatibility).
  132. */
  133. int drm_sysfs_device_add(struct drm_minor *minor)
  134. {
  135. int err;
  136. int i, j;
  137. char *minor_str;
  138. minor->kdev.parent = &minor->dev->pdev->dev;
  139. minor->kdev.class = drm_class;
  140. minor->kdev.release = drm_sysfs_device_release;
  141. minor->kdev.devt = minor->device;
  142. minor_str = "card%d";
  143. snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index);
  144. err = device_register(&minor->kdev);
  145. if (err) {
  146. DRM_ERROR("device add failed: %d\n", err);
  147. goto err_out;
  148. }
  149. for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
  150. err = device_create_file(&minor->kdev, &device_attrs[i]);
  151. if (err)
  152. goto err_out_files;
  153. }
  154. return 0;
  155. err_out_files:
  156. if (i > 0)
  157. for (j = 0; j < i; j++)
  158. device_remove_file(&minor->kdev, &device_attrs[i]);
  159. device_unregister(&minor->kdev);
  160. err_out:
  161. return err;
  162. }
  163. /**
  164. * drm_sysfs_device_remove - remove DRM device
  165. * @dev: DRM device to remove
  166. *
  167. * This call unregisters and cleans up a class device that was created with a
  168. * call to drm_sysfs_device_add()
  169. */
  170. void drm_sysfs_device_remove(struct drm_minor *minor)
  171. {
  172. int i;
  173. for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
  174. device_remove_file(&minor->kdev, &device_attrs[i]);
  175. device_unregister(&minor->kdev);
  176. }