Skip to content

3. Notes

Steve Cirelli edited this page May 17, 2022 · 7 revisions

Kernel Module Notes

Drivers have a class name and a device name. In Listing 2, ebb is used as the class name, and ebbchar as the device name. > This results in the creation of a device that appears on the file system at /sys/class/ebb/ebbchar.

Useful commands

  • lsmod list installed kernel modules
  • sudo insmod <module name>.ko install a loadable kernel module (LKM)
  • sudo rmmod <module name> uninstall a LKM
  • sudo tail -f /var/log/kern.log follow the kernel logs to see module logs
  • cat /proc/interrupts list interrupts
  • ls -l /sys/class/gpio/ exported GPIOs

Give User-level Access to LKM

  • Identify the sysfs entry for the device
    sudo find /sys -name <driver name>
    
  • Identify the KERNEL and SUBSYSTEM values about which to write the udev rule
    udevadm info -a -p /sys/class/<class>/<device>
    
  • Rules are contained in the /etc/udev/rules.d. A new file can be added with the above values, where the file name begins with a priority number. Using a name such as 99-<device name>.rules creates a new rule with the lowest priority, so that it does not interfere with other device rules.
    sudo bash -c "cat > /etc/udev/rules.d/99-<device-name>.rules" << 'EOF'
    KERNEL=="<device name>", SUBSYSTEM=="<class name>", MODE="0666"
    EOF
    
  • Install the driver
    sudo insmod <device name>.ko
    
  • Check the permissions
    ls -l /dev/<device name>
    

strace

The strace command is useful debugging tool that can execute a program in order to intercept and record the system calls that it performs. The system call name, the arguments passed, and the resulting return value are all visible, which makes it a valuable tool for solving runtime issues. For example, you can utilize strace on your user-space application in order to view the communication between the user-space program and the kernel module.

GPIOs and the Kernel

GPIOs can be controlled from Linux user space using the GPIO Sysfs Interface (using a Linux shell directly or from within an executable), which enables you to activate a GPIO and set its state. For example, to activate the LED on header P9 Pin23/GPIO_17 using sysfs and turn the LED on/off, you can perform the following steps (as superuser):

 root@beaglebone:/sys/class/gpio# ls
 export	gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
 root@beaglebone:/sys/class/gpio# echo 49 > export
 root@beaglebone:/sys/class/gpio# ls
 export	gpio49	gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
 root@beaglebone:/sys/class/gpio# cd gpio49
 root@beaglebone:/sys/class/gpio/gpio49# ls
 active_low  direction  edge  power  subsystem  uevent  value
 root@beaglebone:/sys/class/gpio/gpio49# echo out > direction
 root@beaglebone:/sys/class/gpio/gpio49# echo 1 > value
 root@beaglebone:/sys/class/gpio/gpio49# echo 0 > value
 root@beaglebone:/sys/class/gpio/gpio49# cd ..
 root@beaglebone:/sys/class/gpio# echo 49 > unexport
 root@beaglebone:/sys/class/gpio# ls
 export	gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport

Clone this wiki locally