Imported more from last LDD course.
[cascardo/kernel/slides/.git] / 04char / chrdev
1 # Introduction
2
3 Devices in POSIX Systems are files in /dev directory. As with any files
4 in POSIX, they may be opened, closed, read from, written to, seeked,
5 ioctl'd, and others.
6
7 Examples of device files:
8
9 * /dev/sda - A SCSI block device
10 * /dev/ttyS0 - A Serial terminal device
11
12 # POSIX I/O calls
13
14 POSIX systems have some standard calls for I/O. Since devices are files,
15 these same system calls are used to work with devices. We are gonna work
16 with the following calls:
17
18 * open
19 * read
20 * write
21 * close
22 * lseek
23 * ioctl
24
25 # Device types and numbers
26
27 Linux devices may be of different types, including character devices,
28 block devices or network devices. Both character and block devices have
29 identifying numbers, a major and a minor number.
30
31 # Character devices allocation
32
33 In Linux, major and minor numbers have to be requested or allocated. The
34 calls to do that for character devices are:
35
36
37 * int register\\_chrdev\\_region (dev\\_t first, unsigned int count, char
38 *name);
39 * int alloc\\_chrdev\\_region (dev\\_t *dev, unsigned int firstminor,
40 unsigned int count, char *name);
41 * void unregister\\_chrdev\\_region (dev\\_t dev, unsigned int count);
42
43
44
45
46 # VFS
47
48 # Introduction
49
50 The virtual file system is the hub for almost all operations in a Linux-based
51 system. It allows IPC with pipes, access to devices, including storage through
52 regular files and organization with directories.
53
54 # Everything is a file
55
56 In Unix, there's a say: "everything is a file, if it's not a file, it's a
57 process". Well, most things are really files, and that's why the VFS is at the
58 center of the system, including for device drivers.
59
60 # Special files, procfs and others
61
62 When handling with special files (character and block device nodes), procfs
63 files and others, we'll use some common structures. These include the
64 *struct file\\_operations*, *struct file* and *struct inode*.
65
66 # File Operations
67
68 * open
69 * release
70 * read
71 * write
72 * ioctl
73 * llseek
74 * poll
75 * mmap
76 * many others
77
78 # Opened File
79
80 * f\\_mode
81 * f\\_flags
82 * f\\_pos
83 * f\\_op
84 * private\\_data
85
86 # Filesystem File: inode
87
88 The inode is a representation of the file as in its filesystem, including its
89 major/minor numbers and pointers to the corresponding device representation.