i2c kobject

来源:互联网 发布:淘宝女装宝贝描述 编辑:程序博客网 时间:2024/06/15 19:12
215 /**
216  * driver_find - locate driver on a bus by its name.
217  * @name: name of the driver.
218  * @bus: bus to scan for the driver.
219  *
220  * Call kset_find_obj() to iterate over list of drivers on
221  * a bus to find driver by name. Return driver if found.
222  *
223  * This routine provides no locking to prevent the driver it returns
224  * from being unregistered or unloaded while the caller is using it.
225  * The caller is responsible for preventing this.
226  */
227 struct device_driver *driver_find(const char *name, struct bus_type *bus)
228 {
229         struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);//如果注册过将返回  kobject.
230         struct driver_private *priv;
231
232         if (k) {
233                 /* Drop reference added by kset_find_obj() */
234                 kobject_put(k);
235                 priv = to_driver(k);
236                 return priv->driver;
237         }
238         return NULL;
239 }

240 EXPORT_SYMBOL_GPL(driver_find);         


看个driver是否被注册过,kset_find_obj()。

/**     
744  * kset_find_obj - search for object in kset.
745  * @kset: kset we're looking in.
746  * @name: object's name.
747  *      
748  * Lock kset via @kset->subsys, and iterate over @kset->list,
749  * looking for a matching kobject. If matching object is found
750  * take a reference and return the object.
751  */
752 struct kobject *kset_find_obj(struct kset *kset, const char *name)
753 {
754         struct kobject *k;                                                                                                                                                          
755         struct kobject *ret = NULL;
756
757         spin_lock(&kset->list_lock);
758
759         list_for_each_entry(k, &kset->list, entry) {
760                 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
761                         ret = kobject_get_unless_zero(k);//  如果注册过, 这边将返回 kobject
762                         break;
763                 }
764         }
765
766         spin_unlock(&kset->list_lock);
767         return ret;
768 }


532 static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)                                                                                                  
533 {
534         if (!kref_get_unless_zero(&kobj->kref))
535                 kobj = NULL;
536         return kobj;
537 }


那  kobject  是在哪里注册过的呢? 是 driver_register吗?

果然是 !

driver_register  = = >

     ret = bus_add_driver(drv);   


 673 /**
 674  * bus_add_driver - Add a driver to the bus.
 675  * @drv: driver.
 676  */
 677 int bus_add_driver(struct device_driver *drv)
 678 {
 679         struct bus_type *bus;
 680         struct driver_private *priv;
 681         int error = 0;
 682
 683         bus = bus_get(drv->bus);
 684         if (!bus)
 685                 return -EINVAL;
 686
 687         pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
 688
 689         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 690         if (!priv) {
 691                 error = -ENOMEM;                                                                                                                                                   
 692                 goto out_put_bus;
 693         }
 694         klist_init(&priv->klist_devices, NULL, NULL);
 695         priv->driver = drv;
 696         drv->p = priv;
 697         priv->kobj.kset = bus->p->drivers_kset;

                 //在这边对kobject进行了名字赋值, 如果仅仅知道个代码流程, 是不是太肤浅了点?
 698         error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 
 699                                      "%s", drv->name);

 700         if (error)
 701                 goto out_unregister;
 702
 703         klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
 704         if (drv->bus->p->drivers_autoprobe) {
 705                 error = driver_attach(drv);
 706                 if (error)
 707                         goto out_unregister;
 708         }
 709         module_add_driver(drv->owner, drv);
 710
 711         error = driver_create_file(drv, &driver_attr_uevent);
 712         if (error) {
 713                 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
 714                         __func__, drv->name);
 715         }
 716         error = driver_add_attrs(bus, drv);
 717         if (error) {
 718                 /* How the hell do we get out of this pickle? Give up */
 719                 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
 720                         __func__, drv->name);
 721         }
 722
 723         if (!drv->suppress_bind_attrs) {
 724                 error = add_bind_files(drv);
 725                 if (error) {
 726                         /* Ditto */
 727                         printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
 728                                 __func__, drv->name);
 729                 }
 730         }
 731
 732         return 0;
 733
 734 out_unregister:
 735         kobject_put(&priv->kobj);
 736         kfree(drv->p);
 737         drv->p = NULL;
 738 out_put_bus:
 739         bus_put(bus);
 740         return error;
 741 }
 742

