Re: [PATCH v2] HID: logitech-dj: issue udev change event on device connection

From: Bastien Nocera
Date: Tue Mar 24 2020 - 06:20:28 EST


On Wed, 2020-03-18 at 19:27 +0000, Filipe LaÃns wrote:
> As discussed in the mailing list:
>
> > Right now the hid-logitech-dj driver will export one node for each
> > connected device, even when the device is not connected. That
> > causes
> > some trouble because in userspace we don't have have any way to
> > know if
> > the device is connected or not, so when we try to communicate, if
> > the
> > device is disconnected it will fail.

Why is it a problem that user-space communication fails? Note that
sending a signal without any way to fetch the state means that it's
always going to be racy.

> The solution reached to solve this issue is to trigger an udev change
> event when the device connects, this way userspace can just wait on
> those connections instead of trying to ping the device.
>
> Signed-off-by: Filipe LaÃns <lains@xxxxxxxxxxxxx>
>
> ---
>
> v2:
> - Issue udev change event on the connected hid device, not on the
> receiver
>
> ---
> drivers/hid/hid-logitech-dj.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hid/hid-logitech-dj.c b/drivers/hid/hid-
> logitech-dj.c
> index 48dff5d6b605..282e57dd467d 100644
> --- a/drivers/hid/hid-logitech-dj.c
> +++ b/drivers/hid/hid-logitech-dj.c
> @@ -1412,6 +1412,7 @@ static int logi_dj_dj_event(struct hid_device
> *hdev,
> {
> struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
> struct dj_report *dj_report = (struct dj_report *) data;
> + struct dj_device *dj_dev;
> unsigned long flags;
>
> /*
> @@ -1447,7 +1448,9 @@ static int logi_dj_dj_event(struct hid_device
> *hdev,
>
> spin_lock_irqsave(&djrcv_dev->lock, flags);
>
> - if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
> + dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
> +
> + if (!dj_dev) {
> /* received an event for an unknown device, bail out */
> logi_dj_recv_queue_notification(djrcv_dev, dj_report);
> goto out;
> @@ -1464,6 +1467,8 @@ static int logi_dj_dj_event(struct hid_device
> *hdev,
> if (dj_report-
> >report_params[CONNECTION_STATUS_PARAM_STATUS] ==
> STATUS_LINKLOSS) {
> logi_dj_recv_forward_null_report(djrcv_dev,
> dj_report);
> + } else {
> + kobject_uevent(&dj_dev->hdev->dev.kobj,
> KOBJ_CHANGE);
> }
> break;
> default: