/
usb_comm.c
1830 lines (1715 loc) · 51.2 KB
/
usb_comm.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
/*****************************************************************************/
/* usb_comm.c : USB communication handling for hardware USB AVRs */
/*****************************************************************************/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/atomic.h>
#include <string.h>
#include "hwdefs.h"
#include "soarer.h"
#include "intkeys.h"
#include "salloc.h"
#include "processing.h"
#include "usb_comm.h"
#define EP_TYPE_CONTROL 0x00
#define EP_TYPE_BULK_IN 0x81
#define EP_TYPE_BULK_OUT 0x80
#define EP_TYPE_INTERRUPT_IN 0xC1
#define EP_TYPE_INTERRUPT_OUT 0xC0
#define EP_TYPE_ISOCHRONOUS_IN 0x41
#define EP_TYPE_ISOCHRONOUS_OUT 0x40
#define EP_SINGLE_BUFFER 0x02
#define EP_DOUBLE_BUFFER 0x06
#if 1
#define EP_SIZE(s) \
((s) > 32 ? 0x30 : \
(s) > 16 ? 0x20 : \
(s) > 8 ? 0x10 : \
0x00)
#else
// This is too dumb for special cases, but
// perpetuated throughout the AVR products I've seen...
#define EP_SIZE(s) ((s) == 64 ? 0x30 : \
((s) == 32 ? 0x20 : \
((s) == 16 ? 0x10 : \
0x00)))
#endif
#if defined(__AVR_AT90USB162__)
#define HW_CONFIG()
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
#define USB_CONFIG() (USBCON = (1<<USBE))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_ATmega32U4__)
#define HW_CONFIG() (UHWCON = 0x01)
#define PLL_CONFIG() (PLLCSR = 0x12)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_AT90USB646__)
#define HW_CONFIG() (UHWCON = 0x81)
#define PLL_CONFIG() (PLLCSR = 0x1A)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_AT90USB1286__)
#define HW_CONFIG() (UHWCON = 0x81)
#define PLL_CONFIG() (PLLCSR = 0x16)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#endif
// standard control endpoint request types
#define GET_STATUS 0
#define CLEAR_FEATURE 1
#define SET_FEATURE 3
#define SET_ADDRESS 5
#define GET_DESCRIPTOR 6
#define GET_CONFIGURATION 8
#define SET_CONFIGURATION 9
#define GET_INTERFACE 10
#define SET_INTERFACE 11
// HID (human interface device)
#define HID_GET_REPORT 1
#define HID_GET_IDLE 2
#define HID_GET_PROTOCOL 3
#define HID_SET_REPORT 9
#define HID_SET_IDLE 10
#define HID_SET_PROTOCOL 11
// CDC (communication class device)
#define CDC_SET_LINE_CODING 0x20
#define CDC_GET_LINE_CODING 0x21
#define CDC_SET_CONTROL_LINE_STATE 0x22
/**************************************************************************
*
* Configurable Options
*
**************************************************************************/
// You can change these to give your code its own name.
#define STR_MANUFACTURER L"Soarer"
#define STR_PRODUCT L"Soarer's Keyboard Converter"
// Mac OS-X and Linux automatically load the correct drivers. On
// Windows, even though the driver is supplied by Microsoft, an
// INF file is needed to load the driver. These numbers need to
// match the INF file.
#define VENDOR_ID 0x16C0
#define PRODUCT_ID 0x047D
// Here, the various included interfaces can be toggled on and off.
// At least one of them, however, should be present!
#ifndef INC_IF_BOOT
#define INC_IF_BOOT 1 // Boot-compatible keyboard device
#endif
#ifndef INC_IF_KBD
#define INC_IF_KBD 1 // Full capability NKRO keyboard device
#endif
#ifndef INC_IF_DBG
#define INC_IF_DBG 1 // DebugHID Output device
#endif
#ifndef INC_IF_RAW
#define INC_IF_RAW 1 // RawHID Data I/O device
#endif
#define RAWHID_USAGE_PAGE 0xFF99 // recommended: 0xFF00 to 0xFFFF
#define RAWHID_USAGE 0x2468 // recommended: 0x0100 to 0xFFFF
#define DEBUGHID_USAGE_PAGE 0xFF31 // recommended: 0xFF00 to 0xFFFF
#define DEBUGHID_USAGE 0x0074 // recommended: 0x0100 to 0xFFFF
// USB devices are supposed to implement a halt feature, which is
// rarely (if ever) used. If you comment this line out, the halt
// code will be removed, saving 102 bytes of space (gcc 4.3.0).
// This is not strictly USB compliant, but works with all major
// operating systems.
#define SUPPORT_ENDPOINT_HALT
/**************************************************************************
*
* Endpoint Buffer Configuration
*
**************************************************************************/
// Soarer's Converter has a little mixup here...
// keyboard interface has index 2, endpoint 2
// debug interface has index 1, endpoint 3
// IMO, index should be swapped (kbd =>1, debug => 2)
#define ENDPOINT0_SIZE 32
#define BOOT_SIZE 8
#define BOOT_BUFFER EP_DOUBLE_BUFFER
#define KEYBOARD_SIZE 23 //...?
#define KEYBOARD_BUFFER EP_DOUBLE_BUFFER
#define DEBUG_TX_SIZE 32
#define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER
#define RAWHID_TX_BUFFER EP_DOUBLE_BUFFER
#define RAWHID_RX_BUFFER EP_DOUBLE_BUFFER
#define RAWHID_TX_SIZE 64 // transmit packet size
#define RAWHID_TX_INTERVAL 2 // max # of ms between transmit packets
#define RAWHID_RX_SIZE 64 // receive packet size
#define RAWHID_RX_INTERVAL 8 // max # of ms between receive packets
enum /* USB Interface Enumeration: */
{
#if INC_IF_BOOT
IF_Boot, /* Boot Interface */
#endif
#if INC_IF_KBD
IF_Kbd, /* Full Keyboard Interface */
#endif
#if INC_IF_DBG
IF_Debug, /* Debug Interface */
#endif
#if INC_IF_RAW
IF_Raw, /* Raw Interface */
#endif
IF_Count /* Total # interfaces */
};
enum /* USB Endpoint Enumeration : */
{
EP_Default, /* Default end point */
#if INC_IF_BOOT
EP_Boot, /* Boot Interface */
#endif
#if INC_IF_KBD
EP_Kbd, /* Full Keyboard Interface */
#endif
#if INC_IF_DBG
EP_DebugTx, /* Debug Interface */
#endif
#if INC_IF_RAW
EP_RawTx, /* Raw Interface */
EP_RawRx,
#endif
EP_Count /* total (including 0) */
};
// A_035d
static const uint8_t PROGMEM endpoint_config_table[] =
{
#if INC_IF_BOOT
// #1 Boot keyboard endpoint
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(BOOT_SIZE) | BOOT_BUFFER,
#endif
#if INC_IF_KBD
// #2 Keyboard endpoint
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
#endif
#if INC_IF_DBG
// #3 Debug endpoint
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER,
#endif
#if INC_IF_RAW
// #4/#5 Rawhid endpoints
1, EP_TYPE_INTERRUPT_IN, EP_SIZE(RAWHID_TX_SIZE) | RAWHID_TX_BUFFER,
1, EP_TYPE_INTERRUPT_OUT, EP_SIZE(RAWHID_RX_SIZE) | RAWHID_RX_BUFFER
#endif
};
#define CONFIG_NUM_ENDPOINTS (EP_Count - 1)
/**************************************************************************
*
* Descriptor Data
*
**************************************************************************/
// Descriptors are the data that your computer reads when it auto-detects
// this USB device (called "enumeration" in USB lingo). The most commonly
// changed items are editable at the top of this file. Changing things
// in here should only be done by those who've read chapter 9 of the USB
// spec and relevant portions of any USB class specifications!
// A_036c
static uint8_t PROGMEM device_descriptor[] = {
18, // bLength
1, // bDescriptorType
0x00, 0x02, // bcdUSB
0, // bDeviceClass
0, // bDeviceSubClass
0, // bDeviceProtocol
ENDPOINT0_SIZE, // bMaxPacketSize0
LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
0x00, 0x01, // bcdDevice
1, // iManufacturer
2, // iProduct
0, // iSerialNumber
1 // bNumConfigurations
};
#if INC_IF_BOOT
// A_03f2:
static uint8_t PROGMEM boot_hid_report_desc[] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x95, 0x08, // REPORT_COUNT (8)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0xc0, // END_COLLECTION
};
#endif
#if INC_IF_KBD
// Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
// A_0419
static uint8_t PROGMEM keyboard_hid_report_desc[] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x01, // REPORT_ID (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x31, // REPORT_COUNT (49)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x01, // USAGE_MINIMUM (Keyboard ErrorRollOver)
0x29, 0x31, // USAGE_MAXIMUM (Keyboard \ and |)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x69, // REPORT_COUNT (105)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x33, // USAGE_MINIMUM (Keyboard ; and :)
0x29, 0x9b, // USAGE_MAXIMUM (Keyboard Cancel)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x9d, // USAGE_MINIMUM (Keyboard Prior)
0x29, 0xa4, // USAGE_MAXIMUM (Keyboard ExSel)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x04, // REPORT_COUNT (4)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0xc0, // END_COLLECTION
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x80, // USAGE (System Control)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x02, // REPORT_ID (2)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x03, // REPORT_COUNT (3)
0x19, 0x81, // USAGE_MINIMUM (System Power Down)
0x29, 0x83, // USAGE_MAXIMUM (System Wake Up)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0xc0, // END_COLLECTION
0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
0x09, 0x01, // USAGE (Consumer Control)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x03, // REPORT_ID (3)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x18, // REPORT_COUNT (24)
0x09, 0xb5, // USAGE (Scan Next Track)
0x09, 0xb6, // USAGE (Scan Previous Track)
0x09, 0xb7, // USAGE (Stop)
0x09, 0xcd, // USAGE (Unassigned)
0x09, 0xe2, // USAGE (Mute)
0x09, 0xe5, // USAGE (Bass Boost)
0x09, 0xe7, // USAGE (Loudness)
0x09, 0xe9, // USAGE (Volume Up)
0x09, 0xea, // USAGE (Volume Down)
0x0a, 0x52, 0x01, // USAGE (Bass Increment)
0x0a, 0x53, 0x01, // USAGE (Bass Decrement)
0x0a, 0x54, 0x01, // USAGE (Treble Increment)
0x0a, 0x55, 0x01, // USAGE (Treble Decrement)
0x0a, 0x83, 0x01, // USAGE (AL Consumer Control Configuration)
0x0a, 0x8a, 0x01, // USAGE (AL Email Reader)
0x0a, 0x92, 0x01, // USAGE (AL Calculator)
0x0a, 0x94, 0x01, // USAGE (AL Local Machine Browser)
0x0a, 0x21, 0x02, // USAGE (AC Search)
0x0a, 0x23, 0x02, // USAGE (AC Home)
0x0a, 0x24, 0x02, // USAGE (AC Back)
0x0a, 0x25, 0x02, // USAGE (AC Forward)
0x0a, 0x26, 0x02, // USAGE (AC Stop)
0x0a, 0x27, 0x02, // USAGE (AC Refresh)
0x0a, 0x2a, 0x02, // USAGE (AC Bookmarks)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0xc0, // END_COLLECTION
};
#endif
#if INC_IF_DBG
// A_0404:
static uint8_t PROGMEM debug_hid_report_desc[] = {
0x06, LSB(DEBUGHID_USAGE_PAGE), MSB(DEBUGHID_USAGE_PAGE), // Usage Page 0xFF31 (vendor defined)
0x09, DEBUGHID_USAGE, // Usage 0x74
0xA1, 0x53, // Collection 0x53
0x75, 0x08, // report size = 8 bits
0x15, 0x00, // logical minimum = 0
0x26, 0xFF, 0x00, // logical maximum = 255
0x95, DEBUG_TX_SIZE, // report count
0x09, 0x75, // usage
0x81, 0x02, // Input (array)
0xC0 // end collection
};
#endif
#if INC_IF_RAW
// A_04f5
static uint8_t PROGMEM rawhid_hid_report_desc[] = {
0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
0xA1, 0x01, // Collection 0x01
0x75, 0x08, // report size = 8 bits
0x15, 0x00, // logical minimum = 0
0x26, 0xFF, 0x00, // logical maximum = 255
0x95, RAWHID_TX_SIZE, // report count
0x09, 0x01, // usage
0x81, 0x02, // Input (array)
0x95, RAWHID_RX_SIZE, // report count
0x09, 0x02, // usage
0x91, 0x02, // Output (array)
0xC0 // end collection
};
#endif
// If you're desperate for a little extra code memory, these strings
// can be completely removed if iManufacturer, iProduct, iSerialNumber
// in the device desciptor are changed to zeros.
struct usb_string_descriptor_struct {
uint8_t bLength;
uint8_t bDescriptorType;
int16_t wString[];
};
// A_0511
static struct usb_string_descriptor_struct PROGMEM string0 = {
4,
3,
{0x0409}
};
// A_0515
static struct usb_string_descriptor_struct PROGMEM string1 = {
sizeof(STR_MANUFACTURER),
3,
STR_MANUFACTURER
};
// A_0525
static struct usb_string_descriptor_struct PROGMEM string2 = {
sizeof(STR_PRODUCT),
3,
STR_PRODUCT
};
#define CFG_DESC_SZ 9
#define IF_DESC_SZ 9
#define EP_DESC_SZ 7
#if INC_IF_BOOT
#define BOOT_HID_DESC_SZ (2*IF_DESC_SZ)+EP_DESC_SZ
#else
#define BOOT_HID_DESC_SZ 0
#endif
#if INC_IF_KBD
#define KBD_HID_DESC_SZ (2*IF_DESC_SZ)+EP_DESC_SZ
#else
#define KBD_HID_DESC_SZ 0
#endif
#if INC_IF_DBG
#define DBG_HID_DESC_SZ (2*IF_DESC_SZ)+EP_DESC_SZ
#else
#define DBG_HID_DESC_SZ 0
#endif
#if INC_IF_RAW
#define RAW_HID_DESC_SZ (2*IF_DESC_SZ)+(2*EP_DESC_SZ)
#else
#define RAW_HID_DESC_SZ 0
#endif
#define CONFIG1_DESC_SIZE (CFG_DESC_SZ + BOOT_HID_DESC_SZ + KBD_HID_DESC_SZ + DBG_HID_DESC_SZ + RAW_HID_DESC_SZ)
#define BOOT_HID_DESC_OFFSET (CFG_DESC_SZ + IF_DESC_SZ)
#define KEYBOARD_HID_DESC_OFFSET (CFG_DESC_SZ + BOOT_HID_DESC_SZ + IF_DESC_SZ)
#define DEBUG_HID_DESC_OFFSET (CFG_DESC_SZ + BOOT_HID_DESC_SZ + KBD_HID_DESC_SZ + IF_DESC_SZ)
#define RAWHID_HID_DESC_OFFSET (CFG_DESC_SZ + BOOT_HID_DESC_SZ + KBD_HID_DESC_SZ + DBG_HID_DESC_SZ + IF_DESC_SZ)
// A_037e
static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
CFG_DESC_SZ, // bLength;
2, // bDescriptorType: USBDESCR_CONFIG
LSB(CONFIG1_DESC_SIZE), // wTotalLength
MSB(CONFIG1_DESC_SIZE),
IF_Count, // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0xa0, // bmAttributes: USBATTR_BUSPOWER | USBATTR_SELFPOWER
50, // bMaxPower: USB_CFG_MAX_BUS_POWER/2
#if INC_IF_BOOT
// #0: boot device interface descriptor:
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
IF_DESC_SZ, // bLength
4, // bDescriptorType: USBDESCR_INTERFACE
IF_Boot, // bInterfaceNumber
0, // bAlternateSetting
1, // bNumEndpoints
0x03, // bInterfaceClass (0x03 = HID)
0x01, // bInterfaceSubClass (0x01 = Boot)
0x01, // bInterfaceProtocol (0x01 = Keyboard)
0, // iInterface
// HID interface descriptor, HID 1.11 spec, section 6.2.1
IF_DESC_SZ, // bLength
0x21, // bDescriptorType
0x11, 0x01, // bcdHID
0, // bCountryCode
1, // bNumDescriptors
0x22, // bDescriptorType
sizeof(boot_hid_report_desc), // wDescriptorLength
0,
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
EP_DESC_SZ, // bLength
5, // bDescriptorType
EP_Boot | 0x80, // bEndpointAddress
0x03, // bmAttributes (0x03=intr)
BOOT_SIZE, 0, // wMaxPacketSize
1, // bInterval
#endif
#if INC_IF_KBD
// #1: Keyboard Device interface
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
IF_DESC_SZ, // bLength
4, // bDescriptorType: USBDESCR_INTERFACE
IF_Kbd, // bInterfaceNumber
0, // bAlternateSetting
1, // bNumEndpoints
0x03, // bInterfaceClass (0x03 = HID)
0x0, // bInterfaceSubClass (0x01 = Boot)
0x0, // bInterfaceProtocol (0x01 = Keyboard)
0, // iInterface
// HID interface descriptor, HID 1.11 spec, section 6.2.1
IF_DESC_SZ, // bLength
0x21, // bDescriptorType
0x11, 0x01, // bcdHID
0, // bCountryCode
1, // bNumDescriptors
0x22, // bDescriptorType
sizeof(keyboard_hid_report_desc), // wDescriptorLength
0,
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
EP_DESC_SZ, // bLength
5, // bDescriptorType
EP_Kbd | 0x80, // bEndpointAddress
0x03, // bmAttributes (0x03=intr)
KEYBOARD_SIZE, 0, // wMaxPacketSize
1, // bInterval
#endif
#if INC_IF_DBG
// #2: Debug Device
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
IF_DESC_SZ, // bLength
4, // bDescriptorType
IF_Debug, // bInterfaceNumber
0, // bAlternateSetting
1, // bNumEndpoints
0x03, // bInterfaceClass (0x03 = HID)
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0, // iInterface
// HID interface descriptor, HID 1.11 spec, section 6.2.1
IF_DESC_SZ, // bLength
0x21, // bDescriptorType
// A_03ab
0x11, 0x01, // bcdHID
0, // bCountryCode
1, // bNumDescriptors
0x22, // bDescriptorType
sizeof(debug_hid_report_desc), // wDescriptorLength
0,
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
EP_DESC_SZ, // bLength
5, // bDescriptorType
EP_DebugTx | 0x80, // bEndpointAddress
0x03, // bmAttributes (0x03=intr)
DEBUG_TX_SIZE, 0, // wMaxPacketSize
1, // bInterval
#endif
#if INC_IF_RAW
// #3: Rawhid Device interface
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
IF_DESC_SZ, // bLength
4, // bDescriptorType
IF_Raw, // bInterfaceNumber
0, // bAlternateSetting
2, // bNumEndpoints
0x03, // bInterfaceClass (0x03 = HID)
0x00, // bInterfaceSubClass (0x01 = Boot)
0x00, // bInterfaceProtocol (0x01 = Keyboard)
0, // iInterface
// HID interface descriptor, HID 1.11 spec, section 6.2.1
IF_DESC_SZ, // bLength
0x21, // bDescriptorType
0x11, 0x01, // bcdHID
0, // bCountryCode
1, // bNumDescriptors
0x22, // bDescriptorType
sizeof(rawhid_hid_report_desc), // wDescriptorLength
0,
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
EP_DESC_SZ, // bLength
5, // bDescriptorType: end point
EP_RawTx | 0x80, // bEndpointAddress
0x03, // bmAttributes (0x03=intr)
RAWHID_TX_SIZE, 0, // wMaxPacketSize
RAWHID_TX_INTERVAL, // bInterval
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
EP_DESC_SZ, // bLength
5, // bDescriptorType
EP_RawRx, // bEndpointAddress
0x03, // bmAttributes (0x03=intr)
RAWHID_RX_SIZE, 0, // wMaxPacketSize
RAWHID_RX_INTERVAL, // bInterval
#endif
};
// This table defines which descriptor data is sent for each specific
// request from the host (in wValue and wIndex).
static struct descriptor_list_struct {
uint16_t wValue;
uint16_t wIndex;
const uint8_t *addr;
uint8_t length;
// A_0302
} PROGMEM descriptor_list[] = {
{0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
{0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
#if INC_IF_BOOT
{0x2200, IF_Boot, boot_hid_report_desc, sizeof(boot_hid_report_desc)},
{0x2100, IF_Boot, config1_descriptor+BOOT_HID_DESC_OFFSET, 9},
#endif
#if INC_IF_KBD
{0x2200, IF_Kbd, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
{0x2100, IF_Kbd, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
#endif
#if INC_IF_DBG
{0x2200, IF_Debug, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
{0x2100, IF_Debug, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
#endif
#if INC_IF_RAW
{0x2200, IF_Raw, rawhid_hid_report_desc, sizeof(rawhid_hid_report_desc)},
{0x2100, IF_Raw, config1_descriptor+RAWHID_HID_DESC_OFFSET, 9},
#endif
{0x0300, 0x0000, (const uint8_t *)&string0, 4},
{0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
{0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
};
#define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
typedef struct // standard USB Setup Packet Layout
{
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} USBSetupPacket;
/**************************************************************************
*
* Variables - these are the only non-stack RAM usage
*
**************************************************************************/
// zero when we are not configured, non-zero when enumerated
static volatile uint8_t usb_configuration = 0; // 0x0142
// the time remaining before we transmit any partially full
// packet, or send a zero length packet.
static volatile uint8_t debug_flush_timer = 0; // 0x014f
// which modifier keys are currently pressed
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
uint8_t keyboard_modifier_keys=0; // 0x0140
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
uint8_t volatile keyboard_leds = 0; // 0x0141
// which keys are currently pressed, up to 6 keys may be down at once
uint8_t boot_keys[6]={0}; // 0x0149
struct // still slightly shady area
{
uint8_t data1[21]; // 0x0152 HIDX 1..KEY_EXSEL
uint8_t data2; // 0x0167 HIDX KEY_SYSTEM_POWER..KEY_SYSTEM_WAKE
uint8_t data3[3]; // 0x0168 HIDX KEY_MEDIA_NEXT_TRACK..KEY_MEDIA_WWW_FAVORITES
uint8_t data4[2]; // 0x016b unused
} kbd_data = { {0} };
// protocol setting from the host. We use exactly the same report
// either way, so this variable only stores the setting since we
// are required to be able to report which setting is in use.
uint8_t keyboard_protocol = 1; // 0x0119
// the idle configuration, how often we send the report to the
// host (ms * 4) even when it hasn't changed
uint8_t keyboard_idle_config[3] = { 125, 125, 125 }; // 0x011a-0x011c
uint8_t boot_idle_config = 125; // 0x011d
// count until idle timeout
uint8_t keyboard_idle_count[3] = { 0 }; // 0x0145
uint8_t boot_idle_count = 0; // 0x0148
// flag whether device is suspended
uint8_t volatile usb_suspended = 0; // 0x013d
// flag whether device resumed after suspension
uint8_t volatile usb_resuming = 0; // 0x013e
uint8_t volatile usb_status = 0; // 0x013f
uint8_t volatile tx_timeout_count = 0; // 0x0143
uint8_t volatile rx_timeout_count = 0; // 0x0144
/**************************************************************************
*
* Public Functions - these are the API intended for the user
*
**************************************************************************/
/*****************************************************************************/
/* reset_usb_keyboard_data */
/*****************************************************************************/
// A_16c0
// double checked
uint8_t reset_usb_keyboard_data(void)
{
memset(boot_keys, 0, sizeof(boot_keys));
memset(&kbd_data, 0, sizeof(kbd_data));
keyboard_modifier_keys = 0;
return 0x0f;
}
/*****************************************************************************/
/* usb_init : initialize USB stuff */
/*****************************************************************************/
// A_16ee
// double checked
void usb_init(void)
{
reset_usb_keyboard_data();
HW_CONFIG();
USB_FREEZE(); // enable USB
PLL_CONFIG(); // config PLL
while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
USB_CONFIG(); // start USB clock
UDCON = 0; // enable attach resistor
usb_configuration = 0;
usb_suspended = 0;
usb_resuming = 0;
usb_status = 0;
UDIEN = (1<<EORSME)|(1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
sei();
}
/*****************************************************************************/
/* usb_remote_wakeup */
/*****************************************************************************/
// A_172a
// double checked
void usb_remote_wakeup(void)
{
if (usb_status && !usb_resuming)
{
UDCON |= (1 << RMWKUP); /* set "Upstream Resume" flag */
usb_resuming = 1;
}
}
/*****************************************************************************/
/* usb_keyboard_press : set bit for a key ID */
/*****************************************************************************/
// A_174c
uint8_t usb_keyboard_press(uint8_t keyid)
{
uint8_t rc;
if (keyid <= KEY_EXSEL)
{
// A_175a
kbd_data.data1[(keyid - 1) >> 3] |= 1 << ((keyid - 1) & 7);
rc = 0x03;
}
else
rc = 0x00;
// A_1782
if (keyid >= KEY_SYSTEM_POWER && keyid <= KEY_SYSTEM_WAKE)
{
kbd_data.data2 |= 1 << ((keyid - KEY_SYSTEM_POWER) & 7);
rc |= 0x04;
}
// A_17b2
else if (keyid >= KEY_MEDIA_NEXT_TRACK && keyid <= KEY_MEDIA_WWW_FAVORITES)
{
uint8_t id3 = keyid - KEY_MEDIA_NEXT_TRACK; // shift it to 0x00..0x17
kbd_data.data3[id3 >> 3] |= 1 << (id3 & 7);
rc |= 0x08;
}
// A_17e4
else if (keyid >= KEY_LCTRL && keyid <= KEY_RGUI)
{
keyboard_modifier_keys |= 1 << (keyid - KEY_LCTRL);
// A_1836
rc |= 0x03;
}
// A_180c
else if (keyid <= KEY_EXSEL)
{
int i;
for (i = 0; i < sizeof(boot_keys); i++)
{
if (boot_keys[i] == keyid)
return rc;
}
// A_1824
for (i = 0; i < sizeof(boot_keys); i++)
{
// A_1828
if (!boot_keys[i])
{
boot_keys[i] = keyid;
return rc | 0x03;
}
}
}
return rc;
}
/*****************************************************************************/
/* usb_keyboard_release : resets bits for a key id */
/*****************************************************************************/
// A_1848
uint8_t usb_keyboard_release(uint8_t keyid)
{
uint8_t rc;
if (keyid <= KEY_EXSEL)
{
// A_1856
kbd_data.data1[(keyid - 1) >> 3] &= ~(1 << ((keyid - 1) & 0x07));
rc = 3;
}
else
rc = 0;
// A_187e
if (keyid >= KEY_SYSTEM_POWER && keyid <= KEY_SYSTEM_WAKE)
{
kbd_data.data2 &= ~(1 << ((keyid - KEY_SYSTEM_POWER) & 7));
rc |= 4;
}
// A_18b0
else if (keyid >= KEY_MEDIA_NEXT_TRACK && keyid <= KEY_MEDIA_WWW_FAVORITES)
{
uint8_t id3 = keyid - KEY_MEDIA_NEXT_TRACK; // shift it to 0x00..0x17
kbd_data.data3[id3 >> 3] &= ~(1 << (id3 & 7));
rc |= 0x08;
}
// A_18e4
else if (keyid >= KEY_LCTRL && keyid <= KEY_RGUI)
{
keyboard_modifier_keys &= ~(1 << (keyid - KEY_LCTRL));
rc |= 0x03;
}
else if (keyid <= KEY_EXSEL)
{
// A_1900
int i;
for (i = 0; i < sizeof(boot_keys); i++)
if (boot_keys[i] == keyid)
{
boot_keys[i] = 0;
rc |= 0x03;
}
}
return rc;
}
/*****************************************************************************/
/* usb_keyboard_send : sends new keyboard state */
/*****************************************************************************/
// A_192e
// double checked
uint8_t usb_keyboard_send(uint8_t flagset)
{
uint8_t intr_state; // R18
uint8_t i;
// R25 = flagset;
if (usb_configuration == 0)
return flagset;
#if INC_IF_BOOT
if (flagset & 0x01)
{
// A_193e
intr_state = SREG;
cli();
UENUM = EP_Boot;
// are we ready to transmit?
if (UEINTX & (1 << RWAL))
{
// A_1950
UEDATX = keyboard_modifier_keys;
UEDATX = 0;
for (i = 0; i < sizeof(boot_keys); i++)
UEDATX = boot_keys[i];
UEINTX = 0x3a;
// A_1992
boot_idle_count = 0;
flagset &= ~0x01;
}
// A_1998
SREG = intr_state;
}
#else
boot_idle_count = 0;
flagset &= ~0x01;
#endif
// A_199a
if (!keyboard_protocol)
return flagset & 0xf1;
#if INC_IF_KBD
// A_19a4
if (flagset & 0x02)
{
// A_19a8
intr_state = SREG;
cli();
UENUM = EP_Kbd;
// are we ready to transmit?
if (UEINTX & (1 << RWAL))
{
// A_19ba
UEDATX = 1;
UEDATX = keyboard_modifier_keys;
for (i = 0; i < sizeof(kbd_data.data1); i++)
UEDATX = kbd_data.data1[i];
UEINTX = 0x3a;
keyboard_idle_count[0] = 0;
flagset &= ~0x02;
}
// A_19e6
SREG = intr_state;
}
#else
keyboard_idle_count[0] = 0;
flagset &= ~0x02;
#endif
#if INC_IF_KBD
// A_19e8
if (flagset & 0x04)
{
// A_19ec
intr_state = SREG;
cli();
UENUM = EP_Kbd;
// are we ready to transmit?
if (UEINTX & (1 << RWAL))
{
UEDATX = 2;
UEDATX = kbd_data.data2;
UEINTX = 0x3a;
keyboard_idle_count[1] = 0;
flagset &= ~0x04;
}
// A_1a16
SREG = intr_state;
}
#else
keyboard_idle_count[1] = 0;
flagset &= ~0x04;
#endif
#if INC_IF_KBD
// A_1a18
if (flagset & 0x08)
{
// A_1a1c
intr_state = SREG;
cli();
UENUM = EP_Kbd;
// are we ready to transmit?
if (UEINTX & (1 << RWAL))
{
UEDATX = 3;
for (i = 0; i < sizeof(kbd_data.data3); i++)
UEDATX = kbd_data.data3[i];
UEINTX = 0x3a;
keyboard_idle_count[2] = 0;
flagset &= ~0x08;
}
// A_1a58
SREG = intr_state;
}