-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathavrusb.c
955 lines (837 loc) · 28.9 KB
/
avrusb.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
/*
* avrusb.c: USB implementation for ATMEGA32U4.
*
* Copyright (c) 2021-2022 Kimmo Kulovesi, https://arkku.dev/
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#define USB_KEYBOARD_ACCESS_STATE 1
#include "usb_hardware.h"
#include "usbkbd.h"
#include "usbkbd_descriptors.h"
#include "aakbd.h"
#include "avrusb.h"
#include "usb.h"
#include "usb_keys.h"
#include "generic_hid.h"
#include "progmem.h"
// MARK: - Configuration
/// Frame divider for the idle counter. Must be a power of 2.
#define IDLE_COUNT_FRAME_DIVIDER 4
// Not an actual USB status, but we'll recycle the variable.
#define USB_STATUS_JUMP_TO_BOOTLOADER (1 << 7)
#define ENDPOINT_0_FLAGS EP_SINGLE_BUFFER
#ifdef USB_SERIES_2_AVR
#define KEYBOARD_ENDPOINT_FLAGS EP_SINGLE_BUFFER
#else
#define KEYBOARD_ENDPOINT_FLAGS EP_DOUBLE_BUFFER
#endif
#define KEYBOARD_ENDPOINT_TYPE EP_TYPE_INTERRUPT_IN
#define GENERIC_ENDPOINT_FLAGS EP_SINGLE_BUFFER
#define GENERIC_ENDPOINT_IN_TYPE EP_TYPE_INTERRUPT_IN
#define GENERIC_ENDPOINT_OUT_TYPE EP_TYPE_INTERRUPT_OUT
// MARK: - USB Variables
/// How long has the keyboard has been idle, frames / `IDLE_COUNT_FRAME_DIVIDER`.
static volatile uint8_t keyboard_idle_count = 0;
/// The value of `keyboard_idle_count` to send an update on.
static volatile uint8_t keyboard_update_on_idle_count = KEYBOARD_UPDATE_IDLE_MS / IDLE_COUNT_FRAME_DIVIDER;
/// The active USB configuration. This is set by a request from the host.
static volatile uint8_t usb_configuration = 0;
/// USB status flags.
static volatile uint8_t usb_status = 0;
/// Is USB susended?
static volatile bool usb_suspended = 0;
/// Zero or a an ASCII character to identify an error.
static volatile uint8_t usb_error = 0;
#if ENABLE_GENERIC_HID_ENDPOINT
static volatile uint8_t generic_update_on_idle_count = GENERIC_HID_UPDATE_IDLE_MS / IDLE_COUNT_FRAME_DIVIDER;
static volatile uint8_t generic_idle_count = 0;
static volatile uint8_t generic_report_pending = 0;
#if defined(ENABLE_GENERIC_HID_MUTEX) && ENABLE_GENERIC_HID_MUTEX
static volatile int_fast8_t generic_report_busy = 0;
#define generic_report_wait_lock() do { while (generic_report_locked); generic_report_lock(); } while (0)
#define generic_report_lock() do { generic_report_busy = 1; } while (0)
#define generic_report_unlock() do { generic_report_busy = 0; } while (0)
#define generic_report_reset() do { generic_report_busy = 0; } while (0)
#define generic_report_locked (generic_report_busy)
#else
#define generic_report_wait_lock() do { } while (0)
#define generic_report_lock() do { } while (0)
#define generic_report_unlock() do { } while (0)
#define generic_report_reset() do { } while (0)
#define generic_report_locked (false)
#endif
#if GENERIC_HID_REPORT_SIZE != 0
static uint8_t generic_report[GENERIC_HID_REPORT_SIZE] = { 0 };
#else
static uint8_t generic_report[1] = { 0 };
#endif
#endif // ^ ENABLE_GENERIC_HID_ENDPOINT
#if ENABLE_DFU_INTERFACE
static volatile uint8_t usb_request_detach = 0;
#define dfu_app_state (usb_request_detach ? DFU_APP_STATE_DETACH : DFU_APP_STATE_IDLE)
#endif
// MARK: - USB
#define is_boot_protocol usb_keyboard_is_in_boot_protocol
static INLINE bool usb_wait_for_rw(uint8_t * const sregptr, const int_fast8_t endpoint);
static void
usb_devices_reset (void) {
usb_keyboard_reset();
keyboard_idle_count = 0;
keyboard_update_on_idle_count = DIV_ROUND_BYTE(IDLE_COUNT_FRAME_DIVIDER, KEYBOARD_UPDATE_IDLE_MS);
#if ENABLE_GENERIC_HID_ENDPOINT
generic_update_on_idle_count = DIV_ROUND_BYTE(IDLE_COUNT_FRAME_DIVIDER, GENERIC_HID_POLL_INTERVAL_MS);
generic_report_pending = 0;
generic_report_reset();
#endif
}
static void
usb_reset (void) {
usb_freeze();
pll_enable();
while (!is_pll_locked)
;
usb_start_clock();
usb_attach();
}
void
usb_init (void) {
usb_descriptors_init();
usb_hardware_init();
usb_reset();
usb_error = 0;
usb_configuration = 0;
usb_status = 0;
usb_suspended = false;
usb_devices_reset();
usb_clear_interrupts(INT_SUSPEND_FLAG | INT_WAKE_UP_FLAG);
#if IS_SUSPEND_SUPPORTED
usb_set_enabled_interrupts(INT_END_OF_RESET_FLAG | INT_SUSPEND_FLAG);
#else
usb_set_enabled_interrupts(INT_END_OF_RESET_FLAG);
#endif
#if ENABLE_DFU_INTERFACE
usb_request_detach = 0;
#endif
}
static INLINE void
usb_init_endpoint (const uint8_t num, const uint8_t type, const uint8_t size, const uint8_t flags) {
for (uint8_t i = num; i <= USB_MAX_ENDPOINT; ++i) {
uint8_t cfg_type;
uint8_t cfg_flags;
uint8_t cfg_interrupts;
usb_set_endpoint(i);
if (i == num) {
cfg_type = type;
cfg_flags = flags | EP_SIZE_FLAGS(size);
cfg_interrupts = 0;
} else {
cfg_type = usb_endpoint_type_config;
cfg_flags = usb_endpoint_flags_config;
cfg_interrupts = usb_endpoint_interrupts_config;
}
if (!(cfg_flags & EP_ALLOC)) {
continue;
}
usb_disable_endpoint();
usb_deallocate_endpoint();
usb_enable_endpoint();
usb_endpoint_type_config = cfg_type;
usb_endpoint_flags_config = cfg_flags;
usb_endpoint_interrupts_config = cfg_interrupts;
}
usb_set_endpoint(num);
}
static INLINE void
usb_init_endpoints (void) {
#if ENABLE_KEYBOARD_ENDPOINT
_Static_assert(IS_ENDPOINT_SIZE_VALID(KEYBOARD_ENDPOINT_SIZE), "Invalid keyboard endpoint size");
usb_init_endpoint(KEYBOARD_ENDPOINT_NUM, KEYBOARD_ENDPOINT_TYPE, KEYBOARD_ENDPOINT_SIZE, KEYBOARD_ENDPOINT_FLAGS);
#endif
#if ENABLE_GENERIC_HID_ENDPOINT
_Static_assert(IS_ENDPOINT_SIZE_VALID(GENERIC_ENDPOINT_SIZE), "Invalid Generic HID endpoint size");
usb_init_endpoint(GENERIC_HID_ENDPOINT_IN_NUM, GENERIC_ENDPOINT_IN_TYPE, GENERIC_ENDPOINT_SIZE, GENERIC_ENDPOINT_FLAGS);
#if ENABLE_GENERIC_HID_OUTPUT
usb_init_endpoint(GENERIC_HID_ENDPOINT_OUT_NUM, GENERIC_ENDPOINT_OUT_TYPE, GENERIC_ENDPOINT_SIZE, GENERIC_ENDPOINT_FLAGS);
#endif
#endif
//usb_reset_endpoints_1to(USB_MAX_ENDPOINT);
}
static INLINE void
usb_configuration_changed() {
usb_clear_setup();
usb_init_endpoints();
usb_enable_interrupts(INT_START_OF_FRAME_FLAG);
}
bool
usb_is_ok (void) {
return (usb_configuration != 0) && (usb_error == 0);
}
uint8_t
usb_is_configured (void) {
return usb_configuration;
}
uint8_t
usb_address (void) {
return usb_get_address();
}
uint8_t
usb_last_error (void) {
return usb_error;
}
bool
usb_is_suspended (void) {
return usb_suspended;
}
uint8_t
usb_detach_requested (void) {
#if ENABLE_DFU_INTERFACE
return usb_request_detach;
#else
return 0;
#endif
}
static INLINE void
usb_wake_up_if_suspended (void) {
#if IS_SUSPEND_SUPPORTED
if (usb_suspended) {
usb_set_remote_wakeup();
}
#endif
}
bool
usb_wake_up_host (void) {
usb_clear_remote_wakeup();
if (is_usb_remote_wakeup_set || usb_suspended || !(usb_status & USB_STATUS_REMOTE_WAKEUP_ENABLED)) {
usb_error = 'w';
return false;
}
usb_init();
usb_set_remote_wakeup();
return true;
}
static INLINE void
usb_tx_report_header (void) {
#if USE_MULTIPLE_REPORTS
if (!is_boot_protocol) {
usb_tx(KEYBOARD_REPORT_ID);
}
#endif
usb_tx(usb_keys_modifier_flags);
#if RESERVE_BOOT_PROTOCOL_RESERVED_BYTE
usb_tx(0);
#endif
#if APPLE_BYTES_IN_REPORT
if (!(RESERVE_BOOT_PROTOCOL_RESERVED_BYTE && is_boot_protocol)) {
usb_tx(usb_keys_extended_flags & APPLE_VIRTUAL_MASK);
}
#endif
}
static INLINE void
usb_tx_error_report (const uint8_t byte) {
usb_tx_report_header();
keyboard_idle_count = 0;
for (int_fast8_t i = usb_keyboard_rollover; i; --i) {
usb_tx(byte);
}
}
static INLINE void
usb_tx_keys_state (void) {
usb_tx_report_header();
int_fast8_t count = usb_keyboard_rollover;
keyboard_idle_count = 0;
usb_keyboard_updated = false;
#if KEY_IN_RESERVED_BYTE
// Send the last key here so the end result is same as boot protocol
// until we exceed 6 non-modifier keys down (which is probably never
// unless specifically testing rollover). According to HID 1.11 spec
// the order of keys in the array doesn't matter, so conforming hosts
// should not have a problem with a leading zero byte.
usb_tx(usb_keys_buffer[--count]);
#endif
// ...although the order of keys in the array doesn't matter, somehow it
// feels nicer to send them in chronological order.
for (int_fast8_t i = 0; i < count; ++i) {
usb_tx(usb_keys_buffer[i]);
}
}
static INLINE bool
usb_wait_for_rw (uint8_t * const sregptr, const int_fast8_t endpoint) {
const uint8_t timeout = usb_frame_count + 50U;
uint8_t old_sreg = SREG;
cli();
for (;;) {
if (is_usb_rw_allowed) {
break;
}
SREG = old_sreg;
if (!usb_configuration || (usb_frame_count == timeout)) {
*sregptr = old_sreg;
return false;
}
old_sreg = SREG;
cli();
usb_set_endpoint(endpoint);
}
*sregptr = old_sreg;
return true;
}
bool
usb_keyboard_send_report (void) {
#if ENABLE_KEYBOARD_ENDPOINT
if (!usb_configuration) {
usb_error = 'c';
return false;
}
uint8_t old_sreg;
if (!usb_wait_for_rw(&old_sreg, KEYBOARD_ENDPOINT_NUM)) {
usb_error = 'T';
return false;
}
usb_set_endpoint(KEYBOARD_ENDPOINT_NUM);
usb_wake_up_if_suspended();
usb_tx_keys_state();
usb_release_tx();
keyboard_idle_count = 0;
usb_error = 0;
SREG = old_sreg;
#endif
return true;
}
static INLINE bool
usb_keyboard_wait_to_send (uint8_t * const sregptr, const int_fast8_t endpoint) {
const uint8_t timeout = usb_frame_count + 50U;
uint8_t old_sreg = SREG;
cli();
for (;;) {
if (is_usb_rw_allowed) {
break;
}
SREG = old_sreg;
if (!usb_configuration || (usb_frame_count == timeout)) {
*sregptr = old_sreg;
return false;
}
old_sreg = SREG;
cli();
usb_set_endpoint(endpoint);
}
*sregptr = old_sreg;
return true;
}
// MARK: - Generic HID
#if ENABLE_GENERIC_HID_ENDPOINT
bool
send_generic_hid_report (uint8_t report_id, uint8_t count, const uint8_t report[static count]) {
if (!usb_configuration) {
return false;
}
uint8_t old_sreg;
if (!usb_keyboard_wait_to_send(&old_sreg, GENERIC_HID_ENDPOINT_IN_NUM)) {
return false;
}
generic_report_pending = false;
usb_set_endpoint(GENERIC_HID_ENDPOINT_IN_NUM);
usb_wake_up_if_suspended();
if (report_id) {
usb_tx(report_id);
}
for (int_fast8_t i = 0; i < count; ++i) {
const uint8_t byte = report[i];
usb_tx(byte);
}
usb_release_tx();
SREG = old_sreg;
generic_idle_count = 0;
return true;
}
bool
make_and_send_generic_hid_report (void) {
bool result = false;
generic_report_wait_lock();
if (generic_report_pending || make_generic_hid_report(0, GENERIC_HID_REPORT_SIZE, generic_report)) {
result = send_generic_hid_report(0, GENERIC_HID_REPORT_SIZE, generic_report);
}
generic_report_unlock();
return result;
}
static INLINE bool
generic_request_call_handler (const uint8_t report_id, const uint8_t length, uint8_t request[static length]) {
uint8_t response_length = GENERIC_HID_REPORT_SIZE;
const uint8_t response = handle_generic_hid_report(report_id, length, request, &response_length, generic_report);
switch (response) {
case RESPONSE_OK:
break;
case RESPONSE_SEND_REPLY:
generic_report_pending = response_length;
break;
case RESPONSE_JUMP_TO_BOOTLOADER:
usb_configuration = 0;
usb_status |= USB_STATUS_JUMP_TO_BOOTLOADER;
break;
case RESPONSE_ERROR:
return false;
default:
break;
}
return true;
}
#if ENABLE_GENERIC_HID_OUTPUT
static void
receive_generic_hid_report (void) {
uint8_t length = 0;
uint8_t request[GENERIC_HID_FEATURE_SIZE ? GENERIC_HID_FEATURE_SIZE : 1];
usb_set_endpoint(GENERIC_HID_ENDPOINT_OUT_NUM);
if (is_usb_rx_out_ready) {
while (is_usb_rw_allowed && length < sizeof(request)) {
request[length++] = usb_rx();
}
usb_ack_rx_out();
}
if (length) {
generic_report_wait_lock();
generic_request_call_handler(0, length, request);
if (generic_report_pending) {
send_generic_hid_report(0, generic_report_pending, generic_report);
}
generic_report_unlock();
}
}
#endif
#endif // ^ ENABLE_GENERIC_HID_ENDPOINT
// MARK: - Periodically called task
void
usb_tick (void) {
if (usb_configuration == 0) {
if (usb_status & USB_STATUS_JUMP_TO_BOOTLOADER) {
jump_to_bootloader();
}
if (keyboard_idle_count || !usb_keyboard_updated) {
usb_devices_reset();
}
} else {
#if ENABLE_GENERIC_HID_ENDPOINT
#if ENABLE_GENERIC_HID_OUTPUT
receive_generic_hid_report();
#endif
if (generic_report_pending && !generic_report_locked) {
generic_report_lock();
send_generic_hid_report(0, generic_report_pending, generic_report);
generic_report_unlock();
}
#endif // ^ ENABLE_GENERIC_HID_ENDPOINT
}
}
// MARK: - Interrupt handlers (this is most of the USB stuff happens)
ISR(USB_GEN_vect) {
static volatile uint8_t frame_count = 0;
const uint8_t intflags = usb_interrupt_flags_reg;
usb_clear_interrupts(INT_END_OF_RESET_FLAG | INT_START_OF_FRAME_FLAG);
if (intflags & INT_END_OF_RESET_FLAG) {
_Static_assert(IS_ENDPOINT_SIZE_VALID(ENDPOINT_0_SIZE), "Invalid endpoint 0 size");
usb_setup_endpoint(
0,
EP_TYPE_CONTROL,
ENDPOINT_0_SIZE,
ENDPOINT_0_FLAGS
);
usb_configuration = 0;
#if ENABLE_DFU_INTERFACE
if (usb_request_detach) {
usb_status |= USB_STATUS_JUMP_TO_BOOTLOADER;
}
#endif
usb_enable_endpoint_interrupts();
}
if ((intflags & INT_START_OF_FRAME_FLAG)) {
#if ENABLE_DFU_INTERFACE
if (usb_request_detach) {
--usb_request_detach;
}
#endif
if ((++frame_count % IDLE_COUNT_FRAME_DIVIDER) == 0 && !usb_suspended) {
#if ENABLE_KEYBOARD_ENDPOINT
if (keyboard_update_on_idle_count) {
usb_set_endpoint(KEYBOARD_ENDPOINT_NUM);
if (is_usb_rw_allowed) {
if (++keyboard_idle_count == keyboard_update_on_idle_count) {
// Been idle for a while, send the state
const uint8_t error = key_error;
if (error & KEY_ERROR_NEEDS_REPORTING_FLAG) {
key_error = error + 1; // clears the flag, which is 1
usb_tx_error_report(error);
} else {
usb_tx_keys_state();
}
}
usb_release_tx();
}
}
#endif
#if ENABLE_GENERIC_HID_ENDPOINT
if (generic_update_on_idle_count) {
usb_set_endpoint(GENERIC_HID_ENDPOINT_IN_NUM);
if (is_usb_rw_allowed) {
if (++generic_idle_count == generic_update_on_idle_count) {
if (!generic_report_locked) {
generic_report_lock();
if (generic_report_pending) {
// Send the pending report now
const uint8_t count = generic_report_pending;
generic_report_pending = 0;
for (int_fast8_t i = 0; i < count; ++i) {
usb_tx(generic_report[i]);
}
} else if (make_generic_hid_report(0, GENERIC_HID_REPORT_SIZE, generic_report)) {
for (int_fast8_t i = 0; i < GENERIC_HID_REPORT_SIZE; ++i) {
usb_tx(generic_report[i]);
}
}
generic_report_unlock();
}
generic_idle_count = 0;
}
usb_release_tx();
}
}
#endif // ^ ENABLE_GENERIC_HID_ENDPOINT
}
}
if (intflags & INT_WAKE_UP_FLAG) {
usb_disable_interrupts(INT_WAKE_UP_FLAG);
usb_enable_interrupts(INT_SUSPEND_FLAG);
usb_suspended = false;
usb_wake_up_interrupt();
usb_clear_interrupts(INT_WAKE_UP_FLAG);
} else if (intflags & INT_SUSPEND_FLAG) {
usb_disable_interrupts(INT_SUSPEND_FLAG);
usb_enable_interrupts(INT_WAKE_UP_FLAG);
usb_suspended = true;
usb_suspend_interrupt();
usb_clear_interrupts(INT_SUSPEND_FLAG | INT_WAKE_UP_FLAG);
}
}
ISR(USB_COM_vect) {
usb_set_endpoint(0);
if (!is_usb_rx_int_setup) {
usb_stall();
return;
}
bool success = true;
uint8_t i;
uint8_t type = usb_rx();
uint8_t request = usb_rx();
uint_fast16_t value = usb_rx();
value |= usb_rx() << 8;
uint_fast16_t index = usb_rx();
index |= usb_rx() << 8;
uint_fast16_t length = usb_rx();
length |= usb_rx() << 8;
usb_clear_setup_int();
if (type & USB_REQUEST_DIRECTION_TO_HOST) {
usb_wait_tx_in();
} else {
usb_flush_tx_in();
}
if ((type & USB_REQUEST_TYPE_MASK) == USB_REQUEST_TYPE_STANDARD) {
if (request == USB_REQUEST_GET_DESCRIPTOR) {
const char *data;
uint8_t desc_length = usb_descriptor_length_and_data(value, index, &data);
if (!desc_length) {
// Descriptor not found
usb_stall();
usb_error = 'D';
return;
}
#if USB_STRINGS_STORED_AS_ASCII
// 1 = string, 0 = data
type = (index != 0 && MSB(value) == DESCRIPTOR_TYPE_STRING);
#endif
if (desc_length < length) {
length = desc_length;
}
request = (desc_length % ENDPOINT_0_SIZE);
while (length) {
// Split into packets of at most endpoint size
i = (length < ENDPOINT_0_SIZE) ? length : ENDPOINT_0_SIZE;
length -= i;
#if USB_STRINGS_STORED_AS_ASCII
if (type) {
// String - convert ASCII to UTF-16 on the fly
i /= 2; // The length of string descriptors is always even
if (desc_length) {
// The header, only send in the first packet
if (i) {
--i;
usb_tx(desc_length);
desc_length = 0;
usb_tx(DESCRIPTOR_TYPE_STRING);
}
}
while (i--) {
usb_tx(pgm_read_byte(data++));
usb_tx(0); // Upper byte of the UTF-16 string
}
} else
#endif
// Normal data, already in the correct format
while (i--) {
usb_tx(pgm_read_byte(data++));
}
usb_flush_tx_in();
usb_wait_in_or_out();
if (is_usb_rx_out_ready) {
// Abort
return;
}
}
if (request) {
return; // Return since we already flushed the last packet
}
// The data structure was an exact multiple of the endpoint
// size, we need to tell that it is over by sending an empty
// packet. (The flush as the end of this function.)
} else if (request == USB_REQUEST_SET_ADDRESS) {
// Set address
usb_wait_tx_in();
usb_set_address(value);
} else if (request == USB_REQUEST_GET_STATUS) {
// Get status
if (type == USB_REQUEST_DEVICE_TO_HOST_STANDARD_DEVICE) {
i = usb_status;
} else if (type == USB_REQUEST_DEVICE_TO_HOST_STANDARD_ENDPOINT) {
i = index & 0xFF;
usb_set_endpoint(i);
i = is_usb_stall_requested ? 1 : 0;
usb_set_endpoint(0);
} else {
i = 0;
}
usb_tx(i);
usb_tx(0);
} else if (request == USB_REQUEST_GET_CONFIGURATION) {
// Get configuration
if (type == USB_REQUEST_DEVICE_TO_HOST_STANDARD_DEVICE) {
usb_tx(usb_configuration);
} else {
usb_tx(1);
}
} else if (request == USB_REQUEST_SET_CONFIGURATION) {
// Set configuration
if ((type & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_DEVICE) {
if (value <= CONFIGURATIONS_COUNT) {
usb_configuration = value;
usb_configuration_changed();
} else {
success = false;
}
} else {
success = false;
}
} else if (request == USB_REQUEST_CLEAR_FEATURE || request == USB_REQUEST_SET_FEATURE) {
// Set or clear a feature
if (type == USB_REQUEST_HOST_TO_DEVICE_STANDARD_DEVICE) {
if (value == USB_FEATURE_DEVICE_REMOTE_WAKEUP) {
// Remote wakeup
if (request == USB_REQUEST_CLEAR_FEATURE) {
usb_status &= ~USB_STATUS_REMOTE_WAKEUP_ENABLED;
} else {
usb_status |= USB_STATUS_REMOTE_WAKEUP_ENABLED;
}
}
} else if (type == USB_REQUEST_HOST_TO_DEVICE_STANDARD_ENDPOINT) {
if (value == USB_FEATURE_HALT_ENDPOINT) {
// Halt
i = index & 0x7FU;
if (i != 0 && i <= USB_MAX_ENDPOINT) {
usb_set_endpoint(i);
if (request == USB_REQUEST_CLEAR_FEATURE) {
usb_clear_stall();
usb_reset_endpoint(i);
usb_reset_data_toggle();
} else {
usb_stall();
}
usb_set_endpoint(0);
}
}
}
} else if (request == USB_REQUEST_SET_DESCRIPTOR) {
success = false;
} else if (request == USB_REQUEST_GET_INTERFACE) {
if (usb_configuration && length == 1) {
// index = interface number
usb_tx(0); // alternate setting
} else {
success = false;
}
} else if (request == USB_REQUEST_SET_INTERFACE) {
if (usb_configuration && index < INTERFACES_COUNT && value == 0) {
// index = interface number
// value = alternate setting
} else {
success = false;
}
}
} else if (index == KEYBOARD_INTERFACE_INDEX) {
if (type == USB_REQUEST_DEVICE_TO_HOST_CLASS_INTERFACE) {
if (request == HID_REQUEST_GET_REPORT) {
usb_wait_tx_in();
usb_tx_keys_state();
} else if (request == HID_REQUEST_GET_IDLE) {
#if IDLE_COUNT_FRAME_DIVIDER == 4
usb_tx(keyboard_update_on_idle_count);
#else
value = keyboard_update_on_idle_count;
i = (value * IDLE_COUNT_FRAME_DIVIDER) / 4;
usb_tx(i);
#endif
} else if (request == HID_REQUEST_GET_PROTOCOL) {
usb_tx(usb_keyboard_protocol);
}
} else if (type == USB_REQUEST_HOST_TO_DEVICE_CLASS_INTERFACE) {
if (request == HID_REQUEST_SET_REPORT) {
usb_wait_rx_out();
usb_keyboard_leds = usb_rx();
usb_ack_rx_out();
} else if (request == HID_REQUEST_SET_IDLE) {
keyboard_idle_count = 0;
keyboard_update_on_idle_count = (value >> 6) / IDLE_COUNT_FRAME_DIVIDER;
} else if (request == HID_REQUEST_SET_PROTOCOL) {
usb_keyboard_protocol = value;
}
} else {
success = false;
}
}
#if ENABLE_GENERIC_HID_ENDPOINT
else if (index == GENERIC_INTERFACE_INDEX) {
if (type == USB_REQUEST_DEVICE_TO_HOST_CLASS_INTERFACE) {
if (request == HID_REQUEST_GET_REPORT) {
if (length > GENERIC_HID_REPORT_SIZE) {
length = GENERIC_HID_REPORT_SIZE;
}
if (!generic_report_locked) {
generic_report_lock();
if (generic_report_pending || make_generic_hid_report(value & 0xFFU, length, generic_report)) {
for (i = 0; i < length; ++i) {
usb_tx(generic_report[i]);
}
} else {
success = false;
}
generic_report_unlock();
} else {
success = false;
}
} else if (request == HID_REQUEST_GET_IDLE) {
#if IDLE_COUNT_FRAME_DIVIDER == 4
usb_tx(generic_update_on_idle_count);
#else
value = generic_update_on_idle_count;
i = (value * IDLE_COUNT_FRAME_DIVIDER) / 4;
usb_tx(i);
#endif
} else if (request == HID_REQUEST_GET_PROTOCOL) {
usb_tx(INTERFACE_NO_SPECIFIC_PROTOCOL);
}
} else if (type == USB_REQUEST_HOST_TO_DEVICE_CLASS_INTERFACE) {
if (request == HID_REQUEST_SET_REPORT) {
#if ENABLE_GENERIC_HID_OUTPUT
// The request should come through the output endpoint
success = false;
#else
static uint8_t request[GENERIC_HID_FEATURE_SIZE ? GENERIC_HID_FEATURE_SIZE : 1];
if (length > GENERIC_HID_FEATURE_SIZE) {
length = GENERIC_HID_FEATURE_SIZE;
}
usb_wait_rx_out();
for (i = 0; i < length; ++i) {
request[i] = usb_rx();
}
usb_ack_rx_out();
usb_flush_tx_in();
generic_report_wait_lock();
success = generic_request_call_handler(value & 0xFFU, length, request);
generic_report_unlock();
return;
#endif
} else if (request == HID_REQUEST_SET_IDLE) {
generic_idle_count = 0;
#if GENERIC_HID_UPDATE_IDLE_MS
generic_update_on_idle_count = (value >> 6) / IDLE_COUNT_FRAME_DIVIDER;
#endif
}
} else {
success = false;
}
}
#endif // ^ ENABLE_GENERIC_HID_ENDPOINT
#if ENABLE_DFU_INTERFACE
else if (index == DFU_INTERFACE_INDEX) {
if (type == USB_REQUEST_HOST_TO_DEVICE_CLASS_INTERFACE) {
if (request == DFU_REQUEST_DETACH) {
if ((value >> 8)) {
usb_request_detach = 0xFFU;
} else {
usb_request_detach = value ? value : 1;
}
} else {
success = false;
}
} else if (type == USB_REQUEST_DEVICE_TO_HOST_CLASS_INTERFACE) {
if (request == DFU_REQUEST_GET_STATE) {
usb_tx(dfu_app_state);
} else if (request == DFU_REQUEST_GET_STATUS) {
usb_tx(DFU_STATUS_OK); // bStatus
usb_tx(0); usb_tx(0); usb_tx(0); // bwPollTimeout
usb_tx(dfu_app_state); // bState
usb_tx(STRING_INDEX_PRODUCT); // iString
} else {
success = false;
}
} else {
success = false;
}
}
#endif // ^ ENABLE_DFU_INTERFACE
else {
success = false;
}
if (success) {
usb_flush_tx_in();
usb_error = 0;
} else {
usb_stall();
usb_error = 'R';
}
}
void
usb_deinit (void) {
usb_keyboard_release_all_keys();
usb_keyboard_leds = 5;
usb_keyboard_send_report();
_delay_ms(8);
cli();
usb_detach();
usb_freeze();
_delay_ms(8);
usb_disable();
sei();
}