00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <linux/device.h>
00014 #include <linux/module.h>
00015 #include <linux/init.h>
00016 #include <linux/string.h>
00017 #include <linux/kdev_t.h>
00018 #include <linux/err.h>
00019 #include <linux/slab.h>
00020 #include <linux/genhd.h>
00021 #include <linux/mutex.h>
00022 #include "base.h"
00023
00024 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
00025
00026 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
00027 char *buf)
00028 {
00029 struct class_attribute *class_attr = to_class_attr(attr);
00030 struct class_private *cp = to_class(kobj);
00031 ssize_t ret = -EIO;
00032
00033 if (class_attr->show)
00034 ret = class_attr->show(cp->class, buf);
00035 return ret;
00036 }
00037
00038 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
00039 const char *buf, size_t count)
00040 {
00041 struct class_attribute *class_attr = to_class_attr(attr);
00042 struct class_private *cp = to_class(kobj);
00043 ssize_t ret = -EIO;
00044
00045 if (class_attr->store)
00046 ret = class_attr->store(cp->class, buf, count);
00047 return ret;
00048 }
00049
00050 static void class_release(struct kobject *kobj)
00051 {
00052 struct class_private *cp = to_class(kobj);
00053 struct class *class = cp->class;
00054
00055 pr_debug("class '%s': release.\n", class->name);
00056
00057 if (class->class_release)
00058 class->class_release(class);
00059 else
00060 pr_debug("class '%s' does not have a release() function, "
00061 "be careful\n", class->name);
00062 }
00063
00064 static struct sysfs_ops class_sysfs_ops = {
00065 .show = class_attr_show,
00066 .store = class_attr_store,
00067 };
00068
00069 static struct kobj_type class_ktype = {
00070 .sysfs_ops = &class_sysfs_ops,
00071 .release = class_release,
00072 };
00073
00074
00075 static struct kset *class_kset;
00076
00077
00078 int class_create_file(struct class *cls, const struct class_attribute *attr)
00079 {
00080 int error;
00081 if (cls)
00082 error = sysfs_create_file(&cls->p->class_subsys.kobj,
00083 &attr->attr);
00084 else
00085 error = -EINVAL;
00086 return error;
00087 }
00088
00089 void class_remove_file(struct class *cls, const struct class_attribute *attr)
00090 {
00091 if (cls)
00092 sysfs_remove_file(&cls->p->class_subsys.kobj, &attr->attr);
00093 }
00094
00095 static struct class *class_get(struct class *cls)
00096 {
00097 if (cls)
00098 kset_get(&cls->p->class_subsys);
00099 return cls;
00100 }
00101
00102 static void class_put(struct class *cls)
00103 {
00104 if (cls)
00105 kset_put(&cls->p->class_subsys);
00106 }
00107
00108 static int add_class_attrs(struct class *cls)
00109 {
00110 int i;
00111 int error = 0;
00112
00113 if (cls->class_attrs) {
00114 for (i = 0; attr_name(cls->class_attrs[i]); i++) {
00115 error = class_create_file(cls, &cls->class_attrs[i]);
00116 if (error)
00117 goto error;
00118 }
00119 }
00120 done:
00121 return error;
00122 error:
00123 while (--i >= 0)
00124 class_remove_file(cls, &cls->class_attrs[i]);
00125 goto done;
00126 }
00127
00128 static void remove_class_attrs(struct class *cls)
00129 {
00130 int i;
00131
00132 if (cls->class_attrs) {
00133 for (i = 0; attr_name(cls->class_attrs[i]); i++)
00134 class_remove_file(cls, &cls->class_attrs[i]);
00135 }
00136 }
00137
00138 static void klist_class_dev_get(struct klist_node *n)
00139 {
00140 struct device *dev = container_of(n, struct device, knode_class);
00141
00142 get_device(dev);
00143 }
00144
00145 static void klist_class_dev_put(struct klist_node *n)
00146 {
00147 struct device *dev = container_of(n, struct device, knode_class);
00148
00149 put_device(dev);
00150 }
00151
00152 int __class_register(struct class *cls, struct lock_class_key *key)
00153 {
00154 struct class_private *cp;
00155 int error;
00156
00157 pr_debug("device class '%s': registering\n", cls->name);
00158
00159 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
00160 if (!cp)
00161 return -ENOMEM;
00162 klist_init(&cp->class_devices, klist_class_dev_get, klist_class_dev_put);
00163 INIT_LIST_HEAD(&cp->class_interfaces);
00164 kset_init(&cp->class_dirs);
00165 __mutex_init(&cp->class_mutex, "struct class mutex", key);
00166 error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
00167 if (error) {
00168 kfree(cp);
00169 return error;
00170 }
00171
00172
00173 if (!cls->dev_kobj)
00174 cls->dev_kobj = sysfs_dev_char_kobj;
00175
00176 #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) && !defined(DDE_LINUX)
00177
00178 if (cls != &block_class)
00179 cp->class_subsys.kobj.kset = class_kset;
00180 #else
00181 cp->class_subsys.kobj.kset = class_kset;
00182 #endif
00183 cp->class_subsys.kobj.ktype = &class_ktype;
00184 cp->class = cls;
00185 cls->p = cp;
00186
00187 error = kset_register(&cp->class_subsys);
00188 if (error) {
00189 kfree(cp);
00190 return error;
00191 }
00192 error = add_class_attrs(class_get(cls));
00193 class_put(cls);
00194 return error;
00195 }
00196 EXPORT_SYMBOL_GPL(__class_register);
00197
00198 void class_unregister(struct class *cls)
00199 {
00200 pr_debug("device class '%s': unregistering\n", cls->name);
00201 remove_class_attrs(cls);
00202 kset_unregister(&cls->p->class_subsys);
00203 }
00204
00205 static void class_create_release(struct class *cls)
00206 {
00207 pr_debug("%s called for %s\n", __func__, cls->name);
00208 kfree(cls);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 struct class *__class_create(struct module *owner, const char *name,
00224 struct lock_class_key *key)
00225 {
00226 struct class *cls;
00227 int retval;
00228
00229 cls = kzalloc(sizeof(*cls), GFP_KERNEL);
00230 if (!cls) {
00231 retval = -ENOMEM;
00232 goto error;
00233 }
00234
00235 cls->name = name;
00236 cls->owner = owner;
00237 cls->class_release = class_create_release;
00238
00239 retval = __class_register(cls, key);
00240 if (retval)
00241 goto error;
00242
00243 return cls;
00244
00245 error:
00246 kfree(cls);
00247 return ERR_PTR(retval);
00248 }
00249 EXPORT_SYMBOL_GPL(__class_create);
00250
00251
00252
00253
00254
00255
00256
00257
00258 void class_destroy(struct class *cls)
00259 {
00260 if ((cls == NULL) || (IS_ERR(cls)))
00261 return;
00262
00263 class_unregister(cls);
00264 }
00265
00266 #ifdef CONFIG_SYSFS_DEPRECATED
00267 char *make_class_name(const char *name, struct kobject *kobj)
00268 {
00269 char *class_name;
00270 int size;
00271
00272 size = strlen(name) + strlen(kobject_name(kobj)) + 2;
00273
00274 class_name = kmalloc(size, GFP_KERNEL);
00275 if (!class_name)
00276 return NULL;
00277
00278 strcpy(class_name, name);
00279 strcat(class_name, ":");
00280 strcat(class_name, kobject_name(kobj));
00281 return class_name;
00282 }
00283 #endif
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 void class_dev_iter_init(struct class_dev_iter *iter, struct class *class,
00298 struct device *start, const struct device_type *type)
00299 {
00300 struct klist_node *start_knode = NULL;
00301
00302 if (start)
00303 start_knode = &start->knode_class;
00304 klist_iter_init_node(&class->p->class_devices, &iter->ki, start_knode);
00305 iter->type = type;
00306 }
00307 EXPORT_SYMBOL_GPL(class_dev_iter_init);
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 struct device *class_dev_iter_next(struct class_dev_iter *iter)
00322 {
00323 struct klist_node *knode;
00324 struct device *dev;
00325
00326 while (1) {
00327 knode = klist_next(&iter->ki);
00328 if (!knode)
00329 return NULL;
00330 dev = container_of(knode, struct device, knode_class);
00331 if (!iter->type || iter->type == dev->type)
00332 return dev;
00333 }
00334 }
00335 EXPORT_SYMBOL_GPL(class_dev_iter_next);
00336
00337
00338
00339
00340
00341
00342
00343
00344 void class_dev_iter_exit(struct class_dev_iter *iter)
00345 {
00346 klist_iter_exit(&iter->ki);
00347 }
00348 EXPORT_SYMBOL_GPL(class_dev_iter_exit);
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 int class_for_each_device(struct class *class, struct device *start,
00369 void *data, int (*fn)(struct device *, void *))
00370 {
00371 struct class_dev_iter iter;
00372 struct device *dev;
00373 int error = 0;
00374
00375 if (!class)
00376 return -EINVAL;
00377 if (!class->p) {
00378 WARN(1, "%s called for class '%s' before it was initialized",
00379 __func__, class->name);
00380 return -EINVAL;
00381 }
00382
00383 class_dev_iter_init(&iter, class, start, NULL);
00384 while ((dev = class_dev_iter_next(&iter))) {
00385 error = fn(dev, data);
00386 if (error)
00387 break;
00388 }
00389 class_dev_iter_exit(&iter);
00390
00391 return error;
00392 }
00393 EXPORT_SYMBOL_GPL(class_for_each_device);
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 struct device *class_find_device(struct class *class, struct device *start,
00416 void *data,
00417 int (*match)(struct device *, void *))
00418 {
00419 struct class_dev_iter iter;
00420 struct device *dev;
00421
00422 if (!class)
00423 return NULL;
00424 if (!class->p) {
00425 WARN(1, "%s called for class '%s' before it was initialized",
00426 __func__, class->name);
00427 return NULL;
00428 }
00429
00430 class_dev_iter_init(&iter, class, start, NULL);
00431 while ((dev = class_dev_iter_next(&iter))) {
00432 if (match(dev, data)) {
00433 get_device(dev);
00434 break;
00435 }
00436 }
00437 class_dev_iter_exit(&iter);
00438
00439 return dev;
00440 }
00441 EXPORT_SYMBOL_GPL(class_find_device);
00442
00443 int class_interface_register(struct class_interface *class_intf)
00444 {
00445 struct class *parent;
00446 struct class_dev_iter iter;
00447 struct device *dev;
00448
00449 if (!class_intf || !class_intf->class)
00450 return -ENODEV;
00451
00452 parent = class_get(class_intf->class);
00453 if (!parent)
00454 return -EINVAL;
00455
00456 mutex_lock(&parent->p->class_mutex);
00457 list_add_tail(&class_intf->node, &parent->p->class_interfaces);
00458 if (class_intf->add_dev) {
00459 class_dev_iter_init(&iter, parent, NULL, NULL);
00460 while ((dev = class_dev_iter_next(&iter)))
00461 class_intf->add_dev(dev, class_intf);
00462 class_dev_iter_exit(&iter);
00463 }
00464 mutex_unlock(&parent->p->class_mutex);
00465
00466 return 0;
00467 }
00468
00469 void class_interface_unregister(struct class_interface *class_intf)
00470 {
00471 struct class *parent = class_intf->class;
00472 struct class_dev_iter iter;
00473 struct device *dev;
00474
00475 if (!parent)
00476 return;
00477
00478 mutex_lock(&parent->p->class_mutex);
00479 list_del_init(&class_intf->node);
00480 if (class_intf->remove_dev) {
00481 class_dev_iter_init(&iter, parent, NULL, NULL);
00482 while ((dev = class_dev_iter_next(&iter)))
00483 class_intf->remove_dev(dev, class_intf);
00484 class_dev_iter_exit(&iter);
00485 }
00486 mutex_unlock(&parent->p->class_mutex);
00487
00488 class_put(parent);
00489 }
00490
00491 int __init classes_init(void)
00492 {
00493 class_kset = kset_create_and_add("class", NULL, NULL);
00494 if (!class_kset)
00495 return -ENOMEM;
00496 return 0;
00497 }
00498
00499 EXPORT_SYMBOL_GPL(class_create_file);
00500 EXPORT_SYMBOL_GPL(class_remove_file);
00501 EXPORT_SYMBOL_GPL(class_unregister);
00502 EXPORT_SYMBOL_GPL(class_destroy);
00503
00504 EXPORT_SYMBOL_GPL(class_interface_register);
00505 EXPORT_SYMBOL_GPL(class_interface_unregister);