hello1.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/miscdevice.h> #include <linux/mutex.h> #define HELLO_MAJOR 234 static int debug_enable = 0; module_param(debug_enable, int, 0); MODULE_PARM_DESC(debug_enable, "Enable module debug mode."); static struct file_operations hello1_fops; static int hello1_open(struct inode * inode, struct file * file){ printk("hello1_open, \n"); return 0; } static int hello1_release(struct inode * inode, struct file * file){ printk("hello1_release, \n"); return 0; } static ssize_t hello1_read(struct file * file, char * buf, size_t length, loff_t * ofs){ printk("hello1_read, \n"); return 0; } static ssize_t hello1_write(struct file * file, const char * buf, size_t length, loff_t * ofs){ printk("hello1_write, \n"); return 0; } static DEFINE_MUTEX(hello1_mutex); static long hello1_ioctl(struct file * file, unsigned int cmd, unsigned long arg){ printk("hello1_ioctl, \n"); switch(cmd){ default: mutex_unlock(&hello1_mutex); return ENOTTY; } mutex_unlock(&hello1_mutex); return 0; } static struct file_operations hello1_fops = { .owner = THIS_MODULE, .open = hello1_open, .release = hello1_release, .read = hello1_read, .write = hello1_write, .unlocked_ioctl = hello1_ioctl, }; static struct miscdevice hello1_driver = { .minor = MISC_DYNAMIC_MINOR, .name = "hello1", .fops = &hello1_fops, }; static int hello1_init(void){ int ret; printk("Hello Example Init - debug mode is %s\n", debug_enable ? "enabled" : "disabled"); ret = register_chrdev(HELLO_MAJOR, "hello1", &hello1_fops); if (ret < 0) { printk("Error registering hello device\n"); goto hello_fail1; } printk("Hello: registered module successfully!\n"); /* Init processing here... */ return 0; hello_fail1: return ret; } static void hello1_exit(void){ printk("Hello Example Exit\n"); unregister_chrdev(HELLO_MAJOR,"hello1"); } module_init(hello1_init); module_exit(hello1_exit); MODULE_AUTHOR("Chris Hallinan"); MODULE_DESCRIPTION("Hello World Example"); MODULE_LICENSE("GPL"); |
위의 소스에서 진하게 강조된 소스의 원래 문장은 아래와 같았다.
하지만 에러가 생겨 구글링과 이것저것 알아보니 헤더파일의 수정? 보완이 되어
오류가 생긴것이었다.
static struct file_operations hello1_fops = {
owner: THIS_MODULE,
read: hello1_read,
write: hello1_write,
ioctl: hello1_ioctl,
open: hello1_open,
release: hello1_release
}
사용된 명령어 모음
이클립스에서 빌드로 HBE-SM7-S4412 M2모듈로 넣는다.
cd /system/lib/modules/
모듈 폴더로 이동해 이클립스로 넣은 파일확인
ls
mknod /dev/hello1 c 234 0
/dev폴더에 hello1을 만든다
ls -l /dev/hello1 만들어진 파일확인 (필요에따라 chmod 명령어를 이용해 권한설정)
hello1.c 를 실행해주는 helloapp.c 파일작성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char **argv) { /* Our file descriptor */ int fd; int rc = 0; char *rd_buf[16]; printf("%s: entered\n", argv[0]); /* Open the device */ fd = open("/dev/hello1", O_RDWR); if ( fd == -1 ) { perror("open failed"); rc = fd; exit(-1); } printf("%s: open: successful\n", argv[0]); /* Issue a read */ rc = read(fd, rd_buf, 0); if ( rc == -1 ) { perror("read failed"); close(fd); exit(-1); } printf("%s: read: returning %d bytes!\n", argv[0], rc); close(fd); return 0; } |
사용된 명령어 해석
fd = open("/dev/hello", O_RDWR); 의 명령어로 인해 위에서만들어진 /dev/hello1 을 open
cd /data/local/tmp
./helloapp
실행하면 hello1을 오픈하며 다음과 같이 실행된다.
rmmod 파일명 (모듈에서 내리기)
insmod 파일명 인자 (모듈 올리기 인자값은 선택)
dmesg 파일명
dmesg | tail
dmesg | grep 파일명
lsmod 올라가있는목록보기
mknod /dev/hello1 c 234 0
'Study > 리눅스' 카테고리의 다른 글
| 리눅스 파일 open, close 로 디바이스 장치 올리기 (0) | 2014.12.03 |
|---|---|
| 리눅스 c언어로 시리얼 통신하기 (0) | 2014.10.30 |
| 리눅스로 c언어 컴파일하기 Java (2) (0) | 2014.09.19 |
| 리눅스로 c언어 컴파일하기 Java (1) (0) | 2014.09.19 |
| 리눅스로 c언어 컴파일하기 Linked List (2) (0) | 2014.09.19 |
