]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/hidboot.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / hidboot.h
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
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
8 the GPL2 ("Copyleft").
9
10 Contact information
11 -------------------
12
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16 */
17 #if !defined(__HIDBOOT_H__)
18 #define __HIDBOOT_H__
19
20 #include "hid.h"
21
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
30
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))
35
36 // Already defined in hid.h
37 // #define HID_MAX_HID_CLASS_DESCRIPTORS 5
38
39 struct MOUSEINFO {
40
41 struct {
42 uint8_t bmLeftButton : 1;
43 uint8_t bmRightButton : 1;
44 uint8_t bmMiddleButton : 1;
45 uint8_t bmDummy : 5;
46 };
47 int8_t dX;
48 int8_t dY;
49 };
50
51 class MouseReportParser : public HIDReportParser {
52
53 union {
54 MOUSEINFO mouseInfo;
55 uint8_t bInfo[sizeof (MOUSEINFO)];
56 } prevState;
57
58 public:
59 void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
60
61 protected:
62
63 virtual void OnMouseMove(MOUSEINFO *mi) {
64 };
65
66 virtual void OnLeftButtonUp(MOUSEINFO *mi) {
67 };
68
69 virtual void OnLeftButtonDown(MOUSEINFO *mi) {
70 };
71
72 virtual void OnRightButtonUp(MOUSEINFO *mi) {
73 };
74
75 virtual void OnRightButtonDown(MOUSEINFO *mi) {
76 };
77
78 virtual void OnMiddleButtonUp(MOUSEINFO *mi) {
79 };
80
81 virtual void OnMiddleButtonDown(MOUSEINFO *mi) {
82 };
83 };
84
85 struct MODIFIERKEYS {
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;
94 };
95
96 struct KBDINFO {
97
98 struct {
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;
107 };
108 uint8_t bReserved;
109 uint8_t Keys[6];
110 };
111
112 struct KBDLEDS {
113 uint8_t bmNumLock : 1;
114 uint8_t bmCapsLock : 1;
115 uint8_t bmScrollLock : 1;
116 uint8_t bmCompose : 1;
117 uint8_t bmKana : 1;
118 uint8_t bmReserved : 3;
119 };
120
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];
126
127 protected:
128
129 union {
130 KBDINFO kbdInfo;
131 uint8_t bInfo[sizeof (KBDINFO)];
132 } prevState;
133
134 union {
135 KBDLEDS kbdLeds;
136 uint8_t bLeds;
137 } kbdLockingKeys;
138
139 uint8_t OemToAscii(uint8_t mod, uint8_t key);
140
141 public:
142
143 KeyboardReportParser() {
144 kbdLockingKeys.bLeds = 0;
145 };
146
147 void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
148
149 protected:
150
151 virtual uint8_t HandleLockingKeys(HID* hid, uint8_t key) {
152 uint8_t old_keys = kbdLockingKeys.bLeds;
153
154 switch(key) {
155 case UHS_HID_BOOT_KEY_NUM_LOCK:
156 kbdLockingKeys.kbdLeds.bmNumLock = ~kbdLockingKeys.kbdLeds.bmNumLock;
157 break;
158 case UHS_HID_BOOT_KEY_CAPS_LOCK:
159 kbdLockingKeys.kbdLeds.bmCapsLock = ~kbdLockingKeys.kbdLeds.bmCapsLock;
160 break;
161 case UHS_HID_BOOT_KEY_SCROLL_LOCK:
162 kbdLockingKeys.kbdLeds.bmScrollLock = ~kbdLockingKeys.kbdLeds.bmScrollLock;
163 break;
164 }
165
166 if(old_keys != kbdLockingKeys.bLeds && hid)
167 return (hid->SetReport(0, 0/*hid->GetIface()*/, 2, 0, 1, &kbdLockingKeys.bLeds));
168
169 return 0;
170 };
171
172 virtual void OnControlKeysChanged(uint8_t before, uint8_t after) {
173 };
174
175 virtual void OnKeyDown(uint8_t mod, uint8_t key) {
176 };
177
178 virtual void OnKeyUp(uint8_t mod, uint8_t key) {
179 };
180
181 virtual const uint8_t *getNumKeys() {
182 return numKeys;
183 };
184
185 virtual const uint8_t *getSymKeysUp() {
186 return symKeysUp;
187 };
188
189 virtual const uint8_t *getSymKeysLo() {
190 return symKeysLo;
191 };
192
193 virtual const uint8_t *getPadKeys() {
194 return padKeys;
195 };
196 };
197
198 template <const uint8_t BOOT_PROTOCOL>
199 class HIDBoot : public HID //public USBDeviceConfig, public UsbConfigXtracter
200 {
201 EpInfo epInfo[totalEndpoints(BOOT_PROTOCOL)];
202 HIDReportParser *pRptParser[epMUL(BOOT_PROTOCOL)];
203
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
211
212 void Initialize();
213
214 virtual HIDReportParser* GetReportParser(uint8_t id) {
215 return pRptParser[id];
216 };
217
218 public:
219 HIDBoot(USB *p);
220
221 virtual bool SetReportParser(uint8_t id, HIDReportParser *prs) {
222 pRptParser[id] = prs;
223 return true;
224 };
225
226 // USBDeviceConfig implementation
227 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
228 uint8_t Release();
229 uint8_t Poll();
230
231 virtual uint8_t GetAddress() {
232 return bAddress;
233 };
234
235 virtual bool isReady() {
236 return bPollEnable;
237 };
238
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);
242
243 virtual bool DEVCLASSOK(uint8_t klass) {
244 return (klass == USB_CLASS_HID);
245 }
246
247 virtual bool DEVSUBCLASSOK(uint8_t subklass) {
248 return (subklass == BOOT_PROTOCOL);
249 }
250 };
251
252 template <const uint8_t BOOT_PROTOCOL>
253 HIDBoot<BOOT_PROTOCOL>::HIDBoot(USB *p) :
254 HID(p),
255 qNextPollTime(0),
256 bPollEnable(false) {
257 Initialize();
258
259 for(int i = 0; i < epMUL(BOOT_PROTOCOL); i++) {
260 pRptParser[i] = NULL;
261 }
262 if(pUsb)
263 pUsb->RegisterDeviceClass(this);
264 }
265
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;
273 }
274 bNumEP = 1;
275 bNumIface = 0;
276 bConfNum = 0;
277 }
278
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);
282
283 uint8_t buf[constBufSize];
284 uint8_t rcode;
285 UsbDevice *p = NULL;
286 EpInfo *oldep_ptr = NULL;
287 uint8_t len = 0;
288 //uint16_t cd_len = 0;
289
290 uint8_t num_of_conf; // number of configurations
291 //uint8_t num_of_intf; // number of interfaces
292
293 AddressPool &addrPool = pUsb->GetAddressPool();
294
295 USBTRACE("BM Init\r\n");
296 //USBTRACE2("totalEndpoints:", (uint8_t) (totalEndpoints(BOOT_PROTOCOL)));
297 //USBTRACE2("epMUL:", epMUL(BOOT_PROTOCOL));
298
299 if(bAddress)
300 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
301
302 bInterval = 0;
303 // Get pointer to pseudo device with address 0 assigned
304 p = addrPool.GetUsbDevicePtr(0);
305
306 if(!p)
307 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
308
309 if(!p->epinfo) {
310 USBTRACE("epinfo\r\n");
311 return USB_ERROR_EPINFO_IS_NULL;
312 }
313
314 // Save old pointer to EP_RECORD of address 0
315 oldep_ptr = p->epinfo;
316
317 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
318 p->epinfo = epInfo;
319
320 p->lowspeed = lowspeed;
321
322 // Get device descriptor
323 rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
324
325 if(!rcode)
326 len = (buf[0] > constBufSize) ? constBufSize : buf[0];
327
328 if(rcode) {
329 // Restore p->epinfo
330 p->epinfo = oldep_ptr;
331
332 goto FailGetDevDescr;
333 }
334
335 // Restore p->epinfo
336 p->epinfo = oldep_ptr;
337
338 // Allocate new address according to device class
339 bAddress = addrPool.AllocAddress(parent, false, port);
340
341 if(!bAddress)
342 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
343
344 // Extract Max Packet Size from the device descriptor
345 epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
346
347 // Assign new address to the device
348 rcode = pUsb->setAddr(0, 0, bAddress);
349
350 if(rcode) {
351 p->lowspeed = false;
352 addrPool.FreeAddress(bAddress);
353 bAddress = 0;
354 USBTRACE2("setAddr:", rcode);
355 return rcode;
356 }
357 //delay(2); //per USB 2.0 sect.9.2.6.3
358
359 USBTRACE2("Addr:", bAddress);
360
361 p->lowspeed = false;
362
363 p = addrPool.GetUsbDevicePtr(bAddress);
364
365 if(!p)
366 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
367
368 p->lowspeed = lowspeed;
369
370 if(len)
371 rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
372
373 if(rcode)
374 goto FailGetDevDescr;
375
376 num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
377
378 USBTRACE2("NC:", num_of_conf);
379
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");
383 ConfigDescParser<
384 USB_CLASS_HID,
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)))
392 break;
393 }
394 } else {
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++) {
399 ConfigDescParser<
400 USB_CLASS_HID,
401 HID_BOOT_INTF_SUBCLASS,
402 HID_PROTOCOL_KEYBOARD,
403 CP_MASK_COMPARE_ALL> confDescrParserA(this);
404
405 pUsb->getConfDescr(bAddress, 0, i, &confDescrParserA);
406 if(bNumEP == (uint8_t)(totalEndpoints(BOOT_PROTOCOL)))
407 break;
408 }
409 }
410
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++) {
415 ConfigDescParser<
416 USB_CLASS_HID,
417 HID_BOOT_INTF_SUBCLASS,
418 HID_PROTOCOL_MOUSE,
419 CP_MASK_COMPARE_ALL> confDescrParserB(this);
420
421 pUsb->getConfDescr(bAddress, 0, i, &confDescrParserB);
422 if(bNumEP == ((uint8_t)(totalEndpoints(BOOT_PROTOCOL))))
423 break;
424
425 }
426 }
427 }
428 USBTRACE2("bNumEP:", bNumEP);
429
430 if(bNumEP != (uint8_t)(totalEndpoints(BOOT_PROTOCOL))) {
431 rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
432 goto Fail;
433 }
434
435 // Assign epInfo to epinfo pointer
436 rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
437 //USBTRACE2("setEpInfoEntry returned ", rcode);
438 USBTRACE2("Cnf:", bConfNum);
439
440 delay(1000);
441
442 // Set Configuration Value
443 rcode = pUsb->setConf(bAddress, 0, bConfNum);
444
445 if(rcode)
446 goto FailSetConfDescr;
447
448 delay(1000);
449
450 USBTRACE2("bIfaceNum:", bIfaceNum);
451 USBTRACE2("bNumIface:", bNumIface);
452
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);
466 }
467
468 // Get RPIPE and throw it away.
469
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.
474 while(rcode) {
475 rcode >>= 1;
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)
478 delay(25);
479 }
480 }
481 USBTRACE("BM configured\r\n");
482
483 bPollEnable = true;
484 return 0;
485
486 FailGetDevDescr:
487 #ifdef DEBUG_USB_HOST
488 NotifyFailGetDevDescr();
489 goto Fail;
490 #endif
491
492 //FailSetDevTblEntry:
493 //#ifdef DEBUG_USB_HOST
494 // NotifyFailSetDevTblEntry();
495 // goto Fail;
496 //#endif
497
498 //FailGetConfDescr:
499 //#ifdef DEBUG_USB_HOST
500 // NotifyFailGetConfDescr();
501 // goto Fail;
502 //#endif
503
504 FailSetConfDescr:
505 #ifdef DEBUG_USB_HOST
506 NotifyFailSetConfDescr();
507 goto Fail;
508 #endif
509
510 FailSetProtocol:
511 #ifdef DEBUG_USB_HOST
512 USBTRACE("SetProto:");
513 goto Fail;
514 #endif
515
516 //FailSetIdle:
517 //#ifdef DEBUG_USB_HOST
518 // USBTRACE("SetIdle:");
519 //#endif
520
521 Fail:
522 #ifdef DEBUG_USB_HOST
523 NotifyFail(rcode);
524 #endif
525 Release();
526
527 return rcode;
528 }
529
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) {
532
533 // If the first configuration satisfies, the others are not considered.
534 //if(bNumEP > 1 && conf != bConfNum)
535 if(bNumEP == totalEndpoints(BOOT_PROTOCOL))
536 return;
537
538 bConfNum = conf;
539 bIfaceNum = iface;
540
541 if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) {
542 if(pep->bInterval > bInterval) bInterval = pep->bInterval;
543
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;
549 bNumEP++;
550
551 }
552 }
553
554 template <const uint8_t BOOT_PROTOCOL>
555 uint8_t HIDBoot<BOOT_PROTOCOL>::Release() {
556 pUsb->GetAddressPool().FreeAddress(bAddress);
557
558 bConfNum = 0;
559 bIfaceNum = 0;
560 bNumEP = 1;
561 bAddress = 0;
562 qNextPollTime = 0;
563 bPollEnable = false;
564
565 return 0;
566 }
567
568 template <const uint8_t BOOT_PROTOCOL>
569 uint8_t HIDBoot<BOOT_PROTOCOL>::Poll() {
570 uint8_t rcode = 0;
571
572 if(bPollEnable && ((long)(millis() - qNextPollTime) >= 0L)) {
573
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];
578
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;
583
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) {
588 if(pRptParser[i])
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.
592 } else {
593 if(rcode != hrNAK) {
594 USBTRACE3("(hidboot.h) Poll:", rcode, 0x81);
595 }
596 if(!rcode && read) {
597 USBTRACE3("(hidboot.h) Strange read count: ", read, 0x80);
598 USBTRACE3("(hidboot.h) Interface:", i, 0x80);
599 }
600 }
601
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);
606 }
607 if(read)
608 USBTRACE1("\r\n", 0x80);
609 #endif
610 }
611
612 }
613 qNextPollTime = millis() + bInterval;
614 }
615 return rcode;
616 }
617
618 #endif // __HIDBOOTMOUSE_H__
Imprint / Impressum