Linux Device Model and Sysfs Computer Science Engineering

  • Slides: 12
Download presentation
Linux Device Model and Sysfs Computer Science & Engineering Department Arizona State University Tempe,

Linux Device Model and Sysfs Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu. edu (480) 727 -7507

Linux Device Model q A device model after 2. 5 v multiple hierarchies to

Linux Device Model q A device model after 2. 5 v multiple hierarchies to represent devices, driver, subsystems, their relaions (physical or logical) and how they are integrated q Objects (data struct) and their relations v a device is attached to a bus v a driver to manage a gpio chip v a mouse to behave as an input device q To support v Power management and v v system shutdown Communications with user space Hotpluggable devices Device classes Object lifecycles CSE 530 EOSI Fall 2016 1

Kobject q fundamental structure for kernel objects q embedded in a container (e. g.

Kobject q fundamental structure for kernel objects q embedded in a container (e. g. device struct) v finding its embedded kobject or the container v the struct cdev object associated with a struct kobject pointed to by kp struct cdev *device = container_of(kp, struct cdev, kobj); struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; unsigned int count; }; § parent: The parent node of the kobject structure § sd: kobject corresponds to the sysfs directory § state_initialize: 1 representative kobject has already been initialized § state_in_sysfs: Kobject is already in the sysfs file system establishing entrance CSE 530 EOSI Fall 2016 struct kobject { const char *name; struct list_head entry; struct kobject *parent; struct kset *kset; struct kobj_type *ktype; struct sysfs_dirent *sd; struct kref; unsigned int state_initialized: 1; unsigned int state_in_sysfs: 1; unsigned int state_add_uevent_sent: 1; unsigned int state_remove_uevent_sent: 1; unsigned int uevent_suppress: 1; }; 2

Ktype and Kset q Kobjects are associated with a specific ktype v ktypes define

Ktype and Kset q Kobjects are associated with a specific ktype v ktypes define some default properties of related kobjects v Instead of each kobject defining own behavior, behavior is stored in ktype and the kobjects of the same “type” point at the same ktype structure v has a list of default attributes (pointer to an array of pointers to attribute structures) struct kobj_type { void (*release)(struct kobject *kobj); const struct sysfs_ops *sysfs_ops; struct attribute **default_attrs; const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj); const void *(*namespace)(struct kobject *kobj); }; struct sysfs_ops { ssize_t (*show)(struct kobject *, struct attribute *, char *); ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); }; CSE 530 EOSI Fall 2016 struct attribute { const char *name; /* as it appears in a sysfs directory */ struct module *owner; /* no longer used */ mode_t mode; /* file protection bits, e. g. , S_IRUGO */ }; 3

Kset q Kset defines a group of kobjects v the top-level container class for

Kset q Kset defines a group of kobjects v the top-level container class for kobjects v the kobjects can be individually different "types" but overall these kobjects all want to be grouped together and operated on in the same manner. v can define the attribute callbacks and other common events that happen to a kobject. struct kset { struct list_head list; spinlock_t list_lock; struct kobject kobj; const struct kset_uevent_ops *uevent_ops; }; CSE 530 EOSI Fall 2016 (http: //www. cnblogs. com/hello 2 mhb/p/3365204. html) 4

What is “sysfs” q Linux sysfs v an in-memory virtual filesystem that provides a

What is “sysfs” q Linux sysfs v an in-memory virtual filesystem that provides a view of the kobject hierarchy. v enables users to view the device topology as a simple filesystem. v Using attributes, kobjects can export files that enable kernel variables to be read from and optionally written to. q sysfs root “/sys” contains at least 10 directories: block, bus, class, devices, firmware, fs, kernel, module, and power. For instance v bus -- provides a view of the system buses v class -- contains a view of the devices on the system organized by v v high-level function devices -- gives the device topology of the system and maps directly to the hierarchy of device structures inside the kernel dev -- a view of registered device nodes kernel -- contains kernel configuration options and status information modules -- contains a view of the system’s loaded modules CSE 530 EOSI Fall 2016 sys/ block/ bus/ class/ devices/ firmware/ fs/ kernel/ module/ power/ 5

Sysfs Tree q Directories, files, and links Internal External q Kobjects are embedded in

Sysfs Tree q Directories, files, and links Internal External q Kobjects are embedded in some Kernel objects Directories class-specific structure and managed “behind the scenes” by the associated driver subsystem. q Kobject initialization Object attributes Regular files Object relationships Symbolic links void kobject_init(struct kobject *kobj, struct kobj_type *ktype); q To add kobject directory to sysfs: tying kobjects to directory entries via the dentry member of each kobject int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, . . . ); q Where it is added depends on kobject’s location v If parent pointer is set -- Maps to subdirectory inside of parent’s directory v If parent pointer is NULL -- Maps to subdirectory inside kset->kobj v If neither parent nor kset fields are set, Maps to root-level directory in sysfs CSE 530 EOSI Fall 2016 6

