2

The problem is "insmod success", but seems the entry function "demo_init()" is not been executed. lsmod shows the module loaded successfully and no errors or warnings. However, I can't see "Hello, World!" after dmesg, and there is no device node "/dev/demo_device" and there is no class "/sys/class/demo_class". I'm not able to figure it out. Someone could help me, thanks. The kernel version is 3.10.86. The system Android.

This is my code:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>

int major;

static struct class *demo_class;
static struct device *demo_device;

static int demo_open(struct inode * inode, struct file * filp)
{
    printk(KERN_ALERT "demo_open\n");
    return 0;
}
static int demo_write(struct file * file, const char __user * buffer, size_t count, loff_t * ppos)
{
    printk(KERN_ALERT "demo_write\n");
    return 0;
}

static const struct file_operations demo_fops = {
    .owner = THIS_MODULE,
    .open = demo_open,
    .write = demo_write,
};

static int __init demo_init(void) {

    printk(KERN_ALERT "Hello, World!\n");

    major = register_chrdev(0, "demo_drv", &demo_fops);
    demo_class = class_create(THIS_MODULE, "demo_class");
    demo_device = device_create(demo_class, NULL, MKDEV(major, 0), NULL, "demo_device");
    return 0;
}
static void __exit demo_exit(void) {

    printk(KERN_ALERT "Goodbye, World!\n");

    unregister_chrdev(major, "demo_drv");
    device_unregister(demo_device);
    class_destroy(demo_class);
}

module_init(demo_init);
module_exit(demo_exit);


MODULE_VERSION("0.01");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("A simple example Linux module.");
MrJu
  • 41
  • 1
  • 3
  • 1
    Your module code is correct (but return type of `demp_write()` should be `ssize_t` and `major` variable should be static). Can you provide more information: `logcat` output after attempt to load the module, `Makefile` content you used to built the module, last lines in `dmesg` after you tried to load the module, toolchain you used to build it, your board/architecture, `modinfo` output for your module on Android system? And most important, which kernel sources you built your module against? Are you sure it's the same that was used to build your Android kernel? – Sam Protsenko Apr 23 '18 at 23:52
  • Thank you Sam. The problem is solved.That is: – MrJu May 02 '18 at 03:57
  • 1
    The same kernel version, the same toolchains and the same modinfo information, BUT the kernel .config I used is not exactly the same with android's . I don't know which item affects, fortunately I've fixed the problem. The link is useful for the problem https://stackoverflow.com/questions/40148515/mips32-router-module-init-not-called-for-kernel-module – MrJu May 02 '18 at 04:04
  • 1
    The answer provided by Sam at https://stackoverflow.com/questions/40148515/mips32-router-module-init-not-called-for-kernel-module – MrJu May 02 '18 at 05:50

0 Answers0