cdev_init(&dev_c,&globalvar_fops);
err = cdev_add(&dev_c,dev,1);
if(err)
{
printk(KERN_NOTICE "error %d adding FC_dev\n",err);
unregister_chrdev_region(dev, 1);
return err;
}
else
{
printk("device register success! \n");
}
cdev_class = class_create(THIS_MODULE,DEVICE_NAME);
if(IS_ERR(cdev_class))
{
printk("ERR:cannot create a cdev_class\n");
unregister_chrdev_region(dev, 1);
return -1;
}
device_create(cdev_class, NULL, dev, 0, DEVICE_NAME);
init_MUTEX(&sem);
init_waitqueue_head(&outq);
}
return ret;
}
static void __exit globalvar_exit(void)
{
device_destroy(cdev_class,dev);
class_destroy(cdev_class);
unregister_chrdev_region(dev,1);
printk("device ");
}
static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off)
{
if (wait_event_interruptible(outq, flag != 0))
{
return - ERESTARTSYS;
}
if (down_interruptible(&sem))
{
return - ERESTARTSYS;
}
flag = 0;
if (copy_to_user(buf, &global_var, sizeof(int)))
{
up(&sem);
return - EFAULT;
}
up(&sem);
return sizeof(int);
}
static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t *off)
{
if (down_interruptible(&sem))
{
return - ERESTARTSYS;
}
if (copy_from_user(&global_var, buf, sizeof(int)))
{
up(&sem);
return - EFAULT;
}
up(&sem);
flag = 1;
wake_up_interruptible(&outq);
return sizeof(int);
}
static unsigned int globalvar_poll(struct file *filp, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(filp, &outq, wait);
if(flag != 0)
{
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
module_init(globalvar_init);
module_exit(globalvar_exit);
驱动支持select函数(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/8b967cff3231c4939ad5eda8dcfe18b4.html