Download presentation
Presentation is loading. Please wait.
0
Linux Device Model and Sysfs
Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee (480)
1
Linux Device Model A device model after 2.5
multiple hierarchies to represent devices, driver, subsystems, their relaions (physical or logical) and how they are integrated Objects (data struct) and their relations a device is attached to a bus a driver to manage a gpio chip a mouse to behave as an input device To support Power management and system shutdown Communications with user space Hotpluggable devices Device classes Object lifecycles
2
Kobject fundamental structure for kernel objects
embedded in a container (e.g. device struct) finding its embedded kobject or the container 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; }; 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 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; }; 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
3
Ktype and Kset Kobjects are associated with a specific ktype
ktypes define some default properties of related kobjects 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 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); 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 */
4
Kset Kset defines a group of kobjects
the top-level container class for kobjects the kobjects can be individually different "types" but overall these kobjects all want to be grouped together and operated on in the same manner. 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; }; (
5
What is “sysfs” Linux sysfs
an in-memory virtual filesystem that provides a view of the kobject hierarchy. enables users to view the device topology as a simple filesystem. Using attributes, kobjects can export files that enable kernel variables to be read from and optionally written to. sysfs root “/sys” contains at least 10 directories: block, bus, class, dev, devices, firmware, fs, kernel, module, and power. For instance bus -- provides a view of the system buses class -- contains a view of the devices on the system organized by 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 sys/ block/ bus/ class/ dev/ devices/ firmware/ fs/ kernel/ module/ power/
6
Sysfs Tree Directories, files, and links Kobjects are embedded in some
class-specific structure and managed “behind the scenes” by the associated driver subsystem. Kobject initialization void kobject_init(struct kobject *kobj, struct kobj_type *ktype); 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, ...); Where it is added depends on kobject’s location If parent pointer is set -- Maps to subdirectory inside of parent’s directory If parent pointer is NULL -- Maps to subdirectory inside kset->kobj If neither parent nor kset fields are set, Maps to root-level directory in sysfs Internal External Kernel objects Directories Object attributes Regular files Object relationships Symbolic links
7
sysfs Interface (include/linux/sysfs.h)
definition of attribute, attribute_group, sysfs ops macros to define attributes create and remove file group dir link chmod for files struct attribute { const char *name; umode_t 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); };
8
Sysfs+cdev Example
9
GPIO Sysfs (1) In postcore initialization, gpiolib_sysfs_init()
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, for each gpio device, gpiochip_sysfs_register() to register gpiochipN under gpio_class static struct attribute *gpiochip_attrs[] = { &dev_attr_base.attr, &dev_attr_label.attr, &dev_attr_ngpio.attr, NULL, }; ATTRIBUTE_GROUPS(gpiochip);
10
GPIO Sysfs (2) When writing N to gpio/export
export_store is called, then gpio_export device_create_with_groups to create gpioN 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 }
11
Operations on GPIOchip’s
gpiochip_export is called from gpiochip_add gpiolib_sysfs_init (postcore_initcall) call device_create_with_groups() with gpiochip_group (a group of attributes) allocate dev (struct dev) add attribute group to dev 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 kernfs_node contains “kernfs_elem_attr” for files kernfs_ops
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.