Driver fail

I have initially written a virtual device driver, and after the driver is successfully loaded, an error will be reported when opening it, fd = open(filename, O_RDWR); fd = -1. Could you please tell me what is wrong with my driver code?

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>


#define HELLO_MAJOR 504 /* 主设备号,若为0,则会自动分配*/
#define HELLO_NAME "hello_2" /* 设备名 */



static char readbuf[100]; //读缓存区
static char writebuf[100]; //写缓存区
static char kerneldata[] = {"kernel data!"}; //

static int hello_drv_open(struct inode *inode, struct file *filp)
{
    // printk("%s:%s line %d\n", __FILE__, __FUNCTION__, __LINE__);//打印调试信息
    return 0;
}

static ssize_t hello_drv_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue = 0;
    // printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);//打印调试信息

    memcpy(readbuf, kerneldata, sizeof(kerneldata));
    retvalue = copy_to_user(buf, readbuf, cnt);
    if(retvalue ==0 ){
        printk("kernel senddata is ok!\r\n");
    } else{
        printk("kernel senddata failed! \r\n");
    }

    return 0;
}

static ssize_t hello_drv_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue = 0;
    // printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);//打印调试信息

    /* 接收用户空间传递给内核的数据b并且打印出来 */
    retvalue = copy_from_user(writebuf, buf, cnt);

    if(retvalue ==0 ){
        printk("kernel recvdata is %s!\r\n",writebuf);
    } else{
        printk("kernel recvdata failed! \r\n");
    }

    return 0;
}


static int hello_drv_release(struct inode *inode, struct file *filp)
{
    return 0;
}


//设备操作函数结构体
static struct file_operations hello_drv_fops=
{
    .owner = THIS_MODULE,
    .open = hello_drv_open, //对于函数来说,函数名和&函数名都标识函数地址
    .read = hello_drv_read,
    .write = hello_drv_write,
    .release = hello_drv_release,
};

static int __init hello_drv_init(void)
{
    int retvalue, err;
    


    retvalue = register_chrdev(HELLO_MAJOR, HELLO_NAME, &hello_drv_fops);
   


    if(retvalue < 0){
        printk("hello driver register failed\r\n");
    }

    printk("hello_drv_init()\r\n");

    return 0;
}


static void __exit hello_drv_exit(void)
{
    unregister_chrdev(HELLO_MAJOR, HELLO_NAME);

    printk("hello_drv_exit()\r\n");


}

//将上述2个函数z指定为驱动的入口和出口函数
module_init(hello_drv_init);
module_exit(hello_drv_exit);

//LICENSE和作者信息
MODULE_LICENSE("GPL");
MODULE_AUTHOR("WXY");
MODULE_INFO(intree,"Y");





Hi,
The issue is more about Linux kernel driver programming. We would suggest go to Linux community to ask for help. Or see if other forum users can share experience.

For Xavier we have Jetpack 4 and 5 releases. Jetpack 4 is with Kernel 4.9 and 5 is with K5.10.

I also cannot answer, but I’ll suggest a tool that might offer clues (you’ll still need to go to a Linux community forum, but this offers some insight): strace
(you might need to “sudo apt-get install strace”)

I am assuming you see a failure from a user space program trying to make use of your module which has already successfully loaded.

Whenever a program runs it will make calls either to a library, or to the kernel itself. You won’t care about the ltrace (library trace) tool. However, any program which talks to a kernel feature uses a “system call” (or syscall). Those are more or less standardized mechanisms for an API. The calls look very much like a C function call (which is in fact the origin of system calls and the C language itself).

Rather than explaining it all, start by looking at this output:

sudo -s
strace echo testing
exit

I’m using sudo there because not everyone is allowed to monitor system calls. There are a lot of options in how to use strace, e.g., redirecting to a log, following forks, so on. The above just mixes the system call information in with the actual command output. If your program is called “testing”, and you want a log, then it might be something like:

sudo -s
strace -oLogTesting.txt testing
exit

(then you’d examine file “LogTesting.txt”)

Other options exist that let you strace as root/sudo while running the program as a regular user (and incidentally, you might see if your program fails the same way when run as root/sudo versus as a regular user).

"Most of the system calls have a return value (== 0) if successful. Some have a negative value for various failures. Others provide a NULL-terminated text string.

A lot of the output of strace is completely unrelated. An example is that Linux can deal with many languages and the associated “locale” of the language (character sets). You’ll probably see a large number of log lines searching for lots of character sets you don’t use, followed by a negative return value; then when it gets to your locale, the trace line will show success. There is a lot of wading through irrelevant messages.

However, you might see something towards the end of the log that tells you things like lacking permission or depending on something else which isn’t working. You will need to get to a kernel programming forum, which applies to Jetsons (Jetsons do not differ in how a kernel works), but you can use strace to get an idea of what the kernel itself thinks the problem is.

@linuxdev
thanks very much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.