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