| 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) 2022 VMware, Inc. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include <stdlib.h> |
| 23 |
#include <gio/gio.h> |
| 24 |
|
| 25 |
#if defined WITH_UDEV |
| 26 |
# error udev found |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "qfu-helpers.h" |
| 30 |
#include "qfu-helpers-sysfs.h" |
| 31 |
|
| 32 |
/******************************************************************************/ |
| 33 |
|
| 34 |
static const gchar *tty_subsys_list[] = { "tty", NULL }; |
| 35 |
static const gchar *cdc_wdm_subsys_list[] = { "usbmisc", "usb", NULL }; |
| 36 |
|
| 37 |
static gboolean |
| 38 |
has_sysfs_attribute (const gchar *path, |
| 39 |
const gchar *attribute) |
| 40 |
{ |
| 41 |
g_autofree gchar *aux_filepath = NULL; |
| 42 |
|
| 43 |
aux_filepath = g_strdup_printf ("%s/%s", path, attribute); |
| 44 |
return g_file_test (aux_filepath, G_FILE_TEST_EXISTS); |
| 45 |
} |
| 46 |
|
| 47 |
static gchar * |
| 48 |
read_sysfs_attribute_as_string (const gchar *path, |
| 49 |
const gchar *attribute) |
| 50 |
{ |
| 51 |
g_autofree gchar *aux = NULL; |
| 52 |
gchar *contents = NULL; |
| 53 |
|
| 54 |
aux = g_strdup_printf ("%s/%s", path, attribute); |
| 55 |
if (g_file_get_contents (aux, &contents, NULL, NULL)) { |
| 56 |
g_strdelimit (contents, "\r\n", ' '); |
| 57 |
g_strstrip (contents); |
| 58 |
} |
| 59 |
return contents; |
| 60 |
} |
| 61 |
|
| 62 |
static gulong |
| 63 |
read_sysfs_attribute_as_num (const gchar *path, |
| 64 |
const gchar *attribute, |
| 65 |
guint base) |
| 66 |
{ |
| 67 |
g_autofree gchar *contents = NULL; |
| 68 |
gulong val = 0; |
| 69 |
|
| 70 |
contents = read_sysfs_attribute_as_string (path, attribute); |
| 71 |
if (contents) |
| 72 |
val = strtoul (contents, NULL, base); |
| 73 |
return val; |
| 74 |
} |
| 75 |
|
| 76 |
static gchar * |
| 77 |
read_sysfs_attribute_link_basename (const gchar *path, |
| 78 |
const gchar *attribute) |
| 79 |
{ |
| 80 |
g_autofree gchar *aux_filepath = NULL; |
| 81 |
g_autofree gchar *canonicalized_path = NULL; |
| 82 |
|
| 83 |
aux_filepath = g_strdup_printf ("%s/%s", path, attribute); |
| 84 |
if (!g_file_test (aux_filepath, G_FILE_TEST_EXISTS)) |
| 85 |
return NULL; |
| 86 |
|
| 87 |
canonicalized_path = realpath (aux_filepath, NULL); |
| 88 |
return g_path_get_basename (canonicalized_path); |
| 89 |
} |
| 90 |
|
| 91 |
static gboolean |
| 92 |
get_device_details (const gchar *port_sysfs_path, |
| 93 |
gchar **out_sysfs_path, |
| 94 |
QfuHelpersDeviceMode *out_device_mode, |
| 95 |
guint16 *out_vid, |
| 96 |
guint16 *out_pid, |
| 97 |
guint *out_busnum, |
| 98 |
guint *out_devnum, |
| 99 |
GError **error) |
| 100 |
{ |
| 101 |
g_autofree gchar *iter = NULL; |
| 102 |
g_autofree gchar *physdev_sysfs_path = NULL; |
| 103 |
gulong aux; |
| 104 |
|
| 105 |
if (out_vid) |
| 106 |
*out_vid = 0; |
| 107 |
if (out_pid) |
| 108 |
*out_pid = 0; |
| 109 |
if (out_busnum) |
| 110 |
*out_busnum = 0; |
| 111 |
if (out_devnum) |
| 112 |
*out_devnum = 0; |
| 113 |
if (out_sysfs_path) |
| 114 |
*out_sysfs_path = NULL; |
| 115 |
if (out_device_mode) |
| 116 |
*out_device_mode = QFU_HELPERS_DEVICE_MODE_UNKNOWN; |
| 117 |
|
| 118 |
iter = realpath (port_sysfs_path, NULL); |
| 119 |
|
| 120 |
while (iter && (g_strcmp0 (iter, "/") != 0)) { |
| 121 |
gchar *parent; |
| 122 |
|
| 123 |
/* is this the USB physdev? */ |
| 124 |
if (has_sysfs_attribute (iter, "idVendor")) { |
| 125 |
/* stop traversing as soon as the physical device is found */ |
| 126 |
physdev_sysfs_path = g_steal_pointer (&iter); |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
parent = g_path_get_dirname (iter); |
| 131 |
g_clear_pointer (&iter, g_free); |
| 132 |
iter = parent; |
| 133 |
} |
| 134 |
|
| 135 |
if (!physdev_sysfs_path) { |
| 136 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't find parent physical USB device"); |
| 137 |
return FALSE; |
| 138 |
} |
| 139 |
|
| 140 |
if (out_device_mode) { |
| 141 |
aux = read_sysfs_attribute_as_num (physdev_sysfs_path, "bNumInterfaces", 10); |
| 142 |
/* QDL download mode has exactly one single USB interface */ |
| 143 |
if (aux == 1) |
| 144 |
*out_device_mode = QFU_HELPERS_DEVICE_MODE_DOWNLOAD; |
| 145 |
else if (aux > 1) |
| 146 |
*out_device_mode = QFU_HELPERS_DEVICE_MODE_MODEM; |
| 147 |
} |
| 148 |
|
| 149 |
if (out_vid) { |
| 150 |
aux = read_sysfs_attribute_as_num (physdev_sysfs_path, "idVendor", 16); |
| 151 |
if (aux <= G_MAXUINT16) |
| 152 |
*out_vid = (guint16) aux; |
| 153 |
} |
| 154 |
|
| 155 |
if (out_pid) { |
| 156 |
aux = read_sysfs_attribute_as_num (physdev_sysfs_path, "idProduct", 16); |
| 157 |
if (aux <= G_MAXUINT16) |
| 158 |
*out_pid = (guint16) aux; |
| 159 |
} |
| 160 |
|
| 161 |
if (out_busnum) { |
| 162 |
aux = read_sysfs_attribute_as_num (physdev_sysfs_path, "busnum", 10); |
| 163 |
if (aux <= G_MAXUINT) |
| 164 |
*out_busnum = (guint16) aux; |
| 165 |
} |
| 166 |
|
| 167 |
if (out_devnum) { |
| 168 |
aux = read_sysfs_attribute_as_num (physdev_sysfs_path, "devnum", 10); |
| 169 |
if (aux <= G_MAXUINT) |
| 170 |
*out_devnum = (guint16) aux; |
| 171 |
} |
| 172 |
|
| 173 |
if (out_sysfs_path) |
| 174 |
*out_sysfs_path = g_steal_pointer (&physdev_sysfs_path); |
| 175 |
|
| 176 |
return TRUE; |
| 177 |
} |
| 178 |
|
| 179 |
static gboolean |
| 180 |
get_interface_details (const gchar *port_sysfs_path, |
| 181 |
gchar **out_driver, |
| 182 |
GError **error) |
| 183 |
{ |
| 184 |
g_autofree gchar *iter = NULL; |
| 185 |
g_autofree gchar *interface_sysfs_path = NULL; |
| 186 |
|
| 187 |
if (out_driver) |
| 188 |
*out_driver = NULL; |
| 189 |
|
| 190 |
iter = realpath (port_sysfs_path, NULL); |
| 191 |
|
| 192 |
while (iter && (g_strcmp0 (iter, "/") != 0)) { |
| 193 |
gchar *parent; |
| 194 |
|
| 195 |
/* is this the USB interface? */ |
| 196 |
if (has_sysfs_attribute (iter, "bInterfaceClass")) { |
| 197 |
/* stop traversing as soon as the physical device is found */ |
| 198 |
interface_sysfs_path = g_steal_pointer (&iter); |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
parent = g_path_get_dirname (iter); |
| 203 |
g_clear_pointer (&iter, g_free); |
| 204 |
iter = parent; |
| 205 |
} |
| 206 |
|
| 207 |
if (!interface_sysfs_path) { |
| 208 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't find parent interface USB device"); |
| 209 |
return FALSE; |
| 210 |
} |
| 211 |
|
| 212 |
if (out_driver) |
| 213 |
*out_driver = read_sysfs_attribute_link_basename (interface_sysfs_path, "driver"); |
| 214 |
|
| 215 |
return TRUE; |
| 216 |
} |
| 217 |
|
| 218 |
/******************************************************************************/ |
| 219 |
|
| 220 |
gchar * |
| 221 |
qfu_helpers_sysfs_find_by_file (GFile *file, |
| 222 |
GError **error) |
| 223 |
{ |
| 224 |
const gchar **subsys_list = NULL; |
| 225 |
g_autofree gchar *physdev_sysfs_path = NULL; |
| 226 |
g_autofree gchar *found_port_sysfs_path = NULL; |
| 227 |
g_autofree gchar *basename = NULL; |
| 228 |
guint i; |
| 229 |
|
| 230 |
basename = g_file_get_basename (file); |
| 231 |
if (!basename) { |
| 232 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "couldn't get filename"); |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
|
| 236 |
if (g_str_has_prefix (basename, "tty")) |
| 237 |
subsys_list = tty_subsys_list; |
| 238 |
else if (g_str_has_prefix (basename, "cdc-wdm")) |
| 239 |
subsys_list = cdc_wdm_subsys_list; |
| 240 |
else { |
| 241 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unknown device file type"); |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
for (i = 0; !found_port_sysfs_path && subsys_list[i]; i++) { |
| 246 |
g_autofree gchar *tmp = NULL; |
| 247 |
g_autofree gchar *port_sysfs_path = NULL; |
| 248 |
|
| 249 |
tmp = g_strdup_printf ("/sys/class/%s/%s", subsys_list[i], basename); |
| 250 |
|
| 251 |
port_sysfs_path = realpath (tmp, NULL); |
| 252 |
if (port_sysfs_path && g_file_test (port_sysfs_path, G_FILE_TEST_EXISTS)) |
| 253 |
found_port_sysfs_path = g_steal_pointer (&port_sysfs_path); |
| 254 |
} |
| 255 |
|
| 256 |
if (!found_port_sysfs_path) { |
| 257 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "device not found"); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
|
| 261 |
if (!get_device_details (found_port_sysfs_path, |
| 262 |
&physdev_sysfs_path, NULL, NULL, NULL, NULL, NULL, |
| 263 |
error)) |
| 264 |
return NULL; |
| 265 |
|
| 266 |
g_debug ("[qfu-sysfs] sysfs path for '%s' found: %s", basename, physdev_sysfs_path); |
| 267 |
return g_steal_pointer (&physdev_sysfs_path); |
| 268 |
} |
| 269 |
|
| 270 |
/******************************************************************************/ |
| 271 |
|
| 272 |
static gboolean |
| 273 |
device_already_added (GPtrArray *ptr, |
| 274 |
const gchar *sysfs_path) |
| 275 |
{ |
| 276 |
guint i; |
| 277 |
|
| 278 |
for (i = 0; i < ptr->len; i++) { |
| 279 |
if (g_strcmp0 (g_ptr_array_index (ptr, i), sysfs_path) == 0) |
| 280 |
return TRUE; |
| 281 |
} |
| 282 |
|
| 283 |
return FALSE; |
| 284 |
} |
| 285 |
|
| 286 |
static GPtrArray * |
| 287 |
find_by_device_info_in_subsystem (GPtrArray *sysfs_paths, |
| 288 |
const gchar *subsystem, |
| 289 |
guint16 vid, |
| 290 |
guint16 pid, |
| 291 |
guint busnum, |
| 292 |
guint devnum) |
| 293 |
{ |
| 294 |
g_autofree gchar *subsys_sysfs_path = NULL; |
| 295 |
g_autoptr(GFile) subsys_sysfs_file = NULL; |
| 296 |
g_autoptr(GFileEnumerator) direnum = NULL; |
| 297 |
|
| 298 |
subsys_sysfs_path = g_strdup_printf ("/sys/class/%s", subsystem); |
| 299 |
subsys_sysfs_file = g_file_new_for_path (subsys_sysfs_path); |
| 300 |
direnum = g_file_enumerate_children (subsys_sysfs_file, |
| 301 |
G_FILE_ATTRIBUTE_STANDARD_NAME, |
| 302 |
G_FILE_QUERY_INFO_NONE, |
| 303 |
NULL, |
| 304 |
NULL); |
| 305 |
if (direnum) { |
| 306 |
while (TRUE) { |
| 307 |
GFileInfo *info = NULL; |
| 308 |
g_autoptr(GFile) child = NULL; |
| 309 |
g_autofree gchar *child_path = NULL; |
| 310 |
guint16 device_vid = 0; |
| 311 |
guint16 device_pid = 0; |
| 312 |
guint device_busnum = 0; |
| 313 |
guint device_devnum = 0; |
| 314 |
g_autofree gchar *device_sysfs_path = NULL; |
| 315 |
|
| 316 |
if (!g_file_enumerator_iterate (direnum, &info, NULL, NULL, NULL) || !info) |
| 317 |
break; |
| 318 |
|
| 319 |
child = g_file_enumerator_get_child (direnum, info); |
| 320 |
child_path = g_file_get_path (child); |
| 321 |
if (get_device_details (child_path, |
| 322 |
&device_sysfs_path, |
| 323 |
NULL, |
| 324 |
&device_vid, |
| 325 |
&device_pid, |
| 326 |
&device_busnum, |
| 327 |
&device_devnum, |
| 328 |
NULL)) { |
| 329 |
if ((vid == 0 || vid == device_vid) && |
| 330 |
(pid == 0 || pid == device_pid) && |
| 331 |
(busnum == 0 || busnum == device_busnum) && |
| 332 |
(devnum == 0 || devnum == device_devnum) && |
| 333 |
(!device_already_added (sysfs_paths, device_sysfs_path))) |
| 334 |
g_ptr_array_add (sysfs_paths, g_steal_pointer (&device_sysfs_path)); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
return sysfs_paths; |
| 340 |
} |
| 341 |
|
| 342 |
gchar * |
| 343 |
qfu_helpers_sysfs_find_by_device_info (guint16 vid, |
| 344 |
guint16 pid, |
| 345 |
guint busnum, |
| 346 |
guint devnum, |
| 347 |
GError **error) |
| 348 |
{ |
| 349 |
g_autoptr(GPtrArray) sysfs_paths = NULL; |
| 350 |
g_autoptr(GString) match_str = NULL; |
| 351 |
guint i; |
| 352 |
|
| 353 |
sysfs_paths = g_ptr_array_new_with_free_func (g_free); |
| 354 |
|
| 355 |
match_str = g_string_new (""); |
| 356 |
if (vid != 0) |
| 357 |
g_string_append_printf (match_str, "vid 0x%04x", vid); |
| 358 |
if (pid != 0) |
| 359 |
g_string_append_printf (match_str, "%spid 0x%04x", match_str->len > 0 ? ", " : "", pid); |
| 360 |
if (busnum != 0) |
| 361 |
g_string_append_printf (match_str, "%sbus %03u", match_str->len > 0 ? ", " : "", busnum); |
| 362 |
if (devnum != 0) |
| 363 |
g_string_append_printf (match_str, "%sdev %03u", match_str->len > 0 ? ", " : "", devnum); |
| 364 |
g_assert (match_str->len > 0); |
| 365 |
|
| 366 |
for (i = 0; tty_subsys_list[i]; i++) |
| 367 |
sysfs_paths = find_by_device_info_in_subsystem (sysfs_paths, |
| 368 |
tty_subsys_list[i], |
| 369 |
vid, pid, busnum, devnum); |
| 370 |
|
| 371 |
for (i = 0; cdc_wdm_subsys_list[i]; i++) |
| 372 |
sysfs_paths = find_by_device_info_in_subsystem (sysfs_paths, |
| 373 |
cdc_wdm_subsys_list[i], |
| 374 |
vid, pid, busnum, devnum); |
| 375 |
|
| 376 |
for (i = 0; i < sysfs_paths->len; i++) |
| 377 |
g_debug ("[%s] sysfs path: %s", match_str->str, (gchar *) g_ptr_array_index (sysfs_paths, i)); |
| 378 |
|
| 379 |
if (sysfs_paths->len == 0) { |
| 380 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 381 |
"no device found with matching criteria: %s", |
| 382 |
match_str->str); |
| 383 |
return NULL; |
| 384 |
} |
| 385 |
|
| 386 |
if (sysfs_paths->len > 1) { |
| 387 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 388 |
"multiple devices (%u) found with matching criteria: %s", |
| 389 |
sysfs_paths->len, match_str->str); |
| 390 |
return NULL; |
| 391 |
} |
| 392 |
|
| 393 |
return g_strdup (g_ptr_array_index (sysfs_paths, 0)); |
| 394 |
} |
| 395 |
|
| 396 |
/******************************************************************************/ |
| 397 |
|
| 398 |
static GFile * |
| 399 |
device_matches (GFile *file, |
| 400 |
QfuHelpersDeviceType type, |
| 401 |
QfuHelpersDeviceMode mode, |
| 402 |
const gchar *sysfs_path) |
| 403 |
{ |
| 404 |
g_autofree gchar *port_sysfs_path = NULL; |
| 405 |
g_autofree gchar *device_sysfs_path = NULL; |
| 406 |
g_autofree gchar *device_driver = NULL; |
| 407 |
g_autofree gchar *device_path = NULL; |
| 408 |
QfuHelpersDeviceMode device_mode = QFU_HELPERS_DEVICE_MODE_UNKNOWN; |
| 409 |
|
| 410 |
port_sysfs_path = g_file_get_path (file); |
| 411 |
if (!get_device_details (port_sysfs_path, |
| 412 |
&device_sysfs_path, &device_mode, NULL, NULL, NULL, NULL, |
| 413 |
NULL)) |
| 414 |
return NULL; |
| 415 |
|
| 416 |
if (!device_sysfs_path) |
| 417 |
return NULL; |
| 418 |
|
| 419 |
if (g_strcmp0 (device_sysfs_path, sysfs_path) != 0) |
| 420 |
return NULL; |
| 421 |
|
| 422 |
if (device_mode != mode) |
| 423 |
return NULL; |
| 424 |
|
| 425 |
if (!get_interface_details (port_sysfs_path, &device_driver, NULL)) |
| 426 |
return NULL; |
| 427 |
|
| 428 |
switch (type) { |
| 429 |
case QFU_HELPERS_DEVICE_TYPE_TTY: |
| 430 |
if (g_strcmp0 (device_driver, "qcserial") != 0) |
| 431 |
return NULL; |
| 432 |
break; |
| 433 |
case QFU_HELPERS_DEVICE_TYPE_CDC_WDM: |
| 434 |
if (g_strcmp0 (device_driver, "qmi_wwan") != 0 && g_strcmp0 (device_driver, "cdc_mbim") != 0) |
| 435 |
return NULL; |
| 436 |
break; |
| 437 |
case QFU_HELPERS_DEVICE_TYPE_LAST: |
| 438 |
default: |
| 439 |
g_assert_not_reached (); |
| 440 |
} |
| 441 |
|
| 442 |
device_path = g_strdup_printf ("/dev/%s", g_file_get_basename (file)); |
| 443 |
return g_file_new_for_path (device_path); |
| 444 |
} |
| 445 |
|
| 446 |
GList * |
| 447 |
qfu_helpers_sysfs_list_devices (QfuHelpersDeviceType device_type, |
| 448 |
QfuHelpersDeviceMode device_mode, |
| 449 |
const gchar *sysfs_path) |
| 450 |
{ |
| 451 |
const gchar **subsys_list = NULL; |
| 452 |
guint i; |
| 453 |
GList *files = NULL; |
| 454 |
|
| 455 |
switch (device_type) { |
| 456 |
case QFU_HELPERS_DEVICE_TYPE_TTY: |
| 457 |
subsys_list = tty_subsys_list; |
| 458 |
break; |
| 459 |
case QFU_HELPERS_DEVICE_TYPE_CDC_WDM: |
| 460 |
subsys_list = cdc_wdm_subsys_list; |
| 461 |
break; |
| 462 |
case QFU_HELPERS_DEVICE_TYPE_LAST: |
| 463 |
default: |
| 464 |
g_assert_not_reached (); |
| 465 |
} |
| 466 |
|
| 467 |
for (i = 0; subsys_list[i]; i++) { |
| 468 |
g_autofree gchar *subsys_sysfs_path = NULL; |
| 469 |
g_autoptr(GFile) subsys_sysfs_file = NULL; |
| 470 |
g_autoptr(GFileEnumerator) direnum = NULL; |
| 471 |
|
| 472 |
subsys_sysfs_path = g_strdup_printf ("/sys/class/%s", subsys_list[i]); |
| 473 |
subsys_sysfs_file = g_file_new_for_path (subsys_sysfs_path); |
| 474 |
direnum = g_file_enumerate_children (subsys_sysfs_file, |
| 475 |
G_FILE_ATTRIBUTE_STANDARD_NAME, |
| 476 |
G_FILE_QUERY_INFO_NONE, |
| 477 |
NULL, |
| 478 |
NULL); |
| 479 |
if (!direnum) |
| 480 |
continue; |
| 481 |
|
| 482 |
while (TRUE) { |
| 483 |
GFileInfo *info = NULL; |
| 484 |
g_autoptr(GFile) child = NULL; |
| 485 |
g_autoptr(GFile) devfile = NULL; |
| 486 |
|
| 487 |
if (!g_file_enumerator_iterate (direnum, &info, NULL, NULL, NULL) || !info) |
| 488 |
break; |
| 489 |
|
| 490 |
child = g_file_enumerator_get_child (direnum, info); |
| 491 |
devfile = device_matches (child, device_type, device_mode, sysfs_path); |
| 492 |
if (devfile) |
| 493 |
files = g_list_prepend (files, g_steal_pointer (&devfile)); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
return files; |
| 498 |
} |
| 499 |
|
| 500 |
/******************************************************************************/ |
| 501 |
|
| 502 |
/* Check for the new port addition every 10s */ |
| 503 |
#define WAIT_FOR_DEVICE_CHECK_SECS 10 |
| 504 |
|
| 505 |
/* And up to 12 attempts to check (so 120s in total) */ |
| 506 |
#define WAIT_FOR_DEVICE_CHECK_ATTEMPTS 12 |
| 507 |
|
| 508 |
typedef struct { |
| 509 |
QfuHelpersDeviceType device_type; |
| 510 |
QfuHelpersDeviceMode device_mode; |
| 511 |
gchar *sysfs_path; |
| 512 |
gchar *peer_port; |
| 513 |
guint check_attempts; |
| 514 |
guint timeout_id; |
| 515 |
gulong cancellable_id; |
| 516 |
} WaitForDeviceContext; |
| 517 |
|
| 518 |
static void |
| 519 |
wait_for_device_context_free (WaitForDeviceContext *ctx) |
| 520 |
{ |
| 521 |
g_assert (!ctx->timeout_id); |
| 522 |
g_assert (!ctx->cancellable_id); |
| 523 |
|
| 524 |
g_free (ctx->sysfs_path); |
| 525 |
g_free (ctx->peer_port); |
| 526 |
g_slice_free (WaitForDeviceContext, ctx); |
| 527 |
} |
| 528 |
|
| 529 |
GFile * |
| 530 |
qfu_helpers_sysfs_wait_for_device_finish (GAsyncResult *res, |
| 531 |
GError **error) |
| 532 |
{ |
| 533 |
return G_FILE (g_task_propagate_pointer (G_TASK (res), error)); |
| 534 |
} |
| 535 |
|
| 536 |
static GFile * |
| 537 |
wait_for_device_lookup (QfuHelpersDeviceType device_type, |
| 538 |
QfuHelpersDeviceMode device_mode, |
| 539 |
const gchar *sysfs_path, |
| 540 |
const gchar *peer_port) |
| 541 |
{ |
| 542 |
GList *devices; |
| 543 |
GFile *file; |
| 544 |
|
| 545 |
devices = qfu_helpers_sysfs_list_devices (device_type, device_mode, sysfs_path); |
| 546 |
if (!devices) { |
| 547 |
g_autofree gchar *tmp = NULL; |
| 548 |
g_autofree gchar *path = NULL; |
| 549 |
|
| 550 |
if (!peer_port) |
| 551 |
return NULL; |
| 552 |
|
| 553 |
/* Check with peer port */ |
| 554 |
tmp = g_build_filename (peer_port, "device", NULL); |
| 555 |
path = realpath (tmp, NULL); |
| 556 |
if (path) { |
| 557 |
g_debug ("[qfu-sysfs] peer lookup: %s => %s", peer_port, path); |
| 558 |
devices = qfu_helpers_sysfs_list_devices (device_type, device_mode, path); |
| 559 |
if (!devices) |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if (g_list_length (devices) > 1) |
| 565 |
g_warning ("[qfu-sysfs] waiting device (%s) matched multiple times", |
| 566 |
qfu_helpers_device_type_to_string (device_type)); |
| 567 |
|
| 568 |
/* Take the first one from the list */ |
| 569 |
file = G_FILE (g_object_ref (devices->data)); |
| 570 |
g_list_free_full (devices, g_object_unref); |
| 571 |
|
| 572 |
return file; |
| 573 |
} |
| 574 |
|
| 575 |
static gboolean |
| 576 |
wait_for_device_check (GTask *task) |
| 577 |
{ |
| 578 |
WaitForDeviceContext *ctx; |
| 579 |
g_autoptr(GFile) device = NULL; |
| 580 |
g_autofree gchar *device_name = NULL; |
| 581 |
|
| 582 |
ctx = (WaitForDeviceContext *) g_task_get_task_data (task); |
| 583 |
|
| 584 |
/* Check devices matching */ |
| 585 |
device = wait_for_device_lookup (ctx->device_type, ctx->device_mode, ctx->sysfs_path, ctx->peer_port); |
| 586 |
if (!device) { |
| 587 |
/* No devices found */ |
| 588 |
if (ctx->check_attempts == WAIT_FOR_DEVICE_CHECK_ATTEMPTS) { |
| 589 |
/* Disconnect this handler */ |
| 590 |
ctx->timeout_id = 0; |
| 591 |
|
| 592 |
/* Disconnect the other handlers */ |
| 593 |
g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id); |
| 594 |
ctx->cancellable_id = 0; |
| 595 |
|
| 596 |
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT, |
| 597 |
"waiting for device at '%s' timed out", |
| 598 |
ctx->sysfs_path); |
| 599 |
g_object_unref (task); |
| 600 |
return G_SOURCE_REMOVE; |
| 601 |
} |
| 602 |
|
| 603 |
/* go on with next attempt */ |
| 604 |
ctx->check_attempts++; |
| 605 |
return G_SOURCE_CONTINUE; |
| 606 |
} |
| 607 |
|
| 608 |
device_name = g_file_get_basename (device); |
| 609 |
g_debug ("[qfu-sysfs] waiting device (%s) matched: %s", |
| 610 |
qfu_helpers_device_type_to_string (ctx->device_type), |
| 611 |
device_name); |
| 612 |
|
| 613 |
/* Disconnect the other handlers */ |
| 614 |
g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id); |
| 615 |
ctx->cancellable_id = 0; |
| 616 |
|
| 617 |
/* Disconnect this handler */ |
| 618 |
ctx->timeout_id = 0; |
| 619 |
|
| 620 |
g_task_return_pointer (task, g_steal_pointer (&device), g_object_unref); |
| 621 |
g_object_unref (task); |
| 622 |
|
| 623 |
return G_SOURCE_REMOVE; |
| 624 |
} |
| 625 |
|
| 626 |
static void |
| 627 |
wait_for_device_cancelled (GCancellable *cancellable, |
| 628 |
GTask *task) |
| 629 |
{ |
| 630 |
WaitForDeviceContext *ctx; |
| 631 |
|
| 632 |
ctx = (WaitForDeviceContext *) g_task_get_task_data (task); |
| 633 |
|
| 634 |
/* Disconnect this handler */ |
| 635 |
g_cancellable_disconnect (g_task_get_cancellable (task), ctx->cancellable_id); |
| 636 |
ctx->cancellable_id = 0; |
| 637 |
|
| 638 |
/* Disconnect the other handlers */ |
| 639 |
g_source_remove (ctx->timeout_id); |
| 640 |
ctx->timeout_id = 0; |
| 641 |
|
| 642 |
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, |
| 643 |
"waiting for device at '%s' cancelled", |
| 644 |
ctx->sysfs_path); |
| 645 |
g_object_unref (task); |
| 646 |
} |
| 647 |
|
| 648 |
void |
| 649 |
qfu_helpers_sysfs_wait_for_device (QfuHelpersDeviceType device_type, |
| 650 |
QfuHelpersDeviceMode device_mode, |
| 651 |
const gchar *sysfs_path, |
| 652 |
const gchar *peer_port, |
| 653 |
GCancellable *cancellable, |
| 654 |
GAsyncReadyCallback callback, |
| 655 |
gpointer user_data) |
| 656 |
{ |
| 657 |
GTask *task; |
| 658 |
WaitForDeviceContext *ctx; |
| 659 |
|
| 660 |
ctx = g_slice_new0 (WaitForDeviceContext); |
| 661 |
ctx->device_type = device_type; |
| 662 |
ctx->device_mode = device_mode; |
| 663 |
ctx->sysfs_path = g_strdup (sysfs_path); |
| 664 |
ctx->peer_port = g_strdup (peer_port); |
| 665 |
|
| 666 |
task = g_task_new (NULL, cancellable, callback, user_data); |
| 667 |
g_task_set_task_data (task, ctx, (GDestroyNotify) wait_for_device_context_free); |
| 668 |
|
| 669 |
/* Allow cancellation */ |
| 670 |
ctx->cancellable_id = g_cancellable_connect (cancellable, |
| 671 |
(GCallback) wait_for_device_cancelled, |
| 672 |
task, |
| 673 |
NULL); |
| 674 |
|
| 675 |
/* Schedule lookup of of port every once in a while */ |
| 676 |
ctx->timeout_id = g_timeout_add_seconds (WAIT_FOR_DEVICE_CHECK_SECS, |
| 677 |
(GSourceFunc) wait_for_device_check, |
| 678 |
task); |
| 679 |
|
| 680 |
/* Note: task ownership is shared between the signals and the timeout */ |
| 681 |
} |