/* Copyright(c) 2002,2003 Device Drivers Limited, All rights reserved. */ /* info@devdrv.com http://www.devdrv.com/ */ #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #include #include #include /* printk() */ #include /* everything... */ #include #include #include #define BUFSIZ 128 static char st_b[BUFSIZ] = "0\n"; /* データをしまっておくバッファ、初期値は "0" */ /* * hello_read_proc -- ユーザがreadするときに呼び出される */ int hello_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *data) { int data_length; int return_length; printk("read_proc() = %s, count = %d, off = %d\n", st_b, count, offset); data_length = strlen(st_b); if ((offset + count) > data_length) { *eof = 1; if ((return_length = data_length - offset) <= 0) { return 0; /* EOF後や、0バイトで呼ばれる場合がある */ } } else { return_length = count; } strncpy(buf, st_b + offset, return_length); *start = buf + offset; /* 次のスタート位置を更新 */ return return_length; } /* * hello_read_proc -- ユーザがwriteするときに呼び出される */ int hello_write_proc(struct file *file, const char *buf, unsigned long count, void *data) { int length; length = count > (BUFSIZ - 1) ? (BUFSIZ - 1) : count; /* 最大文字数を制限 */ copy_from_user(st_b, buf, length); st_b[length] = '\0'; printk("write_hello(), buf = %s, count = %d, length = %d\n", st_b, count, length); return length; } static void hello_create_proc() { struct proc_dir_entry *entry; entry = create_proc_entry("hello", 0, 0); /* "hello"ファイルの登録 */ entry->read_proc = hello_read_proc; /* read処理ルーチンの登録 */ entry->write_proc = hello_write_proc; /* write処理ルーチンの登録 */ } static void hello_remove_proc() { remove_proc_entry("hello", NULL /* parent dir */); } int init_module(void) { hello_create_proc(); printk("<1>Hello, world data = %s\n", st_b); return 0; } void cleanup_module(void) { printk("<1>Goodbye world data = %s\n", st_b); hello_remove_proc(); }