# Porting Linux driver to FreeBSD



## bcomputerguy (Jan 6, 2017)

Hi

I am trying to get into BSD kernel development but I am having a little bit of trouble porting this code from Linux to FreeBSD.

I am trying to do this without the linuxkpi because I think that's a road to nowhere, so while this will be a bit more difficult to get up and going. It will allow better understanding and a way to actually port Linux code by understanding what the Linux kernel is doing and using equivalent structures and kernel calls in FreeBSD.

With that being said, I have this code below.
http://paste2.org/8AH4wJHf

I am not super familiar with FreeBSD kernel structures and code layout just yet but I am getting there.

I recently downloaded the 4.9 release of the linux kernel and I'm looking around in there to see. A lot of the code seems pretty generic until it gets to things like the memory allocation, virtual memory and some queue type structures in files like slab.h or kworker.


```
// #include <linux/acpi.h>
#include "/home/username/custom_build/linux-4.9/arch/powerpc/boot/stdio.h" //:#define    EINVAL        22    /* Invalid argument */

#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
#include <linux/stringify.h>
#include <linux/version.h>
#include <linux/workqueue.h>
```

For instance above kthread, kernel, input, dmi, delay, mutex, etc...

I am sure FreeBSD must have an equivalent function to stringify, workerqueue, kthread, etc...

How can I find these equivalent files in FreeBSD?


----------



## drhowarddrfine (Jan 6, 2017)

Most of the people who hang out here are sysadmins and not developers, though some devs and good programmers do and you may still get your answer. I suggest you try the mailing lists and irc channels where the developers hang out.


----------



## bcomputerguy (Jan 8, 2017)

hmm, this seems like it will definitely be a challenge.


----------



## SirDice (Jan 11, 2017)

bcomputerguy said:


> I am trying to get into BSD kernel development


I can highly recommend getting yourself a copy of "The Design and Implementation of the FreeBSD Operating System" (2nd edition). I recently got one (hardcover) for about 30 Euro during a sale. 

https://www.amazon.com/Design-Implementation-FreeBSD-Operating-System/dp/0321968972/


----------



## bcomputerguy (Jan 12, 2017)

gpatrick said:


> It isn't insurmountable.  Linux KVM was ported by Joyent and runs on SmartOS.



Oh, I had no idea about SmartOS, interesting; I recognize that CTO as the guy who wrote Dtrace. SmartOS seemed to have forked off of Solaris which also has some BSD code in there. Interesting stuff.



SirDice said:


> I can highly recommend getting yourself a copy of "The Design and Implementation of the FreeBSD Operating System" (2nd edition). I recently got one (hardcover) for about 30 Euro during a sale.
> 
> https://www.amazon.com/Design-Implementation-FreeBSD-Operating-System/dp/0321968972/



I am going to look into getting into this book. I think it's a great investment.


----------



## bcomputerguy (Jan 12, 2017)

actual video url page

https://03-lvl3-pdl.vimeocdn.com/01...xpires=1484204604&token=0e92de2928212cda44d88

Interesting talk on SmartOS but I don't really agree with the path they took, especially when he's talking about all the crap they did to port linux kvm to their project. Am I the only one that thinks all this excessive virtualization or emulation to build any non trivial production system is just a mess of tape, tar, oil sands, etc...


----------



## kev009 (Jan 16, 2017)

Take a look in /usr/src/sys/dev/acpi_support, the asus_wmi implementation in particular should have almost all the KPIs you will need.  There are some philosophical differences from Linux, so you have to consider each concept that the Linux driver is doing and map that to something FreeBSD has (i.e. BSD usually uses sysctls, Linux sysfs).


----------



## bcomputerguy (Jan 16, 2017)

kev009 said:


> Take a look in /usr/src/sys/dev/acpi_support, the asus_wmi implementation in particular should have almost all the KPIs you will need.  There are some philosophical differences from Linux, so you have to consider each concept that the Linux driver is doing and map that to something FreeBSD has (i.e. BSD usually uses sysctls, Linux sysfs).



This is the type info that I am looking for. I actually took up SirDice advice and ordered that book. It's going to take 3-4 weeks to get to my location. 



SirDice said:


> I can highly recommend getting yourself a copy of "The Design and Implementation of the FreeBSD Operating System" (2nd edition). I recently got one (hardcover) for about 30 Euro during a sale.
> 
> https://www.amazon.com/Design-Implementation-FreeBSD-Operating-System/dp/0321968972/



