do more with less - elinux.org · on driver-less interfacing with embedded devices peter korsgaard ...

49
On Driver-less Interfacing with Embedded Devices Peter Korsgaard <[email protected]> Do More With Less

Upload: dinhmien

Post on 21-Apr-2018

227 views

Category:

Documents


2 download

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

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

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

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

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

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

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

.. 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

.. 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

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

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

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

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

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

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

● 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thanks!

Questions?