-
Notifications
You must be signed in to change notification settings - Fork 19
/
hid-g110.c
1408 lines (1148 loc) · 35.1 KB
/
hid-g110.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) 2010 by Alistair Buxton *
* based on hid-g13.c *
* *
* 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/hid.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/mm.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"
#define G110_NAME "Logitech G110"
/* Key defines */
#define G110_KEYS 32
#define G110_KEYMAP_SIZE (G110_KEYS*3)
/* Backlight defaults */
#define G110_DEFAULT_RED (0)
#define G110_DEFAULT_BLUE (255)
/* LED array indices */
#define G110_LED_M1 0
#define G110_LED_M2 1
#define G110_LED_M3 2
#define G110_LED_MR 3
#define G110_LED_BL_R 4
#define G110_LED_BL_B 5
#define G110_REPORT_4_INIT 0x00
#define G110_REPORT_4_FINALIZE 0x01
#define G110_READY_SUBSTAGE_1 0x01
#define G110_READY_SUBSTAGE_2 0x02
#define G110_READY_SUBSTAGE_3 0x04
#define G110_READY_STAGE_1 0x07
#define G110_READY_SUBSTAGE_4 0x08
#define G110_READY_SUBSTAGE_5 0x10
#define G110_READY_STAGE_2 0x1F
#define G110_READY_SUBSTAGE_6 0x20
#define G110_READY_SUBSTAGE_7 0x40
#define G110_READY_STAGE_3 0x7F
#define G110_RESET_POST 0x01
#define G110_RESET_MESSAGE_1 0x02
#define G110_RESET_READY 0x03
/* Per device data structure */
struct g110_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[G110_KEYMAP_SIZE];
int scancode_state[G110_KEYS];
u8 backlight_rb[2];
u8 led;
u8 curkeymap;
u8 keymap_switching;
/* none standard buttons stuff */
u8 ep1keys[2];
struct urb *ep1_urb;
spinlock_t ep1_urb_lock;
/* LED stuff */
struct led_classdev *led_cdev[6];
/* Housekeeping stuff */
spinlock_t lock;
struct completion ready;
int ready_stages;
int need_reset;
};
/* Convenience macros */
#define hid_get_g110data(hdev) \
((struct g110_data *)(hid_get_drvdata(hdev)))
#define input_get_hdev(idev) \
((struct hid_device *)(input_get_drvdata(idev)))
#define input_get_g110data(idev) (hid_get_g110data(input_get_hdev(idev)))
/*
* Keymap array indices
*
* Key Index
* --------- ------
* G1-G12 0-11
* M1 12
* M2 13
* M3 14
* MR 15
* LIGHT 19
*/
static const unsigned int g110_default_key_map[G110_KEYS] = {
/*
KEY_F1, KEY_F2, KEY_F3, KEY_F4,
KEY_F5, KEY_F6, KEY_F7, KEY_F8,
KEY_F9, KEY_F10, KEY_F11, KEY_F12,
*/
KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
/* M1, M2, M3, MR */
KEY_F21, KEY_F22, KEY_F23, KEY_F24,
KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
/* Screen keymap
*
* Key Index
* ----- -----
* Gear 0
* Back 1
* Menu 2
* OK 3
* Right 4
* Left 5
* Down 6
* Up 7
*/
KEY_FORWARD, KEY_BACK, KEY_MENU, KEY_OK,
KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
};
static int g110_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,
.scancode = scancode,
};
retval = input_get_keycode(dev, &ke);
*keycode = ke.keycode;
#else
retval = input_get_keycode(dev, scancode, keycode);
#endif
return retval;
}
static void g110_led_send(struct hid_device *hdev)
{
struct g110_data *data = hid_get_g110data(hdev);
data->led_report->field[0]->value[0] = data->led&0xFF;
usbhid_submit_report(hdev, data->led_report, USB_DIR_OUT);
}
static void g110_led_set(struct led_classdev *led_cdev,
enum led_brightness value,
int led_num)
{
struct device *dev;
struct hid_device *hdev;
struct g110_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_g110data(hdev);
mask = 0x01<<led_num;
if (value)
data->led |= mask;
else
data->led &= ~mask;
g110_led_send(hdev);
}
static void g110_led_m1_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g110_led_set(led_cdev, value, G110_LED_M1);
}
static void g110_led_m2_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g110_led_set(led_cdev, value, G110_LED_M2);
}
static void g110_led_m3_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g110_led_set(led_cdev, value, G110_LED_M3);
}
static void g110_led_mr_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
g110_led_set(led_cdev, value, G110_LED_MR);
}
static enum led_brightness g110_led_brightness_get(struct led_classdev *led_cdev)
{
struct device *dev;
struct hid_device *hdev;
struct g110_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_g110data(hdev);
if (led_cdev == data->led_cdev[G110_LED_M1])
value = data->led & 0x80;
else if (led_cdev == data->led_cdev[G110_LED_M2])
value = data->led & 0x40;
else if (led_cdev == data->led_cdev[G110_LED_M3])
value = data->led & 0x20;
else if (led_cdev == data->led_cdev[G110_LED_MR])
value = data->led & 0x10;
else
dev_info(dev, G110_NAME " error retrieving LED brightness\n");
if (value)
return LED_FULL;
return LED_OFF;
}
static void g110_rgb_send(struct hid_device *hdev)
{
struct g110_data *data = hid_get_g110data(hdev);
/*
* Unlike the other keyboards, the G110 only has 2 LED backlights (red and
* blue). Rather than just setting intensity on each, the keyboard instead
* has a single intensity value, and a second value to specify how red/blue
* the backlight should be. This weird logic converts the two intensity
* values from the user into an intensity/colour value suitable for the
* keyboard.
*
* Additionally, the intensity is only valid from 0x00 - 0x0f (rather than
* 0x00 - 0xff). I decided to keep accepting 0x00 - 0xff as input, and I
* just >>4 to make it fit.
*/
// These are just always zero from what I can tell
data->backlight_report->field[0]->value[1] = 0x00;
data->backlight_report->field[0]->value[2] = 0x00;
// If the intensities are the same, "colour" is 0x80
if ( data->backlight_rb[0] == data->backlight_rb[1] ) {
data->backlight_report->field[0]->value[0] = 0x80;
data->backlight_report->field[1]->value[0] = data->backlight_rb[0]>>4;
}
// If the blue value is higher
else if ( data->backlight_rb[1] > data->backlight_rb[0] ) {
data->backlight_report->field[0]->value[0] = 0xff - ( 0x80 * data->backlight_rb[0] ) / data->backlight_rb[1];
data->backlight_report->field[1]->value[0] = data->backlight_rb[1]>>4;
}
// If the red value is higher
else {
data->backlight_report->field[0]->value[0] = 0x00 - ( 0x80 * data->backlight_rb[1] ) / data->backlight_rb[0];
data->backlight_report->field[1]->value[0] = data->backlight_rb[0]>>4;
}
usbhid_submit_report(hdev, data->backlight_report, USB_DIR_OUT);
}
static void g110_led_bl_brightness_set(struct led_classdev *led_cdev,
int value)
{
struct device *dev;
struct hid_device *hdev;
struct g110_data *data;
/* 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_g110data(hdev);
if (led_cdev == data->led_cdev[G110_LED_BL_R])
data->backlight_rb[0] = value;
else if (led_cdev == data->led_cdev[G110_LED_BL_B])
data->backlight_rb[1] = value;
g110_rgb_send(hdev);
}
static int g110_led_bl_brightness_get(struct led_classdev *led_cdev)
{
struct device *dev;
struct hid_device *hdev;
struct g110_data *data;
/* 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_g110data(hdev);
if (led_cdev == data->led_cdev[G110_LED_BL_R])
return data->backlight_rb[0];
else if (led_cdev == data->led_cdev[G110_LED_BL_B])
return data->backlight_rb[1];
else
dev_info(dev, G110_NAME " error retrieving LED brightness\n");
return 0;
}
static const struct led_classdev g110_led_cdevs[6] = {
{
.brightness_set = g110_led_m1_brightness_set,
.brightness_get = g110_led_brightness_get,
},
{
.brightness_set = g110_led_m2_brightness_set,
.brightness_get = g110_led_brightness_get,
},
{
.brightness_set = g110_led_m3_brightness_set,
.brightness_get = g110_led_brightness_get,
},
{
.brightness_set = g110_led_mr_brightness_set,
.brightness_get = g110_led_brightness_get,
},
{
.brightness_set = g110_led_bl_brightness_set,
.brightness_get = g110_led_bl_brightness_get,
},
{
.brightness_set = g110_led_bl_brightness_set,
.brightness_get = g110_led_bl_brightness_get,
},
};
static int g110_input_setkeycode(struct input_dev *dev,
int scancode,
int keycode)
{
int old_keycode;
int i;
struct g110_data *data = input_get_g110data(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 g110_input_getkeycode(struct input_dev *dev,
int scancode,
int *keycode)
{
struct g110_data *data = input_get_g110data(dev);
if (!dev->keycodesize)
return -EINVAL;
if (scancode >= dev->keycodemax)
return -EINVAL;
*keycode = data->keycode[scancode];
return 0;
}
/*
* The "keymap" attribute
*/
static ssize_t g110_keymap_index_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g110_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%u\n", data->curkeymap);
}
static ssize_t g110_set_keymap_index(struct hid_device *hdev, unsigned k)
{
int scancode;
int offset_old;
int offset_new;
int keycode_old;
int keycode_new;
struct g110_data *data = hid_get_g110data(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 = G110_KEYS * data->curkeymap;
offset_new = G110_KEYS * k;
for (scancode = 0; scancode < G110_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;
g110_led_send(hdev);
}
return 0;
}
static ssize_t g110_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, G110_NAME " unrecognized input: %s", buf);
return -1;
}
set_result = g110_set_keymap_index(hdev, k);
if (set_result < 0)
return set_result;
return count;
}
static DEVICE_ATTR(keymap_index, 0666,
g110_keymap_index_show,
g110_keymap_index_store);
/*
* The "keycode" attribute
*/
static ssize_t g110_keymap_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int offset = 0;
int result;
int scancode;
int keycode;
int error;
struct g110_data *data = dev_get_drvdata(dev);
for (scancode = 0; scancode < G110_KEYMAP_SIZE; scancode++) {
error = g110_input_get_keycode(data->input_dev, scancode, &keycode);
if (error) {
dev_warn(dev, G110_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 g110_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 g110_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_g110data(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 = g110_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 <= G110_KEYS) {
buf += consumed;
scancd = data->curkeymap * G110_KEYS + gkey - 1;
error = g110_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 <= G110_KEYS &&
index >= 0 && index <= 2) {
buf += consumed;
scancd = index * G110_KEYS + gkey - 1;
error = g110_input_setkeycode(data->input_dev, scancd, keycd);
if (error)
goto err_input_setkeycode;
set++;
good = 1;
}
}
}
} while (good);
if (set == 0) {
dev_warn(dev, G110_NAME " unrecognized keycode input: %s", buf);
return -1;
}
return count;
err_input_setkeycode:
dev_warn(dev, G110_NAME " error setting scancode %d to keycode %d\n",
scancd, keycd);
return error;
}
static DEVICE_ATTR(keymap, 0666, g110_keymap_show, g110_keymap_store);
/*
* The "keymap_switching" attribute
*/
static ssize_t g110_keymap_switching_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g110_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%u\n", data->keymap_switching);
}
static ssize_t g110_set_keymap_switching(struct hid_device *hdev, unsigned k)
{
struct g110_data *data = hid_get_g110data(hdev);
data->keymap_switching = k;
if (data->keymap_switching) {
data->led = 1 << data->curkeymap;
g110_led_send(hdev);
}
return 0;
}
static ssize_t g110_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, G110_NAME "unrecognized input: %s", buf);
return -1;
}
set_result = g110_set_keymap_switching(hdev, k);
if (set_result < 0)
return set_result;
return count;
}
static DEVICE_ATTR(keymap_switching, 0644,
g110_keymap_switching_show,
g110_keymap_switching_store);
static ssize_t g110_name_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g110_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 g110_name_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct g110_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, g110_name_show, g110_name_store);
static void g110_feature_report_4_send(struct hid_device *hdev, int which)
{
struct g110_data *data = hid_get_g110data(hdev);
if (which == G110_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 == G110_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);
}
/*
* The "minor" attribute
*/
static ssize_t g110_minor_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct g110_data *data = dev_get_drvdata(dev);
return sprintf(buf, "%d\n", data->hdev->minor);
}
static DEVICE_ATTR(minor, 0444, g110_minor_show, NULL);
/*
* Create a group of attributes so that we can create and destroy them all
* at once.
*/
static struct attribute *g110_attrs[] = {
&dev_attr_name.attr,
&dev_attr_keymap_index.attr,
&dev_attr_keymap_switching.attr,
&dev_attr_keymap.attr,
&dev_attr_minor.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 g110_attr_group = {
.attrs = g110_attrs,
};
static void g110_handle_key_event(struct g110_data *data,
struct input_dev *idev,
int scancode,
int value)
{
int error;
int keycode;
int offset;
offset = G110_KEYS * data->curkeymap;
error = g110_input_get_keycode(idev, scancode+offset, &keycode);
if (unlikely(error)) {
dev_warn(&idev->dev, G110_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 g110_raw_event_process_input(struct hid_device *hdev,
struct g110_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[2] & 0x10)
g110_set_keymap_index(hdev, 0);
else if (data->curkeymap != 1 && raw_data[2] & 0x20)
g110_set_keymap_index(hdev, 1);
else if (data->curkeymap != 2 && raw_data[2] & 0x40)
g110_set_keymap_index(hdev, 2);
}
*/
raw_data[3] &= 0xBF; /* bit 6 is always on */
for (i = 0, mask = 0x01; i < 8; i++, mask <<= 1) {
/* Keys G1 through G8 */
scancode = i;
value = raw_data[1] & mask;
g110_handle_key_event(data, idev, scancode, value);
/* Keys G9 through G16 */
scancode = i + 8;
value = raw_data[2] & mask;
g110_handle_key_event(data, idev, scancode, value);
/* Keys G17 through G22 */
scancode = i + 16;
value = raw_data[3] & mask;
g110_handle_key_event(data, idev, scancode, value);
}
input_sync(idev);
}
static int g110_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 g110_data *data;
data = dev_get_drvdata(&hdev->dev);
spin_lock(&data->lock);
if (unlikely(data->need_reset)) {
g110_rgb_send(hdev);
g110_led_send(hdev);
data->need_reset = 0;
spin_unlock(&data->lock);
return 1;
}
if (unlikely(data->ready_stages != G110_READY_STAGE_3)) {
switch (report->id) {
case 6:
if (!(data->ready_stages & G110_READY_SUBSTAGE_1))
data->ready_stages |= G110_READY_SUBSTAGE_1;
else if (data->ready_stages & G110_READY_SUBSTAGE_4 &&
!(data->ready_stages & G110_READY_SUBSTAGE_5)
)
data->ready_stages |= G110_READY_SUBSTAGE_5;
else if (data->ready_stages & G110_READY_SUBSTAGE_6 &&
raw_data[1] >= 0x80)
data->ready_stages |= G110_READY_SUBSTAGE_7;
break;
case 1:
if (!(data->ready_stages & G110_READY_SUBSTAGE_2))
data->ready_stages |= G110_READY_SUBSTAGE_2;
else
data->ready_stages |= G110_READY_SUBSTAGE_3;
break;
}
if (data->ready_stages == G110_READY_STAGE_1 ||
data->ready_stages == G110_READY_STAGE_2 ||
data->ready_stages == G110_READY_STAGE_3)
complete_all(&data->ready);
spin_unlock(&data->lock);
return 1;
}
spin_unlock(&data->lock);
if (likely(report->id == 2)) {
g110_raw_event_process_input(hdev, data, raw_data);
return 1;
}
return 0;
}
static void g110_initialize_keymap(struct g110_data *data)
{
int i;
for (i = 0; i < G110_KEYS; i++) {
data->keycode[i] = g110_default_key_map[i];
__set_bit(data->keycode[i], data->input_dev->keybit);
}
__clear_bit(KEY_RESERVED, data->input_dev->keybit);
}
/* Unlock the urb so we can reuse it */
static void g110_ep1_urb_completion(struct urb *urb)
{
struct hid_device *hdev = urb->context;
struct g110_data *data = hid_get_g110data(hdev);
struct input_dev *idev = data->input_dev;
int i;
for (i = 0; i < 8; i++)
g110_handle_key_event(data, idev, 24+i, data->ep1keys[0]&(1<<i));
input_sync(idev);
usb_submit_urb(urb, GFP_ATOMIC);
}
static int g110_ep1_read(struct hid_device *hdev)
{
struct usb_interface *intf;