USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
317  index = (pep->bEndpointAddress & 0x80) == 0x80 ? epInterruptInIndex : epInterruptOutIndex;
318 
319  if(!SelectInterface(iface, proto))
320  index = 0;
321 
322  if(index) {
323  // Fill in the endpoint info structure
324  epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F);
325  epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize;
326  epInfo[bNumEP].bmSndToggle = 0;
327  epInfo[bNumEP].bmRcvToggle = 0;
328  epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT;
329 
330  // Fill in the endpoint index list
331  piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F);
332 
333  if(pollInterval < pep->bInterval) // Set the polling interval as the largest polling interval obtained from endpoints
334  pollInterval = pep->bInterval;
335 
336  bNumEP++;
337  }
338 }
339 
342 
343  bNumEP = 1;
344  bAddress = 0;
345  qNextPollTime = 0;
346  bPollEnable = false;
347  return 0;
348 }
349 
350 void HIDComposite::ZeroMemory(uint8_t len, uint8_t *buf) {
351  for(uint8_t i = 0; i < len; i++)
352  buf[i] = 0;
353 }
354 
356  uint8_t rcode = 0;
357 
358  if(!bPollEnable)
359  return 0;
360 
361  if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) {
362  qNextPollTime = (uint32_t)millis() + pollInterval;
363 
364  uint8_t buf[constBuffLen];
365 
366  for(uint8_t i = 0; i < bNumIface; i++) {
367  uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex];
368 
369  if (index == 0)
370  continue;
371 
372  uint16_t read = (uint16_t)epInfo[index].maxPktSize;
373 
374  ZeroMemory(constBuffLen, buf);
375 
376  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf);
377 
378  if(rcode) {
379  if(rcode != hrNAK)
380  USBTRACE3("(hidcomposite.h) Poll:", rcode, 0x81);
381  continue;
382  }
383 
384  if(read == 0)
385  continue;
386 
387  if(read > constBuffLen)
388  read = constBuffLen;
389 
390 #if 0
391  Notify(PSTR("\r\nBuf: "), 0x80);
392 
393  for(uint8_t i = 0; i < read; i++) {
394  D_PrintHex<uint8_t > (buf[i], 0x80);
395  Notify(PSTR(" "), 0x80);
396  }
397 
398  Notify(PSTR("\r\n"), 0x80);
399 #endif
400  ParseHIDData(this, epInfo[index].epAddr, bHasReportId, (uint8_t)read, buf);
401 
402  HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0));
403 
404  if(prs)
405  prs->Parse(this, bHasReportId, (uint8_t)read, buf);
406  }
407 
408  }
409  return rcode;
410 }
411 
412 // Send a report to interrupt out endpoint. This is NOT SetReport() request!
413 uint8_t HIDComposite::SndRpt(uint16_t nbytes, uint8_t *dataptr) {
414  return pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, nbytes, dataptr);
415 }
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
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:96
#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 uint8_t OnInitSuccessful()
Definition: hidcomposite.h:71
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:823
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 USB_TRANSFER_TYPE_INTERRUPT
Definition: usb_ch9.h:93
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:71
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:72
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: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
Definition: address.h:39
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:300
#define hrNAK
Definition: max3421e.h:218
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
uint8_t bAddress
Definition: usbhid.h:146
uint16_t wMaxPacketSize
Definition: usb_ch9.h:153
#define bmUSB_TRANSFER_TYPE
Definition: usb_ch9.h:94
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: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:98
#define PSTR(str)
uint16_t VID
Definition: hidcomposite.h:66
#define USB_NAK_NOWAIT
Definition: address.h:36
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:95
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:90
static const uint8_t totalEndpoints
Definition: usbhid.h:154
uint16_t idProduct
Definition: usb_ch9.h:114
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:213
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:93
uint8_t Release()
uint8_t SndRpt(uint16_t nbytes, uint8_t *dataptr)
uint8_t maxPktSize
Definition: address.h:41
AddressPool & GetAddressPool()
Definition: UsbCore.h:226
Definition: UsbCore.h:210
bool SetReportParser(uint8_t id, HIDReportParser *prs)
HIDReportParser * GetReportParser(uint8_t id)
#define USBTRACE3(s, r, l)
Definition: macros.h:85
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:230
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)=0
#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