Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace...

60

Transcript of Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace...

Page 1: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Make your own USB gadgetKernel and userspace

Andrzej Pietrasiewicz

Samsung R&D Institute Poland

Warsaw, Poland

[email protected]

August 20, 2014

LinuxCon North America 2014 Make your own USB gadget 1 / 34

Page 2: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Table of Contents

1 USB gadgetUSB and functionsUSB gadgetGadget implementation in Linux

2 Con�gfsThe idea, exampleOn implementationStatus

3 Userspacelibusbg & toolsgadgetd & application API

4 Q & A

LinuxCon North America 2014 Make your own USB gadget 2 / 34

Page 3: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

host += function

USB: host, device

extend the host withsome function(s)

4G MODEM flash

drive

16 GB

LinuxCon North America 2014 Make your own USB gadget 3 / 34

Page 4: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

USB device composition

CONFIG 2

CONFIG 3 functions

CONFIG 1

LinuxCon North America 2014 Make your own USB gadget 4 / 34

Page 5: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

enumeration

Device connected,presents itself

Host decides what to doand how to talk to it

LinuxCon North America 2014 Make your own USB gadget 5 / 34

Page 6: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

gadget = UDC + function(s)

A piece in hardware:UDC/OTG/

Functions: HW or SW

UDC(e.g. s3c-hsotg)

HO

ST

usb_gadget_driver

"gadget"

Lin

ux s

yst

em<< something >>

LinuxCon North America 2014 Make your own USB gadget 6 / 34

Page 7: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

composite framework

factor out repeated parts of code

drivers/usb/gadget/composite.c

reusable functions' implementations

f_acm.cf_serial.cf_obex.cf_ecm.c, f_ecm_subset.c, f_eem.c, f_ncm.c, f_rndis.cf_phonet.cf_mass_storage.cf_uvc.cf_uac1.c, f_uac2.cf_midi.c. . .

LinuxCon North America 2014 Make your own USB gadget 7 / 34

Page 8: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

gadgets proper: g_xyz.c / g_xyz kernel modules

hardcoded (!) con�gurations/functions/identity

module parameters

LinuxCon North America 2014 Make your own USB gadget 8 / 34

Page 9: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

Greg

Fact

He doesn't want my code!

Fact

He wouldn't want your code,either :O

Why I don't want your code

Linux Kernel Maintainers,why are they so grumpy

Greg [email protected]

LinuxCon North America 2014 Make your own USB gadget 9 / 34

Page 10: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

USB and functionsUSB gadgetGadget implementation in Linux

Greg

Fact

He doesn't want my code!

Fact

He wouldn't want your code,either :O

Why I don't want your code

Linux Kernel Maintainers,why are they so grumpy

Greg [email protected]

LinuxCon North America 2014 Make your own USB gadget 9 / 34

Page 11: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Separate code from data

decouple the information on actual gadget composition fromimplementation

only provide building blocks (mechanism, not policy)

LinuxCon North America 2014 Make your own USB gadget 10 / 34

Page 12: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Let the user decide at runtime

action �lesystem

create make directorydestroy remove directoryspecify value writeget value read (execute for directory)group things symlinkungroup things remove symlink

Command reference

mkdir, rmdir

echo 'something' > file, cat file, ls directory

ln -s, rm

LinuxCon North America 2014 Make your own USB gadget 11 / 34

Page 13: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Example

Example's prologue

$ modprobe libcomposite

$ mount none cfg -t configfs cfg/usb_gadget

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

drwxr-xr-x .

drwxr-xr-x ./strings

drwxr-xr-x ./configs

drwxr-xr-x ./functions

-rw-r--r-- ./UDC

-rw-r--r-- ./bcdUSB

-rw-r--r-- ./bcdDevice

-rw-r--r-- ./idProduct

-rw-r--r-- ./idVendor

-rw-r--r-- ./bMaxPacketSize0

-rw-r--r-- ./bDeviceProtocol

-rw-r--r-- ./bDeviceSubClass

-rw-r--r-- ./bDeviceClass

$ echo "0x05e8" > idVendor

$ echo "0xa4a1" > idProduct

$ mkdir strings/0x409

$ echo "serialnumber" > strings/0x409/serialnumber

$ echo "manufacturer" > strings/0x409/manufacturer

$ echo "RNDIS Gadget" > strings/0x409/product

LinuxCon North America 2014 Make your own USB gadget 12 / 34

Page 14: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Example

Example's prologue

$ modprobe libcomposite

