1、字符类型设备的驱动程序编写一个简单的字符设备驱动程序。要求改字符设备包括scull_open()、scull_write()、scull_read()、scull_ioctl()和scull_re
时间: 2023-05-02 16:00:57 浏览: 59
这道题目的要求是编写一个简单的字符设备驱动程序。要求修改字符设备驱动包括 scull_open()、scull_write()、scull_read()、scull_ioctl() 和 scull_release() 函数。
相关问题
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include "scull.h" void write_proc(void); void read_proc(void); int main(int argc, char **argv){ if(argc == 1){ puts( "Usage: scull_test [write|read]"); exit(0); } if( !strcmp(argv[1],"write")) write_proc(); else if(!strcmp(argv[1],"read")) read_proc(); else puts( "scull_test: invalid command! "); return 0; } void write_proc(){ int fd, len,quit = 0; char buf[ 100]; fd = open(DEVICE_FILE,O_WRONLY); if(fd <= 0){ printf("Error opening device file %s for writing!\n",DEVICE_FILE); exit(1); } printf( "input 'exit' to exit!"); while( !quit) { printf( "\n write>> "); fgets(buf, 100,stdin); if(!strcmp(buf, "exit\n")) quit =1; while(ioctl(fd,SCULL_QUERY_NEW_MSG)) usleep(1000); len=write(fd, buf, strlen(buf)); if(len<0){ printf( "Error writing to device %s !\n" ,SCULL_NAME); close(fd); exit(1); } printf("%d bytes written to device %s!\n",len- 1,SCULL_NAME); } close(fd); } void read_proc(){ printf("\n read<< "); while(!ioctl(fd,SCULL_QUERY_NEW_MSG)) usleep(1000);// get the msg length len=ioctl(fd, SCULL_QUERY_MSG_LENGTH, NULL); if(len){ if(buf!=NULL) free(buf); buf = malloc(sizeof(char)*(len+1)); len = read(fd, buf, len); if(len < 0){ printf("Error reading from device %s!", SCULL_NAME); }else{ if(!strcmp(buf,"exit\n")){ ioctl(fd, SCULL_RESET); // reset quit = 1; printf("%s\n",buf); }else printf("%s\n",buf); } } free(buf); close(fd); }
在这段代码中,有一个 `quit` 变量被使用了,但是并没有被声明。具体来说,`quit` 变量在 `write_proc` 函数中被定义为整型变量,并在循环体中被使用。
为了解决这个问题,你需要在 `write_proc` 函数中的开头部分,即 `int fd, len,quit = 0;` 这行代码之前,添加 `int quit;` 这行代码,以显示声明 `quit` 变量。这样编译器就能够正确地识别这个变量,并在使用之前进行声明。
#ifndef __KERNEL__ #define __KERNEL__ #endif #ifndef MODULE #define MODULE #endif #include <linuxersion.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux.h> #include <linuxpes.h> #include <linux/errno.h> #include <linux/fs.h> #include <linux/sched.h> #include <linux/ioport.h> #include <linuxab.h> #include <linuxring.h> #include <asm/io.h> #include <asmgment.h> #include <asm/uaccess.h> #include "scull.h"
这段代码中包含了多个头文件,其中可能有一个错误的头文件名字。具体来说,`<asmgment.h>` 是一个非标准的头文件,不属于 Linux 内核或者标准的 C/C++ 库。如果你的代码中包含了这个头文件,编译器就会找不到它并报错。
你需要检查代码中的头文件名字是否正确,如果有误,需要将其更正为正确的头文件名字。如果你不确定应该包含哪个头文件,可以查看相关函数或变量的文档或者示例代码,确定正确的头文件名字。