Introduction to Developing Embedded Linux Device Drivers Nick Gudman njgudman@gmail.com nicholas.jam.gudman@hp.com www.linkedin.com/in/njgudman
Going to Cover Not Going to Cover Kernel modules/objects Char, block, network, & binary blob drivers Char device functions Driver Infrastructure: Unified Device Model, SysFs, Platform driver, Device tree GPIO Interrupts Debugging & Tools References & Resource Discuss Edison Arduino libraries & mraa/upm Mutual Exclusion Blocking & Non-blocking Behavior* Kernel Threads* Timing Memory Allocation* Memory Mapping* Block Devices* Network Devices*
What is a device driver? Why program device drivers? Hides how a piece of hardware/device works behind a standardized set of calls Standardized set of calls allow drivers to be inserted into kernel at runtime Makes a device available, but doesn't dictate how it is used Why program device drivers? Common way to start learning kernel development Learn the internals of Unix/Linux or another operating system Somebody has to
Debugging Tools of the Trade dmesg, printk, Dynamic Debugging ProcFs DebugFs Kdbg Kernel Dumps Kernel Oops Hardware Debuggers <Insert editor of choice> Ctags & Cscope/Gscope Yocto Linaro Bitbake
Kernel Modules/Objects An object or collection of objects either built in the kernel or dynamically linked to a running kernel Stackable Not just for device drivers bitbake -c menuconfig virtual/kernel modprobe, insmod, rmmod, lsmod, modinfo
Types of Device Drivers Character Access as a stream of bytes like a file, usually sequentially Accessed through filesystem nodes (/dev) Ex. text consoles (/dev/console), serial ports (/dev/ttyS0, /dev/ttyUSB0) Block Device that can host a filesystem Any number of bytes can be read/write Ex. hard drives (/dev/sdaX), flash devices (/dev/mmcblkX) ls /dev/
Types of Device Drivers Network Sends & receives data packets through sockets Driven by kernel network subsystem Only handles packets; doesn't know about connections Communicates completely differently than character and block devices Ex. eth0, wlan0 Binary Blobs Source not given Open source glue layer between binary blob and kernel api Ex. Nvidia Linux drivers, ndiswrapper
DON'T. WRITE. BINARY BLOBS.
Types of Device Drivers Network Sends & receives data packets through sockets Driven by kernel network subsystem Only handles packets; doesn't know about connections Communicates completely differently than character and block devices Ex. eth0, wlan0 Binary Blobs Source not given Open source glue layer between binary blob and kernel api Ex. Nvidia Linux drivers, ndiswrapper
“Canonical” Functions of Char Devices Open int (*open)(struct inode *inodp, struct file *filp); Release int (*release)(struct inode *inodp, struct file *filp); Read ssize_t (*read)(struct *filp, char *buf, size_t count, loff_t *offset); put_user(x, addr); unsigned long copy_to_user(void *to, const void *from, unsigned long n); Write ssize_t (*write)(struct file *filp, const char *buf, size_t count, loff_t *offset); get_user(x, addr); unsigned long copy_from_user(void *to, const void *from, unsigned long n);
Other Important Functions unlocked_ioctl For device specific operations that don't fit well elsewhere Called when user application calls ioctl mmap Maps file or character device to user space processes Poll Lets an application wait (block) on several file descriptors at once fasync Asynchronously notifies (signals) application when data is ready lseek Update offsets for reads and writes
Driver Infrastructure Unified Device Model One framework with similar data structures & functional methods Kernel headers for specific hardware ex. linux/gpio.h, linux/i2c-dev.h, linux/spidev.h, linux/serial.h, linux/pci.h, include/usb.h, etc SysFs - /sys Virtual filesystem Not required by unified device model User space driver Class → directory Class Instance → sub-directory Attribute → file
Arduino Example
SysFs Example
Driver Infrastructure Platform Drivers Driver is an object Buses for hardware, classes for software groups Must provide at least probe and remove functions Device Tree Textual representation of board's hardware configuration Allow generic kernel to run To solve problem of growing number of hard coded, board-specific files Device tree source (.dts) and device tree source include (.dtsi) compiled into device tree blob (.dtb) with device tree compiler (dtc)
GPIO – General Purpose Input/Output Divided into chips of 32 gpio<bank>_<pin> Gpio = (bank * 32) + pin ex. GPIO0_16: (0 * 32) + 16 = 16 Sets/gets direction, active_low, value, etc Can be hooked up to interrupts
Interrupts Stops normal execution Typically divided into top & bottom halves Top Half Quick as possible Typically verifies hardware, clears pending interrupt, read/write or another action, prepares new info Bottom Half Tasklet/softirq Workqueue Kernel Thread Threaded Interrupt Handlers User Space Interrupt Handling – Userspace I/O (UIO), integrated into kernel
References & Resources https://www.kernel.org/doc/Documentation http://www.emutexlabs.com/project/215-intel-edison-gpio-pin-multiplexing-guide https://software.intel.com/en-us/blogs/2015/02/27/intel-edison-adding-kernel-modules-to- yocto-example-batman http://iotdk.intel.com/docs/master/mraa/ http://www.linuxjournal.com/article/7136?page=0,0 http://www.linuxjournal.com/content/kbuild-linux-kernel-build-system http://renjucnair.blogspot.ca/2012/01/writing-i2c-driver.html O'Reilly's Linux Device Drivers Book Linux Foundation & Feabhas Device Driver Courses
Introduction to Developing Embedded Linux Device Drivers Nick Gudman njgudman@gmail.com nicholas.jam.gudman@hp.com www.linkedin.com/in/njgudman
Why Linus Doesn't Like Kernel Debuggers “Because I'm a bastard, and proud of it!” “Without a debugger, you basically have to go the next step: understand what the program does.” “You can use a kernel debugger if you want to, and I won't give you the cold shoulder...But I'm not going to help you use one”