sysfs Interface (include/linux/sysfs. h) q definition of attribute, attribute_group, sysfs ops q macros to

sysfs Interface (include/linux/sysfs. h) q definition of attribute, attribute_group, sysfs ops q macros to define attributes q create and remove v file v group v dir v link v chmod for files struct attribute { const char umode_t }; *name; mode; struct attribute_group { const char *name; umode_t (*is_visible)(struct kobject *, struct attribute *, int); struct attribute **attrs; struct bin_attribute **bin_attrs; }; struct device_attribute { struct attribute attr; ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); }; CSE 530 EOSI Fall 2016 7

Sysfs+cdev Example CSE 530 EOSI Fall 2016 8

Sysfs+cdev Example CSE 530 EOSI Fall 2016 8

GPIO Sysfs (1) q In postcore initialization, gpiolib_sysfs_init() v class_register(&gpio_class) to register gpio class

GPIO Sysfs (1) q In postcore initialization, gpiolib_sysfs_init() v class_register(&gpio_class) to register gpio class in sysfs static struct class_attribute gpio_class_attrs[] = { __ATTR(export, 0200, NULL, export_store), __ATTR(unexport, 0200, NULL, unexport_store), __ATTR_NULL, }; static struct class gpio_class = {. name = "gpio", . owner = THIS_MODULE, . class_attrs = gpio_class_attrs, }; v for each gpio device, gpiochip_sysfs_register() to register gpiochip. N under gpio_class static struct attribute *gpiochip_attrs[] = { &dev_attr_base. attr, &dev_attr_label. attr, &dev_attr_ngpio. attr, NULL, }; ATTRIBUTE_GROUPS(gpiochip); CSE 530 EOSI Fall 2016 9

GPIO Sysfs (2) q When writing N to gpio/export v export_store is called, then

GPIO Sysfs (2) q When writing N to gpio/export v export_store is called, then gpio_export v device_create_with_groups to create gpio. N static struct attribute *gpio_attrs[] = { &dev_attr_direction. attr, &dev_attr_edge. attr, &dev_attr_value. attr, &dev_attr_active_low. attr, NULL, }; static const struct attribute_group gpio_group = {. attrs = gpio_attrs, . is_visible = gpio_is_visible, }; static const struct attribute_group *gpio_groups[] = { &gpio_group, NULL } CSE 530 EOSI Fall 2016 10

Operations on GPIOchip’s q gpiochip_export is called from v gpiochip_add v gpiolib_sysfs_init (postcore_initcall) q

Operations on GPIOchip’s q gpiochip_export is called from v gpiochip_add v gpiolib_sysfs_init (postcore_initcall) q call device_create_with_groups() with gpiochip_group (a group of attributes) allocate dev (struct dev) add attribute group to device_add() add kobj to its parent device_add_groups() sysfs_create_group kerfs_create_dir() & create_files() for each attribute, sysfs_add_file_mode_ns __kernfs_create_file() and assign ops to kn v kernfs_node contains “kernfs_elem_attr” for files kernfs_ops CSE 530 EOSI Fall 2016 11