前回のLinuxデバイスドライバ開発 デバイスからの続き。
キャラクタ型デバイスとして、/dev からアクセスできるように幾つかのシステムコールを追加する。
今回は空実装として、open,release,read,writeを用意する。
#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.hh> /* cdev_init(), cdev_add(), cdev_del() */
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 SKEL_DRV_NAME "skel_drv"
static int skel_open(struct inode *inode, struct file *file)
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static int skel_release(struct inode *inode, struct file *file)
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = skel_open,
.release = skel_release,
.read = skel_read,
.write = skel_write,
};
static int skel_init(void)
{
dev_t dev = 0;
int ret;
pr_info("%s\n", __FUNCTION__);
if (drv_major) {
/* 指定デバイス番号を登録する */
dev = MKDEV(drv_major, drv_minor);
ret = register_chrdev_region(dev, drv_nr_devs, SKEL_DRV_NAME);
}
else {
/* デバイス番号を動的に確保する */
ret = alloc_chrdev_region(&dev, drv_minor, drv_nr_devs, SKEL_DRV_NAME);
drv_major = MAJOR(dev);
}
if (ret < 0) {
pr_err("skel_drv: cant't get major %d\n", drv_major);
}
else {
pr_info("skel_drv: char driver major number is %d\n", drv_major);
}
/* デバイスクラスを作成する */
skel_drv_class = class_create(THIS_MODULE, SKEL_DRV_NAME);
if (IS_ERR(skel_drv_class)) {
pr_err("skel_drv: class_create failed\n");
goto unregister_region;
}
/* デバイスを作成する */
skel_drv_device = device_create(skel_drv_class, NULL, MKDEV(drv_major, drv_minor), NULL, "skel_drv%d", drv_minor);
if (IS_ERR(skel_drv_device)) {
pr_err("skel_drv: device_create failed\n");
goto destroy_class;
}
/* キャラクタデバイスの初期化 */
cdev_init(&skel_cdev, &fops);
skel_cdev.owner = THIS_MODULE;
/* キャラクタデバイスの追加 */
ret = cdev_add(&skel_cdev, MKDEV(drv_major, drv_minor), drv_nr_devs);
if (ret < 0) {
pr_err("skel_drv: cdev_add failed\n");
goto destroy_device;
}
else {
goto exit;
}
destroy_device:
device_destroy(skel_drv_class, MKDEV(drv_major, drv_minor));
destroy_class:
class_unregister(skel_drv_class);
class_destroy(skel_drv_class);
unregister_region:
unregister_chrdev_region(dev, drv_nr_devs);
exit:
return 0;
}
static void skel_exit(void)
{
dev_t dev = 0;
pr_info("%s\n", __FUNCTION__);
/* キャラクタデバイスの削除 */
cdev_del(&skel_cdev);
/* デバイスを破棄する */
device_destroy(skel_drv_class, MKDEV(drv_major, drv_minor));
/* デバイスクラスを破棄する */
class_unregister(skel_drv_class);
class_destroy(skel_drv_class);
/* デバイス番号の登録を解除する */
dev = MKDEV(drv_major, drv_minor);
unregister_chrdev_region(dev, drv_nr_devs);
}
module_init(skel_init);
module_exit(skel_exit);
実行確認
$ sudo insmod skel_drv.ko
$ cat /proc/devices | grep skel
246 skel_drv
$ ls -lR /sys/class/ | grep skel
drwxr-xr-x 2 root root 0 2月 22 15:31 skel_drv
/sys/class/skel_drv:
lrwxrwxrwx 1 root root 0 2月 22 15:31 skel_drv0 -> ../../devices/virtual/skel_drv/skel_drv0
$ ls -l /dev | grep skel
crw------- 1 root root 246, 0 2月 22 15:31 skel_drv0
$ sudo cat /dev/skel_drv0
$ sudo rmmod skel_drv
$ dmesg | tail
:
[ 4522.460131] skel_init
[ 4522.460133] skel_drv: char driver major number is 246
[ 4597.951226] skel_open
[ 4597.951233] skel_read
[ 4597.951239] skel_release
[ 4604.985681] skel_exit
このページは言多の備忘録を元に再構成しています。