$ mount none cfg -t configfs cfg/usb_gadget

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

drwxr-xr-x .

drwxr-xr-x ./strings

drwxr-xr-x ./configs

drwxr-xr-x ./functions

-rw-r--r-- ./UDC

-rw-r--r-- ./bcdUSB

-rw-r--r-- ./bcdDevice

-rw-r--r-- ./idProduct

-rw-r--r-- ./idVendor

-rw-r--r-- ./bMaxPacketSize0

-rw-r--r-- ./bDeviceProtocol

-rw-r--r-- ./bDeviceSubClass

-rw-r--r-- ./bDeviceClass

$ echo "0x05e8" > idVendor

$ echo "0xa4a1" > idProduct

$ mkdir strings/0x409

$ echo "serialnumber" > strings/0x409/serialnumber

$ echo "manufacturer" > strings/0x409/manufacturer

$ echo "RNDIS Gadget" > strings/0x409/product

LinuxCon North America 2014 Make your own USB gadget 12 / 34

Page 15: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Example

Example's prologue

$ modprobe libcomposite

$ mount none cfg -t configfs cfg/usb_gadget

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

drwxr-xr-x .

drwxr-xr-x ./strings

drwxr-xr-x ./configs

drwxr-xr-x ./functions

-rw-r--r-- ./UDC

-rw-r--r-- ./bcdUSB

-rw-r--r-- ./bcdDevice

-rw-r--r-- ./idProduct

-rw-r--r-- ./idVendor

-rw-r--r-- ./bMaxPacketSize0

-rw-r--r-- ./bDeviceProtocol

-rw-r--r-- ./bDeviceSubClass

-rw-r--r-- ./bDeviceClass

$ echo "0x05e8" > idVendor

$ echo "0xa4a1" > idProduct

$ mkdir strings/0x409

$ echo "serialnumber" > strings/0x409/serialnumber

$ echo "manufacturer" > strings/0x409/manufacturer

$ echo "RNDIS Gadget" > strings/0x409/product

LinuxCon North America 2014 Make your own USB gadget 12 / 34

Page 16: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Example

Example's prologue

$ modprobe libcomposite

$ mount none cfg -t configfs cfg/usb_gadget

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

drwxr-xr-x .

drwxr-xr-x ./strings

drwxr-xr-x ./configs

drwxr-xr-x ./functions

-rw-r--r-- ./UDC

-rw-r--r-- ./bcdUSB

-rw-r--r-- ./bcdDevice

-rw-r--r-- ./idProduct

-rw-r--r-- ./idVendor

-rw-r--r-- ./bMaxPacketSize0

-rw-r--r-- ./bDeviceProtocol

-rw-r--r-- ./bDeviceSubClass

-rw-r--r-- ./bDeviceClass

$ echo "0x05e8" > idVendor

$ echo "0xa4a1" > idProduct

$ mkdir strings/0x409

$ echo "serialnumber" > strings/0x409/serialnumber

$ echo "manufacturer" > strings/0x409/manufacturer

$ echo "RNDIS Gadget" > strings/0x409/product

LinuxCon North America 2014 Make your own USB gadget 12 / 34

Page 17: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Example

Example's prologue

$ modprobe libcomposite

$ mount none cfg -t configfs cfg/usb_gadget

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

drwxr-xr-x .

drwxr-xr-x ./strings

drwxr-xr-x ./configs

drwxr-xr-x ./functions

-rw-r--r-- ./UDC

-rw-r--r-- ./bcdUSB

-rw-r--r-- ./bcdDevice

-rw-r--r-- ./idProduct

-rw-r--r-- ./idVendor

-rw-r--r-- ./bMaxPacketSize0

-rw-r--r-- ./bDeviceProtocol

-rw-r--r-- ./bDeviceSubClass

-rw-r--r-- ./bDeviceClass

$ echo "0x05e8" > idVendor

$ echo "0xa4a1" > idProduct

$ mkdir strings/0x409

$ echo "serialnumber" > strings/0x409/serialnumber

$ echo "manufacturer" > strings/0x409/manufacturer

$ echo "RNDIS Gadget" > strings/0x409/product

LinuxCon North America 2014 Make your own USB gadget 12 / 34

Page 18: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

One con�g, one function

Example

$ mkdir functions/rndis.usb0

$ mkdir configs/c.1

$ mkdir configs/c.1/strings/0x409

$ echo Conf 1 > configs/c.1/strings/0x409/configuration

