drm_sysfs.c 5.1 KB

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