Download presentation
Presentation is loading. Please wait.
Published byJosh Gass Modified over 10 years ago
1
Sogang University Advanced Operating Systems (Linux Device Drivers) Advanced Operating Systems (Linux Device Drivers) Sang Gue Oh, Ph.D. Email : sgoh@macroimpact.com Email : sgoh@macroimpact.com
2
Sogang University Linux Device Drivers Page 2 Device Driver Overview (1) Device Driver Overview (1) n Hardware Controller t Start & stop, initialize, control, diagnose the devices. n Device Driver t Software that handles or manages hardware controllers. t Located in the kernel space. t Usually built with module structure. t Identified with major & minor number. Devices controlled by the same device driver have a common major device number n Devices in the Linux t All HW devices look like regular files (i.e., All devices are accessed through names in the file system). t Devices appear in /dev directory (e.g., /dev/name). t Devices are accessed by using same file system calls (open, read, write, close, etc.)
3
Sogang University Linux Device Drivers Page 3 Device Driver Overview (2) Device Driver Overview (2) open() read() ioctl() write() close() open() read() ioctl() write() close() open() read() ioctl() write() close() Process 1Process 2Process n User Dev 1 Dev 2 Dev n Set of Registers Device Interface Table (devs[# of devices]) Xopen() Xread() Xioctl() Xwrite() Xclose() Yopen() Yread() Yioctl() Ywrite() Yclose() Yopen() Yread() Yioctl() Ywrite() Yclose() Yopen() Yread() Yioctl() Ywrite() Yclose() Module Register / Un-register Invoke Control X File System Calls Kernel FunctionPointers Hardware Controllers DeviceDrivers
4
Sogang University Linux Device Drivers Page 4 Types of Devices Types of Devices n Character Devices t Processed sequentially and are therefore accessed without buffer. t Console, Keyboard, Sound cards, Scanner, Printer, etc. Identified by a c in the 1st column of the output of ls -l. n Block Devices t Random access. t Accesses are handled transparently by the buffer cache. t Data are accessed by the block. t Hard driver, floppy drive, tape driver, etc. t Identified by a b in the 1st column of the output of ls -l. n Network Devices t Have different interface structures. t Not listed in the /dev directory t Will be covered later.
5
Sogang University Linux Device Drivers Page 5 Registering / Unregistering a Device Driver Registering / Unregistering a Device Driver n Device drivers should be registered before using them and un- registered after the use. n Since device drivers are usually implemented using modules, registration takes place when the module is inserted (init_module()) or removed (cleanup_module()). n Function calls (Character device) register_chrdev(unsigned int major, const char *name, struct file_operations *fops); l major : Major number l name : Device name l fops : File operations related to this device. t unregister_chrdev(unsigned int major, const char *name); t chr -> blk for the block devices, chr -> net for the network devices.
6
Sogang University Linux Device Drivers Page 6 Identifying a Device Driver in the Kernel Identifying a Device Driver in the Kernel n Major and minor number t Major number identifies the driver associated with the device. (e.g., /dev/null and /dev/zero are managed by driver 1, while all the ttys are managed by driver 4). Indexes to the blkdevs[ ] or chrdevs[ ]. Minor number is only used by the device driver; other parts of the kernel dont use it, and merely pass it along to the driver. The minor number provides a way to differentiate several devices in one driver. (e.g., eth0, eth1, etc.). Indexes to the internal virtual devices. n Allocating a major number t Static allocation : Chooses a unused major number from the table and use it when registering the driver (Documentation/devices.txt). t Dynamic allocation : Provides 0 when registering a driver and ask the system to assign one unused number for that driver. n /proc/devices file contains the device name and major number.
7
Sogang University Linux Device Drivers Page 7 Creating a Device File Creating a Device File n Device files should be located at /dev directory. Must be a super-user to create a device file. Command: mknod /dev/name type major_number minor_number t Create the special file NAME of the given TYPE with the MAJOR and MINOR numbers. t Type l b : create a block (buffered) special file l c, u: create a character (unbuffered) special file l p: create a FIFO t Minor numbers should be in the range of 0-255.
8
Sogang University Linux Device Drivers Page 8 File Operations File Operations struct file_operations { loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *); int (*fasync) (int, struct file *, int); int (*check_media_change) (kdev_t dev); int (*revalidate) (kdev_t dev); int (*lock) (struct file *, int, struct file_lock *); };
9
Sogang University Linux Device Drivers Page 9 Assigning File Operations (gcc extension) Assigning File Operations (gcc extension) struct file_operations fops = { read: device_read, write: device_write, open: device_open, release: device_release }; struct file_operations fops = { NULL, device_read, device_write, NULL, ….. device_open, ….. device_release, ….. NULL }; struct file_operations fops = {.read = device_read,.write = device_write,.open = device_open,.release = device_release };
10
Sogang University Linux Device Drivers Page 10 Overview of /Proc File System Overview of /Proc File System n Originally designed to allow easy access to information about processes (e.g., /proc/modules, /proc/meminfo, etc). n Kernel and kernel modules send information to processes through proc file system. n Memory file system rather than a disk file system. n Very similar to the one used with device drivers. n Mainly written for one direction operation (read from the proc). n Registering/Unregistering t proc_register(&proc_root, &my_entry); l proc_root : root directory for the proc file system (i.e., /proc). l my_entry : proc structure (proc_dir_entry). t proc_unregister(&proc_root, inode #); l Inode # : inode # for this proc file. Initially zero. Automatically filled when proc_register is called.
11
Sogang University Linux Device Drivers Page 11 Proc Structure (proc_dir_entry) Proc Structure (proc_dir_entry) struct proc_dir_entry { unsigned short low_ino;/* inode # */ unsigned short namelen; /* length of the file name */ const char *name;/* file name */ mode_t mode;/* file mode */ nlink_t nlink;/* number of links */ uid_t uid;/* uid */ gid_t gid;/* gid */ unsigned long size;/* size of file reported by ls */ struct inode_operations *proc_iops; /* inode operations */ struct file_operations *proc_fops; /* file operations */ ….. };
12
Sogang University Linux Device Drivers Page 12 Information on /proc File System (1) Information on /proc File System (1) n Provides information on the current status of the Linux kernel and running processes. t cmdline : arguments passed to the kernel when the system is started up. t cpuinfo : description of processor(s) used t devices : list of device controllers included in the kernel t dma : list of dma channel used by device controllers. t filesystems : list of file systems supported by the kernel t interrupts : list of hard interrupts used by the device controllers. t ioports : list of I/O ports used by device controllers. t kcore : memory allocated to the kernel. t kmsg : last message displayed by the kernel. t ksyms : list of kernel symbols used by modules. t loadavg : system loading. t locks : list of locks assigned to files. t meminfo : process status of central memory. t modules : list of modules loaded in the kernel.
13
Sogang University Linux Device Drivers Page 13 Information on /proc File System (2) Information on /proc File System (2) t mounts : list of file systems mounted. t pci : list of devices connected on the PCI bus. t profile : information on kernel profiling, used to determine the time spent executing each of the functions. t rtc : information about the real-time clock. t stat : various statistics on operations carried out by the kernel (processor time consumed, number of disk I/O, number of memory page loads, etc.) t smp : information about multi-processor operations. t uptime : time elapsed since system start-up. t version : kernel version. n In addition to these files, the directory /proc contains several directories: t net : files containing information on network protocols. t scsi : files containing information on the control of SCSI devices. t sys : files containing information linked to kernel variables managed by the primitive sysctl. t self : link to the directory corresponding to the current process.
14
Sogang University Linux Device Drivers Page 14 Information on /proc File System (3) Information on /proc File System (3) n Each process in the system which is currently running is assigned a directory /proc/pid, where pid is the process id. This directory contains files holding information on certain characteristics of the process. t cmdline : list of process arguments. t cwd : link to the current directory of the process. t environ : list of variables in the process environment. t exe : link to the binary file executed by the process. t fd : directory containing links to files opened by the process. t maps : list of memory zones contained in the process address space. t mem : contents of the process address space. t root : link to the root directory of the process. t stat, statm, status : state of the process.
15
Sogang University Linux Device Drivers Page 15 Examples (Device Drivers and /proc) Examples (Device Drivers and /proc) n Linux Kernel Module Programming Guide t By Peter Jay Salzman and Ori Pomenrantz, 2001. t Can download from http://dcclab.sogang.ac.kr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.