Linux驱动 | cdev_init、cdev_alloc的区别

系统 Linux
外设有MPU6050、LED两个设备,他们通过外设电路连接到SOC的对应的引脚上。程序要操作外设,就要通过设置soc中对应的SFR来与外设交互。

[[424054]]

这两个函数是字符设备初始化相关的内核函数。

要想了解这两个函数,必须要知道字符设备的架构,以及字符设备创建的流程。

关于字符设备可以参考下面这篇文章 《手把手教Linux驱动3-之字符设备架构详解,有这篇就够了》

一、字符设备架构

下面我们以两个设备:LED、MPU6050为例来讲解字符设备的架构

由上图所示:

1、硬件

外设有MPU6050、LED两个设备,他们通过外设电路连接到SOC的对应的引脚上。程序要操作外设,就要通过设置soc中对应的SFR来与外设交互。

2、驱动层

  • 每一个字符设备都必须首先定义一个结构体变量struct cdev,并注册到内核中
  • 所有的该变量在内核中会通过链表进程管理,其中成员list用于将所有链表串接起来
  • 用于操作外设的功能函数全部被封装在struct file_operations中,包括read、write等
  • 每一个字符设备都必须要有一个设备号,保存在成员dev中,
  • 主、次设备号只能被分配一次
  • 所有的字符设备号,都由数组chrdevs统一管理
  • chrdevs是一个指针数组,成员类型为**struct char_device_struct ***,下标与字符设备号有一定的对应关系,
  • **struct char_device_struct **中有成员:
  1. unsigned int major; 
  2. struct cdev *cdev;  

major : 是主设备号 cdev : 指向该字符设备号对应的cdev结构体

3、应用层、VFS层

  • 用户如果想操作硬件,必须调用内核中的struct file_operations中的操作函数,
  • 那么如何才能找到该结构体呢?必须要依赖文件节点来查找,可以通过以下命令来创建
  1. mknod  /dev/led c 250 0 
  2.  mknod 创建设备文件,可以使字符设备,也可以是块设备 
  3.  /dev/led 设备文件名 
  4.  c  字符设备 
  5.  250  主设备号 
  6.  0    次设备号 

字符设备文件属性中最重要的属性就是字符设备号,该设备号和chedevs的下标有一定对应关系

  • 通过mknod创建的文件,VFS层会分配一个结构体变量来维护该文件,类型为struct inode
  • 每新建1个文件内核都会创建不同的结构体变量与之对应
  • 应用程序要操作某个字符设备,那么必须先通过系统调用open()来打开该字符设备
  • 该函数会返回一个唯一的整型文件描述符,同时内核中会分配结构体变量,类型为struct file,并与文件描述符一一对应,该结构体维护在struct task_struct中
  • 每次打开某个文件,都会分配不同的文件描述符,所以需要用不同的变量来保存文件描述符

二、字符设备创建的流程

了解了架构之后,那么我们来看一下内核中完整的创建字符设备的流程及对应的函数调用关系:

如下图所示,字符设备的创建主要包括以下三个步骤:

  1. 申请设备号
  2. 初始化cdev
  3. 注册cdev 调用的函数见右侧

下面是一个最简单的额字符设备创建的实例

  1. /*   
  2.  *一口Linux 
  3.  *2021.6.21 
  4.  *version: 1.0.0 
  5. */ 
  6.  
  7. #include <linux/init.h> 
  8. #include <linux/module.h> 
  9. #include <linux/kdev_t.h> 
  10. #include <linux/fs.h> 
  11. #include <linux/cdev.h> 
  12.  
  13. static int major = 237; 
  14. static int minor = 0; 
  15. static dev_t devno; 
  16. static struct cdev cdev; 
  17. static int hello_open (struct inode *inode, struct file *filep) 
  18.  printk("hello_open()\n"); 
  19.  return 0; 
  20. static struct file_operations hello_ops =  
  21.  .open = hello_open, 
  22. }; 
  23. static int hello_init(void) 
  24.  int result; 
  25.  int error;  
  26.  printk("hello_init \n"); 
  27.  devno = MKDEV(major,minor);  
  28.  result = register_chrdev_region(devno, 1, "test"); 
  29.  if(result<0) 
  30.  { 
  31.   printk("register_chrdev_region fail \n"); 
  32.   return result; 
  33.  } 
  34.  cdev_init(&cdev,&hello_ops); 
  35.  error = cdev_add(&cdev,devno,1); 
  36.  if(error < 0) 
  37.  { 
  38.   printk("cdev_add fail \n"); 
  39.   unregister_chrdev_region(devno,1); 
  40.   return error; 
  41.  } 
  42.  return 0; 
  43. static void hello_exit(void) 
  44.  printk("hello_exit \n"); 
  45.  cdev_del(cdev); 
  46.  unregister_chrdev_region(devno,1); 
  47.  return
  48. module_init(hello_init); 
  49. module_exit(hello_exit); 

