Debian Codegraph @

src/qmi-firmware-update/qfu-helpers-udev.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmi-firmware-update -- Command line tool to update firmware in QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2016 Zodiac Inflight Innovations
19 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <gio/gio.h>
25
26 #if !defined WITH_UDEV
27 # error udev is required
28 #endif
29
30 #include "qfu-helpers.h"
31 #include "qfu-helpers-udev.h"
32 #include <gudev/gudev.h>
33
34 /******************************************************************************/
35
36 #if defined WITH_UDEV
37
38 static const gchar *tty_subsys_list[] = { "tty", NULL };
39 static const gchar *cdc_wdm_subsys_list[] = { "usbmisc", "usb", NULL };
40
41 static gboolean
42 get_device_details (GUdevDevice *device,
43 gchar **out_sysfs_path,
44 QfuHelpersDeviceMode *out_device_mode,
45 guint16 *out_vid,
46 guint16 *out_pid,
47 guint *out_busnum,
48 guint *out_devnum,
49 GError **error)
50 {
51 GUdevDevice *parent;
52 gulong aux;
53
54 if (out_vid)
55 *out_vid = 0;
56 if (out_pid)
57 *out_pid = 0;
58 if (out_sysfs_path)
59 *out_sysfs_path = NULL;
60 if (out_busnum)
61 *out_busnum = 0;
62 if (out_devnum)
63 *out_devnum = 0;
64 if (out_device_mode)
65 *out_device_mode = QFU_HELPERS_DEVICE_MODE_UNKNOWN;
66
67 /* We need to look for the parent GUdevDevice which has a "usb_device"
68 * devtype. */
69
70 parent = g_udev_device_get_parent (device);
71 while (parent) {
72 GUdevDevice *next;
73
74 if (g_strcmp0 (g_udev_device_get_devtype (parent), "usb_device") == 0)
75 break;
76
77 /* Check next parent */
78 next = g_udev_device_get_parent (parent);
79 g_object_unref (parent);
80 parent = next;
81 }
82
83 if (!parent) {
84 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't find parent physical USB device");
85 return FALSE;
86 }
87
88 if (out_sysfs_path)
89 *out_sysfs_path = g_strdup (g_udev_device_get_sysfs_path (parent));
90
91 if (out_device_mode) {
92 aux = strtoul (g_udev_device_get_sysfs_attr (parent, "bNumInterfaces"), NULL, 10);
93 /* QDL download mode has exactly one single USB interface */
94 if (aux == 1)
95 *out_device_mode = QFU_HELPERS_DEVICE_MODE_DOWNLOAD;
96 else if (aux > 1)
97 *out_device_mode = QFU_HELPERS_DEVICE_MODE_MODEM;
98 }
99
100 if (out_vid) {
101 aux = strtoul (g_udev_device_get_property (parent, "ID_VENDOR_ID"), NULL, 16);
102 if (aux <= G_MAXUINT16)
103 *out_vid = (guint16) aux;
104 }
105
106 if (out_pid) {
107 aux = strtoul (g_udev_device_get_property (parent, "ID_MODEL_ID"), NULL, 16);
108 if (aux <= G_MAXUINT16)
109 *out_pid = (guint16) aux;
110 }
111
112 if (out_busnum) {
113 aux = strtoul (g_udev_device_get_property (parent, "BUSNUM"), NULL, 10);
114 if (aux <= G_MAXUINT)
115 *out_busnum = (guint16) aux;
116 }
117
118 if (out_devnum) {
119 aux = strtoul (g_udev_device_get_property (parent, "DEVNUM"), NULL, 10);
120 if (aux <= G_MAXUINT)
121 *out_devnum = (guint16) aux;
122 }
123
124 g_object_unref (parent);
125 return TRUE;
126 }
127
128 static gboolean
129 udev_helper_get_udev_interface_details (GUdevDevice *device,
130 gchar **out_driver,
131 GError **error)
132 {
133 GUdevDevice *parent;
134
135 /* We need to look for the parent GUdevDevice which has a "usb_interface"
136 * devtype. */
137
138 parent = g_udev_device_get_parent (device);
139 while (parent) {
140 GUdevDevice *next;
141
142 if (g_strcmp0 (g_udev_device_get_devtype (parent), "usb_interface") == 0)
143 break;
144
145 /* Check next parent */
146 next = g_udev_device_get_parent (parent);
147 g_object_unref (parent);
148 parent = next;
149 }
150
151 if (!parent) {
152 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't find parent interface USB device");
153 return FALSE;
154 }
155
156 if (out_driver)
157 *out_driver = g_strdup (g_udev_device_get_driver (parent));
158
159 g_object_unref (parent);
160 return TRUE;
161 }
162
163 /******************************************************************************/
164
165 gchar *
166 qfu_helpers_udev_find_by_file (GFile *file,
167 GError **error)
168 {
169 GUdevClient *client = NULL;
170 GUdevDevice *device = NULL;
171 gchar *basename = NULL;
172 const gchar **subsys_list = NULL;
173 gchar *sysfs_path = NULL;
174 guint i;
175
176 basename = g_file_get_basename (file);
177 if (!basename) {
178 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't get filename");
179 goto out;
180 }
181
182 client = g_udev_client_new (NULL);
183
184 if (g_str_has_prefix (basename, "tty"))
185 subsys_list = tty_subsys_list;
186 else if (g_str_has_prefix (basename, "cdc-wdm"))
187 subsys_list = cdc_wdm_subsys_list;
188 else {
189 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unknown device file type");
190 goto out;
191 }
192
193 for (i = 0; !device && subsys_list[i]; i++)
194 device = g_udev_client_query_by_subsystem_and_name (client, subsys_list[i], basename);
195
196 if (!device) {
197 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "device not found");
198 goto out;
199 }
200
201 if (!get_device_details (device,
202 &sysfs_path, NULL, NULL, NULL, NULL, NULL,
203 error))
204 goto out;
205
206 g_debug ("[qfu-udev] sysfs path for '%s' found: %s", basename, sysfs_path);
207
208 out:
209 g_free (basename);
210 g_clear_object (&device);
211 g_clear_object (&client);
212 return sysfs_path;
213 }
214
215 /******************************************************************************/
216
217 static gboolean
218 udev_helper_device_already_added (GPtrArray *ptr,
219 const gchar *sysfs_path)
220 {
221 guint i;
222
223 for (i = 0; i < ptr->len; i++) {
224 if (g_strcmp0 (g_ptr_array_index (ptr, i), sysfs_path) == 0)
225 return TRUE;
226 }
227
228 return FALSE;
229 }
230
231 static GPtrArray *
232 udev_helper_find_by_device_info_in_subsystem (GPtrArray *sysfs_paths,
233 GUdevClient *udev,
234 const gchar *subsystem,
235 guint16 vid,
236 guint16 pid,
237 guint busnum,
238 guint devnum)
239 {
240 GList *devices;
241 GList *iter;
242
243 devices = g_udev_client_query_by_subsystem (udev, subsystem);
244 for (iter = devices; iter; iter = g_list_next (iter)) {
245 GUdevDevice *device;
246 guint16 device_vid = 0;
247 guint16 device_pid = 0;
248 guint device_busnum = 0;
249 guint device_devnum = 0;
250 gchar *device_sysfs_path = NULL;
251
252 device = G_UDEV_DEVICE (iter->data);
253
254 if (get_device_details (device,
255 &device_sysfs_path,
256 NULL,
257 &device_vid,
258 &device_pid,
259 &device_busnum,
260 &device_devnum,
261 NULL)) {
262 if ((vid == 0 || vid == device_vid) &&
263 (pid == 0 || pid == device_pid) &&
264 (busnum == 0 || busnum == device_busnum) &&
265 (devnum == 0 || devnum == device_devnum) &&
266 (!udev_helper_device_already_added (sysfs_paths, device_sysfs_path)))
267 g_ptr_array_add (sysfs_paths, device_sysfs_path);
268 else
269 g_free (device_sysfs_path);
270 }
271
272 g_object_unref (device);
273 }
274 g_list_free (devices);
275 return sysfs_paths;
276 }
277
278 gchar *
279 qfu_helpers_udev_find_by_device_info (guint16 vid,
280 guint16 pid,
281 guint busnum,
282 guint devnum,
283 GError **error)
284 {
285 GUdevClient *udev;
286 guint i;
287 GPtrArray *sysfs_paths;
288 GString *match_str;
289 gchar *sysfs_path = NULL;
290
291 sysfs_paths = g_ptr_array_new_with_free_func (g_free);
292 udev = g_udev_client_new (NULL);
293 match_str = g_string_new ("");
294
295 if (vid != 0)
296 g_string_append_printf (match_str, "vid 0x%04x", vid);
297 if (pid != 0)
298 g_string_append_printf (match_str, "%spid 0x%04x", match_str->len > 0 ? ", " : "", pid);
299 if (busnum != 0)
300 g_string_append_printf (match_str, "%sbus %03u", match_str->len > 0 ? ", " : "", busnum);
301 if (devnum != 0)
302 g_string_append_printf (match_str, "%sdev %03u", match_str->len > 0 ? ", " : "", devnum);
303 g_assert (match_str->len > 0);
304
305 for (i = 0; tty_subsys_list[i]; i++)
306 sysfs_paths = udev_helper_find_by_device_info_in_subsystem (sysfs_paths,
307 udev,
308 tty_subsys_list[i],
309 vid, pid, busnum, devnum);
310
311 for (i = 0; cdc_wdm_subsys_list[i]; i++)
312 sysfs_paths = udev_helper_find_by_device_info_in_subsystem (sysfs_paths,
313 udev,
314 cdc_wdm_subsys_list[i],
315 vid, pid, busnum, devnum);
316
317 for (i = 0; i < sysfs_paths->len; i++)
318 g_debug ("[%s] sysfs path: %s", match_str->str, (gchar *) g_ptr_array_index (sysfs_paths, i));
319
320 if (sysfs_paths->len == 0) {
321 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
322 "no device found with matching criteria: %s",
323 match_str->str);
324 goto out;
325 }
326
327 if (sysfs_paths->len > 1) {
328 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
329 "multiple devices (%u) found with matching criteria: %s",
330 sysfs_paths->len, match_str->str);
331 goto out;
332 }
333
334 sysfs_path = g_strdup (g_ptr_array_index (sysfs_paths, 0));
335
336 out:
337
338 g_ptr_array_unref (sysfs_paths);
339 g_string_free (match_str, TRUE);
340 g_object_unref (udev);
341
342 return sysfs_path;
343 }
344
345 /******************************************************************************/
346
347 static GFile *
348 device_matches (GUdevDevice *device,
349 QfuHelpersDeviceType type,
350 QfuHelpersDeviceMode mode,
351 const gchar *sysfs_path)
352 {
353 GFile *file = NULL;
354 gchar *device_sysfs_path = NULL;
355 gchar *device_driver = NULL;
356 gchar *device_path = NULL;
357 QfuHelpersDeviceMode device_mode = QFU_HELPERS_DEVICE_MODE_UNKNOWN;
358
359 if (!get_device_details (device,
360 &device_sysfs_path, &device_mode, NULL, NULL, NULL, NULL,
361 NULL))
362 goto out;
363
364 if (!device_sysfs_path)
365 goto out;
366
367 if (g_strcmp0 (device_sysfs_path, sysfs_path) != 0)
368 goto out;
369
370 if (device_mode != mode)
371 return NULL;
372
373 if (!udev_helper_get_udev_interface_details (device,
374 &device_driver,
375 NULL))
376 goto out;
377
378 switch (type) {
379 case QFU_HELPERS_DEVICE_TYPE_TTY:
380 if (g_strcmp0 (device_driver, "qcserial") != 0)
381 goto out;
382 break;
383 case QFU_HELPERS_DEVICE_TYPE_CDC_WDM:
384 if (g_strcmp0 (device_driver, "qmi_wwan") != 0 && g_strcmp0 (device_driver, "cdc_mbim") != 0)
385 goto out;
386 break;
387 case QFU_HELPERS_DEVICE_TYPE_LAST:
388 default:
389 g_assert_not_reached ();
390 }
391
392 device_path = g_strdup_printf ("/dev/%s", g_udev_device_get_name (device));
393 file = g_file_new_for_path (device_path);
394 g_free (device_path);
395
396 out:
397 g_free (device_sysfs_path);
398 g_free (device_driver);
399 return file;
400 }
401
402 GList *
403 qfu_helpers_udev_list_devices (QfuHelpersDeviceType device_type,
404 QfuHelpersDeviceMode device_mode,
405 const gchar *sysfs_path)
406 {
407 GUdevClient *udev;
408 const gchar **subsys_list = NULL;
409 guint i;
410 GList *files = NULL;
411
412 udev = g_udev_client_new (NULL);
413
414 switch (device_type) {
415 case QFU_HELPERS_DEVICE_TYPE_TTY:
416 subsys_list = tty_subsys_list;
417 break;
418 case QFU_HELPERS_DEVICE_TYPE_CDC_WDM:
419 subsys_list = cdc_wdm_subsys_list;
420 break;
421 case QFU_HELPERS_DEVICE_TYPE_LAST:
422 default:
423 g_assert_not_reached ();
424 }
425
426 for (i = 0; subsys_list[i]; i++) {
427 GList *devices, *iter;
428
429 devices = g_udev_client_query_by_subsystem (udev, subsys_list[i]);
430 for (iter = devices; iter; iter = g_list_next (iter)) {
431 GFile *file;
432
433 file = device_matches (G_UDEV_DEVICE (iter->data), device_type, device_mode, sysfs_path);
434 if (file)
435 files = g_list_prepend (files, file);
436 g_object_unref (G_OBJECT (iter->data));
437 }
438 g_list_free (devices);
439 }
440
441 g_object_unref (udev);
442 return files;
443 }
444
445 /******************************************************************************/
446
447 #define WAIT_FOR_DEVICE_TIMEOUT_SECS 120
448
449 typedef struct {
450 QfuHelpersDeviceType device_type;
451 QfuHelpersDeviceMode device_mode;
452 GUdevClient *udev;
453 gchar *sysfs_path;
454 gchar *peer_port;
455 guint timeout_id;
456 gulong uevent_id;
457 gulong cancellable_id;
458 } WaitForDeviceContext;
459
460 static void
461 wait_for_device_context_free (WaitForDeviceContext *ctx)
462 {
463 g_assert (!ctx->timeout_id);
464 g_assert (!ctx->uevent_id);
465 g_assert (!ctx->cancellable_id);
466
467 g_object_unref (ctx->udev);
468 g_free (ctx->sysfs_path);
469 g_free (ctx->peer_port);
470 g_slice_free (WaitForDeviceContext, ctx);
471 }
472
473 GFile *
474 qfu_helpers_udev_wait_for_device_finish (GAsyncResult *res,
475 GError **error)
476 {
477 return G_FILE (g_task_propagate_pointer (G_TASK (res), error));
478 }
479
480 static void
481 handle_uevent (GUdevClient *client,
482 const char *action,
483 GUdevDevice *device,
484 GTask *task)
485 {
486 WaitForDeviceContext *ctx;
487 GFile *file;
488
489 ctx = (WaitForDeviceContext *) g_task_get_task_data (task);
490
491 if (!g_str_equal (action, "add") && !g_str_equal (action, "move") && !g_str_equal (action, "change"))
492 return;
493
494 file = device_matches (device, ctx->device_type, ctx->device_mode, ctx->sysfs_path);
495 if (!file && ctx->peer_port) {
496 gchar *tmp, *path;
497
498 tmp = g_build_filename (ctx->peer_port, "device", NULL);
499 path = realpath (tmp, NULL);
500 g_free (tmp);
501 if (!path)
502 return;
503
504 file = device_matches (device, ctx->device_type, ctx->device_mode, path);
505 g_debug ("[qfu-udev] peer lookup for %s: %s => %s", g_udev_device_get_name (device), ctx->peer_port, path);
506 g_free (path);
507 }
508 if (!file)
509 return;
510
511 g_debug ("[qfu-udev] waiting device (%s) matched: %s",
512 qfu_helpers_device_type_to_string (ctx->device_type),
513 g_udev_device_get_name (device));
514
515 /* Disconnect this handler */
516 g_signal_handler_disconnect (ctx->udev, ctx->uevent_id);
517 ctx->uevent_id = 0;
518
519 /* Disconnect the other handlers */
520 g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id);
521 ctx->cancellable_id = 0;
522 g_source_remove (ctx->timeout_id);
523 ctx->timeout_id = 0;
524
525 g_task_return_pointer (task, file, g_object_unref);
526 g_object_unref (task);
527 }
528
529 static gboolean
530 wait_for_device_timed_out (GTask *task)
531 {
532 WaitForDeviceContext *ctx;
533
534 ctx = (WaitForDeviceContext *) g_task_get_task_data (task);
535
536 /* Disconnect this handler */
537 ctx->timeout_id = 0;
538
539 /* Disconnect the other handlers */
540 g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id);
541 ctx->cancellable_id = 0;
542 g_signal_handler_disconnect (ctx->udev, ctx->uevent_id);
543 ctx->uevent_id = 0;
544
545 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
546 "waiting for device at '%s' timed out",
547 ctx->sysfs_path);
548 g_object_unref (task);
549 return FALSE;
550 }
551
552 static void
553 wait_for_device_cancelled (GCancellable *cancellable,
554 GTask *task)
555 {
556 WaitForDeviceContext *ctx;
557
558 ctx = (WaitForDeviceContext *) g_task_get_task_data (task);
559
560 /* Disconnect this handler */
561 g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id);
562 ctx->cancellable_id = 0;
563
564 /* Disconnect the other handlers */
565 g_source_remove (ctx->timeout_id);
566 ctx->timeout_id = 0;
567 g_signal_handler_disconnect (ctx->udev, ctx->uevent_id);
568 ctx->uevent_id = 0;
569
570 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED,
571 "waiting for device at '%s' cancelled",
572 ctx->sysfs_path);
573 g_object_unref (task);
574 }
575
576 void
577 qfu_helpers_udev_wait_for_device (QfuHelpersDeviceType device_type,
578 QfuHelpersDeviceMode device_mode,
579 const gchar *sysfs_path,
580 const gchar *peer_port,
581 GCancellable *cancellable,
582 GAsyncReadyCallback callback,
583 gpointer user_data)
584 {
585 GTask *task;
586 WaitForDeviceContext *ctx;
587
588 ctx = g_slice_new0 (WaitForDeviceContext);
589 ctx->device_type = device_type;
590 ctx->device_mode = device_mode;
591 ctx->sysfs_path = g_strdup (sysfs_path);
592 ctx->peer_port = g_strdup (peer_port);
593
594 if (ctx->device_type == QFU_HELPERS_DEVICE_TYPE_TTY)
595 ctx->udev = g_udev_client_new (tty_subsys_list);
596 else if (ctx->device_type == QFU_HELPERS_DEVICE_TYPE_CDC_WDM)
597 ctx->udev = g_udev_client_new (cdc_wdm_subsys_list);
598 else
599 g_assert_not_reached ();
600
601 task = g_task_new (NULL, cancellable, callback, user_data);
602 g_task_set_task_data (task, ctx, (GDestroyNotify) wait_for_device_context_free);
603
604 /* Monitor for device additions. */
605 ctx->uevent_id = g_signal_connect (ctx->udev,
606 "uevent",
607 G_CALLBACK (handle_uevent),
608 task);
609
610 /* Allow cancellation */
611 ctx->cancellable_id = g_cancellable_connect (cancellable,
612 (GCallback) wait_for_device_cancelled,
613 task,
614 NULL);
615
616 /* And also, setup a timeout to avoid waiting forever. */
617 ctx->timeout_id = g_timeout_add_seconds (WAIT_FOR_DEVICE_TIMEOUT_SECS,
618 (GSourceFunc) wait_for_device_timed_out,
619 task);
620
621 /* Note: task ownership is shared between the signals and the timeout */
622 }
623
624 /******************************************************************************/
625
626 struct _QfuHelpersUdevGenericMonitor {
627 GUdevClient *udev;
628 };
629
630 void
631 qfu_helpers_udev_generic_monitor_free (QfuHelpersUdevGenericMonitor *self)
632 {
633 g_object_unref (self->udev);
634 g_slice_free (QfuHelpersUdevGenericMonitor, self);
635 }
636
637 static void
638 handle_uevent_generic (GUdevClient *client,
639 const char *action,
640 GUdevDevice *device,
641 GTask *task)
642 {
643 g_debug ("[qfu-udev] event: %s %s", action, g_udev_device_get_name (device));
644 }
645
646 QfuHelpersUdevGenericMonitor *
647 qfu_helpers_udev_generic_monitor_new (const gchar *sysfs_path)
648 {
649 static const gchar *all_list[] = {
650 "usbmisc", "usb",
651 "tty",
652 "net",
653 NULL };
654
655 QfuHelpersUdevGenericMonitor *self;
656
657 self = g_slice_new0 (QfuHelpersUdevGenericMonitor);
658 self->udev = g_udev_client_new (all_list);
659
660 /* Monitor for device events. */
661 g_signal_connect (self->udev, "uevent", G_CALLBACK (handle_uevent_generic), NULL);
662 return self;
663 }
664
665 #endif /* WITH_UDEV */