Some "build" fixes in char device.
[cascardo/kernel/slides/.git] / 04char / chrdev
1 %Character Devices
2 %Thadeu Cascardo
3
4 # Introduction
5
6 Devices in POSIX Systems are files in /dev directory. As with any files
7 in POSIX, they may be opened, closed, read from, written to, seeked,
8 ioctl'd, and others.
9
10 Examples of device files:
11
12 * /dev/sda - A SCSI block device
13 * /dev/ttyS0 - A Serial terminal device
14
15 # POSIX I/O calls
16
17 POSIX systems have some standard calls for I/O. Since devices are files,
18 these same system calls are used to work with devices. We are gonna work
19 with the following calls:
20
21 * open
22 * read
23 * write
24 * close
25 * lseek
26 * ioctl
27
28 # Device types and numbers
29
30 Linux devices may be of different types, including character devices,
31 block devices or network devices. Both character and block devices have
32 identifying numbers, a major and a minor number.
33
34 # VFS
35
36 # Introduction
37
38 The virtual file system is the hub for almost all operations in a Linux-based
39 system. It allows IPC with pipes, access to devices, including storage through
40 regular files and organization with directories.
41
42 # Everything is a file
43
44 In Unix, there's a say: "everything is a file, if it's not a file, it's a
45 process". Well, most things are really files, and that's why the VFS is at the
46 center of the system, including for device drivers.
47
48 # Special files, procfs and others
49
50 When handling with special files (character and block device nodes), procfs
51 files and others, we'll use some common structures. These include the
52 *struct file\\_operations*, *struct file* and *struct inode*.
53
54 Do not forget to include linux/fs.h.
55
56 # File Operations
57
58 * owner
59 * open
60 * release
61 * flush
62 * read
63 * write
64 * ioctl
65 * unlocked\\_ioctl
66 * llseek
67 * poll
68 * mmap
69 * many others
70
71 # Opened File
72
73 * f\\_mode
74 * f\\_flags
75 * f\\_pos
76 * f\\_op
77 * private\\_data
78
79 # Filesystem File: inode
80
81 The inode is a representation of the file as in its filesystem, including its
82 major/minor numbers and pointers to the corresponding device representation.
83
84 # Character Devices
85
86 # Device Number Macros
87
88 Nowadays, major number is 12 bits and minor number is 20 bits. This may change
89 in the future, so macros should be used.
90
91 * include linux/kdev\\_t.h
92 * MAJOR
93 * MINOR
94 * MKDEV
95
96 # Character devices allocation
97
98 In Linux, major and minor numbers have to be requested or allocated. The
99 calls to do that for character devices are:
100
101 * int register\\_chrdev\\_region (dev\\_t first, unsigned int count, char
102 *name);
103 * int alloc\\_chrdev\\_region (dev\\_t *dev, unsigned int firstminor,
104 unsigned int count, char *name);
105 * void unregister\\_chrdev\\_region (dev\\_t dev, unsigned int count);
106
107 # cdev structure
108
109 Use *cdev\\_alloc* or *cdev\\_init* to allocate or initalize a cdev structure.
110
111 Use *cdev\\_add* and *cdev\\_del* to register and unregister it.
112
113 # Open/Release
114
115 Implementing open and release should be very simple for many devices. They
116 usually allocate data, and multiplex devices by minor number.
117
118 # Inode structure
119
120 Use *imajor* and *iminor* to get the major and minor number from an inode
121 structure.
122
123 # Read
124
125 Read may write to user space less bytes than requested.
126
127 # Write
128
129 Write is very similar to read. Only problem is that many applications misbehave
130 when driver writes less bytes than requested.