USB Host Shield 2.0
hiduniversal.cpp
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 
18 #include "hiduniversal.h"
19 
21 USBHID(p),
22 qNextPollTime(0),
23 pollInterval(0),
24 bPollEnable(false),
25 bHasReportId(false) {
26  Initialize();
27 
28  if(pUsb)
30 }
31 
32 uint16_t HIDUniversal::GetHidClassDescrLen(uint8_t type, uint8_t num) {
33  for(uint8_t i = 0, n = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) {
34  if(descrInfo[i].bDescrType == type) {
35  if(n == num)
36  return descrInfo[i].wDescriptorLength;
37  n++;
38  }
39  }
40  return 0;
41 }
42 
43 void HIDUniversal::Initialize() {
44  for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) {
45  rptParsers[i].rptId = 0;
46  rptParsers[i].rptParser = NULL;
47  }
48  for(uint8_t i = 0; i < HID_MAX_HID_CLASS_DESCRIPTORS; i++) {
49  descrInfo[i].bDescrType = 0;
50  descrInfo[i].wDescriptorLength = 0;
51  }
52  for(uint8_t i = 0; i < maxHidInterfaces; i++) {
53  hidInterfaces[i].bmInterface = 0;
54  hidInterfaces[i].bmProtocol = 0;
55 
56  for(uint8_t j = 0; j < maxEpPerInterface; j++)
57  hidInterfaces[i].epIndex[j] = 0;
58  }
59  for(uint8_t i = 0; i < totalEndpoints; i++) {
60  epInfo[i].epAddr = 0;
61  epInfo[i].maxPktSize = (i) ? 0 : 8;
62  epInfo[i].bmSndToggle = 0;
63  epInfo[i].bmRcvToggle = 0;
65  }
66  bNumEP = 1;
67  bNumIface = 0;
68  bConfNum = 0;
69  pollInterval = 0;
70 
71  ZeroMemory(constBuffLen, prevBuf);
72 }
73 
75  for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) {
76  if(rptParsers[i].rptId == 0 && rptParsers[i].rptParser == NULL) {
77  rptParsers[i].rptId = id;
78  rptParsers[i].rptParser = prs;
79  return true;
80  }
81  }
82  return false;
83 }
84 
86  if(!bHasReportId)
87  return ((rptParsers[0].rptParser) ? rptParsers[0].rptParser : NULL);
88 
89  for(uint8_t i = 0; i < MAX_REPORT_PARSERS; i++) {
90  if(rptParsers[i].rptId == id)
91  return rptParsers[i].rptParser;
92  }
93  return NULL;
94 }
95 
96 uint8_t HIDUniversal::Init(uint8_t parent, uint8_t port, bool lowspeed) {
97  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
98 
99  uint8_t buf[constBufSize];
100  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
101  uint8_t rcode;
102  UsbDevice *p = NULL;
103  EpInfo *oldep_ptr = NULL;
104  uint8_t len = 0;
105 
106  uint8_t num_of_conf; // number of configurations
107  //uint8_t num_of_intf; // number of interfaces
108 
109  AddressPool &addrPool = pUsb->GetAddressPool();
110 
111  USBTRACE("HU Init\r\n");
112 
113  if(bAddress)
115 
116  // Get pointer to pseudo device with address 0 assigned
117  p = addrPool.GetUsbDevicePtr(0);
118 
119  if(!p)
121 
122  if(!p->epinfo) {
123  USBTRACE("epinfo\r\n");
125  }
126 
127  // Save old pointer to EP_RECORD of address 0
128  oldep_ptr = p->epinfo;
129 
130  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
131  p->epinfo = epInfo;
132 
133  p->lowspeed = lowspeed;
134 
135  // Get device descriptor
136  rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
137 
138  if(!rcode)
139  len = (buf[0] > constBufSize) ? constBufSize : buf[0];
140 
141  if(rcode) {
142  // Restore p->epinfo
143  p->epinfo = oldep_ptr;
144 
145  goto FailGetDevDescr;
146  }
147 
148  // Restore p->epinfo
149  p->epinfo = oldep_ptr;
150 
151  // Allocate new address according to device class
152  bAddress = addrPool.AllocAddress(parent, false, port);
153 
154  if(!bAddress)
156 
157  // Extract Max Packet Size from the device descriptor
159 
160  // Assign new address to the device
161  rcode = pUsb->setAddr(0, 0, bAddress);
162 
163  if(rcode) {
164  p->lowspeed = false;
165  addrPool.FreeAddress(bAddress);
166  bAddress = 0;
167  USBTRACE2("setAddr:", rcode);
168  return rcode;
169  }
170 
171  //delay(2); //per USB 2.0 sect.9.2.6.3
172 
173  USBTRACE2("Addr:", bAddress);
174 
175  p->lowspeed = false;
176 
177  p = addrPool.GetUsbDevicePtr(bAddress);
178 
179  if(!p)
181 
182  p->lowspeed = lowspeed;
183 
184  if(len)
185  rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
186 
187  if(rcode)
188  goto FailGetDevDescr;
189 
190  VID = udd->idVendor; // Can be used by classes that inherits this class to check the VID and PID of the connected device
191  PID = udd->idProduct;
192 
193  num_of_conf = udd->bNumConfigurations;
194 
195  // Assign epInfo to epinfo pointer
196  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
197 
198  if(rcode)
199  goto FailSetDevTblEntry;
200 
201  USBTRACE2("NC:", num_of_conf);
202 
203  for(uint8_t i = 0; i < num_of_conf; i++) {
204  //HexDumper<USBReadParser, uint16_t, uint16_t> HexDump;
206  CP_MASK_COMPARE_CLASS> confDescrParser(this);
207 
208  //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
209  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
210 
211  if(rcode)
212  goto FailGetConfDescr;
213 
214  if(bNumEP > 1)
215  break;
216  } // for
217 
218  if(bNumEP < 2)
220 
221  // Assign epInfo to epinfo pointer
222  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
223 
224  USBTRACE2("Cnf:", bConfNum);
225 
226  // Set Configuration Value
227  rcode = pUsb->setConf(bAddress, 0, bConfNum);
228 
229  if(rcode)
230  goto FailSetConfDescr;
231 
232  for(uint8_t i = 0; i < bNumIface; i++) {
233  if(hidInterfaces[i].epIndex[epInterruptInIndex] == 0)
234  continue;
235 
236  rcode = SetIdle(hidInterfaces[i].bmInterface, 0, 0);
237 
238  if(rcode && rcode != hrSTALL)
239  goto FailSetIdle;
240  }
241 
242  USBTRACE("HU configured\r\n");
243 
245 
246  bPollEnable = true;
247  return 0;
248 
249 FailGetDevDescr:
250 #ifdef DEBUG_USB_HOST
252  goto Fail;
253 #endif
254 
255 FailSetDevTblEntry:
256 #ifdef DEBUG_USB_HOST
258  goto Fail;
259 #endif
260 
261 FailGetConfDescr:
262 #ifdef DEBUG_USB_HOST
264  goto Fail;
265 #endif
266 
267 FailSetConfDescr:
268 #ifdef DEBUG_USB_HOST
270  goto Fail;
271 #endif
272 
273 
274 FailSetIdle:
275 #ifdef DEBUG_USB_HOST
276  USBTRACE("SetIdle:");
277 #endif
278 
279 #ifdef DEBUG_USB_HOST
280 Fail:
281  NotifyFail(rcode);
282 #endif
283  Release();
284  return rcode;
285 }
286 
287 HIDUniversal::HIDInterface* HIDUniversal::FindInterface(uint8_t iface, uint8_t alt, uint8_t proto) {
288  for(uint8_t i = 0; i < bNumIface && i < maxHidInterfaces; i++)
289  if(hidInterfaces[i].bmInterface == iface && hidInterfaces[i].bmAltSet == alt
290  && hidInterfaces[i].bmProtocol == proto)
291  return hidInterfaces + i;
292  return NULL;
293 }
294 
295 void HIDUniversal::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep) {
296  // If the first configuration satisfies, the others are not concidered.
297  if(bNumEP > 1 && conf != bConfNum)
298  return;
299 
300  //ErrorMessage<uint8_t>(PSTR("\r\nConf.Val"), conf);
301  //ErrorMessage<uint8_t>(PSTR("Iface Num"), iface);
302  //ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt);
303 
304  bConfNum = conf;
305 
306  uint8_t index = 0;
307  HIDInterface *piface = FindInterface(iface, alt, proto);
308 
309  // Fill in interface structure in case of new interface
310  if(!piface) {
311  piface = hidInterfaces + bNumIface;
312  piface->bmInterface = iface;
313  piface->bmAltSet = alt;
314  piface->bmProtocol = proto;
315  bNumIface++;
316  }
317 
319  index = (pep->bEndpointAddress & 0x80) == 0x80 ? epInterruptInIndex : epInterruptOutIndex;
320 
321  if(index) {
322  // Fill in the endpoint info structure
323  epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
324  epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
325  epInfo[bNumEP].bmSndToggle = 0;
326  epInfo[bNumEP].bmRcvToggle = 0;
327  epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
328 
329  // Fill in the endpoint index list
330  piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F);
331 
332  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
333  pollInterval = pep->bInterval;
334 
335  bNumEP++;
336  }
337  //PrintEndpointDescriptor(pep);
338 }
339 
342 
343  bNumEP = 1;
344  bAddress = 0;
345  qNextPollTime = 0;
346  bPollEnable = false;
347  return 0;
348 }
349 
350 bool HIDUniversal::BuffersIdentical(uint8_t len, uint8_t *buf1, uint8_t *buf2) {
351  for(uint8_t i = 0; i < len; i++)
352  if(buf1[i] != buf2[i])
353  return false;
354  return true;
355 }
356 
357 void HIDUniversal::ZeroMemory(uint8_t len, uint8_t *buf) {
358  for(uint8_t i = 0; i < len; i++)
359  buf[i] = 0;
360 }
361 
362 void HIDUniversal::SaveBuffer(uint8_t len, uint8_t *src, uint8_t *dest) {
363  for(uint8_t i = 0; i < len; i++)
364  dest[i] = src[i];
365 }
366 
368  uint8_t rcode = 0;
369 
370  if(!bPollEnable)
371  return 0;
372 
373  if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) {
374  qNextPollTime = (uint32_t)millis() + pollInterval;
375 
376  uint8_t buf[constBuffLen];
377 
378  for(uint8_t i = 0; i < bNumIface; i++) {
379  uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex];
380  uint16_t read = (uint16_t)epInfo[index].maxPktSize;
381 
382  ZeroMemory(constBuffLen, buf);
383 
384  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf);
385 
386  if(rcode) {
387  if(rcode != hrNAK)
388  USBTRACE3("(hiduniversal.h) Poll:", rcode, 0x81);
389  return rcode;
390  }
391 
392  if(read > constBuffLen)
393  read = constBuffLen;
394 
395  bool identical = BuffersIdentical(read, buf, prevBuf);
396 
397  SaveBuffer(read, buf, prevBuf);
398 
399  if(identical)
400  return 0;
401 #if 0
402  Notify(PSTR("\r\nBuf: "), 0x80);
403 
404  for(uint8_t i = 0; i < read; i++) {
405  D_PrintHex<uint8_t > (buf[i], 0x80);
406  Notify(PSTR(" "), 0x80);
407  }
408 
409  Notify(PSTR("\r\n"), 0x80);
410 #endif
411  ParseHIDData(this, bHasReportId, (uint8_t)read, buf);
412 
413  HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0));
414 
415  if(prs)
416  prs->Parse(this, bHasReportId, (uint8_t)read, buf);
417  }
418  }
419  return rcode;
420 }
421 
422 // Send a report to interrupt out endpoint. This is NOT SetReport() request!
423 uint8_t HIDUniversal::SndRpt(uint16_t nbytes, uint8_t *dataptr) {
424  return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr);
425 }
uint16_t PID
Definition: hiduniversal.h:69
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:784
uint8_t bmRcvToggle
Definition: address.h:48
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
bool bHasReportId
Definition: hiduniversal.h:67
Definition: usbhid.h:143
USB * pUsb
Definition: usbhid.h:145
EpInfo * epinfo
Definition: address.h:83
bool lowspeed
Definition: address.h:86
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:94
#define hrSTALL
Definition: max3421e.h:219
uint8_t bmNakPower
Definition: address.h:49
#define HID_MAX_HID_CLASS_DESCRIPTORS
Definition: usbhid.h:24
#define MAX_REPORT_PARSERS
Definition: usbhid.h:23
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:112
#define NotifyFail(...)
Definition: message.h:62
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: hiduniversal.h:78
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:823
#define USB_TRANSFER_TYPE_INTERRUPT
Definition: usb_ch9.h:93
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hiduniversal.h:65
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:71
uint8_t Release()
#define CP_MASK_COMPARE_CLASS
uint16_t VID
Definition: hiduniversal.h:69
virtual uint8_t OnInitSuccessful()
Definition: hiduniversal.h:74
#define USB_CLASS_HID
Definition: UsbCore.h:70
virtual void FreeAddress(uint8_t addr)=0
uint8_t Poll()
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
uint8_t SetIdle(uint8_t iface, uint8_t reportID, uint8_t duration)
Definition: usbhid.cpp:62
#define Notify(...)
Definition: message.h:51
#define USBTRACE2(s, r)
Definition: macros.h:84
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:814
#define NotifyFailGetConfDescr(...)
Definition: message.h:59
uint8_t epAddr
Definition: address.h:40
#define USB_NAK_MAX_POWER
Definition: address.h:34
HIDReportParser * GetReportParser(uint8_t id)
Definition: address.h:39
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:300
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
#define hrNAK
Definition: max3421e.h:218
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
HIDUniversal(USB *p)
uint8_t bAddress
Definition: usbhid.h:146
uint16_t wMaxPacketSize
Definition: usb_ch9.h:153
#define bmUSB_TRANSFER_TYPE
Definition: usb_ch9.h:94
static const uint8_t maxEpPerInterface
Definition: usbhid.h:153
static const uint8_t epInterruptInIndex
Definition: usbhid.h:149
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
uint8_t bEndpointAddress
Definition: usb_ch9.h:151
static const uint8_t maxHidInterfaces
Definition: usbhid.h:152
uint8_t bmSndToggle
Definition: address.h:47
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:96
#define PSTR(str)
#define USB_NAK_NOWAIT
Definition: address.h:36
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:93
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:88
static const uint8_t totalEndpoints
Definition: usbhid.h:154
uint16_t idProduct
Definition: usb_ch9.h:114
uint8_t SndRpt(uint16_t nbytes, uint8_t *dataptr)
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition: Usb.cpp:213
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:91
uint8_t maxPktSize
Definition: address.h:41
AddressPool & GetAddressPool()
Definition: UsbCore.h:224
Definition: UsbCore.h:208
#define USBTRACE3(s, r, l)
Definition: macros.h:85
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:228
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)=0
EpInfo epInfo[totalEndpoints]
Definition: hiduniversal.h:64
bool SetReportParser(uint8_t id, HIDReportParser *prs)
#define USBTRACE(s)
Definition: macros.h:82
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:779
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:58