1、先清除前面安装过的驱动
depmod 先清除前面安装过的驱动
2、 lsmod 查看加载成功的驱动。
modprobe .ko 安装.ko 文件 。
3、
ls /dev
3、查看设备号
cat /proc/devices
4、设备名字 200 主设备号 0 次设备号。
mknod /dev/newchrled C 200 0 /dev/newchrled
5、查看创建成功的设备节点。
ls /dev/newchrled -l
6、
./ledAPP /dev/newchrled 1
7、
./ledAPP /dev/newchrled 0
8、卸载模块
rmmod .ko
1、查看创建的设备节点
cd /proc/device-tree/
2、ls
/* newchrled设备结构体 */
struct newchrled_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};
/* 设备操作函数 */
static struct file_operations newchrled_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};
if (newchrled.major) { /* 定义了设备号 */
newchrled.devid = MKDEV(newchrled.major, 0);
register_chrdev_region(newchrled.devid, NEWCHRLED_CNT, NEWCHRLED_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CNT, NEWCHRLED_NAME); /* 申请设备号 */
newchrled.major = MAJOR(newchrled.devid); /* 获取分配号的主设备号 */
newchrled.minor = MINOR(newchrled.devid); /* 获取分配号的次设备号 */
}
newchrled.cdev.owner = THIS_MODULE;
cdev_init(&newchrled.cdev, &newchrled_fops);
cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CNT);
newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
if (IS_ERR(newchrled.class)) {
return PTR_ERR(newchrled.class);
}
newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, NEWCHRLED_NAME);
if (IS_ERR(newchrled.device)) {
return PTR_ERR(newchrled.device);
}
/* 注销字符设备驱动 */
cdev_del(&newchrled.cdev);/* 删除cdev */
unregister_chrdev_region(newchrled.devid, NEWCHRLED_CNT); /* 注销设备号 */
device_destroy(newchrled.class, newchrled.devid);
class_destroy(newchrled.class);
1、
MKDEV();
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
MKDEV(keydev.major,0);
2、 register_chrdev_region();
extern int register_chrdev_region(dev_t, unsigned, const char *);
ret = register_chrdev_region(keydev.devid,KEY_CNT,KEY_NAME);
3、alloc_chrdev_region();
extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
ret = alloc_chrdev_region(&keydev.devid,0,KEY_CNT,KEY_NAME);
4、 MAJOR();
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
keydev.major = MAJOR(keydev.devid);
5、 MINOR();
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
keydev.minor = MINOR(keydev.devid);
1、
keydev.cdev.owner = THIS_MODULE;
2、 cdev_init();
void cdev_init(struct cdev *, const struct file_operations *);
cdev_init(&keydev.cdev,&keydev.fops);
1、 cdev_add();
int cdev_add(struct cdev *, dev_t, unsigned);
ret = cdev_add(&keydev.cdev,keydev.devid,KEY_CNT);
2、 unregister_chrdev_region(); // 注销
extern void unregister_chrdev_region(dev_t, unsigned);
unregister_chrdev_region(key.devid, KEY_CNT);
1、class_create();
#define class_create(owner, name)
keydev.class = class_create(THIS_MODULE,KEY_NAME);
2、class_destroy(keydev.class);
1、
device_create();
struct device *device_create(struct class *cls, struct device *parent,
dev_t devt, void *drvdata,
const char *fmt, ...);
keydev.device = device_create(keydev.class,NULL,keydev.devid,NULL,KEY_NAME);
2、
cdev_del(&keydev.cdev);
sudo cp arch/arm/boot/dts/imx6ull-alientek-emmc.dtb /home/jk9/linux/tftpboot/ -f
reboot 重启系统