Introduction To Linux Device Drivers Part 2 Platform and Character Drivers
Introduction To Linux Device Drivers Part 2 Platform and Character Drivers
Agenda
Device Nodes
Platform Device Drivers
Character Device Drivers
Sys FileSystem Attributes
Debugging
Prerequisites
Experience building and running Linux
Experience with the C programming language
Goals
Make you aware of the architecture and frameworks of Linux
Point you to good reference material where you can learn all the details
The references are in the last slide
Linux Device Drivers is a book that is heavily used by all Linux kernel developers
Introduction
A lot of good documentation exists in the public domain if you
know where to find it
http://creativecommons.org/licenses/by-sa/3.0/legalcode
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license
identical to this one.
For any reuse or distribution, you must make clear to others the license terms of this work.
Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
Device Nodes
Devices in the kernel are block or character devices and are
identified using a major and a minor number
The major number indicates the family of the device
Device Files
Most system objects in UNIX are represented as files
This allows applications to manipulate system objects with the
normal file operations (open, read, write, close, etc.)
Example C code that uses the file API to write data to a serial port
Platform Devices
Hardware devices may be connected through a bus allowing
enumeration, hotplugging, or providing unique identifiers for devices
(such as PCI, PCIe and USB)
On embedded systems, devices are often not connected through a
bus which allows the devices to be uniquely identified.
Many devices are directly part of a system-on-chip: UARTs, Ethernet
controllers, SPI or I2C controllers, graphic or audio devices, etc.
Platform Driver
A platform driver is a device driver for a specific platform device on the
platform bus
Most Xilinx Linux device drivers used by customers are platform drivers
A platform driver does not inherently have any interface to user space
without hooking into a kernel framework, such as the character device
framework
The name platform only specifies the bus (the platform bus) that the device is
located on
Character, block, and network device drivers can all be platform device drivers if
the device they support is located on the platform bus
2.
3.
module_init(simple_init);
module_exit(simple_exit);
1.
2.
3.
4.
5.
A simple
example
without error
processing
Get the device memory range from the device tree by calling
platform_get_resource()
The devm_ioremap_resource() function is called to map the device
physical memory into the virtual address space
Get the interrupt number from the device tree by calling
platform_get_irq()
The interrupt function simple_irq() is connected to the kernel by calling
devm_request_irq() function
Copyright 2014 Xilinx
.
open() function
Called when a userspace application opens a device file
Contains details such as the current position, the opening mode, etc.
release() function
Called when userspace application closes the file
ioctl() function
Called by a userspace application to perform some special I/O operation
which does not fit neatly into the read/write interface of a character device
Examples might be to control the baud rate of the serial port such that no
data is sent through the serial port, but its configuration is altered
Copyright 2014 Xilinx
.
Reads data from the device, writes a specified maximum number of bytes
in the user-space buffer, and updates the file status
Returns the number of bytes read
Can block when there isn't enough data to read from the device
write() function
Called when a userspace application calls the write() library function for
the device
Writes a specified number of bytes from a userspace buffer, writes the
data to the device, updates the file status
Returns the number of bytes written
Can block when the device is not ready to accept the data
int simple_open() { };
int simple_write() { };
int simple_read() { };
int simple_release() { };
= THIS_MODULE,
.open
= simple_open,
.write
= simple_write,
.read = simple_read,
.release = simple_release,
};
int simple_probe()
{
struct cdev cdev;
cdev_init(&cdev, &simple_fops);
cdev_add(&cdev, .);
}
simple_probe()
{
device_create_file(dev, &dev_attr_irqreg);
}
The log level can be altered from the command line in the proc file
system
echo 7 > /proc/sys/kernel/printk changes the current level so all messages
are printed
References
Linux Device Drivers Version 3
https://lwn.net/Kernel/LDD3/
Free Electrons
http://free-electrons.com/
http://lxr.free-electrons.com/
Linux Foundation
http://training.linuxfoundation.org/free-linux-training/linux-training-videos/howto-build-character-drivers-for-the-linux-kernel
http://training.linuxfoundation.org/free-linux-training/linux-trainingvideos/interrupt-handling-in-linux-device-drivers