Re: How do I write a driver and how do I find out what kernel API calls do?

From: Farhan Khan <farhan_at_farhan.codes>
Date: Fri, 22 Apr 2022 04:35:37 UTC
On Thu, 2022-04-21 at 18:17 -0500, Yusuf Khan wrote:
> Literally just copy paste the title here.
> 
> Also where do I get documentation?

Hi Yusuf!

I'm no expert, but I got my (limited) knowledge from a few sources:

1. There is a section in the documentation on this topic:
https://docs.freebsd.org/en/books/arch-handbook/driverbasics/
2. There's a book called "FreeBSD Device Drivers: A Guide for the
Intrepid". Its a bit dated but the concepts largely still apply.
3. I read a ton of sample drivers. I'm currently struggling through the
USB stack, /usr/src/sys/dev/usb/wlan has a lot of good and simple WiFi
drivers. If you want an example of a wifi driver that is both USB and
PCI, look at rtwn(4).
4. Each stack has some documentation, so in the case of USB, I read the
usbdi man page and looked at examples.
5. Look at other implementations in other BSDs - or even illumos.
Ultimately its not FreeBSD but it helps provide some context,
especially for someone like me who has to learn multiple layers/stack
prior to understanding what is happening, specifically what is device-
specific and what is OS-specific. There's a ton of overlap and in some
cases, such as WiFi, they maintain a slightly older version and
therefore simpler version of the same stack.

In terms of the details of a specific device, I'm honestly just copying
previous implementations, namely Linux and OpenBSD. There's a great
presentation by an OpenBSD developer on how he wrote wifi device
drivers with limited context. You can find it on YouTube as: "Getting
started with OpenBSD device driver development, by Stefan Sperling
(EuroBSDcon 2017)"

I was hoping to write some documentation of my experiences, but I have
no immediate plans. If you struggle with the same thing, maybe write
your experiences for the benefit of the community.

I hope this helps! btw, nice last name!

- Farhan Khan