/
fu-logitech-hidpp-runtime-bolt.c
496 lines (451 loc) · 16 KB
/
fu-logitech-hidpp-runtime-bolt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Copyright 2021 Ricardo Cañuelo <ricardo.canuelo@collabora.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "fu-logitech-hidpp-common.h"
#include "fu-logitech-hidpp-device.h"
#include "fu-logitech-hidpp-hidpp.h"
#include "fu-logitech-hidpp-radio.h"
#include "fu-logitech-hidpp-runtime-bolt.h"
#include "fu-logitech-hidpp-struct.h"
struct _FuLogitechHidppRuntimeBolt {
FuLogitechHidppRuntime parent_instance;
guint8 pairing_slots;
};
G_DEFINE_TYPE(FuLogitechHidppRuntimeBolt, fu_logitech_hidpp_runtime_bolt, FU_TYPE_HIDPP_RUNTIME)
static gboolean
fu_logitech_hidpp_runtime_bolt_detach(FuDevice *device, FuProgress *progress, GError **error)
{
FuLogitechHidppRuntime *self = FU_HIDPP_RUNTIME(device);
g_autoptr(FuLogitechHidppHidppMsg) msg = fu_logitech_hidpp_msg_new();
g_autoptr(GError) error_local = NULL;
msg->report_id = FU_LOGITECH_HIDPP_REPORT_ID_LONG;
msg->device_id = FU_LOGITECH_HIDPP_DEVICE_IDX_RECEIVER;
msg->sub_id = FU_LOGITECH_HIDPP_SUBID_SET_LONG_REGISTER;
msg->function_id = BOLT_REGISTER_DFU_CONTROL;
msg->data[0] = 1; /* Enable DFU */
msg->data[4] = 'P';
msg->data[5] = 'R';
msg->data[6] = 'E';
msg->hidpp_version = 1;
msg->flags = FU_LOGITECH_HIDPP_HIDPP_MSG_FLAG_LONGER_TIMEOUT;
if (!fu_logitech_hidpp_send(fu_logitech_hidpp_runtime_get_io_channel(self),
msg,
FU_LOGITECH_HIDPP_DEVICE_TIMEOUT_MS,
&error_local)) {
if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_WRITE)) {
g_debug("failed to detach to bootloader: %s", error_local->message);
} else {
g_prefix_error(&error_local, "failed to detach to bootloader: ");
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
}
fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
return TRUE;
}
static void
fu_logitech_hidpp_runtime_bolt_to_string(FuDevice *device, guint idt, GString *str)
{
FuLogitechHidppRuntimeBolt *self = FU_HIDPP_RUNTIME_BOLT(device);
fwupd_codec_string_append_int(str, idt, "PairingSlots", self->pairing_slots);
}
static FuLogitechHidppDevice *
fu_logitech_hidpp_runtime_bolt_find_paired_device(FuDevice *device, guint16 hidpp_pid)
{
GPtrArray *children = fu_device_get_children(device);
for (guint i = 0; i < children->len; i++) {
FuDevice *child = g_ptr_array_index(children, i);
if (FU_IS_HIDPP_DEVICE(child) &&
fu_logitech_hidpp_device_get_hidpp_pid(FU_HIDPP_DEVICE(child)) == hidpp_pid)
return FU_HIDPP_DEVICE(g_object_ref(child));
}
return NULL;
}
static gchar *
fu_logitech_hidpp_runtime_bolt_query_device_name(FuLogitechHidppRuntime *self,
guint8 slot,
GError **error)
{
g_autoptr(FuLogitechHidppHidppMsg) msg = fu_logitech_hidpp_msg_new();
g_autoptr(GString) dev_name = g_string_new(NULL);
guint namelen;
msg->report_id = FU_LOGITECH_HIDPP_REPORT_ID_SHORT;
msg->device_id = FU_LOGITECH_HIDPP_DEVICE_IDX_RECEIVER;
msg->sub_id = FU_LOGITECH_HIDPP_SUBID_GET_LONG_REGISTER;
msg->function_id = BOLT_REGISTER_PAIRING_INFORMATION;
msg->data[0] = 0x60 | slot; /* device name */
msg->data[1] = 1;
msg->hidpp_version = 1;
if (!fu_logitech_hidpp_transfer(fu_logitech_hidpp_runtime_get_io_channel(self),
msg,
error)) {
g_prefix_error(error, "failed to retrieve the device name for slot %d: ", slot);
return NULL;
}
namelen = msg->data[2];
g_string_append_len(dev_name, (const char *)(&(msg->data[3])), namelen);
return g_string_free(g_steal_pointer(&dev_name), FALSE);
}
static gboolean
fu_logitech_hidpp_runtime_bolt_update_paired_device(FuLogitechHidppRuntimeBolt *self,
FuLogitechHidppHidppMsg *msg,
GError **error)
{
FuLogitechHidppRuntime *runtime = FU_HIDPP_RUNTIME(self);
gboolean reachable = FALSE;
guint16 hidpp_pid;
g_autoptr(FuLogitechHidppDevice) child = NULL;
if ((msg->data[0] & 0x40) == 0)
reachable = TRUE;
hidpp_pid = (msg->data[1] << 8) | msg->data[2];
child = fu_logitech_hidpp_runtime_bolt_find_paired_device(FU_DEVICE(self), hidpp_pid);
if (child != NULL) {
g_debug("%s [%s] is reachable:%i",
fu_device_get_name(FU_DEVICE(child)),
fu_device_get_name(FU_DEVICE(child)),
reachable);
if (reachable) {
g_autoptr(FuDeviceLocker) locker = NULL;
/* known paired & reachable */
fu_device_probe_invalidate(FU_DEVICE(child));
locker = fu_device_locker_new(FU_DEVICE(child), error);
if (locker == NULL) {
g_prefix_error(error, "cannot rescan paired device: ");
return FALSE;
}
fu_device_remove_flag(FU_DEVICE(child), FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
} else {
GPtrArray *children = NULL;
/* any successful 'ping' will clear this */
fu_device_add_flag(FU_DEVICE(child), FWUPD_DEVICE_FLAG_UNREACHABLE);
children = fu_device_get_children(FU_DEVICE(child));
for (guint i = 0; i < children->len; i++) {
FuDevice *radio = g_ptr_array_index(children, i);
fu_device_add_flag(radio, FWUPD_DEVICE_FLAG_UNREACHABLE);
}
}
} else if (reachable) {
g_autofree gchar *name = NULL;
/* unknown paired device, reachable state */
name = fu_logitech_hidpp_runtime_bolt_query_device_name(runtime,
msg->device_id,
error);
if (name == NULL)
return FALSE;
child = fu_logitech_hidpp_device_new(FU_UDEV_DEVICE(self));
fu_device_set_name(FU_DEVICE(child), name);
fu_logitech_hidpp_device_set_device_idx(child, msg->device_id);
fu_logitech_hidpp_device_set_hidpp_pid(child, hidpp_pid);
if (!fu_device_probe(FU_DEVICE(child), error))
return FALSE;
if (!fu_device_setup(FU_DEVICE(child), error))
return FALSE;
fu_device_add_child(FU_DEVICE(self), FU_DEVICE(child));
} else {
/* unknown paired device, unreachable state */
g_warning("unknown paired device 0x%0x in slot %d (unreachable)",
hidpp_pid,
msg->device_id);
}
return TRUE;
}
static void
fu_logitech_hidpp_runtime_bolt_poll_peripherals(FuDevice *device)
{
FuLogitechHidppRuntime *self = FU_HIDPP_RUNTIME(device);
FuLogitechHidppRuntimeBolt *bolt = FU_HIDPP_RUNTIME_BOLT(device);
for (guint i = 1; i <= bolt->pairing_slots; i++) {
g_autofree gchar *name = NULL;
g_autoptr(FuLogitechHidppHidppMsg) msg = fu_logitech_hidpp_msg_new();
g_autoptr(GError) error_local = NULL;
guint16 hidpp_pid;
name = fu_logitech_hidpp_runtime_bolt_query_device_name(self, i, &error_local);
if (name == NULL) {
g_debug("cannot query paired device name for slot %u", i);
continue;
}
msg->report_id = FU_LOGITECH_HIDPP_REPORT_ID_SHORT;
msg->device_id = FU_LOGITECH_HIDPP_DEVICE_IDX_RECEIVER;
msg->sub_id = FU_LOGITECH_HIDPP_SUBID_GET_LONG_REGISTER;
msg->function_id = BOLT_REGISTER_PAIRING_INFORMATION;
msg->data[0] = 0x50 | i; /* pairing information */
msg->hidpp_version = 1;
if (!fu_logitech_hidpp_transfer(fu_logitech_hidpp_runtime_get_io_channel(self),
msg,
&error_local))
continue;
hidpp_pid = (msg->data[2] << 8) | msg->data[3];
if ((msg->data[1] & 0x40) == 0) {
/* paired device is reachable */
g_autoptr(FuLogitechHidppDevice) child = NULL;
child = fu_logitech_hidpp_device_new(FU_UDEV_DEVICE(device));
fu_device_set_install_duration(FU_DEVICE(child), 270);
fu_device_add_private_flag(FU_DEVICE(child),
FU_LOGITECH_HIDPP_DEVICE_FLAG_ADD_RADIO);
fu_device_set_name(FU_DEVICE(child), name);
fu_logitech_hidpp_device_set_device_idx(child, i);
fu_logitech_hidpp_device_set_hidpp_pid(child, hidpp_pid);
if (!fu_device_probe(FU_DEVICE(child), &error_local))
continue;
if (!fu_device_setup(FU_DEVICE(child), &error_local))
continue;
fu_device_add_child(device, FU_DEVICE(child));
}
}
}
static gboolean
fu_logitech_hidpp_runtime_bolt_process_notification(FuLogitechHidppRuntimeBolt *self,
FuLogitechHidppHidppMsg *msg)
{
g_autoptr(GError) error_local = NULL;
/* HID++1.0 error */
if (!fu_logitech_hidpp_msg_is_error(msg, &error_local)) {
g_warning("failed to get pending read: %s", error_local->message);
return TRUE;
}
/* unifying receiver notification */
if (msg->report_id == FU_LOGITECH_HIDPP_REPORT_ID_SHORT) {
switch (msg->sub_id) {
case FU_LOGITECH_HIDPP_SUBID_DEVICE_CONNECTION:
case FU_LOGITECH_HIDPP_SUBID_DEVICE_DISCONNECTION:
case FU_LOGITECH_HIDPP_SUBID_DEVICE_LOCKING_CHANGED:
if (!fu_logitech_hidpp_runtime_bolt_update_paired_device(self,
msg,
&error_local)) {
g_warning("failed to update paired device status: %s",
error_local->message);
return FALSE;
}
break;
case FU_LOGITECH_HIDPP_SUBID_LINK_QUALITY:
g_debug("ignoring link quality message");
break;
case FU_LOGITECH_HIDPP_SUBID_ERROR_MSG:
g_debug("ignoring error message");
break;
default:
g_debug("unknown SubID %02x", msg->sub_id);
break;
}
}
return TRUE;
}
static FuLogitechHidppHidppMsg *
fu_logitech_hidpp_runtime_bolt_find_newest_msg(GPtrArray *msgs, guint8 device_id, guint8 sub_id)
{
for (guint i = 0; i < msgs->len; i++) {
FuLogitechHidppHidppMsg *msg = g_ptr_array_index(msgs, msgs->len - (i + 1));
if (msg->device_id == device_id && msg->sub_id == sub_id)
return msg;
}
return NULL;
}
static gboolean
fu_logitech_hidpp_runtime_bolt_poll(FuDevice *device, GError **error)
{
FuLogitechHidppRuntime *runtime = FU_HIDPP_RUNTIME(device);
FuLogitechHidppRuntimeBolt *self = FU_HIDPP_RUNTIME_BOLT(device);
const guint timeout = 1; /* ms */
g_autoptr(GPtrArray) msgs = g_ptr_array_new_with_free_func(g_free);
/* open -- not a locker as we have no kernel driver */
if (!fu_device_open(device, error))
return FALSE;
/* drain all the pending messages into the array */
while (TRUE) {
g_autoptr(FuLogitechHidppHidppMsg) msg = fu_logitech_hidpp_msg_new();
g_autoptr(GError) error_local = NULL;
msg->hidpp_version = 1;
if (!fu_logitech_hidpp_receive(fu_logitech_hidpp_runtime_get_io_channel(runtime),
msg,
timeout,
&error_local)) {
if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_TIMED_OUT))
break;
g_propagate_prefixed_error(error,
g_steal_pointer(&error_local),
"error polling Bolt receiver: ");
return FALSE;
}
g_ptr_array_add(msgs, g_steal_pointer(&msg));
}
/* process messages in order, but discard any message with a newer version */
for (guint i = 0; i < msgs->len; i++) {
FuLogitechHidppHidppMsg *msg = g_ptr_array_index(msgs, i);
FuLogitechHidppHidppMsg *msg_newest;
/* find the newest message with the matching device and sub-IDs */
msg_newest = fu_logitech_hidpp_runtime_bolt_find_newest_msg(msgs,
msg->device_id,
msg->sub_id);
if (msg != msg_newest) {
g_debug("ignoring duplicate message device-id:%02x [%s] sub-id:%02x [%s]",
msg->device_id,
fu_logitech_hidpp_device_idx_to_string(msg->device_id),
msg->sub_id,
fu_logitech_hidpp_subid_to_string(msg->sub_id));
continue;
}
fu_logitech_hidpp_runtime_bolt_process_notification(self, msg);
}
return TRUE;
}
static gboolean
fu_logitech_hidpp_runtime_bolt_setup_internal(FuDevice *device, GError **error)
{
FuContext *ctx = fu_device_get_context(device);
FuLogitechHidppRuntime *self = FU_HIDPP_RUNTIME(device);
FuLogitechHidppRuntimeBolt *bolt = FU_HIDPP_RUNTIME_BOLT(device);
g_autoptr(FuLogitechHidppHidppMsg) msg = fu_logitech_hidpp_msg_new();
msg->report_id = FU_LOGITECH_HIDPP_REPORT_ID_SHORT;
msg->device_id = FU_LOGITECH_HIDPP_DEVICE_IDX_RECEIVER;
msg->sub_id = FU_LOGITECH_HIDPP_SUBID_GET_LONG_REGISTER;
msg->function_id = BOLT_REGISTER_PAIRING_INFORMATION;
msg->data[0] = 0x02; /* FW Version (contains the number of pairing slots) */
msg->hidpp_version = 1;
if (!fu_logitech_hidpp_transfer(fu_logitech_hidpp_runtime_get_io_channel(self),
msg,
error)) {
g_prefix_error(error, "failed to fetch the number of pairing slots: ");
return FALSE;
}
bolt->pairing_slots = msg->data[8];
/*
* TODO: Iterate only over the first three entity indexes for
* now.
*/
for (guint i = 0; i < 3; i++) {
guint16 version_raw = 0;
g_autofree gchar *version = NULL;
g_autoptr(FuLogitechHidppRadio) radio = NULL;
g_autoptr(GString) radio_version = NULL;
msg->report_id = FU_LOGITECH_HIDPP_REPORT_ID_SHORT;
msg->device_id = FU_LOGITECH_HIDPP_DEVICE_IDX_RECEIVER;
msg->sub_id = FU_LOGITECH_HIDPP_SUBID_GET_LONG_REGISTER;
msg->function_id = BOLT_REGISTER_RECEIVER_FW_INFORMATION;
msg->data[0] = i;
msg->hidpp_version = 1;
if (!fu_logitech_hidpp_transfer(fu_logitech_hidpp_runtime_get_io_channel(self),
msg,
error)) {
g_prefix_error(error, "failed to read device config: ");
return FALSE;
}
switch (msg->data[0]) {
case 0:
/* main application */
if (!fu_memread_uint16_safe(msg->data,
sizeof(msg->data),
0x03,
&version_raw,
G_BIG_ENDIAN,
error))
return FALSE;
version = fu_logitech_hidpp_format_version("MPR",
msg->data[1],
msg->data[2],
version_raw);
fu_device_set_version(device, version);
break;
case 1:
/* bootloader */
if (!fu_memread_uint16_safe(msg->data,
sizeof(msg->data),
0x03,
&version_raw,
G_BIG_ENDIAN,
error))
return FALSE;
version = fu_logitech_hidpp_format_version("BOT",
msg->data[1],
msg->data[2],
version_raw);
fu_device_set_version_bootloader(device, version);
break;
case 5:
/* SoftDevice */
radio_version = g_string_new(NULL);
radio = fu_logitech_hidpp_radio_new(ctx, i);
fu_device_add_instance_u16(
FU_DEVICE(radio),
"VEN",
fu_udev_device_get_vendor(FU_UDEV_DEVICE(device)));
fu_device_add_instance_u16(
FU_DEVICE(radio),
"DEV",
fu_udev_device_get_model(FU_UDEV_DEVICE(device)));
fu_device_add_instance_u8(FU_DEVICE(radio), "ENT", msg->data[0]);
fu_device_set_physical_id(FU_DEVICE(radio),
fu_device_get_physical_id(device));
fu_device_set_logical_id(FU_DEVICE(radio), "Receiver_SoftDevice");
if (!fu_device_build_instance_id(FU_DEVICE(radio),
error,
"HIDRAW",
"VEN",
"DEV",
"ENT",
NULL))
return FALSE;
if (!fu_memread_uint16_safe(msg->data,
sizeof(msg->data),
0x03,
&version_raw,
G_BIG_ENDIAN,
error))
return FALSE;
g_string_append_printf(radio_version, "0x%.4x", version_raw);
fu_device_set_version(FU_DEVICE(radio), radio_version->str);
fu_device_add_child(device, FU_DEVICE(radio));
break;
default:
break;
}
}
/* enable HID++ notifications */
if (!fu_logitech_hidpp_runtime_enable_notifications(self, error)) {
g_prefix_error(error, "failed to enable notifications: ");
return FALSE;
}
fu_logitech_hidpp_runtime_bolt_poll_peripherals(device);
/* success */
return TRUE;
}
static gboolean
fu_logitech_hidpp_runtime_bolt_setup(FuDevice *device, GError **error)
{
g_autoptr(GError) error_local = NULL;
for (guint i = 0; i < 5; i++) {
g_clear_error(&error_local);
/* HID++1.0 devices have to sleep to allow Solaar to talk to
* the device first -- we can't use the SwID as this is a
* HID++2.0 feature */
fu_device_sleep(device, 200); /* ms */
if (fu_logitech_hidpp_runtime_bolt_setup_internal(device, &error_local))
return TRUE;
if (!g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA)) {
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
}
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
static void
fu_logitech_hidpp_runtime_bolt_class_init(FuLogitechHidppRuntimeBoltClass *klass)
{
FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
device_class->detach = fu_logitech_hidpp_runtime_bolt_detach;
device_class->setup = fu_logitech_hidpp_runtime_bolt_setup;
device_class->poll = fu_logitech_hidpp_runtime_bolt_poll;
device_class->to_string = fu_logitech_hidpp_runtime_bolt_to_string;
}
static void
fu_logitech_hidpp_runtime_bolt_init(FuLogitechHidppRuntimeBolt *self)
{
fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_SIGNED_PAYLOAD);
fu_device_set_remove_delay(FU_DEVICE(self), FU_DEVICE_REMOVE_DELAY_USER_REPLUG);
fu_device_set_vendor(FU_DEVICE(self), "Logitech");
fu_device_set_name(FU_DEVICE(self), "Bolt Receiver");
fu_device_add_protocol(FU_DEVICE(self), "com.logitech.unifyingsigned");
}