+ All Categories
Home > Technology > LinuxAlt 2013: Writing a driver for unknown USB device

LinuxAlt 2013: Writing a driver for unknown USB device

Date post: 19-Jun-2015
Category:
Upload: lubomir-rintel
View: 1,842 times
Download: 2 times
Share this document with a friend
Description:
Reverse engineering an USB Video Grabber device protocol and creating a Linux kernel driver. Mostly the same talk as in OSSConf 2013, with Alice in Wonderland pictures.
Popular Tags:
36
Writing a Linux driver for an unknown device Ľubomír Rintel <[email protected]> LinuxAlt 2013, Brno BTC: 1GrpeEj18B6X7QFbh794bFGnZLRhVMqwL8
Transcript
Page 1: LinuxAlt 2013: Writing a driver for unknown USB device

Writing a Linux driverfor an unknown device

Ľubomír Rintel <[email protected]>LinuxAlt 2013, Brno

BTC: 1GrpeEj18B6X7QFbh794bFGnZLRhVMqwL8

Page 2: LinuxAlt 2013: Writing a driver for unknown USB device

Linux 2.4?

Page 3: LinuxAlt 2013: Writing a driver for unknown USB device

Linux 2.2?

Page 4: LinuxAlt 2013: Writing a driver for unknown USB device

Linux 2.0?

Page 5: LinuxAlt 2013: Writing a driver for unknown USB device

Great hardware support!

Page 6: LinuxAlt 2013: Writing a driver for unknown USB device

Our device● Unknown to Linux● No documentation● No Google hits for chip● Desperate users in

Ubuntu forums

Page 7: LinuxAlt 2013: Writing a driver for unknown USB device

No Linux support at all!

Page 8: LinuxAlt 2013: Writing a driver for unknown USB device

The Plan● Make it work in Windows● Capture what happens● Find image data● Mimic the behavior in userspace● Transform into a kernel module

Page 9: LinuxAlt 2013: Writing a driver for unknown USB device

USB

Page 10: LinuxAlt 2013: Writing a driver for unknown USB device

USB Architecture● Network of Host, Hubs and Devices

Page 11: LinuxAlt 2013: Writing a driver for unknown USB device

USB Addresses● Bus & Device number

Host

Device 1:1Hub

Device 2:1Hub

Device 3:1Mouse

Device 2:2Flash Drive

Page 12: LinuxAlt 2013: Writing a driver for unknown USB device

USB Addresses$ lsusbBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 002 Device 001: ID 1337:abcd Trololol USB 1.1 HubBus 002 Device 002: ID 1337:0123 Trololol Flash DriveBus 003 Device 001: ID dead:b4b3 Random Mouse$ lsusb -v...

Page 13: LinuxAlt 2013: Writing a driver for unknown USB device

USB Device● Self-describing● Endpoints

● CONTROL● INTERRUPT● BULK● ISOCHRONOUS

● Endpoints grouped into Interfaces● Interfaces grouped into Configurations

Page 14: LinuxAlt 2013: Writing a driver for unknown USB device

Our device

Device

Alternate setting 1Endpoints:

0x81 Isochronous IN0x82 Bulk IN0x83 Bulk IN0x84 Interrupt IN

Alternate setting 0Endpoints:

0x81 Isochronous IN0x82 Bulk IN0x83 Bulk IN0x84 Interrupt IN

Page 15: LinuxAlt 2013: Writing a driver for unknown USB device

The Plan● Make it work in Windows● Capture what happens● Find image data● Mimic the behavior in userspace● Transform into a kernel module

Page 16: LinuxAlt 2013: Writing a driver for unknown USB device

Do try thisat home!

Page 17: LinuxAlt 2013: Writing a driver for unknown USB device

Windows & VirtualBox

Page 18: LinuxAlt 2013: Writing a driver for unknown USB device

Wireshark & usbmon

Page 19: LinuxAlt 2013: Writing a driver for unknown USB device

What did we see● Number of CONTROL requests● ISOCHRONOUS packets once capture starts

Page 20: LinuxAlt 2013: Writing a driver for unknown USB device

RGB

R R R R R R R R

G G G G G G G G

B B B B B B B B

Page 21: LinuxAlt 2013: Writing a driver for unknown USB device

YUV2Y Y Y Y U1 U1 U1 U1

Y Y Y Y V1 V1 V1 V1

Y Y Y Y U2 U2 U2 U2

Y Y Y Y V2 V2 V2 V2

Page 22: LinuxAlt 2013: Writing a driver for unknown USB device

LibUSB● We could replay the traffic● In userspace – no kernel hacking needed● C, Python & Perl bindings● Now we need to find start & end of the picture

Page 23: LinuxAlt 2013: Writing a driver for unknown USB device

Test image

0xaaaaaaaa0xff00ff00

0x00ff00ff

0x80808080

0x00000000

Page 24: LinuxAlt 2013: Writing a driver for unknown USB device

Frame format

88 01 00 00

88 01 02 cf

88 02 80 00

88 02 82 cf

88 03 00 00

xx xx xx xx • 240 00 00 00 00 • 15

88 01 00 01...

...

● Frame number● Even/odd● Chunk number 0 – 0x2cf = 719

740 x 480 YUV2 Interlaced (NTSC)

...

Page 25: LinuxAlt 2013: Writing a driver for unknown USB device

To kernel!

Page 26: LinuxAlt 2013: Writing a driver for unknown USB device

To kernel!● Booooring!● A module● USB framework

● Linux Device Drivers: http://lwn.net/Kernel/LDD3/

● Video4Linux2● LWN Series: http://lwn.net/Articles/203924/

● Videobuf2● LWN Article: http://lwn.net/Articles/447435/

Page 27: LinuxAlt 2013: Writing a driver for unknown USB device

Architecture

Video4Linux2

Videobuf2

USB

Use

rspa

ce

Our

cod

e

Har

d war

e

Page 28: LinuxAlt 2013: Writing a driver for unknown USB device

Video4Linux2● Provide a device with known API

● open(), close()● read(), write()● ioctl()● mmap()

● Negotiate format with userspace

Page 29: LinuxAlt 2013: Writing a driver for unknown USB device

Videobuf2● Manages buffers of frames● Connects to Video4Linux2

● read(), write(), mmap()● some ioctl()s

– Start/stop capture– Exchange buffers with userspace

Page 30: LinuxAlt 2013: Writing a driver for unknown USB device

USB framework● Setup the device● Allocate buffers for exchange of data with

device● Handle start/stop● Isochronous callbacks

● Copy data from USB buffers to Videobuf2 buffers

Page 31: LinuxAlt 2013: Writing a driver for unknown USB device

Architecture

Video4Linux2

Videobuf2

USB

Use

rspa

ce

Our

cod

e

Har

d war

e

Page 32: LinuxAlt 2013: Writing a driver for unknown USB device

Works!

Page 33: LinuxAlt 2013: Writing a driver for unknown USB device

Community

Page 34: LinuxAlt 2013: Writing a driver for unknown USB device

Free Software● Mainline Linux 3.11

● drivers/media/usb/usbtv

● Contributions● Review● Bug fixes● Features!

Page 35: LinuxAlt 2013: Writing a driver for unknown USB device

Questions?

Page 36: LinuxAlt 2013: Writing a driver for unknown USB device

See you!

http://base48.cz#[email protected]

Did you find this useful? My Bitcoin address is:1GrpeEj18B6X7QFbh794bFGnZLRhVMqwL8

http://base48.cz#[email protected]


Recommended