at v2.6.12-rc2 199 lines 5.6 kB view raw
1/* 2 * class_simple.c - a "simple" interface for classes for simple char devices. 3 * 4 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com> 5 * Copyright (c) 2003-2004 IBM Corp. 6 * 7 * This file is released under the GPLv2 8 * 9 */ 10 11#include <linux/config.h> 12#include <linux/device.h> 13#include <linux/err.h> 14 15struct class_simple { 16 struct class class; 17}; 18#define to_class_simple(d) container_of(d, struct class_simple, class) 19 20struct simple_dev { 21 struct list_head node; 22 struct class_device class_dev; 23}; 24#define to_simple_dev(d) container_of(d, struct simple_dev, class_dev) 25 26static LIST_HEAD(simple_dev_list); 27static DEFINE_SPINLOCK(simple_dev_list_lock); 28 29static 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 35static void class_simple_release(struct class *class) 36{ 37 struct class_simple *cs = to_class_simple(class); 38 kfree(cs); 39} 40 41/** 42 * class_simple_create - create a struct class_simple structure 43 * @owner: pointer to the module that is to "own" this struct class_simple 44 * @name: pointer to a string for the name of this class. 45 * 46 * This is used to create a struct class_simple pointer that can then be used 47 * in calls to class_simple_device_add(). This is used when you do not wish to 48 * create a full blown class support for a type of char devices. 49 * 50 * Note, the pointer created here is to be destroyed when finished by making a 51 * call to class_simple_destroy(). 52 */ 53struct class_simple *class_simple_create(struct module *owner, char *name) 54{ 55 struct class_simple *cs; 56 int retval; 57 58 cs = kmalloc(sizeof(*cs), GFP_KERNEL); 59 if (!cs) { 60 retval = -ENOMEM; 61 goto error; 62 } 63 memset(cs, 0x00, sizeof(*cs)); 64 65 cs->class.name = name; 66 cs->class.class_release = class_simple_release; 67 cs->class.release = release_simple_dev; 68 69 retval = class_register(&cs->class); 70 if (retval) 71 goto error; 72 73 return cs; 74 75error: 76 kfree(cs); 77 return ERR_PTR(retval); 78} 79EXPORT_SYMBOL(class_simple_create); 80 81/** 82 * class_simple_destroy - destroys a struct class_simple structure 83 * @cs: pointer to the struct class_simple that is to be destroyed 84 * 85 * Note, the pointer to be destroyed must have been created with a call to 86 * class_simple_create(). 87 */ 88void class_simple_destroy(struct class_simple *cs) 89{ 90 if ((cs == NULL) || (IS_ERR(cs))) 91 return; 92 93 class_unregister(&cs->class); 94} 95EXPORT_SYMBOL(class_simple_destroy); 96 97/** 98 * class_simple_device_add - adds a class device to sysfs for a character driver 99 * @cs: pointer to the struct class_simple that this device should be registered to. 100 * @dev: the dev_t for the device to be added. 101 * @device: a pointer to a struct device that is assiociated with this class device. 102 * @fmt: string for the class device's name 103 * 104 * This function can be used by simple char device classes that do not 105 * implement their own class device registration. A struct class_device will 106 * be created in sysfs, registered to the specified class. A "dev" file will 107 * be created, showing the dev_t for the device. The pointer to the struct 108 * class_device will be returned from the call. Any further sysfs files that 109 * might be required can be created using this pointer. 110 * Note: the struct class_simple passed to this function must have previously been 111 * created with a call to class_simple_create(). 112 */ 113struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...) 114{ 115 va_list args; 116 struct simple_dev *s_dev = NULL; 117 int retval; 118 119 if ((cs == NULL) || (IS_ERR(cs))) { 120 retval = -ENODEV; 121 goto error; 122 } 123 124 s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL); 125 if (!s_dev) { 126 retval = -ENOMEM; 127 goto error; 128 } 129 memset(s_dev, 0x00, sizeof(*s_dev)); 130 131 s_dev->class_dev.devt = dev; 132 s_dev->class_dev.dev = device; 133 s_dev->class_dev.class = &cs->class; 134 135 va_start(args, fmt); 136 vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args); 137 va_end(args); 138 retval = class_device_register(&s_dev->class_dev); 139 if (retval) 140 goto error; 141 142 spin_lock(&simple_dev_list_lock); 143 list_add(&s_dev->node, &simple_dev_list); 144 spin_unlock(&simple_dev_list_lock); 145 146 return &s_dev->class_dev; 147 148error: 149 kfree(s_dev); 150 return ERR_PTR(retval); 151} 152EXPORT_SYMBOL(class_simple_device_add); 153 154/** 155 * class_simple_set_hotplug - set the hotplug callback in the embedded struct class 156 * @cs: pointer to the struct class_simple to hold the pointer 157 * @hotplug: function pointer to the hotplug function 158 * 159 * Implement and set a hotplug function to add environment variables specific to this 160 * class on the hotplug event. 161 */ 162int class_simple_set_hotplug(struct class_simple *cs, 163 int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size)) 164{ 165 if ((cs == NULL) || (IS_ERR(cs))) 166 return -ENODEV; 167 cs->class.hotplug = hotplug; 168 return 0; 169} 170EXPORT_SYMBOL(class_simple_set_hotplug); 171 172/** 173 * class_simple_device_remove - removes a class device that was created with class_simple_device_add() 174 * @dev: the dev_t of the device that was previously registered. 175 * 176 * This call unregisters and cleans up a class device that was created with a 177 * call to class_device_simple_add() 178 */ 179void class_simple_device_remove(dev_t dev) 180{ 181 struct simple_dev *s_dev = NULL; 182 int found = 0; 183 184 spin_lock(&simple_dev_list_lock); 185 list_for_each_entry(s_dev, &simple_dev_list, node) { 186 if (s_dev->class_dev.devt == dev) { 187 found = 1; 188 break; 189 } 190 } 191 if (found) { 192 list_del(&s_dev->node); 193 spin_unlock(&simple_dev_list_lock); 194 class_device_unregister(&s_dev->class_dev); 195 } else { 196 spin_unlock(&simple_dev_list_lock); 197 } 198} 199EXPORT_SYMBOL(class_simple_device_remove);