Linuxデバイスドライバ開発 fops(2)†
fops†
前回のLinuxデバイスドライバ開発 デバイスからの続き。
前回作成した空実装に、struct fileを利用してオープンデバイス毎にデータを管理する実装を追加する。
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/types.h> /* dev_t */
- #include <linux/kdev_t.h> /* MKDEV(), MAJOR() */
- #include <linux/fs.h> /* register_chrdev_region(), alloc_chrdev_region(), unregister_chrdev() */
- #include <linux/device.h> /* class_create(), class_unregister(), class_destroy() */
- #include <linux/cdev.h> /* cdev_init(), cdev_add(), cdev_del() */
- #include <linux/slab.h> /* kmalloc(), kfree() */
- #include <linux/uaccess.h> /* copy_to_user(), copy_from_user() */
-
- MODULE_LICENSE("GPL v2");
-
- int drv_major = 0;
- int drv_minor = 0;
- int drv_nr_devs = 1;
- struct class *skel_drv_class = NULL;
- struct device *skel_drv_device = NULL;
- struct cdev skel_cdev;
-
- #define STR_LENGTH 16
-
- struct skel_drv_data {
- char str[STR_LENGTH];
- };
-
- #define SKEL_DRV_NAME "skel_drv"
-
- static int skel_open(struct inode *inode, struct file *file)
- {
- struct skel_drv_data *p = kmalloc(sizeof(struct skel_drv_data), GFP_KERNEL);
-
- pr_info("%s\n", __FUNCTION__);
-
-
- if (p == NULL) {
- pr_err("skel_drv: kmalloc\n");
- return -ENOMEM;
- }
-
-
- memset(p->str, 0, STR_LENGTH);
-
-
- file->private_data = p;
-
- return 0;
- }
-
- static int skel_release(struct inode *inode, struct file *file)
- {
- pr_info("%s\n", __FUNCTION__);
-
-
- if (file->private_data) {
- file->private_data = NULL;
- kfree(file->private_data);
- }
-
- return 0;
- }
-
- static ssize_t skel_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
- {
- struct skel_drv_data *p = file->private_data;
- int len;
-
- pr_info("%s\n", __FUNCTION__);
-
- len = strlen(p->str);
- if (count > len) {
- count = len;
- }
-
-
- if (copy_to_user(buf, p->str, count) != 0) {
- return -EFAULT;
- }
-
- return count;
- }
-
- static ssize_t skel_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
- {
- struct skel_drv_data *p = file->private_data;
-
- pr_info("%s\n", __FUNCTION__);
-
- if (count > STR_LENGTH) {
- return -EFAULT;
- }
-
-
- if (copy_from_user(p->str, buf, count) != 0) {
- return -EFAULT;
- }
-
- return count;
- }
-
- static const struct file_operations fops = {
- .owner = THIS_MODULE,
- .open = skel_open,
- .release = skel_release,
- .read = skel_read,
- .write = skel_write,
- };
-
- ~途中省略~
動作確認のサンプルコード
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
-
- int main(void)
- {
- int fd0, fd1;
- char buf0[16] = {0};
- char buf1[16] = {0};
-
- if ((fd0 = open("/dev/skel_drv0", O_RDWR)) < 0) {
- perror("open");
- }
-
- if ((fd1 = open("/dev/skel_drv0", O_RDWR)) < 0) {
- perror("open");
- }
-
- if ((read(fd0, buf0, 16)) < 0) {
- perror("read");
- }
- printf("fd0 - %s\n", buf0);
-
- if ((read(fd1, buf1, 16)) < 0) {
- perror("read");
- }
- printf("fd1 - %s\n", buf1);
-
- if ((write(fd0, "TEST", 4)) < 0) {
- perror("write");
- }
-
- if ((write(fd1, "ABC", 3)) < 0) {
- perror("write");
- }
-
- if ((read(fd0, buf0, 16)) < 0) {
- perror("read");
- }
- printf("fd0 - %s\n", buf0);
-
- if ((read(fd1, buf1, 16)) < 0) {
- perror("read");
- }
- printf("fd1 - %s\n", buf1);
-
- if ((read(fd0, buf0, 16)) < 0) {
- perror("read");
- }
- printf("fd0 - %s\n", buf0);
-
- close(fd0);
- close(fd1);
- }
実行確認
$ sudo insmod skel_drv.ko
$ ls -l /dev | grep skel
crw-rw-rw- 1 root root 246, 0 4月 15 10:04 skel_drv0
$ ./test
fd0 -
fd1 -
fd0 - TEST
fd1 - ABC
fd0 - TEST
$ sudo rmmod skel_drv
$ dmesg | tail -n15
:
[840878.286390] skel_init
[840878.286393] skel_drv: char driver major number is 246
[840897.801551] skel_open
[840897.801574] skel_open
[840897.801622] skel_read
[840897.801885] skel_read
[840897.801901] skel_write
[840897.801905] skel_write
[840897.801909] skel_read
[840897.801918] skel_read
[840897.801926] skel_read
[840897.801937] skel_release
[840897.801946] skel_release
[840907.857138] skel_exit
ソースコード ダウンロード

