Re: Cannot open from user space a misc device - why ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 23, 2009 at 3:11 AM, Kevin Wilson <wkevils@xxxxxxxxx> wrote:
> Hello,
>
>  Following is a simple kernel module of 40 lines which only registers a
> misc device. I insmod the module and it is ok; the return value of
> misc_register() is 0.
> ls -al /dev/mymodule
> gives:
> crw-rw---- 1 root root 10, 300 22-02-09 20:58 /dev/mymodule
> which is ok.
> However, when I try, from a user space program, to open the device,
> by calling open("/dev/mymodule", O_RDONLY) , I get an error.
> The error I get when running the user space program is:
> open: No such device or address
>
> I would appreciate if anyone has an idea why is it so. The kernel
> module and the user space
> program are below.
>
> kernel module:
> #define MY_MINOR 300
>
> static long my_ioctl(struct file *filp, unsigned int cmd, unsigned long data)
>  {
>  return 0;
>  }
>
>  struct file_operations mymodule_fops =
>  {
>        .owner   = THIS_MODULE,
>        .unlocked_ioctl = my_ioctl,
>        .compat_ioctl   = my_ioctl,
>  };

it seemed your file_operations may be correct (without the traditional
open()) interface, as I noticed that drivers/char/toshiba.c is also
declared thus:

static const struct file_operations tosh_fops = {
        .owner          = THIS_MODULE,
        .ioctl          = tosh_ioctl,
};

But then it also puzzled me, because ioctl() in user space required a
open file descriptor as the first parameter, which is derived from
open() (as specified from "man ioctl").   so then how is the open()
from userspace passed down to kernel?

>
> static struct miscdevice my_dev = {
>        MY_MINOR,
>        "mymodule",
>        &mymodule_fops,
> };
>
> static int __init mymodule_init(void)
>  {
>        int ret;
>        mymodule_fops.owner = THIS_MODULE;
>        ret = misc_register(&my_dev);
>  printk("ret = %d\n",ret);
>        if (ret)
>         return -1;
>        return 0;
>  }
>
>  static void __exit mymodule_exit(void)
>  {
>         misc_deregister(&my_dev);
>  }
> module_init(mymodule_init)
> module_exit(mymodule_exit)
> -----------------
>
>
> The user space program:
>
> int main()
> {
>  int fd;
>
>  fd = open("/dev/mymodule", O_RDONLY);
>        if (fd == -1) {
>                perror("open");
>                exit(1);
>        }
>  printf("open succeeded\n");
>
> }
>

putting aside my open doubts....i think the problem u have is got to
do with udev not setup properly:

search in google:

http://www.google.com/search?q=%22open%3A+No+such+device+or+address%22+udev&btnG=Google+Search

or more specifically (which started with your error):

http://www.ip-phone-forum.de/showthread.php?p=695114

I certainly don't understand any German....but from "You made my
day!", i know it is happy ending.

-- 
Regards,
Peter Teoh

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux