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 
318  if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80)
319  index = epInterruptInIndex;
320  else
321  index = epInterruptOutIndex;
322 
323  if(index) {
324  // Fill in the endpoint info structure
325  epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
326  epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
327  epInfo[bNumEP].bmSndToggle = 0;
328  epInfo[bNumEP].bmRcvToggle = 0;
329  epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
330 
331  // Fill in the endpoint index list
332  piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F);
333 
334  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
335  pollInterval = pep->bInterval;
336 
337  bNumEP++;
338  }
339  //PrintEndpointDescriptor(pep);
340 }
341 
344 
345  bNumEP = 1;
346  bAddress = 0;
347  qNextPollTime = 0;
348  bPollEnable = false;
349  return 0;
350 }
351 
352 bool HIDUniversal::BuffersIdentical(uint8_t len, uint8_t *buf1, uint8_t *buf2) {
353  for(uint8_t i = 0; i < len; i++)
354  if(buf1[i] != buf2[i])
355  return false;
356  return true;
357 }
358 
359 void HIDUniversal::ZeroMemory(uint8_t len, uint8_t *buf) {
360  for(uint8_t i = 0; i < len; i++)
361  buf[i] = 0;
362 }
363 
364 void HIDUniversal::SaveBuffer(uint8_t len, uint8_t *src, uint8_t *dest) {
365  for(uint8_t i = 0; i < len; i++)
366  dest[i] = src[i];
367 }
368 
370  uint8_t rcode = 0;
371 
372  if(!bPollEnable)
373  return 0;
374 
375  if((long)(millis() - qNextPollTime) >= 0L) {
376  qNextPollTime = millis() + pollInterval;
377 
378  uint8_t buf[constBuffLen];
379 
380  for(uint8_t i = 0; i < bNumIface; i++) {
381  uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex];
382  uint16_t read = (uint16_t)epInfo[index].maxPktSize;
383 
384  ZeroMemory(constBuffLen, buf);
385 
386  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf);
387 
388  if(rcode) {
389  if(rcode != hrNAK)
390  USBTRACE3("(hiduniversal.h) Poll:", rcode, 0x81);
391  return rcode;
392  }
393 
394  if(read > constBuffLen)
395  read = constBuffLen;
396 
397  bool identical = BuffersIdentical(read, buf, prevBuf);
398 
399  SaveBuffer(read, buf, prevBuf);
400 
401  if(identical)
402  return 0;
403 #if 0
404  Notify(PSTR("\r\nBuf: "), 0x80);
405 
406  for(uint8_t i = 0; i < read; i++) {
407  D_PrintHex<uint8_t > (buf[i], 0x80);
408  Notify(PSTR(" "), 0x80);
409  }
410 
411  Notify(PSTR("\r\n"), 0x80);
412 #endif
413  ParseHIDData(this, bHasReportId, (uint8_t)read, buf);
414 
415  HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0));
416 
417  if(prs)
418  prs->Parse(this, bHasReportId, (uint8_t)read, buf);
419  }
420  }
421  return rcode;
422 }
423 
424 // Send a report to interrupt out endpoint. This is NOT SetReport() request!
425 uint8_t HIDUniversal::SndRpt(uint16_t nbytes, uint8_t *dataptr) {
426  return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr);
427 }
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:771
uint8_t bmRcvToggle
Definition: address.h:41
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:76
bool lowspeed
Definition: address.h:79
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:83
#define hrSTALL
Definition: max3421e.h:212
uint8_t bmNakPower
Definition: address.h:42
#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:105
#define NotifyFail(...)
Definition: message.h:55
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:810
#define NotifyFailGetDevDescr(...)
Definition: message.h:50
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hiduniversal.h:65
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
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:59
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:44
#define USBTRACE2(s, r)
Definition: macros.h:77
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:801
#define NotifyFailGetConfDescr(...)
Definition: message.h:52
uint8_t epAddr
Definition: address.h:33
#define USB_NAK_MAX_POWER
Definition: address.h:27
HIDReportParser * GetReportParser(uint8_t id)
Definition: address.h:32
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:293
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
#define hrNAK
Definition: max3421e.h:211
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:146
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:144
static const uint8_t maxHidInterfaces
Definition: usbhid.h:152
uint8_t bmSndToggle
Definition: address.h:40
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:85
#define PSTR(str)
#define USB_NAK_NOWAIT
Definition: address.h:29
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:82
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:77
static const uint8_t totalEndpoints
Definition: usbhid.h:154
uint16_t idProduct
Definition: usb_ch9.h:107
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:206
uint8_t bNumConfigurations
Definition: usb_ch9.h:112
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:80
uint8_t maxPktSize
Definition: address.h:34
AddressPool & GetAddressPool()
Definition: UsbCore.h:213
Definition: UsbCore.h:197
#define USBTRACE3(s, r, l)
Definition: macros.h:78
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:217
#define NotifyFailSetConfDescr(...)
Definition: message.h:53
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:75
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:766
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:51