# Canbus support in the kernel for C program



## ianc1215 (Aug 20, 2022)

I am trying to port a 3D printer firmware / daemon called Klipper to run on FreeBSD. So far it all went well until it wanted to <linux/can.h>

I am running out of places to look for that file. I don't see it anywhere in the standard libraries. Is there canbus support in FreeBSD? If so, where can I locate the header file?


----------



## Profighost (Aug 20, 2022)

maybe this is what you're looking for?

After searching wikipedia I found:
SocketCAN-Wikipedia
containing a link leading to
linux-can@github
which contains can.h at:
https://github.com/linux-can/can-utils/tree/master/include/linux


----------



## Phishfry (Aug 20, 2022)

I would import the NetBSD sourcetree work if that fails. FreeBSD does not have canbus.





						can(4) - NetBSD Manual Pages
					






					man.netbsd.org


----------



## ianc1215 (Aug 20, 2022)

Phishfry said:


> I would import the NetBSD sourcetree work if that fails. FreeBSD does not have canbus.
> 
> 
> 
> ...


I found stumbled upon that last night, is there any level of compatibility between FreeBSD and NetBSD kernel components or would it require some work to port over?


----------



## Phishfry (Aug 21, 2022)

ianc1215 said:


> or would it require some work to port over?


I am not a programmer by trade so I cannot answer that.
What I can say is that several FreeBSD drivers have NetBSD origins, just like OpenBSD.

That said it is probably a better code learning guide than any Linux. It will not be a direct swap in.
Especially seeing how CAN has 3 distinct areas of concern:

-canctl
-can network hooks
-canbus protocol

I could see NetBSD having a slightly different ifconfig code base that may need adjusting to FreeBSD.

CAN would have been a great addition to my FreeBSD Foundation Wish List.

That FreeBSD is not in use in vehicle computer systems is disappointing.

Which brings me to this question:
How could a 3D printer firmware need CAN ?
Needing just a header file is one thing, but a printer needing CAN is preposterous.


----------



## ianc1215 (Aug 21, 2022)

Phishfry said:


> I am not a programmer by trade so I cannot answer that.
> What I can say is that several FreeBSD drivers have NetBSD origins, just like OpenBSD.
> 
> That said it is probably a better code learning guide than any Linux. It will not be a direct swap in.
> ...


Klipper uses CANbus for some very high end printers. Most of the time it doesn't use CANbus, sadly for my needs its more of a build dependency rather than a needed feature.


----------



## Phishfry (Aug 21, 2022)

Then I would use the NetBSD source codebase for your can.h needs if it works.
Compilers are quite vocal so you should be able to pinpoint any additional needed dependencies.
The NetBSD source tree may have a different organization as well. So it may take some studying for file placement.
I would only copy over the bare minimum and use compiler errors for further actions.
This seems to be the top level directory for can.h


			Index of pub/NetBSD/NetBSD-current/src/sys/netcan/


----------



## Phishfry (Aug 21, 2022)

The problem I see is inevitably you need more than a header file (can.h).
The real meat and potatoes are the 'c' functions. So you need to see what Klipper needs from can.h and trace it.
NetBSD can.h might not even contain what you need. You may need the Linux version.

Just look at all the other dependencies of NetBSD can.c. Quite a rabbithole.
What are the chances FreeBSD has all these and they work the same? That is what you need to checkout.


> #include <sys/param.h>
> #include <sys/systm.h>
> #include <sys/mbuf.h>
> #include <sys/ioctl.h>
> ...


----------



## Phishfry (Aug 21, 2022)

Phishfry said:


> The NetBSD source tree may have a different organization as well. So it may take some studying for file placement.


For example I see that on NetNSD this resides in /sys/netcan/ but on FreeBSD this would reside in /sys/dev/netcan/

When the source tree is referenced it is common to leave off the full directory path and exclude /usr/src/.
This is because you need to change directory to /usr/src/ to compile base system.
To compile individual components of base you must change directory to the component you want to compile.


----------



## richardtoohey2 (Aug 21, 2022)

ianc1215 said:


> its more of a build dependency rather than a needed feature


So can you remove or make optional that dependency?

Sprinkle a few ifdefs?

Might not be feasible but might be less work (at least initially while you see if any other issues.)


----------



## ianc1215 (Aug 21, 2022)

richardtoohey2 said:


> So can you remove or make optional that dependency?
> 
> Sprinkle a few ifdefs?
> 
> Might not be feasible but might be less work (at least initially while you see if any other issues.)


I can't remove it without making problems. Klipper is part C and part Python. Also its a project that is a bit WIP. I had though about doing that very thing. Perhaps I should revisit that idea.


----------

