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