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,
7 Examples of device files:
9 * /dev/sda - A SCSI block device
10 * /dev/ttyS0 - A Serial terminal device
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:
25 # Device types and numbers
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.
31 # Character devices allocation
33 In Linux, major and minor numbers have to be requested or allocated. The
34 calls to do that for character devices are:
37 * int register\\_chrdev\\_region (dev\\_t first, unsigned int count, char
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);
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.
54 # Everything is a file
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.
60 # Special files, procfs and others
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*.
86 # Filesystem File: inode
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.
96 # Device Number Macros
98 Nowadays, major number is 12 bits and minor number is 20 bits. This may change
99 in the future, so macros should be used.
107 Use *register\\_chrdev\\_region* or *alloc\\_chrdev\\_region* to register or
108 allocate device numbers.
110 Use *unregister\\_chrdev\\_region* to unregister it.
114 Use *cdev\\_alloc* or *cdev\\_init* to allocate or initalize a cdev structure.
116 Use *cdev\\_add* and *cdev\\_del* to register and unregister it.
120 Implementing open and release should be very simple for many devices. They
121 usually allocate data, and multiplex devices by minor number.
125 Use *imajor* and *iminor* to get the major and minor number from an inode
130 Read may write to user space less bytes than requested.
134 Write is very similar to read. Only problem is that many applications misbehave
135 when driver writes less bytes than requested.