diff options
author | Jean Delvare <khali@linux-fr.org> | 2011-03-20 09:50:52 -0400 |
---|---|---|
committer | Jean Delvare <khali@endymion.delvare> | 2011-03-20 09:50:52 -0400 |
commit | 9ea3e941d161f41f920462c50e9b651cc00eccc7 (patch) | |
tree | 47a82a66081fa5377e3a5ec5e91bceb431290ddf /drivers | |
parent | 7ae31482a8376fb0df3a0c5ff0677a92820c06b7 (diff) |
i2c-dev: Use standard bus notification mechanism
Use the standard driver core mechanism to keep track of i2c adapters
present on the system: i2c_for_each_dev and a notifier. This will let
us deprecate and ultimately remove the legacy attach_adapter and
detach_adapter callbacks in i2c_driver.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/i2c/i2c-dev.c | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index cec0f3ba97f8..c90ce50b619f 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c | |||
@@ -28,6 +28,8 @@ | |||
28 | 28 | ||
29 | #include <linux/kernel.h> | 29 | #include <linux/kernel.h> |
30 | #include <linux/module.h> | 30 | #include <linux/module.h> |
31 | #include <linux/device.h> | ||
32 | #include <linux/notifier.h> | ||
31 | #include <linux/fs.h> | 33 | #include <linux/fs.h> |
32 | #include <linux/slab.h> | 34 | #include <linux/slab.h> |
33 | #include <linux/init.h> | 35 | #include <linux/init.h> |
@@ -37,16 +39,13 @@ | |||
37 | #include <linux/jiffies.h> | 39 | #include <linux/jiffies.h> |
38 | #include <linux/uaccess.h> | 40 | #include <linux/uaccess.h> |
39 | 41 | ||
40 | static struct i2c_driver i2cdev_driver; | ||
41 | |||
42 | /* | 42 | /* |
43 | * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a | 43 | * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a |
44 | * slave (i2c_client) with which messages will be exchanged. It's coupled | 44 | * slave (i2c_client) with which messages will be exchanged. It's coupled |
45 | * with a character special file which is accessed by user mode drivers. | 45 | * with a character special file which is accessed by user mode drivers. |
46 | * | 46 | * |
47 | * The list of i2c_dev structures is parallel to the i2c_adapter lists | 47 | * The list of i2c_dev structures is parallel to the i2c_adapter lists |
48 | * maintained by the driver model, and is updated using notifications | 48 | * maintained by the driver model, and is updated using bus notifications. |
49 | * delivered to the i2cdev_driver. | ||
50 | */ | 49 | */ |
51 | struct i2c_dev { | 50 | struct i2c_dev { |
52 | struct list_head list; | 51 | struct list_head list; |
@@ -491,7 +490,6 @@ static int i2cdev_open(struct inode *inode, struct file *file) | |||
491 | return -ENOMEM; | 490 | return -ENOMEM; |
492 | } | 491 | } |
493 | snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); | 492 | snprintf(client->name, I2C_NAME_SIZE, "i2c-dev %d", adap->nr); |
494 | client->driver = &i2cdev_driver; | ||
495 | 493 | ||
496 | client->adapter = adap; | 494 | client->adapter = adap; |
497 | file->private_data = client; | 495 | file->private_data = client; |
@@ -522,19 +520,18 @@ static const struct file_operations i2cdev_fops = { | |||
522 | 520 | ||
523 | /* ------------------------------------------------------------------------- */ | 521 | /* ------------------------------------------------------------------------- */ |
524 | 522 | ||
525 | /* | ||
526 | * The legacy "i2cdev_driver" is used primarily to get notifications when | ||
527 | * I2C adapters are added or removed, so that each one gets an i2c_dev | ||
528 | * and is thus made available to userspace driver code. | ||
529 | */ | ||
530 | |||
531 | static struct class *i2c_dev_class; | 523 | static struct class *i2c_dev_class; |
532 | 524 | ||
533 | static int i2cdev_attach_adapter(struct i2c_adapter *adap) | 525 | static int i2cdev_attach_adapter(struct device *dev, void *dummy) |
534 | { | 526 | { |
527 | struct i2c_adapter *adap; | ||
535 | struct i2c_dev *i2c_dev; | 528 | struct i2c_dev *i2c_dev; |
536 | int res; | 529 | int res; |
537 | 530 | ||
531 | if (dev->type != &i2c_adapter_type) | ||
532 | return 0; | ||
533 | adap = to_i2c_adapter(dev); | ||
534 | |||
538 | i2c_dev = get_free_i2c_dev(adap); | 535 | i2c_dev = get_free_i2c_dev(adap); |
539 | if (IS_ERR(i2c_dev)) | 536 | if (IS_ERR(i2c_dev)) |
540 | return PTR_ERR(i2c_dev); | 537 | return PTR_ERR(i2c_dev); |
@@ -561,10 +558,15 @@ error: | |||
561 | return res; | 558 | return res; |
562 | } | 559 | } |
563 | 560 | ||
564 | static int i2cdev_detach_adapter(struct i2c_adapter *adap) | 561 | static int i2cdev_detach_adapter(struct device *dev, void *dummy) |
565 | { | 562 | { |
563 | struct i2c_adapter *adap; | ||
566 | struct i2c_dev *i2c_dev; | 564 | struct i2c_dev *i2c_dev; |
567 | 565 | ||
566 | if (dev->type != &i2c_adapter_type) | ||
567 | return 0; | ||
568 | adap = to_i2c_adapter(dev); | ||
569 | |||
568 | i2c_dev = i2c_dev_get_by_minor(adap->nr); | 570 | i2c_dev = i2c_dev_get_by_minor(adap->nr); |
569 | if (!i2c_dev) /* attach_adapter must have failed */ | 571 | if (!i2c_dev) /* attach_adapter must have failed */ |
570 | return 0; | 572 | return 0; |
@@ -577,12 +579,23 @@ static int i2cdev_detach_adapter(struct i2c_adapter *adap) | |||
577 | return 0; | 579 | return 0; |
578 | } | 580 | } |
579 | 581 | ||
580 | static struct i2c_driver i2cdev_driver = { | 582 | int i2cdev_notifier_call(struct notifier_block *nb, unsigned long action, |
581 | .driver = { | 583 | void *data) |
582 | .name = "dev_driver", | 584 | { |
583 | }, | 585 | struct device *dev = data; |
584 | .attach_adapter = i2cdev_attach_adapter, | 586 | |
585 | .detach_adapter = i2cdev_detach_adapter, | 587 | switch (action) { |
588 | case BUS_NOTIFY_ADD_DEVICE: | ||
589 | return i2cdev_attach_adapter(dev, NULL); | ||
590 | case BUS_NOTIFY_DEL_DEVICE: | ||
591 | return i2cdev_detach_adapter(dev, NULL); | ||
592 | } | ||
593 | |||
594 | return 0; | ||
595 | } | ||
596 | |||
597 | static struct notifier_block i2cdev_notifier = { | ||
598 | .notifier_call = i2cdev_notifier_call, | ||
586 | }; | 599 | }; |
587 | 600 | ||
588 | /* ------------------------------------------------------------------------- */ | 601 | /* ------------------------------------------------------------------------- */ |
@@ -607,10 +620,14 @@ static int __init i2c_dev_init(void) | |||
607 | goto out_unreg_chrdev; | 620 | goto out_unreg_chrdev; |
608 | } | 621 | } |
609 | 622 | ||
610 | res = i2c_add_driver(&i2cdev_driver); | 623 | /* Keep track of adapters which will be added or removed later */ |
624 | res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier); | ||
611 | if (res) | 625 | if (res) |
612 | goto out_unreg_class; | 626 | goto out_unreg_class; |
613 | 627 | ||
628 | /* Bind to already existing adapters right away */ | ||
629 | i2c_for_each_dev(NULL, i2cdev_attach_adapter); | ||
630 | |||
614 | return 0; | 631 | return 0; |
615 | 632 | ||
616 | out_unreg_class: | 633 | out_unreg_class: |
@@ -624,7 +641,8 @@ out: | |||
624 | 641 | ||
625 | static void __exit i2c_dev_exit(void) | 642 | static void __exit i2c_dev_exit(void) |
626 | { | 643 | { |
627 | i2c_del_driver(&i2cdev_driver); | 644 | bus_unregister_notifier(&i2c_bus_type, &i2cdev_notifier); |
645 | i2c_for_each_dev(NULL, i2cdev_detach_adapter); | ||
628 | class_destroy(i2c_dev_class); | 646 | class_destroy(i2c_dev_class); |
629 | unregister_chrdev(I2C_MAJOR, "i2c"); | 647 | unregister_chrdev(I2C_MAJOR, "i2c"); |
630 | } | 648 | } |