I refactored the code for my Hello World device driver, and now the device will not register.
The module is listed as expected in /proc/modules
after a sudo make load
.
However, the device itself is no longer listed in /proc/devices
, nor are any of the printk()
messages logged in dmesg
.
I suspect the refactoring has caused issues with the Makefile, but I can't seem to get it right.
# Determines the name listed in /proc/modules.TARGET_MODULE := hello_kernel_moduleifeq ($(KERNELRELEASE),) KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.mod *.order *.symversload: insmod ./$(TARGET_MODULE).kounload: rmmod ./$(TARGET_MODULE).ko.PHONY: modules modules_install cleanelse # called from kernel build system: just declare what our modules are $(TARGET_MODULE)-objs := hello_kernel_module.o hello_fops.o obj-m := hello_kernel_module.o hello_fops.oendif
Any help would be great! Can provide any/all other source files upon request.
Edit: Here's the .c file with the init and exit :)
#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/uaccess.h>#include "hello_header.h"MODULE_LICENSE("Dual BSD/GPL");// This global number should be automatically assigned to the device driver by the kernel.static int device_file_major_number = 0;// This determines the name listed in /proc/devicesstatic const char device_name[] = "hello-device"; static int register_device(void) { int result = 0; printk(KERN_NOTICE "hello-device: register_device() is called.\n"); // Passing 0 as arg0 to register_chrdev() tells the kernel to automically assign a major number. result = register_chrdev(0, device_name, &fops); if(result < 0) { printk(KERN_WARNING "hello-device: unable to register character device. Error code: %i\n", result); return result; } device_file_major_number = result; printk(KERN_NOTICE "hello-device: successfully registered character device with major number = %i and minor numbers 0...255\n", device_file_major_number); return 0;}void unregister_device(void) { printk(KERN_NOTICE "hello-device: unregister_device() is called\n"); if(device_file_major_number != 0) { unregister_chrdev(device_file_major_number, device_name); }}// This code will actually run right in the kernel! Hence the use of printk() -- we're not in userspace anymore.module_init(register_device);module_exit(unregister_device);