371 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
372                          struct kobject *parent, const char *fmt, ...)
373 {
374         va_list args;
375         int retval;
376
377         kobject_init(kobj, ktype);
378
379         va_start(args, fmt);
380         retval = kobject_add_varg(kobj, parent, fmt, args);
381         va_end(args);
382                                                                                                                                                                                     
383         return retval;
384 }
385 EXPORT_SYMBOL_GPL(kobject_init_and_add);



297 static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
298                             const char *fmt, va_list vargs)
299 {
300         int retval;
301
302         retval = kobject_set_name_vargs(kobj, fmt, vargs);
303         if (retval) {
304                 printk(KERN_ERR "kobject: can not set name properly!\n");
305                 return retval;
306         }                                                                                                                                                                           
307         kobj->parent = parent;
308         return kobject_add_internal(kobj);
309 }
310

213 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
214                                   va_list vargs)
215 {
216         const char *old_name = kobj->name;
217         char *s;
218
219         if (kobj->name && !fmt)
220                 return 0;
221
222         kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);

223         if (!kobj->name)
224                 return -ENOMEM;
225
226         /* ewww... some of these buggers have '/' in the name ... */
227         while ((s = strchr(kobj->name, '/')))
228                 s[0] = '!';
229
230         kfree(old_name);
231         return 0;
232 }

=========================================

 46 struct driver_private {                                                                                                                                                             
 47         struct kobject kobj;          

 48         struct klist klist_devices;
 49         struct klist_node knode_bus;
 50         struct module_kobject *mkobj;
 51         struct device_driver *driver;
 52 };

====================


  /*
  81  * @p:          The private data of the driver core, only the driver core can
  82  *              touch this.                                                                                                                                                        
         */

  93 struct bus_type {
  94         const char              *name;
  95         const char              *dev_name;
  96         struct device           *dev_root;
  97         struct bus_attribute    *bus_attrs;
  98         struct device_attribute *dev_attrs;
  99         struct driver_attribute *drv_attrs;
 100
 101         int (*match)(struct device *dev, struct device_driver *drv);
 102         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
 103         int (*probe)(struct device *dev);
 104         int (*remove)(struct device *dev);
 105         void (*shutdown)(struct device *dev);
 106
 107         int (*suspend)(struct device *dev, pm_message_t state);
 108         int (*resume)(struct device *dev);
 109
 110         const struct dev_pm_ops *pm;
 111
 112         struct iommu_ops *iommu_ops;
 113
 114         struct subsys_private *p;
 115         struct lock_class_key lock_key;
 116 };

==========

 28 struct subsys_private {
 29         struct kset subsys;
 30         struct kset *devices_kset;
 31         struct list_head interfaces;
 32         struct mutex mutex;
 33
 34         struct kset *drivers_kset;                                                                                                                                                  
 35         struct klist klist_devices;
 36         struct klist klist_drivers;
 37         struct blocking_notifier_head bus_notifier;
 38         unsigned int drivers_autoprobe:1;
 39         struct bus_type *bus;
 40
 41         struct kset glue_dirs;
 42         struct class *class;
 43 };

========

142 /**
143  * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
144  *
145  * A kset defines a group of kobjects.  They can be individually
146  * different "types" but overall these kobjects all want to be grouped
147  * together and operated on in the same manner.  ksets are used to
148  * define the attribute callbacks and other common events that happen to
149  * a kobject.
150  *
151  * @list: the list of all kobjects for this kset
152  * @list_lock: a lock for iterating over the kobjects
153  * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
154  * @uevent_ops: the set of uevent operations for this kset.  These are
155  * called whenever a kobject has something happen to it so that the kset
156  * can add new environment variables, or filter out the uevents if so
157  * desired.
158  */
159 struct kset {                                                                                                                                                                       
160         struct list_head list;
161         spinlock_t list_lock;
162         struct kobject kobj;
163         const struct kset_uevent_ops *uevent_ops;
164 };

==============

 60 struct kobject {
 61         const char              *name;
//指针
 62         struct list_head        entry;
 63         struct kobject          *parent;
 64         struct kset             *kset;
 65         struct kobj_type        *ktype;
 66         struct sysfs_dirent     *sd;
 67         struct kref             kref;
 68         unsigned int state_initialized:1;
 69         unsigned int state_in_sysfs:1;
 70         unsigned int state_add_uevent_sent:1;
 71         unsigned int state_remove_uevent_sent:1;
 72         unsigned int uevent_suppress:1;
 73 };

0 0
原创粉丝点击