X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fkernel%2Fold_slides%2F.git;a=blobdiff_plain;f=02.a.posix%2F02.vfs.xml;fp=02.a.posix%2F02.vfs.xml;h=0000000000000000000000000000000000000000;hp=8056a1f06d506b2277c679125d037632fc6ee6a7;hb=fee356adc87d539814f1cf5e3e873a6df0785e00;hpb=26cded7024aa8d08c3b972975b0e4bf9cc7f8068 diff --git a/02.a.posix/02.vfs.xml b/02.a.posix/02.vfs.xml deleted file mode 100644 index 8056a1f..0000000 --- a/02.a.posix/02.vfs.xml +++ /dev/null @@ -1,230 +0,0 @@ - - - - - - -Linux Device Drivers -ThadeuCascardo - - - -Introduction - -Devices in POSIX Systems are files in /dev directory. As with any files -in POSIX, they may be opened, closed, read from, written to, seeked, -ioctl'd, and others. - - - -Examples of device files: - - - - - -/dev/sda - A SCSI block device - - - - -/dev/ttyS0 - A Serial terminal device - - - - - - - -POSIX I/O calls - -POSIX systems have some standard calls for I/O. Since devices are files, -these same system calls are used to work with devices. We are gonna work -with the following calls: - - - -open - - -read - - -write - - -close - - -lseek - - -ioctl - - - - - -open - -The open system call opens a file. When working with devices, that's -when some initialization should be done. Some devices may be opened only -once at a time. - - -int open (char * filename, int flags); - - -flags may be O\_RDONLY, O\_WRONLY, O\_RDWR and many others, some not -important for devices. - - -Example: - - -fd = open ("/dev/ttyS0", O\_RDWR); - - - - -close - -The close system call closes an open file. - - -int close (int fd); - - -Example: - - -close (fd); - - - - -read - -The read system call reads data from an open file into a buffer. For -devices, it reads from the device. - - -int read (int fd, char *buffer, int bsz); - - -Read returns the number of bytes written into the buffer, which may be -less than the number of bytes requested for any number of reasons. - - -Example: - - -c = read (fd, buffer, sizeof (buffer)); - - - - -write - -The write system call writes data to an open file from a buffer. For -devices, it writes to the device. - - -int write (int fd, char *buffer, int bsz); - - -Write returns the number of bytes written to the file, which may be -less than the number of bytes requested for any number of reasons. - - -Example: - - -c = write (fd, buffer, strlen (buffer)); - - - - -lseek - -The lseek system call changes the current position of the file, allowing -to read or write in that position. Seeking on a device may have many -different meanings. - - -off\_t lseek (int fd, off\_t pos, int whence); - - -The meaning of the position depends on the value of whence, which can be -SEEK\_SET (the absolute position), SEEK\_CUR (relative to the current -position), SEEK\_END (relative to the end of the file). - - -Example: - - -lseek (fd, 0, SEEK\_END); - - - - -ioctl - -The ioctl system call is a catch-all operation. For those operations -which doesn't fit in the read/write model, the ioctl allows the user to -send a command with an optional argument to the device. This command may -accept input or generate output. - - -int ioctl (int fd, int request, char *arg); - - -The last argument is optional and depends on the type of request. Every -device or device class may have its different set of ioctl's. - - -Example: - - -struct ifreq req; ioctl (fd, SIOCGIFFLAGS, \&req); - - - - -Linux Modules - -Linux is modularized. Drivers, filesystems, network protocols and others -may be loaded at runtime. Every module has an init and an exit -functions. - - -Modules may have parameters. In load time, parameters, which may be -boolean, integers or strings, are given by the user. - - - - -Device types and numbers - -Linux devices may be of different types, including character devices, -block devices or network devices. Both character and block devices have -identifying numbers, a major and a minor number. - - - - -Character devices allocation - -In Linux, major and minor numbers have to be requested or allocated. The -calls to do that for character devices are: - - -int register\_chrdev\_region (dev\_t first, unsigned int count, char -*name); -int alloc\_chrdev\_region (dev\_t *dev, unsigned int firstminor, -unsigned int count, char *name); -void unregister\_chrdev\_region (dev\_t dev, unsigned int count); - - - -