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,
10 Examples of device files:
12 * /dev/sda - A SCSI block device
13 * /dev/ttyS0 - A Serial terminal device
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:
28 # Device types and numbers
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.
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.
42 # Everything is a file
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.
48 # Special files, procfs and others
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*.
54 Do not forget to include linux/fs.h.
79 # Filesystem File: inode
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.
86 # Device Number Macros
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.
91 * include linux/kdev\\_t.h
96 # Character devices allocation
98 In Linux, major and minor numbers have to be requested or allocated. The
99 calls to do that for character devices are:
101 * int register\\_chrdev\\_region (dev\\_t first, unsigned int count, char
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);
109 Use *cdev\\_alloc* or *cdev\\_init* to allocate or initalize a cdev structure.
111 Use *cdev\\_add* and *cdev\\_del* to register and unregister it.
115 Implementing open and release should be very simple for many devices. They
116 usually allocate data, and multiplex devices by minor number.
120 Use *imajor* and *iminor* to get the major and minor number from an inode
125 Read may write to user space less bytes than requested.
129 Write is very similar to read. Only problem is that many applications misbehave
130 when driver writes less bytes than requested.