Class 3
Class 3
Devices
Classes of Devices
• Character Devices
• Block Devices
• Network Devices
Character Devices
• Accessed as a stream of bytes (like a file)
• Such drivers at least implement open,
close, read and write system calls
• Examples:
– /dev/console (Text Console)
– /dev/ttyS0 (Serial Ports)
– /dev/lp0 (Line Printers)
Block Devices
• Block devices also accessed by filesystem
nodes in the /dev directory
• Block device can host a filesystem such as
a disk
• Block devices can be accessed only as
multiple of blocks, where block is
generally as 1KB or another power of 2
Network Devices
• Any network interconnection is made through the
interface (device), that is able to exchange data with other
hosts.
• Transactions made using an Interface
• Interface can be hardware or software
• Interface is incharge of sending and receiving data
packets
• Network Interface isn’t easily mapped to a node in the
filesystem
• Though UNIX will map it as eth0
Linux Kernel Internals
Split view of kernel
Linux Device Driver
• Distinct Black Boxes
• And they make a particular piece of H/W,
respond to a well-defined internal
programming interface
• They hide completely the details of how
the device works
Contd…
• A ‘C’ program that controls a device
Interrupt Handlers
Hardware
Kernel Modules Vs Applications
• Applications performs a single task from
beginning to end, A module regisres itself
in order to serve future requests.
• Application call functions resolves external
references using appropriate lib functions
during link stage, whereas modules are
linked only to kernel, and only functions it
can call are exported by kernel.
Locating Driver Entry Points---
switch table
• The kernel maintains a set of data
structures known as :
• -block device switch table
• -character device switch table
• These data structures are used to locate and
invoke the entry points of a device driver.
Locating Driver Entry Points
• Each switch table is an array of
structures.Each structure contains a
number of function pointers.
• To decide which element of the switch
table should be used, the system uses the
“major device number” associated with the
device special file that represents the
device.
Locating Driver Entry Points
• Having decided which element of the
switch table to use, the system decides
which entry-point of the element should be
used.
• This depends upon the system call used by
the process. If the process used “open”
system call, then the “open” entry point is
used and so on……
“Hello World” Module
#define MODULE
#include <linux/module.h>
int init_module(void)
{
printk("<1>Hello, world\n");
return 0;
}
void cleanup_module(void)
{
printk("<1>Goodbye cruel world\n");
Since u r in
} kernel use
its own
function
Linking a module to the kernel
init_module
• This function is not actually included in the
file_operations structure.
• This function registers file-operations structure
with VFS in the first place –without this function,
the VFS could not route any request to the driver.
• When init() function runs, it registers your driver
by calling proper registration function. For
character devices it is register_chrdev().
• When registration is done, char special file is
accessed, VFS file system switch automatically
routes the call.
init_module
• The init() entry point of a driver is called when
the device driver is loaded in memory and it
verifies whether the device is present or not.
• Setup certain data structures if the device is
found
• Registers the resources like major number, IRQ,
IO ports etc., which are needed by the driver with
the kernel.
Usage count of the Driver
• The usage count: The system keeps a usage count
for every module in order to determine whether
the module can be safely removed.
• MOD_INC_USE_COUNT
• Increments the count for the current module
• MOD_DEC_USE_COUNT
• Decrements the count
• MOD_IN_USE
• Evaluated to true if the count is not zero
Driver-Kernel Communication
• Any device operation is typically initiated by a
process.
• For example a process might use the read()
system call to read 10 bytes for a file opened
earlier, and store these 10 bytes in a local buffer.
• The read() system call now finds out which
device is associated with this request, the type of
the device and the major device number
associated with the device.
Driver-Kernel Communication
• The read() system call then invokes the read()
entry point of the device and supplies the address
of the user (process) buffer to the device driver.
• However, the user buffer is located in the process
address space. Whereas the device driver
executes in the kernel address space.
• Therefore the device driver may need to use
another kernel support routine to copy data
between process address space and the kernel
address space.
Driver-Device Communication
---using I/O Ports
• The Linux kernel headers <asm/io.h> define the following
inline functions to access I/O ports.
• unsigned inb (unsigned port);
• Void outb (unsigned char byte, unsigned port);
• Read or write byte ports (8 bits wide)
• unsigned inw (unsigned port);
• Void outw (unsigned short word, unsigned port);
• These functions access 16-bit ports (16 bits wide).
• Unsigned inl (unsigned port);
• void outl (unsigned longword, unsigned port);
• These functions access 32-bit ports.
Device file creation
• 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, the comand
• mknod/dev/dev1 c 254 0
• Creates a char device ( c) whose major number is
254 and whose minor number is 0.
• Please note that once created by mknod, the
special device file remains unless it is explicitly
deleted, like any information stored on disk.
Major / Minor numbers
•Major number: identifies device “type”
–Device of same type can be handled by
same driver (e.g. multiple hard disks)
–Index into device tables
•devices
Minor number: identifies individual
…..
4
3
…
Is a kernel structure
file structure represents an open file
file or filp
file_operations Structure
Defined in <linux/fs.h>
}
void cleanup_module(void) /*used for a clean shutdown*/
• all: par.o
• par.o: par.c
• $(CC) $(CFLAGS) -o $@ $<
• clean:
• rm -rf *.o
• Inserting Module in the Kernel
insmod $(MODULE).o
• Viewing Messages
dmesg
Removing Module from Kernel
rmmod $(MODULE)