tame the usb gadgets talkative beast - elinux · tame the usb gadgets talkative beast author:...
TRANSCRIPT
![Page 1: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/1.jpg)
Embedded Linux Conference Europe15th October 2014 - Dusseldorf, Germany
Tame the USB gadgetstalkative beast
Krzysztof Opasiak,[email protected]
Samsung R&D Institute Poland
![Page 2: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/2.jpg)
AgendaUSB Overview
ConfigFS composite gadget
libusbg & gtC APIGadget Schemesgt
gadgetd & gadgetctlFeaturesgadgetctl
Q&A
2
![Page 3: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/3.jpg)
USB Overview
![Page 4: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/4.jpg)
Host vs DevicesHost is being extended with some functionality by deviceHCD driver - Driver for Host ControllerUSB device driver - Driver for particular USB functionality/device
Device extends the host with some functionsUDC driver Driver for USB Device ControlerUSB Gadget driver Driver implementing peripheral logic
4
![Page 5: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/5.jpg)
USB Composite Device
Functions
5
![Page 6: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/6.jpg)
USB Function• Independent from each other• Set of USB interfaces• Implementation of some protocol(ex. HID, Mass Storage or Custom)
• Piece of code in kernel module
6
![Page 7: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/7.jpg)
USB functions in kernel• Serial
• ACM• Serial• OBEX
• Ethernet• ECM• EEM• NCM• Subset• RNDIS
• Phonet• Mass Storage• Loopback• SourceSink• UVC and HID - WIP
7
![Page 8: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/8.jpg)
FunctionFS• Access to system environment• Easier implementation• Kernel USB function & file system• Wraps file IO operations into usb_requests
8
![Page 9: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/9.jpg)
FunctionFS - HOWTO
• Usage:• open ep0 file• Write function descriptors• Write function strings• Open epXX (if any)• Read events from ep0• …(Protocol specific)
• Performance - use Async IO• See Alan's Ott presentation:
''USB and the Real World''
9
![Page 10: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/10.jpg)
Gadget composition
• Fill the identity of gadget• Vendor ID• Product ID• Device Class details• Strings (manufacturer, product and serial)
• Decide what functions it has• Decide how many configurations• Decide what functions are availablein each configuration
10
![Page 11: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/11.jpg)
Gadget composition: old-school
• One gadget one kernel module• Composition and identity hardcoded in .c file• Module parametersthe only way of gadget modification
• One gadget build into kernel ora few as modules
• Small change - recompilation• Simple usage:
$ modprobe g_ether
11
![Page 12: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/12.jpg)
Gadget composition: ConfigFS• Allow user to compose gadget at runtime• Register as subsystem in ConfigFS• Use file system ops to compose a gadget• Load module on request
12
![Page 13: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/13.jpg)
ConfigFS composite gadget• Separate code from configuration• New composition without recompilation• Provide blocks and framework to compose them• Flexibility
• Usage:
13
![Page 14: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/14.jpg)
ConfigFS composite gadget• Separate code from configuration• New composition without recompilation• Provide blocks and framework to compose them• Flexibility• Usage:
13
![Page 15: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/15.jpg)
ConfigFS composite gadget
![Page 16: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/16.jpg)
Prerequisites - menuconfig
Give a chance to request_module() - use depmod
15
![Page 17: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/17.jpg)
Prologue
.Prologue..
......
$ modprobe libcomposite$ mount none -t configfs /sys/kernel/config$ cd /sys/kernel/config/usb_gadget
16
![Page 18: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/18.jpg)
Simple example
.Create gadget, fill identity..
......
$ mkdir g1$ cd g1$ echo "0x1d6b" > idVendor$ echo "0x0104" > idProduct$ mkdir strings/0x409$ echo "My␣serial" > strings/0x409/serialnumber$ echo "My␣Vendor" > strings/0x409/manufacturer$ echo "My␣Product" > strings/0x409/product
17
![Page 19: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/19.jpg)
Simple example
.One config, one function..
......
$ mkdir functions/rndis.usb0$ mkdir configs/c.1$ mkdir configs/c.1/strings/0x409$ echo "Config␣1" > \
configs/c.1/strings/0x409/configuration$ ln -s functions/rndis.usb0 configs/c.1/
18
![Page 20: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/20.jpg)
Simple example
.Check UDC name..
......
$ ls /sys/class/udc12480000.hsotg
.Bind gadget to udc..
......$ echo "12480000.hsotg" > UDC
19
![Page 21: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/21.jpg)
Simple example.On host side..
......
$ lsusb -vBus 003 Device 026: ID 1d6b:0104
bcdUSB 0.00bDeviceClass 0bDeviceSubClass 0bDeviceProtocol 0bMaxPacketSize0 64idVendor 0x1d6b Linux FoundationidProduct 0x0104 Multifunction CompositebcdDevice 3.17iManufacturer 1 My VendoriProduct 2 My ProductiSerial 3 My serialbNumConfigurations 1
20
![Page 22: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/22.jpg)
ConfigFS and FunctionFS
.ConfigFS modifications..
......
$ echo "" > UDC$ mkdir functions/ffs.my_func_name$ ln -s functions/ffs.my_func_name configs/c.1/$ mount my_func_name -t functionfs /tmp/mount_point$ run_function_daemon$ wait_for_daemon_initialization$ echo "12480000.hsotg" > UDC
21
![Page 23: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/23.jpg)
Problems?• Very verbose (~20 commands for simple gadget)• A lot of dependencies, magic numbers andsyntax rules
• User has to know function types offered bycurrent kernel
• Generate unique instance names for FFS• Run demon manually and pass mount point• Complex userspace function setup• Reliability - death of daemon causes gadgetunbind
• Limited security - only unix users rights
22
![Page 24: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/24.jpg)
libusbg & gt
![Page 25: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/25.jpg)
libusbg - goals• Allow to create gadget from code• Provide abstraction layer for ConfigFS• Reduce number of magic numbers• Limit number of potential mistakes• Allow for fast and easy gadget creation• Make gadget creation declarative
24
![Page 26: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/26.jpg)
Few words about libusbg
• Announced by Matt Porter in September 2013• C library for fast and easy gadget creation• Official:
https://github.com/libusbg/libusbg• Unofficial (my devel):
https://github.com/kopasiak/libusbg• Review through linux-usb and pull requests• Support for almost all USB functions• Just take the code and check it!
25
![Page 27: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/27.jpg)
C API Overview
• Opaque structures for all entities:• usbg_state• usbg_gadget• usbg_config• usbg_function• usbg_binding• usbg_udc
• usbg_gadget_attrs - gadget attributes,similar layout to libusb_device_descriptor ;)
• No static buffers, reentrant• Snapshot taken on initialization
26
![Page 28: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/28.jpg)
Example.Attributes and strings..
......
static usbg_gadget_attrs g_attrs = {/* Class defined at interface level */.idVendor = 0x1d6b,.idProduct = 0x104,
};
usbg_gadget_strs g_strs = {.str_ser = "My␣serial",.str_mnf = "My␣Vendor.",.str_prd = "My␣Product␣Name",
};
usbg_config_strs c_str = {"Config␣1"
};
27
![Page 29: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/29.jpg)
Example
.Gadget creation..
......
usbg_init("/sys/kernel/config", &s);
usbg_create_gadget(s, "g1", &g_attrs , &g_strs, &g);usbg_create_function(g, F_RNDIS , "usb0", NULL, &f_rndis);usbg_create_config(g, 1, "c", NULL, &c1_strs , &c);
usbg_add_config_function(c1, "rndis_func", f_rndis);
usbg_enable_gadget(g, DEFAULT_UDC);usbg_cleanup(s);
28
![Page 30: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/30.jpg)
Checkpoint
• Flexible API for gadget creation• Fast gadget removal
• Hardcode in shell script• Hardcode in C program• Maybe some cmd line tool?
• Still no equivalent to$ modprobe g_ether
29
![Page 31: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/31.jpg)
Gadget schemes
• Use configuration files for gadget composition• libconfig syntax instead of reinventing the wheel• Set of usbg_import_*() and usbg_export_*()functions
• Finally close to modprobe g_ether !!!
30
![Page 32: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/32.jpg)
Example
.Canonical form..
......
attrs = {idVendor = 0x1D6BidProduct = 0x104
}
strings = ({lang = 0x409;manufacturer = "My␣vendor"product = "My␣product"serialnumber = "My␣Serial"
})
31
![Page 33: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/33.jpg)
Example.Canonical form..
......
functions = {rndis_func = {
instance = "usb0"type = "rndis"
}}
configs = ({id = 1name = "c"strings = ({
lang = 0x409configuration = "Config␣1"
})functions = ("rndis_func")
})
32
![Page 34: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/34.jpg)
Example.Shorter form..
......
attrs = {idVendor = 0x1D6B; idProduct = 0x104;}
strings = ({lang = 0x409;manufacturer = "My␣vendor"product = "My␣product"serialnumber = "My␣Serial"
})
configs = ({id = 1name = "c"strings = ({lang = 0x409; configuration = "Config␣1";})functions = ({function = {instance = "usb0"; type = "rndis";}})
})
33
![Page 35: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/35.jpg)
Example.Gadget loader..
......
usbg_init("/sys/kernel/config", &s);
file = fopen("my_gadget.gs", "r");
usbg_import_gadget(s, file, "g1", &g);
usbg_enable_gadget(g, DEFAULT_UDC);usbg_cleanup(s);
• More gadget schemes tweaks:https://github.com/kopasiak/libusbg/tree/master/doc
• More examples:https://github.com/kopasiak/libusbg/tree/master/examples
34
![Page 36: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/36.jpg)
Example.Gadget loader..
......
usbg_init("/sys/kernel/config", &s);
file = fopen("my_gadget.gs", "r");
usbg_import_gadget(s, file, "g1", &g);
usbg_enable_gadget(g, DEFAULT_UDC);usbg_cleanup(s);
• More gadget schemes tweaks:https://github.com/kopasiak/libusbg/tree/master/doc
• More examples:https://github.com/kopasiak/libusbg/tree/master/examples
34
![Page 37: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/37.jpg)
libusbg - our plans
• Support for all other USB functions• Support for OS descriptors• API improvements• Schemes with equivalentsof legacy gadgets
• Multi-process awareness• Change notifications• Tests
35
![Page 38: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/38.jpg)
Gadget tool
• C API is not enough• Access to libusbg goodies from command line• Easy gadget administration• Combine libusbg examples into one binary• Finally gadget composed with one command!
36
![Page 39: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/39.jpg)
gt• Command line tool for gadget management• Uses libusbg• Developed on github:
https://github.com/kopasiak/gt• WIP - initial state• First few commands working• Significant contribution from Paweł Szewczyk
UNDER CONSTRUCTION
37
![Page 40: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/40.jpg)
Example
.One config, one function..
......
$ gt create g1 \idVendor=0x1d6b \idProduct=0x104 \manufacturer="My␣Vendor" \serialnumber="My␣Serial" \product="My␣product"
$ gt func create g1 rndis usb0$ gt config create g1 label 1$ gt config add g1 1 rndis usb0$ gt enable g1
38
![Page 41: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/41.jpg)
Checkpoint• Very verbose (~20 commands for simple gadget)• A lot of dependencies, magic numbers and forms
SOLVED• User has to know function types offered bycurrent kernel
PARTIALLY SOLVED• Generate unique instance names for FFS• Run demon manually and pass mount point• Complex userspace function setup• Reliability - death of daemon causes gadgetunbind
• Limited security - only unix users rightsNOT SOLVED
39
![Page 42: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/42.jpg)
gadgetd & gadgetctl
![Page 43: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/43.jpg)
gadgetd - goals• System-wide USB gadget management• Uniform API for kernel and userspace functions• High level API• Simplify userspace functions• Resource efficiency• Extend Security• Make USB gadgets easy to use
41
![Page 44: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/44.jpg)
Few words about gadgetd• Created by me and Stanisław Wadas• Developed on github:
https://github.com/gadgetd/gadgetd• Idea in early 2014• Uses libusbg• WIP• Proof of Concept ready
42
![Page 45: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/45.jpg)
gadgetd - features
• Userspace and kernel functions unification• Lazy user functions startup• Functions introspection• Polkit/Cynara cooperation• DBUS API
43
![Page 46: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/46.jpg)
Functions unification• Separate code and configuration• Provide config file for each user function• New function type for each service• Take care of naming and mounting• User simply creates ''ffs.mtp'' function• Handle instance naming issues
44
![Page 47: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/47.jpg)
FFS-inet• Only enabled functions are required• Just In Time service startup• Wait for events on all ep0• Spawn on enable, pass descriptors• Keep an eye on services• Error handling policy
45
![Page 48: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/48.jpg)
Introspection• User need to know function types• How to collect all type names?• Check config files for user functions• Check available modules• How to reliably check build-in functions?
Currently IMPOSSIBLE!!!We are working on it:
http://article.gmane.org/gmane.linux.usb.general/111068
46
![Page 49: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/49.jpg)
Security - TODO
• USB profilescharger, private, public…
• USB gadget for everyone or no?• Different functions for differentusers?
• This is definitely policy!• What to do?• Ask someone who knows:
Polkit || Cynara
47
![Page 50: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/50.jpg)
DBUS API
• High-level interface• USB entities as DBUS objects• Specialized interfaces for functions• Good for configuration purposes
48
![Page 51: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/51.jpg)
gadgetctl
• Command line tool for gadgetd• Reference DBUS client• Share the code with gt• Developed in the same repo as gt• Second backend for gt• WIP - very initial state
49
![Page 52: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/52.jpg)
Example
.One config, one ffs function..
......
$ gadgetctl create g1 \idVendor=0x1d6b \idProduct=0x104 \manufacturer="My␣Vendor" \serialnumber="My␣Serial" \product="My␣product"
$ gadgetctl func create g1 ffs.sample i_name$ gadgetctl config create g1 label 1$ gadgetctl config add g1 1 ffs.sample i_name$ gadgetctl enable g1
50
![Page 53: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/53.jpg)
gadgetd - future work
• Code cleanup• Continue API implementation• Simplify service config files format• Finish service lifecycle management• Improve ffs-daemon library implementation• Tests
51
![Page 54: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/54.jpg)
Summary• Very verbose (~20 commands for simple gadget)• A lot of dependencies, magic numbers and forms• User has to know function types offered bycurrent kernel
• Generate unique instance names for FFS• Run demon manually and pass mount point• Complex userspace function setup• Reliability - death of daemon causes gadgetunbind
• Limited security - only unix users rightsSOLVED
52
![Page 55: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/55.jpg)
Q&A
![Page 57: Tame the USB gadgets talkative beast - eLinux · Tame the USB gadgets talkative beast Author: Krzysztof Opasiak,k.opasiak@samsung.com Created Date: 10/15/2014 4:07:28 PM](https://reader036.vdocuments.net/reader036/viewer/2022081607/5f01eb157e708231d401ad6c/html5/thumbnails/57.jpg)
Refereces
• Andrzej Pietrasiewicz, Make your own USB gadget• Matt Porter, Kernel USB Gadget ConfigFS Interface• https://github.com/gadgetd/gadgetd/wiki
• https://github.com/libusbg/libusbg
• https://github.com/kopasiak/libusbg
• https://github.com/kopasiak/gt
• https://github.com/hyperrealm/libconfig
• http://lwn.net/Articles/395712/• https://wiki.tizen.org/wiki/USB/Linux_USB_Layers/Configfs_Composite_Gadget
• https://wiki.tizen.org/wiki/Security:Cynara
• http://www.freedesktop.org/wiki/Software/polkit/
55