1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * class.c - basic device class management 4 * 5 * Copyright (c) 2002-3 Patrick Mochel 6 * Copyright (c) 2002-3 Open Source Development Labs 7 * Copyright (c) 2003-2004 Greg Kroah-Hartman 8 * Copyright (c) 2003-2004 IBM Corp. 9 */ 10 11 #include <linux/device/class.h> 12 #include <linux/device.h> 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/string.h> 16 #include <linux/kdev_t.h> 17 #include <linux/err.h> 18 #include <linux/slab.h> 19 #include <linux/blkdev.h> 20 #include <linux/mutex.h> 21 #include "base.h" 22 23 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) 24 25 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, 26 char *buf) 27 { 28 struct class_attribute *class_attr = to_class_attr(attr); 29 struct subsys_private *cp = to_subsys_private(kobj); 30 ssize_t ret = -EIO; 31 32 if (class_attr->show) 33 ret = class_attr->show(cp->class, class_attr, buf); 34 return ret; 35 } 36 37 static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, 38 const char *buf, size_t count) 39 { 40 struct class_attribute *class_attr = to_class_attr(attr); 41 struct subsys_private *cp = to_subsys_private(kobj); 42 ssize_t ret = -EIO; 43 44 if (class_attr->store) 45 ret = class_attr->store(cp->class, class_attr, buf, count); 46 return ret; 47 } 48 49 static void class_release(struct kobject *kobj) 50 { 51 struct subsys_private *cp = to_subsys_private(kobj); 52 struct class *class = cp->class; 53 54 pr_debug("class '%s': release.\n", class->name); 55 56 class->p = NULL; 57 58 if (class->class_release) 59 class->class_release(class); 60 else 61 pr_debug("class '%s' does not have a release() function, " 62 "be careful\n", class->name); 63 64 kfree(cp); 65 } 66 67 static const struct kobj_ns_type_operations *class_child_ns_type(const struct kobject *kobj) 68 { 69 const struct subsys_private *cp = to_subsys_private(kobj); 70 struct class *class = cp->class; 71 72 return class->ns_type; 73 } 74 75 static const struct sysfs_ops class_sysfs_ops = { 76 .show = class_attr_show, 77 .store = class_attr_store, 78 }; 79 80 static const struct kobj_type class_ktype = { 81 .sysfs_ops = &class_sysfs_ops, 82 .release = class_release, 83 .child_ns_type = class_child_ns_type, 84 }; 85 86 /* Hotplug events for classes go to the class subsys */ 87 static struct kset *class_kset; 88 89 90 int class_create_file_ns(const struct class *cls, const struct class_attribute *attr, 91 const void *ns) 92 { 93 int error; 94 95 if (cls) 96 error = sysfs_create_file_ns(&cls->p->subsys.kobj, 97 &attr->attr, ns); 98 else 99 error = -EINVAL; 100 return error; 101 } 102 EXPORT_SYMBOL_GPL(class_create_file_ns); 103 104 void class_remove_file_ns(const struct class *cls, const struct class_attribute *attr, 105 const void *ns) 106 { 107 if (cls) 108 sysfs_remove_file_ns(&cls->p->subsys.kobj, &attr->attr, ns); 109 } 110 EXPORT_SYMBOL_GPL(class_remove_file_ns); 111 112 static struct class *class_get(struct class *cls) 113 { 114 if (cls) 115 kset_get(&cls->p->subsys); 116 return cls; 117 } 118 119 static void class_put(struct class *cls) 120 { 121 if (cls) 122 kset_put(&cls->p->subsys); 123 } 124 125 static struct device *klist_class_to_dev(struct klist_node *n) 126 { 127 struct device_private *p = to_device_private_class(n); 128 return p->device; 129 } 130 131 static void klist_class_dev_get(struct klist_node *n) 132 { 133 struct device *dev = klist_class_to_dev(n); 134 135 get_device(dev); 136 } 137 138 static void klist_class_dev_put(struct klist_node *n) 139 { 140 struct device *dev = klist_class_to_dev(n); 141 142 put_device(dev); 143 } 144 145 static int class_add_groups(struct class *cls, 146 const struct attribute_group **groups) 147 { 148 return sysfs_create_groups(&cls->p->subsys.kobj, groups); 149 } 150 151 static void class_remove_groups(struct class *cls, 152 const struct attribute_group **groups) 153 { 154 return sysfs_remove_groups(&cls->p->subsys.kobj, groups); 155 } 156 157 int class_register(struct class *cls) 158 { 159 struct subsys_private *cp; 160 struct lock_class_key *key; 161 int error; 162 163 pr_debug("device class '%s': registering\n", cls->name); 164 165 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 166 if (!cp) 167 return -ENOMEM; 168 klist_init(&cp->klist_devices, klist_class_dev_get, klist_class_dev_put); 169 INIT_LIST_HEAD(&cp->interfaces); 170 kset_init(&cp->glue_dirs); 171 key = &cp->lock_key; 172 lockdep_register_key(key); 173 __mutex_init(&cp->mutex, "subsys mutex", key); 174 error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); 175 if (error) { 176 kfree(cp); 177 return error; 178 } 179 180 /* set the default /sys/dev directory for devices of this class */ 181 if (!cls->dev_kobj) 182 cls->dev_kobj = sysfs_dev_char_kobj; 183 184 cp->subsys.kobj.kset = class_kset; 185 cp->subsys.kobj.ktype = &class_ktype; 186 cp->class = cls; 187 cls->p = cp; 188 189 error = kset_register(&cp->subsys); 190 if (error) 191 goto err_out; 192 193 error = class_add_groups(class_get(cls), cls->class_groups); 194 class_put(cls); 195 if (error) { 196 kobject_del(&cp->subsys.kobj); 197 kfree_const(cp->subsys.kobj.name); 198 goto err_out; 199 } 200 return 0; 201 202 err_out: 203 kfree(cp); 204 cls->p = NULL; 205 return error; 206 } 207 EXPORT_SYMBOL_GPL(class_register); 208 209 void class_unregister(struct class *cls) 210 { 211 pr_debug("device class '%s': unregistering\n", cls->name); 212 class_remove_groups(cls, cls->class_groups); 213 kset_unregister(&cls->p->subsys); 214 } 215 EXPORT_SYMBOL_GPL(class_unregister); 216 217 static void class_create_release(struct class *cls) 218 { 219 pr_debug("%s called for %s\n", __func__, cls->name); 220 kfree(cls); 221 } 222 223 /** 224 * class_create - create a struct class structure 225 * @name: pointer to a string for the name of this class. 226 * @key: the lock_class_key for this class; used by mutex lock debugging 227 * 228 * This is used to create a struct class pointer that can then be used 229 * in calls to device_create(). 230 * 231 * Returns &struct class pointer on success, or ERR_PTR() on error. 232 * 233 * Note, the pointer created here is to be destroyed when finished by 234 * making a call to class_destroy(). 235 */ 236 struct class *class_create(const char *name) 237 { 238 struct class *cls; 239 int retval; 240 241 cls = kzalloc(sizeof(*cls), GFP_KERNEL); 242 if (!cls) { 243 retval = -ENOMEM; 244 goto error; 245 } 246 247 cls->name = name; 248 cls->class_release = class_create_release; 249 250 retval = class_register(cls); 251 if (retval) 252 goto error; 253 254 return cls; 255 256 error: 257 kfree(cls); 258 return ERR_PTR(retval); 259 } 260 EXPORT_SYMBOL_GPL(class_create); 261 262 /** 263 * class_destroy - destroys a struct class structure 264 * @cls: pointer to the struct class that is to be destroyed 265 * 266 * Note, the pointer to be destroyed must have been created with a call 267 * to class_create(). 268 */ 269 void class_destroy(struct class *cls) 270 { 271 if (IS_ERR_OR_NULL(cls)) 272 return; 273 274 class_unregister(cls); 275 } 276 EXPORT_SYMBOL_GPL(class_destroy); 277 278 /** 279 * class_dev_iter_init - initialize class device iterator 280 * @iter: class iterator to initialize 281 * @class: the class we wanna iterate over 282 * @start: the device to start iterating from, if any 283 * @type: device_type of the devices to iterate over, NULL for all 284 * 285 * Initialize class iterator @iter such that it iterates over devices 286 * of @class. If @start is set, the list iteration will start there, 287 * otherwise if it is NULL, the iteration starts at the beginning of 288 * the list. 289 */ 290 void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class, 291 const struct device *start, const struct device_type *type) 292 { 293 struct klist_node *start_knode = NULL; 294 295 if (start) 296 start_knode = &start->p->knode_class; 297 klist_iter_init_node(&class->p->klist_devices, &iter->ki, start_knode); 298 iter->type = type; 299 } 300 EXPORT_SYMBOL_GPL(class_dev_iter_init); 301 302 /** 303 * class_dev_iter_next - iterate to the next device 304 * @iter: class iterator to proceed 305 * 306 * Proceed @iter to the next device and return it. Returns NULL if 307 * iteration is complete. 308 * 309 * The returned device is referenced and won't be released till 310 * iterator is proceed to the next device or exited. The caller is 311 * free to do whatever it wants to do with the device including 312 * calling back into class code. 313 */ 314 struct device *class_dev_iter_next(struct class_dev_iter *iter) 315 { 316 struct klist_node *knode; 317 struct device *dev; 318 319 while (1) { 320 knode = klist_next(&iter->ki); 321 if (!knode) 322 return NULL; 323 dev = klist_class_to_dev(knode); 324 if (!iter->type || iter->type == dev->type) 325 return dev; 326 } 327 } 328 EXPORT_SYMBOL_GPL(class_dev_iter_next); 329 330 /** 331 * class_dev_iter_exit - finish iteration 332 * @iter: class iterator to finish 333 * 334 * Finish an iteration. Always call this function after iteration is 335 * complete whether the iteration ran till the end or not. 336 */ 337 void class_dev_iter_exit(struct class_dev_iter *iter) 338 { 339 klist_iter_exit(&iter->ki); 340 } 341 EXPORT_SYMBOL_GPL(class_dev_iter_exit); 342 343 /** 344 * class_for_each_device - device iterator 345 * @class: the class we're iterating 346 * @start: the device to start with in the list, if any. 347 * @data: data for the callback 348 * @fn: function to be called for each device 349 * 350 * Iterate over @class's list of devices, and call @fn for each, 351 * passing it @data. If @start is set, the list iteration will start 352 * there, otherwise if it is NULL, the iteration starts at the 353 * beginning of the list. 354 * 355 * We check the return of @fn each time. If it returns anything 356 * other than 0, we break out and return that value. 357 * 358 * @fn is allowed to do anything including calling back into class 359 * code. There's no locking restriction. 360 */ 361 int class_for_each_device(const struct class *class, const struct device *start, 362 void *data, int (*fn)(struct device *, void *)) 363 { 364 struct class_dev_iter iter; 365 struct device *dev; 366 int error = 0; 367 368 if (!class) 369 return -EINVAL; 370 if (!class->p) { 371 WARN(1, "%s called for class '%s' before it was initialized", 372 __func__, class->name); 373 return -EINVAL; 374 } 375 376 class_dev_iter_init(&iter, class, start, NULL); 377 while ((dev = class_dev_iter_next(&iter))) { 378 error = fn(dev, data); 379 if (error) 380 break; 381 } 382 class_dev_iter_exit(&iter); 383 384 return error; 385 } 386 EXPORT_SYMBOL_GPL(class_for_each_device); 387 388 /** 389 * class_find_device - device iterator for locating a particular device 390 * @class: the class we're iterating 391 * @start: Device to begin with 392 * @data: data for the match function 393 * @match: function to check device 394 * 395 * This is similar to the class_for_each_dev() function above, but it 396 * returns a reference to a device that is 'found' for later use, as 397 * determined by the @match callback. 398 * 399 * The callback should return 0 if the device doesn't match and non-zero 400 * if it does. If the callback returns non-zero, this function will 401 * return to the caller and not iterate over any more devices. 402 * 403 * Note, you will need to drop the reference with put_device() after use. 404 * 405 * @match is allowed to do anything including calling back into class 406 * code. There's no locking restriction. 407 */ 408 struct device *class_find_device(const struct class *class, const struct device *start, 409 const void *data, 410 int (*match)(struct device *, const void *)) 411 { 412 struct class_dev_iter iter; 413 struct device *dev; 414 415 if (!class) 416 return NULL; 417 if (!class->p) { 418 WARN(1, "%s called for class '%s' before it was initialized", 419 __func__, class->name); 420 return NULL; 421 } 422 423 class_dev_iter_init(&iter, class, start, NULL); 424 while ((dev = class_dev_iter_next(&iter))) { 425 if (match(dev, data)) { 426 get_device(dev); 427 break; 428 } 429 } 430 class_dev_iter_exit(&iter); 431 432 return dev; 433 } 434 EXPORT_SYMBOL_GPL(class_find_device); 435 436 int class_interface_register(struct class_interface *class_intf) 437 { 438 struct class *parent; 439 struct class_dev_iter iter; 440 struct device *dev; 441 442 if (!class_intf || !class_intf->class) 443 return -ENODEV; 444 445 parent = class_get(class_intf->class); 446 if (!parent) 447 return -EINVAL; 448 449 mutex_lock(&parent->p->mutex); 450 list_add_tail(&class_intf->node, &parent->p->interfaces); 451 if (class_intf->add_dev) { 452 class_dev_iter_init(&iter, parent, NULL, NULL); 453 while ((dev = class_dev_iter_next(&iter))) 454 class_intf->add_dev(dev, class_intf); 455 class_dev_iter_exit(&iter); 456 } 457 mutex_unlock(&parent->p->mutex); 458 459 return 0; 460 } 461 EXPORT_SYMBOL_GPL(class_interface_register); 462 463 void class_interface_unregister(struct class_interface *class_intf) 464 { 465 struct class *parent = class_intf->class; 466 struct class_dev_iter iter; 467 struct device *dev; 468 469 if (!parent) 470 return; 471 472 mutex_lock(&parent->p->mutex); 473 list_del_init(&class_intf->node); 474 if (class_intf->remove_dev) { 475 class_dev_iter_init(&iter, parent, NULL, NULL); 476 while ((dev = class_dev_iter_next(&iter))) 477 class_intf->remove_dev(dev, class_intf); 478 class_dev_iter_exit(&iter); 479 } 480 mutex_unlock(&parent->p->mutex); 481 482 class_put(parent); 483 } 484 EXPORT_SYMBOL_GPL(class_interface_unregister); 485 486 ssize_t show_class_attr_string(struct class *class, 487 struct class_attribute *attr, char *buf) 488 { 489 struct class_attribute_string *cs; 490 491 cs = container_of(attr, struct class_attribute_string, attr); 492 return sysfs_emit(buf, "%s\n", cs->str); 493 } 494 495 EXPORT_SYMBOL_GPL(show_class_attr_string); 496 497 struct class_compat { 498 struct kobject *kobj; 499 }; 500 501 /** 502 * class_compat_register - register a compatibility class 503 * @name: the name of the class 504 * 505 * Compatibility class are meant as a temporary user-space compatibility 506 * workaround when converting a family of class devices to a bus devices. 507 */ 508 struct class_compat *class_compat_register(const char *name) 509 { 510 struct class_compat *cls; 511 512 cls = kmalloc(sizeof(struct class_compat), GFP_KERNEL); 513 if (!cls) 514 return NULL; 515 cls->kobj = kobject_create_and_add(name, &class_kset->kobj); 516 if (!cls->kobj) { 517 kfree(cls); 518 return NULL; 519 } 520 return cls; 521 } 522 EXPORT_SYMBOL_GPL(class_compat_register); 523 524 /** 525 * class_compat_unregister - unregister a compatibility class 526 * @cls: the class to unregister 527 */ 528 void class_compat_unregister(struct class_compat *cls) 529 { 530 kobject_put(cls->kobj); 531 kfree(cls); 532 } 533 EXPORT_SYMBOL_GPL(class_compat_unregister); 534 535 /** 536 * class_compat_create_link - create a compatibility class device link to 537 * a bus device 538 * @cls: the compatibility class 539 * @dev: the target bus device 540 * @device_link: an optional device to which a "device" link should be created 541 */ 542 int class_compat_create_link(struct class_compat *cls, struct device *dev, 543 struct device *device_link) 544 { 545 int error; 546 547 error = sysfs_create_link(cls->kobj, &dev->kobj, dev_name(dev)); 548 if (error) 549 return error; 550 551 /* 552 * Optionally add a "device" link (typically to the parent), as a 553 * class device would have one and we want to provide as much 554 * backwards compatibility as possible. 555 */ 556 if (device_link) { 557 error = sysfs_create_link(&dev->kobj, &device_link->kobj, 558 "device"); 559 if (error) 560 sysfs_remove_link(cls->kobj, dev_name(dev)); 561 } 562 563 return error; 564 } 565 EXPORT_SYMBOL_GPL(class_compat_create_link); 566 567 /** 568 * class_compat_remove_link - remove a compatibility class device link to 569 * a bus device 570 * @cls: the compatibility class 571 * @dev: the target bus device 572 * @device_link: an optional device to which a "device" link was previously 573 * created 574 */ 575 void class_compat_remove_link(struct class_compat *cls, struct device *dev, 576 struct device *device_link) 577 { 578 if (device_link) 579 sysfs_remove_link(&dev->kobj, "device"); 580 sysfs_remove_link(cls->kobj, dev_name(dev)); 581 } 582 EXPORT_SYMBOL_GPL(class_compat_remove_link); 583 584 int __init classes_init(void) 585 { 586 class_kset = kset_create_and_add("class", NULL, NULL); 587 if (!class_kset) 588 return -ENOMEM; 589 return 0; 590 } 591