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>Character Devices</title>
8 <author><firstname>Thadeu</firstname><surname>Cascardo</surname></author>
12 <title>Introduction</title>
16 <title>Device Number Macros</title>
18 Nowadays, major number is 12 bits and minor number is 20 bits. This may change
19 in the future, so macros should be used.
35 <title>Allocating numbers</title>
37 Use <emphasis>register\_chrdev\_region</emphasis> or
38 <emphasis>alloc\_chrdev\_region</emphasis> to register or allocate device
42 Use <emphasis>unregister\_chrdev\_region</emphasis> to unregister it.
47 <title>cdev structure</title>
49 Use <emphasis>cdev\_alloc</emphasis> or <emphasis>cdev\_init</emphasis> to
50 allocate or initalize a cdev structure.
53 Use <emphasis>cdev\_add</emphasis> and <emphasis>cdev\_del</emphasis> to
54 register and unregister it.
59 <title>Open/Release</title>
61 Implementing open and release should be very simple for many devices. They
62 usually allocate data, and multiplex devices by minor number.
67 <title>Inode structure</title>
69 Use <emphasis>imajor</emphasis> and <emphasis>iminor</emphasis> to get the major
70 and minor number from an inode structure.
77 Read may write to user space less bytes than requested.
84 Write is very similar to read. Only problem is that many applications misbehave
85 when driver writes less bytes than requested.
90 <title>Memory Allocation</title>
92 <emphasis>kmalloc</emphasis> and <emphasis>kfree</emphasis> are defined in
93 <emphasis>include/linux/slab.h</emphasis> and are equivalent to their standard C
94 functions <emphasis>malloc</emphasis> and <emphasis>free</emphasis>.
99 <title>Concurrency</title>
101 Semaphores may be used for protection with the <emphasis>up</emphasis> and
102 <emphasis>down</emphasis> operations.