该实例代码主要功能:

  1. 申请了字符设备号237
  2. 初始化cdev,并注册了cdev

应用程序如果要想使用,还必须创建字符设备节点

  1. mknod /dev/test c 237 0 

这样应用程序就可以通过设备节点/dev/test 调用到对应的内核操作函数.open = hello_open,

  1. /*   
  2.  *一口Linux 
  3.  *2021.6.21 
  4.  *version: 1.0.0 
  5. */ 
  6.  
  7. #include <stdio.h> 
  8. #include <sys/types.h> 
  9. #include <sys/stat.h> 
  10. #include <fcntl.h> 
  11. main() 
  12.  int fd; 
  13.  fd = open("/dev/test",O_RDWR); 
  14.  if(fd<0) 
  15.  { 
  16.   perror("open fail \n"); 
  17.   return
  18.  } 
  19.  printf("open ok \n "); 

三、函数功能和定义

搞懂上面字符设备创建步骤之后,我们就可以来真正分析cdev_init、cdev_alloc这两个函数了

1. cdev_init()

  1. 原型 
  2. void cdev_init(struct cdev *cdev, const struct file_operations *fops) 
  3.  
  4. 功能 
  5.  用于初始化cdev结构体,并填充其成员ops 
  6. 参数 
  7.    cdev:字符设备 
  8.    fops :驱动操作函数集合 
  9. 返回值 
  10.   无 

该函数实现如下:

  1. /** 
  2.  * cdev_init() - initialize a cdev structure 
  3.  * @cdev: the structure to initialize 
  4.  * @fops: the file_operations for this device 
  5.  * 
  6.  * Initializes @cdev, remembering @fops, making it ready to add to the 
  7.  * system with cdev_add(). 
  8.  */ 
  9. void cdev_init(struct cdev *cdev, const struct file_operations *fops) 
  10.  memset(cdev, 0, sizeof *cdev); 
  11.  INIT_LIST_HEAD(&cdev->list); 
  12.  kobject_init(&cdev->kobj, &ktype_cdev_default); 
  13.  cdev->ops = fops; 

2. cdev_alloc

  1. 原型 
  2. struct cdev *cdev_alloc(void) 
  3.  
  4. 功能 
  5.  用于分配cdev结构体,并添加到内核中 
  6. 参数 
  7. 返回值 
  8.   成功:返回分配的cdev结构体变量指针 
  9.   失败: 返回NULL 

该函数实现如下:

  1. /** 
  2.  * cdev_alloc() - allocate a cdev structure 
  3.  * 
  4.  * Allocates and returns a cdev structure, or NULL on failure. 
  5.  */ 
  6. struct cdev *cdev_alloc(void) 
  7.  struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL); 
  8.  if (p) { 
  9.   INIT_LIST_HEAD(&p->list); 
  10.   kobject_init(&p->kobj, &ktype_cdev_dynamic); 
  11.  } 
  12.  return p; 

注意,该函数分配的cdev需要free掉 该函数没有初始化cdev->ops成员

四、cdev_alloc()的使用

该函数主要用于让用户省去操作cdev的操作,只需要提供**struct file_operations **变量就可以通过以下函数注册字符设备

  1. static inline int register_chrdev(unsigned int major, const char *name
  2.       const struct file_operations *fops) 
  3.  return __register_chrdev(major, 0, 256, name, fops); 

