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