Linux Device Model A device model after 2.5

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
CSI 400/500 Operating Systems Spring 2009 Lecture #14 – Device Management and Drivers Monday, March 23 rd, 2009.
USERSPACE I/O Reporter: R 張凱富.
Concepts about the file system 2. The disk structure 3. Files in disk – The ext2 FS 4. The Virtual File System (c) 2013, Prof. Jordi Garcia.
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
File Management. Persistent storage Shared device Why Programmers Need Files HTML Editor HTML Editor … … Web Browser Web Browser Structured information.
Chapter 10: File-System Interface
USB Driver and Linux Device Model
Linux Device Model Part 2
Data Structures in the Kernel Sarah Diesburg COP 5641.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
The file structure and related utilities CS240 Computer Science II.
CS 6560 Operating System Design Lecture 13 Finish File Systems Block I/O Layer.
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Input Systems (ESP – Fall 2014) Computer Science & Engineering Department Arizona State.
GPIO interface From programmer’s view
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Modules and Device Drivers (ESP – Fall 2014) Computer Science & Engineering Department.
Linux+ Guide to Linux Certification, Second Edition
C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come?
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
UNIX Files File organization and a few primitives.
Real-time Systems Lab, Computer Science and Engineering, ASU Quark SPI Interface (ESP – Fall 2014) Computer Science & Engineering Department Arizona State.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Linux Device Model Part 1
7/23 Coldfire 5211 Signals and IO Multiplexing Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
CE Operating Systems Lecture 17 File systems – interface and implementation.
1 © 2001 John Urrutia. All rights reserved. Chapter 4 The LINUX Filesystem.
Interfacing Device Drivers with the Kernel
Linux File system Implementations
User-Space-to-Kernel Interface
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Chapter 10: File-System Interface Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 1, 2005 File-System Interface.
The Unix File system (UFS) Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University.
NCHU System & Network Lab Lab 14 File and Directory.
Lecture 19 Linux/Unix – File System
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
Linux file systems Name: Peijun Li Student ID: Prof. Morteza Anvari.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Fall 2013 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to Embedded Systems Dr. Jerry Shiao, Silicon Valley University.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Overview Dr. Xiao Qin Auburn University
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
Kernel data structures. outline linked list queues maps binary trees algorithmic complexity.
FPGA Support in the upstream kernel Alan Tull
EXT in Detail High-Performance Database Research Center
Introduction to Kernel
Introduction to Developing Embedded Linux Device Drivers
GPIO Driver and Driver Binding
Garbage collection for C
struct bus_type platform_bus_type bus_register(&platform_bus_type);
Computer System Laboratory
Module 10: File-System Interface
Linux Kernel Driver.
The PCI bus (Peripheral Component Interconnect ) is the most commonly used peripheral bus on desktops and bigger computers. higher-level bus architectures.
Zephyr Device Driver and Device Model
Advanced Programming Basics
Lecture 44 Syed Mansoor Sarwar
Memory Allocation CS 217.
Honnappa Nagarahalli Principal Software Engineer Arm
chapter 3-Char Device Driver
File System Implementation
CSE 451 Fall 2003 Section 11/20/2003.
Implementation of Embedded OS
Module 10: File-System Interface
Introduction to Linux Device Drivers
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

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 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

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

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 */

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; }; (http://www.cnblogs.com/hello2mhb/p/3365204.html)

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/

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

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); };

Sysfs+cdev Example

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);

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 }

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