2009-08-15 09:28:08     a simple char dev driver but open error

Document created by Aaronwu Employee on Aug 16, 2013
Version 1Show Document
  • View in full screen mode

2009-08-15 09:28:08     a simple char dev driver but open error

xm ye (CHINA)

Message: 78922   

 

hi ,

 

i am  a newer ,i follow the documnet write a simple led.c,like this:

 

 

 

 

#include<linux/module.h>

#include<linux/kernel.h>

#include<linux/init.h>

#include<asm/io.h>

 

define  LED_DEV "led"

 

 

 

/* For dynamic major number */

 

static int led_major = 0;

 

static int led_open(struct inode *inode, struct file *file)

{

     

 

       writeb(0x00,0x20350000);

       writeb(0x20,0x20380000);

      printk(KERN_INFO LED_DEV "Open!\n");

 

        return 0;

}

 

 

struct file_operations led_driver_fops = {

    

       .owner    = THIS_MODULE,

 

      .open = led_open,

 

 

};

 

 

 

static int __init led_init(void)

{

 

  int ret;

    ret = register_chrdev(led_major, LED_DEV, &led_driver_fops);           

    if (ret < 0) {

        printk(KERN_NOTICE LED_DEV ": chrdev registration failed (ret=%i)\n", ret);

        return ret;

    }

 

    led_major = ret;

    printk(KERN_INFO LED_DEV ": register major %i\n", led_major);

 

    return 0;

 

}

 

 

module_init(led_init);

 

 

static void __exit led_exit(void)

{

  

int ret;

    ret = unregister_chrdev(led_major, LED_DEV);

    printk(KERN_INFO LED_DEV ": unregistered char device (ret=%i)\n", ret);

}

module_exit(led_exit);

 

MODULE_AUTHOR("XXX");

MODULE_DESCRIPTION("Simple led");

MODULE_LICENSE("GPL");

 

 

 

 

 

i load it in the board and   insmod  led.ko ,it display "led: register major 253 "

 

and also have creat a node in the /dev/led0,but i run test application to use

 

open("/dev/led0",ORDWR),it can't open it, why??  please give me some help.

 

thanks!

QuoteReplyEditDelete

 

 

2009-08-15 16:29:31     Re: a simple char dev driver but open error

Mike Frysinger (UNITED STATES)

Message: 78923   

 

please start with the documentation:

  docs.blackfin.uclinux.org/doku.php?id=uclinux-dist:dev-management#troubleshooting

QuoteReplyEditDelete

 

 

2009-08-15 23:27:51     Re: a simple char dev driver but open error

xm ye (CHINA)

Message: 78925   

 

please start with the documentation:

  docs.blackfin.uclinux.org/doku.php?id=uclinux-dist:dev-management#troubleshooting

 

---

 

thank you for your rely ,i have looked the doc. and it says:

 

"If you get errors such as

no such device

 

whenever you open a device node, this is usually because the major number of the device node is not actually registered with the kernel. Creating device nodes because it was not created automatically is a common mistake. Doing this rarely (if ever) fixes the problem. The correct answer is to check and fix your kernel device driver"

 

 

 

i am very sorry i don't uderstand the mean. it is the problem that  register  way of major num in the above code?? i use the uclinux dist 2008r1.5, it is dynamic way for register the major num.so in above code it    #define led_major=0;

QuoteReplyEditDelete

 

 

2009-08-18 02:36:01     Re: a simple char dev driver but open error

Yi Li (CHINA)

Message: 78970   

 

You may refer to the book Linux Device Driver:   lwn.net/Kernel/LDD3/, on how to create a char device driver.

 

-Yi

Attachments

    Outcomes