diff options
Diffstat (limited to 'drivers/base/class.c')
-rw-r--r-- | drivers/base/class.c | 151 |
1 files changed, 101 insertions, 50 deletions
diff --git a/drivers/base/class.c b/drivers/base/class.c index ce23dc8c18c5..db65fd0babe9 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c | |||
@@ -17,6 +17,7 @@ | |||
17 | #include <linux/string.h> | 17 | #include <linux/string.h> |
18 | #include <linux/kdev_t.h> | 18 | #include <linux/kdev_t.h> |
19 | #include <linux/err.h> | 19 | #include <linux/err.h> |
20 | #include <linux/slab.h> | ||
20 | #include "base.h" | 21 | #include "base.h" |
21 | 22 | ||
22 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) | 23 | #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) |
@@ -99,7 +100,8 @@ struct class * class_get(struct class * cls) | |||
99 | 100 | ||
100 | void class_put(struct class * cls) | 101 | void class_put(struct class * cls) |
101 | { | 102 | { |
102 | subsys_put(&cls->subsys); | 103 | if (cls) |
104 | subsys_put(&cls->subsys); | ||
103 | } | 105 | } |
104 | 106 | ||
105 | 107 | ||
@@ -165,14 +167,25 @@ void class_unregister(struct class * cls) | |||
165 | 167 | ||
166 | static void class_create_release(struct class *cls) | 168 | static void class_create_release(struct class *cls) |
167 | { | 169 | { |
170 | pr_debug("%s called for %s\n", __FUNCTION__, cls->name); | ||
168 | kfree(cls); | 171 | kfree(cls); |
169 | } | 172 | } |
170 | 173 | ||
171 | static void class_device_create_release(struct class_device *class_dev) | 174 | static void class_device_create_release(struct class_device *class_dev) |
172 | { | 175 | { |
176 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); | ||
173 | kfree(class_dev); | 177 | kfree(class_dev); |
174 | } | 178 | } |
175 | 179 | ||
180 | /* needed to allow these devices to have parent class devices */ | ||
181 | static int class_device_create_hotplug(struct class_device *class_dev, | ||
182 | char **envp, int num_envp, | ||
183 | char *buffer, int buffer_size) | ||
184 | { | ||
185 | pr_debug("%s called for %s\n", __FUNCTION__, class_dev->class_id); | ||
186 | return 0; | ||
187 | } | ||
188 | |||
176 | /** | 189 | /** |
177 | * class_create - create a struct class structure | 190 | * class_create - create a struct class structure |
178 | * @owner: pointer to the module that is to "own" this struct class | 191 | * @owner: pointer to the module that is to "own" this struct class |
@@ -301,10 +314,12 @@ static void class_dev_release(struct kobject * kobj) | |||
301 | kfree(cd->devt_attr); | 314 | kfree(cd->devt_attr); |
302 | cd->devt_attr = NULL; | 315 | cd->devt_attr = NULL; |
303 | 316 | ||
304 | if (cls->release) | 317 | if (cd->release) |
318 | cd->release(cd); | ||
319 | else if (cls->release) | ||
305 | cls->release(cd); | 320 | cls->release(cd); |
306 | else { | 321 | else { |
307 | printk(KERN_ERR "Device class '%s' does not have a release() function, " | 322 | printk(KERN_ERR "Class Device '%s' does not have a release() function, " |
308 | "it is broken and must be fixed.\n", | 323 | "it is broken and must be fixed.\n", |
309 | cd->class_id); | 324 | cd->class_id); |
310 | WARN_ON(1); | 325 | WARN_ON(1); |
@@ -382,14 +397,18 @@ static int class_hotplug(struct kset *kset, struct kobject *kobj, char **envp, | |||
382 | buffer = &buffer[length]; | 397 | buffer = &buffer[length]; |
383 | buffer_size -= length; | 398 | buffer_size -= length; |
384 | 399 | ||
385 | if (class_dev->class->hotplug) { | 400 | if (class_dev->hotplug) { |
386 | /* have the bus specific function add its stuff */ | 401 | /* have the class device specific function add its stuff */ |
387 | retval = class_dev->class->hotplug (class_dev, envp, num_envp, | 402 | retval = class_dev->hotplug(class_dev, envp, num_envp, |
388 | buffer, buffer_size); | 403 | buffer, buffer_size); |
389 | if (retval) { | 404 | if (retval) |
390 | pr_debug ("%s - hotplug() returned %d\n", | 405 | pr_debug("class_dev->hotplug() returned %d\n", retval); |
391 | __FUNCTION__, retval); | 406 | } else if (class_dev->class->hotplug) { |
392 | } | 407 | /* have the class specific function add its stuff */ |
408 | retval = class_dev->class->hotplug(class_dev, envp, num_envp, | ||
409 | buffer, buffer_size); | ||
410 | if (retval) | ||
411 | pr_debug("class->hotplug() returned %d\n", retval); | ||
393 | } | 412 | } |
394 | 413 | ||
395 | return retval; | 414 | return retval; |
@@ -442,6 +461,13 @@ static ssize_t show_dev(struct class_device *class_dev, char *buf) | |||
442 | return print_dev_t(buf, class_dev->devt); | 461 | return print_dev_t(buf, class_dev->devt); |
443 | } | 462 | } |
444 | 463 | ||
464 | static ssize_t store_uevent(struct class_device *class_dev, | ||
465 | const char *buf, size_t count) | ||
466 | { | ||
467 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); | ||
468 | return count; | ||
469 | } | ||
470 | |||
445 | void class_device_initialize(struct class_device *class_dev) | 471 | void class_device_initialize(struct class_device *class_dev) |
446 | { | 472 | { |
447 | kobj_set_kset_s(class_dev, class_obj_subsys); | 473 | kobj_set_kset_s(class_dev, class_obj_subsys); |
@@ -469,34 +495,45 @@ static char *make_class_name(struct class_device *class_dev) | |||
469 | 495 | ||
470 | int class_device_add(struct class_device *class_dev) | 496 | int class_device_add(struct class_device *class_dev) |
471 | { | 497 | { |
472 | struct class * parent = NULL; | 498 | struct class *parent_class = NULL; |
473 | struct class_interface * class_intf; | 499 | struct class_device *parent_class_dev = NULL; |
500 | struct class_interface *class_intf; | ||
474 | char *class_name = NULL; | 501 | char *class_name = NULL; |
475 | int error; | 502 | int error = -EINVAL; |
476 | 503 | ||
477 | class_dev = class_device_get(class_dev); | 504 | class_dev = class_device_get(class_dev); |
478 | if (!class_dev) | 505 | if (!class_dev) |
479 | return -EINVAL; | 506 | return -EINVAL; |
480 | 507 | ||
481 | if (!strlen(class_dev->class_id)) { | 508 | if (!strlen(class_dev->class_id)) |
482 | error = -EINVAL; | ||
483 | goto register_done; | 509 | goto register_done; |
484 | } | ||
485 | 510 | ||
486 | parent = class_get(class_dev->class); | 511 | parent_class = class_get(class_dev->class); |
512 | if (!parent_class) | ||
513 | goto register_done; | ||
514 | parent_class_dev = class_device_get(class_dev->parent); | ||
487 | 515 | ||
488 | pr_debug("CLASS: registering class device: ID = '%s'\n", | 516 | pr_debug("CLASS: registering class device: ID = '%s'\n", |
489 | class_dev->class_id); | 517 | class_dev->class_id); |
490 | 518 | ||
491 | /* first, register with generic layer. */ | 519 | /* first, register with generic layer. */ |
492 | kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); | 520 | kobject_set_name(&class_dev->kobj, "%s", class_dev->class_id); |
493 | if (parent) | 521 | if (parent_class_dev) |
494 | class_dev->kobj.parent = &parent->subsys.kset.kobj; | 522 | class_dev->kobj.parent = &parent_class_dev->kobj; |
523 | else | ||
524 | class_dev->kobj.parent = &parent_class->subsys.kset.kobj; | ||
495 | 525 | ||
496 | if ((error = kobject_add(&class_dev->kobj))) | 526 | error = kobject_add(&class_dev->kobj); |
527 | if (error) | ||
497 | goto register_done; | 528 | goto register_done; |
498 | 529 | ||
499 | /* add the needed attributes to this device */ | 530 | /* add the needed attributes to this device */ |
531 | class_dev->uevent_attr.attr.name = "uevent"; | ||
532 | class_dev->uevent_attr.attr.mode = S_IWUSR; | ||
533 | class_dev->uevent_attr.attr.owner = parent_class->owner; | ||
534 | class_dev->uevent_attr.store = store_uevent; | ||
535 | class_device_create_file(class_dev, &class_dev->uevent_attr); | ||
536 | |||
500 | if (MAJOR(class_dev->devt)) { | 537 | if (MAJOR(class_dev->devt)) { |
501 | struct class_device_attribute *attr; | 538 | struct class_device_attribute *attr; |
502 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); | 539 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
@@ -505,12 +542,10 @@ int class_device_add(struct class_device *class_dev) | |||
505 | kobject_del(&class_dev->kobj); | 542 | kobject_del(&class_dev->kobj); |
506 | goto register_done; | 543 | goto register_done; |
507 | } | 544 | } |
508 | |||
509 | attr->attr.name = "dev"; | 545 | attr->attr.name = "dev"; |
510 | attr->attr.mode = S_IRUGO; | 546 | attr->attr.mode = S_IRUGO; |
511 | attr->attr.owner = parent->owner; | 547 | attr->attr.owner = parent_class->owner; |
512 | attr->show = show_dev; | 548 | attr->show = show_dev; |
513 | attr->store = NULL; | ||
514 | class_device_create_file(class_dev, attr); | 549 | class_device_create_file(class_dev, attr); |
515 | class_dev->devt_attr = attr; | 550 | class_dev->devt_attr = attr; |
516 | } | 551 | } |
@@ -524,20 +559,23 @@ int class_device_add(struct class_device *class_dev) | |||
524 | class_name); | 559 | class_name); |
525 | } | 560 | } |
526 | 561 | ||
562 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); | ||
563 | |||
527 | /* notify any interfaces this device is now here */ | 564 | /* notify any interfaces this device is now here */ |
528 | if (parent) { | 565 | if (parent_class) { |
529 | down(&parent->sem); | 566 | down(&parent_class->sem); |
530 | list_add_tail(&class_dev->node, &parent->children); | 567 | list_add_tail(&class_dev->node, &parent_class->children); |
531 | list_for_each_entry(class_intf, &parent->interfaces, node) | 568 | list_for_each_entry(class_intf, &parent_class->interfaces, node) |
532 | if (class_intf->add) | 569 | if (class_intf->add) |
533 | class_intf->add(class_dev); | 570 | class_intf->add(class_dev, class_intf); |
534 | up(&parent->sem); | 571 | up(&parent_class->sem); |
535 | } | 572 | } |
536 | kobject_hotplug(&class_dev->kobj, KOBJ_ADD); | ||
537 | 573 | ||
538 | register_done: | 574 | register_done: |
539 | if (error && parent) | 575 | if (error) { |
540 | class_put(parent); | 576 | class_put(parent_class); |
577 | class_device_put(parent_class_dev); | ||
578 | } | ||
541 | class_device_put(class_dev); | 579 | class_device_put(class_dev); |
542 | kfree(class_name); | 580 | kfree(class_name); |
543 | return error; | 581 | return error; |
@@ -552,21 +590,28 @@ int class_device_register(struct class_device *class_dev) | |||
552 | /** | 590 | /** |
553 | * class_device_create - creates a class device and registers it with sysfs | 591 | * class_device_create - creates a class device and registers it with sysfs |
554 | * @cs: pointer to the struct class that this device should be registered to. | 592 | * @cs: pointer to the struct class that this device should be registered to. |
593 | * @parent: pointer to the parent struct class_device of this new device, if any. | ||
555 | * @dev: the dev_t for the char device to be added. | 594 | * @dev: the dev_t for the char device to be added. |
556 | * @device: a pointer to a struct device that is assiociated with this class device. | 595 | * @device: a pointer to a struct device that is assiociated with this class device. |
557 | * @fmt: string for the class device's name | 596 | * @fmt: string for the class device's name |
558 | * | 597 | * |
559 | * This function can be used by char device classes. A struct | 598 | * This function can be used by char device classes. A struct |
560 | * class_device will be created in sysfs, registered to the specified | 599 | * class_device will be created in sysfs, registered to the specified |
561 | * class. A "dev" file will be created, showing the dev_t for the | 600 | * class. |
562 | * device. The pointer to the struct class_device will be returned from | 601 | * A "dev" file will be created, showing the dev_t for the device, if |
563 | * the call. Any further sysfs files that might be required can be | 602 | * the dev_t is not 0,0. |
564 | * created using this pointer. | 603 | * If a pointer to a parent struct class_device is passed in, the newly |
604 | * created struct class_device will be a child of that device in sysfs. | ||
605 | * The pointer to the struct class_device will be returned from the | ||
606 | * call. Any further sysfs files that might be required can be created | ||
607 | * using this pointer. | ||
565 | * | 608 | * |
566 | * Note: the struct class passed to this function must have previously | 609 | * Note: the struct class passed to this function must have previously |
567 | * been created with a call to class_create(). | 610 | * been created with a call to class_create(). |
568 | */ | 611 | */ |
569 | struct class_device *class_device_create(struct class *cls, dev_t devt, | 612 | struct class_device *class_device_create(struct class *cls, |
613 | struct class_device *parent, | ||
614 | dev_t devt, | ||
570 | struct device *device, char *fmt, ...) | 615 | struct device *device, char *fmt, ...) |
571 | { | 616 | { |
572 | va_list args; | 617 | va_list args; |
@@ -585,6 +630,9 @@ struct class_device *class_device_create(struct class *cls, dev_t devt, | |||
585 | class_dev->devt = devt; | 630 | class_dev->devt = devt; |
586 | class_dev->dev = device; | 631 | class_dev->dev = device; |
587 | class_dev->class = cls; | 632 | class_dev->class = cls; |
633 | class_dev->parent = parent; | ||
634 | class_dev->release = class_device_create_release; | ||
635 | class_dev->hotplug = class_device_create_hotplug; | ||
588 | 636 | ||
589 | va_start(args, fmt); | 637 | va_start(args, fmt); |
590 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); | 638 | vsnprintf(class_dev->class_id, BUS_ID_SIZE, fmt, args); |
@@ -602,17 +650,18 @@ error: | |||
602 | 650 | ||
603 | void class_device_del(struct class_device *class_dev) | 651 | void class_device_del(struct class_device *class_dev) |
604 | { | 652 | { |
605 | struct class * parent = class_dev->class; | 653 | struct class *parent_class = class_dev->class; |
606 | struct class_interface * class_intf; | 654 | struct class_device *parent_device = class_dev->parent; |
655 | struct class_interface *class_intf; | ||
607 | char *class_name = NULL; | 656 | char *class_name = NULL; |
608 | 657 | ||
609 | if (parent) { | 658 | if (parent_class) { |
610 | down(&parent->sem); | 659 | down(&parent_class->sem); |
611 | list_del_init(&class_dev->node); | 660 | list_del_init(&class_dev->node); |
612 | list_for_each_entry(class_intf, &parent->interfaces, node) | 661 | list_for_each_entry(class_intf, &parent_class->interfaces, node) |
613 | if (class_intf->remove) | 662 | if (class_intf->remove) |
614 | class_intf->remove(class_dev); | 663 | class_intf->remove(class_dev, class_intf); |
615 | up(&parent->sem); | 664 | up(&parent_class->sem); |
616 | } | 665 | } |
617 | 666 | ||
618 | if (class_dev->dev) { | 667 | if (class_dev->dev) { |
@@ -620,6 +669,7 @@ void class_device_del(struct class_device *class_dev) | |||
620 | sysfs_remove_link(&class_dev->kobj, "device"); | 669 | sysfs_remove_link(&class_dev->kobj, "device"); |
621 | sysfs_remove_link(&class_dev->dev->kobj, class_name); | 670 | sysfs_remove_link(&class_dev->dev->kobj, class_name); |
622 | } | 671 | } |
672 | class_device_remove_file(class_dev, &class_dev->uevent_attr); | ||
623 | if (class_dev->devt_attr) | 673 | if (class_dev->devt_attr) |
624 | class_device_remove_file(class_dev, class_dev->devt_attr); | 674 | class_device_remove_file(class_dev, class_dev->devt_attr); |
625 | class_device_remove_attrs(class_dev); | 675 | class_device_remove_attrs(class_dev); |
@@ -627,8 +677,8 @@ void class_device_del(struct class_device *class_dev) | |||
627 | kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE); | 677 | kobject_hotplug(&class_dev->kobj, KOBJ_REMOVE); |
628 | kobject_del(&class_dev->kobj); | 678 | kobject_del(&class_dev->kobj); |
629 | 679 | ||
630 | if (parent) | 680 | class_device_put(parent_device); |
631 | class_put(parent); | 681 | class_put(parent_class); |
632 | kfree(class_name); | 682 | kfree(class_name); |
633 | } | 683 | } |
634 | 684 | ||
@@ -708,7 +758,8 @@ struct class_device * class_device_get(struct class_device *class_dev) | |||
708 | 758 | ||
709 | void class_device_put(struct class_device *class_dev) | 759 | void class_device_put(struct class_device *class_dev) |
710 | { | 760 | { |
711 | kobject_put(&class_dev->kobj); | 761 | if (class_dev) |
762 | kobject_put(&class_dev->kobj); | ||
712 | } | 763 | } |
713 | 764 | ||
714 | 765 | ||
@@ -728,7 +779,7 @@ int class_interface_register(struct class_interface *class_intf) | |||
728 | list_add_tail(&class_intf->node, &parent->interfaces); | 779 | list_add_tail(&class_intf->node, &parent->interfaces); |
729 | if (class_intf->add) { | 780 | if (class_intf->add) { |
730 | list_for_each_entry(class_dev, &parent->children, node) | 781 | list_for_each_entry(class_dev, &parent->children, node) |
731 | class_intf->add(class_dev); | 782 | class_intf->add(class_dev, class_intf); |
732 | } | 783 | } |
733 | up(&parent->sem); | 784 | up(&parent->sem); |
734 | 785 | ||
@@ -747,7 +798,7 @@ void class_interface_unregister(struct class_interface *class_intf) | |||
747 | list_del_init(&class_intf->node); | 798 | list_del_init(&class_intf->node); |
748 | if (class_intf->remove) { | 799 | if (class_intf->remove) { |
749 | list_for_each_entry(class_dev, &parent->children, node) | 800 | list_for_each_entry(class_dev, &parent->children, node) |
750 | class_intf->remove(class_dev); | 801 | class_intf->remove(class_dev, class_intf); |
751 | } | 802 | } |
752 | up(&parent->sem); | 803 | up(&parent->sem); |
753 | 804 | ||