Linux device driver Model-device/bus/driver, linuxbus
In the linux driver model, in order to facilitate the management of various devices, we have mounted different devices on their respective bus, and the drivers corresponding to the devices are also located on the bus, in this way, the deivce-bus-driver model is proposed. There are many device bus in the hardware, So we abstract the concept of bus in the device model, and the corresponding device represents the device, driver indicates the driver. In the code, the corresponding struct is described below. For the actual device and bus, these struct can be embedded into the actual bus.
1. bus
To understand the bus, we need to first introduce the structure of the bus. After defining a bus, we need to register it in the system. The second section introduces the registration function, and then introduces some other related APIs.
1.1 struct bus_type
Struct bus_type {const char * name; coach bus name const char * dev_name; struct device * dev_root; struct device_attribute * dev_attrs;/* use dev_groups instead */const struct attribute_group * bus_groups; const struct attribute_group ** dev_groups; const struct attribute_group ** drv_groups; int (* match) (struct device * dev, struct device_driver * drv ); ------- matching function (used to match device & driver) int (* uevent) (struct device * dev, struct kobj_uevent_env * env); int (* probe) (struct device * dev ); -------------------------------- used to initialize the driver int (* remove) (struct device * dev); void (* shutdown) (struct device * dev); int (* online) (struct device * dev ); int (* offline) (struct device * dev); int (* suspend) (struct device * dev, pm_message_t state); ----------- PM-related int (* resume) (struct device * dev); consumer PM const struct dev_pm_ops * pm; Consumer PM const struct iommu_ops * iommu_ops; struct subsys_private * p; struct lock_class_key lock_key ;};1.2 register Bus
Int bus_register (struct bus_type * bus) {int retval; struct subsys_private * priv; struct lock_class_key * key = & bus-> lock_key; priv = kzarloc (sizeof (struct subsys_private ), GFP_KERNEL); if (! Priv) return-ENOMEM; priv-> bus = bus; bus-> p = priv; BLOCKING_INIT_NOTIFIER_HEAD (& priv-> bus_notifier); retval = kobject_set_name (& priv-> subsys. kobj, "% s", bus-> name); if (retval) goto out; priv-> subsys. kobj. kset = bus_kset; priv-> subsys. kobj. ktype = & bus_ktype; priv-> drivers_autoprobe = 1; retval = kset_register (& priv-> subsys); ------------------ create the current bus directory in the/sys/bus directory if (retval) goto out; retval = Bus_create_file (bus, & bus_attr_uevent); ---------- create the file uevent if (retval) goto bus_uevent_fail in the current bus directory; priv-> devices_kset = delimiter ("devices", NULL, & amp; priv-& gt; subsys. kobj); ----------------- create the devices directory if (! Priv-> devices_kset) {retval =-ENOMEM; goto bus_devices_fail;} priv-> drivers_kset = kset_create_and_add ("drivers", NULL, & priv-> subsys. kobj); ---------------- create the drivers directory if (! Priv-> drivers_kset) {retval =-ENOMEM; goto bus_drivers_fail;} INIT_LIST_HEAD (& priv-> interfaces); _ mutex_init (& priv-> mutex, "subsys mutex ", key); klist_init (& priv-> klist_devices, devices, klist_devices_put); klist_init (& priv-> klist_drivers, NULL, NULL); retval = add_probe_files (bus ); ------------------------- create the probe file in the current bus directory if (retval) goto bus_probe_files_fail; retval = bus_add_gro Ups (bus, bus-> bus_groups); if (retval) goto bus_groups_fail; pr_debug ("bus: '% s': registered \ n", bus-> name ); return 0 ;......}1.3 Other APIs
Bus unmount Function
extern void bus_unregister(struct bus_type *bus);
2. device
First, introduce the devices struct.
2.1 struct device
Struct device {struct device * parent; ----------------------- parent device struct device_private * p; struct kobject kobj; ---------------------------- embedded kobject const char * init_name; /* initial name of the device */const struct device_type * type; ------------------- the device type struct mutex;/* mutex to synchronize cballs to * its driver. */struct bus_type * bus;/* type of bus device is on */--- bus struct device_driver * driver; /* which driver has allocated this device */----------------------------------- driver void * platform_data;/* Platform specific data, device core doesn' t touch it */------------------ private platform data void * driver_data;/* Driver data, set and get with dev_set/get_drvdata */-------------------- private driver data struct dev_pm_power Info; struct PM-related struct dev_pm_domain * pm_domain; ------------------------ PM-related # ifdef CONFIG_PINCTRL struct dev_pin_info * pins; # endif # ifdef CONFIG_NUMA int numa_node; /* NUMA node this device is close to */# endif u64 * dma_mask;/* dma mask (if dma 'device) */u64 coherent_dma_mask;/* Like dma_mask, but for alloc_coherent mappings as not all hardware supports 64 bit addresses for consistent allocations such descriptors. */unsigned long dma_pfn_offset; struct device_dma_parameters * dma_parms; struct list_head dma_pools;/* dma pools (if dma 'ble) */struct dma_coherent_mem * dma_mem; /* internal for coherent mem override */# ifdef CONFIG_DMA_CMA struct cma * cma_area;/* contiguous memory area for dma allocations */# endif/* arch specific additions */struct dev_archdata archdata; struct device_node * of_node;/* associated device tree node */------ struct fwnode_handle * fwnode related to the device tree;/* firmware device node */dev_t devt;/* dev_t, creates the sysfs "dev" */u32 id;/* device instance */spinlock_t devres_lock; struct list_head devres_head; struct klist_node knode_class; struct class * class; class const struct attribute_group ** groups;/* optional groups */void (* release) (struct device * dev); struct iommu_group * iommu_group; bool offline_disabled: 1; bool offline: 1 ;};2.2 device registration function
The device registration function device_register is divided into two steps. The first step is to initialize device_initialize, which is mainly to initialize the kset to the/sys/devices directory and other (such as PM-related), and then register the device, the function is device_add.
Int device_add (struct device * dev) {struct device * parent = NULL; struct kobject * kobj; struct class_interface * class_intf; int error =-EINVAL; dev = get_device (dev ); if (! Dev) goto done; if (! Dev-> p) {error = device_private_init (dev); if (error) goto done;}/** for statically allocated devices, which shoshould all be converted * some day, we need to initialize the name. we prevent reading back * the name, and force the use of dev_name () */if (dev-> init_name) {dev_set_name (dev, "% s ", dev-> init_name); ---------------- set dev-> init_name = NULL if the initial name is available;}/* subsystems can specify simple Device enumeration */if (! Dev_name (dev) & dev-> bus-> dev_name) dev_set_name (dev, "% s % u", dev-> bus-> dev_name, dev-> id); --- set the default if (! Dev_name (dev) {error =-EINVAL; goto name_error;} pr_debug ("device: '% s': % s \ n", dev_name (dev ), _ func _); parent = get_device (dev-> parent); kobj = get_device_parent (dev, parent); if (kobj) dev-> kobj. parent = kobj;/* use parent numa_node */if (parent & (dev_to_node (dev) = NUMA_NO_NODE) set_dev_node (dev, dev_to_node (parent);/* first, register with generic layer. * // * we require the name t O be set before, and pass NULL */error = kobject_add (& dev-> kobj, dev-> kobj. parent, NULL); --- create the Current device directory under the/sys/devices directory if (error) goto Error;/* policy platform of device entry */if (platform_policy) platform_policy (dev); error = device_create_file (dev, & dev_attr_uevent); -------- create a file uevent if (error) goto attrError; error = device_add_class_symlinks (dev) in the Current Device directory ); ------------------- create a link file if (error) Goto SymlinkError; error = device_add_attrs (dev); -------------------------- create other files, such as if (error) goto AttrsError; error = bus_add_device (dev) in the class directory ); ------------------------------ add device to the linked list of the bus-device if (error) goto BusError; error = dpm_sysfs_add (dev); if (error) goto DPMError; device_pm_add (dev ); if (MAJOR (dev-> devt) {error = device_create_file (dev, & dev_attr_dev); if (error) goto DevAt TrError; error = device_create_sys_dev_entry (dev); if (error) goto SysEntryError; devtmpfs_create_node (dev);}/* policy clients of device addition. this call must come * after dpm_sysfs_add () and before kobject_uevent (). */if (dev-> bus) blocking_notifier_call_chain (& dev-> bus-> p-> bus_notifier, bus_policy_add_device, dev); kobject_uevent (& dev-> kobj, KOBJ_ADD ); bus_probe_device (dev); -------- _ devic E_attach ------- call this function to match with the driver if (parent) ----------------------------------------- add to the parent device linked list klist_add_tail (& dev-> p-> knode_parent, & parent-> p-> klist_children); if (dev-> class) {----------------------------------- class-related operations mutex_lock (& dev-> class-> p-> mutex ); /* tie the class to the device */klist_add_tail (& dev-> knode_class, & dev-> class-> p-> klist_devices);/* define y any interfaces that the devic E is here */list_for_each_entry (class_intf, & dev-> class-> p-> interfaces, node) if (class_intf-> add_dev) class_intf-> add_dev (dev, class_intf ); mutex_unlock (& dev-> class-> p-> mutex );}......}
How does the device match the corresponding driver in the function _ device_attach? If no driver is added to the device, the corresponding driver will be found in the bus. See the function _ device_attach.
Static int _ device_attach (struct device * dev, bool allow_async) {int ret = 0; device_lock (dev); if (dev-> driver) {if (device_is_bound (dev )) {ret = 1; goto out_unlock;} ret = device_bind_driver (dev); --------- if a driver already exists, bind it to the device and perform probe if (ret = 0) ret = 1; else {dev-> driver = NULL; ret = 0 ;}} else {struct device_attach_data data = {. dev = dev ,. check_async = allow_async ,. want_async = False ,}; if (dev-> parent) pm_runtime_get_sync (dev-> parent); ret = bus_for_each_drv (dev-> bus, NULL, & data, _ device_attach_driver ); ---- if no driver exists, traverse the driver on the bus until the probe if (! Ret & allow_async & data. have_async) {/** If we cocould not find appropriate driver * synchronously and we are allowed to do * async probes and there are drivers that * want to probe asynchronously, we'll * try them. */dev_dbg (dev, "scheduling asynchronous probe \ n"); get_device (dev); async_schedule (_ folder, dev);} else {pm_request_idle (dev );} if (dev-> parent) pm_runtime_put (dev-> parent) ;}out_unlock: device_unlock (dev); return ret ;}2.3 other APIs
Device uninstall Function
extern void device_unregister(struct device *dev);
3. driver
First introduce the driver struct, then introduce the registration function
3.1 struct device_driver
Struct device_driver {const char * name; --------------------------- driver name struct bus_type * bus; -------------------------- bus struct module * owner; const char * mod_name; /* used for built-in modules */bool tables;/* disables bind/unbind via sysfs */enum probe_type; const struct of_device_id * handle; const struct acpi_device_id * handle; int (* probe) (struct device * dev); -------------- test initialization function int (* remove) (struct device * dev); --------------- delete function void (* shutdown) (struct device * dev); int (* suspend) (struct device * dev, pm_message_t state); --- PM-related int (* resume) (struct device * dev ); ------------------------ const struct attribute_group ** groups; const struct dev_pm_ops * PM; ----------------------------- pm-related struct driver_private * p ;};3.2 register a function
Int driver_register (struct device_driver * drv) {int ret; struct device_driver * other; BUG_ON (! Drv-> bus-> p); if (drv-> bus-> probe & drv-> probe) | (drv-> bus-> remove & drv-> remove) | (drv-> bus-> shutdown & drv-> shutdown )) printk (KERN_WARNING "Driver '% s' needs updating-please use" "bus_type methods \ n", drv-> name); other = driver_find (drv-> name, drv-> bus); if (other) {printk (KERN_ERR "Error: Driver '% s' is already registered," "aborting... \ n ", drv-> name); return-EBUSY;} ret = bus_add_driver (drv); ------------------ register if (ret) return ret here; ret = driver_add_groups (drv, drv-> groups); if (ret) {bus_remove_driver (drv); return ret;} kobject_uevent (& drv-> p-> kobj, KOBJ_ADD ); return ret ;}
See the bus_add_driver function.
Int bus_add_driver (struct device_driver * drv) {struct bus_type * bus; struct driver_private * priv; int error = 0; bus = bus_get (drv-> bus ); ----------------------- get the bus if (! Bus) return-EINVAL; pr_debug ("bus: '% s': add driver % s \ n", bus-> name, drv-> name ); priv = kzarloc (sizeof (* priv), GFP_KERNEL); if (! Priv) {error =-ENOMEM; goto out_put_bus;} klist_init (& priv-> klist_devices, NULL, NULL); priv-> driver = drv; drv-> p = priv; priv-> kobj. kset = bus-> p-> drivers_kset; ------- set drivers error = kobject_init_and_add (& priv-> kobj, & driver_ktype, NULL, "% s ", drv-> name); ------------ create the driver directory if (error) goto out_unregister; klist_add_tail (& priv-> knode_bus, & bus-> p-> klist_drivers); --- add the driver To the bus driver chain table, if (drv-> bus-> p-> drivers_autoprobe) {if (driver_allows_async_probing (drv) {pr_debug ("bus: '% s ': probing driver % s asynchronously \ n ", drv-> bus-> name, drv-> name); async_schedule (driver_attach_async, drv);} else {error = driver_attach (drv ); if (error) goto out_unregister; }}------------------ _ driver_attach ------------ call this function to initialize module_add_driver (drv-> owner, drv); error = driver_c Reate_file (drv, & driver_attr_uevent); if (error) {printk (KERN_ERR "% s: uevent attr (% s) failed \ n", _ func __, drv-> name);} error = driver_add_groups (drv, bus-> drv_groups); if (error) {/* How the hell do we get out of this pickle? Give up */printk (KERN_ERR "% s: driver_create_groups (% s) failed \ n", _ func __, drv-> name);} if (! Drv-> suppress_bind_attrs) {error = add_bind_files (drv); if (error) {/* Ditto */printk (KERN_ERR "% s: add_bind_files (% s) failed \ n ", _ func __, drv-> name) ;}} return 0 ;........}
Then, how does the driver match the corresponding device? Continue to explore the function _ driver_attach.
(Drivers/base/dd. c)
Static int _ driver_attach (struct device * dev, void * data) {struct device_driver * drv = data; int ret;/** Lock device and try to bind to it. we drop the error * here and always return 0, because we need to keep trying * to bind to devices and some drivers will return an error * simply if it didn't support the device. ** driver_probe_device () will spit a warning if there * is an error. */ret = d River_match_device (drv, dev); -------- call the bus match function to match if (ret = 0) {/* no match */return 0 ;} else if (ret =-EPROBE_DEFER) {dev_dbg (dev, "Device match requests probe deferral \ n"); driver_deferred_probe_add (dev);} else if (ret <0) {dev_dbg (dev, "Bus failed to match device: % d", ret); return ret;}/* ret> 0 means positive match */if (dev-> parent) /* Needed for USB */device_lock (dev-> pa Rent); device_lock (dev); if (! Dev-> driver) driver_probe_device (drv, dev); ------ if there is no driver, set this driver as the driver of the device, and then call the driver probe to initialize device_unlock (dev ); if (dev-> parent) device_unlock (dev-> parent); return 0 ;}3.3 other APIs
Unmount a function
extern void driver_unregister(struct device_driver *drv);