$ echo 120 > configs/c.1/MaxPower

$ ln -s functions/rndis.usb0 configs/c.1

$ echo 12480000.hsotg > UDC$ ls /sys/class/udc

12480000.hsotg

# formerly s3c-hsotgbind!

LinuxCon North America 2014 Make your own USB gadget 13 / 34

Page 19: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

One con�g, one function

Example

$ mkdir functions/rndis.usb0

$ mkdir configs/c.1

$ mkdir configs/c.1/strings/0x409

$ echo Conf 1 > configs/c.1/strings/0x409/configuration

$ echo 120 > configs/c.1/MaxPower

$ ln -s functions/rndis.usb0 configs/c.1

$ echo 12480000.hsotg > UDC$ ls /sys/class/udc

12480000.hsotg

# formerly s3c-hsotgbind!

LinuxCon North America 2014 Make your own USB gadget 13 / 34

Page 20: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

One con�g, one function

Example

$ mkdir functions/rndis.usb0

$ mkdir configs/c.1

$ mkdir configs/c.1/strings/0x409

$ echo Conf 1 > configs/c.1/strings/0x409/configuration

$ echo 120 > configs/c.1/MaxPower

$ ln -s functions/rndis.usb0 configs/c.1

$ echo 12480000.hsotg > UDC$ ls /sys/class/udc

12480000.hsotg

# formerly s3c-hsotgbind!

LinuxCon North America 2014 Make your own USB gadget 13 / 34

Page 21: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

One con�g, one function

Example

$ mkdir functions/rndis.usb0

$ mkdir configs/c.1

$ mkdir configs/c.1/strings/0x409

$ echo Conf 1 > configs/c.1/strings/0x409/configuration

$ echo 120 > configs/c.1/MaxPower

$ ln -s functions/rndis.usb0 configs/c.1

$ echo 12480000.hsotg > UDC$ ls /sys/class/udc

12480000.hsotg

# formerly s3c-hsotgbind!

LinuxCon North America 2014 Make your own USB gadget 13 / 34

Page 22: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

expected by some proprietary OSes

(ab)use string #EE hex, language 0

if present and has expected structure, use custom requests

"Extended Compatibility" descriptors"Extended Properties" descriptors

Example - extended compatibility

$ cd functions # $CONFIGFS_ROOT/usb_gadget/g1/functions

$ echo RNDIS > \

rndis.usb0/os_desc/interface.rndis/compatible_id

LinuxCon North America 2014 Make your own USB gadget 14 / 34

Page 23: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

expected by some proprietary OSes

(ab)use string #EE hex, language 0

if present and has expected structure, use custom requests

"Extended Compatibility" descriptors"Extended Properties" descriptors

Example - extended compatibility

$ cd functions # $CONFIGFS_ROOT/usb_gadget/g1/functions

$ echo RNDIS > \

rndis.usb0/os_desc/interface.rndis/compatible_id

LinuxCon North America 2014 Make your own USB gadget 14 / 34

Page 24: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

expected by some proprietary OSes

(ab)use string #EE hex, language 0

if present and has expected structure, use custom requests

"Extended Compatibility" descriptors"Extended Properties" descriptors

Example - extended compatibility

$ cd functions # $CONFIGFS_ROOT/usb_gadget/g1/functions

$ echo RNDIS > \

rndis.usb0/os_desc/interface.rndis/compatible_id

LinuxCon North America 2014 Make your own USB gadget 14 / 34

Page 25: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 26: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 27: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 28: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 29: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 30: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - extended properties

$ mkdir rndis.usb0/os_desc/interface.rndis/Icons

$ echo 2 \

> rndis.usb0/os_desc/interface.rndis/Icons/type

$ echo "%SystemRoot%\system32\shell32.dll,-233" \

> rndis.usb0/os_desc/interface.rndis/Icons/data

$ mkdir rndis.usb0/os_desc/interface.rndis/Label

$ echo 1 \

> rndis.usb0/os_desc/interface.rndis/Label/type

$ echo "XYZ Device" \

> rndis.usb0/os_desc/interface.rndis/Label/data

LinuxCon North America 2014 Make your own USB gadget 15 / 34

Page 31: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - activate

$ cd ../ # $CONFIGFS_ROOT/usb_gadget/g1

$ echo 0xcd > os_desc/b_vendor_code

$ echo MSFT100 > os_desc/qw_sign

$ ln -s configs/c.1 os_desc

$ echo 1 > os_desc/use

