

How to write a Linux device driver - rohshall
http://www.freesoftwaremagazine.com/articles/drivers_linux

======
codemac
VFIO!

The I/O MMU!

Write your drivers in userspace, save your sanity. I realize it's hard to get
a kernel that supports everything you need to accomplish this, but it's
definitely "the future" when it comes to device drivers.

However, this is a decent beginning point for people completely unfamiliar
with writing a driver. The table and the makefile parts are especially
important for beginners to _just get freaking started_.

~~~
nitrogen
No, don't write your drivers in user space. Especially for low-latency
devices, all the user-kernel-user message passing will be problematic. You
can't run at interrupt priority in user space, either.

~~~
jrockway
Most devices are USB dongles that flash lights and shift a couple bits, not
100Gbps network cards. It follows that most people's needs will be met by a
Python script and libusb, rather than a kernel module.

~~~
nitrogen
I suppose. I speak from the limited experience of reverse engineering a driver
for a PCI device that would freeze the entire system if its interrupts weren't
acknowledged immediately.

~~~
codemac
Interrupts are the slowest part of VFIO, but everything else is comparable.
The interrupt performance can and will be fixed in the future. Polling is an
option for some network uses, but interrupts are obviously technically
superior for most workloads.

The I/O MMU can be slightly slower than "true" DMA if done poorly, but the
simple use case is actually performs the best (in my teeny experience).

------
ChuckMcM
They missed the part where you need documentation on how the chip used in the
I/O device actually works. This is the 'tricky' bit way to often.

~~~
pmjordan
It's not too bad if the device uses USB and you have a working Windows driver
for it: run Windows in a virtual machine on Linux, pass the USB device through
to it and log the traffic using Linux' USB debug facility. Then figure out
what that traffic means. It depends on the device how difficult this is, but
for simpler devices it's actually quite straightforward.

For other types of devices or if you don't have ANY driver, it's obviously
somewhat trickier, as most I/O protocols are a lot lower level than USB (the
host controller driver takes care of most of the heavy lifting for USB). I
suspect you'll need specialist debug hardware to log the generated traffic.

~~~
shurane
What do you mean by 'specialist debug hardware'?

~~~
pmjordan
Basically, one of these:

<http://en.wikipedia.org/wiki/Bus_analyzer>

------
gte910h
This isn't half bad. (From someone who did this for a living at one point)

Btw, if you're writing a USB driver, most of the time the device uses HID,
even though that's for a completely different type of device then what HID is
supposed to be specced for.

~~~
pmjordan
_"From someone who did this for a living at one point"_

I'm curious: was this as an independent contractor or for an established
company? I really enjoy driver programming and low level stuff for some reason
(OSX & Linux and game consoles so far) and would rather like to specialise my
contract work on that kind of thing. Beyond people stumbling across my work
online, I really don't know who to approach and where to look for that kind of
work. It's rather a lot easier to get referrals for more mainstream
development work.

~~~
gte910h
I did it two places. Both were full time

Cable set top boxes often use Linux, and often need drivers.

------
chris_wot
Has much changed for 3.0?

~~~
spartango
No.

Keep in mind that the 2.6.x -> 3.0 transition was largely just in the version
numbers...in terms of the code there was no major overhaul or change.

ref: <http://lkml.org/lkml/2011/5/29/204/>

~~~
ajross
It was entirely just the version numbers. 3.0.0 is what 2.6.40 would have
been; there were no meaningful process changes in that release, and the list
of new features was entirely in keeping with all the other recent releases.
They simply popped a minor version number off the stack for simplicity.

------
Tequals0
Thanks for the post. I tend to write python to talk to custom USB devices, but
this looks like a nice alternative.

~~~
sontek
Can you elaborate? I specifically have a couple USB devices I need to work
with and thought I would have to write a device driver for them :)

~~~
noselasd
<http://sourceforge.net/apps/trac/pyusb/>

