diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c index 4f15eb951039..32927b20bcfe 100644 --- a/drivers/hid/hid-roccat.c +++ b/drivers/hid/hid-roccat.c @@ -39,8 +39,8 @@ struct roccat_report { struct roccat_device { unsigned int minor; int report_size; - int open; int exist; + struct kref ref; wait_queue_head_t wait; struct device *dev; struct hid_device *hid; @@ -70,10 +70,12 @@ static struct roccat_device *devices[ROCCAT_MAX_DEVICES]; /* protects modifications of devices array */ static DEFINE_MUTEX(devices_lock); -static void roccat_free_device(struct roccat_device *device) +static void roccat_free_device(struct kref *ref) { + struct roccat_device *device; int i; + device = container_of(ref, struct roccat_device, ref); for (i = 0; i < ROCCAT_CBUF_SIZE; i++) kfree(device->cbuf[i].value); kfree(device); @@ -177,20 +179,18 @@ static int roccat_open(struct inode *inode, struct file *file) mutex_lock(&device->readers_lock); - if (!device->open++) { + if (list_empty(&device->readers)) { /* power on device on adding first reader */ error = hid_hw_power(device->hid, PM_HINT_FULLON); - if (error < 0) { - --device->open; + if (error < 0) goto exit_err_readers; - } error = hid_hw_open(device->hid); if (error < 0) { hid_hw_power(device->hid, PM_HINT_NORMAL); - --device->open; goto exit_err_readers; } + kref_get(&device->ref); } reader->device = device; @@ -211,32 +211,28 @@ static int roccat_open(struct inode *inode, struct file *file) static int roccat_release(struct inode *inode, struct file *file) { - unsigned int minor = iminor(inode); struct roccat_reader *reader = file->private_data; - struct roccat_device *device; - - mutex_lock(&devices_lock); + struct roccat_device *device = reader->device; + bool open; - device = devices[minor]; - if (!device) { - mutex_unlock(&devices_lock); - pr_emerg("roccat device with minor %d doesn't exist\n", minor); + if (WARN_ON_ONCE(!device)) return -ENODEV; - } mutex_lock(&device->readers_lock); list_del(&reader->node); + open = !list_empty(&device->readers); mutex_unlock(&device->readers_lock); kfree(reader); - if (!--device->open) { + mutex_lock(&devices_lock); + + if (!open) { /* removing last reader */ if (device->exist) { hid_hw_power(device->hid, PM_HINT_NORMAL); hid_hw_close(device->hid); - } else { - roccat_free_device(device); } + kref_put(&device->ref, roccat_free_device); } mutex_unlock(&devices_lock); @@ -344,18 +340,18 @@ int roccat_connect(const struct class *klass, struct hid_device *hid, int report return temp; } - mutex_unlock(&devices_lock); - init_waitqueue_head(&device->wait); INIT_LIST_HEAD(&device->readers); mutex_init(&device->readers_lock); mutex_init(&device->cbuf_lock); + kref_init(&device->ref); device->minor = minor; device->hid = hid; device->exist = 1; device->cbuf_end = 0; device->report_size = report_size; + mutex_unlock(&devices_lock); return minor; } EXPORT_SYMBOL_GPL(roccat_connect); @@ -366,25 +362,31 @@ EXPORT_SYMBOL_GPL(roccat_connect); void roccat_disconnect(int minor) { struct roccat_device *device; + bool open; mutex_lock(&devices_lock); device = devices[minor]; - mutex_unlock(&devices_lock); + if (!device) + goto out; device->exist = 0; /* TODO exist maybe not needed */ device_destroy(device->dev->class, MKDEV(roccat_major, minor)); - mutex_lock(&devices_lock); devices[minor] = NULL; - mutex_unlock(&devices_lock); - if (device->open) { + mutex_lock(&device->readers_lock); + open = !list_empty(&device->readers); + mutex_unlock(&device->readers_lock); + + if (open) { hid_hw_close(device->hid); wake_up_interruptible(&device->wait); - } else { - roccat_free_device(device); } + + kref_put(&device->ref, roccat_free_device); +out: + mutex_unlock(&devices_lock); } EXPORT_SYMBOL_GPL(roccat_disconnect);