WiFi support for a hobby OS

This forums is for OS project announcements including project openings, new releases, update notices, test requests, and job openings (both paying and volunteer).
Post Reply
User avatar
mohammedisam
Member
Member
Posts: 38
Joined: Tue Jan 06, 2015 5:15 pm
GitHub: https://github.com/moisam

WiFi support for a hobby OS

Post by mohammedisam »

Hi OSDevs,

I wanted to share my journey in adding WiFi support to LaylaOS.

TL;DR:

I managed to port Linux Qualcomm Atheros WiFi driver to LaylaOS with minimal code changes and with a thin glue layer to bridge Linux code to my own kernel.

My code, including the glue layer, code changes, Makefile and build script are available from my GitHub repo.

The long story:

I have a native network stack that I tested on QEmu and Oracle VM VirtualBox, but I wanted to move things to real hardware. I have a Toshiba Satellite Pro laptop with Qualcomm Atheros card, and given I do not have an Ethernet cable, I wanted to make use of the WiFi card to work on my network stack.

The question of how to add WiFi support to a hobby OS has been raised before on this forum and others (1, 2, 3, 4, 5). One approach is to choose a card and start writing a driver for it, as Haiku and some hobby OSes did (1, 2, 3). The problem is, as mentioned in many places before, it takes a lot of time to write one driver per card, and a lot of cards do not have enough documentation available for hobbyists.

The second option was to forget WiFi and focus on wired networking. I didn't want to do that.

The third option was to port an existing driver to LaylaOS. After a lot of search, I narrowed my options down to either FreeBSD or the Linux driver.

I started with the FreeBSD driver, thinking it would be more modular and easy to separate from the rest of the kernel. Big mistake. I entered header dependency hell. Header files are used both by the kernel and userspace, and I ended up rewriting so many header files to remove dependencies, redefine macros and change structs to fit the ones from musl, etc. I eventually gave up on it.

Even working on FreeBSD's translation layer that uses Linux WiFi infrastructure (LinuxKPI) was too much work that was not worth it.

I eventually turned to Linux. The driver (ath9k) comes with implicit dependencies on other kernel subsystems:
  • mac80211: handles network traffic, rate control, frame management, etc.
  • cfg80211: network socket connection for userland
  • Red-blue hash tables
I created a Makefile and a build script and started the build. Failed.

I went back and fixed a few macro definitions and a few lines in the Linux source and tried again. Failed.

After a few rounds I managed to build the module's shared object. Yay!

I used nm -u to find undefined symbols and started adding those functions as empty skeletons. I kept going until there were no more undefined symbols (apart from my kernel's functions, which will be resolved at runtime).

I then started fleshing out the functions. Areas that needed most attention were:
  • kmalloc() as Linux defines this as a macro and the kmalloc function family have extra arguments I don't use in my kernel
  • Virtual-to-physical address conversion and vice versa
  • port I/O to bypass Linux's subsystem and use direct port I/O
  • Converting Linux skbuff packets to my native packets and vice versa
  • Linux's asynchronous jobs (work queues, tasklets, and timers)
  • I ended up importing Mbed TLS to handle cryptography
I ended up with a "glue layer" that bridges Linux code to my own, to give the driver the functions it expects to find in a Linux kernel. After a few tests on real hardware, the driver was able to identify the card and initialise it, and IRQs were working properly.

The next step was to build a userland program (wlan-ctrl, similar to wpa_suppicant) that I can use to control the driver (request network scan, get scan results, bind to a network, etc.). As my desktop is Qt-based, I added a systray widget to allow the user to interact with wlan-ctrl and show results, connect to a network, etc.

It took a few months, but its working! Having Linux WiFi infrastructure will allow me to add more pieces, e.g. Intel iwlwifi driver, in the future if needed.

Now to the pros and cons of this approach (using Linux code in my OS):

Pros:
  • Using a tested working driver instead of starting from scratch
  • Leveraging the thousands lines of code that are written, tested, and debugged by other programmers
  • Minimal code changes to the Linux source, in addition to my thin glue layer
Cons:
  • Using other people's code instead of writing your own
  • The time it takes to figure out someone else's code to write your glue layer
  • While Atheros code does not include binary blobs, others (e.g. iwlwifi) include proprietary binary blobs that some might not be happy to use

Thoughts and comments are welcome as always.

Best,

Mo
Post Reply