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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
|
/*
* Razer device access library
*
* Copyright (C) 2007-2018 Michael Buesch <m@bues.ch>
*
* 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 program 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.
*/
#include "librazer.h"
#include "razer_private.h"
#include "config.h"
#include "profile_emulation.h"
#include "hw_deathadder.h"
#include "hw_deathadder2013.h"
#include "hw_deathadder_chroma.h"
#include "hw_naga.h"
#include "hw_krait.h"
#include "hw_lachesis.h"
#include "hw_lachesis5k6.h"
#include "hw_copperhead.h"
#include "hw_boomslangce.h"
#include "hw_imperator.h"
#include "hw_taipan.h"
#include "hw_mamba_tournament_edition.h"
#include "hw_diamondback_chroma.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
enum razer_devtype {
RAZER_DEVTYPE_MOUSE,
};
/** struct razer_mouse_base_ops - Basic device-init operations
*
* @type: The type ID.
*
* @init: Initialize the device and its private data structures.
*
* @release: Release device and data structures.
*/
struct razer_mouse_base_ops {
enum razer_mouse_type type;
int (*init)(struct razer_mouse *m, struct libusb_device *udev);
void (*release)(struct razer_mouse *m);
};
struct razer_usb_device {
uint16_t vendor; /* Vendor ID */
uint16_t product; /* Product ID */
enum razer_devtype type;
union {
const struct razer_mouse_base_ops *mouse_ops;
} u;
};
static const struct razer_mouse_base_ops razer_deathadder_base_ops = {
.type = RAZER_MOUSETYPE_DEATHADDER,
.init = razer_deathadder_init,
.release = razer_deathadder_release,
};
static const struct razer_mouse_base_ops razer_deathadder2013_base_ops = {
.type = RAZER_MOUSETYPE_DEATHADDER,
.init = razer_deathadder2013_init,
.release = razer_deathadder2013_release,
};
static const struct razer_mouse_base_ops razer_deathadder_chroma_base_ops = {
.type = RAZER_MOUSETYPE_DEATHADDER,
.init = razer_deathadder_chroma_init,
.release = razer_deathadder_chroma_release,
};
static const struct razer_mouse_base_ops razer_naga_base_ops = {
.type = RAZER_MOUSETYPE_NAGA,
.init = razer_naga_init,
.release = razer_naga_release,
};
static const struct razer_mouse_base_ops razer_krait_base_ops = {
.type = RAZER_MOUSETYPE_KRAIT,
.init = razer_krait_init,
.release = razer_krait_release,
};
static const struct razer_mouse_base_ops razer_lachesis_base_ops = {
.type = RAZER_MOUSETYPE_LACHESIS,
.init = razer_lachesis_init,
.release = razer_lachesis_release,
};
#if 0
static const struct razer_mouse_base_ops razer_lachesis5k6_base_ops = {
.type = RAZER_MOUSETYPE_LACHESIS,
.init = razer_lachesis5k6_init,
.release = razer_lachesis5k6_release,
};
#endif
static const struct razer_mouse_base_ops razer_copperhead_base_ops = {
.type = RAZER_MOUSETYPE_COPPERHEAD,
.init = razer_copperhead_init,
.release = razer_copperhead_release,
};
static const struct razer_mouse_base_ops razer_boomslangce_base_ops = {
.type = RAZER_MOUSETYPE_BOOMSLANGCE,
.init = razer_boomslangce_init,
.release = razer_boomslangce_release,
};
static const struct razer_mouse_base_ops razer_imperator_base_ops = {
.type = RAZER_MOUSETYPE_IMPERATOR,
.init = razer_imperator_init,
.release = razer_imperator_release,
};
static const struct razer_mouse_base_ops razer_taipan_base_ops = {
.type = RAZER_MOUSETYPE_TAIPAN,
.init = razer_taipan_init,
.release = razer_taipan_release,
};
static const struct razer_mouse_base_ops razer_mamba_te_base_ops = {
.type = RAZER_MOUSETYPE_MAMBA_TE,
.init = razer_mamba_te_init,
.release = razer_mamba_te_release,
};
static const struct razer_mouse_base_ops razer_diamondback_chroma_base_ops = {
.type = RAZER_MOUSETYPE_DIAMONDBACK_CHROMA,
.init = razer_diamondback_chroma_init,
.release = razer_diamondback_chroma_release,
};
#define USBVENDOR_ANY 0xFFFF
#define USBPRODUCT_ANY 0xFFFF
#define USB_MOUSE(_vendor, _product, _mouse_ops) \
{ .vendor = _vendor, .product = _product, \
.type = RAZER_DEVTYPE_MOUSE, \
.u = { .mouse_ops = _mouse_ops, }, }
/* Table of supported USB devices. */
static const struct razer_usb_device razer_usbdev_table[] = {
USB_MOUSE(0x1532, 0x0007, &razer_deathadder_base_ops), /* classic */
USB_MOUSE(0x1532, 0x0016, &razer_deathadder_base_ops), /* 3500 DPI */
USB_MOUSE(0x1532, 0x0029, &razer_deathadder_base_ops), /* black edition */
USB_MOUSE(0x1532, 0x0037, &razer_deathadder2013_base_ops), /* 2013 edition */
USB_MOUSE(0x1532, 0x0038, &razer_deathadder2013_base_ops), /* 1800 edition/Korea PCBang edition */
USB_MOUSE(0x1532, 0x0043, &razer_deathadder_chroma_base_ops), /* Chroma edition */
// USB_MOUSE(0x04B4, 0xE006, &razer_deathadder_base_ops), /* cypress bootloader */
USB_MOUSE(0x1532, 0x0003, &razer_krait_base_ops),
USB_MOUSE(0x1532, 0x000C, &razer_lachesis_base_ops), /* classic */
// USB_MOUSE(0x1532, 0x001E, &razer_lachesis5k6_base_ops), /* 5600 DPI */
USB_MOUSE(0x1532, RAZER_NAGA_PID_CLASSIC, &razer_naga_base_ops),
USB_MOUSE(0x1532, RAZER_NAGA_PID_EPIC, &razer_naga_base_ops),
USB_MOUSE(0x1532, RAZER_NAGA_PID_2012, &razer_naga_base_ops),
USB_MOUSE(0x1532, RAZER_NAGA_PID_HEX, &razer_naga_base_ops),
USB_MOUSE(0x1532, RAZER_NAGA_PID_2014, &razer_naga_base_ops),
USB_MOUSE(0x1532, RAZER_NAGA_PID_HEX_2014, &razer_naga_base_ops),
USB_MOUSE(0x1532, 0x0101, &razer_copperhead_base_ops),
USB_MOUSE(0x1532, 0x0005, &razer_boomslangce_base_ops),
USB_MOUSE(0x1532, 0x0017, &razer_imperator_base_ops),
USB_MOUSE(0x1532, 0x0034, &razer_taipan_base_ops),
USB_MOUSE(0x1532, 0x0046, &razer_mamba_te_base_ops), /*mamba tournament edition*/
USB_MOUSE(0x1532, 0x004C, &razer_diamondback_chroma_base_ops),
{ 0, }, /* List end */
};
#undef USB_MOUSE
static struct libusb_context *libusb_ctx;
static struct razer_mouse *mice_list = NULL;
/* We currently only have one handler. */
static razer_event_handler_t event_handler;
static struct config_file *razer_config_file = NULL;
static bool profile_emu_enabled;
razer_logfunc_t razer_logfunc_info;
razer_logfunc_t razer_logfunc_error;
razer_logfunc_t razer_logfunc_debug;
static inline bool razer_initialized(void)
{
return !!libusb_ctx;
}
int razer_register_event_handler(razer_event_handler_t handler)
{
if (event_handler)
return -EEXIST;
event_handler = handler;
return 0;
}
void razer_unregister_event_handler(razer_event_handler_t handler)
{
event_handler = NULL;
}
static void razer_notify_event(enum razer_event type,
const struct razer_event_data *data)
{
if (event_handler)
event_handler(type, data);
}
static int match_usbdev(const struct libusb_device_descriptor *desc,
const struct razer_usb_device *id)
{
if ((desc->idVendor != id->vendor) &&
(id->vendor != USBVENDOR_ANY))
return 0;
if ((desc->idProduct != id->product) &&
(id->product != USBPRODUCT_ANY))
return 0;
return 1;
}
static const struct razer_usb_device * usbdev_lookup(const struct libusb_device_descriptor *desc)
{
const struct razer_usb_device *id = &(razer_usbdev_table[0]);
while (id->vendor || id->product) {
if (match_usbdev(desc, id))
return id;
id++;
}
return NULL;
}
static void mouse_list_add(struct razer_mouse **base, struct razer_mouse *new_entry)
{
struct razer_mouse *i;
new_entry->next = NULL;
if (!(*base)) {
*base = new_entry;
return;
}
for (i = *base; i->next; i = i->next)
;
i->next = new_entry;
}
static void mouse_list_del(struct razer_mouse **base, struct razer_mouse *del_entry)
{
struct razer_mouse *i;
if (del_entry == *base) {
*base = (*base)->next;
return;
}
for (i = *base; i && (i->next != del_entry); i = i->next)
;
if (i)
i->next = del_entry->next;
}
static struct razer_mouse * mouse_list_find(struct razer_mouse *base,
struct libusb_device *udev)
{
struct razer_mouse *m, *next;
uint8_t busnr = libusb_get_bus_number(udev);
uint8_t devaddr = libusb_get_device_address(udev);
razer_for_each_mouse(m, next, base) {
if (m->usb_ctx) {
if (libusb_get_bus_number(m->usb_ctx->dev) == busnr &&
libusb_get_device_address(m->usb_ctx->dev) == devaddr)
return m;
}
}
return NULL;
}
static int parse_idstr(char *idstr, char **devtype, char **devname,
char **buspos, char **devid)
{
*devtype = idstr;
*devname = razer_strsplit(*devtype, ':');
*buspos = razer_strsplit(*devname, ':');
*devid = razer_strsplit(*buspos, ':');
if (!*devtype || !*devname || !*buspos || !*devid)
return -EINVAL;
return 0;
}
static bool simple_globcmp(const char *string,
const char *template)
{
char s, t, tnext;
while (1) {
s = string[0];
t = template[0];
if (s == '\0' && t == '\0')
break;
if (t == '*') {
tnext = template[1];
if (s == '\0') {
if (tnext == '\0')
break;
return 0;
}
if (s == tnext) {
template++;
continue;
}
} else {
if (s == '\0' || t == '\0')
return 0;
if (s != t)
return 0;
template++;
}
string++;
}
return 1; /* Match */
}
static bool mouse_idstr_glob_match(struct config_file *f,
void *context, void *data,
const char *section)
{
struct razer_mouse *m = context;
const char **matched_section = data;
char idstr[RAZER_IDSTR_MAX_SIZE + 1] = { 0, };
char *idstr_devtype, *idstr_devname, *idstr_buspos, *idstr_devid;
char globstr[RAZER_IDSTR_MAX_SIZE + 1] = { 0, };
char *globstr_devtype, *globstr_devname, *globstr_buspos, *globstr_devid;
if (strlen(section) > RAZER_IDSTR_MAX_SIZE) {
razer_error("globbed idstr \"%s\" in config too long\n",
section);
return 1;
}
strcpy(globstr, section);
strcpy(idstr, m->idstr);
if (parse_idstr(globstr, &globstr_devtype, &globstr_devname,
&globstr_buspos, &globstr_devid))
return 1;
if (parse_idstr(idstr, &idstr_devtype, &idstr_devname,
&idstr_buspos, &idstr_devid)) {
razer_error("INTERNAL-ERROR: Failed to parse idstr \"%s\"\n",
idstr);
return 1;
}
if (!simple_globcmp(idstr_devtype, globstr_devtype))
return 1;
if (!simple_globcmp(idstr_devname, globstr_devname))
return 1;
if (!simple_globcmp(idstr_buspos, globstr_buspos))
return 1;
if (!simple_globcmp(idstr_devid, globstr_devid))
return 1;
*matched_section = section;
return 0; /* Match */
}
static struct razer_mouse_profile * find_prof(struct razer_mouse *m, unsigned int nr)
{
struct razer_mouse_profile *list;
unsigned int i;
if (!m->get_profiles)
return NULL;
list = m->get_profiles(m);
if (!list)
return NULL;
for (i = 0; i < m->nr_profiles; i++) {
if (list[i].nr == nr)
return &list[i];
}
return NULL;
}
static int parse_int_int_pair(const char *str, int *val0, int *val1)
{
char a[64] = { 0, }, b[64] = { 0, };
int err;
*val0 = *val1 = -1;
err = razer_split_tuple(str, ':', min(sizeof(a), sizeof(b)),
a, b, NULL);
if (err) {
/* It's not a pair. Interpret it as one value. */
razer_strlcpy(a, str, sizeof(a));
err = razer_string_to_int(razer_string_strip(a), val1);
if (err)
return -EINVAL;
return 1;
}
err = razer_string_to_int(razer_string_strip(a), val0);
err |= razer_string_to_int(razer_string_strip(b), val1);
if (err)
return -EINVAL;
return 0;
}
static bool mouse_apply_one_config(struct config_file *f,
void *context, void *data,
const char *section,
const char *item,
const char *value)
{
struct razer_mouse *m = context;
struct razer_mouse_profile *prof;
bool *error_status = data;
int err, nr;
static const size_t tmplen = 128;
char a[tmplen], b[tmplen], c[tmplen];
//FIXME fixes for glob/prof configs
if (strcasecmp(item, "profile") == 0) {
int profile;
err = razer_string_to_int(value, &profile);
if (err || profile < 1 || (unsigned int)profile > m->nr_profiles)
goto error;
if (m->set_active_profile) {
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
err = m->set_active_profile(m, prof);
if (err)
goto error;
}
} else if (strcasecmp(item, "res") == 0) {
int profile, resolution, i;
struct razer_mouse_dpimapping *mappings;
err = parse_int_int_pair(value, &profile, &resolution);
if (err == 1) {
prof = m->get_active_profile(m);
profile = prof->nr + 1;
} else if (err)
goto error;
if (profile < 1 || resolution < 1)
goto error;
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
nr = m->supported_dpimappings(m, &mappings);
if (nr <= 0)
goto error;
//FIXME dims
for (i = 0; i < nr; i++) {
if (resolution >= 100) {
if ((int)(mappings[i].res[RAZER_DIM_0]) != resolution)
continue;
} else {
if (mappings[i].nr != (unsigned int)resolution)
continue;
}
err = prof->set_dpimapping(prof, NULL, &mappings[i]);
if (err)
goto error;
goto ok;
}
goto invalid; /* res is invalid. Ignore it. */
} else if (strcasecmp(item, "freq") == 0) {
int profile, freq, i;
enum razer_mouse_freq *freqs;
err = parse_int_int_pair(value, &profile, &freq);
if (err == 1) {
prof = m->get_active_profile(m);
profile = prof->nr + 1;
} else if (err)
goto error;
if (profile < 1 || freq < 1)
goto error;
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
nr = m->supported_freqs(m, &freqs);
if (nr <= 0)
goto error;
for (i = 0; i < nr; i++) {
if (freqs[i] != (enum razer_mouse_freq)freq)
continue;
if (!prof->set_freq)
goto invalid;
err = prof->set_freq(prof, freqs[i]);
razer_free_freq_list(freqs, nr);
if (err)
goto error;
goto ok;
}
razer_free_freq_list(freqs, nr);
goto error;
} else if (strcasecmp(item, "led") == 0) {
bool on;
struct razer_led *leds, *led;
const char *ledname, *ledstate;
int profile;
err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
if (err && err != -ENODATA)
goto error;
if (!strlen(a) || !strlen(b))
goto error;
if (strlen(c)) {
/* A profile was specified */
err = razer_string_to_int(razer_string_strip(a), &profile);
if (err || profile < 1)
goto error;
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
ledname = razer_string_strip(b);
ledstate = razer_string_strip(c);
} else {
/* Modify global LEDs */
prof = NULL;
ledname = razer_string_strip(a);
ledstate = razer_string_strip(b);
}
err = razer_string_to_bool(ledstate, &on);
if (err)
goto error;
if (prof) {
if (prof->get_leds) {
err = prof->get_leds(prof, &leds);
} else {
/* Try to fall back to global */
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
} else {
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
if (err < 0)
goto error;
if (err == 0)
goto ok; /* No LEDs. Ignore config. */
for (led = leds; led; led = led->next) {
if (strcasecmp(led->name, ledname) != 0)
continue;
if (!led->toggle_state) {
razer_free_leds(leds);
goto invalid;
}
err = led->toggle_state(led,
on ? RAZER_LED_ON : RAZER_LED_OFF);
razer_free_leds(leds);
if (err)
goto error;
goto ok;
}
razer_free_leds(leds);
goto error;
} else if (strcasecmp(item, "mode") == 0) {
enum razer_led_mode mode;
struct razer_led *leds, *led;
const char *ledname, *ledmode;
int profile;
err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
if (err && err != -ENODATA)
goto error;
if (!strlen(a) || !strlen(b))
goto error;
if (strlen(c)) {
/* A profile was specified */
err = razer_string_to_int(razer_string_strip(a), &profile);
if (err || profile < 1)
goto error;
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
ledname = razer_string_strip(b);
ledmode = razer_string_strip(c);
} else {
/* Modify global LEDs */
prof = NULL;
ledname = razer_string_strip(a);
ledmode = razer_string_strip(b);
}
err = razer_string_to_mode(ledmode, &mode);
if (err)
goto error;
if (prof) {
if (prof->get_leds) {
err = prof->get_leds(prof, &leds);
} else {
/* Try to fall back to global */
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
} else {
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
if (err < 0)
goto error;
if (err == 0)
goto ok; /* No LEDs. Ignore config. */
for (led = leds; led; led = led->next) {
if (strcasecmp(led->name, ledname) != 0)
continue;
if (!led->set_mode) {
razer_free_leds(leds);
goto invalid;
}
err = led->set_mode(led, mode);
razer_free_leds(leds);
if (err)
goto error;
goto ok;
}
razer_free_leds(leds);
goto error;
} else if (strcasecmp(item, "color") == 0) {
struct razer_rgb_color color;
struct razer_led *leds, *led;
const char *ledname, *ledcolor;
int profile;
err = razer_split_tuple(value, ':', tmplen, a, b, c, NULL);
if (err && err != -ENODATA)
goto error;
if (!strlen(a) || !strlen(b))
goto error;
if (strlen(c)) {
/* A profile was specified */
err = razer_string_to_int(razer_string_strip(a), &profile);
if (err || profile < 1)
goto error;
prof = find_prof(m, profile - 1);
if (!prof)
goto error;
ledname = razer_string_strip(b);
ledcolor = razer_string_strip(c);
} else {
/* Modify global LEDs */
prof = NULL;
ledname = razer_string_strip(a);
ledcolor = razer_string_strip(b);
}
err = razer_string_to_color(ledcolor, &color);
if (err)
goto error;
if (prof) {
if (prof->get_leds) {
err = prof->get_leds(prof, &leds);
} else {
/* Try to fall back to global */
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
} else {
if (!m->global_get_leds)
goto ok; /* No LEDs. Ignore config. */
err = m->global_get_leds(m, &leds);
}
if (err < 0)
goto error;
if (err == 0)
goto ok; /* No LEDs. Ignore config. */
for (led = leds; led; led = led->next) {
if (strcasecmp(led->name, ledname) != 0)
continue;
if (!led->change_color) {
razer_free_leds(leds);
goto invalid;
}
err = led->change_color(led, &color);
razer_free_leds(leds);
if (err)
goto error;
goto ok;
}
razer_free_leds(leds);
goto error;
} else if (strcasecmp(item, "disabled") == 0) {
goto ok;
} else
goto invalid;
ok:
return 1;
error:
*error_status = 1;
invalid:
razer_error("Config section \"%s\" item \"%s\" "
"invalid.\n", section, item);
return *error_status ? 0 : 1;
}
static void mouse_apply_initial_config(struct razer_mouse *m)
{
const char *section = NULL;
int err;
bool error_status = 0;
config_for_each_section(razer_config_file,
m, §ion,
mouse_idstr_glob_match);
if (!section)
return;
if (config_get_bool(razer_config_file, section,
"disabled", 0, CONF_NOCASE)) {
razer_debug("Initial config for \"%s\" is disabled. Not applying.\n",
m->idstr);
return;
}
razer_debug("Applying config section \"%s\" to \"%s\"\n",
section, m->idstr);
err = m->claim(m);
if (err) {
razer_error("Failed to claim \"%s\"\n", m->idstr);
return;
}
config_for_each_item(razer_config_file,
m, &error_status,
section,
mouse_apply_one_config);
m->release(m);
if (error_status) {
razer_error("Failed to apply initial config "
"to \"%s\"\n", m->idstr);
}
}
static struct razer_usb_context * razer_create_usb_ctx(struct libusb_device *dev)
{
struct razer_usb_context *ctx;
ctx = zalloc(sizeof(*ctx));
if (!ctx)
return NULL;
ctx->dev = dev;
ctx->bConfigurationValue = 1;
return ctx;
}
static int mouse_default_claim(struct razer_mouse *m)
{
return razer_generic_usb_claim_refcount(m->usb_ctx, &m->claim_count);
}
static int mouse_default_release(struct razer_mouse *m)
{
int err = 0;
if (m->claim_count == 1) {
if (m->commit)
err = m->commit(m, 0);
}
razer_generic_usb_release_refcount(m->usb_ctx, &m->claim_count);
return err;
}
static struct razer_mouse * mouse_new(const struct razer_usb_device *id,
struct libusb_device *udev)
{
struct razer_event_data ev;
struct razer_mouse *m;
int err;
libusb_ref_device(udev);
m = zalloc(sizeof(*m));
if (!m)
return NULL;
m->usb_ctx = razer_create_usb_ctx(udev);
if (!m->usb_ctx)
goto err_free_mouse;
/* Set default values and callbacks */
m->nr_profiles = 1;
m->claim = mouse_default_claim;
m->release = mouse_default_release;
/* Call the driver init */
m->base_ops = id->u.mouse_ops;
err = m->base_ops->init(m, udev);
if (err)
goto err_free_ctx;
udev = m->usb_ctx->dev;
if (WARN_ON(m->nr_profiles <= 0))
goto err_release;
if (m->nr_profiles == 1 && !m->get_active_profile)
m->get_active_profile = m->get_profiles;
if (profile_emu_enabled && m->nr_profiles == 1) {
err = razer_mouse_init_profile_emulation(m);
if (err)
goto err_release;
}
mouse_apply_initial_config(m);
razer_debug("Allocated and initialized new mouse \"%s\"\n",
m->idstr);
ev.u.mouse = m;
razer_notify_event(RAZER_EV_MOUSE_ADD, &ev);
return m;
err_release:
m->base_ops->release(m);
err_free_ctx:
razer_free(m->usb_ctx, sizeof(*(m->usb_ctx)));
err_free_mouse:
razer_free(m, sizeof(*m));
libusb_unref_device(udev);
return NULL;
}
static void razer_free_mouse(struct razer_mouse *m)
{
struct razer_event_data ev;
razer_debug("Freeing mouse (type=%d)\n",
m->base_ops->type);
ev.u.mouse = m;
razer_notify_event(RAZER_EV_MOUSE_REMOVE, &ev);
if (m->release == mouse_default_release) {
while (m->claim_count)
m->release(m);
}
razer_mouse_exit_profile_emulation(m);
m->base_ops->release(m);
libusb_unref_device(m->usb_ctx->dev);
razer_free(m->usb_ctx, sizeof(*(m->usb_ctx)));
razer_free(m, sizeof(*m));
}
static void razer_free_mice(struct razer_mouse *mouse_list)
{
struct razer_mouse *mouse, *next;
for (mouse = mouse_list; mouse; ) {
next = mouse->next;
razer_free_mouse(mouse);
mouse = next;
}
}
struct new_razer_usb_device {
const struct razer_usb_device *id;
struct usb_device *udev;
};
struct razer_mouse * razer_rescan_mice(void)
{
struct libusb_device **devlist, *dev;
ssize_t nr_devices;
unsigned int i;
int err;
struct libusb_device_descriptor desc;
const struct razer_usb_device *id;
struct razer_mouse *m, *next;
nr_devices = libusb_get_device_list(libusb_ctx, &devlist);
if (nr_devices < 0) {
razer_error("razer_rescan_mice: Failed to get USB device list\n");
return NULL;
}
for (i = 0; i < nr_devices; i++) {
dev = devlist[i];
err = libusb_get_device_descriptor(dev, &desc);
if (err) {
razer_error("razer_rescan_mice: Failed to get descriptor\n");
continue;
}
id = usbdev_lookup(&desc);
if (!id || id->type != RAZER_DEVTYPE_MOUSE)
continue;
m = mouse_list_find(mice_list, dev);
if (m) {
/* We already had this mouse */
m->flags |= RAZER_MOUSEFLG_PRESENT;
} else {
/* We don't have this mouse, yet. Create a new one */
m = mouse_new(id, dev);
if (m) {
m->flags |= RAZER_MOUSEFLG_PRESENT;
mouse_list_add(&mice_list, m);
}
}
}
/* Remove mice that are not connected anymore. */
razer_for_each_mouse(m, next, mice_list) {
if (m->flags & RAZER_MOUSEFLG_PRESENT) {
m->flags &= ~RAZER_MOUSEFLG_PRESENT;
continue;
}
mouse_list_del(&mice_list, m);
razer_free_mouse(m);
}
libusb_free_device_list(devlist, 1);
return mice_list;
}
int razer_reconfig_mice(void)
{
struct razer_mouse *m, *next;
int err;
razer_for_each_mouse(m, next, mice_list) {
err = m->claim(m);
if (err)
return err;
if (m->commit)
err = m->commit(m, 1);
m->release(m);
if (err)
return err;
}
return 0;
}
void razer_free_freq_list(enum razer_mouse_freq *freq_list, int count)
{
if (freq_list)
free(freq_list);
}
void razer_free_resolution_list(enum razer_mouse_res *res_list, int count)
{
if (res_list)
free(res_list);
}
void razer_free_leds(struct razer_led *led_list)
{
struct razer_led *led, *next;
for (led = led_list; led; ) {
next = led->next;
free(led);
led = next;
}
}
int razer_init(int enable_profile_emu)
{
int err = 0;
if (!razer_initialized())
err = libusb_init(&libusb_ctx);
if (!err)
profile_emu_enabled = enable_profile_emu;
return err ? -EINVAL : 0;
}
void razer_exit(void)
{
if (!razer_initialized())
return;
razer_free_mice(mice_list);
mice_list = NULL;
config_file_free(razer_config_file);
razer_config_file = NULL;
libusb_exit(libusb_ctx);
libusb_ctx = NULL;
}
int razer_usb_add_used_interface(struct razer_usb_context *ctx,
int bInterfaceNumber,
int bAlternateSetting)
{
struct razer_usb_interface *interf;
if (ctx->nr_interfaces >= ARRAY_SIZE(ctx->interfaces)) {
razer_error("USB context interface array overflow\n");
return -ENOSPC;
}
interf = &ctx->interfaces[ctx->nr_interfaces];
interf->bInterfaceNumber = bInterfaceNumber;
interf->bAlternateSetting = bAlternateSetting;
ctx->nr_interfaces++;
return 0;
}
static void razer_reattach_usb_kdrv(struct razer_usb_context *ctx,
int bInterfaceNumber)
{
int res;
res = libusb_kernel_driver_active(ctx->h, bInterfaceNumber);
if (res == 1)
return;
if (res) {
razer_error("Failed to get kernel driver state\n");
return;
}
res = libusb_attach_kernel_driver(ctx->h, bInterfaceNumber);
if (res) {
razer_error("Failed to reconnect the kernel driver (%d).\n"
"The device most likely won't work now. Try to replug it.\n", res);
return;
}
}
static void razer_usb_release(struct razer_usb_context *ctx,
int bInterfaceNumber)
{
libusb_release_interface(ctx->h, bInterfaceNumber);
razer_reattach_usb_kdrv(ctx, bInterfaceNumber);
}
int razer_generic_usb_claim(struct razer_usb_context *ctx)
{
unsigned int tries, i;
int err, config;
struct razer_usb_interface *interf;
err = libusb_open(ctx->dev, &ctx->h);
if (err) {
razer_error("razer_generic_usb_claim: Failed to open USB device\n");
return -ENODEV;
}
/* Detach kernel drivers for all interfaces. */
for (i = 0; i < ctx->nr_interfaces; i++) {
interf = &ctx->interfaces[i];
err = libusb_kernel_driver_active(ctx->h, interf->bInterfaceNumber);
if (err == 1) {
err = libusb_detach_kernel_driver(ctx->h, interf->bInterfaceNumber);
if (err) {
err = -EBUSY;
razer_error("Failed to detach kernel driver\n");
goto err_close;
}
} else if (err) {
err = -ENODEV;
razer_error("Failed to get kernel driver state\n");
goto err_close;
}
}
tries = 0;
while (1) {
if (tries >= 10) {
razer_error("razer_generic_usb_claim: Failed to claim config\n");
goto err_close;
}
/* Select the correct configuration */
err = libusb_get_configuration(ctx->h, &config);
if (err) {
err = -EBUSY;
razer_error("razer_generic_usb_claim: Failed to get configuration\n");
goto err_close;
}
if (config != ctx->bConfigurationValue) {
err = libusb_set_configuration(ctx->h, ctx->bConfigurationValue);
if (err) {
err = -EBUSY;
razer_error("razer_generic_usb_claim: Failed to set configuration\n");
goto err_close;
}
}
/* And finally claim all interfaces. */
for (i = 0; i < ctx->nr_interfaces; i++) {
interf = &ctx->interfaces[i];
err = libusb_claim_interface(ctx->h, interf->bInterfaceNumber);
if (err) {
err = -EIO;
razer_error("Failed to claim USB interface\n");
goto err_close;
}
err = libusb_set_interface_alt_setting(ctx->h, interf->bInterfaceNumber,
interf->bAlternateSetting);
if (err) {
/* This error triggers on some devices.
* Just print a warning and ignore. */
razer_info("razer_generic_usb_claim: "
"Failed to set alt setting %d on interface %d. "
"Ignoring...\n",
(int)interf->bAlternateSetting,
(int)interf->bInterfaceNumber);
}
}
/* To make sure there was no race, check config value again */
err = libusb_get_configuration(ctx->h, &config);
if (err) {
err = -EBUSY;
razer_error("razer_generic_usb_claim: Failed to get configuration\n");
goto err_close;
}
if (config == ctx->bConfigurationValue)
break;
razer_msleep(100);
tries++;
}
return 0;
err_close:
libusb_close(ctx->h);
return err;
}
int razer_generic_usb_claim_refcount(struct razer_usb_context *ctx,
unsigned int *refcount)
{
int err;
if (!(*refcount)) {
err = razer_generic_usb_claim(ctx);
if (err)
return err;
}
(*refcount)++;
return 0;
}
void razer_generic_usb_release(struct razer_usb_context *ctx)
{
int i;
for (i = ctx->nr_interfaces - 1; i >= 0; i--)
razer_usb_release(ctx, ctx->interfaces[i].bInterfaceNumber);
libusb_close(ctx->h);
}
void razer_generic_usb_release_refcount(struct razer_usb_context *ctx,
unsigned int *refcount)
{
if (*refcount) {
(*refcount)--;
if (!(*refcount))
razer_generic_usb_release(ctx);
}
}
void razer_generic_usb_gen_idstr(struct libusb_device *udev,
struct libusb_device_handle *h,
const char *devname,
bool include_devicenr,
const char *serial,
char *idstr_buf)
{
char devid[96];
char serial_buf[64];
char buspos[16];
char c;
size_t i;
unsigned int serial_index = 0;
int err;
struct libusb_device_descriptor devdesc;
struct razer_usb_context usbctx = {
.dev = udev,
.h = h,
};
err = libusb_get_device_descriptor(udev, &devdesc);
if (err) {
razer_error("razer_generic_usb_gen_idstr: Failed to get "
"device descriptor (%d)\n", err);
return;
}
if (serial && strlen(serial)) {
/* Enforce ASCII characters. */
for (i = 0; i < ARRAY_SIZE(serial_buf) - 1; i++) {
c = serial[i];
if (c == '\0')
break;
if ((unsigned char)c <= 0x7Fu)
serial_buf[i] = c;
else
serial_buf[i] = '?'; /* Non-ASCII char. */
}
serial_buf[i] = '\0';
serial = serial_buf;
} else {
serial_buf[0] = '\0';
serial_index = devdesc.iSerialNumber;
err = -EINVAL;
if (serial_index) {
err = 0;
if (!h)
err = razer_generic_usb_claim(&usbctx);
if (err) {
razer_error("Failed to claim device for serial fetching.\n");
} else {
err = libusb_get_string_descriptor_ascii(
usbctx.h, serial_index,
(unsigned char *)serial_buf, sizeof(serial_buf));
if (!h)
razer_generic_usb_release(&usbctx);
/* Enforce ASCII characters. */
for (i = 0; i < ARRAY_SIZE(serial_buf); i++) {
if (serial_buf[i] == '\0')
break;
if ((unsigned char)serial_buf[i] > 0x7Fu)
serial_buf[i] = '?'; /* Non-ASCII char. */
}
}
}
if (err <= 0)
strcpy(serial_buf, "0");
serial = serial_buf;
}
snprintf(devid, sizeof(devid), "%04X-%04X-%s",
devdesc.idVendor,
devdesc.idProduct, serial);
if (include_devicenr) {
snprintf(buspos, sizeof(buspos), "%03d-%03d",
libusb_get_bus_number(udev),
libusb_get_device_address(udev));
} else {
snprintf(buspos, sizeof(buspos), "%03d",
libusb_get_bus_number(udev));
}
razer_create_idstr(idstr_buf, BUSTYPESTR_USB, buspos,
DEVTYPESTR_MOUSE, devname, devid);
}
/** razer_usb_force_hub_reset
* Force reset of the hub the specified device is on
*/
int razer_usb_force_hub_reset(struct razer_usb_context *device_ctx)
{
struct libusb_device_handle *h;
struct libusb_device *hub = NULL, *dev;
uint8_t hub_bus_number, hub_device_address;
struct razer_usb_reconnect_guard rg;
int err;
struct libusb_device **devlist;
ssize_t devlist_size, i;
razer_debug("Forcing hub reset for device %03u:%03u\n",
libusb_get_bus_number(device_ctx->dev),
libusb_get_device_address(device_ctx->dev));
razer_usb_reconnect_guard_init(&rg, device_ctx);
hub_bus_number = libusb_get_bus_number(device_ctx->dev);
hub_device_address = 1; /* Constant */
devlist_size = libusb_get_device_list(libusb_ctx, &devlist);
for (i = 0; i < devlist_size; i++) {
dev = devlist[i];
if (libusb_get_bus_number(dev) == hub_bus_number &&
libusb_get_device_address(dev) == hub_device_address) {
hub = dev;
break;
}
}
if (!hub) {
razer_error("razer_usb_force_reinit: Failed to find hub\n");
err = -ENODEV;
goto error;
}
razer_debug("Resetting root hub %03u:%03u\n",
hub_bus_number, hub_device_address);
err = libusb_open(hub, &h);
if (err) {
razer_error("razer_usb_force_reinit: Failed to open hub device\n");
err = -ENODEV;
goto error;
}
libusb_reset_device(h);
libusb_close(h);
err = razer_usb_reconnect_guard_wait(&rg, 1);
if (err) {
razer_error("razer_usb_force_reinit: "
"Failed to discover the reconnected device\n");
goto error;
}
razer_debug("Hub reset completed. Device rediscovered as %03u:%03u\n",
libusb_get_bus_number(device_ctx->dev),
libusb_get_device_address(device_ctx->dev));
err = 0;
error:
libusb_free_device_list(devlist, 1);
return err;
}
/** razer_usb_reconnect_guard_init - Init the reconnect-guard context
*
* Call this _before_ triggering any device operations that might
* reset the device.
*/
int razer_usb_reconnect_guard_init(struct razer_usb_reconnect_guard *guard,
struct razer_usb_context *ctx)
{
int err;
guard->ctx = ctx;
err = libusb_get_device_descriptor(ctx->dev, &guard->old_desc);
if (err) {
razer_error("razer_usb_reconnect_guard_init: Failed to "
"get device descriptor\n");
return err;
}
guard->old_busnr = libusb_get_bus_number(ctx->dev);
guard->old_devaddr = libusb_get_device_address(ctx->dev);
return 0;
}
static struct libusb_device * guard_find_usb_dev(const struct libusb_device_descriptor *expected_desc,
uint8_t expected_bus_number,
uint8_t expected_dev_addr,
bool exact_match)
{
struct libusb_device **devlist, *dev;
struct libusb_device_descriptor desc;
uint8_t dev_addr;
ssize_t nr_devices, i, j;
int err;
nr_devices = libusb_get_device_list(libusb_ctx, &devlist);
if (nr_devices < 0) {
razer_error("guard_find_usb_dev: Failed to get device list\n");
return NULL;
}
for (i = 0; i < nr_devices; i++) {
dev = devlist[i];
if (libusb_get_bus_number(dev) != expected_bus_number)
continue;
err = libusb_get_device_descriptor(dev, &desc);
if (err)
continue;
if (memcmp(&desc, expected_desc, sizeof(desc)) != 0)
continue;
dev_addr = libusb_get_device_address(dev);
if (exact_match) {
if (dev_addr == expected_dev_addr)
goto found_dev;
} else {
for (j = 0; j < 64; j++) {
if (dev_addr == ((expected_dev_addr + j) & 0x7F))
goto found_dev;
}
}
}
libusb_free_device_list(devlist, 1);
return NULL;
found_dev:
libusb_ref_device(dev);
libusb_free_device_list(devlist, 1);
return dev;
}
/** razer_usb_reconnect_guard_wait - Protect against a firmware reconnect.
*
* If the firmware does a reconnect of the device on the USB bus, this
* function tries to keep track of the device and it will update the
* usb context information.
* Of course, this is not completely race-free, but we try to do our best.
*
* hub_reset is true, if the device reconnects due to a HUB reset event.
* Otherwise it's assumed that the device reconnects on behalf of itself.
* If hub_reset is false, the device is expected to be claimed.
*/
int razer_usb_reconnect_guard_wait(struct razer_usb_reconnect_guard *guard, bool hub_reset)
{
uint8_t reconn_dev_addr;
uint8_t old_dev_addr = guard->old_devaddr;
uint8_t old_bus_number = guard->old_busnr;
int res, errorcode = 0;
struct libusb_device *dev;
struct timeval now, timeout;
if (!hub_reset) {
/* Release the device, so the kernel can detect the bus reconnect. */
razer_generic_usb_release(guard->ctx);
}
/* Wait for the device to disconnect. */
gettimeofday(&timeout, NULL);
razer_timeval_add_msec(&timeout, 3000);
while (1) {
dev = guard_find_usb_dev(&guard->old_desc,
old_bus_number, old_dev_addr, 1);
if (!dev)
break;
libusb_unref_device(dev);
gettimeofday(&now, NULL);
if (razer_timeval_after(&now, &timeout)) {
/* Timeout. Hm. It seems the device won't reconnect.
* That's probably OK. Reclaim it. */
razer_error("razer_usb_reconnect_guard: "
"The device did not disconnect! If it "
"does not work anymore, try to replug it.\n");
goto reclaim;
}
razer_msleep(50);
}
/* Construct the device address it will reconnect on.
* On a device reset the new dev addr will be >= reconn_dev_addr.
*/
reconn_dev_addr = (old_dev_addr + 1) & 0x7F;
/* Wait for the device to reconnect. */
gettimeofday(&timeout, NULL);
razer_timeval_add_msec(&timeout, 3000);
while (1) {
dev = guard_find_usb_dev(&guard->old_desc,
old_bus_number, reconn_dev_addr, 0);
if (dev)
break;
gettimeofday(&now, NULL);
if (razer_timeval_after(&now, &timeout)) {
razer_error("razer_usb_reconnect_guard: The device did not "
"reconnect! It might not work anymore. Try to replug it.\n");
razer_debug("Expected reconnect busid was: %02u:>=%03u\n",
old_dev_addr, reconn_dev_addr);
errorcode = -EBUSY;
goto out;
}
razer_msleep(50);
}
/* Update the USB context. */
libusb_unref_device(guard->ctx->dev);
guard->ctx->dev = dev;
reclaim:
if (!hub_reset) {
/* Reclaim the new device. */
res = razer_generic_usb_claim(guard->ctx);
if (res) {
razer_error("razer_usb_reconnect_guard: Reclaim failed.\n");
return res;
}
}
out:
return errorcode;
}
int razer_load_config(const char *path)
{
struct config_file *conf = NULL;
if (!razer_initialized())
return -EINVAL;
if (!path)
path = RAZER_DEFAULT_CONFIG;
if (strlen(path)) {
conf = config_file_parse(path, 1);
if (!conf)
return -ENOENT;
}
config_file_free(razer_config_file);
razer_config_file = conf;
return 0;
}
void razer_set_logging(razer_logfunc_t info_callback,
razer_logfunc_t error_callback,
razer_logfunc_t debug_callback)
{
razer_logfunc_info = info_callback;
razer_logfunc_error = error_callback;
razer_logfunc_debug = debug_callback;
}
static void do_init_axis(struct razer_axis *axis,
unsigned int id, const char *name, unsigned int flags)
{
if (name) {
axis->id = id;
axis->name = name;
axis->flags = flags;
}
}
void razer_init_axes(struct razer_axis *axes,
const char *name0, unsigned int flags0,
const char *name1, unsigned int flags1,
const char *name2, unsigned int flags2)
{
do_init_axis(&axes[0], 0, name0, flags0);
do_init_axis(&axes[1], 1, name1, flags1);
do_init_axis(&axes[2], 2, name2, flags2);
}
struct razer_mouse_dpimapping * razer_mouse_get_dpimapping_by_res(
struct razer_mouse_dpimapping *mappings, size_t nr_mappings,
enum razer_dimension dim, enum razer_mouse_res res)
{
struct razer_mouse_dpimapping *mapping = NULL;
size_t i;
for (i = 0; i < nr_mappings; i++) {
if (mappings[i].res[dim] == res) {
mapping = &mappings[i];
break;
}
}
return mapping;
}
void razer_event_spacing_init(struct razer_event_spacing *es,
unsigned int msec)
{
memset(es, 0, sizeof(*es));
es->spacing_msec = msec;
}
void razer_event_spacing_enter(struct razer_event_spacing *es)
{
struct timeval now, deadline;
int wait_msec;
gettimeofday(&now, NULL);
deadline = es->last_event;
razer_timeval_add_msec(&deadline, es->spacing_msec);
if (razer_timeval_after(&deadline, &now)) {
/* We have to sleep long enough to ensure we're
* after the deadline. */
wait_msec = razer_timeval_msec_diff(&deadline, &now);
WARN_ON(wait_msec < 0);
razer_msleep(wait_msec + 1);
gettimeofday(&now, NULL);
razer_error_on(razer_timeval_after(&deadline, &now),
"Failed to maintain event spacing\n");
}
}
void razer_event_spacing_leave(struct razer_event_spacing *es)
{
gettimeofday(&es->last_event, NULL);
}
|