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 OnKeyDown(uint8_t mod, uint8_t key) {
165  };
166 
167  virtual void OnKeyUp(uint8_t mod, uint8_t key) {
168  };
169 };
170 
171 #define totalEndpoints 2
172 
173 #define HID_MAX_HID_CLASS_DESCRIPTORS 5
174 
175 template <const uint8_t BOOT_PROTOCOL>
176 class HIDBoot : public HID //public USBDeviceConfig, public UsbConfigXtracter
177 {
178  EpInfo epInfo[totalEndpoints];
179 
180  HIDReportParser *pRptParser;
181 
182  uint8_t bConfNum; // configuration number
183  uint8_t bIfaceNum; // Interface Number
184  uint8_t bNumIface; // number of interfaces in the configuration
185  uint8_t bNumEP; // total number of EP in the configuration
186  uint32_t qNextPollTime; // next poll time
187  bool bPollEnable; // poll enable flag
188 
189  void Initialize();
190 
191  virtual HIDReportParser* GetReportParser(uint8_t id) {
192  return pRptParser;
193  };
194 
195 public:
196  HIDBoot(USB *p);
197 
198  virtual bool SetReportParser(uint8_t id, HIDReportParser *prs) {
199  pRptParser = prs;
200  return true;
201  };
202 
203  // USBDeviceConfig implementation
204  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
205  virtual uint8_t Release();
206  virtual uint8_t Poll();
207 
208  virtual uint8_t GetAddress() {
209  return bAddress;
210  };
211 
212  // UsbConfigXtracter implementation
213  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
214 };
215 
216 template <const uint8_t BOOT_PROTOCOL>
218 HID(p),
219 qNextPollTime(0),
220 bPollEnable(false),
221 pRptParser(NULL) {
222  Initialize();
223 
224  if(pUsb)
225  pUsb->RegisterDeviceClass(this);
226 }
227 
228 template <const uint8_t BOOT_PROTOCOL>
230  for(uint8_t i = 0; i < totalEndpoints; i++) {
231  epInfo[i].epAddr = 0;
232  epInfo[i].maxPktSize = (i) ? 0 : 8;
233  epInfo[i].epAttribs = 0;
234  epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
235  }
236  bNumEP = 1;
237  bNumIface = 0;
238  bConfNum = 0;
239 }
240 
241 template <const uint8_t BOOT_PROTOCOL>
242 uint8_t HIDBoot<BOOT_PROTOCOL>::Init(uint8_t parent, uint8_t port, bool lowspeed) {
243  const uint8_t constBufSize = sizeof(USB_DEVICE_DESCRIPTOR);
244 
245  uint8_t buf[constBufSize];
246  uint8_t rcode;
247  UsbDevice *p = NULL;
248  EpInfo *oldep_ptr = NULL;
249  uint8_t len = 0;
250  uint16_t cd_len = 0;
251 
252  uint8_t num_of_conf; // number of configurations
253  uint8_t num_of_intf; // number of interfaces
254 
255  AddressPool &addrPool = pUsb->GetAddressPool();
256 
257  USBTRACE("BM Init\r\n");
258 
259  if(bAddress)
261 
262  // Get pointer to pseudo device with address 0 assigned
263  p = addrPool.GetUsbDevicePtr(0);
264 
265  if(!p)
267 
268  if(!p->epinfo) {
269  USBTRACE("epinfo\r\n");
271  }
272 
273  // Save old pointer to EP_RECORD of address 0
274  oldep_ptr = p->epinfo;
275 
276  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
277  p->epinfo = epInfo;
278 
279  p->lowspeed = lowspeed;
280 
281  // Get device descriptor
282  rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*) buf);
283 
284  if(!rcode)
285  len = (buf[0] > constBufSize) ? constBufSize : buf[0];
286 
287  if(rcode) {
288  // Restore p->epinfo
289  p->epinfo = oldep_ptr;
290 
291  goto FailGetDevDescr;
292  }
293 
294  // Restore p->epinfo
295  p->epinfo = oldep_ptr;
296 
297  // Allocate new address according to device class
298  bAddress = addrPool.AllocAddress(parent, false, port);
299 
300  if(!bAddress)
302 
303  // Extract Max Packet Size from the device descriptor
304  epInfo[0].maxPktSize = (uint8_t) ((USB_DEVICE_DESCRIPTOR*) buf)->bMaxPacketSize0;
305 
306  // Assign new address to the device
307  rcode = pUsb->setAddr(0, 0, bAddress);
308 
309  if(rcode) {
310  p->lowspeed = false;
311  addrPool.FreeAddress(bAddress);
312  bAddress = 0;
313  USBTRACE2("setAddr:", rcode);
314  return rcode;
315  }
316 
317  USBTRACE2("Addr:", bAddress);
318 
319  p->lowspeed = false;
320 
321  p = addrPool.GetUsbDevicePtr(bAddress);
322 
323  if(!p)
325 
326  p->lowspeed = lowspeed;
327 
328  if(len)
329  rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*) buf);
330 
331  if(rcode)
332  goto FailGetDevDescr;
333 
334  num_of_conf = ((USB_DEVICE_DESCRIPTOR*) buf)->bNumConfigurations;
335 
336  // Assign epInfo to epinfo pointer
337  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
338 
339  if(rcode)
340  goto FailSetDevTblEntry;
341 
342  //USBTRACE2("NC:", num_of_conf);
343 
344  for(uint8_t i = 0; i < num_of_conf; i++) {
348  BOOT_PROTOCOL,
349  CP_MASK_COMPARE_ALL> confDescrParser(this);
350 
351  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
352 
353  if(bNumEP > 1)
354  break;
355  } // for
356 
357  if(bNumEP < 2)
359 
360  //USBTRACE2("\r\nbAddr:", bAddress);
361  //USBTRACE2("\r\nbNumEP:", bNumEP);
362 
363  // Assign epInfo to epinfo pointer
364  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
365 
366  //USBTRACE2("\r\nCnf:", bConfNum);
367 
368  // Set Configuration Value
369  rcode = pUsb->setConf(bAddress, 0, bConfNum);
370 
371  if(rcode)
372  goto FailSetConfDescr;
373 
374  //USBTRACE2("\r\nIf:", bIfaceNum);
375 
376  rcode = SetProtocol(bIfaceNum, HID_BOOT_PROTOCOL);
377 
378  if(rcode)
379  goto FailSetProtocol;
380 
381  if(BOOT_PROTOCOL == 1) {
382  rcode = SetIdle(bIfaceNum, 0, 0);
383 
384  if(rcode)
385  goto FailSetIdle;
386  }
387  USBTRACE("BM configured\r\n");
388 
389  bPollEnable = true;
390  return 0;
391 
392 FailGetDevDescr:
394  goto Fail;
395 
396 FailSetDevTblEntry:
398  goto Fail;
399 
400 FailGetConfDescr:
402  goto Fail;
403 
404 FailSetConfDescr:
406  goto Fail;
407 
408 
409 FailSetProtocol:
410  USBTRACE("SetProto:");
411  goto Fail;
412 
413 FailSetIdle:
414  USBTRACE("SetIdle:");
415  goto Fail;
416 
417 Fail:
418  NotifyFail(rcode);
419  Release();
420  return rcode;
421 }
422 
423 template <const uint8_t BOOT_PROTOCOL>
424 void HIDBoot<BOOT_PROTOCOL>::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
425  // If the first configuration satisfies, the others are not concidered.
426  if(bNumEP > 1 && conf != bConfNum)
427  return;
428 
429  bConfNum = conf;
430  bIfaceNum = iface;
431 
432  uint8_t index;
433 
434  if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) {
435  index = epInterruptInIndex;
436 
437  // Fill in the endpoint info structure
438  epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
439  epInfo[index].maxPktSize = (uint8_t) pep->wMaxPacketSize;
440  epInfo[index].epAttribs = 0;
441 
442  bNumEP++;
443  }
444 }
445 
446 template <const uint8_t BOOT_PROTOCOL>
448  pUsb->GetAddressPool().FreeAddress(bAddress);
449 
450  bConfNum = 0;
451  bIfaceNum = 0;
452  bNumEP = 1;
453  bAddress = 0;
454  qNextPollTime = 0;
455  bPollEnable = false;
456  return 0;
457 }
458 
459 template <const uint8_t BOOT_PROTOCOL>
461  uint8_t rcode = 0;
462 
463  if(!bPollEnable)
464  return 0;
465 
466  if(qNextPollTime <= millis()) {
467  qNextPollTime = millis() + 10;
468 
469  const uint8_t const_buff_len = 16;
470  uint8_t buf[const_buff_len];
471 
472  uint16_t read = (uint16_t) epInfo[epInterruptInIndex].maxPktSize;
473 
474  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex].epAddr, &read, buf);
475 
476  if(rcode) {
477  if(rcode != hrNAK)
478  USBTRACE2("Poll:", rcode);
479  return rcode;
480  }
481  //for (uint8_t i=0; i<read; i++)
482  // PrintHex<uint8_t>(buf[i]);
483  //if (read)
484  // Serial.println("");
485 
486  if(pRptParser)
487  pRptParser->Parse((HID*)this, 0, (uint8_t) read, buf);
488  }
489  return rcode;
490 }
491 
492 
493 #endif // __HIDBOOTMOUSE_H__