2012-07-29 88 views
0

我是设备驱动程序编程的初学者,试图学习如何在linux中编写设备驱动程序,以下是来自google和ldd3的一些参考。我可以在下面插入模块,但是当我尝试在内核崩溃的应用程序中打开设备时。简单的linux设备驱动程序打开调用崩溃

的代码和建立的步骤,接着,如下:

#include <linux/module.h>  /* Needed by all modules */ 
#include <linux/kernel.h>  /* Needed for KERN_INFO */ 
#include <linux/init.h>   /* Needed for the macros */ 
#include <linux/ioport.h> 
#include <asm/io.h> 
#include <linux/interrupt.h> 
#include <linux/sched.h> 
#include <linux/string.h> 
#include <linux/delay.h> 
#include <linux/errno.h> 
#include <linux/types.h> 
#include <asm/uaccess.h> 
#include <asm/irq.h> 
#include <asm/param.h> 
#include <linux/fs.h> 
/* =============== Constant Definitions ============ */ 
#define SERIAL_IRQ 4 
/* =============== Variable Definitions ============ */ 
static int SER_MAJOR = 0; 
int ser_open(struct inode *inode, struct file *filp); 
int ser_release(struct inode *inode, struct file *filp); 

irqreturn_t my_ser_dev_isr(int irq,void *ser_data,struct pt_regs * pt_reg_var) 
{ 
printk("\n\n ------- INTR raised -----------\n\n"); 
return 0; 
} 

int ser_open(struct inode *inode, struct file *filp) 
{ 
if(request_irq(SERIAL_IRQ,&my_ser_dev_isr,1,"my_ser_dev_intr",NULL)) 
{ 
printk("\n interrupt req failed\n"); 
} 
else 
{ 
enable_irq(SERIAL_IRQ); 
printk("\n!!!! ..obtained the requested interrupt and enabled\n"); 
} 
} 
int ser_release(struct inode *inode, struct file *filp) 
{ 
disable_irq(SERIAL_IRQ); 
free_irq(SERIAL_IRQ,NULL) ; 
} 
static struct file_operations ser_fops = { 
open: ser_open, 
release: ser_release 
}; 

void *p = NULL; 
irqreturn_t my_ser_dev_isr (int, void *, struct pt_regs *); 
static int __init hello_start(void) 
{ 
int ret_val=-1; 
int result; 
printk(KERN_INFO "Loading hello module...\n"); 
printk(KERN_INFO "Hello world\n"); 
result = register_chrdev(SER_MAJOR,"SER_DEV",&ser_fops); 
if(result < 0) 
{ 
printk(KERN_WARNING"Can't get major %d\n",SER_MAJOR); 
return result; 
} 
if(SER_MAJOR == 0) 
{ 
SER_MAJOR = result; 
printk("SER DEV Major Number : %d",SER_MAJOR); 
} 
return 0; 
} 

static void __exit hello_end(void) 
{ 
// free_irq(SERIAL_IRQ,NULL); 
//release_region(0x0031,1); 
printk(KERN_INFO "Goodbye Mr.\n"); 
} 
module_init(hello_start); 
module_exit(hello_end); 

生成文件为模块:

obj-m := hello.o 
default: 
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules 

用于accesing的应用如下:

#include <stdio.h> /* test.c */ 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <errno.h> 
static int dev; 
int main(void) 
{ 
char buff[40]; 
dev = open("/dev/my_ser_dev",O_RDONLY); 
if(dev < 0) 
{ 
printf("Device Open ERROR!\n"); 
exit(1); 
} 
printf("Please push the GPIO_16 port!\n"); 
//read(dev,buff,40); 
// scanf("%s",buff);  
printf("%s\n",buff); 
close(dev); 
return 0; 
} 

insmod的给

[ 3837.312140] Loading hello module... 
[ 3837.312147] Hello world 
[ 3837.312218] SER DEV Major Number : 251 

然后,我使用mknod/dev/my_ser_dev c创建了特殊文件执行应用程序导致内核崩溃。我正在使用UBUNTU 3.2.0-23-generic-pae 如果我缺乏足够的信息并指出所需的详细信息,请与我联系。

由于提前, 安托

回答

0

您注册为您的IRQ处理函数的函数有错误的原型 - 它应该像

irqreturn_t irq_handler(int, void *); 

也许你指的是旧的文档。

+0

非常感谢你的信息。 – Anto 2012-07-31 07:03:10

+0

我向代码添加了模块许可,并在ser_open中返回了一些值(我之前没有这样做)。有了这些改变,崩溃是不可见的。不知道这两个问题中的哪一个造成了问题。将恢复和更新。 – Anto 2012-07-31 07:17:19