Linuxデバイスドライバ開発 fops
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
* Linuxデバイスドライバ開発 fops [#n1fccd9a]
#seo(description,Linuxのデバイスドライバの書き方をメモ)
#seo(keywords,Linux, Device Driver)
** fops [#yeaeb126]
前回の[[Linuxデバイスドライバ開発 デバイス]]からの続き。~
キャラクタ型デバイスとして、/dev からアクセスできるように...
今回は空実装として、open,release,read,writeを用意する。
#highlight(c){{
#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(), all...
#include <linux/device.h> /* class_create(), class_unre...
#include <linux/cdev.h> /* cdev_init(), cdev_add(), cd...
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 *fi...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static int skel_release(struct inode *inode, struct file ...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_read(struct file *file, char __user *...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_write(struct file *file, const char _...
{
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...
}
else {
/* デバイス番号を動的に確保する */
ret = alloc_chrdev_region(&dev, drv_minor, drv_nr_devs,...
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", d...
}
/* デバイスクラスを作成する */
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, MK...
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), ...
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_mino...
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_mino...
/* デバイスクラスを破棄する */
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);
}}
#highlight(end)
実行確認
$ 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 -> ../....
$ ls -l /dev | grep skel
crw------- 1 root root 246, 0 2月 22 15:31 skel_d...
$ 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
#ref(skel_drv_part7.tgz,,ソースコード ダウンロード)
#br
#include(Linuxデバイスドライバ開発,notitle)
#br
#htmlinsert(amazon_book.html);
終了行:
* Linuxデバイスドライバ開発 fops [#n1fccd9a]
#seo(description,Linuxのデバイスドライバの書き方をメモ)
#seo(keywords,Linux, Device Driver)
** fops [#yeaeb126]
前回の[[Linuxデバイスドライバ開発 デバイス]]からの続き。~
キャラクタ型デバイスとして、/dev からアクセスできるように...
今回は空実装として、open,release,read,writeを用意する。
#highlight(c){{
#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(), all...
#include <linux/device.h> /* class_create(), class_unre...
#include <linux/cdev.h> /* cdev_init(), cdev_add(), cd...
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 *fi...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static int skel_release(struct inode *inode, struct file ...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_read(struct file *file, char __user *...
{
pr_info("%s\n", __FUNCTION__);
return 0;
}
static ssize_t skel_write(struct file *file, const char _...
{
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...
}
else {
/* デバイス番号を動的に確保する */
ret = alloc_chrdev_region(&dev, drv_minor, drv_nr_devs,...
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", d...
}
/* デバイスクラスを作成する */
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, MK...
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), ...
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_mino...
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_mino...
/* デバイスクラスを破棄する */
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);
}}
#highlight(end)
実行確認
$ 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 -> ../....
$ ls -l /dev | grep skel
crw------- 1 root root 246, 0 2月 22 15:31 skel_d...
$ 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
#ref(skel_drv_part7.tgz,,ソースコード ダウンロード)
#br
#include(Linuxデバイスドライバ開発,notitle)
#br
#htmlinsert(amazon_book.html);
ページ名: