디바이스 드라이버 모듈 프로그래밍
글쓴이: yuleam / 작성시간: 목, 2003/05/22 - 2:43오전
" 리눅스 매니아를 위한 커널 프로그래밍 " 이란 책으로 리눅스 프로그래밍 공부를 하고 있는데 ....
잘 되지 않는 부분이 있어서 ... 질문 올립니다 ...
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/malloc.h>
4 #include <linux/fs.h>
5 #include <asm/uaccess.h>
6
7 #define DEVICE_NAME "mydrv"
8 #define MYDRV_MAJOR 250
9
10 static int mydrv_open(struct inode *inode, struct file *file)
11 {
12 if (MAJOR(inode->i_rdev)!=MYDRV_MAJOR)
13 return -1;
14 MOD_INC_USE_COUNT;
15 return 0;
16 }
17
18 static int mydrv_release(struct inode * inode, struct file *file)
19 {
20 if (MAJOR(inode->i_rdev) != MYDRV_MAJOR)
21 return -1;
22 MOD_DEC_USE_COUNT;
23 return 0;
24 }
25
26 static ssize_t mydrv_read(struct file *file, char *buf, size_t count, loff_t *ppos)
27 {
28 printk("mydrv_read is invorked\n");
29 return 0;
30 }
31
32 static ssize_t mydrv_write(struct file *file, char *buf, size_t count, loff_t *ppos)
33 {
34 printk("mydrv_write is invoked\n");
35 return 0;
36 }
37
38 struct file_operations mydrv_fops = {
39 NULL,
40 mydrv_read,
41 mydrv_write,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 mydrv_open,
47 NULL,
48 mydrv_release,
49 NULL,
50 NULL,
51 NULL,
52 NULL
53 };
54
55 int init_module()
56 {
57 int result;
58
59 result = register_chrdev(MYDRV_MAJOR, "mydrv", &mydrv_fops);
60 if (result < 0) {
61 printk("mydrv : %d character device driver can't be registered\n", MYDRV_MAJOR);
62 return result;
63 }
64
65 return 0;
66 }
67
68 void cleanup(void)
69 {
70 unregister_chrdev(MYDRV_MAJOR, "mydev");
71 }
이 프로그램을 컴파일했는데
[2002 Thu May 22 root@pulip] gcc -D__KERNEL__ -D_LINUX -DMODULE -c mydrv.c
mydrv.c:40: warning: initialization from incompatible pointer type
mydrv.c:46: warning: initialization from incompatible pointer type
mydrv.c:48: warning: initialization from incompatible pointer type
이런 에러가 뜨네여
file_operations 이란 구조체가 이상한듯 해서 .....
include/linux/fs.h 라는 파일을 뒤져보았습니다.
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
};
구조체랑 file_operations 라는 구조체가 맞지 않는것 같아서
소스코드 부분을
struct file_operations mydrv_fops = {
NULL,
NULL,
mydrv_read,
mydrv_write,
NULL,
NULL,
NULL,
NULL,
mydrv_open,
NULL,
mydrv_release,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
이렇게 고치고 해보았지만 ... 역시 warning이 뜨네여 ..
mydrv.c:42: warning: initialization from incompatible pointer type
pointer의 타입이 맞지 않는다고 하는데 .... 저는 이상한점을 찾을수가 없네요.
도움 부탁드립니다 ........
Forums:


구조체와 님께서 설정한 구조체의 위치를 비교해 보세요.. 잘못된것 같은데
구조체와 님께서 설정한 구조체의 위치를 비교해 보세요.. 잘못된것 같은데..
^^ 그럼 이만
그리고 커널의 버젼에 따라 약간의 차이가 있을것입니다. 체크해 보세요 ^^
const...
32 static ssize_t mydrv_write(struct file *file, char *buf, size_t count, loff_t *ppos) 33 { 34 printk("mydrv_write is invoked\n"); 35 return 0; 36 }여기서 char *buf가 const char *buf로 바뀌면 될것 같군요..
그리고...왠만하면..
struct file_operations mydrv_fops = { read: mydrv_read, write: mydrv_write, open: mydrv_open, release: mydrv_release, };이렇게 처리해 주는게 좋겠죠? 보기에도 좋구, 소스크기도 줄일수 있으니까요
감사합니다 ~ ^^*
몇일동안 보면서 삽질했는데 ..
바루 해결이 되버리네여 :D
너무 감사합니다 ~
글구 프로그램팁두 ....^^
오늘 하루 즐겁게 보내세여 ~ ^^*
댓글 달기