Linux Device Model and Sysfs - Arizona State University

Linux Device Model and Sysfs Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee [email protected] (480) 727-7507 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 CSE 530 EOSI Fall 2016 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; }; 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 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; }; 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); }; 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 */ }; 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; }; CSE 530 EOSI Fall 2016 ( 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 systems loaded modules CSE 530 EOSI Fall 2016 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 Internal External Kernel objects Directories Object attributes Regular files Object relationships Symbolic links

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 kobjects location If parent pointer is set -- Maps to subdirectory inside of parents 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 CSE 530 EOSI Fall 2016 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 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

8 Sysfs+cdev Example CSE 530 EOSI Fall 2016 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); CSE 530 EOSI Fall 2016 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

} CSE 530 EOSI Fall 2016 11 Operations on GPIOchips 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 CSE 530 EOSI Fall 2016 12

Recently Viewed Presentations

  • International accounting - Islamic University of Gaza

    International accounting - Islamic University of Gaza

    International accounting. Frederick D. S. Choi . Gary K. Meek. Chapter 7: Financial Reporting and Changing Prices Examples are prepared based on IFRS materials
  • Les Profils de Reactivitee Serique

    Les Profils de Reactivitee Serique

    Il n'y a pas de dérouillage matinal, les antalgiques et les anti-inflammatoires ont été de peu d 'effet. A présent, elle se plaint d'insomnie et de dépression. 13 des 18 « points de Yunus » sont douloureux à la pression,...
  • What Did Queen Elizabeth Look Like?

    What Did Queen Elizabeth Look Like?

    Queen Elizabeth The Rainbow Portrait One of the reasons Elizabeth stayed as queen for so long was because she made sure everybody knew how powerful and important she was, by having her portraits painted. Copies of these were sent all...
  • Introduction - University of Tennessee at Chattanooga

    Introduction - University of Tennessee at Chattanooga

    OS Concepts. An operating system (OS) provides the interface between the users of a computer and that computer's hardware.. An operating system manages the ways applications access the resources in a computer, including its disk drives, CPU, main memory, input...
  • Introduction to Art and Culture of Japan - East-West Center

    Introduction to Art and Culture of Japan - East-West Center

    Women in postwar era culture and society. Philosophical constructions of linear vs. cyclical worldview. Competing/complimentary styles of religious architecture. Evidence of classic Western landscape influence in East Asian art
  • Budgets Amendments - Florida Department of Health

    Budgets Amendments - Florida Department of Health

    Provided all of the supporting documentation . Labeled each document. Provided clear copies. Highlighted or circled pertinent information. Provided cost methodologies and allocations. Listed each document in the "List Supporting Documentation" section of each relevant schedule. Common Budget Issues
  • KPMG Talkbook template

    KPMG Talkbook template

    Immediate payments are possible with closed-loop Wallets - a touchless solution. Providing a digital identity. Free Bank Account & ATM through PradhanMantri Jan DhanYojana. BPL App for BPL Schemes & Benefits. ... Real time Identification - Touch & Go.
  • 72x36 Poster Template - College of Health Professions and ...

    72x36 Poster Template - College of Health Professions and ...

    2 Chandler JM, Hadley EC: Exercise to improve physiologic and functional performance in old age. ClinGeriatr Med 1996, 12:761-784. 3 Tallon MJ, Harris RC, Maffulli N, Tarnopolsky MA: Carnosine, taurine and enzyme activities of human skeletal muscle fibers from elderly...