1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
17 #if !defined(__HIDBOOT_H__)
22 #define UHS_HID_BOOT_KEY_ZERO 0x27
23 #define UHS_HID_BOOT_KEY_ENTER 0x28
24 #define UHS_HID_BOOT_KEY_SPACE 0x2c
25 #define UHS_HID_BOOT_KEY_CAPS_LOCK 0x39
26 #define UHS_HID_BOOT_KEY_SCROLL_LOCK 0x47
27 #define UHS_HID_BOOT_KEY_NUM_LOCK 0x53
28 #define UHS_HID_BOOT_KEY_ZERO2 0x62
29 #define UHS_HID_BOOT_KEY_PERIOD 0x63
31 // Don't worry, GCC will optimize the result to a final value.
32 #define bitsEndpoints(p) ((((p) & HID_PROTOCOL_KEYBOARD)? 2 : 0) | (((p) & HID_PROTOCOL_MOUSE)? 1 : 0))
33 #define totalEndpoints(p) ((bitsEndpoints(p) == 3) ? 3 : 2)
34 #define epMUL(p) ((((p) & HID_PROTOCOL_KEYBOARD)? 1 : 0) + (((p) & HID_PROTOCOL_MOUSE)? 1 : 0))
36 // Already defined in hid.h
37 // #define HID_MAX_HID_CLASS_DESCRIPTORS 5
42 uint8_t bmLeftButton
: 1;
43 uint8_t bmRightButton
: 1;
44 uint8_t bmMiddleButton
: 1;
51 class MouseReportParser
: public HIDReportParser
{
55 uint8_t bInfo
[sizeof (MOUSEINFO
)];
59 void Parse(HID
*hid
, bool is_rpt_id
, uint8_t len
, uint8_t *buf
);
63 virtual void OnMouseMove(MOUSEINFO
*mi
) {
66 virtual void OnLeftButtonUp(MOUSEINFO
*mi
) {
69 virtual void OnLeftButtonDown(MOUSEINFO
*mi
) {
72 virtual void OnRightButtonUp(MOUSEINFO
*mi
) {
75 virtual void OnRightButtonDown(MOUSEINFO
*mi
) {
78 virtual void OnMiddleButtonUp(MOUSEINFO
*mi
) {
81 virtual void OnMiddleButtonDown(MOUSEINFO
*mi
) {
86 uint8_t bmLeftCtrl
: 1;
87 uint8_t bmLeftShift
: 1;
88 uint8_t bmLeftAlt
: 1;
89 uint8_t bmLeftGUI
: 1;
90 uint8_t bmRightCtrl
: 1;
91 uint8_t bmRightShift
: 1;
92 uint8_t bmRightAlt
: 1;
93 uint8_t bmRightGUI
: 1;
99 uint8_t bmLeftCtrl
: 1;
100 uint8_t bmLeftShift
: 1;
101 uint8_t bmLeftAlt
: 1;
102 uint8_t bmLeftGUI
: 1;
103 uint8_t bmRightCtrl
: 1;
104 uint8_t bmRightShift
: 1;
105 uint8_t bmRightAlt
: 1;
106 uint8_t bmRightGUI
: 1;
113 uint8_t bmNumLock
: 1;
114 uint8_t bmCapsLock
: 1;
115 uint8_t bmScrollLock
: 1;
116 uint8_t bmCompose
: 1;
118 uint8_t bmReserved
: 3;
121 class KeyboardReportParser
: public HIDReportParser
{
122 static const uint8_t numKeys
[10];
123 static const uint8_t symKeysUp
[12];
124 static const uint8_t symKeysLo
[12];
125 static const uint8_t padKeys
[5];
131 uint8_t bInfo
[sizeof (KBDINFO
)];
139 uint8_t OemToAscii(uint8_t mod
, uint8_t key
);
143 KeyboardReportParser() {
144 kbdLockingKeys
.bLeds
= 0;
147 void Parse(HID
*hid
, bool is_rpt_id
, uint8_t len
, uint8_t *buf
);
151 virtual uint8_t HandleLockingKeys(HID
* hid
, uint8_t key
) {
152 uint8_t old_keys
= kbdLockingKeys
.bLeds
;
155 case UHS_HID_BOOT_KEY_NUM_LOCK
:
156 kbdLockingKeys
.kbdLeds
.bmNumLock
= ~kbdLockingKeys
.kbdLeds
.bmNumLock
;
158 case UHS_HID_BOOT_KEY_CAPS_LOCK
:
159 kbdLockingKeys
.kbdLeds
.bmCapsLock
= ~kbdLockingKeys
.kbdLeds
.bmCapsLock
;
161 case UHS_HID_BOOT_KEY_SCROLL_LOCK
:
162 kbdLockingKeys
.kbdLeds
.bmScrollLock
= ~kbdLockingKeys
.kbdLeds
.bmScrollLock
;
166 if(old_keys
!= kbdLockingKeys
.bLeds
&& hid
)
167 return (hid
->SetReport(0, 0/*hid->GetIface()*/, 2, 0, 1, &kbdLockingKeys
.bLeds
));
172 virtual void OnControlKeysChanged(uint8_t before
, uint8_t after
) {
175 virtual void OnKeyDown(uint8_t mod
, uint8_t key
) {
178 virtual void OnKeyUp(uint8_t mod
, uint8_t key
) {
181 virtual const uint8_t *getNumKeys() {
185 virtual const uint8_t *getSymKeysUp() {
189 virtual const uint8_t *getSymKeysLo() {
193 virtual const uint8_t *getPadKeys() {
198 template <const uint8_t BOOT_PROTOCOL
>
199 class HIDBoot
: public HID
//public USBDeviceConfig, public UsbConfigXtracter
201 EpInfo epInfo
[totalEndpoints(BOOT_PROTOCOL
)];
202 HIDReportParser
*pRptParser
[epMUL(BOOT_PROTOCOL
)];
204 uint8_t bConfNum
; // configuration number
205 uint8_t bIfaceNum
; // Interface Number
206 uint8_t bNumIface
; // number of interfaces in the configuration
207 uint8_t bNumEP
; // total number of EP in the configuration
208 uint32_t qNextPollTime
; // next poll time
209 bool bPollEnable
; // poll enable flag
210 uint8_t bInterval
; // largest interval
214 virtual HIDReportParser
* GetReportParser(uint8_t id
) {
215 return pRptParser
[id
];
221 virtual bool SetReportParser(uint8_t id
, HIDReportParser
*prs
) {
222 pRptParser
[id
] = prs
;
226 // USBDeviceConfig implementation
227 uint8_t Init(uint8_t parent
, uint8_t port
, bool lowspeed
);
231 virtual uint8_t GetAddress() {
235 virtual bool isReady() {
239 // UsbConfigXtracter implementation
240 // Method should be defined here if virtual.
241 virtual void EndpointXtract(uint8_t conf
, uint8_t iface
, uint8_t alt
, uint8_t proto
, const USB_ENDPOINT_DESCRIPTOR
*ep
);
243 virtual bool DEVCLASSOK(uint8_t klass
) {
244 return (klass
== USB_CLASS_HID
);
247 virtual bool DEVSUBCLASSOK(uint8_t subklass
) {
248 return (subklass
== BOOT_PROTOCOL
);
252 template <const uint8_t BOOT_PROTOCOL
>
253 HIDBoot
<BOOT_PROTOCOL
>::HIDBoot(USB
*p
) :
259 for(int i
= 0; i
< epMUL(BOOT_PROTOCOL
); i
++) {
260 pRptParser
[i
] = NULL
;
263 pUsb
->RegisterDeviceClass(this);
266 template <const uint8_t BOOT_PROTOCOL
>
267 void HIDBoot
<BOOT_PROTOCOL
>::Initialize() {
268 for(int i
= 0; i
< totalEndpoints(BOOT_PROTOCOL
); i
++) {
269 epInfo
[i
].epAddr
= 0;
270 epInfo
[i
].maxPktSize
= (i
) ? 0 : 8;
271 epInfo
[i
].epAttribs
= 0;
272 epInfo
[i
].bmNakPower
= (i
) ? USB_NAK_NOWAIT
: USB_NAK_MAX_POWER
;
279 template <const uint8_t BOOT_PROTOCOL
>
280 uint8_t HIDBoot
<BOOT_PROTOCOL
>::Init(uint8_t parent
, uint8_t port
, bool lowspeed
) {
281 const uint8_t constBufSize
= sizeof (USB_DEVICE_DESCRIPTOR
);
283 uint8_t buf
[constBufSize
];
286 EpInfo
*oldep_ptr
= NULL
;
288 //uint16_t cd_len = 0;
290 uint8_t num_of_conf
; // number of configurations
291 //uint8_t num_of_intf; // number of interfaces
293 AddressPool
&addrPool
= pUsb
->GetAddressPool();
295 USBTRACE("BM Init\r\n");
296 //USBTRACE2("totalEndpoints:", (uint8_t) (totalEndpoints(BOOT_PROTOCOL)));
297 //USBTRACE2("epMUL:", epMUL(BOOT_PROTOCOL));
300 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
;
303 // Get pointer to pseudo device with address 0 assigned
304 p
= addrPool
.GetUsbDevicePtr(0);
307 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
;
310 USBTRACE("epinfo\r\n");
311 return USB_ERROR_EPINFO_IS_NULL
;
314 // Save old pointer to EP_RECORD of address 0
315 oldep_ptr
= p
->epinfo
;
317 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
320 p
->lowspeed
= lowspeed
;
322 // Get device descriptor
323 rcode
= pUsb
->getDevDescr(0, 0, 8, (uint8_t*)buf
);
326 len
= (buf
[0] > constBufSize
) ? constBufSize
: buf
[0];
330 p
->epinfo
= oldep_ptr
;
332 goto FailGetDevDescr
;
336 p
->epinfo
= oldep_ptr
;
338 // Allocate new address according to device class
339 bAddress
= addrPool
.AllocAddress(parent
, false, port
);
342 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
;
344 // Extract Max Packet Size from the device descriptor
345 epInfo
[0].maxPktSize
= (uint8_t)((USB_DEVICE_DESCRIPTOR
*)buf
)->bMaxPacketSize0
;
347 // Assign new address to the device
348 rcode
= pUsb
->setAddr(0, 0, bAddress
);
352 addrPool
.FreeAddress(bAddress
);
354 USBTRACE2("setAddr:", rcode
);
357 //delay(2); //per USB 2.0 sect.9.2.6.3
359 USBTRACE2("Addr:", bAddress
);
363 p
= addrPool
.GetUsbDevicePtr(bAddress
);
366 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
;
368 p
->lowspeed
= lowspeed
;
371 rcode
= pUsb
->getDevDescr(bAddress
, 0, len
, (uint8_t*)buf
);
374 goto FailGetDevDescr
;
376 num_of_conf
= ((USB_DEVICE_DESCRIPTOR
*)buf
)->bNumConfigurations
;
378 USBTRACE2("NC:", num_of_conf
);
380 // GCC will optimize unused stuff away.
381 if((BOOT_PROTOCOL
& (HID_PROTOCOL_KEYBOARD
| HID_PROTOCOL_MOUSE
)) == (HID_PROTOCOL_KEYBOARD
| HID_PROTOCOL_MOUSE
)) {
382 USBTRACE("HID_PROTOCOL_KEYBOARD AND MOUSE\r\n");
385 HID_BOOT_INTF_SUBCLASS
,
386 HID_PROTOCOL_KEYBOARD
| HID_PROTOCOL_MOUSE
,
387 CP_MASK_COMPARE_ALL
> confDescrParser(this);
388 confDescrParser
.SetOR(); // Use the OR variant.
389 for(uint8_t i
= 0; i
< num_of_conf
; i
++) {
390 pUsb
->getConfDescr(bAddress
, 0, i
, &confDescrParser
);
391 if(bNumEP
== (uint8_t)(totalEndpoints(BOOT_PROTOCOL
)))
395 // GCC will optimize unused stuff away.
396 if(BOOT_PROTOCOL
& HID_PROTOCOL_KEYBOARD
) {
397 USBTRACE("HID_PROTOCOL_KEYBOARD\r\n");
398 for(uint8_t i
= 0; i
< num_of_conf
; i
++) {
401 HID_BOOT_INTF_SUBCLASS
,
402 HID_PROTOCOL_KEYBOARD
,
403 CP_MASK_COMPARE_ALL
> confDescrParserA(this);
405 pUsb
->getConfDescr(bAddress
, 0, i
, &confDescrParserA
);
406 if(bNumEP
== (uint8_t)(totalEndpoints(BOOT_PROTOCOL
)))
411 // GCC will optimize unused stuff away.
412 if(BOOT_PROTOCOL
& HID_PROTOCOL_MOUSE
) {
413 USBTRACE("HID_PROTOCOL_MOUSE\r\n");
414 for(uint8_t i
= 0; i
< num_of_conf
; i
++) {
417 HID_BOOT_INTF_SUBCLASS
,
419 CP_MASK_COMPARE_ALL
> confDescrParserB(this);
421 pUsb
->getConfDescr(bAddress
, 0, i
, &confDescrParserB
);
422 if(bNumEP
== ((uint8_t)(totalEndpoints(BOOT_PROTOCOL
))))
428 USBTRACE2("bNumEP:", bNumEP
);
430 if(bNumEP
!= (uint8_t)(totalEndpoints(BOOT_PROTOCOL
))) {
431 rcode
= USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
;
435 // Assign epInfo to epinfo pointer
436 rcode
= pUsb
->setEpInfoEntry(bAddress
, bNumEP
, epInfo
);
437 //USBTRACE2("setEpInfoEntry returned ", rcode);
438 USBTRACE2("Cnf:", bConfNum
);
442 // Set Configuration Value
443 rcode
= pUsb
->setConf(bAddress
, 0, bConfNum
);
446 goto FailSetConfDescr
;
450 USBTRACE2("bIfaceNum:", bIfaceNum
);
451 USBTRACE2("bNumIface:", bNumIface
);
453 // Yes, mouse wants SetProtocol and SetIdle too!
454 for(uint8_t i
= 0; i
< epMUL(BOOT_PROTOCOL
); i
++) {
455 USBTRACE2("\r\nInterface:", i
);
456 rcode
= SetProtocol(i
, HID_BOOT_PROTOCOL
);
457 if(rcode
) goto FailSetProtocol
;
458 USBTRACE2("PROTOCOL SET HID_BOOT rcode:", rcode
);
459 rcode
= SetIdle(i
, 0, 0);
460 USBTRACE2("SET_IDLE rcode:", rcode
);
461 // if(rcode) goto FailSetIdle; This can fail.
462 // Get the RPIPE and just throw it away.
463 SinkParser
<USBReadParser
, uint16_t, uint16_t> sink
;
464 rcode
= GetReportDescr(i
, &sink
);
465 USBTRACE2("RPIPE rcode:", rcode
);
468 // Get RPIPE and throw it away.
470 if(BOOT_PROTOCOL
& HID_PROTOCOL_KEYBOARD
) {
471 // Wake keyboard interface by twinkling up to 5 LEDs that are in the spec.
472 // kana, compose, scroll, caps, num
473 rcode
= 0x20; // Reuse rcode.
476 // Ignore any error returned, we don't care if LED is not supported
477 SetReport(0, 0, 2, 0, 1, &rcode
); // Eventually becomes zero (All off)
481 USBTRACE("BM configured\r\n");
487 #ifdef DEBUG_USB_HOST
488 NotifyFailGetDevDescr();
492 //FailSetDevTblEntry:
493 //#ifdef DEBUG_USB_HOST
494 // NotifyFailSetDevTblEntry();
499 //#ifdef DEBUG_USB_HOST
500 // NotifyFailGetConfDescr();
505 #ifdef DEBUG_USB_HOST
506 NotifyFailSetConfDescr();
511 #ifdef DEBUG_USB_HOST
512 USBTRACE("SetProto:");
517 //#ifdef DEBUG_USB_HOST
518 // USBTRACE("SetIdle:");
522 #ifdef DEBUG_USB_HOST
530 template <const uint8_t BOOT_PROTOCOL
>
531 void HIDBoot
<BOOT_PROTOCOL
>::EndpointXtract(uint8_t conf
, uint8_t iface
, uint8_t alt
, uint8_t proto
, const USB_ENDPOINT_DESCRIPTOR
*pep
) {
533 // If the first configuration satisfies, the others are not considered.
534 //if(bNumEP > 1 && conf != bConfNum)
535 if(bNumEP
== totalEndpoints(BOOT_PROTOCOL
))
541 if((pep
->bmAttributes
& 0x03) == 3 && (pep
->bEndpointAddress
& 0x80) == 0x80) {
542 if(pep
->bInterval
> bInterval
) bInterval
= pep
->bInterval
;
544 // Fill in the endpoint info structure
545 epInfo
[bNumEP
].epAddr
= (pep
->bEndpointAddress
& 0x0F);
546 epInfo
[bNumEP
].maxPktSize
= (uint8_t)pep
->wMaxPacketSize
;
547 epInfo
[bNumEP
].epAttribs
= 0;
548 epInfo
[bNumEP
].bmNakPower
= USB_NAK_NOWAIT
;
554 template <const uint8_t BOOT_PROTOCOL
>
555 uint8_t HIDBoot
<BOOT_PROTOCOL
>::Release() {
556 pUsb
->GetAddressPool().FreeAddress(bAddress
);
568 template <const uint8_t BOOT_PROTOCOL
>
569 uint8_t HIDBoot
<BOOT_PROTOCOL
>::Poll() {
572 if(bPollEnable
&& ((long)(millis() - qNextPollTime
) >= 0L)) {
574 // To-do: optimize manually, using the for loop only if needed.
575 for(int i
= 0; i
< epMUL(BOOT_PROTOCOL
); i
++) {
576 const uint16_t const_buff_len
= 16;
577 uint8_t buf
[const_buff_len
];
579 USBTRACE3("(hidboot.h) i=", i
, 0x81);
580 USBTRACE3("(hidboot.h) epInfo[epInterruptInIndex + i].epAddr=", epInfo
[epInterruptInIndex
+ i
].epAddr
, 0x81);
581 USBTRACE3("(hidboot.h) epInfo[epInterruptInIndex + i].maxPktSize=", epInfo
[epInterruptInIndex
+ i
].maxPktSize
, 0x81);
582 uint16_t read
= (uint16_t)epInfo
[epInterruptInIndex
+ i
].maxPktSize
;
584 rcode
= pUsb
->inTransfer(bAddress
, epInfo
[epInterruptInIndex
+ i
].epAddr
, &read
, buf
);
585 // SOME buggy dongles report extra keys (like sleep) using a 2 byte packet on the wrong endpoint.
586 // Since keyboard and mice must report at least 3 bytes, we ignore the extra data.
587 if(!rcode
&& read
> 2) {
589 pRptParser
[i
]->Parse((HID
*)this, 0, (uint8_t)read
, buf
);
590 #ifdef DEBUG_USB_HOST
591 // We really don't care about errors and anomalies unless we are debugging.
594 USBTRACE3("(hidboot.h) Poll:", rcode
, 0x81);
597 USBTRACE3("(hidboot.h) Strange read count: ", read
, 0x80);
598 USBTRACE3("(hidboot.h) Interface:", i
, 0x80);
602 if(!rcode
&& read
&& (UsbDEBUGlvl
> 0x7f)) {
603 for(uint8_t i
= 0; i
< read
; i
++) {
604 PrintHex
<uint8_t > (buf
[i
], 0x80);
605 USBTRACE1(" ", 0x80);
608 USBTRACE1("\r\n", 0x80);
613 qNextPollTime
= millis() + bInterval
;
618 #endif // __HIDBOOTMOUSE_H__