-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlUsbDev.cpp
900 lines (812 loc) · 29.3 KB
/
BlUsbDev.cpp
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
/*****************************************************************************/
/* BlUsbDev.cpp : implementation of the USB device communication classes */
/*****************************************************************************/
/*
Copyright (C) 2019 Hermann Seib
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, version 3.
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.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "wxStd.h"
#include "BlUsbDev.h"
using namespace std;
// For original firmware,
// #define OLD_VERSION
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
#define USB_ENABLE_VENDOR_RQ 0x11
#define USB_DISABLE_VENDOR_RQ 0x10
#ifdef OLD_VERSION
#define USB_LED_ON 0x21
#define USB_LED_OFF 0x20
#else
#define USB_READ_BR 0x20
#define USB_WRITE_BR 0x21
#endif
#define USB_READ_MATRIX 0x30
#define USB_READ_LAYOUT 0x40
#ifdef OLD_VERSION
#define USB_WRITE_LAYOUT 0x50
#else
#define USB_WRITE_LAYOUT_OLD 0x50
#define USB_WRITE_LAYOUT 0x41
#define USB_READ_DEBOUNCE 0x50
#define USB_WRITE_DEBOUNCE 0x51
#define USB_READ_MACROS 0x60
#define USB_WRITE_MACROS 0x61
#define USB_READ_VERSION 0x70
#endif
// Definitions for V1.5 and above:
// interface numbers application
#define HID_IFACE_NUMBER_KBD 0
#define HID_IFACE_NUMBER_MEDIA_CTRL_FEAT 1
// HID report ids for bootloader
#define HID_REPORT_ID_PAGE_DATA 0x01
#define HID_REPORT_ID_EXIT_BOOTLOADER 0x02
// HID report ids for application
// interface 0 - no ids used
#define HID_REPORT_ID_BOOT 0x00
#define HID_REPORT_ID_KEYBOARD 0x01
// interface 1
#define HID_REPORT_ID_MEDIA 0x02
#define HID_REPORT_ID_SYSCTRL 0x03
#define HID_REPORT_ID_FEATURE_READ_WRITE_LAYOUT 0x01
#define HID_REPORT_ID_FEATURE_READ_WRITE_MACROS 0x02
#define HID_REPORT_ID_FEATURE_READ_MATRIX 0x03
#define HID_REPORT_ID_FEATURE_READ_WRITE_BR 0x04
#define HID_REPORT_ID_FEATURE_READ_VERSION 0x05
#define HID_REPORT_ID_FEATURE_READ_WRITE_DEBOUNCE 0x06
#define HID_REPORT_ID_FEATURE_ENTER_BOOTLOADER 0x07
#define SPM_PAGESIZE 256
// vendor request ids
#define USB_ENTER_BOOTLOADER 0xFF
#define USB_EXIT_BOOTLOADER 0xFE
// defined in layout.h; SHOULD be unnecessary in this low-level code, but isn't
#ifndef NUM_MACROKEYS
#define NUM_MACROKEYS 24
#define LEN_MACRO 8
#endif
/*****************************************************************************/
/* Enumerations */
/*****************************************************************************/
enum blusb_request_recipient
{
BLUSB_RECIPIENT_DEVICE = 0x00,
BLUSB_RECIPIENT_INTERFACE = 0x01,
BLUSB_RECIPIENT_ENDPOINT = 0x02,
BLUSB_RECIPIENT_OTHER = 0x03,
};
enum blusb_endpoint_direction
{
BLUSB_ENDPOINT_IN = 0x80,
BLUSB_ENDPOINT_OUT = 0x00,
};
enum blusb_request_type
{
BLUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
BLUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
BLUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
BLUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
};
enum blusb_report_type
{
BLUSB_REQUEST_GET_REPORT = 1,
BLUSB_REQUEST_SET_REPORT = 9,
BLUSB_REQUEST_IN_REPORT = 1 << 8,
BLUSB_REQUEST_OUT_REPORT = 2 << 8,
BLUSB_REQUEST_FEATURE_REPORT = 3 << 8,
};
/*===========================================================================*/
/* Structure Definitions */
/*===========================================================================*/
// we need to make sure the members of the struct and union are byte-aligned and packed contiguously
// to be able to cast them over the memory buffer
#pragma pack(push, 1)
union hid_page_data_report_t
{
struct
{
wxUint8 id;
wxUint16 page_address;
wxUint8 page_data[SPM_PAGESIZE];
};
wxUint8 buffer[SPM_PAGESIZE + 3];
};
union hid_layout_data_report_t
{
struct
{
wxUint8 id;
wxUint8 num_pgs;
wxUint8 pg_cnt;
wxUint8 page_data[SPM_PAGESIZE];
};
wxUint8 buffer[3 + SPM_PAGESIZE];
};
union hid_macro_data_report_t
{
struct
{
wxUint8 id;
wxUint8 macro_data[NUM_MACROKEYS * LEN_MACRO];
};
wxUint8 buffer[1 + NUM_MACROKEYS * LEN_MACRO];
};
union hid_ctrl_report_t
{
struct
{
wxUint8 id;
wxUint8 payload[7];
};
wxUint8 buffer[8];
};
#pragma pack(pop)
/*===========================================================================*/
/* BlUsbDev : USB device communication class members */
/*===========================================================================*/
/*****************************************************************************/
/* BlUsbDev : constructor */
/*****************************************************************************/
BlUsbDev::BlUsbDev(void)
{
handle = NULL;
blVer[0] = blVer[1] = 0x00;
}
/*****************************************************************************/
/* ~BlUsbDev : destructor */
/*****************************************************************************/
BlUsbDev::~BlUsbDev(void)
{
Close();
}
/*****************************************************************************/
/* Open : opens the first attached Model M */
/*****************************************************************************/
int BlUsbDev::Open
(
wxUint16 vendor,
wxUint16 product,
wxUint16 Usage,
wxUint16 UsagePage
)
{
if (IsOpen())
return BLUSB_SUCCESS;
vector<void*> devList;
UsbLLDevDesc desc;
handle = NULL;
int rc = BLUSB_ERROR_NOT_FOUND;
int cnt = GetDeviceList(devList);
for (int i = 0; i < cnt; i++)
{
if (GetDeviceDescriptor(devList[i], desc) == BLUSB_SUCCESS &&
vendor == desc.VendorID &&
product == desc.ProductID &&
Usage == desc.UsageID &&
UsagePage == desc.UsagePage)
{
rc = UsbLL::Open(devList[i], handle);
if (rc == BLUSB_SUCCESS)
ReadVersion(blVer, sizeof(blVer));
break;
}
}
FreeDeviceList(devList);
return rc;
}
/*****************************************************************************/
/* EnableServiceMode : put Model M into service mode */
/*****************************************************************************/
int BlUsbDev::EnableServiceMode()
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
if (GetFwVersion() >= 0x0105) /* not available in V1.5 and above */
return BLUSB_ERROR_NOT_SUPPORTED; /* so just ignore the call */
return ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_ENABLE_VENDOR_RQ,
0, 0, 0, 0, 1000);
}
/*****************************************************************************/
/* DisableServiceMode : put Model M into normal operation mode */
/*****************************************************************************/
int BlUsbDev::DisableServiceMode()
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
if (GetFwVersion() >= 0x0105) /* not available in V1.5 and above */
return BLUSB_ERROR_NOT_SUPPORTED; /* so just ignore the call */
return ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_DISABLE_VENDOR_RQ,
0, 0, 0, 0, 1000);
}
/*****************************************************************************/
/* ReadVersion : read firmware version */
/*****************************************************************************/
int BlUsbDev::ReadVersion(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = 0;
hid_ctrl_report_t ctrl = {0};
// this one is needed to DETERMINE the version ...
// if (GetFwVersion() >= 0x0105)
// so first, try V1.5++ method
ctrl.id = HID_REPORT_ID_FEATURE_READ_VERSION;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_VERSION,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
// if that did not return a sufficiently large buffer, try old method
if (rc < 2)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_VERSION,
0, 0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
if (rc >= BLUSB_SUCCESS)
for (int i = 0; i < rc && i < buflen; i++)
buffer[i] = ctrl.buffer[i];
#ifdef WIN32
else // obviously a pre-v1.5 keyboard with standard HID driver
{ // so treat it as v1.4 - everything will fail, however.
buffer[0] = 0x01;
buffer[1] = 0x04;
}
#endif
return rc > buflen ? buflen : rc;
}
/*****************************************************************************/
/* ReadPWM : read PWM values for USB / Bluetooth operation */
/*****************************************************************************/
int BlUsbDev::ReadPWM(wxUint8 &pwmUSB, wxUint8 &pwmBT)
{
pwmUSB = pwmBT = 0;
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = 0;
hid_ctrl_report_t ctrl = {0};
if (GetFwVersion() >= 0x0105)
{
// try V1.5++ method
ctrl.id = HID_REPORT_ID_FEATURE_READ_WRITE_BR;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_BR,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
// if that did not return a sufficiently large buffer, try old method
if (rc < 2)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_BR,
0, 0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
if (rc >= 1)
pwmUSB = ctrl.buffer[0];
if (rc >= 2)
pwmBT = ctrl.buffer[1];
return rc;
}
/*****************************************************************************/
/* WritePWM : write PWM values for USB / Bluetooth operation */
/*****************************************************************************/
int BlUsbDev::WritePWM(wxUint8 pwmUSB, wxUint8 pwmBT)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_ctrl_report_t ctrl = {0};
ctrl.id = HID_REPORT_ID_FEATURE_READ_WRITE_BR;
ctrl.payload[0] = pwmUSB;
ctrl.payload[1] = pwmBT;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_BR,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
// if that did not return OK, try old method
if (rc < BLUSB_SUCCESS)
{
wxUint8 buffer[8] = { pwmUSB, pwmBT, 0 };
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_WRITE_BR,
0, 0,
buffer, sizeof(buffer),
1000);
}
return rc;
}
/*****************************************************************************/
/* ReadMatrix : read current matrix data (only in service mode!) */
/*****************************************************************************/
int BlUsbDev::ReadMatrix(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = 0;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_ctrl_report_t ctrl = {0};
ctrl.id = HID_REPORT_ID_FEATURE_READ_MATRIX;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_MATRIX,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
if (rc >= 2)
memcpy(buffer, ctrl.buffer, min(buflen, rc));
}
// if that did not return a sufficiently large buffer, try old method
if (rc < 2)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_MATRIX,
#ifdef OLD_VERSION
0, 1,
#else
0, 0,
#endif
buffer, buflen,
1000);
}
return rc;
}
/*****************************************************************************/
/* ReadLayout : read current keyboard layout (Model M transfer format) */
/*****************************************************************************/
int BlUsbDev::ReadLayout(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_layout_data_report_t layout = {0};
layout.id = HID_REPORT_ID_FEATURE_READ_WRITE_LAYOUT;
int totlen = 0;
rc = BLUSB_SUCCESS;
while (rc >= BLUSB_SUCCESS)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_LAYOUT,
0,
layout.buffer, sizeof(layout.buffer),
1000);
if (rc >= sizeof(layout.buffer))
{
if (layout.num_pgs < 1)
break;
if (layout.pg_cnt < 1)
return BLUSB_ERROR_OTHER;
int tgtoff = SPM_PAGESIZE*(layout.pg_cnt - 1);
if (tgtoff + SPM_PAGESIZE <= buflen)
{
memcpy(buffer + tgtoff, layout.page_data, SPM_PAGESIZE);
if (tgtoff + SPM_PAGESIZE > totlen)
totlen = tgtoff + SPM_PAGESIZE;
}
}
if (rc < 3 || layout.pg_cnt >= layout.num_pgs)
break;
}
rc = totlen;
}
// if that did not work, try old method
if (rc < 0)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_ENDPOINT |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_LAYOUT,
0, 0,
buffer, buflen,
1000);
}
return rc;
}
/*****************************************************************************/
/* WriteLayout : write current layout to Model M (Model M transfer format) */
/*****************************************************************************/
int BlUsbDev::WriteLayout(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
int sent = 0;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_layout_data_report_t layout = {0};
layout.id = HID_REPORT_ID_FEATURE_READ_WRITE_LAYOUT;
// calculate number of pages necessary
layout.num_pgs = (buflen + SPM_PAGESIZE - 1) / SPM_PAGESIZE;
// set current page, starting with 1
layout.pg_cnt = 1;
while (1)
{
// fill layout buffer and send
int pgoff = SPM_PAGESIZE*(layout.pg_cnt - 1);
int pglen = min(SPM_PAGESIZE, buflen - pgoff);
memcpy(layout.page_data, buffer + pgoff, pglen);
if (pglen < SPM_PAGESIZE)
memset(layout.page_data + pglen, 0, SPM_PAGESIZE - pglen);
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_LAYOUT,
0,
layout.buffer, sizeof(layout.buffer),
1000);
if (rc < BLUSB_SUCCESS)
break;
sent += rc - 3;
if (layout.pg_cnt == layout.num_pgs)
{
rc = sent;
break;
}
layout.pg_cnt++;
}
}
if (rc <= 0)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_ENDPOINT |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_WRITE_LAYOUT,
0, 0,
buffer, buflen,
1000);
#ifdef USB_WRITE_LAYOUT_OLD
if (rc < BLUSB_SUCCESS)
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_ENDPOINT |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_WRITE_LAYOUT_OLD,
0, 0,
buffer, buflen,
1000);
#endif
}
return rc;
}
/*****************************************************************************/
/* ReadDebounce : read current debounce from Model M */
/*****************************************************************************/
int BlUsbDev::ReadDebounce()
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
hid_ctrl_report_t ctrl = {0};
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
ctrl.id = HID_REPORT_ID_FEATURE_READ_WRITE_DEBOUNCE;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_DEBOUNCE,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
// if that did not return a sufficiently large buffer, try old method
if (rc < 1)
{
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_DEBOUNCE,
0, 0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
if (rc >= BLUSB_SUCCESS)
return ctrl.buffer[GetFwMajorVersion() ? 0 : 4];
}
else
return ctrl.buffer[0];
return rc;
}
/*****************************************************************************/
/* WriteDebounce : write debounce to Model M */
/*****************************************************************************/
int BlUsbDev::WriteDebounce(int nDebounce)
{
if (nDebounce < 1 || nDebounce > 255)
return BLUSB_ERROR_INVALID_PARAM;
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
hid_ctrl_report_t ctrl = {0};
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
ctrl.id = HID_REPORT_ID_FEATURE_READ_WRITE_DEBOUNCE;
ctrl.payload[0] = (wxUint8)nDebounce;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_DEBOUNCE,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
// if that did not return OK, try old method
if (rc < BLUSB_SUCCESS)
{
ctrl.buffer[GetFwMajorVersion() ? 0 : 4] = (wxUint8)nDebounce;
return ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_WRITE_DEBOUNCE,
0, 0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
return rc;
}
/*****************************************************************************/
/* ReadMacros : read the macro set */
/*****************************************************************************/
int BlUsbDev::ReadMacros(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_macro_data_report_t macros = {0};
macros.id = HID_REPORT_ID_FEATURE_READ_WRITE_MACROS;
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_INTERFACE |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_CLASS,
BLUSB_REQUEST_GET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_MACROS,
0,
macros.buffer, sizeof(macros.buffer),
1000);
if (rc >= 1)
for (int i = 0; i < rc && i < buflen; i++)
buffer[i] = macros.buffer[i];
}
// if that did not return a sufficiently large buffer, try old method
if (rc < 1)
{
wxUint8 ibuffer[192] = { 0 };
rc = ControlTransfer(handle,
BLUSB_RECIPIENT_ENDPOINT |
BLUSB_ENDPOINT_IN |
BLUSB_REQUEST_TYPE_VENDOR,
USB_READ_MACROS,
0, 0,
ibuffer, sizeof(ibuffer),
1000);
if (rc >= BLUSB_SUCCESS)
{
for (int i = 0; i < rc && i < buflen; i++)
buffer[i] = ibuffer[i];
}
}
return rc > buflen ? buflen : rc;
}
/*****************************************************************************/
/* WriteMacros : write the macro set */
/*****************************************************************************/
int BlUsbDev::WriteMacros(wxUint8 *buffer, int buflen)
{
if (!IsOpen())
return BLUSB_ERROR_NO_DEVICE;
int rc = BLUSB_ERROR_INVALID_PARAM;
if (GetFwVersion() >= 0x0105)
{
// first, try V1.5++ method
hid_macro_data_report_t macros = {0};
macros.id = HID_REPORT_ID_FEATURE_READ_WRITE_MACROS;
memcpy(macros.macro_data, buffer, min(buflen, NUM_MACROKEYS*LEN_MACRO));
rc = ControlTransfer(handle,
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS |
BLUSB_RECIPIENT_INTERFACE,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_READ_WRITE_MACROS,
0,
macros.buffer, sizeof(macros.buffer),
1000);
}
// if that did not return OK, try old method
if (rc < BLUSB_SUCCESS)
return ControlTransfer(handle,
BLUSB_RECIPIENT_ENDPOINT |
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_VENDOR,
USB_WRITE_MACROS,
0, 0,
buffer, buflen,
1000);
return rc;
}
/*****************************************************************************/
/* EnterBootloader : puts device into boot loader mode */
/*****************************************************************************/
int BlUsbDev::EnterBootloader()
{
if (GetFwVersion() >= 0x0105)
{
// only works in V1.5++
hid_ctrl_report_t ctrl = { 0 };
ctrl.id = HID_REPORT_ID_FEATURE_ENTER_BOOTLOADER;
return ControlTransfer(handle,
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS |
BLUSB_RECIPIENT_INTERFACE,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_FEATURE_ENTER_BOOTLOADER,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
return BLUSB_ERROR_INVALID_PARAM;
}
/*****************************************************************************/
/* ExitBootloader : puts device back into keyboard mode */
/*****************************************************************************/
int BlUsbDev::ExitBootloader()
{
// only works in V1.5++
hid_ctrl_report_t ctrl = { 0 };
ctrl.id = HID_REPORT_ID_EXIT_BOOTLOADER;
return ControlTransfer(handle,
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS |
BLUSB_RECIPIENT_INTERFACE,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_EXIT_BOOTLOADER,
0,
ctrl.buffer, sizeof(ctrl.buffer),
1000);
}
/*****************************************************************************/
/* UpdateFirmware : writes new firmware out to the device */
/*****************************************************************************/
int BlUsbDev::UpdateFirmware
(
wxUint16 startAddr, /* start address to write to */
wxUint16 endAddr, /* end address to write to */
wxUint8 *buffer /* buffer (start = start address) */
)
{
// only works in V1.5++
hid_page_data_report_t hid_data = { 0 };
hid_data.id = HID_REPORT_ID_PAGE_DATA;
int rc = BLUSB_SUCCESS;
// Attention: the following assumes that SPM_PAGESIZE is 2^something.
// If this should ever change, the block calculations need to be redone!
const wxUint16 mask = SPM_PAGESIZE - 1;
wxUint16 pgSend = startAddr & mask;
wxUint16 pgEnd = (startAddr + mask) & ~mask;
if (pgSend >= pgEnd)
return BLUSB_ERROR_INVALID_PARAM;
while (rc >= BLUSB_SUCCESS &&
pgSend < pgEnd)
{
hid_data.page_address = pgSend;
wxUint16 bufOff = pgSend - startAddr;
if (pgSend < startAddr)
{
memset(hid_data.page_data, 0xff, startAddr - pgSend);
memcpy(hid_data.page_data + startAddr - pgSend,
buffer,
SPM_PAGESIZE - (startAddr - pgSend));
}
else if (pgSend + SPM_PAGESIZE > endAddr)
{
memcpy(hid_data.page_data,
buffer + bufOff,
endAddr + 1 - pgSend);
memset(hid_data.page_data + endAddr + 1 - pgSend,
0xff,
SPM_PAGESIZE - (endAddr + 1 - pgSend));
}
else
memcpy(hid_data.page_data,
buffer + bufOff,
SPM_PAGESIZE);
rc = ControlTransfer(handle,
BLUSB_ENDPOINT_OUT |
BLUSB_REQUEST_TYPE_CLASS |
BLUSB_RECIPIENT_INTERFACE,
BLUSB_REQUEST_SET_REPORT,
BLUSB_REQUEST_FEATURE_REPORT |
HID_REPORT_ID_PAGE_DATA,
0,
hid_data.buffer, sizeof(hid_data.buffer),
1000);
pgSend += SPM_PAGESIZE;
}
return rc;
}