Want to permanently mount Kernel-Driver into System - linux

I wrote my own kernel driver for a usb-device. After I compile it with make, I have the kernelobejct file usbdriver.ko and with sudo insmod usbdriver.ko I can install and then use it.
But if I restart my Debian, I need to do do insmod again to use it ...
How can i mount/install that driver permanently into the system, so that it loads when the os is starting? And how can I also grant other users than only root to access the asscoiated device-files under /dev/usbdriver0?

Copy your driver to /lib/modules/$(uname -r)/kernel/drivers/ and add it to /etc/modules: http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html.
I don't know but I think it's in the source code somewhere.

Related

PWM using bcm2835 library as non-root in Raspberry Pi

I need to control some DC motors using PWM. I have been trying to use the bcm2835 library, but I need root access to run my program. I need to avoid this, as this program is part of a ROS package, which gives problems if run as root. I have tried to follow the instructions that appear in the section "Running as root", but I haven't been successful. I have done the following.
First, I have installed libcap2 and libcap-dev and added my user to the kmem group.
sudo apt-get install libcap2 libcap-dev
sudo adduser ubuntu kmem
Then, I have installed the version latest version of bcm2835 library.
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.67.tar.gz
tar zxvf bcm2835-1.67.tar.gz
cd bcm2835-1.67
./configure
make
sudo make check
sudo make install
After that, I uncommented the #define BCM2835_HAVE_LIBCAP line in bcm2835.h, which I found in /usr/local/include/.
I allowed write access to /dev/mem by members of kmem group.
echo 'SUBSYSTEM=="mem", KERNEL=="mem", GROUP="kmem", MODE="0660"' | sudo tee /etc/udev/rules.d/98-mem.rules
sudo reboot
And finally, I compiled my program (called l298n) and run the following command.
sudo setcap cap_sys_rawio+ep l298n
After doing all this, my program is still not able to provide PWM control unless I run it as root.
Have I done anything wrong? Is there any other way around this? As far as I know, other libraries would run into the same problem, as it depends on the access level of /dev/mem.
I am running Ubuntu 20.04 LTS in a Raspberry Pi 3B+.
What you did wrong:
You edited the wrong bcm2835.h, you need to edit bcm2835.h where ever you unpacked bcm2835-1.67.tar.gz, not /usr/local/include.
You didn't recompile the library to actually have the added feature in it.
Also, if you're just accessing the GPIO pins and don't need non-root access to /dev/mem then there's no need to do anything more than adding access rights to /dev/gpiomem to your non-root user.
Have you tried version 68 already ?
There where made some changings concerning 'BCM2835_HAVE_LIBCAP'.

How can I solve stdarg.h No such file or directory while compiling out-of-tree Linux kernel module?

I have an out-of-tree Linux kernel module that I need to compile. When I execute "make" in the kernel module directory I am getting:
"fatal error: stdarg.h: No such file or directory"
Before starting the build I installed the header file based on my Linux distribution.
$sudo apt-get install kernel-headers-$(uname -r)
How can I solve this compilation error? (my distribution is Ubuntu 16.04 with linux-headers-4.15.0-42-generic)
I ran a search of stdarg.h with the "locate" command to see if I can sport the file on the system.
I got:
/usr/include/c++/5/tr1/stdarg.h
/usr/lib/gcc/i686-linux-gnu/5/include/cross-stdarg.h
/usr/lib/gcc/i686-linux-gnu/5/include/stdarg.h
...
It tells me there is at least one stdarg.h provided by the compiler.
I tried to include the path "/usr/lib/gcc/i686-linux-gnu/5/include" in the kernel module Makefile so stdarg.h can be picked up. It did not work (while building, another reference to stdarg.h in the official kernel header was not being resolved).
I finally created a symlink directly under:
/usr/src/linux-headers-4.15.0-42-generic/include
$sudo ln -s /usr/lib/gcc/i686-linux-gnu/5/include/stdarg.h stdarg.h
This was just enough to solve the compilation issue.
I am wondering if the kernel headers should come with an implementation of stdarg.h by default (that is the first time I encounter this issue). I have also read that the compiler provide one implementation and most of the time it is better to use the compiler version.
Updated note: if the above solution still does not solve the problem:
Before running make again, do a make clean. Do a ls -la in the folder and look for a ".cache.mk" file. If this is still there, remove it and run "make" again. It should solve the problem.
I had the same issue with CentOS 9, and the other answers didn't work for me. Apparently the problem is that in more recent kernels, it shouldn't be <stdarg.h> but <linux/stdarg.h>. With virtualbox guest additions 6.1.34, it correctly checks for kernel with a version of 5.15.0 or more. But my kernel is the 5.14.xx, meaning the include for stdarg.h is wrong.
Solving the issue
Dependencies
Install all the dependencies for the guest edition
gcc make perl kernel-devel kernel-headers bzip2 dkms
Installation
Run the Guest Addition installation like you would normally. It will fail by saying it is unable to compile the kernel modules. That's expected. It will copy all the file we need to the VM disk.
Editing
We now need to edit the erroneous files.
/opt/VBoxGuestAdditions-6.1.34/src/vboxguest-6.1.34/vboxguest/include/iprt/stdarg.h
/opt/VBoxGuestAdditions-6.1.34/src/vboxguest-6.1.34/vboxsf/include/iprt/stdarg.h
On line 48 (may change for different versions), it check for a version of Linux and select the correct header depending on the version. We need to replace if RTLNX_VER_MIN(5,15,0) with if RTLNX_VER_MIN(5,14,0) in both files.
Compile the kernel modules
We can now compile the kernel modules, and the error should be gone.
sudo rcvboxadd quicksetup all
I personally got an error the first time, but then I recompiled without changing anything and it worked.
Remember that it's just a workaround, it may not work with different versions.
If you using Arch Linux with zen-kernel:
sudo CPATH=/usr/src/linux-zen/include/linux vmware-modconfig --console --install-all
I had the same problem with VirtualBox 6.1.0 running archlinux with kernel 6.1.9.
I downloaded VirtualBoxGuestAdditions_7.2.0.iso file from https://download.virtualbox.org/virtualbox/7.0.2/ link(you may select more appropriate to your VirtualBox version) and assigned as an optical drive to virtualbox machine. After start of the system running blkid command on terminal showed the name of CD rom device which was /dev/sr0. then I created iso folder on
/mnt folder
mkdir /mnt/iso
and mounted cd drive to that folder
mount -o loop /dev/sr0 /mnt/iso
after I cd'ed to /mnt/iso
cd /mnt/iso
and manually run VirtualBoxGuestAdditions.run script
sh ./VirtualBoxGuestAdditions.run
which successfully compiled and istalled required virtualbox guest modules.
Now everytime I update kernel version I redo the same procedure. And it work fine.
It also remove old 6.1.0 guest additons folder.

