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
|
From 6c7e8631be53ab14f57c7014ecb1db821d402abe Mon Sep 17 00:00:00 2001
From: "Luke D. Jones" <luke@ljones.dev>
Date: Wed, 2 Oct 2024 23:51:36 +1300
Subject: [PATCH 09/29] hid-asus-ally: initial gamepad configuration
Signed-off-by: Luke D. Jones <luke@ljones.dev>
---
drivers/hid/hid-asus-ally.c | 246 +++++++++++++++++++++++++++++++++++-
drivers/hid/hid-asus-ally.h | 38 +++---
2 files changed, 266 insertions(+), 18 deletions(-)
diff --git a/drivers/hid/hid-asus-ally.c b/drivers/hid/hid-asus-ally.c
index 3d950535c3e2..822139e2a5c0 100644
--- a/drivers/hid/hid-asus-ally.c
+++ b/drivers/hid/hid-asus-ally.c
@@ -10,7 +10,6 @@
#include <linux/platform_device.h>
#include "linux/pm.h"
#include "linux/slab.h"
-#include "linux/stddef.h"
#include <linux/hid.h>
#include <linux/types.h>
#include <linux/usb.h>
@@ -39,6 +38,9 @@
#define FEATURE_KBD_LED_REPORT_ID1 0x5d
#define FEATURE_KBD_LED_REPORT_ID2 0x5e
+#define BTN_DATA_LEN 11;
+#define BTN_CODE_BYTES_LEN 8
+
static const u8 EC_INIT_STRING[] = { 0x5A, 'A', 'S', 'U', 'S', ' ', 'T', 'e','c', 'h', '.', 'I', 'n', 'c', '.', '\0' };
static const u8 EC_MODE_LED_APPLY[] = { 0x5A, 0xB4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static const u8 EC_MODE_LED_SET[] = { 0x5A, 0xB5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
@@ -50,6 +52,58 @@ static const struct hid_device_id rog_ally_devices[] = {
{}
};
+struct btn_code_map {
+ u64 code;
+ const char *name;
+};
+
+/* byte_array must be >= 8 in length */
+static void btn_code_to_byte_array(u64 keycode, u8 *byte_array)
+{
+ /* Convert the u64 to bytes[8] */
+ for (int i = 0; i < 8; ++i) {
+ byte_array[i] = (keycode >> (56 - 8 * i)) & 0xFF;
+ }
+}
+
+struct btn_data {
+ u64 button;
+ u64 macro;
+};
+
+struct btn_mapping {
+ struct btn_data btn_a;
+ struct btn_data btn_b;
+ struct btn_data btn_x;
+ struct btn_data btn_y;
+ struct btn_data btn_lb;
+ struct btn_data btn_rb;
+ struct btn_data btn_ls;
+ struct btn_data btn_rs;
+ struct btn_data btn_lt;
+ struct btn_data btn_rt;
+ struct btn_data dpad_up;
+ struct btn_data dpad_down;
+ struct btn_data dpad_left;
+ struct btn_data dpad_right;
+ struct btn_data btn_view;
+ struct btn_data btn_menu;
+ struct btn_data btn_m1;
+ struct btn_data btn_m2;
+};
+
+/* ROG Ally has many settings related to the gamepad, all using the same n-key endpoint */
+struct ally_gamepad_cfg {
+ struct hid_device *hdev;
+ struct input_dev *input;
+
+ enum xpad_mode mode;
+ /*
+ * index: [mode]
+ */
+ struct btn_mapping *key_mapping[xpad_mode_mouse];
+};
+
/* The hatswitch outputs integers, we use them to index this X|Y pair */
static const int hat_values[][2] = {
{ 0, 0 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 },
@@ -120,6 +174,7 @@ struct ally_rgb_data {
static struct ally_drvdata {
struct hid_device *hdev;
struct ally_x_device *ally_x;
+ struct ally_gamepad_cfg *gamepad_cfg;
struct ally_rgb_dev *led_rgb_dev;
struct ally_rgb_data led_rgb_data;
} drvdata;
@@ -174,6 +229,172 @@ static u8 get_endpoint_address(struct hid_device *hdev)
return -ENODEV;
}
+/**************************************************************************************************/
+/* ROG Ally gamepad configuration */
+/**************************************************************************************************/
+
+/* This should be called before any attempts to set device functions */
+static int ally_gamepad_check_ready(struct hid_device *hdev)
+{
+ int ret, count;
+ u8 *hidbuf;
+
+ hidbuf = kzalloc(FEATURE_ROG_ALLY_REPORT_SIZE, GFP_KERNEL);
+ if (!hidbuf)
+ return -ENOMEM;
+
+ ret = 0;
+ for (count = 0; count < READY_MAX_TRIES; count++) {
+ hidbuf[0] = FEATURE_ROG_ALLY_REPORT_ID;
+ hidbuf[1] = FEATURE_ROG_ALLY_CODE_PAGE;
+ hidbuf[2] = xpad_cmd_check_ready;
+ hidbuf[3] = 01;
+ ret = asus_dev_set_report(hdev, hidbuf, FEATURE_ROG_ALLY_REPORT_SIZE);
+ if (ret < 0)
+ hid_dbg(hdev, "ROG Ally check failed set report: %d\n", ret);
+
+ hidbuf[0] = hidbuf[1] = hidbuf[2] = hidbuf[3] = 0;
+ ret = asus_dev_get_report(hdev, hidbuf, FEATURE_ROG_ALLY_REPORT_SIZE);
+ if (ret < 0)
+ hid_dbg(hdev, "ROG Ally check failed get report: %d\n", ret);
+
+ ret = hidbuf[2] == xpad_cmd_check_ready;
+ if (ret)
+ break;
+ usleep_range(
+ 1000,
+ 2000); /* don't spam the entire loop in less than USB response time */
+ }
+
+ if (count == READY_MAX_TRIES)
+ hid_warn(hdev, "ROG Ally never responded with a ready\n");
+
+ kfree(hidbuf);
+ return ret;
+}
+
+/* A HID packet conatins mappings for two buttons: btn1, btn1_macro, btn2, btn2_macro */
+static void _btn_pair_to_hid_pkt(struct ally_gamepad_cfg *ally_cfg,
+ enum btn_pair_index pair,
+ struct btn_data *btn1, struct btn_data *btn2,
+ u8 *out, int out_len)
+{
+ int start = 5;
+
+ out[0] = FEATURE_ROG_ALLY_REPORT_ID;
+ out[1] = FEATURE_ROG_ALLY_CODE_PAGE;
+ out[2] = xpad_cmd_set_mapping;
+ out[3] = pair;
+ out[4] = xpad_cmd_len_mapping;
+
+ btn_code_to_byte_array(btn1->button, &out[start]);
+ start += BTN_DATA_LEN;
+ btn_code_to_byte_array(btn1->macro, &out[start]);
+ start += BTN_DATA_LEN;
+ btn_code_to_byte_array(btn2->button, &out[start]);
+ start += BTN_DATA_LEN;
+ btn_code_to_byte_array(btn2->macro, &out[start]);
+ //print_hex_dump(KERN_DEBUG, "byte_array: ", DUMP_PREFIX_OFFSET, 64, 1, out, 64, false);
+}
+
+/* Apply the mapping pair to the device */
+static int _gamepad_apply_btn_pair(struct hid_device *hdev, struct ally_gamepad_cfg *ally_cfg,
+ enum btn_pair_index btn_pair)
+{
+ u8 mode = ally_cfg->mode - 1;
+ struct btn_data *btn1, *btn2;
+ u8 *hidbuf;
+ int ret;
+
+ ret = ally_gamepad_check_ready(hdev);
+ if (ret < 0)
+ return ret;
+
+ hidbuf = kzalloc(FEATURE_ROG_ALLY_REPORT_SIZE, GFP_KERNEL);
+ if (!hidbuf)
+ return -ENOMEM;
+
+ switch (btn_pair) {
+ case btn_pair_m1_m2:
+ btn1 = &ally_cfg->key_mapping[mode]->btn_m1;
+ btn2 = &ally_cfg->key_mapping[mode]->btn_m2;
+ break;
+ default:
+ break;
+ }
+
+ _btn_pair_to_hid_pkt(ally_cfg, btn_pair, btn1, btn2, hidbuf, FEATURE_ROG_ALLY_REPORT_SIZE);
+ ret = asus_dev_set_report(hdev, hidbuf, FEATURE_ROG_ALLY_REPORT_SIZE);
+
+ kfree(hidbuf);
+
+ return ret;
+}
+
+static struct ally_gamepad_cfg *ally_gamepad_cfg_create(struct hid_device *hdev)
+{
+ struct ally_gamepad_cfg *ally_cfg;
+ struct input_dev *input_dev;
+ int err;
+
+ ally_cfg = devm_kzalloc(&hdev->dev, sizeof(*ally_cfg), GFP_KERNEL);
+ if (!ally_cfg)
+ return ERR_PTR(-ENOMEM);
+ ally_cfg->hdev = hdev;
+ // Allocate memory for each mode's `btn_mapping`
+ ally_cfg->mode = xpad_mode_game;
+ for (int i = 0; i < xpad_mode_mouse; i++) {
+ ally_cfg->key_mapping[i] = devm_kzalloc(&hdev->dev, sizeof(struct btn_mapping), GFP_KERNEL);
+ if (!ally_cfg->key_mapping[i]) {
+ err = -ENOMEM;
+ goto free_key_mappings;
+ }
+ }
+
+ input_dev = devm_input_allocate_device(&hdev->dev);
+ if (!input_dev) {
+ err = -ENOMEM;
+ goto free_ally_cfg;
+ }
+
+ input_dev->id.bustype = hdev->bus;
+ input_dev->id.vendor = hdev->vendor;
+ input_dev->id.product = hdev->product;
+ input_dev->id.version = hdev->version;
+ input_dev->uniq = hdev->uniq;
+ input_dev->name = "ASUS ROG Ally Config";
+ input_set_capability(input_dev, EV_KEY, KEY_PROG1);
+ input_set_capability(input_dev, EV_KEY, KEY_F16);
+ input_set_capability(input_dev, EV_KEY, KEY_F17);
+ input_set_capability(input_dev, EV_KEY, KEY_F18);
+ input_set_drvdata(input_dev, hdev);
+
+ err = input_register_device(input_dev);
+ if (err)
+ goto free_input_dev;
+ ally_cfg->input = input_dev;
+
+ /* ignore all errors for this as they are related to USB HID I/O */
+ ally_cfg->key_mapping[ally_cfg->mode - 1]->btn_m1.button = BTN_KB_M1;
+ ally_cfg->key_mapping[ally_cfg->mode - 1]->btn_m2.button = BTN_KB_M2;
+ _gamepad_apply_btn_pair(hdev, ally_cfg, btn_pair_m1_m2);
+
+ return ally_cfg;
+
+free_input_dev:
+ devm_kfree(&hdev->dev, input_dev);
+
+free_key_mappings:
+ for (int i = 0; i < xpad_mode_mouse; i++) {
+ if (ally_cfg->key_mapping[i])
+ devm_kfree(&hdev->dev, ally_cfg->key_mapping[i]);
+ }
+
+free_ally_cfg:
+ devm_kfree(&hdev->dev, ally_cfg);
+ return ERR_PTR(err);
+}
+
/**************************************************************************************************/
/* ROG Ally gamepad i/o and force-feedback */
/**************************************************************************************************/
@@ -730,6 +951,7 @@ static void ally_rgb_remove(struct hid_device *hdev)
static int ally_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
int size)
{
+ struct ally_gamepad_cfg *cfg = drvdata.gamepad_cfg;
struct ally_x_device *ally_x = drvdata.ally_x;
if (ally_x) {
@@ -742,6 +964,14 @@ static int ally_raw_event(struct hid_device *hdev, struct hid_report *report, u8
}
}
+ if (cfg && !ally_x) {
+ input_report_key(cfg->input, KEY_PROG1, data[1] == 0x38);
+ input_report_key(cfg->input, KEY_F16, data[1] == 0xA6);
+ input_report_key(cfg->input, KEY_F17, data[1] == 0xA7);
+ input_report_key(cfg->input, KEY_F18, data[1] == 0xA8);
+ input_sync(cfg->input);
+ }
+
return 0;
}
@@ -900,7 +1130,13 @@ static int ally_hid_probe(struct hid_device *hdev, const struct hid_device_id *_
else
hid_info(hdev, "Created Ally RGB LED controls.\n");
- if (IS_ERR(drvdata.led_rgb_dev))
+ drvdata.gamepad_cfg = ally_gamepad_cfg_create(hdev);
+ if (IS_ERR(drvdata.gamepad_cfg))
+ hid_err(hdev, "Failed to create Ally gamepad attributes.\n");
+ else
+ hid_info(hdev, "Created Ally gamepad attributes.\n");
+
+ if (IS_ERR(drvdata.led_rgb_dev) && IS_ERR(drvdata.gamepad_cfg))
goto err_close;
}
@@ -913,6 +1149,12 @@ static int ally_hid_probe(struct hid_device *hdev, const struct hid_device_id *_
goto err_close;
}
hid_info(hdev, "Created Ally X controller.\n");
+
+ // Not required since we send this inputs ep through the gamepad input dev
+ if (drvdata.gamepad_cfg && drvdata.gamepad_cfg->input) {
+ input_unregister_device(drvdata.gamepad_cfg->input);
+ hid_info(hdev, "Ally X removed unrequired input dev.\n");
+ }
}
return 0;
diff --git a/drivers/hid/hid-asus-ally.h b/drivers/hid/hid-asus-ally.h
index 458d02996bca..2b298ad4da0e 100644
--- a/drivers/hid/hid-asus-ally.h
+++ b/drivers/hid/hid-asus-ally.h
@@ -8,35 +8,41 @@
#include <linux/hid.h>
#include <linux/types.h>
+/*
+ * the xpad_mode is used inside the mode setting packet and is used
+ * for indexing (xpad_mode - 1)
+ */
+enum xpad_mode {
+ xpad_mode_game = 0x01,
+ xpad_mode_wasd = 0x02,
+ xpad_mode_mouse = 0x03,
+};
+
/* the xpad_cmd determines which feature is set or queried */
enum xpad_cmd {
- xpad_cmd_set_mode = 0x01,
xpad_cmd_set_mapping = 0x02,
- xpad_cmd_set_js_dz = 0x04, /* deadzones */
- xpad_cmd_set_tr_dz = 0x05, /* deadzones */
- xpad_cmd_set_vibe_intensity = 0x06,
xpad_cmd_set_leds = 0x08,
xpad_cmd_check_ready = 0x0A,
- xpad_cmd_set_calibration = 0x0D,
- xpad_cmd_set_turbo = 0x0F,
- xpad_cmd_set_response_curve = 0x13,
- xpad_cmd_set_adz = 0x18,
};
/* the xpad_cmd determines which feature is set or queried */
enum xpad_cmd_len {
- xpad_cmd_len_mode = 0x01,
xpad_cmd_len_mapping = 0x2c,
- xpad_cmd_len_deadzone = 0x04,
- xpad_cmd_len_vibe_intensity = 0x02,
xpad_cmd_len_leds = 0x0C,
- xpad_cmd_len_calibration2 = 0x01,
- xpad_cmd_len_calibration3 = 0x01,
- xpad_cmd_len_turbo = 0x20,
- xpad_cmd_len_response_curve = 0x09,
- xpad_cmd_len_adz = 0x02,
};
+/* Values correspond to the actual HID byte value required */
+enum btn_pair_index {
+ btn_pair_m1_m2 = 0x08,
+};
+
+#define BTN_KB_M2 0x02008E0000000000
+#define BTN_KB_M1 0x02008F0000000000
+
+#define ALLY_DEVICE_ATTR_WO(_name, _sysfs_name) \
+ struct device_attribute dev_attr_##_name = \
+ __ATTR(_sysfs_name, 0200, NULL, _name##_store)
+
/* required so we can have nested attributes with same name but different functions */
#define ALLY_DEVICE_ATTR_RW(_name, _sysfs_name) \
struct device_attribute dev_attr_##_name = \
--
2.48.1
|