2828
2929#include <linux/kernel.h>
3030#include <linux/module.h>
31+ #include <linux/device.h>
32+ #include <linux/notifier.h>
3133#include <linux/fs.h>
3234#include <linux/slab.h>
3335#include <linux/init.h>
3739#include <linux/jiffies.h>
3840#include <linux/uaccess.h>
3941
40- static struct i2c_driver i2cdev_driver ;
41-
4242/*
4343 * An i2c_dev represents an i2c_adapter ... an I2C or SMBus master, not a
4444 * slave (i2c_client) with which messages will be exchanged. It's coupled
4545 * with a character special file which is accessed by user mode drivers.
4646 *
4747 * The list of i2c_dev structures is parallel to the i2c_adapter lists
48- * maintained by the driver model, and is updated using notifications
49- * delivered to the i2cdev_driver.
48+ * maintained by the driver model, and is updated using bus notifications.
5049 */
5150struct i2c_dev {
5251 struct list_head list ;
@@ -491,7 +490,6 @@ static int i2cdev_open(struct inode *inode, struct file *file)
491490 return - ENOMEM ;
492491 }
493492 snprintf (client -> name , I2C_NAME_SIZE , "i2c-dev %d" , adap -> nr );
494- client -> driver = & i2cdev_driver ;
495493
496494 client -> adapter = adap ;
497495 file -> private_data = client ;
@@ -522,19 +520,18 @@ static const struct file_operations i2cdev_fops = {
522520
523521/* ------------------------------------------------------------------------- */
524522
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-
531523static struct class * i2c_dev_class ;
532524
533- static int i2cdev_attach_adapter (struct i2c_adapter * adap )
525+ static int i2cdev_attach_adapter (struct device * dev , void * dummy )
534526{
527+ struct i2c_adapter * adap ;
535528 struct i2c_dev * i2c_dev ;
536529 int res ;
537530
531+ if (dev -> type != & i2c_adapter_type )
532+ return 0 ;
533+ adap = to_i2c_adapter (dev );
534+
538535 i2c_dev = get_free_i2c_dev (adap );
539536 if (IS_ERR (i2c_dev ))
540537 return PTR_ERR (i2c_dev );
@@ -561,10 +558,15 @@ static int i2cdev_attach_adapter(struct i2c_adapter *adap)
561558 return res ;
562559}
563560
564- static int i2cdev_detach_adapter (struct i2c_adapter * adap )
561+ static int i2cdev_detach_adapter (struct device * dev , void * dummy )
565562{
563+ struct i2c_adapter * adap ;
566564 struct i2c_dev * i2c_dev ;
567565
566+ if (dev -> type != & i2c_adapter_type )
567+ return 0 ;
568+ adap = to_i2c_adapter (dev );
569+
568570 i2c_dev = i2c_dev_get_by_minor (adap -> nr );
569571 if (!i2c_dev ) /* attach_adapter must have failed */
570572 return 0 ;
@@ -577,12 +579,23 @@ static int i2cdev_detach_adapter(struct i2c_adapter *adap)
577579 return 0 ;
578580}
579581
580- static struct i2c_driver i2cdev_driver = {
581- .driver = {
582- .name = "dev_driver" ,
583- },
584- .attach_adapter = i2cdev_attach_adapter ,
585- .detach_adapter = i2cdev_detach_adapter ,
582+ int i2cdev_notifier_call (struct notifier_block * nb , unsigned long action ,
583+ void * data )
584+ {
585+ struct device * dev = data ;
586+
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 ,
586599};
587600
588601/* ------------------------------------------------------------------------- */
@@ -607,10 +620,14 @@ static int __init i2c_dev_init(void)
607620 goto out_unreg_chrdev ;
608621 }
609622
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 );
611625 if (res )
612626 goto out_unreg_class ;
613627
628+ /* Bind to already existing adapters right away */
629+ i2c_for_each_dev (NULL , i2cdev_attach_adapter );
630+
614631 return 0 ;
615632
616633out_unreg_class :
@@ -624,7 +641,8 @@ static int __init i2c_dev_init(void)
624641
625642static void __exit i2c_dev_exit (void )
626643{
627- i2c_del_driver (& i2cdev_driver );
644+ bus_unregister_notifier (& i2c_bus_type , & i2cdev_notifier );
645+ i2c_for_each_dev (NULL , i2cdev_detach_adapter );
628646 class_destroy (i2c_dev_class );
629647 unregister_chrdev (I2C_MAJOR , "i2c" );
630648}
0 commit comments