A preliminary study on Linux Drive model 3--device

Source: Internet
Author: User
Tags mutex

A preliminary study on Linux Drive model 3--device
before I speak device, I'm going to introduce a metaphor that comes from a senior (z2007b). Driver is handsome, device is beautiful, bus is a matchmaker, bus is to provide device and driver match place (method?). )。 Well, for the time being, this is about beauty. 1, the old rule, first look at Struce device This beauty has what features (members) and methodsstruct Device {struct device *parent;struct device_private*p;struct kobject kobj;const char *init_name;/* Initial name of the device * /
struct bus_type *bus;/* Type of bus device is on */struct device_driver *driver;/* Which driver have allocated this...struct Klist_node knode_class;struct class *class;const struct Attribute_group **groups;/* Optional groups * /
void (*release) (struct device *dev);...}look at the class diagram (the class diagram is complicated by me, I do not know how to organize, first cut a small section)

2,device_register () method
int device_register (struct device *dev){device_initialize (dev);return Device_add (dev);}analyze Device_initialize First (dev) and generally know its function: Initialize the device structure, including list/mutex/pm/ ... -dev->kobj.kset = Devices_kset;
Kobject_init (&dev->kobj, &device_ktype);///The previous sentence is to create a new devices directory under the/sys/directory.
Init_list_head (&dev->dma_pools);
Mutex_init (&dev->mutex);
Lockdep_set_novalidate_class (&dev->mutex);
Spin_lock_init (&dev->devres_lock);
Init_list_head (&dev->devres_head);
device_pm_init (dev);
Set_dev_node (Dev,-1);
re-analysis of device_add (dev); the approximate function is to return the initialized device to the system's device level
device_private_init (dev);//If the private member is not initialized, Init here. |dev_set_name (Dev, "%s", dev->init_name);->kobject_set_name_vargs (&dev->kobj, FMT, Vargs);//If the device's init_name is specified, through Kobject_set_name_vargs, the init_name is actually assigned to the Dev->kobject->name|if (!dev_name (dev) && dev->bus && dev->bus->dev_name)dev_set_name (Dev, "%s%u", Dev->bus->dev_name, dev->id);//If Init_name is not specified, then the Bus->dev_name + Dev->id is combined into a dev-kobject->name name|Kobject_add (&dev->kobj, dev->kobj.parent, NULL);// go back to the Kobject system and manage his uevent,kref and so on. You have specified a name, why pass a null value? |device_create_file (Dev, &uevent_attr);//Create Uevent Property|device_create_file (Dev, &devt_attr);//If the dev->devt is already instantiated, then the relevant dev node is established. Where Devt is the return value of the Mkdev function|device_add_class_symlinks (Dev)//If class is instantiated, the symlink of class is established|Blocking_notifier_call_chain (&dev->bus->p->bus_notifier,bus_notify_add_device, dev);kobject_uevent (&dev->kobj, kobj_add);//through the uevent mechanism, notify the user layer, such as a device inserted, can be mounted. | bus_probe_device(dev);//notice where the bus, a device joined, and probe. -if (bus->p->drivers_autoprobe) {//has already put this in the Device_init 1. ret = device_attach(Dev); Analysis of Device_attch and Driver_attch on Linux drive Model 2}
the realization of 3,kf_deviceCode Snippet:static void Kf_device_release (struct device *dev) {PRINTK ("%s\n", __func__);

}static struct device kf_device={. Init_name = "Kf-device0",. devt = MKDEV (kfmajor,1),. Release = Kf_device_release,};static int kf_device_register (void) {
int ret =-1;//do not the This class//kf_device.class = &kf_class; Kf_device.bus = &kf_bus_type; kf_device.parent = &kf_bus;ret = Device_register (&kf_device);if (Ret < 0) {PRINTK ("KF device Reister error!\n");    }PRINTK ("KF device Reister ok!\n");return ret;}static void Kf_device_unregister (void) {Device_unregister (&kf_device);}compiled into KO, on the platform, insod up, let's take a look at the SYS file system.
uevent:1,device_register (struct device *dev)->device_add (dev)->device_create_file (Dev, &uevent_attr);2, if the class is specified, the relevant directory will be built in the class directory, through the functiondevice_add_class_symlinks (dev);device_add_attrs (dev);3, if the relevant bus is specified, the relevant node will be built in the/sys/bus/Bus_add_device (dev);
Power:1,device_register (struct device *dev)->device_add (dev)->dpm_sysfs_add (dev);->ysfs_merge_group (& Dev->kobj, &pm_runtime_attr_group);     ->pm_runtime_attr_group< Span style= "Line-height:1.428571em" is the Power directory, several files in this directory static struct attribute *runtime_attrs [] = {
#ifdef config_pm_runtime
#ifndef config_pm_advanced_debug
      &dev_attr_runtime_status . attr,
#endif
     &dev_attr_control . attr,
      &dev_attr_runtime_suspended_time . attr,
     &dev_attr_runtime_active_time . attr,
     &dev_attr_autosuspend_delay_ms . attr,
#endif/* config_pm_runtime */
     NULL,
}; Device_driver member in 2,struct device, and no device member in struct Device_drivercan also indicate that device (beauty) is very specific, she can only have the specified driver. and Device_driver (handsome) very much, he has a lot of device, so he can not specify a device in his members.

A preliminary study on Linux Drive model 3--device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.