2 * class_simple.c - a "simple" interface for classes for simple char devices.
4 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (c) 2003-2004 IBM Corp.
7 * This file is released under the GPLv2
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
18 #define to_class_simple(d) container_of(d, struct class_simple, class)
21 struct list_head node
;
22 struct class_device class_dev
;
24 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
26 static LIST_HEAD(simple_dev_list
);
27 static DEFINE_SPINLOCK(simple_dev_list_lock
);
29 static void release_simple_dev(struct class_device
*class_dev
)
31 struct simple_dev
*s_dev
= to_simple_dev(class_dev
);
35 static void class_simple_release(struct class *class)
37 struct class_simple
*cs
= to_class_simple(class);
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.
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.
50 * Note, the pointer created here is to be destroyed when finished by making a
51 * call to class_simple_destroy().
53 struct class_simple
*class_simple_create(struct module
*owner
, char *name
)
55 struct class_simple
*cs
;
58 cs
= kmalloc(sizeof(*cs
), GFP_KERNEL
);
63 memset(cs
, 0x00, sizeof(*cs
));
65 cs
->class.name
= name
;
66 cs
->class.class_release
= class_simple_release
;
67 cs
->class.release
= release_simple_dev
;
69 retval
= class_register(&cs
->class);
77 return ERR_PTR(retval
);
79 EXPORT_SYMBOL(class_simple_create
);
82 * class_simple_destroy - destroys a struct class_simple structure
83 * @cs: pointer to the struct class_simple that is to be destroyed
85 * Note, the pointer to be destroyed must have been created with a call to
86 * class_simple_create().
88 void class_simple_destroy(struct class_simple
*cs
)
90 if ((cs
== NULL
) || (IS_ERR(cs
)))
93 class_unregister(&cs
->class);
95 EXPORT_SYMBOL(class_simple_destroy
);
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
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().
113 struct class_device
*class_simple_device_add(struct class_simple
*cs
, dev_t dev
, struct device
*device
, const char *fmt
, ...)
116 struct simple_dev
*s_dev
= NULL
;
119 if ((cs
== NULL
) || (IS_ERR(cs
))) {
124 s_dev
= kmalloc(sizeof(*s_dev
), GFP_KERNEL
);
129 memset(s_dev
, 0x00, sizeof(*s_dev
));
131 s_dev
->class_dev
.devt
= dev
;
132 s_dev
->class_dev
.dev
= device
;
133 s_dev
->class_dev
.class = &cs
->class;
136 vsnprintf(s_dev
->class_dev
.class_id
, BUS_ID_SIZE
, fmt
, args
);
138 retval
= class_device_register(&s_dev
->class_dev
);
142 spin_lock(&simple_dev_list_lock
);
143 list_add(&s_dev
->node
, &simple_dev_list
);
144 spin_unlock(&simple_dev_list_lock
);
146 return &s_dev
->class_dev
;
150 return ERR_PTR(retval
);
152 EXPORT_SYMBOL(class_simple_device_add
);
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
159 * Implement and set a hotplug function to add environment variables specific to this
160 * class on the hotplug event.
162 int 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
))
165 if ((cs
== NULL
) || (IS_ERR(cs
)))
167 cs
->class.hotplug
= hotplug
;
170 EXPORT_SYMBOL(class_simple_set_hotplug
);
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.
176 * This call unregisters and cleans up a class device that was created with a
177 * call to class_device_simple_add()
179 void class_simple_device_remove(dev_t dev
)
181 struct simple_dev
*s_dev
= NULL
;
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
) {
192 list_del(&s_dev
->node
);
193 spin_unlock(&simple_dev_list_lock
);
194 class_device_unregister(&s_dev
->class_dev
);
196 spin_unlock(&simple_dev_list_lock
);
199 EXPORT_SYMBOL(class_simple_device_remove
);