Imported some slides 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);