USB Host Shield 2.0
Loading...
Searching...
No Matches
hidcomposite.cpp
Go to the documentation of this file.
1/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3This software may be distributed and modified under the terms of the GNU
4General Public License version 2 (GPL2) as published by the Free Software
5Foundation and appearing in the file GPL2.TXT included in the packaging of
6this file. Please note that GPL2 Section 2[b] requires that all works based
7on this software must also be made publicly available under the terms of
8the GPL2 ("Copyleft").
9
10Contact information
11-------------------
12
13Circuits At Home, LTD
14Web : http://www.circuitsathome.com
15e-mail : support@circuitsathome.com
16 */
17
18#include "hidcomposite.h"
19
21USBHID(p),
22qNextPollTime(0),
23pollInterval(0),
24bPollEnable(false),
25bHasReportId(false) {
26 Initialize();
27
28 if(pUsb)
30}
31
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)
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 }
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
96
98 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
100 UsbDevice *p = 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
156 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
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)
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
195
196 if(rcode)
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;
205
206 //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
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
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
252#ifdef DEBUG_USB_HOST
254 goto Fail;
255#endif
256
258#ifdef DEBUG_USB_HOST
260 goto Fail;
261#endif
262
264#ifdef DEBUG_USB_HOST
266 goto Fail;
267#endif
268
270#ifdef DEBUG_USB_HOST
272 goto Fail;
273#endif
274
275
277#ifdef DEBUG_USB_HOST
278 USBTRACE("SetIdle:");
279#endif
280
281#ifdef DEBUG_USB_HOST
282Fail:
284#endif
285 Release();
286 return rcode;
287}
288
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
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;
306
307 // Fill in interface structure in case of new interface
308 if(!piface) {
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 }
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
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
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) {
377
379
380 for(uint8_t i = 0; i < bNumIface; i++) {
382
383 if (index == 0)
384 continue;
385
386 uint16_t read = (uint16_t)epInfo[index].maxPktSize;
387
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
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!
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition UsbCore.h:103
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition UsbCore.h:108
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition UsbCore.h:100
#define USB_CLASS_HID
Definition UsbCore.h:82
#define USB_ERROR_EPINFO_IS_NULL
Definition UsbCore.h:106
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition UsbCore.h:105
#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 UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset)
virtual uint8_t OnInitSuccessful()
uint16_t PID
uint32_t qNextPollTime
HIDReportParser * GetReportParser(uint8_t id)
uint8_t Release()
static const uint16_t constBuffLen
HIDInterface hidInterfaces[maxHidInterfaces]
EpInfo epInfo[totalEndpoints]
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]
uint8_t bConfNum
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)
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)
uint8_t bNumIface
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
uint16_t VID
virtual bool SelectInterface(uint8_t iface, uint8_t proto)=0
uint8_t pollInterval
uint8_t bNumEP
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:220
AddressPool & GetAddressPool()
Definition UsbCore.h:236
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:850
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition Usb.cpp:841
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition UsbCore.h:240
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition Usb.cpp:806
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
#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:56
#define NotifyFailSetDevTblEntry(...)
Definition message.h:55
#define NotifyFailGetDevDescr(...)
Definition message.h:54
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]
#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)