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 
43 void HIDComposite::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 
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
220  rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
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  piface = hidInterfaces + bNumIface;
310  piface->bmInterface = iface;
311  piface->bmAltSet = alt;
312  piface->bmProtocol = proto;
313  bNumIface++;
314  }
315 
316  if((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80)
317  index = epInterruptInIndex;
318  else
319  index = epInterruptOutIndex;
320 
321  if(!SelectInterface(iface, proto))
322  index = 0;
323 
324  if(index) {
325  // Fill in the endpoint info structure
326  epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
327  epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
328  epInfo[bNumEP].bmSndToggle = 0;
329  epInfo[bNumEP].bmRcvToggle = 0;
330  epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
331 
332  // Fill in the endpoint index list
333  piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F);
334 
335  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
336  pollInterval = pep->bInterval;
337 
338  bNumEP++;
339  }
340 }
341 
344 
345  bNumEP = 1;
346  bAddress = 0;
347  qNextPollTime = 0;
348  bPollEnable = false;
349  return 0;
350 }
351 
352 void HIDComposite::ZeroMemory(uint8_t len, uint8_t *buf) {
353  for(uint8_t i = 0; i < len; i++)
354  buf[i] = 0;
355 }
356 
358  uint8_t rcode = 0;
359 
360  if(!bPollEnable)
361  return 0;
362 
363  if((long)(millis() - qNextPollTime) >= 0L) {
364  qNextPollTime = millis() + pollInterval;
365 
366  uint8_t buf[constBuffLen];
367 
368  for(uint8_t i = 0; i < bNumIface; i++) {
369  uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex];
370 
371  if (index == 0)
372  continue;
373 
374  uint16_t read = (uint16_t)epInfo[index].maxPktSize;
375 
376  ZeroMemory(constBuffLen, buf);
377 
378  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf);
379 
380  if(rcode) {
381  if(rcode != hrNAK)
382  USBTRACE3("(hidcomposite.h) Poll:", rcode, 0x81);
383  continue;
384  }
385 
386  if(read == 0)
387  continue;
388 
389  if(read > constBuffLen)
390  read = constBuffLen;
391 
392 #if 0
393  Notify(PSTR("\r\nBuf: "), 0x80);
394 
395  for(uint8_t i = 0; i < read; i++) {
396  D_PrintHex<uint8_t > (buf[i], 0x80);
397  Notify(PSTR(" "), 0x80);
398  }
399 
400  Notify(PSTR("\r\n"), 0x80);
401 #endif
402  ParseHIDData(this, epInfo[index].epAddr, bHasReportId, (uint8_t)read, buf);
403 
404  HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0));
405 
406  if(prs)
407  prs->Parse(this, bHasReportId, (uint8_t)read, buf);
408  }
409 
410  }
411  return rcode;
412 }
413 
414 // Send a report to interrupt out endpoint. This is NOT SetReport() request!
415 uint8_t HIDComposite::SndRpt(uint16_t nbytes, uint8_t *dataptr) {
416  return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr);
417 }
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
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 uint8_t OnInitSuccessful()
Definition: hidcomposite.h:71
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:810
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hidcomposite.h:62
virtual void ParseHIDData(USBHID *hid, uint8_t ep, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: hidcomposite.h:75
bool bHasReportId
Definition: hidcomposite.h:64
#define NotifyFailGetDevDescr(...)
Definition: message.h:50
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:61
#define CP_MASK_COMPARE_CLASS
#define USB_CLASS_HID
Definition: UsbCore.h:59
virtual void FreeAddress(uint8_t addr)=0
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
Definition: address.h:32
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:293
#define hrNAK
Definition: max3421e.h:211
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
uint8_t bAddress
Definition: usbhid.h:146
uint16_t wMaxPacketSize
Definition: usb_ch9.h:146
uint8_t Poll()
static const uint8_t maxEpPerInterface
Definition: usbhid.h:153
uint16_t PID
Definition: hidcomposite.h:66
virtual bool SelectInterface(uint8_t iface, uint8_t proto)=0
HIDComposite(USB *p)
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)
uint16_t VID
Definition: hidcomposite.h:66
#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
void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep)
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 Release()
uint8_t SndRpt(uint16_t nbytes, uint8_t *dataptr)
uint8_t maxPktSize
Definition: address.h:34
AddressPool & GetAddressPool()
Definition: UsbCore.h:213
Definition: UsbCore.h:197
bool SetReportParser(uint8_t id, HIDReportParser *prs)
HIDReportParser * GetReportParser(uint8_t id)
#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
#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