[lvc-project] [PATCH v4 2/4] HID: roccat: use device_is_registered() to check whether device is available
Dmitry Antipov
dmantipov at yandex.ru
Wed Sep 16 18:47:31 MSK 2026
Introduce roccat_device_available() to check whether device
is actually available (i.e. not disconnected), thus removing
explicit 'exist' flag from 'struct roccat_device'.
Signed-off-by: Dmitry Antipov <dmantipov at yandex.ru>
---
v4: use READ_ONCE() and WRITE_ONCE() for device access
v3: initial version to join the series
---
drivers/hid/hid-roccat.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c
index 5deb6da8d4f7..96aa508111a1 100644
--- a/drivers/hid/hid-roccat.c
+++ b/drivers/hid/hid-roccat.c
@@ -40,7 +40,6 @@ struct roccat_device {
unsigned int minor;
int report_size;
int open;
- int exist;
wait_queue_head_t wait;
struct device *dev;
struct hid_device *hid;
@@ -70,6 +69,13 @@ static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
/* protects modifications of devices array */
static DEFINE_MUTEX(devices_lock);
+static bool roccat_device_available(struct roccat_device *device)
+{
+ struct device *dev = READ_ONCE(device->dev);
+
+ return dev ? device_is_registered(dev) : false;
+}
+
static void roccat_free_device(struct roccat_device *device)
{
int i;
@@ -105,7 +111,7 @@ static ssize_t roccat_read(struct file *file, char __user *buffer,
retval = -ERESTARTSYS;
break;
}
- if (!device->exist) {
+ if (!roccat_device_available(device)) {
retval = -EIO;
break;
}
@@ -149,7 +155,7 @@ static __poll_t roccat_poll(struct file *file, poll_table *wait)
poll_wait(file, &reader->device->wait, wait);
if (reader->cbuf_start != reader->device->cbuf_end)
return EPOLLIN | EPOLLRDNORM;
- if (!reader->device->exist)
+ if (!roccat_device_available(reader->device))
return EPOLLERR | EPOLLHUP;
return 0;
}
@@ -231,7 +237,7 @@ static int roccat_release(struct inode *inode, struct file *file)
if (!--device->open) {
/* removing last reader */
- if (device->exist) {
+ if (roccat_device_available(device)) {
hid_hw_power(device->hid, PM_HINT_NORMAL);
hid_hw_close(device->hid);
} else {
@@ -350,7 +356,6 @@ int roccat_connect(const struct class *klass, struct hid_device *hid, int report
mutex_init(&device->cbuf_lock);
device->minor = minor;
device->hid = hid;
- device->exist = 1;
device->cbuf_end = 0;
device->report_size = report_size;
@@ -369,10 +374,8 @@ void roccat_disconnect(int minor)
mutex_lock(&devices_lock);
device = devices[minor];
- device->exist = 0; /* TODO exist maybe not needed */
-
device_destroy(device->dev->class, MKDEV(roccat_major, minor));
-
+ WRITE_ONCE(device->dev, NULL);
devices[minor] = NULL;
if (device->open) {
--
2.55.0
More information about the lvc-project
mailing list