Linux内核学习-字符设备驱动学习(二)

Linux内核学习-字符设备驱动学习(一)中编写字符设备驱动的一种方法,但是需要手动创建设备节点。

有没有能够自动的创建设备节点的呢?

有!使用class_create()和device_create()函数可以自动创建节点。

class_create : 创建class
class_destroy : 销毁class
class_device_create : 创建device
class_device_destroy : 销毁device

class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
class_create - create a struct class structure
@owner: pointer to the module that is to "own" this struct class
@name: pointer to a string for the name of this class.
在/sys/class/下创建类目录

class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_create(struct class *cls,
struct class_device *parent,
dev_t devt,
struct device *device,
const char *fmt, ...)

class_device_create - creates a class device and registers it with sysfs
@cls: pointer to the struct class that this device should be registered to.
@parent: pointer to the parent struct class_device of this new device, if any.
@devt: the dev_t for the char device to be added.
@device: a pointer to a struct device that is assiociated with this class device.
@fmt: string for the class device's name
在驱动模块初始化函数中实现设备节点的自动创建


设备驱动文件char01.c

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/cdev.h>
  4. #include <linux/device.h>
  5. #include <linux/kdev_t.h>
  6. #include <linux/fs.h>
  7. /
  8. int char01_open(struct inode *inode, struct file *filp)
  9. {
  10. printk("char01 open!/n");
  11. return 0;
  12. }
  13. int char01_release(struct inode *inode, struct file *filp)
  14. {
  15. printk("char01 release!/n");
  16. return 0;
  17. }
  18. ssize_t char01_read(struct file *filp, char *buf,
  19. size_t count, loff_t *fpos)
  20. {
  21. printk("char01 read!/n");
  22. return 0;
  23. }
  24. ssize_t char01_write(struct file *filp, const char *buf,
  25. size_t count, loff_t *fpos)
  26. {
  27. printk("char01 write!/n");
  28. return 0;
  29. }
  30. int char01_ioctl(struct inode *inode, struct file *filp,
  31. unsigned int cmd, unsigned long param)
  32. {
  33. printk("char01 ioctl!/n");
  34. printk("cmd:%d param:%ld/n", cmd, param);
  35. return 0;
  36. }
  37. /
  38. struct file_operations fops =
  39. {
  40. .owner = THIS_MODULE,
  41. .open = char01_open,
  42. .release = char01_release,
  43. .read = char01_read,
  44. .write = char01_write,
  45. .ioctl = char01_ioctl
  46. };
  47. dev_t devno;
  48. struct class *pclass;
  49. struct cdev dev;
  50. int char01_dev_init(void)
  51. {
  52. int result;
  53. result = alloc_chrdev_region(&devno, 0, 1, "char01");
  54. if (result != 0)
  55. {
  56. printk("alloc_chrdev_region failed!/n");
  57. goto ERR1;
  58. }
  59. cdev_init(&dev, &fops);
  60. dev.owner = THIS_MODULE;
  61. dev.ops = &fops;
  62. result = cdev_add(&dev, devno, 1);
  63. if (result != 0)
  64. {
  65. printk("cdev_add failed!/n");
  66. goto ERR2;
  67. }
  68. pclass = class_create(THIS_MODULE, "char01");
  69. if (IS_ERR(pclass))
  70. {
  71. printk("class_create failed!/n");
  72. goto ERR3;
  73. }
  74. device_create(pclass, NULL, devno, NULL, "char01");
  75. return 0;
  76. ERR3:
  77. cdev_del(&dev);
  78. ERR2:
  79. unregister_chrdev_region(devno, 1);
  80. ERR1:
  81. return -1;
  82. }
  83. /
  84. /
  85. /
  86. static int __init char01_init(void)
  87. {
  88. printk("module char01 init!/n");
  89. return char01_dev_init();
  90. }
  91. static void __exit char01_exit(void)
  92. {
  93. printk("module char01 exit!/n");
  94. class_destroy(pclass);
  95. device_destroy(pclass,devno);
  96. cdev_del(&dev);
  97. unregister_chrdev_region(devno, 1);
  98. }
  99. /
  100. MODULE_LICENSE("Dual BSD/GPL");
  101. MODULE_AUTHOR("Yao.GUET");
  102. module_init(char01_init);
  103. module_exit(char01_exit);
  104. /

测试文件char01_test.c

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <errno.h>
  4. #include <sys/stat.h>
  5. /
  6. int main(int argc, char *argv)
  7. {
  8. int fd;
  9. fd = open("/dev/char01", O_RDWR);
  10. if (fd<0)
  11. {
  12. printf("open /dev/char01 failed!/n");
  13. printf("%s/n", strerror(errno));
  14. return -1;
  15. }
  16. printf("open /dev/char01 ok!/n");
  17. ioctl(fd, 0);
  18. close(fd);
  19. }

Makefile文件:

  1. obj-m := char01.o
  2. PWD := $(shell pwd)
  3. K_DIR := /lib/modules/$(shell uname -r)/build
  4. all:
  5. $(MAKE) -C $(K_DIR) M=$(PWD) modules
  6. clean:
  7. $(MAKE) -C $(K_DIR) M=$(PWD) clean
  8. test:char01_test.o
  9. gcc -o $@ $^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值