其中函数__register_chrdev()定义如下:

  1. /** 
  2.  * __register_chrdev() - create and register a cdev occupying a range of minors 
  3.  * @major: major device number or 0 for dynamic allocation 
  4.  * @baseminor: first of the requested range of minor numbers 
  5.  * @count: the number of minor numbers required 
  6.  * @namename of this range of devices 
  7.  * @fops: file operations associated with this devices 
  8.  * 
  9.  * If @major == 0 this functions will dynamically allocate a major and return 
  10.  * its number. 
  11.  * 
  12.  * If @major > 0 this function will attempt to reserve a device with the given 
  13.  * major number and will return zero on success. 
  14.  * 
  15.  * Returns a -ve errno on failure. 
  16.  * 
  17.  * The name of this device has nothing to do with the name of the device in 
  18.  * /dev. It only helps to keep track of the different owners of devices. If 
  19.  * your module name has only one type of devices it's ok to use e.g. the name 
  20.  * of the module here. 
  21.  */ 
  22. int __register_chrdev(unsigned int major, unsigned int baseminor, 
  23.         unsigned int count, const char *name
  24.         const struct file_operations *fops) 
  25.  struct char_device_struct *cd; 
  26.  struct cdev *cdev; 
  27.  int err = -ENOMEM; 
  28.  
  29.  cd = __register_chrdev_region(major, baseminor, countname); 
  30.  if (IS_ERR(cd)) 
  31.   return PTR_ERR(cd); 
  32.  
  33.  cdev = cdev_alloc(); 
  34.  if (!cdev) 
  35.   goto out2; 
  36.  
  37.  cdev->owner = fops->owner; 
  38.  cdev->ops = fops; 
  39.  kobject_set_name(&cdev->kobj, "%s"name); 
  40.  
  41.  err = cdev_add(cdev, MKDEV(cd->major, baseminor), count); 
  42.  if (err) 
  43.   goto out
  44.  
  45.  cd->cdev = cdev; 
  46.  
  47.  return major ? 0 : cd->major; 
  48. out
  49.  kobject_put(&cdev->kobj); 
  50. out2: 
  51.  kfree(__unregister_chrdev_region(cd->major, baseminor, count)); 
  52.  return err; 

可以看到该函数,复用了cdev_alloc()、cdev_add(),我们只需要提供以下3个参数即可:

  1. unsigned int major  主设备号 
  2. const char *name    设备号名字 
  3. const struct file_operations *fops 驱动操作函数集合 

五、结论

cdev_alloc()函数相当于

  1. struct cdev cdev; 
  2. cdev_init($cdev,&hello_ops) 

本文转载自微信公众号「一口Linux」

 

责任编辑:姜华 来源: 一口Linux
相关推荐

2013-07-25 13:15:55

iOS开发学习new与allocinit区别

2017-02-14 12:34:28

iOSAllocInit

2021-10-11 08:51:05

Linux console Linux 系统

2011-01-14 17:05:52

Linuxinit

2022-07-07 06:27:59

Python__init____new__

2021-03-11 12:23:13

Linux驱动开发

2012-05-28 15:49:06

Linux凯迪拉克

2010-03-03 09:16:17

2020-12-03 08:59:06

Linux设备驱动

2021-11-29 07:55:45

Linux GPIO Linux 系统

2011-04-11 13:26:25

Linux驱动

2015-07-20 10:00:28

Linux内核编码风格

2017-03-23 14:30:13

Linux内核驱动编码风格

2009-12-17 09:56:26

Linux添加驱动模块

2017-11-06 17:16:55

Linux设备驱动并发控制

2021-08-10 11:30:30

Linux代码中断控制器

2021-08-03 15:10:26

Linux代码驱动

2023-05-15 08:58:41

块设备驱动Linux

2009-12-03 10:12:24

LinuxUnix

2017-08-31 14:40:54

Linuxinit进程app启动
点赞
收藏

51CTO技术栈公众号