File: fu-framework-qmk-device.c

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (130 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2025 Framework Computer Inc
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "fu-framework-qmk-device.h"
#include "fu-framework-qmk-struct.h"

struct _FuFrameworkQmkDevice {
	FuHidrawDevice parent_instance;
	guint8 iface_reset;
};

G_DEFINE_TYPE(FuFrameworkQmkDevice, fu_framework_qmk_device, FU_TYPE_HIDRAW_DEVICE)

#define FU_FRAMEWORK_QMK_RAW_USAGE_PAGE 0xFF60
#define FU_FRAMEWORK_QMK_RAW_USAGE_ID	0x61

static gboolean
fu_framework_qmk_device_detach(FuDevice *device, FuProgress *progress, GError **error)
{
	FuFrameworkQmkDevice *self = FU_FRAMEWORK_QMK_DEVICE(device);
	g_autoptr(FuStructFrameworkQmkResetRequest) st_req =
	    fu_struct_framework_qmk_reset_request_new();

	if (!fu_hidraw_device_set_report(FU_HIDRAW_DEVICE(self),
					 st_req->buf->data,
					 st_req->buf->len,
					 FU_IO_CHANNEL_FLAG_NONE,
					 error)) {
		g_prefix_error_literal(error, "failed to write packet: ");
		return FALSE;
	}

	/* success */
	fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
	return TRUE;
}

static void
fu_framework_qmk_device_set_progress(FuDevice *device, FuProgress *progress)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_add_step(progress, FWUPD_STATUS_DECOMPRESSING, 0, "prepare-fw");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 70, "detach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 29, "write");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, "attach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 1, "reload");
}

static void
fu_framework_qmk_device_init(FuFrameworkQmkDevice *self)
{
	fu_device_add_icon(FU_DEVICE(self), FU_DEVICE_ICON_INPUT_KEYBOARD);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_INTERNAL);
	fu_device_set_remove_delay(FU_DEVICE(self), 15000); /* 15s */
	fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_BCD);
	fu_device_add_protocol(FU_DEVICE(self), "com.microsoft.uf2");
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UPDATABLE);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UNSIGNED_PAYLOAD);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_ADD_COUNTERPART_GUIDS);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_REPLUG_MATCH_GUID);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_RETRY_OPEN);
	/* revisions indicate incompatible hardware */
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_ADD_INSTANCE_ID_REV);
	fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_WRITE);
	fu_device_retry_set_delay(FU_DEVICE(self), 100);
}

static gboolean
fu_framework_qmk_device_setup(FuDevice *device, GError **error)
{
	g_autoptr(FuHidDescriptor) descriptor = NULL;
	g_autoptr(FuHidReport) report = NULL;
	g_autoptr(FuDevice) device_usb = NULL;
	guint16 version = 0x000;

	descriptor = fu_hidraw_device_parse_descriptor(FU_HIDRAW_DEVICE(device), error);
	if (descriptor == NULL) {
		g_prefix_error_literal(error, "failed to parse descriptor: ");
		return FALSE;
	}
	report = fu_hid_descriptor_find_report(descriptor,
					       error,
					       "usage-page",
					       FU_FRAMEWORK_QMK_RAW_USAGE_PAGE,
					       "usage",
					       FU_FRAMEWORK_QMK_RAW_USAGE_ID,
					       "collection",
					       0x01,
					       NULL);
	if (report == NULL) {
		/* Wrong usage page, this hidraw instance is not what we're looking for */
		return FALSE;
	}

	device_usb = fu_device_get_backend_parent_with_subsystem(device, "usb:usb_device", error);
	if (device_usb == NULL) {
		g_prefix_error_literal(error, "no USB device: ");
		return FALSE;
	}
	if (!fu_device_probe(device_usb, error)) {
		g_prefix_error_literal(error, "USB probe failed: ");
		return FALSE;
	}
	version = fu_usb_device_get_release(FU_USB_DEVICE(device_usb));

	fu_device_set_version_raw(device, version);

	return TRUE;
}

static gchar *
fu_framework_qmk_device_convert_version(FuDevice *device, guint64 version_raw)
{
	return fu_version_from_uint16(version_raw, fu_device_get_version_format(device));
}

static void
fu_framework_qmk_device_class_init(FuFrameworkQmkDeviceClass *klass)
{
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	device_class->setup = fu_framework_qmk_device_setup;
	device_class->detach = fu_framework_qmk_device_detach;
	device_class->convert_version = fu_framework_qmk_device_convert_version;
	device_class->set_progress = fu_framework_qmk_device_set_progress;
}