Linux内核学习笔记 -48 动手实践-编写字符设备驱动程序

在linux内核中,字符设备是由dev结构体来描述的,它位于/include/linux/cdev.h中

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_CDEV_H
#define _LINUX_CDEV_H

#include <linux/kobject.h>
#include <linux/kdev_t.h>
#include <linux/list.h>
#include <linux/device.h>

struct file_operations;
struct inode;
struct module;

struct cdev {
	struct kobject kobj;
	struct module *owner;
	const struct file_operations *ops;
	struct list_head list;   //list字段来将所有的字符设备组织成一个链表。每个设备由主设备号与次设备号确定,
	dev_t dev;      //dev就是字符设备的设备号,包括主设备号和次设备号
	unsigned int count;   //count字段是同一个主设备号中次设备号的个数
} __randomize_layout;

void cdev_init(struct cdev *, const struct file_operations *);

struct cdev *cdev_alloc(void);

void cdev_put(struct cdev *p);

int cdev_add(struct cdev *, dev_t, unsigned);

void cdev_set_parent(struct cdev *p, struct kobject *kobj);
int cdev_device_add(struct cdev *cdev, struct device *dev);
void cdev_device_del(struct cdev *cdev, struct device *dev

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/108135086