LinuxCon North America 2014 Make your own USB gadget 16 / 34

Page 32: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - activate

$ cd ../ # $CONFIGFS_ROOT/usb_gadget/g1

$ echo 0xcd > os_desc/b_vendor_code

$ echo MSFT100 > os_desc/qw_sign

$ ln -s configs/c.1 os_desc

$ echo 1 > os_desc/use

LinuxCon North America 2014 Make your own USB gadget 16 / 34

Page 33: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - activate

$ cd ../ # $CONFIGFS_ROOT/usb_gadget/g1

$ echo 0xcd > os_desc/b_vendor_code

$ echo MSFT100 > os_desc/qw_sign

$ ln -s configs/c.1 os_desc

$ echo 1 > os_desc/use

LinuxCon North America 2014 Make your own USB gadget 16 / 34

Page 34: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - activate

$ cd ../ # $CONFIGFS_ROOT/usb_gadget/g1

$ echo 0xcd > os_desc/b_vendor_code

$ echo MSFT100 > os_desc/qw_sign

$ ln -s configs/c.1 os_desc

$ echo 1 > os_desc/use

LinuxCon North America 2014 Make your own USB gadget 16 / 34

Page 35: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

OS Descriptors

Example - activate

$ cd ../ # $CONFIGFS_ROOT/usb_gadget/g1

$ echo 0xcd > os_desc/b_vendor_code

$ echo MSFT100 > os_desc/qw_sign

$ ln -s configs/c.1 os_desc

$ echo 1 > os_desc/use

LinuxCon North America 2014 Make your own USB gadget 16 / 34

Page 36: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 37: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 38: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 39: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 40: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 41: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Static composittion vs composition with con�gfs

Traditional g_xyz.ko modules directly #included f_*.c!

static composition con�gfs composition

config

config

config

function

function

function

config

config

function

function

config

config

function

function

config

config

config

function

function

function

config

config

config

function

function

function

No g_xyz.ko modules!

LinuxCon North America 2014 Make your own USB gadget 17 / 34

Page 42: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Function registration framework

$ mkdir function/rndis.usb0

request_module()

Sebastian Andrzej Siewior

LinuxCon North America 2014 Make your own USB gadget 18 / 34

Page 43: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Mapping of generic �lesystem concepts to con�gfs entities

directory : con�g_item (con�g_group)

�le : con�gfs_attribute

mkdir : con�gfs_mkdir()->make_item() (make_group())

read, write : show(), store()

ln -s : allow_link()

see : Documentation/�lesystems/con�gfs/con�gfs.txt

structconfig_item

structconfigfs_attribute

data to be set

(*show)(structure *, buffer)(*store)(structure *, buffer)

structure

attribute

$CONFIGFS_ROOT/structure/attribute

dentry->d_fsdata->s_element

LinuxCon North America 2014 Make your own USB gadget 19 / 34

Page 44: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

usb_function_instance

$ mkdir function/rndis.usb0

user-accessible con�guration data of this speci�c instance

legacy gadgets ≈ hardcoded

LinuxCon North America 2014 Make your own USB gadget 20 / 34

Page 45: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

usb_function

$ ln -s function/rndis.usb0 configs/c.1

usb_function

/* struct */

com

posi

te

funct

ion -

sp

ecific

the same as in legacy gadgets

composing a gadget (legacy/con�gfs) ends up providing this

see: drivers/usb/gadget

LinuxCon North America 2014 Make your own USB gadget 21 / 34

Page 46: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

FunctionFS vs con�gfs

delegate function implementation to userspace

mount FunctionFSwrite descriptors to ep0read/write/poll ep[1-]

ptp instance instance

ep0

ep1ep2ep3

ep0

ep1

ep0

ep1ep2

con�gfs: only create FunctionFS instances

eg $CONFIGFS_ROOT/usb_gadget/gadget/functions/�s.ptp

LinuxCon North America 2014 Make your own USB gadget 22 / 34

Page 47: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Some history

Sebastian Andrzej Siewior

idea (Decmber 2011)function registration interfacef_acm.c conversion to the function registration interfacef_acm.c con�gfs support (December 2012)

Andrzej Pietrasiewicz

From where he left o�, I took over

LinuxCon North America 2014 Make your own USB gadget 23 / 34

Page 48: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

Status matrix

with con�gfs

f_acm

.c

f_ecm.c

f_eem.c

f_ncm

.c

f_obex.c

f_phonet.c

f_rndis.c

f_serial.c

f_subset.c

