0
나는 google과 ldd3의 참고 문헌에 따라 리눅스에서 장치 드라이버를 작성하는 방법을 배우려는 장치 드라이버 프로그래밍의 초보자입니다. 아래 모듈을 삽입 할 수 있지만 응용 프로그램에서 장치를 열려고 할 때 커널이 손상되었습니다.간단한 리눅스 장치 드라이버 오픈 콜 크래시
코드 및 빌드 단계는 다음과 다음 : 모듈
#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 251 0 응용 프로그램을 실행하면 커널 충돌이 발생합니다. 나는 UBUNTU 3.2.0-23-generic-pae를 사용하고 있습니다. 충분한 정보를 얻지 못하고 필요한 세부 사항을 지적하지 못했을 때 저와 동거 해주십시오. 사전에
감사합니다, ANTO
정보를 제공해 주셔서 감사합니다. – Anto
코드에 모듈 라이센싱을 추가하고 ser_open에서 일부 값을 반환했습니다 (이전에는하지 않았 음). 이러한 변경 사항으로 충돌이 발생하지 않습니다.이 두 문제 중 어느 것이 문제를 일으켰는지 확실하지 않습니다. 복귀하고 업데이트합니다. – Anto