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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
|
From a714c9673418747453ed085a560f3e3dab1e598c Mon Sep 17 00:00:00 2001
From: "Luke D. Jones" <luke@ljones.dev>
Date: Mon, 26 Aug 2024 12:49:35 +1200
Subject: [PATCH 07/29] hid-asus-ally: Add joystick LED ring support
Adds basic support for the joystick RGB LED rings as a multicolour LED
device with 4 LEDs.
Move the MCU check in to new driver as it must be done after init, and
hid-asus-ally takes over from hid-asus.
Signed-off-by: Luke D. Jones <luke@ljones.dev>
---
drivers/hid/Kconfig | 9 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-asus-ally.c | 627 ++++++++++++++++++++++++++++++++++++
drivers/hid/hid-asus-ally.h | 38 +++
drivers/hid/hid-asus.c | 116 +------
5 files changed, 689 insertions(+), 102 deletions(-)
create mode 100644 drivers/hid/hid-asus-ally.c
create mode 100644 drivers/hid/hid-asus-ally.h
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 4d2a89d65b65..89357822c27b 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -164,6 +164,15 @@ config HID_ASUS
- GL553V series
- GL753V series
+config HID_ASUS_ALLY
+ tristate "Asus Ally gamepad configuration support"
+ depends on USB_HID
+ depends on LEDS_CLASS
+ depends on LEDS_CLASS_MULTICOLOR
+ select POWER_SUPPLY
+ help
+ Support for configuring the Asus ROG Ally gamepad using attributes.
+
config HID_AUREAL
tristate "Aureal"
help
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 24de45f3677d..f338c9eb4600 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_HID_APPLE) += hid-apple.o
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
obj-$(CONFIG_HID_ASUS) += hid-asus.o
+obj-$(CONFIG_HID_ASUS_ALLY) += hid-asus-ally.o
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
obj-$(CONFIG_HID_BELKIN) += hid-belkin.o
obj-$(CONFIG_HID_BETOP_FF) += hid-betopff.o
diff --git a/drivers/hid/hid-asus-ally.c b/drivers/hid/hid-asus-ally.c
new file mode 100644
index 000000000000..4ceb4aab8084
--- /dev/null
+++ b/drivers/hid/hid-asus-ally.c
@@ -0,0 +1,627 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * HID driver for Asus ROG laptops and Ally
+ *
+ * Copyright (c) 2023 Luke Jones <luke@ljones.dev>
+ */
+
+#include "linux/device.h"
+#include <linux/platform_data/x86/asus-wmi.h>
+#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>
+#include <linux/leds.h>
+#include <linux/led-class-multicolor.h>
+
+#include "hid-ids.h"
+#include "hid-asus-ally.h"
+
+#define READY_MAX_TRIES 3
+#define FEATURE_REPORT_ID 0x0d
+#define FEATURE_ROG_ALLY_REPORT_ID 0x5a
+#define FEATURE_ROG_ALLY_CODE_PAGE 0xD1
+#define FEATURE_ROG_ALLY_REPORT_SIZE 64
+#define ALLY_X_INPUT_REPORT_USB 0x0B
+#define ALLY_X_INPUT_REPORT_USB_SIZE 16
+
+#define ROG_ALLY_REPORT_SIZE 64
+#define ROG_ALLY_X_MIN_MCU 313
+#define ROG_ALLY_MIN_MCU 319
+
+#define ROG_ALLY_CFG_INTF_IN 0x83
+#define ROG_ALLY_CFG_INTF_OUT 0x04
+#define ROG_ALLY_X_INTF_IN 0x87
+
+#define FEATURE_KBD_LED_REPORT_ID1 0x5d
+#define FEATURE_KBD_LED_REPORT_ID2 0x5e
+
+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 };
+static const u8 FORCE_FEEDBACK_OFF[] = { 0x0D, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
+
+static const struct hid_device_id rog_ally_devices[] = {
+ { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X) },
+ {}
+};
+
+struct ally_rgb_dev {
+ struct hid_device *hdev;
+ struct led_classdev_mc led_rgb_dev;
+ struct work_struct work;
+ bool output_worker_initialized;
+ spinlock_t lock;
+
+ bool removed;
+ bool update_rgb;
+ uint8_t red[4];
+ uint8_t green[4];
+ uint8_t blue[4];
+};
+
+struct ally_rgb_data {
+ uint8_t brightness;
+ uint8_t red[4];
+ uint8_t green[4];
+ uint8_t blue[4];
+ bool initialized;
+};
+
+static struct ally_drvdata {
+ struct hid_device *hdev;
+ struct ally_rgb_dev *led_rgb_dev;
+ struct ally_rgb_data led_rgb_data;
+} drvdata;
+
+/**
+ * asus_dev_set_report - send set report request to device.
+ *
+ * @hdev: hid device
+ * @buf: in/out data to transfer
+ * @len: length of buf
+ *
+ * Return: count of data transferred, negative if error
+ *
+ * Same behavior as hid_hw_raw_request. Note that the input buffer is duplicated.
+ */
+static int asus_dev_set_report(struct hid_device *hdev, const u8 *buf, size_t len)
+{
+ unsigned char *dmabuf;
+ int ret;
+
+ dmabuf = kmemdup(buf, len, GFP_KERNEL);
+ if (!dmabuf)
+ return -ENOMEM;
+
+ ret = hid_hw_raw_request(hdev, buf[0], dmabuf, len, HID_FEATURE_REPORT,
+ HID_REQ_SET_REPORT);
+ kfree(dmabuf);
+
+ return ret;
+}
+
+static int asus_dev_get_report(struct hid_device *hdev, u8 *out_buf, size_t out_buf_size)
+{
+ return hid_hw_raw_request(hdev, FEATURE_REPORT_ID, out_buf, out_buf_size,
+ HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
+}
+
+static u8 get_endpoint_address(struct hid_device *hdev)
+{
+ struct usb_interface *intf;
+ struct usb_host_endpoint *ep;
+
+ intf = to_usb_interface(hdev->dev.parent);
+
+ if (intf) {
+ ep = intf->cur_altsetting->endpoint;
+ if (ep) {
+ return ep->desc.bEndpointAddress;
+ }
+ }
+
+ return -ENODEV;
+}
+
+/**************************************************************************************************/
+/* ROG Ally LED control */
+/**************************************************************************************************/
+static void ally_rgb_schedule_work(struct ally_rgb_dev *led)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&led->lock, flags);
+ if (!led->removed)
+ schedule_work(&led->work);
+ spin_unlock_irqrestore(&led->lock, flags);
+}
+
+/*
+ * The RGB still has the basic 0-3 level brightness. Since the multicolour
+ * brightness is being used in place, set this to max
+ */
+static int ally_rgb_set_bright_base_max(struct hid_device *hdev)
+{
+ u8 buf[] = { FEATURE_KBD_LED_REPORT_ID1, 0xba, 0xc5, 0xc4, 0x02 };
+
+ return asus_dev_set_report(hdev, buf, sizeof(buf));
+}
+
+static void ally_rgb_do_work(struct work_struct *work)
+{
+ struct ally_rgb_dev *led = container_of(work, struct ally_rgb_dev, work);
+ int ret;
+ unsigned long flags;
+
+ u8 buf[16] = { [0] = FEATURE_ROG_ALLY_REPORT_ID,
+ [1] = FEATURE_ROG_ALLY_CODE_PAGE,
+ [2] = xpad_cmd_set_leds,
+ [3] = xpad_cmd_len_leds };
+
+ spin_lock_irqsave(&led->lock, flags);
+ if (!led->update_rgb) {
+ spin_unlock_irqrestore(&led->lock, flags);
+ return;
+ }
+
+ for (int i = 0; i < 4; i++) {
+ buf[5 + i * 3] = drvdata.led_rgb_dev->green[i];
+ buf[6 + i * 3] = drvdata.led_rgb_dev->blue[i];
+ buf[4 + i * 3] = drvdata.led_rgb_dev->red[i];
+ }
+ led->update_rgb = false;
+
+ spin_unlock_irqrestore(&led->lock, flags);
+
+ ret = asus_dev_set_report(led->hdev, buf, sizeof(buf));
+ if (ret < 0)
+ hid_err(led->hdev, "Ally failed to set gamepad backlight: %d\n", ret);
+}
+
+static void ally_rgb_set(struct led_classdev *cdev, enum led_brightness brightness)
+{
+ struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
+ struct ally_rgb_dev *led = container_of(mc_cdev, struct ally_rgb_dev, led_rgb_dev);
+ int intensity, bright;
+ unsigned long flags;
+
+ led_mc_calc_color_components(mc_cdev, brightness);
+ spin_lock_irqsave(&led->lock, flags);
+ led->update_rgb = true;
+ bright = mc_cdev->led_cdev.brightness;
+ for (int i = 0; i < 4; i++) {
+ intensity = mc_cdev->subled_info[i].intensity;
+ drvdata.led_rgb_dev->red[i] = (((intensity >> 16) & 0xFF) * bright) / 255;
+ drvdata.led_rgb_dev->green[i] = (((intensity >> 8) & 0xFF) * bright) / 255;
+ drvdata.led_rgb_dev->blue[i] = ((intensity & 0xFF) * bright) / 255;
+ }
+ spin_unlock_irqrestore(&led->lock, flags);
+ drvdata.led_rgb_data.initialized = true;
+
+ ally_rgb_schedule_work(led);
+}
+
+static int ally_rgb_set_static_from_multi(struct hid_device *hdev)
+{
+ u8 buf[17] = {FEATURE_KBD_LED_REPORT_ID1, 0xb3};
+ int ret;
+
+ /*
+ * Set single zone single colour based on the first LED of EC software mode.
+ * buf[2] = zone, buf[3] = mode
+ */
+ buf[4] = drvdata.led_rgb_data.red[0];
+ buf[5] = drvdata.led_rgb_data.green[0];
+ buf[6] = drvdata.led_rgb_data.blue[0];
+
+ ret = asus_dev_set_report(hdev, buf, sizeof(buf));
+ if (ret < 0)
+ return ret;
+
+ ret = asus_dev_set_report(hdev, EC_MODE_LED_APPLY, sizeof(EC_MODE_LED_APPLY));
+ if (ret < 0)
+ return ret;
+
+ return asus_dev_set_report(hdev, EC_MODE_LED_SET, sizeof(EC_MODE_LED_SET));
+}
+
+/*
+ * Store the RGB values for restoring on resume, and set the static mode to the first LED colour
+*/
+static void ally_rgb_store_settings(void)
+{
+ int arr_size = sizeof(drvdata.led_rgb_data.red);
+
+ struct ally_rgb_dev *led_rgb = drvdata.led_rgb_dev;
+
+ drvdata.led_rgb_data.brightness = led_rgb->led_rgb_dev.led_cdev.brightness;
+
+ memcpy(drvdata.led_rgb_data.red, led_rgb->red, arr_size);
+ memcpy(drvdata.led_rgb_data.green, led_rgb->green, arr_size);
+ memcpy(drvdata.led_rgb_data.blue, led_rgb->blue, arr_size);
+
+ ally_rgb_set_static_from_multi(led_rgb->hdev);
+}
+
+static void ally_rgb_restore_settings(struct ally_rgb_dev *led_rgb, struct led_classdev *led_cdev,
+ struct mc_subled *mc_led_info)
+{
+ int arr_size = sizeof(drvdata.led_rgb_data.red);
+
+ memcpy(led_rgb->red, drvdata.led_rgb_data.red, arr_size);
+ memcpy(led_rgb->green, drvdata.led_rgb_data.green, arr_size);
+ memcpy(led_rgb->blue, drvdata.led_rgb_data.blue, arr_size);
+ for (int i = 0; i < 4; i++) {
+ mc_led_info[i].intensity = (drvdata.led_rgb_data.red[i] << 16) |
+ (drvdata.led_rgb_data.green[i] << 8) |
+ drvdata.led_rgb_data.blue[i];
+ }
+ led_cdev->brightness = drvdata.led_rgb_data.brightness;
+}
+
+/* Set LEDs. Call after any setup. */
+static void ally_rgb_resume(void)
+{
+ struct ally_rgb_dev *led_rgb = drvdata.led_rgb_dev;
+ struct led_classdev *led_cdev;
+ struct mc_subled *mc_led_info;
+
+ if (!led_rgb)
+ return;
+
+ led_cdev = &led_rgb->led_rgb_dev.led_cdev;
+ mc_led_info = led_rgb->led_rgb_dev.subled_info;
+
+ if (drvdata.led_rgb_data.initialized) {
+ ally_rgb_restore_settings(led_rgb, led_cdev, mc_led_info);
+ led_rgb->update_rgb = true;
+ ally_rgb_schedule_work(led_rgb);
+ ally_rgb_set_bright_base_max(led_rgb->hdev);
+ }
+}
+
+static int ally_rgb_register(struct hid_device *hdev, struct ally_rgb_dev *led_rgb)
+{
+ struct mc_subled *mc_led_info;
+ struct led_classdev *led_cdev;
+
+ mc_led_info =
+ devm_kmalloc_array(&hdev->dev, 12, sizeof(*mc_led_info), GFP_KERNEL | __GFP_ZERO);
+ if (!mc_led_info)
+ return -ENOMEM;
+
+ mc_led_info[0].color_index = LED_COLOR_ID_RGB;
+ mc_led_info[1].color_index = LED_COLOR_ID_RGB;
+ mc_led_info[2].color_index = LED_COLOR_ID_RGB;
+ mc_led_info[3].color_index = LED_COLOR_ID_RGB;
+
+ led_rgb->led_rgb_dev.subled_info = mc_led_info;
+ led_rgb->led_rgb_dev.num_colors = 4;
+
+ led_cdev = &led_rgb->led_rgb_dev.led_cdev;
+ led_cdev->brightness = 128;
+ led_cdev->name = "ally:rgb:joystick_rings";
+ led_cdev->max_brightness = 255;
+ led_cdev->brightness_set = ally_rgb_set;
+
+ if (drvdata.led_rgb_data.initialized) {
+ ally_rgb_restore_settings(led_rgb, led_cdev, mc_led_info);
+ }
+
+ return devm_led_classdev_multicolor_register(&hdev->dev, &led_rgb->led_rgb_dev);
+}
+
+static struct ally_rgb_dev *ally_rgb_create(struct hid_device *hdev)
+{
+ struct ally_rgb_dev *led_rgb;
+ int ret;
+
+ led_rgb = devm_kzalloc(&hdev->dev, sizeof(struct ally_rgb_dev), GFP_KERNEL);
+ if (!led_rgb)
+ return ERR_PTR(-ENOMEM);
+
+ ret = ally_rgb_register(hdev, led_rgb);
+ if (ret < 0) {
+ cancel_work_sync(&led_rgb->work);
+ devm_kfree(&hdev->dev, led_rgb);
+ return ERR_PTR(ret);
+ }
+
+ led_rgb->hdev = hdev;
+ led_rgb->removed = false;
+
+ INIT_WORK(&led_rgb->work, ally_rgb_do_work);
+ led_rgb->output_worker_initialized = true;
+ spin_lock_init(&led_rgb->lock);
+
+ ally_rgb_set_bright_base_max(hdev);
+
+ /* Not marked as initialized unless ally_rgb_set() is called */
+ if (drvdata.led_rgb_data.initialized) {
+ msleep(1500);
+ led_rgb->update_rgb = true;
+ ally_rgb_schedule_work(led_rgb);
+ }
+
+ return led_rgb;
+}
+
+static void ally_rgb_remove(struct hid_device *hdev)
+{
+ struct ally_rgb_dev *led_rgb = drvdata.led_rgb_dev;
+ unsigned long flags;
+ int ep;
+
+ ep = get_endpoint_address(hdev);
+ if (ep != ROG_ALLY_CFG_INTF_IN)
+ return;
+
+ if (!drvdata.led_rgb_dev || led_rgb->removed)
+ return;
+
+ spin_lock_irqsave(&led_rgb->lock, flags);
+ led_rgb->removed = true;
+ led_rgb->output_worker_initialized = false;
+ spin_unlock_irqrestore(&led_rgb->lock, flags);
+ cancel_work_sync(&led_rgb->work);
+ devm_led_classdev_multicolor_unregister(&hdev->dev, &led_rgb->led_rgb_dev);
+
+ hid_info(hdev, "Removed Ally RGB interface");
+}
+
+/**************************************************************************************************/
+/* ROG Ally driver init */
+/**************************************************************************************************/
+
+/*
+ * We don't care about any other part of the string except the version section.
+ * Example strings: FGA80100.RC72LA.312_T01, FGA80100.RC71LS.318_T01
+ */
+static int mcu_parse_version_string(const u8 *response, size_t response_size)
+{
+ int dot_count = 0;
+ size_t i;
+
+ // Look for the second '.' to identify the start of the version
+ for (i = 0; i < response_size; i++) {
+ if (response[i] == '.') {
+ dot_count++;
+ if (dot_count == 2) {
+ int version =
+ simple_strtol((const char *)&response[i + 1], NULL, 10);
+ return (version >= 0) ? version : -EINVAL;
+ }
+ }
+ }
+
+ return -EINVAL;
+}
+
+static int mcu_request_version(struct hid_device *hdev)
+{
+ const u8 request[] = { 0x5a, 0x05, 0x03, 0x31, 0x00, 0x20 };
+ u8 *response;
+ int ret;
+
+ response = kzalloc(FEATURE_ROG_ALLY_REPORT_SIZE, GFP_KERNEL);
+ if (!response)
+ return -ENOMEM;
+
+ ret = asus_dev_set_report(hdev, request, sizeof(request));
+ if (ret < 0)
+ goto out;
+
+ ret = asus_dev_get_report(hdev, response, FEATURE_ROG_ALLY_REPORT_SIZE);
+ if (ret < 0)
+ goto out;
+
+ ret = mcu_parse_version_string(response, FEATURE_ROG_ALLY_REPORT_SIZE);
+out:
+ if (ret < 0)
+ hid_err(hdev, "Failed to get MCU version: %d\n", ret);
+ kfree(response);
+ return ret;
+}
+
+static void mcu_maybe_warn_version(struct hid_device *hdev, int idProduct)
+{
+ int min_version, version;
+ struct asus_wmi *asus;
+ struct device *dev;
+
+ min_version = ROG_ALLY_X_MIN_MCU;
+ version = mcu_request_version(hdev);
+ if (version) {
+ switch (idProduct) {
+ case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY:
+ min_version = ROG_ALLY_MIN_MCU;
+ break;
+ case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X:
+ min_version = ROG_ALLY_X_MIN_MCU;
+ break;
+ }
+ }
+
+ hid_info(hdev, "Ally device MCU version: %d\n", version);
+ if (version < min_version) {
+ hid_warn(hdev,
+ "The MCU version must be %d or greater\n"
+ "Please update your MCU with official ASUS firmware release\n",
+ min_version);
+ /* Get the asus platform device */
+ dev = bus_find_device_by_name(&platform_bus_type, NULL, "asus-nb-wmi");
+ if (dev) {
+ asus = dev_get_drvdata(dev);
+ /* Do not show the powersave attribute if MCU version too low */
+ if (asus)
+ asus->mcu_powersave_available = false;
+ put_device(dev);
+ }
+ }
+}
+
+static int ally_hid_init(struct hid_device *hdev)
+{
+ int ret;
+
+ ret = asus_dev_set_report(hdev, EC_INIT_STRING, sizeof(EC_INIT_STRING));
+ if (ret < 0) {
+ hid_err(hdev, "Ally failed to send init command: %d\n", ret);
+ return ret;
+ }
+
+ ret = asus_dev_set_report(hdev, FORCE_FEEDBACK_OFF, sizeof(FORCE_FEEDBACK_OFF));
+ if (ret < 0)
+ hid_err(hdev, "Ally failed to send init command: %d\n", ret);
+
+ return ret;
+}
+
+static int ally_hid_probe(struct hid_device *hdev, const struct hid_device_id *_id)
+{
+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ struct usb_device *udev = interface_to_usbdev(intf);
+ u16 idProduct = le16_to_cpu(udev->descriptor.idProduct);
+ int ret, ep;
+
+ ep = get_endpoint_address(hdev);
+ if (ep < 0)
+ return ep;
+
+ if (ep != ROG_ALLY_CFG_INTF_IN)
+ return -ENODEV;
+
+ ret = hid_parse(hdev);
+ if (ret) {
+ hid_err(hdev, "Parse failed\n");
+ return ret;
+ }
+
+ ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
+ if (ret) {
+ hid_err(hdev, "Failed to start HID device\n");
+ return ret;
+ }
+
+ ret = hid_hw_open(hdev);
+ if (ret) {
+ hid_err(hdev, "Failed to open HID device\n");
+ goto err_stop;
+ }
+
+ /* Initialize MCU even before alloc */
+ ret = ally_hid_init(hdev);
+ if (ret < 0)
+ return ret;
+
+ drvdata.hdev = hdev;
+ hid_set_drvdata(hdev, &drvdata);
+
+ /* This should almost always exist */
+ if (ep == ROG_ALLY_CFG_INTF_IN) {
+ mcu_maybe_warn_version(hdev, idProduct);
+
+ drvdata.led_rgb_dev = ally_rgb_create(hdev);
+ if (IS_ERR(drvdata.led_rgb_dev))
+ hid_err(hdev, "Failed to create Ally gamepad LEDs.\n");
+ else
+ hid_info(hdev, "Created Ally RGB LED controls.\n");
+
+ if (IS_ERR(drvdata.led_rgb_dev))
+ goto err_close;
+ }
+
+ return 0;
+
+err_close:
+ hid_hw_close(hdev);
+err_stop:
+ hid_hw_stop(hdev);
+ return ret;
+}
+
+static void ally_hid_remove(struct hid_device *hdev)
+{
+ if (drvdata.led_rgb_dev)
+ ally_rgb_remove(hdev);
+
+ hid_hw_close(hdev);
+ hid_hw_stop(hdev);
+}
+
+static int ally_hid_resume(struct hid_device *hdev)
+{
+ ally_rgb_resume();
+
+ return 0;
+}
+
+static int ally_hid_reset_resume(struct hid_device *hdev)
+{
+ int ep = get_endpoint_address(hdev);
+ if (ep != ROG_ALLY_CFG_INTF_IN)
+ return 0;
+
+ ally_hid_init(hdev);
+ ally_rgb_resume();
+
+ return 0;
+}
+
+static int ally_pm_thaw(struct device *dev)
+{
+ struct hid_device *hdev = to_hid_device(dev);
+
+ return ally_hid_reset_resume(hdev);
+}
+
+static int ally_pm_suspend(struct device *dev)
+{
+ if (drvdata.led_rgb_dev) {
+ ally_rgb_store_settings();
+ }
+
+ return 0;
+}
+
+static const struct dev_pm_ops ally_pm_ops = {
+ .thaw = ally_pm_thaw,
+ .suspend = ally_pm_suspend,
+ .poweroff = ally_pm_suspend,
+};
+
+MODULE_DEVICE_TABLE(hid, rog_ally_devices);
+
+static struct hid_driver
+ rog_ally_cfg = { .name = "asus_rog_ally",
+ .id_table = rog_ally_devices,
+ .probe = ally_hid_probe,
+ .remove = ally_hid_remove,
+ /* HID is the better place for resume functions, not pm_ops */
+ .resume = ally_hid_resume,
+ .reset_resume = ally_hid_reset_resume,
+ .driver = {
+ .pm = &ally_pm_ops,
+ } };
+
+static int __init rog_ally_init(void)
+{
+ return hid_register_driver(&rog_ally_cfg);
+}
+
+static void __exit rog_ally_exit(void)
+{
+ hid_unregister_driver(&rog_ally_cfg);
+}
+
+module_init(rog_ally_init);
+module_exit(rog_ally_exit);
+
+MODULE_AUTHOR("Luke D. Jones");
+MODULE_DESCRIPTION("HID Driver for ASUS ROG Ally gamepad configuration.");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-asus-ally.h b/drivers/hid/hid-asus-ally.h
new file mode 100644
index 000000000000..eb8617c80c2a
--- /dev/null
+++ b/drivers/hid/hid-asus-ally.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * HID driver for Asus ROG laptops and Ally
+ *
+ * Copyright (c) 2023 Luke Jones <luke@ljones.dev>
+ */
+
+#include <linux/hid.h>
+#include <linux/types.h>
+
+/* 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,
+};
diff --git a/drivers/hid/hid-asus.c b/drivers/hid/hid-asus.c
index 641bf1f8c747..bd28250777f1 100644
--- a/drivers/hid/hid-asus.c
+++ b/drivers/hid/hid-asus.c
@@ -53,9 +53,9 @@ MODULE_DESCRIPTION("Asus HID Keyboard and TouchPad");
#define FEATURE_KBD_LED_REPORT_ID1 0x5d
#define FEATURE_KBD_LED_REPORT_ID2 0x5e
-#define ROG_ALLY_REPORT_SIZE 64
-#define ROG_ALLY_X_MIN_MCU 313
-#define ROG_ALLY_MIN_MCU 319
+#define ROG_ALLY_CFG_INTF_IN 0x83
+#define ROG_ALLY_CFG_INTF_OUT 0x04
+#define ROG_ALLY_X_INTF_IN 0x87
#define SUPPORT_KBD_BACKLIGHT BIT(0)
@@ -540,100 +540,9 @@ static bool asus_kbd_wmi_led_control_present(struct hid_device *hdev)
return !!(value & ASUS_WMI_DSTS_PRESENCE_BIT);
}
-/*
- * We don't care about any other part of the string except the version section.
- * Example strings: FGA80100.RC72LA.312_T01, FGA80100.RC71LS.318_T01
- */
-static int mcu_parse_version_string(const u8 *response, size_t response_size)
-{
- int dot_count = 0;
- size_t i;
-
- // Look for the second '.' to identify the start of the version
- for (i = 0; i < response_size; i++) {
- if (response[i] == '.') {
- dot_count++;
- if (dot_count == 2) {
- int version =
- simple_strtol((const char *)&response[i + 1], NULL, 10);
- return (version >= 0) ? version : -EINVAL;
- }
- }
- }
-
- return -EINVAL;
-}
-
-static int mcu_request_version(struct hid_device *hdev)
-{
- const u8 request[] = { 0x5a, 0x05, 0x03, 0x31, 0x00, 0x20 };
- u8 *response;
- int ret;
-
- response = kzalloc(ROG_ALLY_REPORT_SIZE, GFP_KERNEL);
- if (!response)
- return -ENOMEM;
-
- ret = asus_kbd_set_report(hdev, request, sizeof(request));
- if (ret < 0)
- goto out;
-
- ret = hid_hw_raw_request(hdev, FEATURE_REPORT_ID, response,
- ROG_ALLY_REPORT_SIZE, HID_FEATURE_REPORT,
- HID_REQ_GET_REPORT);
- if (ret < 0)
- goto out;
-
- ret = mcu_parse_version_string(response, ROG_ALLY_REPORT_SIZE);
-out:
- if (ret < 0)
- hid_err(hdev, "Failed to get MCU version: %d\n", ret);
- kfree(response);
- return ret;
-}
-
-static void mcu_maybe_warn_version(struct hid_device *hdev, int idProduct)
-{
- int min_version, version;
- struct asus_wmi *asus;
- struct device *dev;
-
- min_version = ROG_ALLY_X_MIN_MCU;
- version = mcu_request_version(hdev);
- if (version) {
- switch (idProduct) {
- case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY:
- min_version = ROG_ALLY_MIN_MCU;
- break;
- case USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X:
- min_version = ROG_ALLY_X_MIN_MCU;
- break;
- }
- }
-
- hid_info(hdev, "Ally device MCU version: %d\n", version);
- if (version < min_version) {
- hid_warn(hdev,
- "The MCU version must be %d or greater\n"
- "Please update your MCU with official ASUS firmware release\n",
- min_version);
- /* Get the asus platform device */
- dev = bus_find_device_by_name(&platform_bus_type, NULL, "asus-nb-wmi");
- if (dev) {
- asus = dev_get_drvdata(dev);
- /* Do not show the powersave attribute if MCU version too low */
- if (asus)
- asus->mcu_powersave_available = false;
- put_device(dev);
- }
- }
-}
-
static int asus_kbd_register_leds(struct hid_device *hdev)
{
struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
- struct usb_interface *intf;
- struct usb_device *udev;
unsigned char kbd_func;
int ret;
@@ -657,14 +566,6 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
if (ret < 0)
return ret;
}
-
- if (drvdata->quirks & QUIRK_ROG_ALLY_XPAD) {
- intf = to_usb_interface(hdev->dev.parent);
- udev = interface_to_usbdev(intf);
- mcu_maybe_warn_version(hdev,
- le16_to_cpu(udev->descriptor.idProduct));
- }
-
} else {
/* Initialize keyboard */
ret = asus_kbd_init(hdev, FEATURE_KBD_REPORT_ID);
@@ -1134,6 +1035,17 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
drvdata->quirks = id->driver_data;
+ /* Ignore these endpoints as they are used by hid-asus-ally */
+ if (drvdata->quirks & QUIRK_ROG_ALLY_XPAD) {
+ struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+ struct usb_host_endpoint *ep = intf->cur_altsetting->endpoint;
+
+ if (ep->desc.bEndpointAddress == ROG_ALLY_X_INTF_IN ||
+ ep->desc.bEndpointAddress == ROG_ALLY_CFG_INTF_IN ||
+ ep->desc.bEndpointAddress == ROG_ALLY_CFG_INTF_OUT)
+ return -ENODEV;
+ }
+
/*
* T90CHI's keyboard dock returns same ID values as T100CHI's dock.
* Thus, identify T90CHI dock with product name string.
--
2.48.1
|