cdev_alloc()函数相当于 structcdevcdev;cdev_init($cdev,&hello_ops)
/*** cdev_init() - initialize a cdev structure* @cdev: the structuretoinitialize* @fops: the file_operationsforthis device** Initializes @cdev, remembering @fops, making it readytoaddtothe* systemwithcdev_add().*/void cdev_init(struct cdev *cdev, const struct file_operations *fops){mems...
1. cdev_init原型 voidcdev_init(struct cdev *cdev,conststruct file_operations *fops) 功能 用于初始化cdev结构体,并填充其成员ops 参数 cdev:字符设备 fops :驱动操作函数集合 返回值 无 该函数实现如下: /** * cdev_init - initialize a cdev structure * @cdev: the structure to initialize * @fops:...
cdev->ops =fops; } 通过cdev_init的代码可以看出,主要是对空间起到一个清零作用并较之cdev_alloc多了一个ops的赋值操作。 这里需要注意的是kzalloc后的空间是不需要再执行memset的,因为它本身就包含了这个操作。而memset一般作用在已经存在的空间上。 由此基本上对这两个函数有了一个基本的概念,cdev_alloc函数针...
cdev_init:将struct cdev类型的结构体变量和file_operations结构体进行绑定的 cdev_add:向内核里面添加一个驱动,注册驱动 cdev_del:从内核中注销掉一个驱动。注销驱动 设备号 (1)dev_t类型(包括了主设备号和次设备号 不同的内核中定义不一样有的是16位次设备号和16位主设备号构成 有的是20为次设备号12位主...
kobject_init(&cdev->kobj, &ktype_cdev_default); cdev->ops = fops; } 由此可见,两个函数完成都功能基本一致,只是 cdev_init() 还多赋了一个 cdev->ops 的值。 初始化 cdev 后,需要把它添加到系统中去。为此可以调用 cdev_add() 函数。传入 cdev 结构的指针,起始设备编号,以及设备编号范围。
cdev_init(cdev *, fops):将struct cdev类型的结构体变量和file_operations结构体进行绑定。但若前面使用了cdev_alloc,则就可以直接利用pcdev->ops = fops;来进行绑定,就不需要cdev_init函数了。 在cdev_init函数中,除了cdev->ops = fops;之外的其他的操作都在cdev_alloc函数中做了。
static int __init cdev_alloc_init (void) { printk("into the cdev_alloc_init\n"); mem_cdev = cdev_alloc(); //调用函数动态分配字符设备 if (mem_cdev == NULL) //检测函数调用成功与否 { printk("cdev_alloc failed! \n"); return -1; } /*显示设备地址空间*/ printk("cdev_alloc ...
- cdev_init(&port->cdev, &port_fops); + port->cdev = cdev_alloc(); + if (!port->cdev) { + dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n"); + err = -ENOMEM; + goto free_port; + } + port->cdev->ops = &port_fops; ...
printk("hello_init \n"); devno = MKDEV(major,minor); result = register_chrdev_region(devno,1,"test"); if(result<0) { printk("register_chrdev_region fail \n"); returnresult; } cdev_init(&cdev,&hello_ops); error = cdev_add(&cdev,devno,1); ...