drm_sysfs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/config.h>
  14. #include <linux/device.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/err.h>
  17. #include "drm_core.h"
  18. #include "drmP.h"
  19. struct drm_sysfs_class {
  20. struct class_device_attribute attr;
  21. struct class class;
  22. };
  23. #define to_drm_sysfs_class(d) container_of(d, struct drm_sysfs_class, class)
  24. struct simple_dev {
  25. dev_t dev;
  26. struct class_device class_dev;
  27. };
  28. #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
  29. static void release_simple_dev(struct class_device *class_dev)
  30. {
  31. struct simple_dev *s_dev = to_simple_dev(class_dev);
  32. kfree(s_dev);
  33. }
  34. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  35. {
  36. struct simple_dev *s_dev = to_simple_dev(class_dev);
  37. return print_dev_t(buf, s_dev->dev);
  38. }
  39. static void drm_sysfs_class_release(struct class *class)
  40. {
  41. struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
  42. kfree(cs);
  43. }
  44. /* Display the version of drm_core. This doesn't work right in current design */
  45. static ssize_t version_show(struct class *dev, char *buf)
  46. {
  47. return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
  48. CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  49. }
  50. static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
  51. /**
  52. * drm_sysfs_create - create a struct drm_sysfs_class structure
  53. * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
  54. * @name: pointer to a string for the name of this class.
  55. *
  56. * This is used to create a struct drm_sysfs_class pointer that can then be used
  57. * in calls to drm_sysfs_device_add().
  58. *
  59. * Note, the pointer created here is to be destroyed when finished by making a
  60. * call to drm_sysfs_destroy().
  61. */
  62. struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
  63. {
  64. struct drm_sysfs_class *cs;
  65. int retval;
  66. cs = kmalloc(sizeof(*cs), GFP_KERNEL);
  67. if (!cs) {
  68. retval = -ENOMEM;
  69. goto error;
  70. }
  71. memset(cs, 0x00, sizeof(*cs));
  72. cs->class.name = name;
  73. cs->class.class_release = drm_sysfs_class_release;
  74. cs->class.release = release_simple_dev;
  75. cs->attr.attr.name = "dev";
  76. cs->attr.attr.mode = S_IRUGO;
  77. cs->attr.attr.owner = owner;
  78. cs->attr.show = show_dev;
  79. cs->attr.store = NULL;
  80. retval = class_register(&cs->class);
  81. if (retval)
  82. goto error;
  83. class_create_file(&cs->class, &class_attr_version);
  84. return cs;
  85. error:
  86. kfree(cs);
  87. return ERR_PTR(retval);
  88. }
  89. /**
  90. * drm_sysfs_destroy - destroys a struct drm_sysfs_class structure
  91. * @cs: pointer to the struct drm_sysfs_class that is to be destroyed
  92. *
  93. * Note, the pointer to be destroyed must have been created with a call to
  94. * drm_sysfs_create().
  95. */
  96. void drm_sysfs_destroy(struct drm_sysfs_class *cs)
  97. {
  98. if ((cs == NULL) || (IS_ERR(cs)))
  99. return;
  100. class_unregister(&cs->class);
  101. }
  102. static ssize_t show_dri(struct class_device *class_device, char *buf)
  103. {
  104. drm_device_t * dev = ((drm_head_t *)class_get_devdata(class_device))->dev;
  105. if (dev->driver->dri_library_name)
  106. return dev->driver->dri_library_name(dev, buf);
  107. return snprintf(buf, PAGE_SIZE, "%s\n", dev->driver->pci_driver.name);
  108. }
  109. static struct class_device_attribute class_device_attrs[] = {
  110. __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
  111. };
  112. /**
  113. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  114. * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
  115. * @dev: the dev_t for the device to be added.
  116. * @device: a pointer to a struct device that is assiociated with this class device.
  117. * @fmt: string for the class device's name
  118. *
  119. * A struct class_device will be created in sysfs, registered to the specified
  120. * class. A "dev" file will be created, showing the dev_t for the device. The
  121. * pointer to the struct class_device will be returned from the call. Any further
  122. * sysfs files that might be required can be created using this pointer.
  123. * Note: the struct drm_sysfs_class passed to this function must have previously been
  124. * created with a call to drm_sysfs_create().
  125. */
  126. struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
  127. drm_head_t *head)
  128. {
  129. struct simple_dev *s_dev = NULL;
  130. int i, retval;
  131. if ((cs == NULL) || (IS_ERR(cs))) {
  132. retval = -ENODEV;
  133. goto error;
  134. }
  135. s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
  136. if (!s_dev) {
  137. retval = -ENOMEM;
  138. goto error;
  139. }
  140. memset(s_dev, 0x00, sizeof(*s_dev));
  141. s_dev->dev = MKDEV(DRM_MAJOR, head->minor);
  142. s_dev->class_dev.dev = &(head->dev->pdev)->dev;
  143. s_dev->class_dev.class = &cs->class;
  144. snprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, "card%d", head->minor);
  145. retval = class_device_register(&s_dev->class_dev);
  146. if (retval)
  147. goto error;
  148. class_device_create_file(&s_dev->class_dev, &cs->attr);
  149. class_set_devdata(&s_dev->class_dev, head);
  150. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  151. class_device_create_file(&s_dev->class_dev, &class_device_attrs[i]);
  152. return &s_dev->class_dev;
  153. error:
  154. kfree(s_dev);
  155. return ERR_PTR(retval);
  156. }
  157. /**
  158. * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
  159. * @dev: the dev_t of the device that was previously registered.
  160. *
  161. * This call unregisters and cleans up a class device that was created with a
  162. * call to drm_sysfs_device_add()
  163. */
  164. void drm_sysfs_device_remove(struct class_device *class_dev)
  165. {
  166. struct simple_dev *s_dev = to_simple_dev(class_dev);
  167. int i;
  168. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  169. class_device_remove_file(&s_dev->class_dev, &class_device_attrs[i]);
  170. class_device_unregister(&s_dev->class_dev);
  171. }