Quantcast
Channel: ウィリアムのいたずらの、まちあるき、たべあるき
Viewing all articles
Browse latest Browse all 7271

Linuxデバイスドライバの作りかた(2.6以降) その1

$
0
0
Linuxのデバイスドライバの作りかたをメモメモ。

【参考】

以下のサイトをもとに、作成しています。

Linux Kernel 2.6.x プログラミングガイド
http://uguisu.skr.jp/algo/device.html


【動作確認環境】
ubuntu
(なので、suしてrootでやるところは、sudoしてる)



■手順

1.ソースを作成する。

上記サイトを参考に、以下のファイルをhello.cとして作成した
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> MODULE_LICENSE("GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello world!\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye\n"); } module_init(hello_init); module_exit(hello_exit);


2.Makefileを、以下の内容で、上記hello.cと同じフォルダに作成

obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules


3.コマンドラインからmake
sudo make -C /lib/modules/`uname -r`/build SUBDIRS=`pwd` modules

4.実行
sudo insmod hello.ko
  →デバイスドライバのインストール

sudo lsmod
  →入っているデバイスドライバの表示


5.削除と書き出し内容の確認

sudo rmmod hello.ko
  →デバイスドライバのアンインストール

dmesg
  →デバイスドライバでprintkで出したものを表示


(dmsgの下のほう:2回実行したので、2回出ている)


※uname -rは、実行するとわかるけど、linuxカーネルのバージョンを返す。
 つまり、makeの-Cで、カーネルのバージョンに対応した/lib/modulesを
 渡している

Viewing all articles
Browse latest Browse all 7271

Trending Articles