do more with less - elinux.org · on driver-less interfacing with embedded devices peter korsgaard ...
TRANSCRIPT
![Page 1: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/1.jpg)
On Driver-less Interfacing
with Embedded Devices
Peter Korsgaard <[email protected]>
Do More With Less
![Page 2: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/2.jpg)
Peter Korsgaard
NSLU2-Linux
![Page 3: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/3.jpg)
Driver-less
Interfacing?
![Page 4: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/4.jpg)
Interfacing without having to install any custom SW on PC
![Page 5: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/5.jpg)
Embedded == Custom Stuff
![Page 6: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/6.jpg)
Embedded == Custom Stuff
But Why?
![Page 7: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/7.jpg)
.. Its
![Page 8: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/8.jpg)
.. Its
.. At First
![Page 9: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/9.jpg)
Time Pressure
![Page 10: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/10.jpg)
Flexibility
![Page 11: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/11.jpg)
I'm Special
![Page 12: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/12.jpg)
Does It Need To Be So?
● While these are sometimes valid concerns, often standard interfaces can be used instead
● Work done with Fabien Chouteau
![Page 13: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/13.jpg)
Primer
![Page 14: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/14.jpg)
Host
Devices
![Page 15: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/15.jpg)
● Device● Configuration (mode)
● Interface (functionality)● Endpoint (pipe)
![Page 16: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/16.jpg)
A device with multiple interfaces is called a composite device
![Page 17: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/17.jpg)
Functions can implement●Specific vendor protocols (custom)●USB class protocols (standard)
![Page 18: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/18.jpg)
Class protocols most interesting as OS'es have built in drivers
●HID (keyboard/mouse/..)●Storage (Hard drivers, USB sticks)●Audio (headsets, speakers)●Video (webcams)● ..
![Page 19: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/19.jpg)
USB Peripheral Controller Driver
USB Function Driver USB Function Driver
Composite
User Space Other Kernel Subsystems
Linux USB Gadget Stack
![Page 20: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/20.jpg)
Examples
![Page 21: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/21.jpg)
Function Keys
Display with function keys used to control PC
![Page 22: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/22.jpg)
Issues
Historically interfaced to PC using custom serial protocol
● Custom PC SW needed● Support issues● Not usable during BIOS/BOOT● New PCs lack serial
![Page 23: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/23.jpg)
Solution
Emulate USB HID keyboard
![Page 24: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/24.jpg)
Human Interface Devices Class
● HID descriptors to specify device type and supported report (message) formats● Reports to device to set properties (E.G LEDs on keyboard)
● On control endpoint● Reports from device to report changes (E.G. key presses / releases)
● On interrupt endpoint
![Page 25: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/25.jpg)
HID Keyboard (Boot) Protocol
● 1 byte reports to device● Bitmask of LED states (numlock, capslock, ..)
● 8 byte reports from device● Modifier key mask (alt, ctrl, ..) and currently pressed keys
See HID usage tables for key code definitions
![Page 26: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/26.jpg)
Implementation
● HID gadget function driver● Mainline since 2.6.35● Split kernel / user space implementation
● HID descriptor handling in kernel,● /dev/hidgX character device to get/set HID reports
● See Documentation/usb/gadget_hid.txt for details
![Page 27: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/27.jpg)
Kernel Side
● Platform device in platform code defining HID device descriptor(s)
● Can emulate as many devices as controller has endpoints
● g_hid USB gadget driver
![Page 28: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/28.jpg)
static struct hidg_func_descriptor hid_data = {.subclass = 0, /* No subclass */.protocol = 1, /* Keyboard */.report_length = 8,.report_desc_length = 63,.report_desc = {
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */0x09, 0x06, /* USAGE (Keyboard) */0xa1, 0x01, /* COLLECTION (Application) */
...}
};
static struct platform_device hid = {.name = "hidg",.id = 0,.num_resources = 0,.resource = 0,.dev.platform_data = &hid_data,
};
platform_device_register(&hid);
Kernel Side
![Page 29: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/29.jpg)
User Space Side
Read/write to /dev/hidgX
E.G. To send 'a': echo -en '\0\0\4\0\0\0\0\0' >/dev/hidg0 echo -en '\0\0\0\0\0\0\0\0' >/dev/hidg0
User Space Side
![Page 30: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/30.jpg)
DemoDemo
![Page 31: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/31.jpg)
Similar Setups
Yubico UbiKey one-time password generator
ThinkGeek Phantom Keystroker
![Page 32: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/32.jpg)
Potential Pitfalls
Key codes in HID reports are scancodesCorresponding key depends on PC keyboard layout
![Page 33: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/33.jpg)
Example 2
![Page 34: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/34.jpg)
Data Transfers
Firmware Upgrades through USB
![Page 35: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/35.jpg)
Issues
Historically using custom serial protocol● Custom PC SW needed● Support issues● New PCs lack serial
![Page 36: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/36.jpg)
Solution
Emulate USB memory stick
Alternatively access USB stick if host port available
![Page 37: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/37.jpg)
Pitfalls
● Mass Storage == Block device● Filesystems / OSes don't support concurrent access
● Need to detect when it is safe to access device
![Page 38: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/38.jpg)
Use Case
● Provide virtual drive where firmware upgrade can be copied to● Perform upgrade when unplugged
![Page 39: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/39.jpg)
Use Case
● Provide virtual drive where firmware upgrade can be copied to● Perform upgrade when unplugged
![Page 40: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/40.jpg)
Use Case
● Provide virtual drive where firmware upgrade can be copied to● Perform upgrade when unplugged
![Page 41: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/41.jpg)
Implementation
● File storage gadget function driver in kernel● Userspace notification on unplug / eject● Mainline since 2.6.35● Sysfs attributes:
● /sys/<gadget>/suspended● /sys/<gadget>/lunX/file
![Page 42: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/42.jpg)
Implementation
● User space program that on unplug / eject:● Ejects file ● Loopback mounts filesystem● Inspects it for interesting files● Recreates file system● Adds file to file storage driver
![Page 43: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/43.jpg)
Implementation
● File system could simply be a pregenerated template
● Prepopulated with any needed help/documentation files
● Stored on local storage or RAM (tmpfs)● If tmpfs, sparse file interesting● FAT table / help file << filesystem size
![Page 44: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/44.jpg)
Demo● Check for image files● Show on framebuffer
![Page 45: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/45.jpg)
Similar Setups
● Same approach could be used to transfer data from device
● Several 3G modems have Windows drivers on emulated USB drive
![Page 46: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/46.jpg)
Alternatives
● Device Firmware Upgrade (DFU)● Picture Transfer Protocol (PTP)
● Gadgetfs implementation: http://git.denx.de/?p=ptp-gadget.git● Media Transfer Protocol (MTP)
● MeeGo implementation: http://wiki.meego.com/Buteo/MTP
● None are as generic or well supported
![Page 47: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/47.jpg)
Web Interfaces
● Good Alternative to custom PC GUI Software
● Many open source libraries exists● JQuery UI (GUI Widgets)● Flot (Graphs)● ..
● Modern AJAX is nice for embedded● Heavy processing on client side
![Page 48: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/48.jpg)
Conclusion
● USB Class protocols can be (ab)used for driver-less interfacing
● Easiest PC SW support is NO SW● Easy to integrate, in mainline● Extends to lots of other areas
![Page 49: Do More With Less - eLinux.org · On Driver-less Interfacing with Embedded Devices Peter Korsgaard Do More With Less. Peter Korsgaard NSLU2-Linux](https://reader030.vdocuments.net/reader030/viewer/2022021421/5ada3c3f7f8b9afc0f8c3865/html5/thumbnails/49.jpg)
Thanks!
Questions?