This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
forked from ali1234/lg4l
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hid-g13.c
1427 lines (1169 loc) · 35.3 KB
/
hid-g13.c
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2009 by Rick L. Vinyard, Jr. *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* This driver is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this software. If not see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include <linux/fb.h>
#include <linux/hid.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/sysfs.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/vmalloc.h>
#include <linux/leds.h>
#include <linux/completion.h>
#include <linux/version.h>
#include "hid-ids.h"
#include "usbhid/usbhid.h"
#include "hid-gfb.h"
#ifdef __GNUC__
#define __UNUSED __attribute__ ((unused))
#else
#define __UNUSED
#endif
#define G13_NAME "Logitech G13"
/* Key defines */
#define G13_KEYS 35
#define G13_KEYMAP_SIZE (G13_KEYS*3)
/* Framebuffer defines */
#define G13FB_NAME "g13fb"
#define G13FB_WIDTH (160)
#define G13FB_LINE_LENGTH (160/8)
#define G13FB_HEIGHT (43)
#define G13FB_SIZE (G13FB_LINE_LENGTH*G13FB_HEIGHT)
#define G13FB_UPDATE_RATE_LIMIT (20)
#define G13FB_UPDATE_RATE_DEFAULT (10)
/*
* The native G13 format uses vertical bits. Therefore the number of bytes
* needed to represent the first column is 43/8 (rows/bits) rounded up.
* Additionally, the format requires a padding of 32 bits in front of the
* image data.
*
* Therefore the vbitmap size must be:
* 160 * ceil(43/8) + 32 = 160 * 6 + 32 = 992
*/
#define G13_VBITMAP_SIZE (992)
/* Backlight defaults */
#define G13_DEFAULT_RED (0)
#define G13_DEFAULT_GREEN (255)
#define G13_DEFAULT_BLUE (0)
/* LED array indices */
#define G13_LED_M1 0
#define G13_LED_M2 1
#define G13_LED_M3 2
#define G13_LED_MR 3
#define G13_REPORT_4_INIT 0x00
#define G13_REPORT_4_FINALIZE 0x01
#define G13_READY_SUBSTAGE_1 0x01
#define G13_READY_SUBSTAGE_2 0x02
#define G13_READY_SUBSTAGE_3 0x04
#define G13_READY_STAGE_1 0x07
#define G13_READY_SUBSTAGE_4 0x08
#define G13_READY_SUBSTAGE_5 0x10
#define G13_READY_STAGE_2 0x1F
#define G13_READY_SUBSTAGE_6 0x20
#define G13_READY_SUBSTAGE_7 0x40
#define G13_READY_STAGE_3 0x7F
#define G13_RESET_POST 0x01
#define G13_RESET_MESSAGE_1 0x02
#define G13_RESET_READY 0x03
/* Per device data structure */
struct g13_data {
/* HID reports */
struct hid_device *hdev;
struct hid_report *backlight_report;
struct hid_report *start_input_report;
struct hid_report *feature_report_4;
struct hid_report *led_report;
struct hid_report *output_report_3;
struct input_dev *input_dev;
/* core state */
char *name;
int keycode[G13_KEYMAP_SIZE];
int scancode_state[G13_KEYS];
u8 rgb[3];
u8 led;
u8 curkeymap;
u8 keymap_switching;
/* Framebuffer stuff */
struct gfb_data *gfb_data;
/* LED stuff */
struct led_classdev *led_cdev[4];
/* Housekeeping stuff */
spinlock_t lock;
struct completion ready;
int ready_stages;
int need_reset;
};
/* Convenience macros */
#define hid_get_g13data(hdev) \
((struct g13_data *)(hid_get_drvdata(hdev)))
#define input_get_hdev(idev) \
((struct hid_device *)(input_get_drvdata(idev)))
#define input_get_g13data(idev) (hid_get_g13data(input_get_hdev(idev)))
/*
* Keymap array indices
*
* Key Index
* --------- ------
* G1-G22 0-21
* FUNC 22
* LCD1 23
* LCD2 24
* LCD3 25
* LCD4 26
* M1 27
* M2 28
* M3 29
* MR 30
* BTN_LEFT 31
* BTN_DOWN 32
* BTN_STICK 33
* LIGHT 34
*/
static const unsigned int g13_default_key_map[G13_KEYS] = {
/* first row g1 - g7 */
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7,
/* second row g8 - g11 */
KEY_UNKNOWN, KEY_UNKNOWN, KEY_BACK, KEY_UP,
/* second row g12 - g13 */
KEY_FORWARD, KEY_UNKNOWN, KEY_UNKNOWN,
/* third row g15 - g19 */
KEY_UNKNOWN, KEY_LEFT, KEY_DOWN, KEY_RIGHT, KEY_UNKNOWN,
/* fourth row g20 - g22 */
KEY_BACKSPACE, KEY_ENTER, KEY_SPACE,
/* next, light left, light center left, light center right, light right */
BTN_0, BTN_1, BTN_2, BTN_3, BTN_4,
/* M1, M2, M3, MR */
KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
/* button left, button down, button stick, light */
BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, KEY_RESERVED,
};
static DEVICE_ATTR(fb_node, 0444, gfb_fb_node_show, NULL);
static DEVICE_ATTR(fb_update_rate, 0666,
gfb_fb_update_rate_show,
gfb_fb_update_rate_store);
static int g13_input_get_keycode(struct input_dev * dev,
unsigned int scancode,
unsigned int * keycode)
{
int retval;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
struct input_keymap_entry ke = {
.flags = 0,
.len = sizeof(scancode),
.index = scancode
};
memcpy(&ke.scancode, &scancode, sizeof scancode);
retval = input_get_keycode(dev, &ke);
*keycode = ke.keycode;
#else
retval = input_get_keycode(dev, scancode, keycode);
#endif
return retval;
}
static void g13_led_send(struct hid_device *hdev)
{
struct g13_data *data = hid_get_g13data(hdev);
data->led_report->field[0]->value[0] = data->led&0x0F;
data->led_report->field[0]->value[1] = 0x00;
data->led_report->field[0]->value[2] = 0x00;
data->led_report->field[0]->value[3] = 0x00;
usbhid_submit_report(hdev, data->led_report, USB_DIR_OUT);
}
static void g13_led_set(struct led_classdev *led_cdev,
enum led_brightness value,
int led_num)
{
struct device *dev;
struct hid_device *hdev;
struct g13_data *data;
u8 mask;
/* Get the device associated with the led */
dev = led_cdev->dev->parent;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* Get the underlying data value */
data = hid_get_g13data(hdev);
mask = 0x01<<led_num;
if (value)
data->led |= mask;
else
data->led &= ~mask;
g13_led_send(hdev);
}
static void g13_led_m1_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g13_led_set(led_cdev, value, G13_LED_M1);
}
static void g13_led_m2_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g13_led_set(led_cdev, value, G13_LED_M2);
}
static void g13_led_m3_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g13_led_set(led_cdev, value, G13_LED_M3);
}
static void g13_led_mr_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g13_led_set(led_cdev, value, G13_LED_MR);
}
static enum led_brightness g13_led_brightness_get(struct led_classdev *led_cdev)
{
struct device *dev;
struct hid_device *hdev;
struct g13_data *data;
int value = 0;
/* Get the device associated with the led */
dev = led_cdev->dev->parent;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* Get the underlying data value */
data = hid_get_g13data(hdev);
if (led_cdev == data->led_cdev[G13_LED_M1])
value = data->led & 0x01;
else if (led_cdev == data->led_cdev[G13_LED_M2])
value = data->led & 0x02;
else if (led_cdev == data->led_cdev[G13_LED_M3])
value = data->led & 0x04;
else if (led_cdev == data->led_cdev[G13_LED_MR])
value = data->led & 0x08;
else
dev_info(dev, G13_NAME " error retrieving LED brightness\n");
if (value)
return LED_FULL;
return LED_OFF;
}
static const struct led_classdev g13_led_cdevs[4] = {
{
.brightness_set = g13_led_m1_brightness_set,
.brightness_get = g13_led_brightness_get,
},
{
.brightness_set = g13_led_m2_brightness_set,
.brightness_get = g13_led_brightness_get,
},
{
.brightness_set = g13_led_m3_brightness_set,
.brightness_get = g13_led_brightness_get,
},
{
.brightness_set = g13_led_mr_brightness_set,
.brightness_get = g13_led_brightness_get,
},
};
static int g13_input_setkeycode(struct input_dev *dev,
int scancode,
int keycode)
{
int old_keycode;
int i;
struct g13_data *data = input_get_g13data(dev);
if (scancode >= dev->keycodemax)
return -EINVAL;
spin_lock(&data->lock);
old_keycode = data->keycode[scancode];
data->keycode[scancode] = keycode;
__clear_bit(old_keycode, dev->keybit);
__set_bit(keycode, dev->keybit);
for (i = 0; i < dev->keycodemax; i++) {
if (data->keycode[i] == old_keycode) {
__set_bit(old_keycode, dev->keybit);
break; /* Setting the bit twice is useless, so break*/
}
}
spin_unlock(&data->lock);
return 0;
}
static int g13_input_getkeycode(struct input_dev *dev,
int scancode,
int *keycode)
{
struct g13_data *data = input_get_g13data(dev);
if (!dev->keycodesize)
return -EINVAL;
if (scancode >= dev->keycodemax)
return -EINVAL;
*keycode = data->keycode[scancode];
return 0;
}
/*
* The "keymap" attribute
*/
static ssize_t g13_keymap_index_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g13_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%u\n", data->curkeymap);
}
static ssize_t g13_set_keymap_index(struct hid_device *hdev, unsigned k)
{
int scancode;
int offset_old;
int offset_new;
int keycode_old;
int keycode_new;
struct g13_data *data = hid_get_g13data(hdev);
struct input_dev *idev = data->input_dev;
if (k > 2)
return -EINVAL;
/*
* Release all the pressed keys unless the new keymap has the same key
* in the same scancode position.
*
* Also, clear the scancode state unless the new keymap has the same
* key in the same scancode position.
*
* This allows a keycode mapped to the same scancode in two different
* keymaps to remain pressed without a key up code when the keymap is
* switched.
*/
offset_old = G13_KEYS * data->curkeymap;
offset_new = G13_KEYS * k;
for (scancode = 0; scancode < G13_KEYS; scancode++) {
keycode_old = data->keycode[offset_old+scancode];
keycode_new = data->keycode[offset_new+scancode];
if (keycode_old != keycode_new) {
if (keycode_old != KEY_RESERVED)
input_report_key(idev, keycode_old, 0);
data->scancode_state[scancode] = 0;
}
}
data->curkeymap = k;
if (data->keymap_switching) {
data->led = 1 << k;
g13_led_send(hdev);
}
return 0;
}
static ssize_t g13_keymap_index_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hdev;
int i;
unsigned k;
ssize_t set_result;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* If we have an invalid pointer we'll return ENODATA */
if (hdev == NULL || &(hdev->dev) != dev)
return -ENODATA;
i = sscanf(buf, "%u", &k);
if (i != 1) {
dev_warn(dev, G13_NAME " unrecognized input: %s", buf);
return -1;
}
set_result = g13_set_keymap_index(hdev, k);
if (set_result < 0)
return set_result;
return count;
}
static DEVICE_ATTR(keymap_index, 0666,
g13_keymap_index_show,
g13_keymap_index_store);
/*
* The "keycode" attribute
*/
static ssize_t g13_keymap_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int offset = 0;
int result;
int scancode;
int keycode;
int error;
struct g13_data *data = dev_get_drvdata(dev);
for (scancode = 0; scancode < G13_KEYMAP_SIZE; scancode++) {
error = g13_input_get_keycode(data->input_dev, scancode, &keycode);
if (error) {
dev_warn(dev, G13_NAME " error accessing scancode %d\n",
scancode);
continue;
}
result = sprintf(buf+offset, "0x%03x 0x%04x\n",
scancode, keycode);
if (result < 0)
return -EINVAL;
offset += result;
}
return offset+1;
}
static ssize_t g13_keymap_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hdev;
int scanned;
int consumed;
int scancd;
int keycd;
int error;
int set = 0;
int gkey;
int index;
int good;
struct g13_data *data;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* If we have an invalid pointer we'll return ENODATA */
if (hdev == NULL || &(hdev->dev) != dev)
return -ENODATA;
/* Now, let's get the data structure */
data = hid_get_g13data(hdev);
do {
good = 0;
/* Look for scancode keycode pair in hex */
scanned = sscanf(buf, "%x %x%n", &scancd, &keycd, &consumed);
if (scanned == 2) {
buf += consumed;
error = g13_input_setkeycode(data->input_dev, scancd, keycd);
if (error)
goto err_input_setkeycode;
set++;
good = 1;
} else {
/*
* Look for Gkey keycode pair and assign to current
* keymap
*/
scanned = sscanf(buf, "G%d %x%n", &gkey, &keycd, &consumed);
if (scanned == 2 && gkey > 0 && gkey <= G13_KEYS) {
buf += consumed;
scancd = data->curkeymap * G13_KEYS + gkey - 1;
error = g13_input_setkeycode(data->input_dev, scancd, keycd);
if (error)
goto err_input_setkeycode;
set++;
good = 1;
} else {
/*
* Look for Gkey-index keycode pair and assign
* to indexed keymap
*/
scanned = sscanf(buf, "G%d-%d %x%n", &gkey, &index, &keycd, &consumed);
if (scanned == 3 &&
gkey > 0 && gkey <= G13_KEYS &&
index >= 0 && index <= 2) {
buf += consumed;
scancd = index * G13_KEYS + gkey - 1;
error = g13_input_setkeycode(data->input_dev, scancd, keycd);
if (error)
goto err_input_setkeycode;
set++;
good = 1;
}
}
}
} while (good);
if (set == 0) {
dev_warn(dev, G13_NAME " unrecognized keycode input: %s", buf);
return -1;
}
return count;
err_input_setkeycode:
dev_warn(dev, G13_NAME " error setting scancode %d to keycode %d\n",
scancd, keycd);
return error;
}
static DEVICE_ATTR(keymap, 0666, g13_keymap_show, g13_keymap_store);
/*
* The "keymap_switching" attribute
*/
static ssize_t g13_keymap_switching_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g13_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%u\n", data->keymap_switching);
}
static ssize_t g13_set_keymap_switching(struct hid_device *hdev, unsigned k)
{
struct g13_data *data = hid_get_g13data(hdev);
data->keymap_switching = k;
if (data->keymap_switching) {
data->led = 1 << data->curkeymap;
g13_led_send(hdev);
}
return 0;
}
static ssize_t g13_keymap_switching_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hdev;
int i;
unsigned k;
ssize_t set_result;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* If we have an invalid pointer we'll return ENODATA */
if (hdev == NULL || &(hdev->dev) != dev)
return -ENODATA;
i = sscanf(buf, "%u", &k);
if (i != 1) {
dev_warn(dev, G13_NAME "unrecognized input: %s", buf);
return -1;
}
set_result = g13_set_keymap_switching(hdev, k);
if (set_result < 0)
return set_result;
return count;
}
static DEVICE_ATTR(keymap_switching, 0644,
g13_keymap_switching_show,
g13_keymap_switching_store);
static ssize_t g13_name_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g13_data *data = dev_get_drvdata(dev);
int result;
if (data->name == NULL) {
buf[0] = 0x00;
return 1;
}
spin_lock(&data->lock);
result = sprintf(buf, "%s", data->name);
spin_unlock(&data->lock);
return result;
}
static ssize_t g13_name_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct g13_data *data = dev_get_drvdata(dev);
size_t limit = count;
char *end;
spin_lock(&data->lock);
if (data->name != NULL) {
kfree(data->name);
data->name = NULL;
}
end = strpbrk(buf, "\n\r");
if (end != NULL)
limit = end - buf;
if (end != buf) {
if (limit > 100)
limit = 100;
data->name = kzalloc(limit+1, GFP_ATOMIC);
strncpy(data->name, buf, limit);
}
spin_unlock(&data->lock);
return count;
}
static DEVICE_ATTR(name, 0666, g13_name_show, g13_name_store);
static void g13_feature_report_4_send(struct hid_device *hdev, int which)
{
struct g13_data *data = hid_get_g13data(hdev);
if (which == G13_REPORT_4_INIT) {
data->feature_report_4->field[0]->value[0] = 0x02;
data->feature_report_4->field[0]->value[1] = 0x00;
data->feature_report_4->field[0]->value[2] = 0x00;
data->feature_report_4->field[0]->value[3] = 0x00;
} else if (which == G13_REPORT_4_FINALIZE) {
data->feature_report_4->field[0]->value[0] = 0x02;
data->feature_report_4->field[0]->value[1] = 0x80;
data->feature_report_4->field[0]->value[2] = 0x00;
data->feature_report_4->field[0]->value[3] = 0xFF;
} else {
return;
}
usbhid_submit_report(hdev, data->feature_report_4, USB_DIR_OUT);
}
static void g13_rgb_send(struct hid_device *hdev)
{
struct g13_data *data = hid_get_g13data(hdev);
data->backlight_report->field[0]->value[0] = data->rgb[0];
data->backlight_report->field[0]->value[1] = data->rgb[1];
data->backlight_report->field[0]->value[2] = data->rgb[2];
data->backlight_report->field[0]->value[3] = 0x00;
usbhid_submit_report(hdev, data->backlight_report, USB_DIR_OUT);
}
/*
* The "rgb" attribute
* red green blue
* each with values 0 - 255 (black - full intensity)
*/
static ssize_t g13_rgb_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
unsigned r, g, b;
struct g13_data *data = dev_get_drvdata(dev);
r = data->rgb[0];
g = data->rgb[1];
b = data->rgb[2];
return sprintf(buf, "%u %u %u\n", r, g, b);
}
static void g13_rgb_set(struct hid_device *hdev,
unsigned r, unsigned g, unsigned b)
{
struct g13_data *data = hid_get_g13data(hdev);
data->rgb[0] = r;
data->rgb[1] = g;
data->rgb[2] = b;
g13_rgb_send(hdev);
}
static ssize_t g13_rgb_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hdev;
int i;
unsigned r;
unsigned g;
unsigned b;
/* Get the hid associated with the device */
hdev = container_of(dev, struct hid_device, dev);
/* If we have an invalid pointer we'll return ENODATA */
if (hdev == NULL || &(hdev->dev) != dev)
return -ENODATA;
i = sscanf(buf, "%u %u %u", &r, &g, &b);
if (i != 3) {
dev_warn(dev, G13_NAME "unrecognized input: %s", buf);
return -1;
}
g13_rgb_set(hdev, r, g, b);
return count;
}
static DEVICE_ATTR(rgb, 0666, g13_rgb_show, g13_rgb_store);
/*
* The "minor" attribute
*/
static ssize_t g13_minor_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g13_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%d\n", data->hdev->minor);
}
static DEVICE_ATTR(minor, 0444, g13_minor_show, NULL);
/*
* Create a group of attributes so that we can create and destroy them all
* at once.
*/
static struct attribute *g13_attrs[] = {
&dev_attr_name.attr,
&dev_attr_rgb.attr,
&dev_attr_keymap_index.attr,
&dev_attr_keymap_switching.attr,
&dev_attr_keymap.attr,
&dev_attr_minor.attr,
&dev_attr_fb_update_rate.attr,
&dev_attr_fb_node.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* An unnamed attribute group will put all of the attributes directly in
* the kobject directory. If we specify a name, a subdirectory will be
* created for the attributes with the directory being the name of the
* attribute group.
*/
static struct attribute_group g13_attr_group = {
.attrs = g13_attrs,
};
static void g13_handle_key_event(struct g13_data *data,
struct input_dev *idev,
int scancode,
int value)
{
int error;
int keycode;
int offset;
offset = G13_KEYS * data->curkeymap;
error = g13_input_get_keycode(idev, scancode+offset, &keycode);
if (unlikely(error)) {
dev_warn(&idev->dev, G13_NAME " error in input_get_keycode(): scancode=%d\n", scancode);
return;
}
/* Only report mapped keys */
if (keycode != KEY_RESERVED)
input_report_key(idev, keycode, value);
/* Or report MSC_SCAN on keypress of an unmapped key */
else if (data->scancode_state[scancode] == 0 && value)
input_event(idev, EV_MSC, MSC_SCAN, scancode);
data->scancode_state[scancode] = value;
}
static void g13_raw_event_process_input(struct hid_device *hdev,
struct g13_data *data,
u8 *raw_data)
{
struct input_dev *idev = data->input_dev;
int scancode;
int value;
int i;
int mask;
/*
* We'll check for the M* keys being pressed before processing
* the remainder of the key data. That way the new keymap will
* be loaded if there is a keymap switch.
*/
if (unlikely(data->keymap_switching)) {
if (data->curkeymap != 0 && raw_data[6] & 0x20)
g13_set_keymap_index(hdev, 0);
else if (data->curkeymap != 1 && raw_data[6] & 0x40)
g13_set_keymap_index(hdev, 1);
else if (data->curkeymap != 2 && raw_data[6] & 0x80)
g13_set_keymap_index(hdev, 2);
}
for (i = 0, mask = 0x01; i < 8; i++, mask <<= 1) {
/* Keys G1 through G8 */
scancode = i;
value = raw_data[3] & mask;
g13_handle_key_event(data, idev, scancode, value);
/* Keys G9 through G16 */
scancode = i + 8;
value = raw_data[4] & mask;
g13_handle_key_event(data, idev, scancode, value);
/* Keys G17 through G22 */
scancode = i + 16;
value = raw_data[5] & mask;
if (i <= 5)
g13_handle_key_event(data, idev, scancode, value);
/* Keys FUNC through M3 */
scancode = i + 22;
value = raw_data[6] & mask;
g13_handle_key_event(data, idev, scancode, value);
/* Keys MR through LIGHT */
scancode = i + 30;
value = raw_data[7] & mask;
if (i <= 4)
g13_handle_key_event(data, idev, scancode, value);
}
input_report_abs(idev, ABS_X, raw_data[1]);
input_report_abs(idev, ABS_Y, raw_data[2]);
input_sync(idev);
}
static int g13_raw_event(struct hid_device *hdev,
struct hid_report *report,
u8 *raw_data, int size)
{
/*
* On initialization receive a 258 byte message with
* data = 6 0 255 255 255 255 255 255 255 255 ...
*/
struct g13_data *data;
data = dev_get_drvdata(&hdev->dev);
spin_lock(&data->lock);
if (unlikely(data->need_reset)) {
g13_rgb_send(hdev);
g13_led_send(hdev);
data->need_reset = 0;
spin_unlock(&data->lock);
return 1;
}
if (unlikely(data->ready_stages != G13_READY_STAGE_3)) {
switch (report->id) {
case 6:
if (!(data->ready_stages & G13_READY_SUBSTAGE_1))
data->ready_stages |= G13_READY_SUBSTAGE_1;
else if (data->ready_stages & G13_READY_SUBSTAGE_4 &&
!(data->ready_stages & G13_READY_SUBSTAGE_5)
)
data->ready_stages |= G13_READY_SUBSTAGE_5;
else if (data->ready_stages & G13_READY_SUBSTAGE_6 &&
raw_data[1] >= 0x80)
data->ready_stages |= G13_READY_SUBSTAGE_7;
break;
case 1:
if (!(data->ready_stages & G13_READY_SUBSTAGE_2))
data->ready_stages |= G13_READY_SUBSTAGE_2;
else
data->ready_stages |= G13_READY_SUBSTAGE_3;
break;
}
if (data->ready_stages == G13_READY_STAGE_1 ||
data->ready_stages == G13_READY_STAGE_2 ||
data->ready_stages == G13_READY_STAGE_3)
complete_all(&data->ready);
spin_unlock(&data->lock);
return 1;
}
spin_unlock(&data->lock);
if (likely(report->id == 1)) {
g13_raw_event_process_input(hdev, data, raw_data);
return 1;
}
return 0;
}
static void g13_initialize_keymap(struct g13_data *data)
{
int i;
for (i = 0; i < G13_KEYS; i++) {
data->keycode[i] = g13_default_key_map[i];
__set_bit(data->keycode[i], data->input_dev->keybit);
}
__clear_bit(KEY_RESERVED, data->input_dev->keybit);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)