1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE slides SYSTEM "/usr/share/xml/docbook/custom/slides/3.3.1/schema/dtd/slides-full.dtd">
7 <title>Linux Device Drivers</title>
8 <author><firstname>Thadeu</firstname><surname>Cascardo</surname></author>
12 <title>Introduction</title>
14 Devices in POSIX Systems are files in /dev directory. As with any files
15 in POSIX, they may be opened, closed, read from, written to, seeked,
20 Examples of device files:
26 /dev/sda - A SCSI block device
31 /dev/ttyS0 - A Serial terminal device
39 <title>POSIX I/O calls</title>
41 POSIX systems have some standard calls for I/O. Since devices are files,
42 these same system calls are used to work with devices. We are gonna work
43 with the following calls:
70 The open system call opens a file. When working with devices, that's
71 when some initialization should be done. Some devices may be opened only
75 int open (char * filename, int flags);
78 flags may be O\_RDONLY, O\_WRONLY, O\_RDWR and many others, some not
79 important for devices.
85 fd = open ("/dev/ttyS0", O\_RDWR);
92 The close system call closes an open file.
108 The read system call reads data from an open file into a buffer. For
109 devices, it reads from the device.
112 int read (int fd, char *buffer, int bsz);
115 Read returns the number of bytes written into the buffer, which may be
116 less than the number of bytes requested for any number of reasons.
122 c = read (fd, buffer, sizeof (buffer));
129 The write system call writes data to an open file from a buffer. For
130 devices, it writes to the device.
133 int write (int fd, char *buffer, int bsz);
136 Write returns the number of bytes written to the file, which may be
137 less than the number of bytes requested for any number of reasons.
143 c = write (fd, buffer, strlen (buffer));
150 The lseek system call changes the current position of the file, allowing
151 to read or write in that position. Seeking on a device may have many
155 off\_t lseek (int fd, off\_t pos, int whence);
158 The meaning of the position depends on the value of whence, which can be
159 SEEK\_SET (the absolute position), SEEK\_CUR (relative to the current
160 position), SEEK\_END (relative to the end of the file).
166 lseek (fd, 0, SEEK\_END);
173 The ioctl system call is a catch-all operation. For those operations
174 which doesn't fit in the read/write model, the ioctl allows the user to
175 send a command with an optional argument to the device. This command may
176 accept input or generate output.
179 int ioctl (int fd, int request, char *arg);
182 The last argument is optional and depends on the type of request. Every
183 device or device class may have its different set of ioctl's.
189 struct ifreq req; ioctl (fd, SIOCGIFFLAGS, \&req);
194 <title>Linux Modules</title>
196 Linux is modularized. Drivers, filesystems, network protocols and others
197 may be loaded at runtime. Every module has an init and an exit
201 Modules may have parameters. In load time, parameters, which may be
202 boolean, integers or strings, are given by the user.
207 <title>Device types and numbers</title>
209 Linux devices may be of different types, including character devices,
210 block devices or network devices. Both character and block devices have
211 identifying numbers, a major and a minor number.
214 In Linux, major and minor numbers have to be requested or allocated. The
215 calls to do that are:
218 int register\_chrdev\_region (dev\_t first, unsigned int count, char
220 int alloc\_chrdev\_region (dev\_t *dev, unsigned int firstminor,
221 unsigned int count, char *name);
222 void unregister\_chrdev\_region (dev\_t dev, unsigned int count);