I wanted to work on linux kernel from some time, but didn’t really know where to start. One day a wise man told me “a good way to learn is to do it! So get an USB device and write the driver for it.” I answered: “Well I didn’t thought of that, usually I’d use libusb for such cases.”
A USB device
Some days later, I didn’t find any nice device, but I thought, why not building it as well?
As I already told you, I’ve already hacked out an USB device: USBtinyISP. So I got back to the original code: usbtiny and I build myself a small device with 4 LED and 2 buttons:
I didn’t use and ATtiny, but an ATmega8 to keep more room for future improvements 😉 (The ATtiny2313 is already full with the USBtinyISP code).
And here is the schematic: (as picture, the gschem sources stay in git under the sch
folder):
Once the wiring done, some few lines of code based on the usbtiny sample, and that’s it: A firmware for the ATmega (in the root of the project).
Then I needed to test it, so back to the first idea, some code lines using libusb, available in the test
, helped to improve the firmware 😉
Everything’s OK, let’s write a driver.
The Linux driver
Well I just followed some tutorials:
- /dev/hello_world: A Simple Introduction to Device Drivers under Linux by Valerie Henson
- Writing a Simple USB Driver by Greg Kroah-Hartman
Finaly looking at the usbled.c
file in Linux sources to get some more updates.
This build a module that adds the files leds
and keys
in the device folder that Linux creates in /sys/bus/usb/
.
That’s it! I don’t know if you learned something, but I did learn a lot! 😉
All source code are available under GPL2 or GPL2+ on my gitweb: jvdg_usbgadget.git
As for the egocentric name, it’s just to avoid any name conflict. This has no mean to be integrated anywhere anyway. (The device uses an USB ID reserved to prototyping, so unusable for products…)