Kernel USB API vs Userspace (libusb)
Vasily anarsoul Khoruzhick
email: anarsoul@gmail.com, skype: anarsoul
github.com/anarsoul

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

1 / 11
Template of (minimal) libusb app

libusb_init()
dev_handle = libusb_open_device_with_vid_pid(vid, pid)
libusb_claim_interface(dev_handle, intf_num)
libusb_set_interface_altsetting()
libusb_alloc_transfer() ... libusb_fill_*_transfer() ...
libusb_submit_transfer() ... completion callback ...
libusb_close(dev_handle)
libusb_exit()

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

2 / 11
Template of (simple) kernel driver for USB device

USB ID compatibility table (possible to match device class also)
driver probe function
usb_driver_claim_interface()
usb_set_interface()
usb_alloc_urb() ... usb_fill_*_urb() ... usb_submit_urb() ...
completion callback ... usb_free_urb()
driver removal function

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

3 / 11
Generic Kernel vs Userspace differences

No one cleanups after you in Kernel
Dereferenced NULL pointer? -> reboot
Corrupted some memory of other driver? -> reboot
Atomic/non-atomic context

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

4 / 11
Entry point

Userspace: should obtain device handle by itself
(libusb_open_device_with_vid_pid())
Kernel: contains USB ID compatibility table, device handle is passed
by driver core

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

5 / 11
Async API: libusb_transfer vs usb_urb

Represents single USB transfer
Contains source & destination (EP num, direction, pointer to buffer
and its size)
Completion callback
In kernel completion callback is called from atomic context

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

6 / 11
Async API libusb_submit_transfer() vs usb_urb_submit()

Almost no difference - both submit asynchronous transfer

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

7 / 11
Sync API: libusb_{control,bulk,interrupt}_transfer vs
usb_{control,bulk,interrupt}_msg

Kernel: can’t use sync API in atomic context
Kernel: userspace: it’s convenient, but usually not a good idea to use
synchronous API

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

8 / 11
Userspace: Pros and Cons

Pros:
Good for prototyping, decent development speed
Easy to debug
Convenient synchronous API

Cons:
Performance and response latency is worse

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

9 / 11
Kernel: Pros and Cons

Pros:
Decent performance and response latency

Cons:
Development speed is slower
Not so easy to debug
One should always keep in mind that it’s a kernel:
Can’t sleep in completion callback
Can’t use synchronous API in atomic context
Be carefull with memory and other resources
Async stuff is complicated, sync stuff in kernel is evil.

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

10 / 11
The End

Thanks! Questions?

Vasily Khoruzhick, Synesis LLC

Kernel USB API vs Userspace (libusb)

October, 2013

11 / 11

Vasily Khoruzhik - libusb vs linux kernel — сравнение userspace & kernelspace API

  • 1.
    Kernel USB APIvs Userspace (libusb) Vasily anarsoul Khoruzhick email: anarsoul@gmail.com, skype: anarsoul github.com/anarsoul Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 1 / 11
  • 2.
    Template of (minimal)libusb app libusb_init() dev_handle = libusb_open_device_with_vid_pid(vid, pid) libusb_claim_interface(dev_handle, intf_num) libusb_set_interface_altsetting() libusb_alloc_transfer() ... libusb_fill_*_transfer() ... libusb_submit_transfer() ... completion callback ... libusb_close(dev_handle) libusb_exit() Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 2 / 11
  • 3.
    Template of (simple)kernel driver for USB device USB ID compatibility table (possible to match device class also) driver probe function usb_driver_claim_interface() usb_set_interface() usb_alloc_urb() ... usb_fill_*_urb() ... usb_submit_urb() ... completion callback ... usb_free_urb() driver removal function Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 3 / 11
  • 4.
    Generic Kernel vsUserspace differences No one cleanups after you in Kernel Dereferenced NULL pointer? -> reboot Corrupted some memory of other driver? -> reboot Atomic/non-atomic context Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 4 / 11
  • 5.
    Entry point Userspace: shouldobtain device handle by itself (libusb_open_device_with_vid_pid()) Kernel: contains USB ID compatibility table, device handle is passed by driver core Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 5 / 11
  • 6.
    Async API: libusb_transfervs usb_urb Represents single USB transfer Contains source & destination (EP num, direction, pointer to buffer and its size) Completion callback In kernel completion callback is called from atomic context Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 6 / 11
  • 7.
    Async API libusb_submit_transfer()vs usb_urb_submit() Almost no difference - both submit asynchronous transfer Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 7 / 11
  • 8.
    Sync API: libusb_{control,bulk,interrupt}_transfervs usb_{control,bulk,interrupt}_msg Kernel: can’t use sync API in atomic context Kernel: userspace: it’s convenient, but usually not a good idea to use synchronous API Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 8 / 11
  • 9.
    Userspace: Pros andCons Pros: Good for prototyping, decent development speed Easy to debug Convenient synchronous API Cons: Performance and response latency is worse Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 9 / 11
  • 10.
    Kernel: Pros andCons Pros: Decent performance and response latency Cons: Development speed is slower Not so easy to debug One should always keep in mind that it’s a kernel: Can’t sleep in completion callback Can’t use synchronous API in atomic context Be carefull with memory and other resources Async stuff is complicated, sync stuff in kernel is evil. Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 10 / 11
  • 11.
    The End Thanks! Questions? VasilyKhoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 11 / 11