| 
 | 
 
这是一段应用程序代码,我测试过open 和ioctl函数都能正常测试  但是write函数总是返回-1,用printk在驱动的write函数里面也证明根本就没找到该驱动的write函数。 
#include <unistd.h> 
#include <sys/ioctl.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define LED_ON 1 
#define LED_OFF 0 
static int fd;  //用于存放打开设备时返回的文件句柄 
 
int main(int argc, char **argv) 
{ 
 int led_no = 0; 
 int val ; 
 int ret; 
 fd = open("/dev/GPIO_DRV", 0);  //打开设备 
 if(fd < 0) 
  { 
   perror("open device failed"); 
   exit(1); 
  }  
   
 while(1) 
  { 
   for(led_no = 0; led_no < 4; led_no++) 
    { 
     ioctl(fd, LED_ON, led_no);  //点亮一盏灯 
     sleep(2); 
    } 
   led_no = 0; 
   for(led_no = 0; led_no < 4; led_no++) 
    { 
     ioctl(fd, LED_OFF, led_no);  //点亮一盏灯 
     sleep(1); 
    } 
   led_no = 0; 
   if(argc != 2) 
    { 
     printf("Usage :\n"); 
     printf("%s <on|off>\n", argv[0]); 
     return 0;   //退出重新运行应用程序 
    } 
    
   if (strcmp(argv[1], "go") != 0)  //如果第二个参数不是go 
   { 
    if (strcmp(argv[1], "on") == 0)  //如果第二个参数为on 
    { 
     printf("led app on\n"); 
     val  = 1; 
    } 
    else if (strcmp(argv[1], "off") == 0) //第二个参数为off 
    { 
     printf("led app off\n"); 
     val = 0; 
    } 
     ret = write(fd, &val, 4); 
    if(ret < 0) 
     { 
       printf("write failed ret = %d\n", ret); 
       return 1; 
     } 
    return 0; 
   } 
  
    
  } 
 //close(fd); //关闭设备 
 return 0; 
} 
 
printf("write failed ret = %d\n", ret); 
到这句就打印出 ret = -1 
 |   
 
 
 
 |