当前位置: 技术问答>linux和unix
初学linux驱动
来源: 互联网 发布时间:2015-10-26
本文导语: 我照猫画虎,写了一个很短的,就想试着跑通一下, #include #include #include #include #include #include #include #include /*kernel2.4建议用它代替*#include */ #include unsigned int major_num = 125; const char *name = "Test"; ...
我照猫画虎,写了一个很短的,就想试着跑通一下,
#include
#include
#include
#include
#include
#include
#include
#include /*kernel2.4建议用它代替*#include */
#include
unsigned int major_num = 125;
const char *name = "Test";
int my_open(struct inode *inode,struct file *filp){
char *s = "This is Test!";
filp->private_data = s;
printk("------ invoke my_open!-------n");
MOD_INC_USE_COUNT;
return 0;
}
int my_release(struct inode *inode,struct file *filp){
printk("------invoke my_release!------n");
MOD_DEC_USE_COUNT;
return 0;
}
ssize_t my_read(struct file *filp,char *buf,size_t count,loff_t *f_pos){
char *s = filp->private_data;
printk("------invoke my_read------n");
copy_to_user(buf,s,5);
ssize_t result = 0;
return result;
}
ssize_t my_write(struct file *filp,char *buf,size_t count,loff_t *f_pos){
printk("------invoke my_write ------n");
ssize_t result = 0;
return result;
}
int init_module(void){
// Device Major Number for Dveloping,60-63,120-127,240-254
// 0 means using Dynamic!
struct file_operations my_ops = {
open: my_open,
release: my_release,
read: my_read,
write: my_write,
};
// register my device!
int result = register_chrdev(major_num,name,&my_ops);
if(result==0){
printk("Device major number is ->%in",major_num);
}
return 0;
}
void cleanup_module(void){
int exit_result = unregister_chrdev(major_num,name);
printk("unregister result is -> %in",exit_result);
}
结果报错 :
/var/log/messages:
Oct 7 11:27:43 localhost kernel: Device major number is ->125 // 我写死了主设备号125
Oct 7 11:28:30 localhost kernel: Unable to handle kernel paging request at virtual address d72ea6c4
Oct 7 11:28:30 localhost kernel: printing eip:
Oct 7 11:28:30 localhost kernel: c0e33b84
Oct 7 11:28:30 localhost kernel: *pde = 00000000
Oct 7 11:28:30 localhost kernel: Oops: 0002
Oct 7 11:28:30 localhost kernel: Test es1371 ac97_codec gameport soundcore nls_iso8859-1 nls_cp437 vfat fat usb-storage parport_pc lp parport autofs pcnet32 mii keybdev mousedev hid input usb
Oct 7 11:28:30 localhost kernel: CPU: 0
Oct 7 11:28:30 localhost kernel: EIP: 0060:[] Tainted: PF
Oct 7 11:28:30 localhost kernel: EFLAGS: 00010217
Oct 7 11:28:30 localhost kernel:
Oct 7 11:28:30 localhost kernel: EIP is at ___strtok_R29805c13 [] 0xa58a88 (2.4.20-8)
Oct 7 11:28:30 localhost kernel: eax: c72e9ec0 ebx: 00000000 ecx: c197f480 edx: c0e33b80
Oct 7 11:28:30 localhost kernel: esi: c197f480 edi: c2538900 ebp: c16b1340 esp: c7285f3c
Oct 7 11:28:30 localhost kernel: ds: 0068 es: 0068 ss: 0068
Oct 7 11:28:30 localhost kernel: Process cat (pid: 2692, stackpage=c7285000)
Oct 7 11:28:30 localhost kernel: Stack: c014792e c2538900 c197f480 c197f480 c2538900 ffffffe9 c0145fb3 c2538900
Oct 7 11:28:30 localhost kernel: c197f480 00008000 c9ede000 bffffaea bfffe488 c0145edd c4f1db80 c16b1340
Oct 7 11:28:30 localhost kernel: 00008000 c7285f84 c4f1db80 c16b1340 c9ede000 c0f5e79c 00000003 00000001
Oct 7 11:28:31 localhost kernel: Call Trace: [] chrdev_open [kernel] 0x5e (0xc7285f3c))
Oct 7 11:28:31 localhost kernel: [] dentry_open [kernel] 0xd3 (0xc7285f54))
Oct 7 11:28:31 localhost kernel: [] filp_open [kernel] 0x6d (0xc7285f70))
Oct 7 11:28:31 localhost kernel: [] sys_open [kernel] 0x53 (0xc7285fa8))
Oct 7 11:28:31 localhost kernel: [] system_call [kernel] 0x33 (0xc7285fc0))
Oct 7 11:28:31 localhost kernel:
Oct 7 11:28:31 localhost kernel:
Oct 7 11:28:31 localhost kernel: Code: 00 80 04 08 00 10 05 08 00 3c e3 c0 25 00 00 00 75 18 00 00
求那位仁兄能帮我指出来错误!!!困扰我很多天了!!!感激不尽!!!
#include
#include
#include
#include
#include
#include
#include
#include /*kernel2.4建议用它代替*#include */
#include
unsigned int major_num = 125;
const char *name = "Test";
int my_open(struct inode *inode,struct file *filp){
char *s = "This is Test!";
filp->private_data = s;
printk("------ invoke my_open!-------n");
MOD_INC_USE_COUNT;
return 0;
}
int my_release(struct inode *inode,struct file *filp){
printk("------invoke my_release!------n");
MOD_DEC_USE_COUNT;
return 0;
}
ssize_t my_read(struct file *filp,char *buf,size_t count,loff_t *f_pos){
char *s = filp->private_data;
printk("------invoke my_read------n");
copy_to_user(buf,s,5);
ssize_t result = 0;
return result;
}
ssize_t my_write(struct file *filp,char *buf,size_t count,loff_t *f_pos){
printk("------invoke my_write ------n");
ssize_t result = 0;
return result;
}
int init_module(void){
// Device Major Number for Dveloping,60-63,120-127,240-254
// 0 means using Dynamic!
struct file_operations my_ops = {
open: my_open,
release: my_release,
read: my_read,
write: my_write,
};
// register my device!
int result = register_chrdev(major_num,name,&my_ops);
if(result==0){
printk("Device major number is ->%in",major_num);
}
return 0;
}
void cleanup_module(void){
int exit_result = unregister_chrdev(major_num,name);
printk("unregister result is -> %in",exit_result);
}
结果报错 :
/var/log/messages:
Oct 7 11:27:43 localhost kernel: Device major number is ->125 // 我写死了主设备号125
Oct 7 11:28:30 localhost kernel: Unable to handle kernel paging request at virtual address d72ea6c4
Oct 7 11:28:30 localhost kernel: printing eip:
Oct 7 11:28:30 localhost kernel: c0e33b84
Oct 7 11:28:30 localhost kernel: *pde = 00000000
Oct 7 11:28:30 localhost kernel: Oops: 0002
Oct 7 11:28:30 localhost kernel: Test es1371 ac97_codec gameport soundcore nls_iso8859-1 nls_cp437 vfat fat usb-storage parport_pc lp parport autofs pcnet32 mii keybdev mousedev hid input usb
Oct 7 11:28:30 localhost kernel: CPU: 0
Oct 7 11:28:30 localhost kernel: EIP: 0060:[] Tainted: PF
Oct 7 11:28:30 localhost kernel: EFLAGS: 00010217
Oct 7 11:28:30 localhost kernel:
Oct 7 11:28:30 localhost kernel: EIP is at ___strtok_R29805c13 [] 0xa58a88 (2.4.20-8)
Oct 7 11:28:30 localhost kernel: eax: c72e9ec0 ebx: 00000000 ecx: c197f480 edx: c0e33b80
Oct 7 11:28:30 localhost kernel: esi: c197f480 edi: c2538900 ebp: c16b1340 esp: c7285f3c
Oct 7 11:28:30 localhost kernel: ds: 0068 es: 0068 ss: 0068
Oct 7 11:28:30 localhost kernel: Process cat (pid: 2692, stackpage=c7285000)
Oct 7 11:28:30 localhost kernel: Stack: c014792e c2538900 c197f480 c197f480 c2538900 ffffffe9 c0145fb3 c2538900
Oct 7 11:28:30 localhost kernel: c197f480 00008000 c9ede000 bffffaea bfffe488 c0145edd c4f1db80 c16b1340
Oct 7 11:28:30 localhost kernel: 00008000 c7285f84 c4f1db80 c16b1340 c9ede000 c0f5e79c 00000003 00000001
Oct 7 11:28:31 localhost kernel: Call Trace: [] chrdev_open [kernel] 0x5e (0xc7285f3c))
Oct 7 11:28:31 localhost kernel: [] dentry_open [kernel] 0xd3 (0xc7285f54))
Oct 7 11:28:31 localhost kernel: [] filp_open [kernel] 0x6d (0xc7285f70))
Oct 7 11:28:31 localhost kernel: [] sys_open [kernel] 0x53 (0xc7285fa8))
Oct 7 11:28:31 localhost kernel: [] system_call [kernel] 0x33 (0xc7285fc0))
Oct 7 11:28:31 localhost kernel:
Oct 7 11:28:31 localhost kernel:
Oct 7 11:28:31 localhost kernel: Code: 00 80 04 08 00 10 05 08 00 3c e3 c0 25 00 00 00 75 18 00 00
求那位仁兄能帮我指出来错误!!!困扰我很多天了!!!感激不尽!!!
|
有问题的地方:
char *s = "This is Test!";
应该将定义放在外部而不是函数内;
struct file_operations my_ops...
也是一样。
其他的就是my_write的buf参数应该是const char *,my_read()/my_write()的返回值都不对。
char *s = "This is Test!";
应该将定义放在外部而不是函数内;
struct file_operations my_ops...
也是一样。
其他的就是my_write的buf参数应该是const char *,my_read()/my_write()的返回值都不对。
|
将printk的优先级设为KERN_ALERT试一下:
printk(KERN_ALERT "testn");
printk(KERN_ALERT "testn");
|
为什么我照你改正后的程序敲了一遍,但是还是报几大屏的错呢?哪位兄台给指点一下.而且很多关于什么timer.h等等的都出来了.我自己写过很多小程序,也都是这些莫名其妙的错误,不知道怎么办.
而报couldn't find the kernel version the module was compiled for这样的错误又该如何解决?如果大家有好的资料,特别是基础的,给我发到 pinfield@gmail.com,我将不胜感激.另外可以开帖给分.我被这些问题搞得头都大了
而报couldn't find the kernel version the module was compiled for这样的错误又该如何解决?如果大家有好的资料,特别是基础的,给我发到 pinfield@gmail.com,我将不胜感激.另外可以开帖给分.我被这些问题搞得头都大了
|
可惜我不知道呀!!我一直开发国家邮政的项目 做Pb 4 年了,把powerbuilder从头到尾都从新封装了!现在作出的东西找不到pb的风格了!呵呵。。。。