Partial answer: USB drivers generally are kernel modules that are loaded for specific USB devices by looking at vendor/device id's and other patterns embedded into the module. You can find this information for existing drivers with modinfo
, e.g.
$ sudo modinfo usbhid
filename: /lib/modules/4.12.13/kernel/drivers/hid/usbhid/usbhid.ko
license: GPL
description: USB HID core driver
author: Jiri Kosina
author: Vojtech Pavlik
author: Andreas Gal
alias: usb:v*p*d*dc*dsc*dp*ic03isc*ip*in*
depends: hid,usbcore
intree: Y
vermagic: 4.12.13 SMP preempt mod_unload modversions 686
parm: mousepoll:Polling interval of mice (uint)
parm: jspoll:Polling interval of joysticks (uint)
parm: ignoreled:Autosuspend with active leds (uint)
parm: quirks:Add/modify USB HID quirks by specifying
quirks=vendorID:productID:quirks where vendorID, productID, and quirks are all in 0x-prefixed hex (array of charp)
There are also various tools that can show for an existing device which driver is in use. I believe these all end up using the information from the /sys
hierarchy eventually.
So if you want to write a new driver, you need to learn how to write a kernel module (google, there are tutorials). Pick an existing driver and modify it.
You can also write "drivers" (or rather appliation programs) in user space with a library like libusb.
The kernel documentation also includes information about the USB API, and by extension, information how the USB stack works.