f_mass_storage.c

f_fs.c

f_loopback.c

f_sourcesink.c

f_uac1.c

f_uac2.c

f_uvc.c

f_hid.c

f_midi.c

legacy gadget componentsg_hidg_midig_cdcg_etherg_ncmg_serialg_nokiag_multi

g_acm_msg_mass_storage

g_�sg_zerog_audio

g_webcam

gmainlineg lsent/RFCg ltodoj lunusedjLinuxCon North America 2014 Make your own USB gadget 24 / 34

Page 49: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

The idea, exampleOn implementationStatus

TODO

f_midi, f_hid

remove legacy gadgets - some (perhaps long) time in thefuture

gadgets not using composite framework

LinuxCon North America 2014 Make your own USB gadget 25 / 34

Page 50: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

userspace

With con�gfs ease of use is not concernedComposing is not di�cult, but tedious (at the very least ≈20shell commands)I want my "modprobe g_ether"!!!

shell scriptdedicated userspace program

$ modprobe libcomposite

$ mount none cfg -t configfs

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

$ echo 0x04e8 > idVendor

$ echo 0xa4a1 > idProduct

$ echo Foo > strings/0x409/manufacturer

$ echo Bar > strings/0x409/product

$ echo 123 > strings/0x409/serialnumber

$ mkdir configs/c.1

$ mkdir strings/0x409

$ mkdir configs/c.1/strings/0x409

$ echo "Conf 1" > configs/c.1/strings/0x409/configuration

$ mkdir functions/ecm.usb0

$ ln -s functions/ecm.usb0 configs/c.1

$ echo 12480000.hsotg > UDC

LinuxCon North America 2014 Make your own USB gadget 26 / 34

Page 51: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

userspace

With con�gfs ease of use is not concernedComposing is not di�cult, but tedious (at the very least ≈20shell commands)I want my "modprobe g_ether"!!!

shell scriptdedicated userspace program

$ modprobe libcomposite

$ mount none cfg -t configfs

$ mkdir cfg/usb_gadget/g1

$ cd cfg/usb_gadget/g1

$ echo 0x04e8 > idVendor

$ echo 0xa4a1 > idProduct

$ echo Foo > strings/0x409/manufacturer

$ echo Bar > strings/0x409/product

$ echo 123 > strings/0x409/serialnumber

$ mkdir configs/c.1

$ mkdir strings/0x409

$ mkdir configs/c.1/strings/0x409

$ echo "Conf 1" > configs/c.1/strings/0x409/configuration

$ mkdir functions/ecm.usb0

$ ln -s functions/ecm.usb0 configs/c.1

$ echo 12480000.hsotg > UDC

LinuxCon North America 2014 Make your own USB gadget 26 / 34

Page 52: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

libusbg

C API for all (wrap �lesystem operations)https://github.com/libusbg/libusbg, maintainer: Matt Portermost active developer: Krzysztof Opasiak (73/85 commits)Pending pull requests: https://github.com/kopasiak/libusbggadget-schemes

gadget (composition) export to �legadget (composition) import from �le

attrs = { idVendor = 0x04e8; idProduct = 0xa4a1; }

strings = (

{ lang = 0x409; manufacturer = "Foo"; product = "Bar"; serialnumber = "123"; }

)

configs = (

{

name = "c"

id = 1

strings = (

{ lang = 0x409; configuration = "Conf 1"; }

)

functions = (

{ function = { type = "ecm"; instance = "usb0"; } }

)

}

)

LinuxCon North America 2014 Make your own USB gadget 27 / 34

Page 53: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

libusbg

C API for all (wrap �lesystem operations)https://github.com/libusbg/libusbg, maintainer: Matt Portermost active developer: Krzysztof Opasiak (73/85 commits)Pending pull requests: https://github.com/kopasiak/libusbggadget-schemes

gadget (composition) export to �legadget (composition) import from �le

attrs = { idVendor = 0x04e8; idProduct = 0xa4a1; }

strings = (

{ lang = 0x409; manufacturer = "Foo"; product = "Bar"; serialnumber = "123"; }

)

configs = (

{

name = "c"

id = 1

strings = (

{ lang = 0x409; configuration = "Conf 1"; }

)

functions = (

{ function = { type = "ecm"; instance = "usb0"; } }

)

}

)

LinuxCon North America 2014 Make your own USB gadget 27 / 34

Page 54: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

gt

command line tool which uses libusbg

https://github.com/kopasiak/gt - _very_ initial stage