XRUSB raspberry pi

I'm working with a raspberry pi connected with xrusb to a controller using python.I use make file to Compile and install the common usb serial driver module and it works fine. After reboot i have problem. The driver is lost. I have to install the module again using this
modprobe usbserial
insmod ./xr_usb_serial_common.ko
Any idea?
Now my answer might be off because of the way you say "install the driver". I bet the make script most likely just loaded the driver just like you did via modprobe.
In order to get the module to be loaded at boot time, you need to tell udev what to load/do during bootup. And tell the kernel to load your driver.. Otherwise it assumes you don't want it to be loaded at boot time.
Either you can do a automatic module handling via:
#nano /etc/modules-load.d/usbserial.conf
usbserial
or, you can specify options:
#nano /etc/modprobe.d/usbserial.conf
options usbserial parameter_name=parameter_value
Here's some documentation on how this works:
http://man7.org/linux/man-pages/man5/modules-load.d.5.html
https://wiki.archlinux.org/index.php/kernel_modules
(Even if you're not running Arch on your RPi, they still have one of the best documentation websites for Linux out there. User friendly, in depth etc. So apply the information there to your Distribution, they should be very much the same this day and age)
I found maybe a temporary solution so i can finish my project and look for the best way later.
I make a script to run after reboot to load the driver.
use:
sudo crontab -e
then go to the bottom and write
#reboot bash /your/path/script/script.sh
`

How to install Kernel Modules to lib/modules/ from Kernel-Source

Got a Kernel-Source and I want to install the Modules to my ARM-System to the path lib/modules. How can I do that?
You can place them in a directory in /lib/modules. Then you will need to edit /etc/depmod.conf to make the kernel use them on boot. Then run the depmod command to make these changes take effect. If you want to load them immediately, you will have to use the modprobe command.

Quickest way to modify/reload existing Linux driver

I am trying to understand an existing Linux Wi-Fi driver for a USB Wi-Fi adapter. While I can read the C code, I would also like to be able to insert debug/print statements at certain critical spots in the driver to see how it behaves when executed. On a Linux system, after modifying the driver code, how does one go about loading it into the kernel in such a way that it replaces the old driver? Is there a way to "hotplug substitute" it straight over the old driver, or is it more complicated than that?
I intend on doing this inside of an expendible virtual machine, so I am not concerned about messing up the original driver, for what it counts.
If the driver is compiled as a module, all you need to do is to add your debug print outs, compile the module, rmmod the original module, insmod your new module and initiate the WLAN connection as usual.
If you want to test and edit on the fly :
lsmod to find the module name
rmmod it
edit source
Make sur you got a Makefile : obj-m := module_name.o
recompile (on Ubuntu) : make -C /usr/src/linux-headers-'uname -r' M='pwd' modules
insmod module_name.ko
If it's a device module, you might want to rm any devices in /dev, then do a mknod to remake them (see mknod man) and finally chmod to correct rights.

Resources