insmod dev_led.ko 之后cat /proc/devices 里有100ask_led 但ls /dev/里没有 myled

insmod dev_led.ko 之后cat /proc/devices 里有100ask_led 但ls /dev/里没有 myled 可能是什么原因?

把 dev_led.c 的代码贴出来看看;

#include <linux/module.h>

#include <linux/fs.h>

#include <linux/errno.h>

#include <linux/miscdevice.h>

#include <linux/kernel.h>

#include <linux/major.h>

#include <linux/mutex.h>

#include <linux/proc_fs.h>

#include <linux/seq_file.h>

#include <linux/stat.h>

#include <linux/init.h>

#include <linux/device.h>

#include <linux/tty.h>

#include <linux/kmod.h>

#include <linux/gfp.h>

#include <asm/io.h>

//#define FUN_INFO_LOG() printk("%s %s ",FILE,LINE)

/确定主设备号 major device number/

static int major =0;

static struct class *led_class;

static volatile unsigned int *SNVS_TAMPER3;

static volatile unsigned int *gpio5_GDIR;

static volatile unsigned int *gpio5_DR;

//#define GPIO5_ROOT_ARRD 0x02290000

//#define GPIO5_3 (GPIO5_ROOT_ARRD+0X14)

static int led_dev_open (struct inode *node, struct file *file)

{

//enable pin

//config pin as gpio

//config gpio open

*SNVS_TAMPER3 &=~0x0f;

*SNVS_TAMPER3 |=0x05; //配置为gpio

*gpio5_GDIR |= (1<<3);



return 0;

}

static ssize_t led_dev_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)

{

char value =0;

if(copy_from_user(&value,buf,1))

{

    return -EFAULT;

}

if(value)

{

    *gpio5_DR &= ~(1<<3); //set led on;

}

else

{

     *gpio5_DR |= (1<<3);

    //set led off;

}

return 1;

}

static struct file_operations ledfops = {

.owner      = THIS_MODULE,

.write      = led_dev_write,

.open       = led_dev_open,

};

static int __init led_init(void)

{

// FUN_INFO_LOG();

register_chrdev(0, "100ask_led",&ledfops);

//ioremap



SNVS_TAMPER3 = ioremap(0x02290000+0x14,4);



gpio5_GDIR = ioremap(0x020AC004+0x14,4);



gpio5_DR = ioremap(0x020AC000+0x14,4);

//class 让内核分配设备号-》 /dev/myled

led_class = class_create(THIS_MODULE, "myled");

if (IS_ERR(led_class))

{

    unregister_chrdev(major,  "100ask_led");

    printk("classerr*******\n");

    return PTR_ERR(led_class);

}

   

device_create(led_class,NULL,MKDEV(major,0),NULL, "myled");

printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

return 0;

}

static void __exit led_exit(void)

{

  iounmap(SNVS_TAMPER3);

iounmap(gpio5_GDIR);

iounmap(gpio5_DR);

 device_destroy(led_class,MKDEV(major,0));

class_destroy(led_class);

unregister_chrdev(major,  "100ask_led");

};

module_init(led_init);

module_exit(led_exit);

MODULE_LICENSE(“GPL”);

把
register_chrdev(0, "100ask_led",&ledfops); 
修改为
major = register_chrdev(0, "100ask_led",&ledfops);

原因就是没有主设备号 device_create 失败?我对这个只了解大概;
1 个赞

还真是,谢谢嘻嘻