+%Character Devices
+%Thadeu Cascardo
+
# Introduction
Devices in POSIX Systems are files in /dev directory. As with any files
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);
-
-
-
-
# VFS
# Introduction
files and others, we'll use some common structures. These include the
*struct file\\_operations*, *struct file* and *struct inode*.
+Do not forget to include linux/fs.h.
+
# File Operations
+* owner
* open
* release
+* flush
* read
* write
* ioctl
+* unlocked\\_ioctl
* llseek
* poll
* mmap
The inode is a representation of the file as in its filesystem, including its
major/minor numbers and pointers to the corresponding device representation.
+
+# Character Devices
+
+# Device Number Macros
+
+Nowadays, major number is 12 bits and minor number is 20 bits. This may change
+in the future, so macros should be used.
+
+* include linux/kdev\\_t.h
+* MAJOR
+* MINOR
+* MKDEV
+
+# 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);
+
+# cdev structure
+
+Use *cdev\\_alloc* or *cdev\\_init* to allocate or initalize a cdev structure.
+
+Use *cdev\\_add* and *cdev\\_del* to register and unregister it.
+
+# Open/Release
+
+Implementing open and release should be very simple for many devices. They
+usually allocate data, and multiplex devices by minor number.
+
+# Inode structure
+
+Use *imajor* and *iminor* to get the major and minor number from an inode
+structure.
+
+# Read
+
+Read may write to user space less bytes than requested.
+
+# Write
+
+Write is very similar to read. Only problem is that many applications misbehave
+when driver writes less bytes than requested.