Question

I have written a user defined function in my device driver and I want to call it from user space application program. How do I achieve this?

What I mean by user defined function is, any function other than the kernel defined functions. Pointers to which is defined in struct file_operations, as below.

struct file_operations {
       struct module *owner;
       loff_t (*llseek) (struct file *, loff_t, int);
       ssize_t (*read) (struct file *, char *, size_t, loff_t *);
       ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
       int (*readdir) (struct file *, void *, filldir_t);
       unsigned int (*poll) (struct file *, struct poll_table_struct *);
       int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
       int (*mmap) (struct file *, struct vm_area_struct *);
       int (*open) (struct inode *, struct file *);
       int (*flush) (struct file *);
       int (*release) (struct inode *, struct file *);
       int (*fsync) (struct file *, struct dentry *, int datasync);
       int (*fasync) (int, struct file *, int);
       int (*lock) (struct file *, int, struct file_lock *);
         ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
         ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
    };

For eg. in my driver I've the following,

struct file_operations fops = {
.read = my_read,
.write = my_write,
};

and I can call these functions from user space application program using the calls read and write.

I also have a function named user_defined in kernel source and my question is how do I call this from user space program?

Was it helpful?

Solution

Well, what you have there is normally referred to as "syscall". I suggest you read into the kernel developer documentation on how to expose a new syscall. On the userspace side, the libc provides you a function syscall, that you can use to call into kernel space. But normally you write a wrapper around it.

However introducing new syscalls should be avoided. The preferred way to call into kernel space is to use sysfs and expose the user space callable functions as files therein.

OTHER TIPS

The quicker & easiest way is to write an ioctl() & call that ioctl() from userspace application. Writing a system call will also do but we should avoid writing a new system call & it is time consuming also. For writing ioctl you can refer following links:

  1. TPLD

  2. LJ

  3. LDD3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top