drm_sysfs.c 5.3 KB

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