11
Kernel USB API vs Userspace (libusb) Vasily anarsoul Khoruzhick email: [email protected], skype: anarsoul github.com/anarsoul Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 1 / 11

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

Embed Size (px)

DESCRIPTION

Доклад Василия Хоружика на октябрьской линуксовке MLUG 2013

Citation preview

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

Kernel USB API vs Userspace (libusb)Vasily anarsoul Khoruzhick

email: [email protected], skype: anarsoul

github.com/anarsoul

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 1 / 11

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

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

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

Template of (simple) kernel driver for USB device

USB ID compatibility table (possible to match device class also)driver probe functionusb_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

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

Generic Kernel vs Userspace differences

No one cleanups after you in KernelDereferenced NULL pointer? -> rebootCorrupted some memory of other driver? -> rebootAtomic/non-atomic context

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 4 / 11

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

Entry point

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

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 5 / 11

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

Async API: libusb_transfer vs usb_urb

Represents single USB transferContains source & destination (EP num, direction, pointer to bufferand its size)Completion callback

I In kernel completion callback is called from atomic context

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 6 / 11

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

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

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

Sync API: libusb_{control,bulk,interrupt}_transfer vsusb_{control,bulk,interrupt}_msg

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

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 8 / 11

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

Userspace: Pros and Cons

Pros:I Good for prototyping, decent development speedI Easy to debugI Convenient synchronous API

Cons:I Performance and response latency is worse

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 9 / 11

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

Kernel: Pros and Cons

Pros:I Decent performance and response latency

Cons:I Development speed is slowerI Not so easy to debugI One should always keep in mind that it’s a kernel:

F Can’t sleep in completion callbackF Can’t use synchronous API in atomic contextF Be carefull with memory and other resourcesF Async stuff is complicated, sync stuff in kernel is evil.

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 10 / 11

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

The End

Thanks! Questions?

Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 11 / 11