我用的linux驱动下编写一个 LED的驱动,没有自定义 open 和 close函数,我在上层调用的时候,利用:
int fd=open(DEV_NAME,O_RDWR);
return fd;
可以正常打开设备,并进行 LED 的操作。
但是我调用:
int ret;
ret = close(fd);
return ret;
关闭设备,就出现问题了,ret 一直是 -1.
不知道出什么问题了,求指导哦。
#define DEVICE_NAME "xxxx"
static int led_gpios[] = {
S5PV210_GPJ2(0),
S5PV210_GPJ2(1),
S5PV210_GPJ2(2),
S5PV210_GPJ2(3),
};
#define LED_NUM ARRAY_SIZE(led_gpios)
static long fjicc_leds_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > LED_NUM) {
return -EINVAL;
}
gpio_set_value(led_gpios[arg], !cmd);
//printk(DEVICE_NAME": %d %d\n", arg, cmd);
break;
default:
return -EINVAL;
}
return 0;
}
static struct file_operations fjicc_led_dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = fjicc_leds_ioctl,
};
static struct miscdevice fjicc_led_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &fjicc_led_dev_fops,
};
static int __init fjicc_led_dev_init(void) {
int ret;
int i;
for (i = 0; i < LED_NUM; i++) {
ret = gpio_request(led_gpios[i], "LED");
if (ret) {
printk("%s: request GPIO %d for LED failed, ret = %d\n", DEVICE_NAME,
led_gpios[i], ret);
return ret;
}
s3c_gpio_cfgpin(led_gpios[i], S3C_GPIO_OUTPUT);
gpio_set_value(led_gpios[i], 1);
}
ret = misc_register(&fjicc_led_dev);
printk(DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit fjicc_led_dev_exit(void) {
int i;
for (i = 0; i < LED_NUM; i++) {
gpio_free(led_gpios[i]);
}
misc_deregister(&fjicc_led_dev);
}
module_init(fjicc_led_dev_init);
module_exit(fjicc_led_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xxxxxxxxxxx");
后来我也尝试着加入 open 和 close 函数,但是这回连打开设备都出现问题了,我添加的open和close 如下: