Linux系统ioctl使用示例 ioctl实验(二)

2014-11-24 07:34:55 · 作者: · 浏览: 7
default:
pr_err("Invalid ioctl command.\n");
return -ENOTTY;
}

return err;
}

static const struct file_operations gpio_test_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = gpio_test_ioctl,
.open = gpio_test_open,
.release = gpio_test_release
};

static struct miscdevice gpio_test_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "gpio_test",
.fops = &gpio_test_fops
};


static int gpio_test_probe(struct platform_device *pdev)
{
int ret = 0;

printk("gpio_test_probe 0\n");

/* Initialize */
gpio_priv = kzalloc(sizeof(struct gpio_priv_t), GFP_KERNEL);

if (gpio_priv == NULL) {
pr_alert("Not enough memory to initialize device\n");
return -ENOMEM;
}

ret = misc_register(&gpio_test_dev);

if (ret < 0)
goto err;
return 0;

err:
printk("gpio_test register failed\n");


kfree(gpio_priv);
gpio_priv = NULL;

return ret;

}

static int __devexit gpio_test_remove(struct platform_device *plat)
{

kfree(gpio_priv);
gpio_priv = NULL;

misc_deregister(&gpio_test_dev);
printk("gpio_test remove\n");
return 0;
}

static struct platform_driver gpio_test_driver = {
.probe = gpio_test_probe,
.remove = gpio_test_remove,
.driver = {
.name = "gpio_test",
.owner = THIS_MODULE,
},
};

static int __init gpio_test_init(void)

{
printk("gpio_test_init \n");
return platform_driver_register(&gpio_test_driver);
}

static void __exit gpio_test_exit(void)
{
platform_driver_unregister(&gpio_test_driver);
}



module_init(gpio_test_init);
module_exit(gpio_test_exit);