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

747 views

Published on

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

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
747
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. 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
  2. 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. 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. 4. 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
  5. 5. 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
  6. 6. 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
  7. 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. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. The End Thanks! Questions? Vasily Khoruzhick, Synesis LLC Kernel USB API vs Userspace (libusb) October, 2013 11 / 11

×