Created
October 7, 2012 11:30
-
-
Save hnakagawa/3847981 to your computer and use it in GitHub Desktop.
Hello linux driver(勉強会配布用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/fs.h> | |
#include <linux/platform_device.h> | |
#include <linux/uaccess.h> | |
#define DEVICE_NAME "hello" | |
static int major_num; | |
static ssize_t hello_read(struct file *file, char __user *buf, size_t count, loff_t * ppos) | |
{ | |
static const char *val = "Hello!!!"; | |
ssize_t size; | |
if (*ppos) | |
return 0; | |
size = strlen(val); | |
if (copy_to_user(buf, val, size)) | |
return -EFAULT; | |
*ppos = size; | |
return size; | |
} | |
static const struct file_operations device_ops = { | |
.owner = THIS_MODULE, | |
.read = hello_read, | |
}; | |
static int __devinit hello_probe(struct platform_device *dev) | |
{ | |
major_num = register_chrdev(0, DEVICE_NAME, &device_ops); | |
if (major_num < 0) { | |
printk(KERN_ERR"hello driver: Unable to register char device\n"); | |
return major_num; | |
} | |
return 0; | |
} | |
static int __devexit hello_remove(struct platform_device *dev) | |
{ | |
unregister_chrdev(major_num, DEVICE_NAME); | |
return 0; | |
} | |
static struct platform_device *hello_device; | |
static struct platform_driver hello_driver = { | |
.driver = { | |
.name = "hello", | |
.owner = THIS_MODULE, | |
}, | |
.probe = hello_probe, | |
.remove = __devexit_p(hello_remove), | |
}; | |
static int __init hello_driver_init(void) | |
{ | |
int ret; | |
printk(KERN_INFO"Initialize hello driver\n"); | |
hello_device = platform_device_alloc("hello", -1); | |
if (!hello_device) { | |
ret = -ENOMEM; | |
goto err_device_alloc; | |
} | |
ret = platform_device_add(hello_device); | |
if (ret) { | |
printk(KERN_ERR"hello driver: Unable to add device\n"); | |
goto err_device_add; | |
} | |
ret = platform_driver_register(&hello_driver); | |
if (ret) { | |
printk(KERN_ERR"hello driver: Unable to register driver\n"); | |
goto err_driver_register; | |
} | |
return 0; | |
err_driver_register: | |
platform_device_unregister(hello_device); | |
err_device_add: | |
platform_device_put(hello_device); | |
err_device_alloc: | |
return ret; | |
} | |
static void __exit hello_driver_exit(void) | |
{ | |
printk(KERN_INFO"Cleanup hello driver\n"); | |
platform_driver_unregister(&hello_driver); | |
platform_device_unregister(hello_device); | |
} | |
MODULE_LICENSE("GPL"); | |
module_init(hello_driver_init); | |
module_exit(hello_driver_exit); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KERNEL ?= /usr/src/linux | |
PWD := $(shell pwd) | |
obj-m := hello_driver.o | |
all: | |
make -C $(KERNEL) M=$(PWD) modules | |
clean: | |
rm -f *.o *.ko *.mod.c |
シンプルなキャラクタデバイスドライバです。insmod後
$ cat /proc/devices
でメジャー番号を確認し、
$ mknod /dev/hello c {メジャー番号} 0
でデバイスファイルを作成して下さい。
$ cat /dev/hello
とすると"Helllo!!!"と表示されるはずです。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
勉強会配布用