Linux Kernel and Device Drivers
Linux Kernel and Device Drivers
Free Electrons
Embedded Linux
Developers
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
1/492
Rights to copy
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
2/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
3/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
4/492
5/492
6/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
7/492
Generic course
information
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
8/492
9/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
10/492
Participate!
During the lectures...
Dont hesitate to ask questions. Other people in the audience
may have similar questions too.
This helps the trainer to detect any explanation that wasnt
clear or detailed enough.
Dont hesitate to share your experience, for example to
compare Linux / Android with other operating systems used
in your company.
Your point of view is most valuable, because it can be similar
to your colleagues and different from the trainers.
Your participation can make our session more interactive and
make the topics easier to learn.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
11/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
12/492
Cooperate!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
13/492
14/492
vi basic commands
The vi editor is very useful to
make quick changes to files in an
embedded target.
Though not very user friendly at
first, vi is very powerful and its
main 15 commands are easy to
learn and are sufficient for 99% of
everyones needs!
Get an electronic copy on
http://free-electrons.com/
doc/training/embeddedlinux/vi_memento.pdf
You can also take the quick tutorial
by running vimtutor. This is a
worthy investment!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
15/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
16/492
Linux Kernel
Introduction
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
17/492
Linux features
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
18/492
History
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
19/492
Compliance to standards
and interoperability.
Exhaustive networking
support.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
20/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
21/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
22/492
System calls
The main interface between the kernel and user space is the
set of system calls
About 300 system calls that provide the main kernel services
File and device operations, networking operations,
inter-process communication, process management, memory
mapping, timers, threads, synchronization primitives, etc.
This interface is stable over time: only new system calls can
be added by the kernel developers
This system call interface is wrapped by the C library, and
user space applications usually never make a system call
directly but rather use the corresponding C library function
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
23/492
Pseudo filesystems
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
24/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
25/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
26/492
Embedded Linux
Kernel Usage
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
27/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
28/492
29/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
30/492
31/492
tools/: 0.9%
scripts/: 0.5%
mm/: 0.5%
crypto/: 0.4%
security/: 0.4%
lib/: 0.4%
block/: 0.2%
...
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
32/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
33/492
Kernel Source
Code
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
34/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
35/492
Programming language
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
36/492
No C library
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
37/492
Portability
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
38/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
39/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
40/492
No memory protection
Accessing illegal memory locations result in (often fatal)
kernel oopses.
Fixed size stack (8 or 4 KB). Unlike in user space, theres no
way to make it grow.
Kernel memory cant be swapped out (for the same reasons).
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
41/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
42/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
43/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
44/492
Once your sources are accepted in the mainline tree, they are
maintained by people making changes.
Near cost-free maintenance, security fixes and improvements.
Easy access to your sources by users.
Many more people reviewing your code.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
45/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
46/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
47/492
Drawbacks
Less straightforward to handle interrupts.
Increased interrupt latency vs. kernel code.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
48/492
Linux sources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
49/492
block/
Block layer core
COPYING
Linux copying conditions (GNU GPL)
CREDITS
Linux main contributors
crypto/
Cryptographic libraries
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
50/492
drivers/
All device drivers except sound ones (usb, pci...)
firmware/
Legacy: firmware images extracted from old drivers
fs/
Filesystems (fs/ext3/, etc.)
include/
Kernel headers
include/linux/
Linux kernel core headers
include/uapi/
User space API headers
init/
Linux initialization (including main.c)
ipc/
Code used for process communication
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
51/492
Kconfig
Top level description file for configuration parameters
kernel/
Linux kernel core (very small!)
lib/
Misc library routines (zlib, crc32...)
MAINTAINERS
Maintainers of each kernel part. Very useful!
Makefile
Top Linux Makefile (sets arch and version)
mm/
Memory management code (small too!)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
52/492
README
Overview and building instructions
REPORTING-BUGS
Bug report instructions
samples/
Sample code (markers, kprobes, kobjects...)
scripts/
Scripts for internal or external use
security/
Security model implementations (SELinux...)
sound/
Sound support code and drivers
tools/
Code for various user space tools (mostly C)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
53/492
usr/
Code to generate an initramfs cpio archive
virt/
Virtualization support (KVM)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
54/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
55/492
Cscope
Tool to browse source code (mainly C, but also C++ or Java)
Supports huge projects like the Linux kernel. Typically takes
less than 1 min. to index the whole Linux sources.
In Linux kernel sources, two ways of running it:
cscope -Rk
All files for all architectures at once
make cscope
cscope -d scope.out
Only files for your current architecture
56/492
Cscope screenshot
57/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
58/492
LXR screenshot
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
59/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
60/492
Kernel configuration
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
61/492
Example
cd linux-3.6.x/
make <target>
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
62/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
63/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
64/492
Kernel or module?
The kernel image is a single file, resulting from the linking
of all object files that correspond to features enabled in the
configuration
This is the file that gets loaded in memory by the bootloader
All included features are therefore available as soon as the
kernel starts, at a time where no filesystem exists
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
65/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
66/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
67/492
make xconfig
make xconfig
The most common graphical interface to configure the kernel.
Make sure you read
help -> introduction: useful options!
File browser: easier to load configuration files
Search interface to look for parameters
Required Debian / Ubuntu packages: libqt4-dev g++
(libqt3-mt-dev for older kernel releases)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
68/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
69/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
70/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
71/492
72/492
make gconfig
make gconfig
GTK based graphical
configuration interface.
Functionality similar to that
of make xconfig.
Just lacking a search
functionality.
Required Debian packages:
libglade2-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
73/492
make menuconfig
make menuconfig
Useful when no graphics are
available. Pretty convenient
too!
Same interface found in
other tools: BusyBox,
Buildroot...
Required Debian packages:
libncurses-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
74/492
make nconfig
make nconfig
A newer, similar text
interface
More user friendly (for
example, easier to access
help information).
Required Debian packages:
libncurses-dev
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
75/492
make oldconfig
make oldconfig
Needed very often!
Useful to upgrade a .config file from an earlier kernel release
Issues warnings for configuration parameters that no longer
exist in the new kernel.
Asks for values for new parameters
If you edit a .config file by hand, its strongly recommended to
run make oldconfig afterwards!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
76/492
A frequent problem:
After changing several kernel configuration settings, your
kernel no longer works.
If you dont remember all the changes you made, you can get
back to your previous configuration:
$ cp .config.old .config
All the configuration interfaces of the kernel (xconfig,
menuconfig, oldconfig...) keep this .config.old backup
copy.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
77/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
78/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
79/492
Kernel compilation
make
in the main kernel source directory
Remember to run multiple jobs in parallel if you have multiple
CPU cores. Example: make -j 4
No need to run as root!
Generates
vmlinux, the raw uncompressed kernel image, at the ELF
format, useful for debugging purposes, but cannot be booted
arch/<arch>/boot/*Image, the final, usually compressed,
kernel image that can be booted
bzImage for x86, zImage for ARM, vmImage.gz for Blackfin,
etc.
80/492
Kernel installation
make install
Does the installation for the host system by default, so needs
to be run as root. Generally not used when compiling for an
embedded system, and it installs files on the development
workstation.
Installs
/boot/vmlinuz-<version>
Compressed kernel image. Same as the one in
arch/<arch>/boot
/boot/System.map-<version>
Stores kernel symbol addresses
/boot/config-<version>
Kernel configuration for this version
81/492
Module installation
make modules_install
Does the installation for the host system by default, so needs
to be run as root
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
82/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
83/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
84/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
85/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
86/492
87/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
88/492
Make sure you have the support for the right CPU, the right
board and the right device drivers.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
89/492
Device Tree
Many embedded architectures have a lot of non-discoverable
hardware.
Depending on the architecture, such hardware is either
described using C code directly within the kernel, or using a
special hardware description language in a Device Tree.
ARM, PowerPC, OpenRISC, ARC, Microblaze are examples of
architectures using the Device Tree.
A Device Tree Source, written by kernel developers, is
compiled into a binary Device Tree Blob, passed at boot time
to the kernel.
There is one different Device Tree for each board/platform
supported by the kernel, available in
arch/arm/boot/dts/<board>.dtb.
The bootloader must load both the kernel image and the
Device Tree Blob in memory before starting the kernel.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
90/492
91/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
92/492
93/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
94/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
95/492
Advantages of modules
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
96/492
Module dependencies
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
97/492
Kernel log
When a new module is loaded, related information is available in
the kernel log.
The kernel keeps its messages in a circular buffer (so that it
doesnt consume more memory with many messages)
Kernel log messages are available through the dmesg
command (diagnostic message)
Kernel log messages are also displayed in the system console
(console messages can be filtered by level using the loglevel
kernel parameter, or completely disabled with the quiet
parameter).
Note that you can write to the kernel log from user space too:
echo "<n>Debug info" > /dev/kmsg
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
98/492
modinfo <module_name>
modinfo <module_path>.ko
Gets information about a module: parameters, license,
description and dependencies.
Very useful before deciding to load a module or not.
sudo insmod <module_path>.ko
Tries to load the given module. The full path to the module
object file must be given.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
99/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
100/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
101/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
102/492
103/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
104/492
Useful reading
Linux Kernel in a Nutshell, Dec 2006
By Greg Kroah-Hartman, OReilly
http://www.kroah.com/lkn/
A good reference book and guide on
configuring, compiling and managing the
Linux kernel sources.
Freely available on-line!
Great companion to the printed book for
easy electronic searches!
Available as single PDF file on
http://free-electrons.com/
community/kernel/lkn/
Our rating: 2 stars
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
105/492
Developing Kernel
Modules
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
106/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
107/492
__init
removed after initialization (static kernel or module.)
__exit
discarded when module compiled statically into the kernel, or
when module unloading support is not enabled.
Example available on
http://git.free-electrons.com/trainingmaterials/plain/code/hello/hello.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
108/492
An initialization function
Called when the module is loaded, returns an error code (0 on
success, negative value on failure)
Declared by the module_init() macro: the name of the
function doesnt matter, even though <modulename>_init()
is a convention.
A cleanup function
Called when the module is unloaded
Declared by the module_exit() macro.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
109/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
110/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
111/492
Module License
Several usages
Used to restrict the kernel functions that the module can use if
it isnt a GPL licensed module
Difference between EXPORT_SYMBOL() and
EXPORT_SYMBOL_GPL()
Values
GPL compatible (see include/linux/license.h: GPL,
GPL v2, GPL and additional rights, Dual MIT/GPL,
Dual BSD/GPL, Dual MPL/GPL)
Proprietary
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
112/492
Compiling a Module
Two solutions
Out of tree
When the code is outside of the kernel source tree, in a
different directory
Advantage: Might be easier to handle than modifications to
the kernel itself
Drawbacks: Not integrated to the kernel
configuration/compilation process, needs to be built
separately, the driver cannot be built statically
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
113/492
114/492
115/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
116/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
117/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
118/492
/* hello_param.c */
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
/* A couple of parameters that can be passed in: how many
times we say hello, and to whom */
static char *whom = "world";
module_param(whom, charp, 0);
static int howmany = 1;
module_param(howmany, int, 0);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
119/492
120/492
module_param(
name, /* name of an already defined variable */
type, /* either byte, short, ushort, int, uint, long, ulong,
charp, bool or invbool. (checked at run time!) */
perm /* for /sys/module/<module_name>/parameters/<param>,
0: no such module parameter value file */
);
/* Example */
static int irq=5;
module_param(irq, int, S_IRUGO);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
121/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
122/492
Useful
general-purpose
kernel APIs
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
123/492
Memory/string utilities
In linux/string.h
Memory-related: memset(), memcpy(), memmove(),
memscan(), memcmp(), memchr()
String-related: strcpy(), strcat(), strcmp(), strchr(),
strrchr(), strlen() and variants
Allocate and copy a string: kstrdup(), kstrndup()
Allocate and copy a memory area: kmemdup()
In linux/kernel.h
String to int conversion: simple_strtoul(),
simple_strtol(), simple_strtoull(),
simple_strtoll()
Other string functions: sprintf(), sscanf()
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
124/492
Linked lists
Convenient linked-list facility in linux/list.h
Used in thousands of places in the kernel
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
125/492
From include/linux/atmel_tc.h
/*
* Definition of a list element, with a
* struct list_head member
*/
struct atmel_tc
{
/* some members */
struct list_head node;
};
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
126/492
127/492
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
128/492
Introduction
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
129/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
130/492
131/492
132/492
Bus Drivers
The first component of the device model is the bus driver
One bus driver for each type of bus: USB, PCI, SPI, MMC,
I2C, etc.
It is responsible for
Registering the bus type (struct bus_type)
Allowing the registration of adapter drivers (USB controllers,
I2C adapters, etc.), able to detect the connected devices, and
providing a communication mechanism with the devices
Allowing the registration of device drivers (USB devices, I2C
devices, PCI devices, etc.), managing the devices
Matching the device drivers against the devices detected by
the adapter drivers.
Provides an API to both adapter drivers and device drivers
Defining driver and device specific structures, mainly
struct usb_driver and struct usb_interface
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
133/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
134/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
135/492
Adapter drivers
drivers/usb/host
For EHCI, UHCI, OHCI, XHCI, and their implementations on
various systems (Atmel, IXP, Xilinx, OMAP, Samsung, PXA,
etc.)
Device drivers
Everywhere in the kernel tree, classified by their type
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
136/492
We will only look at the device driver side, and not the
adapter driver side
The driver we will look at is drivers/net/usb/rtl8150.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
137/492
Device Identifiers
Defines the set of devices that this driver can manage, so that
the USB core knows for which devices this driver should be
used
The MODULE_DEVICE_TABLE() macro allows depmod to
extract at compile time the relation between device identifiers
and drivers, so that drivers can be loaded automatically by
udev. See
/lib/modules/$(uname -r)/modules.{alias,usbmap}
static struct usb_device_id rtl8150_table[] = {
{ USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150) },
{ USB_DEVICE(VENDOR_ID_MELCO, PRODUCT_ID_LUAKTX) },
{ USB_DEVICE(VENDOR_ID_MICRONET, PRODUCT_ID_SP128AR) },
{ USB_DEVICE(VENDOR_ID_LONGSHINE, PRODUCT_ID_LCS8138TX) },
[...]
{}
};
MODULE_DEVICE_TABLE(usb, rtl8150_table);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
138/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
139/492
Driver (Un)Registration
When the driver is loaded or unloaded, it must register or
unregister itself from the USB core
Done using usb_register() and usb_deregister(),
provided by the USB core.
static int __init usb_rtl8150_init(void)
{
return usb_register(&rtl8150_driver);
}
static void __exit usb_rtl8150_exit(void)
{
usb_deregister(&rtl8150_driver);
}
module_init(usb_rtl8150_init);
module_exit(usb_rtl8150_exit);
140/492
At Initialization
The USB adapter driver that corresponds to the USB
controller of the system registers itself to the USB core
The rtl8150 USB device driver registers itself to the USB
core
141/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
142/492
Probe Method
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
143/492
144/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
145/492
Platform drivers
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
146/492
Non-discoverable buses
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
147/492
Platform devices
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
148/492
149/492
150/492
151/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
152/492
Declaring resources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
153/492
Using Resources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
154/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
155/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
156/492
157/492
Device Tree
On many embedded architectures, manual instantiation of
platform devices was considered to be too verbose and not
easily maintainable.
Such architectures are moving, or have moved, to use the
Device Tree.
It is a tree of nodes that models the hierarchy of devices in
the system, from the devices inside the processor to the
devices on the board.
Each node can have a number of properties describing various
properties of the devices: addresses, interrupts, clocks, etc.
At boot time, the kernel is given a compiled version, the
Device Tree Blob, which is parsed to instantiate all the
devices described in the DT.
On ARM, they are located in arch/arm/boot/dts.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
158/492
uart0: serial@44e09000 {
compatible = "ti,omap3-uart";
ti,hwmods = "uart1";
clock-frequency = <48000000>;
reg = <0x44e09000 0x2000>;
interrupts = <72>;
status = "disabled";
};
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
159/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
160/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
161/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
162/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
163/492
164/492
sysfs
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
165/492
References
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
166/492
Introduction to
the I2C subsystem
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
167/492
What is I2C?
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
168/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
169/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
170/492
171/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
172/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
173/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
174/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
175/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
176/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
177/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
178/492
Probe/remove example
static int <driver>_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
/* initialize device */
/* register to a kernel framework */
i2c_set_clientdata(client, <private data>);
return 0;
}
static int <driver>_remove(struct i2c_client *client)
{
<private data> = i2c_get_clientdata(client);
/* unregister device from kernel framework */
/* shut down the device */
return 0;
}
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
179/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
180/492
The most basic API to communicate with the I2C device provides
functions to either send or receive data:
int i2c_master_send(struct i2c_client *client,
const char *buf, int count);
Sends the contents of buf to the client.
int i2c_master_recv(struct i2c_client *client,
char *buf, int count);
Receives count bytes from the client, and store them into
buf.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
181/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
182/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
183/492
SMBus calls
SMBus is a subset of the I2C protocol.
It defines a standard set of transactions, for example to read
or write a register into a device.
Linux provides SMBus functions that should be used when
possible instead of the raw API, if the I2C device uses this
standard type of transactions.
Example: the i2c_smbus_read_byte_data() function allows
to read one byte of data from a device register.
It does the following operations:
S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P
Which means it first writes a one byte data command
(Comm), and then reads back one byte of data ([Data]).
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
184/492
185/492
I2C functionality
Not all I2C controllers support all functionalities.
The I2C controller drivers therefore tell the I2C core which
functionalities they support.
An I2C device driver must check that the functionalities they
need are provided by the I2C controller in use on the system.
The i2c_check_functionality() function allows to make
such a check.
Examples of functionalities: I2C_FUNC_I2C to be able to use
the raw I2C functions, I2C_FUNC_SMBUS_BYTE_DATA to be
able to use SMBus commands to write a command and
read/write one byte of data.
See include/uapi/linux/i2c.h for the full list of existing
functionalities.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
186/492
References
http://free-electrons.com/pub/video/2012/elce/
elce-2012-anders-board-bringup-i2c.webm, excellent
talk: You, me and I2C from David Anders at ELCE 2012.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
187/492
Introduction to pin
muxing
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
188/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
189/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
190/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
191/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
192/492
See Documentation/devicetree/bindings/pinctrl/
pinctrl-bindings.txt for details.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
193/492
194/492
Example on OMAP/AM33xx
On OMAP/AM33xx, the
pinctrl-single driver is
used. It is common between
multiple SoCs and simply
allows to configure pins by
writing a value to a register.
In each pin configuration,
a pinctrl-single,pins
value gives a list of
(register, value) pairs
needed to configure the
pins.
To know the correct values,
one must use the SoC and
board datasheets.
am33xx_pinmux: pinmux@44e10800 {
i2c0_pins: pinmux_i2c0_pins {
pinctrl-single,pins = <
/* i2c0_sda.i2c0_sda */
0x188 (PIN_INPUT_PULLUP | MUX_MODE0)
/* i2c0_scl.i2c0_scl */
0x18c (PIN_INPUT_PULLUP | MUX_MODE0)
>;
};
};
i2c0: i2c@44e0b000 {
pinctrl-names = "default";
pinctrl-0 = <&i2c0_pins>;
status = "okay";
clock-frequency = <400000>;
tps: tps@2d {
reg = <0x2d>;
};
};
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
195/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
196/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
197/492
Kernel frameworks
for device drivers
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
198/492
199/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
200/492
Types of devices
Under Linux, there are essentially three types of devices:
Network devices. They are represented as network
interfaces, visible in user space using ifconfig.
Block devices. They are used to provide user space
applications access to raw storage devices (hard disks, USB
keys). They are visible to the applications as device files in
/dev.
Character devices. They are used to provide user space
applications access to all other types of devices (input, sound,
graphics, serial, etc.). They are also visible to the applications
as device files in /dev.
Most devices are character devices, so we will study those in
more details.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
201/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
202/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
203/492
/dev/zero
/dev/sda1
/dev/sda2
/dev/tty1
/dev/ttyS0
/dev/zero
Example C code that uses the usual file API to write data to a
serial port
int fd;
fd = open("/dev/ttyS0", O_RDWR);
write(fd, "Hello", 5);
close(fd);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
204/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
205/492
Character drivers
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
206/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
207/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
208/492
File operations
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
209/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
210/492
read()
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
211/492
write()
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
212/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
213/492
put_user(v, p);
The value pointed by the user space pointer p is set to the
contents of the kernel variable v.
A buffer
unsigned long copy_to_user(void __user *to,
const void *from, unsigned long n);
unsigned long copy_from_user(void *to,
const void __user *from, unsigned long n);
214/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
215/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
216/492
unlocked ioctl()
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
217/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
218/492
219/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
220/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
221/492
Kernel Frameworks
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
222/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
223/492
Implemented in drivers/video/
fb.c, fbmem.c, fbmon.c, fbcmap.c, fbsysfs.c, modedb.c,
fbcvt.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
224/492
xxxfb_fillrect()
xxxfb_copyarea()
xxxfb_imageblit()
xxxfb_cursor()
xxxfb_rotate()
xxxfb_sync()
xxxfb_ioctl()
xxxfb_mmap()
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
225/492
226/492
227/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
228/492
*spi;
*rtc;
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
229/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
230/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
231/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
232/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
233/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
234/492
The input
subsystem
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
235/492
236/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
237/492
Implemented in drivers/input/
input.c, input-polldev.c, evbug.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
238/492
239/492
240/492
The events are sent by the driver to the event handler using
input_event(struct input_dev *dev, unsigned int
type, unsigned int code, int value);
The event types are documented in
Documentation/input/event-codes.txt
An event is composed by one or several input data changes
(packet of input data changes) such as the button state, the
relative or absolute position along an axis, etc..
After submitting potentially multiple events, the input core
must be notified by calling:
void input_sync(struct input_dev *dev):
The input subsystem provides other wrappers such as
input_report_key(), input_report_abs(), ...
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
241/492
242/492
243/492
244/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
245/492
Memory Management
Memory
Management
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
246/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
247/492
248/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
249/492
250/492
If your 32 bit platform hosts more than 4GB, they just cannot
be mapped
PAE (Physical Address Expansion) may be supported by your
architecture
Adds some address extension bits used to index memory areas
Allows accessing up to 64 GB of physical memory on x86
Note that each user space process is still limited to a 3 GB
memory space
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
251/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
252/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
253/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
254/492
Page Allocator
Appropriate for medium-size allocations
A page is usually 4K, but can be made greater in some
architectures (sh, mips: 4, 8, 16 or 64 KB, but not
configurable in x86 or arm).
Buddy allocator strategy, so only allocations of power of two
number of pages are possible: 1 page, 2 pages, 4 pages, 8
pages, 16 pages, etc.
Typical maximum size is 8192 KB, but it might depend on the
kernel configuration.
The allocated area is virtually contiguous (of course), but also
physically contiguous. It is allocated in the identity-mapped
part of the kernel memory space.
This means that large areas may not be available or hard to
retrieve due to physical memory fragmentation.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
255/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
256/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
257/492
GFP_ATOMIC
RAM allocated from code which is not allowed to block
(interrupt handlers or critical sections). Never blocks, allows
to access emergency pools, but can fail if no free memory is
readily available.
GFP_DMA
Allocates memory in an area of the physical memory usable
for DMA transfers. See our DMA chapter.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
258/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
259/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
260/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
261/492
kmalloc Allocator
The kmalloc allocator is the general purpose memory allocator
in the Linux kernel
For small sizes, it relies on generic SLAB caches, named
kmalloc-XXX in /proc/slabinfo
For larger sizes, it relies on the page allocator
The allocated area is guaranteed to be physically contiguous
The allocated area size is rounded up to the size of the
smallest SLAB cache in which it can fit (while using the SLAB
allocator directly allows to have more flexibility)
It uses the same flags as the page allocator (GFP_KERNEL,
GFP_ATOMIC, GFP_DMA, etc.) with the same semantics.
Maximum sizes, on x86 and arm (see http://j.mp/YIGq6W):
- Per allocation: 4 MB
- Total allocations: 128 MB
Should be used as the primary allocator unless there is a
strong reason to use another one.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
262/492
Example: (drivers/infiniband/core/cache.c)
struct ib_update_work *work;
work = kmalloc(sizeof *work, GFP_ATOMIC);
...
kfree(work);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
263/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
264/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
265/492
vmalloc Allocator
The vmalloc() allocator can be used to obtain virtually
contiguous memory zones, but not physically contiguous. The
requested memory size is rounded up to the next page.
The allocated area is in the kernel space part of the address
space, but outside of the identically-mapped area
Allocations of fairly large areas is possible (almost as big as
total available memory, see http://j.mp/YIGq6W again),
since physical memory fragmentation is not an issue, but areas
cannot be used for DMA, as DMA usually requires physically
contiguous buffers.
API in include/linux/vmalloc.h
void *vmalloc(unsigned long size);
Returns a virtual address
266/492
Kmemcheck
Dynamic checker for access to uninitialized memory.
Only available on x86 so far (Linux 3.10-rc5 status), but will
help to improve architecture independent code anyway.
See Documentation/kmemcheck.txt for details.
Kmemleak
Dynamic checker for memory leaks
This feature is available for all architectures.
See Documentation/kmemleak.txt for details.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
267/492
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
268/492
MMIO
Same address bus to address memory and I/O devices
Access to the I/O devices using regular instructions
Most widely used I/O method across the different architectures
supported by Linux
PIO
Different address spaces for memory and I/O devices
Uses a special class of CPU instructions to access I/O devices
Example on x86: IN and OUT instructions
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
269/492
MMIO vs PIO
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
270/492
271/492
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
dma1
pic1
timer0
timer1
rtc
dma page reg
pic2
dma2
fpu
ide1
ide0
ide1
ide0
serial
0000:00:1f.0
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
272/492
Examples
read 8 bits
oldlcr = inb(baseio + UART_LCR)
write 8 bits
outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
273/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
274/492
/proc/iomem example
00000000-0009efff
0009f000-0009ffff
000a0000-000bffff
000c0000-000cffff
000f0000-000fffff
00100000-3ffadfff
00100000-0030afff
0030b000-003b4bff
3ffae000-3fffffff
40000000-400003ff
40001000-40001fff
40400000-407fffff
40800000-40bfffff
a0000000-a0000fff
e8000000-efffffff
...
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
System RAM
reserved
Video RAM area
Video ROM
System ROM
System RAM
Kernel code
Kernel data
reserved
0000:00:1f.1
0000:02:01.0
PCI CardBus #03
PCI CardBus #03
pcmcia_socket0
PCI Bus #01
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
275/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
276/492
ioremap()
277/492
Managed API
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
278/492
279/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
280/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
281/492
/dev/mem
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
282/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
283/492
The misc
subsystem
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
284/492
However, there are some devices that really do not fit in any
of the existing frameworks.
Highly customized devices implemented in a FPGA, or other
weird devices for which implementing a complete framework is
not useful.
285/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
286/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
287/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
288/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
289/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
290/492
Processes,
scheduling and
interrupts
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
291/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
292/492
Process, thread?
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
293/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
294/492
A thread life
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
295/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
296/492
Sleeping
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
297/492
Sleeping
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
298/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
299/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
300/492
int wait_event_interruptible_timeout(queue,
condition, timeout);
Same as above, interruptible. Returns 0 if the timeout elapsed,
-ERESTARTSYS if interrupted, positive value if the condition
was met.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
301/492
ret = wait_event_interruptible
(sonypi_device.fifo_proc_list,
kfifo_len(sonypi_device.fifo) != 0);
if (ret)
return ret;
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
302/492
Waking up!
wake_up_interruptible(&queue);
Wakes up all processes waiting in an interruptible sleep on the
given queue
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
303/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
304/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
305/492
wake_up(&queue);
All processes waiting in queue are woken up, so they get
scheduled later and have the opportunity to evaluate the
condition again and go back to sleep if it is not met.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
306/492
Interrupt Management
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
307/492
308/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
309/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
310/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
311/492
CPU0
4
0
0
0
20294
0
CPU1
0
0
0
0
0
0
GIC
GIC
GIC
GIC
GIC
GIC
0
23095
0
231
0
196407
0
0
25663
0
173
0
136995
TWL6030-PIH
l3-dbg-irq
l3-app-irq
prcm
DMA
gpmc
Note: interrupt numbers shown on the left-most column are virtual numbers
when the Device Tree is used. The real physical interrupt numbers are either
shown as an additional column, or can be seen in
/sys/kernel/debug/irq_domain_mapping.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
312/492
Return value
IRQ_HANDLED: recognized and handled interrupt
IRQ_NONE: not on a device managed by the module. Useful to
share interrupt channels and/or report spurious interrupts to
the kernel.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
313/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
314/492
Threaded interrupts
In 2.6.30, support for threaded interrupts has been added to the
Linux kernel
The interrupt handler is executed inside a thread.
Allows to block during the interrupt handler, which is often
needed for I2C/SPI devices as the interrupt handler needs to
communicate with them.
Allows to set a priority for the interrupt handler execution,
which is useful for real-time usage of Linux
int devm_request_threaded_irq(
struct device *dev,
unsigned int irq,
irq_handler_t handler, irq_handler_t thread_fn
unsigned long flags, const char *name, void *dev);
handler, hard IRQ handler
thread_fn, executed in a thread
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
315/492
Bottom half
Is the general Linux name for various mechanisms which allow
to postpone the handling of interrupt-related work.
Implemented in Linux as softirqs, tasklets or workqueues.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
316/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
317/492
Softirqs
Softirqs are a form of bottom half processing
The softirqs handlers are executed with all interrupts enabled,
and a given softirq handler can run simultaneously on multiple
CPUs
They are executed once all interrupt handlers have completed,
before the kernel resumes scheduling processes, so sleeping is
not allowed.
The number of softirqs is fixed in the system, so softirqs are
not directly used by drivers, but by complete kernel
subsystems (network, etc.)
The list of softirqs is defined in
include/linux/interrupt.h: HI, TIMER, NET_TX, NET_RX,
BLOCK, BLOCK_IOPOLL, TASKLET, SCHED, HRTIMER, RCU
The HI and TASKLET softirqs are used to execute tasklets
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
318/492
Tasklets
Tasklets are executed within the HI and TASKLET softirqs.
They are executed with all interrupts enabled, but a given
tasklet is guaranteed to execute on a single CPU at a time.
A tasklet can be declared statically with the
DECLARE_TASKLET() macro or dynamically with the
tasklet_init() function. A tasklet is simply implemented
as a function. Tasklets can easily be used by individual device
drivers, as opposed to softirqs.
The interrupt handler can schedule the execution of a tasklet
with
tasklet_schedule() to get it executed in the TASKLET
softirq
tasklet_hi_schedule() to get it executed in the HI softirq
(higher priority)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
319/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
320/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
321/492
Workqueues
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
322/492
Interrupt handler
Called when an interrupt is raised.
Acknowledge the interrupt
If needed, schedule a tasklet taking care of handling data.
Otherwise, wake up processes waiting for the data.
Tasklet
Process the data
Wake up processes waiting for the data
Device driver
When the device is no longer opened by any process,
unregister the interrupt handler.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
323/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
324/492
Concurrent Access
to Resources:
Locking
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
325/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
326/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
327/492
Linux mutexes
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
328/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
329/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
330/492
Spinlocks
Locks to be used for code that is not allowed to sleep
(interrupt handlers), or that doesnt want to sleep (critical
sections). Be very careful not to call functions which can
sleep!
Originally intended for multiprocessor systems
Spinlocks never sleep and keep spinning in a loop until the
lock is available.
Spinlocks cause kernel preemption to be disabled on the CPU
executing them.
The critical section protected by a spinlock is not allowed to
sleep.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
331/492
Initializing Spinlocks
Statically
DEFINE_SPINLOCK(my_lock);
Dynamically
void spin_lock_init(spinlock_t *lock);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
332/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
333/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
334/492
Spinlock example
Spinlock structure embedded into struct uart_port
struct uart_port {
spinlock_t lock;
/* Other fields */
};
Spinlock taken/released with protection against interrupts
static unsigned int ulite_tx_empty
(struct uart_port *port) {
unsigned long flags;
spin_lock_irqsave(&port->lock, flags);
/* Do something */
spin_unlock_irqrestore(&port->lock, flags);
}
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
335/492
Deadlock Situations
They can lock up your system. Make sure they never happen!
Dont call a function that can try to get access to the same
lock
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
336/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
337/492
Alternatives to Locking
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
338/492
atomic_t
Contains a signed integer (at least 24 bits)
339/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
340/492
341/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
342/492
Kernel Debugging
Free Electrons
Kernel Debugging
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
343/492
344/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
345/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
346/492
DebugFS
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
347/492
DebugFS API
Create a sub-directory for your driver:
struct dentry *debugfs_create_dir(const char *name,
struct dentry *parent);
348/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
349/492
Example commands:
n: makes RT processes nice-able.
w: shows the kernel stack of all sleeping processes
t: shows the kernel stack of all running processes
b: reboot the system
You can even register your own!
Detailed in Documentation/sysrq.txt
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
350/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
351/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
352/492
Once connected, you can debug a kernel the way you would
debug an application program.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
353/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
354/492
Enable CONFIG_KALLSYMS_ALL
General Setup > Configure standard kernel features
To get oops messages with symbol names instead of raw
addresses
This obsoletes the ksymoops tool
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
355/492
356/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
357/492
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
358/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
359/492
360/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
361/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
362/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
363/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
364/492
Crystalfontz CFA-10036
Uses the Freescale iMX28 SoC, from the
MXS family.
128MB of RAM
1 serial port, 1 LED
1 I2C bus, equipped with an OLED display
1 SD-Card slot
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
365/492
366/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
367/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
368/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
369/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
370/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
371/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
372/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
373/492
To ensure that the Device Tree Blob gets built for this board
Device Tree Source, one need to ensure it is listed in
arch/arm/boot/dts/Makefile:
dtb-$(CONFIG_ARCH_MXS) += imx28-cfa10036.dtb \
imx28-cfa10037.dtb \
imx28-cfa10049.dtb \
imx28-cfa10055.dtb \
imx28-cfa10056.dtb \
imx28-cfa10057.dtb \
imx28-cfa10058.dtb \
imx28-evk.dtb
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
374/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
375/492
arch/arm/mach-mvebu/armada-370-xp.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
376/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
377/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
378/492
Once the core pieces of the SoC support have been implemented,
the remaining part is to add drivers for the different hardware
blocks:
Ethernet driver, in drivers/net/ethernet/mvneta.c
SATA driver, in drivers/ata/sata_mv.c
I2C driver, in drivers/i2c/busses/i2c-mv64xxx.c
SPI driver, in drivers/spi/spi-orion.c
PCIe driver, in drivers/pci/host/pci-mvebu.c
USB driver, in drivers/usb/host/ehci-orion.c
etc.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
379/492
Marvell EBU
mainlining
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
380/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
381/492
Process
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
382/492
Submission
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
383/492
Timeline (1)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
384/492
Timeline (2)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
385/492
From U-Boot
If DT capable: bootz <kerneladdr> - <dtbaddr>
If not DT capable
Kernel must have CONFIG_ARM_APPENDED_DTB=y, and
optionally CONFIG_ARM_ATAG_DTB_COMPAT=y.
cat zImage <dtb> > zImage.<foo>
mkimage -A arm -O linux -C none -a <loadaddr> e <entrypoint> -d zImage.<foo> uImage.<foo>
bootm <kerneladdr>
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
386/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
387/492
mvebu-soc-id.c SoC
identification for 370, XP, 375,
38x
netxbig.c Remaining board
support for Kirkwood. Should be
removed at some point.
platsmp.c, platsmp-a9.c,
headsmp.S, headsmp-a9.S SMP
support for Armada 370/XP and
Armada 375/38x
pmsu.c Power Management
Service Unit of Armada 370, XP,
38x (used for SMP, cpuidle,
cpufreq, etc.)
system-controller.c Used for
platform reset, SMP boot on
some platforms.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
388/492
armada-370-netgear-rn102.dts
armada-370-xp.dtsi
armada-370-netgear-rn104.dts
armada-375.dtsi
armada-370-rd.dts
armada-380.dtsi
armada-375-db.dts
armada-385.dtsi
armada-385-db.dts
armada-38x.dtsi
armada-385-rd.dts
armada-xp.dtsi
armada-xp-axpwifiap.dts
armada-xp-mv78230.dtsi
armada-xp-db.dts
armada-xp-mv78260.dtsi
armada-xp-gp.dts
armada-xp-mv78460.dtsi
armada-xp-matrix.dts
armada-xp-netgear-rn2120.dts
armada-xp-openblocks-ax3-4.dts
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
389/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
390/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
391/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
392/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
393/492
394/492
0 0x00002000
0 0x00002000
/* Port 0 MEM
/* Port 0 IO
/* Port 1 MEM
/* Port 1 IO
*/
*/
*/
*/>;
pcie@1,0 {
device_type = "pci";
assigned-addresses = <0x82000800 0 0x40000 0 0x2000>;
reg = <0x0800 0 0 0 0>;
#address-cells = <3>;
#size-cells = <2>;
#interrupt-cells = <1>;
ranges = <0x82000000 0 0 0x82000000 0x1 0 1 0
0x81000000 0 0 0x81000000 0x1 0 1 0>;
interrupt-map-mask = <0 0 0 0>;
interrupt-map = <0 0 0 0 &gic GIC_SPI 29 IRQ_TYPE_LEVEL_HIGH>;
...
};
};
};
};
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
395/492
Armada 375 DB
/ {
soc {
ranges = <MBUS_ID(0xf0, 0x01) 0 0xf1000000 0x100000
MBUS_ID(0x01, 0x1d) 0 0xfff00000 0x100000>;
};
};
Armada XP GP
/ {
soc {
ranges = <MBUS_ID(0xf0, 0x01) 0 0 0xf1000000 0x100000
MBUS_ID(0x01, 0x1d) 0 0 0xfff00000 0x100000
MBUS_ID(0x01, 0x2f) 0 0 0xf0000000 0x1000000>;
};
};
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
396/492
Programming practices
Device Tree
Problem of stability: once accepted, a Device Tree binding
cannot be changed.
Need to identify hardware blocks. For some core SoC features,
lack of good separation between hardware blocks, difficult to
create nodes in the Device Tree.
Multiplatform
One single kernel image for all v6/v7 platforms
Everything must be runtime detected (from the Device Tree or
by looking at the hardware)
No more compile time conditionals!
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
397/492
Power Management
Power
Management
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
398/492
PM Building Blocks
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
399/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
400/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
401/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
402/492
Hardware-specific
Callbacks registered with struct clk_ops and the
corresponding hardware-specific structures
Has to be written for each new hardware clock
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
403/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
404/492
Device drivers
suspend() and resume() hooks in the *_driver structures
(struct platform_driver, struct usb_driver, etc.)
See drivers/net/macb.c
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
405/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
406/492
arch/arm/mach-cpu/sleep.S
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
407/492
Triggering Suspend
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
408/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
409/492
The idle loop is what you run when theres nothing left to run
in the system.
Implemented in all architectures in
arch/<arch>/kernel/process.c
Example to read: look for cpu_idle in
arch/arm/kernel/process.c
Each ARM cpu defines its own arch_idle function.
The CPU can run power saving HLT instructions, enter NAP
mode, and even disable the timers (tickless systems).
See also http://en.wikipedia.org/wiki/Idle_loop
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
410/492
Managing Idle
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
411/492
PowerTOP
https://01.org/powertop/
With dynamic ticks, allows to fix parts of kernel code and
applications that wake up the system too often.
PowerTOP allows to track the worst offenders
Now available on ARM cpus implementing CPUidle
Also gives you useful hints for reducing power.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
412/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
413/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
414/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
415/492
Regulator Framework
Modern embedded hardware have hardware responsible for
voltage and current regulation
The regulator framework allows to take advantage of this
hardware to save power when parts of the system are unused
A consumer interface for device drivers (i.e users)
Regulator driver interface for regulator drivers
Machine interface for board configuration
sysfs interface for user space
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
416/492
In case you just need to create a BSP for your board, and your
CPU already has full PM support, you should just need to:
Create clock definitions and bind your devices to them.
Implement PM handlers (suspend, resume) in the drivers for
your board specific devices.
Implement runtime PM handlers in your drivers.
Implement board specific power management if needed (mainly
battery management)
Implement regulator framework hooks for your board if needed.
All other parts of the PM infrastructure should be already
there: suspend / resume, cpuidle, cpu frequency and voltage
scaling.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
417/492
Useful Resources
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
418/492
The kernel
development and
contribution
process
Free Electrons
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
419/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
420/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
421/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
422/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
423/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
424/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
425/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
426/492
427/492
Very difficult to find out the key changes and to get the global
picture out of individual changes.
428/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
429/492
Solving Issues
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
430/492
Getting Help
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
431/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
432/492
Recommended resources
See Documentation/SubmittingPatches for guidelines and
http://kernelnewbies.org/UpstreamMerge for very
helpful advice to have your changes merged upstream (by Rik
van Riel).
Watch the Write and Submit your first Linux kernel Patch talk
by Greg. K.H:
http://www.youtube.com/watch?v=LLBrBBImJt4
How to Participate in the Linux Community (by Jonathan
Corbet) A Guide To The Kernel Development Process
http://j.mp/tX2Ld6
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
433/492
434/492
435/492
Make sure you already have configured your name and e-mail
address (should be done before the first commit).
git config --global user.name My Name
git config --global user.email me@mydomain.net
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
436/492
437/492
If you have just one patch, or a trivial patch, you can remove
the empty line after In-Reply-To:. This way, you wont add
a summary e-mail introducing your changes (recommended
otherwise).
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
438/492
Wait for replies about your changes, take the comments into
account, and resubmit if needed, until your changes are
eventually accepted.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
439/492
440/492
Kernel Resources
Free Electrons
Kernel Resources
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
441/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
442/492
443/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
444/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
445/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
446/492
Kernel Newbies
http://kernelnewbies.org/
Glossary, articles, presentations, HOWTOs, recommended
reading, useful tools for people getting familiar with Linux
kernel or driver development.
Kernel glossary
http://kernelnewbies.org/KernelGlossary
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
447/492
International Conferences
Embedded Linux Conference:
http://embeddedlinuxconference.com/
Organized by the CE Linux Forum:
in California (San Francisco, April)
in Europe (October-November)
Very interesting kernel and user space topics for embedded
systems developers.
Presentation slides freely available
linux.conf.au: http://linux.org.au/conf/
In Australia / New Zealand
Features a few presentations by key kernel hackers.
448/492
ARM resources
ARM Linux project: http://www.arm.linux.org.uk/
Developer documentation:
http://www.arm.linux.org.uk/developer/
linux-arm-kernel mailing list:
http://lists.infradead.org/mailman/listinfo/linuxarm-kernel
FAQ:
http://www.arm.linux.org.uk/armlinux/mlfaq.php
Linaro: http://linaro.org
Many optimizations and resources for recent ARM CPUs
(toolchains, kernels, debugging utilities...).
449/492
Last slides
Free Electrons
Last slides
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
450/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
451/492
Evaluation form
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
452/492
Last slide
Thank you!
And may the Source be with you
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
453/492
Backup slides
Free Electrons
Backup slides
Embedded Linux
Developers
Free Electrons
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
454/492
Backup slides
DMA
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
455/492
DMA Integration
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
456/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
457/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
458/492
Bidirectional DMA
Need to flush caches before and after the DMA transfer.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
459/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
460/492
Coherent mappings
The kernel allocates a suitable buffer and sets the mapping for
the driver.
Can simultaneously be accessed by the CPU and device.
So, has to be in a cache coherent memory area.
Usually allocated for the whole time the module is loaded.
Can be expensive to setup and use on some platforms.
Streaming mappings
The kernel just sets the mapping for a buffer provided by the
driver.
Use a buffer already allocated by the driver.
Mapping set up for each transfer. Keeps DMA registers free on
the hardware.
The recommended solution.
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
461/492
device structure */
Needed buffer size in bytes */
Output: DMA bus address */
Standard GFP flags */
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
462/492
/*
/*
/*
/*
*
*
device structure */
input: buffer to use */
buffer size */
Either DMA_BIDIRECTIONAL,
DMA_TO_DEVICE or
DMA_FROM_DEVICE */
);
void dma_unmap_single(struct device *dev, dma_addr_t handdle,
size_t size, enum dma_data_direction dir);
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
463/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
464/492
Backup slides
mmap
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
465/492
mmap
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
466/492
/proc/<pid>/maps
start-end
...
7f4516d04000-7f4516d06000
7f4516d07000-7f4516d0b000
...
7f4518728000-7f451874f000
7f451874f000-7f451894f000
7f451894f000-7f4518951000
7f4518951000-7f4518952000
...
7f451da4f000-7f451dc3f000
7f451de3e000-7f451de41000
7f451de41000-7f451de4c000
...
/dev/dri/card0
/dev/dri/card0
r-xp
---p
r--p
rw-p
/lib/x86_64-linux-gnu/libexpat.so.1.5.2
/lib/x86_64-linux-gnu/libexpat.so.1.5.2
/lib/x86_64-linux-gnu/libexpat.so.1.5.2
/lib/x86_64-linux-gnu/libexpat.so.1.5.2
00000000
00027000
00027000
00029000
08:01
08:01
08:01
08:01
268909
268909
268909
268909
/usr/bin/Xorg
/usr/bin/Xorg
/usr/bin/Xorg
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
467/492
mmap Overview
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
468/492
/*
/*
/*
/*
/*
/*
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
469/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
470/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
471/492
472/492
devmem2
http://free-electrons.com/pub/mirror/devmem2.c, by
Jan-Derk Bakker
Very useful tool to directly peek (read) or poke (write) I/O
addresses mapped in physical address space from a shell
command line!
Very useful for early interaction experiments with a device,
without having to code and compile a driver.
Uses mmap to /dev/mem.
Examples (b: byte, h: half, w: word)
devmem2 0x000c0004 h (reading)
devmem2 0x000c0008 w 0xffffffff (writing)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
473/492
mmap Summary
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
474/492
Backup slides
Introduction to Git
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
475/492
What is Git?
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
476/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
477/492
Clone a Repository
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
478/492
git log -p will list the commits with the corresponding diff
The history in Git is not linear like in CVS or SVN, but it is a
graph of commits
Makes it a little bit more complicated to understand at the
beginning
But this is what allows the powerful features of Git
(distributed, branching, merging)
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
479/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
480/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
481/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
482/492
Tags
To get the list of changes between a given tag and the latest
available version
git log v2.6.30..master
With gitk
gitk v2.6.30..master
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
483/492
Branches
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
484/492
Branches
Create a branch
git branch <branchname>
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
485/492
Making Changes
Edit a file with your favorite text editor
Get the status of your working copy
git status
Git has a feature called the index, which allows you to stage
your commits before committing them. It allows to commit
only part of your modifications, by file or even by chunk.
On each modified file
git add <filename>
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
486/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
487/492
GitHub (https://github.com/)
For public repositories. Have to pay for private repositories.
488/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
489/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
490/492
About Git
Git Book
http://git-scm.com/book
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
491/492
Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com
492/492