//////////////gpio驱动模板//////////////////////////////
static int major = 0;
static void gpio_setup_cdev(struct cdev *dev, int minor, struct file_operations *fops)
{
int err, devno = MKDEV(major, minor);
cdev_init(dev, fops);
dev->owner = THIS_MODULE;
dev->ops = fops;
err = cdev_add(dev, devno, 1);
if( err )
{
printk(KERN_NOTICE "Error %d adding gpio %d\n", err, minor);
}
}
static struct file_operations gpio_fops =
{
.owner = THIS_MODULE,
.open = gpio_open, /*进行初始化设置*/
.release = gpio_release, /*释放设备*/
.read = gpio_read,
.write = gpio_write,
.ioctl = gpio_ioctl, /*实现主要控制功能*/
};
static struct cdev gpio_devs;
static int gpio_init(void)
{
int result;
dev_t dev = MKDEV(major, 0);
if( major )
{/*设备号注册*/
result = register_chrdev_region(dev, 1, GPIO_DEVICE_NAME);
}else
{/*设备号动态分配*/
result = alloc_chrdev_region(&dev, 0, 1, GPIO_DEVICE_NAME);
major = MAJOR(dev);
}
gpio_setup_cdev(&gpio_devs, 0, &gpio_fops);
printk("The major of the gpio device is %d\n", major);
return 0;
}
static void gpio_cleanup(void)
{
cdev_del(&gpio_devs);/*字符设备的注销*/
unregister_chrdev_region(MKDEV(major, 0), 1);/*设备号的注销*/
printk("gpio device uninstalled\n");
}
module_init(gpio_init);
module_exit(gpio_cleanup);
MODULE_AUTHOR("transistor");
MODULE_LICENSE("GPL");
///////////end gpio驱动模板//////////////////////////////////////////////
insmod *.ko #加载驱动
rmmod *.ko #卸载驱动
阅读(2252) | 评论(3) | 转发(0) |