libusbg provides example programs

show-gadgetscreate a gadgetremove a gadgetgadget export (whole or parts)gadget import (whole or parts)

gt should combine these concepts into one app

LinuxCon North America 2014 Make your own USB gadget 28 / 34

Page 55: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

gadgetd

exposes gadgets' con�gfs throug DBUS

https://github.com/gadgetd/gadgetd

abstraction layer for in-kernel and FunctionFS functions

FunctionFS functions

accepts con�g �les for each functionmanages mounting FunctionFS (naming the instances!)opens ep0 and writes descriptors, opens other ep �lesstarts daemons only when needed (passes ep �les' descriptors)allows policing

LinuxCon North America 2014 Make your own USB gadget 29 / 34

Page 56: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

libusbg & toolsgadgetd & application API

the ecosystem - how it all �ts together

LinuxCon North America 2014 Make your own USB gadget 30 / 34

Page 57: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Q & A

Andrzej [email protected]

LinuxCon North America 2014 Make your own USB gadget 31 / 34

Page 58: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Referenceshttp://www.spinics.net/lists/linux-usb/msg74991.html

http://www.spinics.net/lists/linux-usb/msg76378.html

http://www.spinics.net/lists/linux-usb/msg83460.html

http://www.spinics.net/lists/linux-usb/msg86311.html

http://www.spinics.net/lists/linux-usb/msg86321.html

http://www.spinics.net/lists/linux-usb/msg86327.html

http://www.spinics.net/lists/linux-usb/msg86561.html

http://www.spinics.net/lists/linux-usb/msg90757.html

http://www.spinics.net/lists/linux-usb/msg90774.html

http://www.spinics.net/lists/linux-usb/msg90776.html

http://www.spinics.net/lists/linux-usb/msg97006.html

http://www.spinics.net/lists/linux-usb/msg98731.html

http://www.spinics.net/lists/linux-usb/msg110639.html

http://www.spinics.net/lists/linux-usb/msg110718.html

http://www.spinics.net/lists/linux-usb/msg110962.html

https://github.com/libusbg/libusbg

https://github.com/kopasiak/gt

https://github.com/gadgetd/gadgetd

https://github.com/gadgetd/gadgetd/wiki

LinuxCon North America 2014 Make your own USB gadget 32 / 34

Page 59: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Imageshttp://openclipart.org/detail/174619/4g-modem-and-sim-by-witcombem-174619 - slide 3

http://openclipart.org/detail/1964/calcubot-by-johnny_automatic - slide 3

http://openclipart.org/detail/96913/mouse-by-yves_guillou - slide 3

http://openclipart.org/detail/27549/keyboard-keys-by-simanek - slide 3

http://openclipart.org/detail/17924/computer-by-aj - slide 3

http://openclipart.org/detail/176486/pen-drive-by-carloernesto-176486 - slide 3

http://openclipart.org/detail/6633/neo1973-%28tango%29-by-ryanlerch - slide 3

http://openclipart.org/detail/17026/icon_puzzle_blue-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17027/icon_puzzle_green-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17028/icon_puzzle_grey-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17029/icon_puzzle_purple-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17030/icon_puzzle_red-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17031/icon_puzzle_yellow-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17060/icon_cube_green-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17061/icon_cube_orange-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/17062/icon_cube_red-by-jean_victor_balin - slides 4, 8, 9, 10

http://openclipart.org/detail/122449/question-button-by-ricardomaia - slide 5

http://openclipart.org/detail/3705/usb-plug-by-klaasvangend - slide 5

LinuxCon North America 2014 Make your own USB gadget 33 / 34

Page 60: Make your own USB gadget gadget Con gfs Userspace Make your own USB gadget Kernel and userspace Andrzej Pietrasiewicz Samsung R&D Institute Poland Warsaw, Poland andrzej.p@samsung.com

USB gadgetCon�gfs

Userspace

Imageshttp://www.linaro.org/documents/download/304a9a3e4024a2bb70312fc81d79446d51311e50ed8f4 - slide 9

http://openclipart.org/detail/10833/green-tick-by-ryan_taylor - slide 11

http://openclipart.org/detail/104197/calendrier�calendar-by-lmproulx - slide 23

http://openclipart.org/detail/33265/liste-/-list-by-lmproulx - slide 25

http://openclipart.org/detail/12929/large-braces-by-anonymous-12929 - slide 20, 21

LinuxCon North America 2014 Make your own USB gadget 34 / 34