USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hidboot.h
Go to the documentation of this file.
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 <inttypes.h>
21 #include <avr/pgmspace.h>
22 #include "avrpins.h"
23 #include "max3421e.h"
24 #include "usbhost.h"
25 #include "usb_ch9.h"
26 #include "Usb.h"
27 #include "hid.h"
28 
29 #if defined(ARDUINO) && ARDUINO >=100
30 #include "Arduino.h"
31 #else
32 #include <WProgram.h>
33 #endif
34 
35 #include "printhex.h"
36 #include "hexdump.h"
37 #include "message.h"
38 
39 #include "confdescparser.h"
40 
41 #define KEY_SPACE 0x2c
42 #define KEY_ZERO 0x27
43 #define KEY_ZERO2 0x62
44 #define KEY_ENTER 0x28
45 #define KEY_PERIOD 0x63
46 
47 struct MOUSEINFO {
48 
49  struct {
50  uint8_t bmLeftButton : 1;
51  uint8_t bmRightButton : 1;
52  uint8_t bmMiddleButton : 1;
53  uint8_t bmDummy : 1;
54  };
55  int8_t dX;
56  int8_t dY;
57 };
58 
60 
61  union {
63  uint8_t bInfo[sizeof(MOUSEINFO)];
64  } prevState;
65 
66 public:
67  virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
68 
69 protected:
70 
71  virtual void OnMouseMove(MOUSEINFO *mi) {
72  };
73 
74  virtual void OnLeftButtonUp(MOUSEINFO *mi) {
75  };
76 
77  virtual void OnLeftButtonDown(MOUSEINFO *mi) {
78  };
79 
80  virtual void OnRightButtonUp(MOUSEINFO *mi) {
81  };
82 
83  virtual void OnRightButtonDown(MOUSEINFO *mi) {
84  };
85 
86  virtual void OnMiddleButtonUp(MOUSEINFO *mi) {
87  };
88 
89  virtual void OnMiddleButtonDown(MOUSEINFO *mi) {
90  };
91 };
92 
93 struct MODIFIERKEYS {
94  uint8_t bmLeftCtrl : 1;
95  uint8_t bmLeftShift : 1;
96  uint8_t bmLeftAlt : 1;
97  uint8_t bmLeftGUI : 1;
98  uint8_t bmRightCtrl : 1;
99  uint8_t bmRightShift : 1;
100  uint8_t bmRightAlt : 1;
101  uint8_t bmRightGUI : 1;
102 };
103 
104 struct KBDINFO {
105 
106  struct {
107  uint8_t bmLeftCtrl : 1;
108  uint8_t bmLeftShift : 1;
109  uint8_t bmLeftAlt : 1;
110  uint8_t bmLeftGUI : 1;
111  uint8_t bmRightCtrl : 1;
112  uint8_t bmRightShift : 1;
113  uint8_t bmRightAlt : 1;
114  uint8_t bmRightGUI : 1;
115  };
116  uint8_t bReserved;
117  uint8_t Keys[6];
118 };
119 
120 struct KBDLEDS {
121  uint8_t bmNumLock : 1;
122  uint8_t bmCapsLock : 1;
123  uint8_t bmScrollLock : 1;
124  uint8_t bmCompose : 1;
125  uint8_t bmKana : 1;
126  uint8_t bmReserved : 3;
127 };
128 
129 #define KEY_NUM_LOCK 0x53
130 #define KEY_CAPS_LOCK 0x39
131 #define KEY_SCROLL_LOCK 0x47
132 
134  static const uint8_t numKeys[];
135  static const uint8_t symKeysUp[];
136  static const uint8_t symKeysLo[];
137  static const uint8_t padKeys[];
138 
139 protected:
140 
141  union {
143  uint8_t bInfo[sizeof(KBDINFO)];
144  } prevState;
145 
146  union {
148  uint8_t bLeds;
149  } kbdLockingKeys;
150 
151  uint8_t OemToAscii(uint8_t mod, uint8_t key);
152 
153 public:
154 
156  kbdLockingKeys.bLeds = 0;
157  };
158 
159  virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
160 
161 protected:
162  virtual uint8_t HandleLockingKeys(HID* hid, uint8_t key);
163 
164  virtual void OnControlKeysChanged(uint8_t before, uint8_t after) {
165  };
166 
167  virtual void OnKeyDown(uint8_t mod, uint8_t key) {
168  };
169 
170  virtual void OnKeyUp(uint8_t mod, uint8_t key) {
171  };
172 };
173 
174 #define totalEndpoints 2
175 
176 #define HID_MAX_HID_CLASS_DESCRIPTORS 5
177 
178 template <const uint8_t BOOT_PROTOCOL>
179 class HIDBoot : public HID //public USBDeviceConfig, public UsbConfigXtracter
180 {
181  EpInfo epInfo[totalEndpoints];
182 
183  HIDReportParser *pRptParser;
184 
185  uint8_t bConfNum; // configuration number
186  uint8_t bIfaceNum; // Interface Number
187  uint8_t bNumIface; // number of interfaces in the configuration
188  uint8_t bNumEP; // total number of EP in the configuration
189  uint32_t qNextPollTime; // next poll time
190  bool bPollEnable; // poll enable flag
191 
192  void Initialize();
193 
194  virtual HIDReportParser* GetReportParser(uint8_t id) {
195  return pRptParser;
196  };
197 
198 public:
199  HIDBoot(USB *p);
200 
201  virtual bool SetReportParser(uint8_t id, HIDReportParser *prs) {
202  pRptParser = prs;
203  return true;
204  };
205 
206  // USBDeviceConfig implementation
207  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
208  virtual uint8_t Release();
209  virtual uint8_t Poll();
210 
211  virtual uint8_t GetAddress() {
212  return bAddress;
213  };
214 
215  // UsbConfigXtracter implementation
216  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
217 };
218 
219 template <const uint8_t BOOT_PROTOCOL>
221 HID(p),
222 qNextPollTime(0),
223 bPollEnable(false),
224 pRptParser(NULL) {
225  Initialize();
226 
227  if(pUsb)
228  pUsb->RegisterDeviceClass(this);
229 }
230 
231 template <const uint8_t BOOT_PROTOCOL>
233  for(uint8_t i = 0; i < totalEndpoints; i++) {
234  epInfo[i].epAddr = 0;
235  epInfo[i].maxPktSize = (i) ? 0 : 8;
236  epInfo[i].epAttribs = 0;
237  epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
238  }
239  bNumEP = 1;
240  bNumIface = 0;
241  bConfNum = 0;
242 }
243 
244 template <const uint8_t BOOT_PROTOCOL>
245 uint8_t HIDBoot<BOOT_PROTOCOL>::Init(uint8_t parent, uint8_t port, bool lowspeed) {
246  const uint8_t constBufSize = sizeof(USB_DEVICE_DESCRIPTOR);
247 
248  uint8_t buf[constBufSize];
249  uint8_t rcode;
250  UsbDevice *p = NULL;
251  EpInfo *oldep_ptr = NULL;
252  uint8_t len = 0;
253  uint16_t cd_len = 0;
254 
255  uint8_t num_of_conf; // number of configurations
256  uint8_t num_of_intf; // number of interfaces
257 
258  AddressPool &addrPool = pUsb->GetAddressPool();
259 
260  USBTRACE("BM Init\r\n");
261 
262  if(bAddress)
264 
265  // Get pointer to pseudo device with address 0 assigned
266  p = addrPool.GetUsbDevicePtr(0);
267 
268  if(!p)
270 
271  if(!p->epinfo) {
272  USBTRACE("epinfo\r\n");
274  }
275 
276  // Save old pointer to EP_RECORD of address 0
277  oldep_ptr = p->epinfo;
278 
279  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
280  p->epinfo = epInfo;
281 
282  p->lowspeed = lowspeed;
283 
284  // Get device descriptor
285  rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*) buf);
286 
287  if(!rcode)
288  len = (buf[0] > constBufSize) ? constBufSize : buf[0];
289 
290  if(rcode) {
291  // Restore p->epinfo
292  p->epinfo = oldep_ptr;
293 
294  goto FailGetDevDescr;
295  }
296 
297  // Restore p->epinfo
298  p->epinfo = oldep_ptr;
299 
300  // Allocate new address according to device class
301  bAddress = addrPool.AllocAddress(parent, false, port);
302 
303  if(!bAddress)
305 
306  // Extract Max Packet Size from the device descriptor
307  epInfo[0].maxPktSize = (uint8_t) ((USB_DEVICE_DESCRIPTOR*) buf)->bMaxPacketSize0;
308 
309  // Assign new address to the device
310  rcode = pUsb->setAddr(0, 0, bAddress);
311 
312  if(rcode) {
313  p->lowspeed = false;
314  addrPool.FreeAddress(bAddress);
315  bAddress = 0;
316  USBTRACE2("setAddr:", rcode);
317  return rcode;
318  }
319 
320  USBTRACE2("Addr:", bAddress);
321 
322  p->lowspeed = false;
323 
324  p = addrPool.GetUsbDevicePtr(bAddress);
325 
326  if(!p)
328 
329  p->lowspeed = lowspeed;
330 
331  if(len)
332  rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*) buf);
333 
334  if(rcode)
335  goto FailGetDevDescr;
336 
337  num_of_conf = ((USB_DEVICE_DESCRIPTOR*) buf)->bNumConfigurations;
338 
339  // Assign epInfo to epinfo pointer
340  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
341 
342  if(rcode)
343  goto FailSetDevTblEntry;
344 
345  //USBTRACE2("NC:", num_of_conf);
346 
347  for(uint8_t i = 0; i < num_of_conf; i++) {
351  BOOT_PROTOCOL,
352  CP_MASK_COMPARE_ALL> confDescrParser(this);
353 
354  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
355 
356  if(bNumEP > 1)
357  break;
358  } // for
359 
360  if(bNumEP < 2)
362 
363  //USBTRACE2("\r\nbAddr:", bAddress);
364  //USBTRACE2("\r\nbNumEP:", bNumEP);
365 
366  // Assign epInfo to epinfo pointer
367  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
368 
369  //USBTRACE2("\r\nCnf:", bConfNum);
370 
371  // Set Configuration Value
372  rcode = pUsb->setConf(bAddress, 0, bConfNum);
373 
374  if(rcode)
375  goto FailSetConfDescr;
376 
377  //USBTRACE2("\r\nIf:", bIfaceNum);
378 
379  rcode = SetProtocol(bIfaceNum, HID_BOOT_PROTOCOL);
380 
381  if(rcode)
382  goto FailSetProtocol;
383 
384  if(BOOT_PROTOCOL == 1) {
385  rcode = SetIdle(bIfaceNum, 0, 0);
386 
387  if(rcode)
388  goto FailSetIdle;
389  }
390  USBTRACE("BM configured\r\n");
391 
392  bPollEnable = true;
393  return 0;
394 
395 FailGetDevDescr:
396 #ifdef DEBUG_USB_HOST
398  goto Fail;
399 #endif
400 
401 FailSetDevTblEntry:
402 #ifdef DEBUG_USB_HOST
404  goto Fail;
405 #endif
406 
407 FailGetConfDescr:
408 #ifdef DEBUG_USB_HOST
410  goto Fail;
411 #endif
412 
413 FailSetConfDescr:
414 #ifdef DEBUG_USB_HOST
416  goto Fail;
417 #endif
418 
419 FailSetProtocol:
420 #ifdef DEBUG_USB_HOST
421  USBTRACE("SetProto:");
422  goto Fail;
423 #endif
424 
425 FailSetIdle:
426 #ifdef DEBUG_USB_HOST
427  USBTRACE("SetIdle:");
428 #endif
429 
430 Fail:
431 #ifdef DEBUG_USB_HOST
432  NotifyFail(rcode);
433 #endif
434  Release();
435  return rcode;
436 }
437 
438 template <const uint8_t BOOT_PROTOCOL>
439 void HIDBoot<BOOT_PROTOCOL>::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
440  // If the first configuration satisfies, the others are not concidered.
441  if(bNumEP > 1 && conf != bConfNum)
442  return;
443 
444  bConfNum = conf;
445  bIfaceNum = iface;
446 
447  uint8_t index;
448 
449  if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) {
450  index = epInterruptInIndex;
451 
452  // Fill in the endpoint info structure
453  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
454  epInfo[index].maxPktSize = (uint8_t) pep->wMaxPacketSize;
455  epInfo[index].epAttribs = 0;
456  epInfo[index].bmNakPower = USB_NAK_NOWAIT;
457 
458  bNumEP++;
459  }
460 }
461 
462 template <const uint8_t BOOT_PROTOCOL>
464  pUsb->GetAddressPool().FreeAddress(bAddress);
465 
466  bConfNum = 0;
467  bIfaceNum = 0;
468  bNumEP = 1;
469  bAddress = 0;
470  qNextPollTime = 0;
471  bPollEnable = false;
472  return 0;
473 }
474 
475 template <const uint8_t BOOT_PROTOCOL>
477  uint8_t rcode = 0;
478 
479  if(!bPollEnable)
480  return 0;
481 
482  if(qNextPollTime <= millis()) {
483  qNextPollTime = millis() + 10;
484 
485  const uint8_t const_buff_len = 16;
486  uint8_t buf[const_buff_len];
487 
488  uint16_t read = (uint16_t) epInfo[epInterruptInIndex].maxPktSize;
489 
490  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex].epAddr, &read, buf);
491 
492  if(rcode) {
493  if(rcode != hrNAK)
494  USBTRACE2("Poll:", rcode);
495  return rcode;
496  }
497  //for (uint8_t i=0; i<read; i++)
498  // PrintHex<uint8_t>(buf[i]);
499  //if (read)
500  // USB_HOST_SERIAL.println("");
501 
502  if(pRptParser)
503  pRptParser->Parse((HID*)this, 0, (uint8_t) read, buf);
504  }
505  return rcode;
506 }
507 
508 
509 #endif // __HIDBOOTMOUSE_H__