I will learn these differences and use the linux code to bootstrap FreeBSD graphics stack. I really don't want to make FreeBSD Linux, I think once I sufficiently understand the problem I can translate the code in a way that's easy to maintain and update once the foundation is set. It will take some time but I'm investing that time.

on another note, I watch a lot of *BSD lectures on Youtube and it seems like most *BSD developers use Mac laptops, why is that?


----------



## tingo (Jan 17, 2017)

Because they don't want to setup FreeBSD on their laptop? Or perhaps they bought a Mac OS X laptop, thinking that it was "almost the same as FreeBSD" and later ended up installing FreeBSD on it? Or even, perhaps, because they like the way the Apple machines look? (personally I don't understand  this, but taste is a different thing for everyone)
It could be thousand different reasons - don't put too much weight on it.


----------



## bcomputerguy (Jan 17, 2017)

tingo said:


> Because they don't want to setup FreeBSD on their laptop? Or perhaps they bought a Mac OS X laptop, thinking that it was "almost the same as FreeBSD" and later ended up installing FreeBSD on it? Or even, perhaps, because they like the way the Apple machines look? (personally I don't understand  this, but taste is a different thing for everyone)
> It could be thousand different reasons - don't put too much weight on it.



This seems a bit odd to me. Initially I had a retina macbook pro because mac use to be great but now they are becoming giant overpriced ipads.

What was strange is that almost nothing on the mac worked, except the trackpad, which was strange. I got a more general purpose laptop and other things work on the PC that didn't work on the mac but things that I would think should be better supported on the PC doesn't.

A strange matrix of things that work/ doesn't work on PC/Mac hardware when running current FreeBSD.

Then when I am researching things, looking at the *BSD conferences available on youtube and seeing the devices all the speakers uses, I could count on 1 hand the number of times you'd see a non mac device. This is odd to me since the guys/ gals giving the talks are the most dedicated to the BSD projects but they run Mac hardware.

The way apple products are going, is this a viable solution for long term? Wouldn't it be better to use devices that have real hardware.

My assumption is that since BSD is used mainly on servers, most people just use Mac HW and ssh into a BSD server somewhere. What if I want to use BSD on my hardware, should I have to go buy some 10 year old mac or ibm?

Why can't we have nice things?

[EDIT]
speaking of mac hardware. I was just doing some more research and reading again, came across this teachbsd powerpoint and check it yourself.

https://github.com/teachbsd/course/blob/master/teaching_with_freebsd.pptx

This explains why certain parts of BSD is undercooked. It will take a lil time and good understanding but at least I ordered the book in that power point. It will be here in a month or less.


----------



## tingo (Jan 17, 2017)

You can have "nice things" - but you might have to create them yourself if they don't exist already. Nothing happens as if by magic; if you can buy a "nice thing" (whatever it is) some people has worked hard to create that thing or its blueprint.
Consuming is easy, creating is hard.


----------



## tankist02 (Jan 18, 2017)

I heard that even OpenBSD and DragonFlyBSD are more advanced in some respects (graphics, wifi) for desktop than FreeBSD. I suspect most FreeBSD developers are happy with their Macs and are not that much interested in desktop development.


----------



## bcomputerguy (Jan 18, 2017)

tankist02 said:


> I heard that even OpenBSD and DragonFlyBSD are more advanced in some respects (graphics, wifi) for desktop than FreeBSD. I suspect most FreeBSD developers are happy with their Macs and are not that much interested in desktop development.



OpenBSD and DragonFlyBSD uses the linuxkpi for the graphics, sure it's up and running but I have differing opinion with their methods "porting" linux drivers to BSD. It's basically importing all the linux headers and trying to emulate linux on BSD.

This little simple file is actually a project that I am tackling in hopes to learn more about linux vs BSD kernel so that I can properly port software without using the linuxkpi stuff.

As far as WIFI is concerned, I think Adrian is doing most of that work and I think he mainly develops on FreeBSD.

Now sure, TrueOS does do the desktop out the box thing a bit quicker but TrueOS is mostly FreeBSD CURRENT with some of their own changes.

I personally think it and am working on porting linux software to BSD. I would like to bootstrap the graphics stack but I won't be using those linuxkpi headers, that doesn't seem like a viable long term strategy; especially with the way the linux kernel is gaining weight.

I don't understand why people think an increasing LOC is a good thing. Keep ABI stable, refactor and shrink LOC to do the same work would seem like a win to me but maybe I am just nuts.


----------



## NUNO MIGUEL PIRES FAISCA (Jan 4, 2019)

tuxedo would be fantastic , as linux or kernel / headers


----------

