红联Linux门户
Linux帮助

求助:自己写的SPI设备驱动问题

发布时间:2012-09-14 10:54:18来源:红联作者:zwxhehe2012
自己写了一个SPI设备的驱动,加载驱动后已经成功调用probe函数并创建了设备文件,问题是现在应用程序打开设备文件后就出现以下错误:将驱动中的open函数与release函数中其它删除只留下打印信息,应用程序中只留下打开和关闭设备文件代码,还是会出现如下错误,麻烦大侠帮忙解决以下该问题(已经调试好久了还没解决)


Unable to handle kernel NULL pointer dereference at virtual address 0000002fe driver uvcv
usbcore:
LR is at sys_close+0xa0/0xcc
pc : [] lr : [] psr: 80000013
sp : c082bf68 ip : c082bf88 fp : c082bf84
r10: 00000000 r9 : c082a000 r8 : c0c80008
r7 : 00000006 r6 : 0000001b r5 : c2b01cc0 r4 : 00000003
r3 : 00000007 r2 : 00000004 r1 : c2b01cc0 r0 : 0000001b
Flags: Nzcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: c000717f Table: 32b84000 DAC: 00000015
Process app_gp21 (pid: 504, stack limit = 0xc082a268)
Stack: (0xc082bf68 to 0xc082c000)
bf60: 00000003 c2b01cc0 0000001b 00000006 c082bfa4 c082bf88
bf80: c0cde8d0 c0cde7c4 0000b494 00000000 0000b450 00000006 00000000 c082bfa8
bfa0: c0c7fe60 c0cde840 0000b494 00000000 00000003 00000000 00084f2c 00000001
bfc0: 0000b494 00000000 0000b450 00000006 00000000 00000000 00000000 be851b94
bfe0: 00000000 be851b78 00008278 0001522c 60000010 00000003 e5803008 e542c001
Backtrace:
[] (filp_close+0x0/0x7c) from [] (sys_close+0xa0/0xcc)
r7:00000006 r6:0000001b r5:c2b01cc0 r4:00000003
[] (sys_close+0x0/0xcc) from [] (ret_fast_syscall+0x0/0x2c)
r7:00000006 r6:0000b450 r5:00000000 r4:0000b494
Code: e89da800 e1a0c00d e92dd8f0 e24cb004 (e5903014)
---[ end trace d982a507971a8f45 ]---
Segmentation fault


驱动程序主要代码如下:
[code=C/C++][/code]


#define ARRYSIZE(x) ( sizeof(x)/ sizeof(x[0]))
#define EINT3_IRQ 7
#define SPI_MAJOR 252
#define HIGH 1
#define LOW 0


extern int spi_register_driver(struct spi_driver *sdrv);
extern int spi_write_then_read(struct spi_device *spi,const u8 *txbuf,unsigned n_tx,u8 *rxbuf,unsigned n_rx);
extern int spi_sync(struct spi_device *spi,struct spi_message *message);
extern struct class *__class_creat(struct module *owner ,const char *name,struct lock_class_key *key);
extern int register_chrdev(unsigned int major,const char *name,const struct file_operations *fops);




static char ev_irq = 0; // 定义表示中断发生的变量

static DECLARE_WAIT_QUEUE_HEAD(measure_waitq); //定义一个等待队列




struct spidev_data
{
dev_t devt;
struct spi_device *spi ;
};



irqreturn_t mp_interrupt (int irq ,void *dev_id)
{
unsigned char value ;
value = s3c2410_gpio_getpin(S3C2410_GPF3);
if (!value)
{
ev_irq = 1 ;
wake_up_interruptible (&measure_waitq);
}


}



int Mp_Open (struct inode *inode ,struct file *file)
{
/* struct spidev_data *data_spi ;
file->private_data = data_spi ;
if (request_irq(EINT3_IRQ,mp_interrupt,IRQF_DISABLED|IRQF_TRIGGER_LOW,"mp",NULL)) //申请中断
{
printk ("request_irq error\n");

} */


printk("kernel open device success\n");
}




int Mp_Release (struct inode *inode ,struct file *filp)
{
printk("close device\n");



}
struct file_operations mp_fops =
{
.owner = THIS_MODULE ,
.open = Mp_Open ,
.release = Mp_Release ,


};

static struct class *spi_class ;



static int Mp_Probe (struct spi_device *spi_dev)
{
int ret ;
struct spidev_data *data_spi ;
data_spi= kzalloc (sizeof(*data_spi),GFP_KERNEL);
if(!data_spi)
return -ENOMEM ;
printk("kzalloc success\n");
data_spi->spi = spi_dev ;
data_spi->devt = MKDEV(SPI_MAJOR,0);
device_create(spi_class,&spi_dev->dev,data_spi->devt,data_spi,"mp");
spi_set_drvdata(spi_dev,data_spi);
printk ("probe mp \n ");
return 0 ;


}


static int Mp_Remove (struct spi_device *spi_dev)
{

struct spidev_data *data_spi ;
cdev_del (&data_spi->spi);//注销字符设备
return 0 ;


}


static struct spi_driver mp_driver =
{
.probe = Mp_Probe ,
.remove = Mp_Remove ,
.driver =
{
.name = "mp" ,
.owner = THIS_MODULE ,
}

} ;



static int __init Mp_init (void)
{
unsigned int ret ;
ret = register_chrdev(SPI_MAJOR,"spi",&mp_fops);
if (ret<0)
{
printk ("cdev add error\n");
return ret;
}
spi_class = class_create (THIS_MODULE,"mp");
if (IS_ERR(spi_class))
{
return PTR_ERR(spi_class);
}
return spi_register_driver (&mp_driver);

}

static void __exit Mp_exit (void)
{
spi_unregister_driver (&mp_driver);
}



module_init(Mp_init);
module_exit(Mp_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("mp for EmbedSky SKY2440/TQ2440 Board");


应用程序如下:
[code=C/C++][/code]
void main (int argc ,char **argv)
{
int fd ;
fd = open("/dev/mp" ,O_RDWR);

if (fd<0)
{
printf ("open device error\n");
return ;
}

printf ("open seccuss\n");
close(fd);
}
文章评论

共有 0 条评论