14TheLinuxDeviceModel

上传人:sx****84 文档编号:242866144 上传时间:2024-09-10 格式:PPT 页数:36 大小:258KB
返回 下载 相关 举报
14TheLinuxDeviceModel_第1页
第1页 / 共36页
14TheLinuxDeviceModel_第2页
第2页 / 共36页
14TheLinuxDeviceModel_第3页
第3页 / 共36页
点击查看更多>>
资源描述
按一下以編輯母片標題樣式,按一下以編輯母片本文樣式,第二層,第三層,第四層,第五層,*,Chapter 14The Linux Device Model,CCU EE&COMM,1,The 2.6 device model,The model provides abstraction, which supports:,Power management and system shutdown,Understanding of the systems structure,Right order to shutdown,Communication with user space,Sysfs,Knobs for changing operating parameters,Hot-pluggable devices,Device classes,Describe devices at a functional level,Object lifecycles,Reference count,2,Device model tree,Sysfs (跑個tree /sys 吧?),/proc, /dev, /sysfs,Authors can ignore the model, and trust it,Understanding device model is good, if,struct,leaks,Ex. the generic DMA code works with,struct device,Advanced material that need not be read,3,Object oriented programming (插個花),Abstract Data typing,Information hiding,Encapsulation,Inheritance,Derive more specialized classes from a common class,Polymorphism,Refers to the objects ability to respond in an individual manner to the same message,Dynamic binding,Refers to the mechanism that resolves a virtual function call at runtime,You can derive modified action that override the old one even after the code is compiled .,Kobject, Kset,Bus, driver, device, partition,hotplug(), match(), probe(), kobj_type,4,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,5,Kobject, Ksets, and Subsystems,struct kobject,supports,Reference counting of objects,Tracking the lifecycle,Sysfs representation,A visible representation,Data structure glue,Made up of multiple hierarchies with numerous links,Hotplug event handling,Notify user space about the comings and goings of hardware,$(KERNELDIR)/lib/kobject*.c,6,Kobject basics (0/3),struct,kobject,const char *,k_name,;,char,nameKOBJ_NAME_LEN,;,struct,kref,kref,;,struct,list_head,entry;,struct,kobject,* parent;,struct,kset,*,kset,;,struct,kobj_type,*,ktype,;,struct,dentry,*,dentry,;,;,struct,kset,struct,subsystem *,subsys,;,struct,kobj_type,*,ktype,;,struct,list_head,list;,spinlock_t,list_lock,;,struct,kobject,kobj,;,struct,kset_hotplug_ops,*,hotplug_ops,;,;,Directory entry, maybe for sysfs,7,Kobject basics (1/3),Embedded kobjects,A common type embedded in other structures,A top-level, abstract class from which other classes are derived,Ex. in ch3,struct cdev ,struct kobject kobj;,struct module *owner;,struct *ops;,dev_t dev;,;,struct kobject *kp = ;,struct cdev *device = container_of(kp, struct cdev, kobj);,8,Kobject basics (2/3),Initialization,Set the entire kobject to 0, memset(),Set up some of fields with kobject_init(), ex. reference count to 1,Set the name by kobject_set_name(kobj, char *format, ),Set up the other field, such as ktype, kset and parent,Reference count,struct kobject *kobject_get(struct kobject *kobj); /+,void kobject_put(struct kobject *kobj); /-, 0 to cleanup,“struct module *owner” in struct cdev?,The existence of a kobject require the existence of module that created that kobject. ex. cdev_get(),9,Kobject basics (3/3),Release functions,Even predictable object life cycles become more complicated when sysfs is brought in; user-space programs can keep a reference for an arbitrary period of time.,Every kobject must have a release method.,The release method is not stored in the kobject itself,kobject types kobj_type,struct kobj_type ,void (*release)(struct kobject *);,struct sysfs_ops * sysfs_ops;,struct attribute * default_attrs;,;,The kobject contains a field, pointer ktype,If kobject is a member of kset, the pointer provided by kset,struct kobj_type *get_ktype(struct kobject*kobj);,跟sysfs有關,也許因為 擴充或overload方便,10,Kobject hierarchies, kset,The,parent,pointer and,ksets,“parent” points to another kobject, representing the next level up,“kset” is a collection of kobjects,kset are always represented in sysfs,Every kobject that is a member of a kset is represented in sysfs,11,ksets,Adding a kobject to a kset,kobjects kset must be pointed at the kset of interest,Call kobject_add(struct kobject *kobj); / reference count +,kobject_init( ) + kobject_add( ) kobject_register( ),Removing from the kset,kobject_del( ),kobject_del( ) + kobject_put( ), kobject_unregister( ),Operation on ksets,void kset_init(struct kset *kset);,int kset_add(struct kset *kset);,int kset_register(struct kset *kset);,void kset_unregister(struct kset *kset);,struct kset *kset_get(struct kset *kset);,void kset_put(struct kset *kset);,ktype, is used in preference to the,ktype,in a kobject,12,Kobjects hierarchy of block subsystem,13,Subsystems,Representation for a high-level portion of the kernel,Usually show up at the top of the sysfs,Block devices,block_subsys, /sys/block,Core device hierarchy,devices_subsys, /sys/devices,Every bus type known to the kernel,Driver authors almost never needs to create one,Probably want is to add a new “class”,Subsystem is really just a wrapper around a kset,struct subsystem ,struct kset kset;,struct rw_semaphore rwsem; / used to serialize access,;,14,Subsystems,fs/char_dev.c, line 442subsystem_init( /not public in sysfs,drivers/firmware/efivars.c, line 689subsystem_register( / Extensible Firmware Interface (EFI),drivers/pci/hotplug/pci_hotplug_core.c, line 672 subsystem_register(,drivers/base/sys.c, line 392 subsystem_register(/pseudo-bus for cpus, PICs, timers, etc,drivers/base/core.c, line 423 subsystem_register(&,devices_subsys,);,drivers/base/bus.c: line 697 subsystem_register(,drivers/base/bus.c: line 745 subsystem_register(&,bus_subsys,);,drivers/block/genhd.c, line 307 subsystem_register(&,block_subsys,);,drivers/base/class.c: line 148 subsystem_register(,drivers/base/class.c: line 567 subsystem_register(&,class_subsys,);,fs/debugfs/inode.c, line 308 subsystem_register(,kernel/power/main.c, line 259 subsystem_register(&,power_subsys,);,kernel/params.c, line 690 subsystem_register(&,module_subsys,);,kernel/ksysfs.c, line 49 subsystem_register( /kernel sysfs attr,security/seclvl.c, line 655 subsystem_register(&seclvl_subsys)/ BSD Secure Levels LSM,drivers/base/firmware.c: line 20 subsystem_register(s);,drivers/base/firmware.c: line 30 subsystem_register(&,firmware_subsys,);,15,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,16,Low-level sysfs operations,Every kobject exports attributes, in that its sysfs dir,#include ,Call kobject_add( ) to show up in sysfs,Default attributes,struct attribute ,char *name;,struct module *owner;,mode_t mode;,;,struct sysfs_ops ,ssize_t (*show)(*kobj, struct attribute *attr, char *buffer);,ssize_t (*store)(*kobj, struct attribute *attr, const char *buffer, size_t size);,;,(*release)( ),*sysfs_ops,*default_attrs,kfree();,kobj_type,PAGE_SIZE,*(show),*(store),sysfs_ops,snprintf();,attribute,“version”,*,S_IRUGO,17,Low-level sysfs operations,Non default attributes,Attributes can be added and removed at will,int sysfs_create_ kobject *kobj, struct attribute *attr);,int sysfs_remove_ kobject *kobj, struct attribute *attr);,The same show() and store() are called,Binary attributes,e.g., when a device is hot-plugged, a user-space program can be started via hot-plug mechanism and then passes the firmware code,struct bin_attribute ,struct attribute attr;,size_t size;,ssize_t (*read)(struct kobject *kobj, char *buffer, loff_t pos, size_t size);,ssize_t (*write)(struct kobject *kobj, char *buffer, loff_t pos, size_t size);,;,int sysfs_create_bin_file(*kobj, struct bin_attribute *attr);,int sysfs_remove_bin_file(*kobj, struct bin_attribute *attr);,Symbolic links,int sysfs_create_link(*kobj, struct kobject *target, char *name);,void sysfs_remove_link(*kobj, char *name);,18,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,19,Hotplug event generation,Hotplug event,a notification to user space from the kernel that something has changed in the systems configuration,is generated whenever a kobject is,created,(kobject_add) or,destroyed,(kobject_del,),e.g., a camera is plugged in USB cable, disk is repartitioned,To invoke /sbin/hotplug,/proc/sys/kernel/hotplug,specifies hotplug program path,Operations in “hotplug_ops” of kset,Search up via parent until finding a kset,(*filter): to suppress hotplug event generation,(*name): to pass the name of relevant subsystem for a parameter,(*hotplug): to add useful environment variables for hotplug script,詳細運作容後再述,20,Hotplug operations sample code,Filter example,User space may want to react to the addition of a disk or a partition, but it does not normally care about request queues.,static int block_hotplug_filter(struct kset *kset, struct kobject *kobj),struct kobj_type *ktype = get_ktype(kobj);,return (ktype = = ,The generation of hotplug events is usually handled by logic at the bus driver level,配著前面的block subsystem 圖,21,kobject_hotplug( ),Called by kobject_register( ),kobject_hotplug( ),努力的往爸爸方向找kset,呼叫kset/subsystem的filter(),呼叫kset/subsystem的name(),作為/sbin/hotplug 參數$1,return,呼叫kset/subsystem的hotplug(),建構環境變數,call_usermodehelper( ),Setup a completion without wait,0 to skip,22,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,23,Buses, devices, and drivers,Buses,Channel between the processor and one or more devices,Devices and device drivers,Once again, much of the material covered here will,never be needed,by many driver authors.,kobject,core,Driver,core,bus,driver,device,Functional view inside kernel,struct,device,struct,device,driver,struct,kobject,struct,ldd_device,struct,ldd_driver,24,Buses (0/2),struct bus_type ,char *,name,;,struct subsystem subsys;,struct kset drivers;,struct kset devices;,int (*,match,)(struct device *dev, struct device_driver *drv);,struct device *(*add)(struct device * parent, char * bus_id);,int (*,hotplug,) (struct device *dev, char *envp,int num_envp, char *buffer, int buffer_size);,/* Some fields omitted */,;,25,Buses (1/2),For example, lddbus in example.tgz,Bus registration,struct bus_type ldd_bus_type = ,.name = ldd,.match = ldd_match, /容後再述,.hotplug = ldd_hotplug, /容後再述,;,int _init ldd_bus_init(void) ,ret = bus_register( /ret value must be checked, / 在bus subsystem下, /sys/bus/ldd,ret = device_register(,Deregistration,void ldd_bus_exit(void),device_unregister(,bus_unregister(,26,Buses (2/2),Bus methods,int (*match)(struct device *device, struct device_driver *driver);,Called whenever a new device or driver is added for this bus,Return a nonzero value if the device can be handled by driver,static int ldd_match(struct device *dev, struct device_driver *driver),return !strncmp(dev-bus_id, driver-name, strlen(driver-name);,int (*hotplug) (struct device *device, char *envp, int num_envp, char *buffer, int buffer_size);,Allow the bus to add environment variables,直接看範例程式, LDDBUS_VERSION,Iterating over devices and drivers,bus_for_each_dev( ), bus_for_each_drv( ),Bus attributes,struct bus_attribute, (*show), (*store),BUS_ATTR(name, mode, show, store);, declare “struct bus_attr_name”,bus_create_file( ), bus_remove_file( ), 看,lddbus,的BUS_ATTR(version,27,Devices (0/1),struct device ,struct device *parent;,struct kobject kobj;,char bus_idBUS_ID_SIZE;,struct bus_type *,bus,;,struct device_driver *,driver,;,void *,driver_data,;,void (*,release,)(struct device *dev);,/* Several fields omitted */,;,Must be set before registering,device-kobj-parent,= &device-parent-kobj,kobject_unregister( ),kobject_del(),kobject_hotplug(),kobject_put(),kobject_release( ),ksets release,也就是device_release( ),dev-release( ),28,Devices (1/1),Device registration,int device_register(struct device *dev);,void device_unregister(struct device *dev);,An actual bus is a device and must be registered,static void ldd_bus_release(struct device *dev), printk(KERN_DEBUG lddbus releasen); ,struct device ldd_bus = ,.bus_id = ldd0,.release = ldd_bus_release,;,/ device_register( ) & unregister( ) 在 ldd_bus_init( ) & exit( )被叫,/ 在devices subsystem下, /sys/devices/ldd0/,Device attributes,struct device_attribute, DEVICE_ATTR( ), device_create_file, ,29,Device structure embeddingfor a specific bus (e.g., pci_dev, ldd_device),“struct device” contains the,device core,s information,Most subsystems track other about the devices they host,As a result, “struct device” is usually embedded,lddbus creates its own device type for ldd devices,struct ldd_device ,char *name;,struct ldd_driver *driver;,struct device dev;,;,#define to_ldd_device(dev) container_of(dev, struct ldd_device, dev);,sculld 多了device register之類動作, 納入sysfs, 理論上可以hotplug; scullp僅有module下有,30,Device drivers,struct device_driver ,char *name;,struct bus_type *bus;,struct kobject kobj;,struct list_head devices;,int (*probe)(struct device *dev);,int (*remove)(struct device *dev);,void (*shutdown) (struct device *dev);,;,31,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,32,Classes,net/core/net-sysfs.c, line 460 class_register(&,net_class,);,net/bluetooth/hci_sysfs.c, line 147 class_register(,drivers/pcmcia/cs.c, line 1892 class_register(,drivers/usb/core/file.c: line 90 class_register(,drivers/usb/core/hcd.c, line 649 class_register(,drivers/pci/probe.c, line 110 class_register(,還有很多,33,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,34,35,Outlines,Base type,Kobjects, Ksets, and Subsystems,Low-level sysfs operations,Derived type and interaction,Hotplug event generation,Buses, devices, and drivers,High level view,Classes,Put it all together,番外篇 Dealing with Firmware,36,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!