Char Devices

A character (char) device is one that can be accessed as a stream of bytes (like a file); a char driver is in charge of implementing this behavior.

Char devices are accessed through names in the filesystem. Those names are called special files or device files or simply nodes of the filesystem tree; they are conventionally located in the /dev directory. Special files for char drivers are identified by a "c" in the first column of the output of ls -l. Block devices appear in /dev as well, but they are identified by a "b."

The command to create a device node on a filesystem is mknod; superuser privileges are required for this operation. The command takes three arguments in addition to the name of the file being created. For example:

mknod /dev/hello0 c 254 0

File Operations

ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
(The ioctl system call offers a way to issue device-specific commands)

struct file_operations teste_fops = {
read: teste_read,
write: teste_write,
ioctl: teste_ioctl,
open: teste_open,
release: teste_release,
};

Kernel Modules

Ponto de entrada - init_module
Ponto de saida - cleanup_module,

User Space and Kernel Space

A module runs in the so-called kernel space, whereas applications run in user space. This concept is at the base of operating systems theory.

Exemplo