Linux Kernel Tutorials
Linux Kernel Tutorials
Linux Kernel Tutorials
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Latest update: January 1, 2017.
Document updates and sources:
http://free- electrons.com/doc/training/linux- kernel
Corrections, suggestions, contributions and translations are welcome!
Send them to feedback@free-electrons.com
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
1/475
Rights to copy
Copyright 2004-2017, Free Electrons
License: Creative Commons Attribution - Share Alike 3.0
http://creativecommons.org/licenses/by-sa/3.0/legalcode
You are free:
to copy, distribute, display, and perform the work
to make derivative works
to make commercial use of the work
Under the following conditions:
Attribution. You must give the original author credit.
Share Alike. If you alter, transform, or build upon this work, you may distribute
the resulting work only under a license identical to this one.
For any reuse or distribution, you must make clear to others the license terms of
this work.
Any of these conditions can be waived if you get permission from the copyright
holder.
Your fair use and other rights are in no way aected by the above.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
2/475
Regular hyperlinks:
http://kernel.org/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
3/475
Head count: 12
Only Free Software enthusiasts!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
4/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
5/475
Generic course
information
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
6/475
CPU)
SoC with 3D acceleration, additional
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
7/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
8/475
See http://beagleboard.org/Products/
Nintendo Nunchuck with UEXT connector:
Olimex: http://j.mp/1dTYLfs
Breadboard jumper wires - Male ends:
Olimex: http://j.mp/IUaBsr
USB Serial Cable - Male ends:
Olimex: http://j.mp/1eUuY2K
USB Serial Cable - Female ends:
Olimex: http://j.mp/18Hk8yF
Note that both USB serial cables are the same.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
9/475
Participate!
During the lectures...
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
10/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
11/475
Lab commands
Cross-compiling kernel:
export ARCH=arm
export CROSS_COMPILE=arm-linuxmake sama5_defcong
Booting kernel through tftp:
setenv bootargs console=ttyS0 root=/dev/nfs
setenv bootcmd tftp 0x21000000 zImage; tftp
0x22000000 dtb; bootz 0x21000000 - 0x2200...
Making ubifs images:
mkfs.ubifs -d rootfs -o root.ubifs -e 124KiB
-m 2048 -c 1024
Encountered issues:
Restart NFS server after editing /etc/exports!
gedit ~/lab-history.txt
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
12/475
Cooperate!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
13/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
14/475
vi basic commands
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
15/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
16/475
Linux Kernel
Introduction
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
17/475
Linux features
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
18/475
History
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
19/475
Compliance to standards
and interoperability.
Exhaustive networking
support.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
20/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
21/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
22/475
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
This interface is stable over time: only new system calls can
be added by the kernel developers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
23/475
Pseudo lesystems
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
24/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
25/475
64 bit architectures:
Examples: alpha, arm64, ia64, tile
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
26/475
Embedded Linux
Kernel Usage
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
27/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
28/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
29/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
30/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
31/475
drivers/: 57.0%
arch/: 16.3%
fs/: 5.5%
sound/: 4.4%
net/: 4.3%
include/: 3.5%
Documentation/: 2.8%
tools/: 1.3%
kernel/: 1.2%
firmware/: 0.6%
lib/: 0.5%
mm/: 0.5%
scripts/: 0.4%
crypto/: 0.4%
security/: 0.3%
block/: 0.1%
...
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
32/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
33/475
Kernel Source
Code
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
34/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
35/475
Programming language
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
36/475
No C library
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
37/475
Portability
Endianness
cpu_to_be32()
cpu_to_le32()
be32_to_cpu()
le32_to_cpu()
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
38/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
39/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
40/475
No memory protection
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
41/475
This license gives you the right to use, study, modify and share
the software freely
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
42/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
43/475
You don't have to write your driver from scratch. You can
reuse code from similar free software drivers.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
44/475
This will for sure reduce your maintenance and support work
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
45/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
46/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
47/475
Advantages
Drawbacks
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
48/475
Linux sources
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
49/475
arch/<ARCH>
block/
CREDITS
COPYING
crypto/
Cryptographic libraries
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
50/475
Documentation/
drivers/
include/
fs/
firmware/
Kernel headers
include/linux/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
51/475
include/uapi/
init/
kernel/
Kconfig
Kbuild
ipc/
lib/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
52/475
MAINTAINERS
Makefile
REPORTING-BUGS
README
net/
mm/
samples/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
53/475
scripts/
security/
usr/
tools/
sound/
virt/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
54/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
55/475
Cscope
cscope -Rk
All les for all architectures at once
make cscope
cscope -d cscope.out
Only les for your current architecture
http://cscope.sourceforge.net/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
56/475
Cscope screenshot
57/475
http://sourceforge.net/projects/lxr
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
58/475
LXR screenshot
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
59/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
60/475
Kernel conguration
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
61/475
Example
cd linux-3.6.x/
make <target>
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
62/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
63/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
64/475
Kernel or module?
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
65/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
66/475
make xconfig allows to see all options, even the ones that
cannot be selected because of missing dependencies. In this
case, they are displayed in gray.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
67/475
make xcong
make xconfig
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
68/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
69/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
70/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
71/475
72/475
make gcong
make gconfig
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
73/475
make menucong
make menuconfig
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
74/475
make ncong
make nconfig
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
75/475
make oldcong
make oldconfig
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
76/475
A frequent problem:
If you don't remember all the changes you made, you can get
back to your previous conguration:
$ cp .config.old .config
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
77/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
78/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
79/475
Kernel compilation
make
Generates
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
80/475
Kernel installation
make install
Installs
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
81/475
Module installation
make modules_install
kernel/
Module .ko (Kernel Object) les, in the same directory
structure as in the sources.
modules.alias
Module aliases for module loading utilities. Example line:
alias sound-service-?-0 snd_mixer_oss
modules.dep, modules.dep.bin (binary hashed)
Module dependencies
modules.symbols, modules.symbols.bin (binary hashed)
Tells which module a given symbol belongs to.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
82/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
83/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
84/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
85/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
86/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
87/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
88/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
89/475
Device Tree
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 - Development, consulting, training and support. http://free-electrons.com
90/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
91/475
Run make
Copy the nal kernel image to the target storage
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
92/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
93/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
94/475
Set up a cross-compiling
environment
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
95/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
96/475
Advantages of modules
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
97/475
Module dependencies
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
98/475
Kernel log
When a new module is loaded, related information is available in
the kernel log.
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 - Development, consulting, training and support. http://free-electrons.com
99/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
100/475
Example:
$ sudo insmod ./intr_monitor.ko
insmod: error inserting './intr_monitor.ko': -1 Device or resource busy
$ dmesg
[17549774.552000] Failed to register handler for irq channel 2
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
101/475
lsmod
Displays the list of loaded modules
Compare its output with the contents of /proc/modules!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
102/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
103/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
104/475
Check /sys/module/<name>/parameters.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
105/475
Useful reading
Linux Kernel in a Nutshell, Dec 2006
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
106/475
Developing Kernel
Modules
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
107/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
108/475
__init
__exit
Example available on
http://git.free-electrons.com/trainingmaterials/plain/code/hello/hello.c
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
109/475
An initialization function
A cleanup function
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
110/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
111/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
112/475
Module License
Several usages
Used to restrict the kernel functions that the module can use if
it isn't a GPL licensed module
Values
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
113/475
Compiling a Module
Two solutions
Out of tree
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
114/475
.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /path/to/kernel/sources
all:
<tab>$(MAKE) -C $(KDIR) M=$$PWD
endif
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
115/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
116/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
117/475
.
config USB_SERIAL_NAVMAN
tristate "USB Navman GPS device"
depends on USB_SERIAL
help
To compile this driver as a module, choose M
here: the module will be called navman.
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
118/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
119/475
/* 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 - Development, consulting, training and support. http://free-electrons.com
120/475
121/475
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 - Development, consulting, training and support. http://free-electrons.com
122/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
123/475
Useful
general-purpose
kernel APIs
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
124/475
Memory/string utilities
In include/linux/string.h
In include/linux/kernel.h
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
125/475
Linked lists
Dene the list with the LIST_HEAD() macro for a global list,
or dene a struct list_head element and initialize it with
INIT_LIST_HEAD() for lists embedded in a structure.
Then use the list_*() API to manipulate the list
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
126/475
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 - Development, consulting, training and support. http://free-electrons.com
127/475
From drivers/misc/atmel_tclib.c
128/475
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
129/475
Introduction
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
130/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
131/475
132/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
133/475
Bus Drivers
One bus driver for each type of bus: USB, PCI, SPI, MMC,
I2C, etc.
It is responsible for
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
134/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
135/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
136/475
Adapter drivers
drivers/usb/core
struct bus_type is dened in drivers/usb/core/driver.c
and registered in drivers/usb/core/usb.c
drivers/usb/host
For EHCI, UHCI, OHCI, XHCI, and their implementations on
various systems (Atmel, IXP, Xilinx, OMAP, Samsung, PXA,
etc.)
Device drivers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
137/475
We will only look at the device driver side, and not the
adapter driver side
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
138/475
Device Identiers
Denes the set of devices that this driver can manage, so that
the USB core knows for which devices this driver should be
used
139/475
Instantiation of usb_driver
.
static struct usb_driver rtl8150_driver = {
.name = "rtl8150",
.probe = rtl8150_probe,
.disconnect = rtl8150_disconnect,
.id_table = rtl8150_table,
.suspend = rtl8150_suspend,
.resume = rtl8150_resume
};
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
140/475
Driver (Un)Registration
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
141/475
At Initialization
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
142/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
143/475
Probe Method
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
144/475
145/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
146/475
Platform drivers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
147/475
Non-discoverable buses
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
148/475
Platform devices
In the Linux kernel, a special bus, called the platform bus has
been created to handle such devices.
It works like any other bus (USB, PCI), except that devices are
enumerated statically instead of being discovered dynamically.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
149/475
150/475
151/475
And the list of devices was added to the system during board
initialization
152/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
153/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
154/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
155/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
156/475
struct imxuart_platform_data {
int (*init)(struct platform_device *pdev);
void (*exit)(struct platform_device *pdev);
unsigned int flags;
void (*irda_enable)(int enable);
unsigned int irda_inv_rx:1;
unsigned int irda_inv_tx:1;
unsigned short transceiver_delay;
};
.
157/475
158/475
Device Tree
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
159/475
uart0: serial@44e09000 {
compatible = "ti,omap3-uart";
ti,hwmods = "uart1";
clock-frequency = <48000000>;
reg = <0x44e09000 0x2000>;
interrupts = <72>;
status = "disabled";
};
.
other lines are properties. Their values are usually strings, list
of integers, or references to other nodes.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
160/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
161/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
162/475
#if defined(CONFIG_OF)
static const struct of_device_id omap_serial_of_match[] = {
{ .compatible = "ti,omap2-uart" },
{ .compatible = "ti,omap3-uart" },
{ .compatible = "ti,omap4-uart" },
{},
};
MODULE_DEVICE_TABLE(of, omap_serial_of_match);
#endif
static struct platform_driver serial_omap_driver = {
.probe
= serial_omap_probe,
.remove
= serial_omap_remove,
.driver
= {
.name = DRIVER_NAME,
.pm
= &serial_omap_dev_pm_ops,
.of_match_table = of_match_ptr(omap_serial_of_match),
},
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
163/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
164/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
165/475
sysfs
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
166/475
References
Documentation/drivermodel/
Documentation/
devicetree/
Documentation/
filesystems/sysfs.txt
http://devicetree.org
The kernel source code
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
167/475
Introduction to
the I2C subsystem
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
168/475
What is I2C?
Uses only two wires: SDA for the data, SCL for the clock.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
169/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
170/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
171/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
172/475
module_i2c_driver(<driver>_driver);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
173/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
174/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
175/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
176/475
.
Denition of the I2C controller, sun7i-a20.dtsi le
.
i2c0: i2c@01c2ac00 {
compatible = "allwinner,sun7i-a20-i2c",
"allwinner,sun4i-a10-i2c";
reg = <0x01c2ac00 0x400>;
interrupts = <GIC_SPI 7 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&apb1_gates 0>;
status = "disabled";
#address-cells = <1>;
#size-cells = <0>;
};
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
177/475
&i2c0 {
pinctrl-names = "default";
pinctrl-0 = <&i2c0_pins_a>;
status = "okay";
axp209: pmic@34 {
compatible = "x-powers,axp209";
reg = <0x34>;
interrupt-parent = <&nmi_intc>;
interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
interrupt-controller;
#interrupt-cells = <1>;
};
};
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
178/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
179/475
Probe/remove example
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
180/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
181/475
The most basic API to communicate with the I2C device provides
functions to either send or receive data:
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
182/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
183/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
184/475
SMBus calls
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
185/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
186/475
I2C functionality
The I2C controller drivers therefore tell the I2C core which
functionalities they support.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
187/475
References
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
188/475
Introduction to pin
muxing
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
189/475
For this reason, not all of the internal hardware block features
can be exposed on the pins simultaneously.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
190/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
191/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
192/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
193/475
See
Documentation/devicetree/bindings/pinctrl/pinctrlbindings.txt for details.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
194/475
the SoC level (.dtsi le), for pin congurations that are often
shared between multiple boards
at the board level (.dts le) for congurations that are board
specic.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
195/475
Example on OMAP/AM33xx
On OMAP/AM33xx, the
pinctrl-single driver is used.
It is common between multiple
SoCs and simply allows to
congure pins by writing a
value to a register.
In each pin conguration,
a pinctrl-single,pins
value gives a list of
(register, value) pairs
needed to congure 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 - Development, consulting, training and support. http://free-electrons.com
196/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
197/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
198/475
Kernel frameworks
for device drivers
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
199/475
200/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
201/475
Types of devices
Under Linux, there are essentially three types of devices:
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
202/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
203/475
Devices: everything is a le
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
204/475
/dev/zero
/dev/sda1
/dev/sda2
/dev/tty1
/dev/ttyS0
/dev/zero
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
205/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
206/475
Character drivers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
207/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
208/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
209/475
File operations
#include <linux/fs.h>
struct file_operations {
ssize_t (*read) (struct file *, char __user *,
size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *,
size_t, loff_t *);
long (*unlocked_ioctl) (struct file *, unsigned int,
unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
210/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
211/475
read()
Called when user space uses the read() system call on the
device.
Must read data from the device, write at most sz bytes to the
user space buer buf, and update the current position in the
le off. f is a pointer to the same le structure that was
passed in the open() operation
Must return the number of bytes read.
0 is usually interpreted by userspace as the end of the le.
On UNIX, read() operations typically block when there isn't
enough data to read from the device
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
212/475
write()
Called when user space uses the write() system call on the
device
The opposite of read, must read at most sz bytes from buf,
write it to the device, update off and return the number of
bytes written.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
213/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
214/475
A single value
get_user(v, p);
put_user(v, p);
The kernel variable v gets the value pointed by the user space
pointer p
The value pointed by the user space pointer p is set to the
contents of the kernel variable v.
A buer
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
215/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
216/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
217/475
unlocked_ioctl()
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
218/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
219/475
220/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
221/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
222/475
Kernel Frameworks
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
223/475
menuconfig FB
struct fb_ops
Second part of include/linux/fb.h
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
224/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
225/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
226/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
227/475
*spi;
*rtc;
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
228/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
229/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
230/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
231/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
232/475
233/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
234/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
235/475
The input
subsystem
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
236/475
The input subsystem takes care of all the input events coming
from the human user.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
237/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
238/475
menuconfig INPUT
Implemented in drivers/input/
include/uapi/linux/input.h
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
239/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
240/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
241/475
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);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
242/475
struct input_polled_dev {
void *private;
void (*open)(struct input_polled_dev *dev);
void (*close)(struct input_polled_dev *dev);
void (*poll)(struct input_polled_dev *dev);
unsigned int poll_interval; /* msec */
unsigned int poll_interval_max; /* msec */
unsigned int poll_interval_min; /* msec */
struct input_dev *input;
/* private: */
struct delayed_work work;
}.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
243/475
The elds id, name, evkey and keybit of the input eld
must be initialized too.
input_register_polled_device(struct input_polled_dev
*dev).
Unregistration is automatic after using
devm_input_allocate_polled_device()!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
244/475
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
245/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
246/475
Memory Management
Memory
Management
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
247/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
248/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
249/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
250/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
251/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
252/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
253/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
254/475
Page Allocator
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
255/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
256/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
257/475
GFP_ATOMIC
GFP_DMA
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
258/475
The object size can be smaller or greater than the page size
See /proc/slabinfo
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
259/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
260/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
261/475
kmalloc Allocator
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
262/475
#include <linux/slab.h>
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 - Development, consulting, training and support. http://free-electrons.com
263/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
264/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
265/475
vmalloc Allocator
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
266/475
Kmemcheck
Kmemleak
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
267/475
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
268/475
MMIO
PIO
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
269/475
MMIO vs PIO
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
270/475
Allows to prevent other drivers from using the same I/O ports,
but is purely voluntary.
request_region(0x0170, 8, "ide1");
void release_region(
unsigned long start,
unsigned long len);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
271/475
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
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 - Development, consulting, training and support. http://free-electrons.com
272/475
Examples
read 8 bits
write 8 bits
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
273/475
void release_mem_region(
unsigned long start,
unsigned long len);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
274/475
/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 - Development, consulting, training and support. http://free-electrons.com
275/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
276/475
ioremap()
277/475
Managed API
devm_ioremap()
devm_iounmap()
devm_ioremap_resource()
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
278/475
32 bits write
__raw_writel(1 << KS8695_IRQ_UART_TX,
membase + KS8695_INTST);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
279/475
Use the macros, they do the right thing for your architecture
The compiler and/or CPU can reorder memory accesses,
which might cause troubles for your devices is they expect one
register to be read/written before another one.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
280/475
/dev/mem
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
281/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
282/475
The misc
subsystem
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
283/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
284/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
285/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
286/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
287/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
288/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
289/475
Processes,
scheduling and
interrupts
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
290/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
291/475
Process, thread?
They run in the same address space as the initial thread of the
process
They start executing a function passed as argument to
pthread_create()
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
292/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
293/475
A thread life
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
294/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
295/475
Sleeping
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
296/475
Sleeping
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
297/475
Must declare a wait queue, which will be used to store the list
of threads waiting for an event
Dynamic queue declaration:
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
298/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
299/475
Also stops sleeping when the task is woken up and the timeout
expired. Returns 0 if the timeout elapsed, non-zero if the
condition was met.
int wait_event_interruptible_timeout(queue,
condition, timeout);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
300/475
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 - Development, consulting, training and support. http://free-electrons.com
301/475
Waking up!
wake_up_interruptible(&queue);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
302/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
303/475
wait_event(queue, cond);
The process is put in the
TASK_UNINTERRUPTIBLE
state.
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.
304/475
Interrupt Management
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
305/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
306/475
Dened in include/linux/interrupt.h
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
307/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
308/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
309/475
CPU0
CPU1
CPU2
16:
0
0
0
32: 1660065960
0
0
49:
0
0
0
50:
0
0
0
65:
77339
0
0
66:
2
0
0
75:
1
0
0
77:
825261
0
0
82:
819926
0
0
83:
6
0
0
96:
0
0
0
97: 45040705 26523650 16191929
FIQ:
usb_fiq
IPI0:
0
0
0
IPI1:
0
0
0
IPI2: 34944338 35870609 37410637
IPI3:
9
10
9
IPI4:
3
3
1
IPI5:
0
0
0
IPI6:
0
0
0
IPI7:
0
0
0
Err:
0
CPU3
0 ARMCTRL
0 ARMCTRL 32
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
0 ARMCTRL
47339273
ARMCTRL
0
0
12127900
11
1
0
0
0
Note: interrupt numbers shown on the left-most column are virtual numbers
when the Device Tree is used. The real physical interrupt numbers can be seen
in /sys/kernel/debug/irq_domain_mapping.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
310/475
Return value
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
311/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
312/475
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);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
313/475
Bottom half
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
314/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
315/475
Softirqs
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
316/475
Tasklets
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
317/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
318/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
319/475
Workqueues
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
320/475
Device driver
Interrupt handler
Device driver
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
321/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
322/475
Concurrent Access
to Resources:
Locking
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
323/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
324/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
325/475
Linux mutexes
#include <linux/mutex.h>
DEFINE_MUTEX(name);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
326/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
327/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
328/475
Spinlocks
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
329/475
Initializing Spinlocks
Statically
DEFINE_SPINLOCK(my_lock);
Dynamically
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
330/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
331/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
332/475
Spinlock example
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
333/475
Deadlock Situations
They can lock up your system. Make sure they never happen!
Don't call a function that can try to get access to the same
lock
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
334/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
335/475
Alternatives to Locking
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
336/475
atomic_t
#include <asm/atomic.h>
Contains a signed integer (at least 24 bits)
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
337/475
int atomic_inc_and_test(...);
int atomic_dec_and_test(...);
int atomic_sub_and_test(...);
int atomic_inc_return(...);
int atomic_dec_return(...);
int atomic_add_return(...);
int atomic_sub_return(...);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
338/475
int test_and_set_bit(...);
int test_and_clear_bit(...);
int test_and_change_bit(...);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
339/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
340/475
Kernel Debugging
Kernel Debugging
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
341/475
Dened in include/linux/printk.h
They take a classic format string with arguments
Example:
pr_info("Booting CPU %d\n", cpu);
[
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
342/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
343/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
344/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
345/475
DebugFS
DocBook/filesystems/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
346/475
DebugFS API
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
347/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
348/475
Example commands:
Detailed in Documentation/sysrq.txt
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
349/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
350/475
kgdboc=<tty-device>,<bauds>.
For example: kgdboc=ttyS0,115200
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
351/475
Then also pass kgdbwait to the kernel: it makes kgdb wait for
a debugger connection.
gdb ./vmlinux
(gdb) set remotebaud 115200
(gdb) target remote /dev/ttyS0
Once connected, you can debug a kernel the way you would
debug an application program.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
352/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
353/475
Is turned on by default
To get oops messages with symbol names instead of raw
addresses
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
354/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
355/475
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
356/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
357/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
358/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
359/475
The
The
The
The
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
360/475
Your Device Tree will describe all the SoC peripherals that are
enabled, the pin muxing, as well as all the devices on the
board.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
361/475
This platform inherits from the OMAP family of TI, for which
kernel support has been around for a long time.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
362/475
Crystalfontz CFA-10036
128MB of RAM
1 SD-Card slot
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
363/475
.
.#include "imx28.dtsi"
.
/
.{
.
m
.odel = "Crystalfontz CFA-10036 Board";
.
.compatible = "crystalfontz,cfa10036", "fsl,imx28";
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
364/475
chosen {
bootargs = "console=ttyS0,115200 earlyprintk";
.};
memory {
device_type = "memory";
reg = <0x40000000 0x8000000>; /* 128 MB */
};
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
365/475
apb@80000000 {
apbh@80000000 {
apbx@80040000 {
duart: serial@80074000 {
pinctrl-names = "default";
pinctrl-0 = <&duart_pins_b>;
status = "okay";
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
366/475
pinctrl@80018000 {
ssd1306_cfa10036: ssd1306-10036@0 {
reg = <0>;
fsl,pinmux-ids = <
0x2073 /* MX28_PAD_SSP0_D7__GPIO_2_7 */
>;
fsl,drive-strength = <0>;
fsl,voltage = <1>;
fsl,pull-up = <0>;
};
led_pins_cfa10036: leds-10036@0 {
reg = <0>;
fsl,pinmux-ids = <
0x3043 /* MX28_PAD_AUART1_RX__GPIO_3_4 */
>;
fsl,drive-strength = <0>;
fsl,voltage = <1>;
fsl,pull-up = <0>;
};
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
367/475
leds {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&led_pins_cfa10036>;
power {
gpios = <&gpio3 4 1>;
default-state = "on";
};
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
368/475
usb0: usb@80080000 {
pinctrl-names = "default";
pinctrl-0 = <&usb0_otg_cfa10036>;
status = "okay";
};
ssp0: ssp@80010000 {
compatible = "fsl,imx28-mmc";
pinctrl-names = "default";
pinctrl-0 = <&mmc0_4bit_pins_a
&mmc0_cd_cfg &mmc0_sck_cfg>;
bus-width = <4>;
status = "okay";
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
369/475
i2c0: i2c@80058000 {
pinctrl-names = "default";
pinctrl-0 = <&i2c0_pins_b>;
clock-frequency = <400000>;
status = "okay";
ssd1306: oled@3c {
compatible = "solomon,ssd1306fb-i2c";
pinctrl-names = "default";
pinctrl-0 = <&ssd1306_cfa10036>;
reg = <0x3c>;
reset-gpios = <&gpio2 7 0>;
solomon,height = <32>;
solomon,width = <128>;
solomon,page-offset = <0>;
};
};
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
370/475
.
.#include "imx28-cfa10036.dts"
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
371/475
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 - Development, consulting, training and support. http://free-electrons.com
372/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
373/475
arch/arm/mach-mvebu/board-v7.c
.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
374/475
arch/arm/boot/dts
A interrupt controller driver, drivers/irqchip/irq-armada-370-xp.c
A timer driver, drivers/clocksource/time-armada-370-xp.c
An earlyprintk implementation to get early messages from the console,
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
375/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
376/475
Once the core pieces of the SoC support have been implemented,
the remaining part is to add drivers for the dierent hardware
blocks:
Ethernet driver, in
drivers/net/ethernet/marvell/mvneta.c
etc.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
377/475
Power Management
Power
Management
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
378/475
PM Building Blocks
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
379/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
380/475
Is implemented in drivers/clk
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
381/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
382/475
Hardware-specic
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
383/475
Parse the device tree to setup the clock: the resources but
also the properties are retrieved.
Declare the compatible clocks and associate it with an
initialization function using CLK_OF_DECLARE
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
384/475
Device drivers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
385/475
Triggering Suspend
Read kernel/power/suspend.c
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
386/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
387/475
The idle loop is what you run when there's nothing left to run
in the system.
The CPU can run power saving HLT instructions, enter NAP
mode, and even disable the timers (tickless systems).
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
388/475
Managing Idle
Adding support for multiple idle levels
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
389/475
PowerTOP
https://01.org/powertop/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
390/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
391/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
392/475
See Documentation/power/pm_qos_interface.txt
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
393/475
Regulator Framework
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
394/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
395/475
Useful Resources
http://wiki.linaro.org/WorkingGroups/PowerManagement/
Ongoing developments on the ARM platform.
http://elinux.org/images/d/dd/Intro_Kernel_PM.svg
https://www.youtube.com/watch?v=Um0oRanCtzY
http://j.mp/fVdxKh
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
396/475
The kernel
development and
contribution
process
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
397/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
398/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
399/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
400/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
401/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
402/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
403/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
404/475
Very dicult to nd out the key changes and to get the global
picture out of individual changes.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
405/475
http://wiki.kernelnewbies.org/LinuxChanges
(some versions are missing)
http://lwn.net
http://www.linux-arm.info
News about Linux on ARM, including kernel changes.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
406/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
407/475
Solving Issues
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
408/475
Getting Help
Either contact the Linux mailing list for your architecture (like
linux-arm-kernel or linuxsh-dev...).
Or contact the mailing list for the subsystem you're dealing
with (linux-usb-devel, linux-mtd...). Don't ask the maintainer
directly!
Most mailing lists come with a FAQ page. Make sure you read
it before contacting the mailing list.
Useful IRC resources are available too (for example on
http://kernelnewbies.org).
Refrain from contacting the Linux Kernel mailing list, unless
you're an experienced developer and need advice.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
409/475
Make sure you investigate the issue as much as you can: see
Documentation/BUG-HUNTING
If the subsystem you report a bug on has a mailing list, use it.
Otherwise, contact the ocial maintainer (see the
MAINTAINERS le). Always give as many useful details as
possible.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
410/475
Recommended resources
Watch the Write and Submit your rst Linux kernel Patch
talk by Greg. K.H:
http://www.youtube.com/watch?v=LLBrBBImJt4
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
411/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
412/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
413/475
Make sure you already have congured your name and e-mail
address (should be done before the rst commit).
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
414/475
git commit -s
Make sure your rst description line is a useful summary and
starts with the name of the modied subsystem. This rst
description line will appear in your e-mails
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
415/475
rm 00*.patch
If you have just one patch, or a trivial patch, you can remove
the empty line after In-Reply-To:. This way, you won't add
a summary e-mail introducing your changes (recommended
otherwise).
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
416/475
Check that you received your e-mail properly, and that it looks
good.
Now, nd the maintainers for your patches
scripts/get_maintainer.pl ~/patches/00*.patch
Russell King <linux@arm.linux.org.uk> (maintainer:ARM PORT)
Nicolas Pitre <nicolas.pitre@linaro.org>
(commit_signer:1/1=100%)
linux-arm-kernel@lists.infradead.org (open list:ARM PORT)
linux-kernel@vger.kernel.org (open list)
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 - Development, consulting, training and support. http://free-electrons.com
417/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
418/475
Kernel Resources
Kernel Resources
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
419/475
http://lwn.net/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
420/475
http://elinuxdd.com/
By Sreekrishnan Venkateswaran, an
embedded IBM engineer with more than
10 years of experience
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
421/475
http://shop.oreilly.com/product/
0636920030867.do
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
422/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
423/475
Kernel documentation
http://www.tux.org/lkml/
Complete Linux kernel FAQ
Read this before asking a question to the mailing list
Kernel Newbies
https://kernel.org/doc/
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 - Development, consulting, training and support. http://free-electrons.com
424/475
International Conferences
linux.conf.au: http://linux.org.au/conf/
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
425/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
426/475
Last slides
Last slides
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
427/475
Last slide
Thank you!
And may the Source be with you
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
428/475
Backup slides
Backup slides
Embedded Linux
Experts
free electrons
Copyright 2004-2017, Free Electrons.
Creative Commons BY-SA 3.0 license.
Corrections, suggestions, contributions and translations are welcome!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
429/475
Backup slides
DMA
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
430/475
DMA Integration
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
431/475
Peripheral DMA
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
432/475
DMA Controllers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
433/475
DMA descriptors
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
434/475
Backup slides
DMA Usage
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
435/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
436/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
437/475
Bidirectional DMA
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
438/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
439/475
Coherent mappings
The kernel allocates a suitable buer 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 buer provided by the
driver.
Use a buer 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 - Development, consulting, training and support. http://free-electrons.com
440/475
device structure */
Needed buffer size in bytes */
Output: DMA bus address */
Standard GFP flags */
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
441/475
/*
/*
/*
/*
*
*
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 - Development, consulting, training and support. http://free-electrons.com
442/475
When the mapping is active: only the device should access the
buer (potential cache issues otherwise).
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
443/475
Backup slides
DMA transfers
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
444/475
Ask the hardware to use DMA, so that it will drive its request
line
Use Linux DMAEngine framework, especially its slave API
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
445/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
446/475
Backup slides
mmap
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
447/475
mmap
/proc/<pid>/maps
pmap <pid>
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
448/475
/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 - Development, consulting, training and support. http://free-electrons.com
449/475
mmap Overview
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
450/475
Call the mmap system call (see man mmap for details):
void * mmap(
void *start,
size_t length,
int prot,
int flags,
int fd,
off_t offset
);
/*
/*
/*
/*
/*
/*
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
451/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
452/475
remap_pfn_range()
The most signicant bits of the page address (without the bits
corresponding to the page size).
#include <linux/mm.h>
int remap_pfn_range(
struct vm_area_struct *, /* VMA struct */
unsigned long virt_addr, /* Starting user
* virtual address */
unsigned long pfn,
/* pfn of the starting
* physical address */
unsigned long size,
/* Mapping size */
pgprot_t prot
/* Page permissions */
);
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
453/475
454/475
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!
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
455/475
mmap Summary
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
456/475
Backup slides
Introduction to Git
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
457/475
What is Git?
No central repository
Everybody has a local repository
Local branches are possible, and very important
Easy exchange of code between developers
Well-suited to the collaborative development model used in
open-source projects
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
458/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
459/475
Clone a Repository
With Git, you get a full copy of the repository, including the
history, which allows to perform most of the operations oine.
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
460/475
git log will list all the commits. The latest commit is the
rst.
commit 4371ee353c3fc41aad9458b8e8e627eb508bc9a3
Author: Florian Fainelli <florian@openwrt.org>
Date: Mon Jun 1 02:43:17 2009 -0700
MAINTAINERS: take maintainership of the cpmac Ethernet driver
This patch adds me as the maintainer of the CPMAC (AR7)
Ethernet driver.
Signed-off-by: Florian Fainelli <florian@openwrt.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
461/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
462/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
463/475
git pull
Internally, does two things
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
464/475
Tags
To get the list of changes between a given tag and the latest
available version
git tag -l
With gitk
gitk v2.6.30..master
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
465/475
Branches
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
466/475
Branches
Create a branch
git branch
git branch -a
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
467/475
Making Changes
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 modications, by le or even by chunk.
On each modied le
git status
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
468/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
469/475
GitLab (http://gitlab.com/)
GitHub (https://github.com/)
Easy to implement.
Just needs git software on the server and ssh access.
Drawback: only supports http cloning (less ecient)
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
470/475
cd /tmp
git clone --bare ~/project project.git
touch project.git/git-daemon-export-ok
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
471/475
git fetch rt
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
472/475
git-gui
http://www.git-scm.com/docs/git-gui
A graphical interface to create and manipulate commits,
replacing multiple git command-line commands.
Not meant for history browsing (opens gitk when needed).
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
473/475
About Git
We have just seen the very basic features of Git.
Many more interesting features are available (rebasing, bisection,
merging and more). For more details:
Git Manual
http://schacon.github.com/
git/user-manual.html
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
474/475
free electrons - Embedded Linux, kernel, drivers - Development, consulting, training and support. http://free-electrons.com
475/475