?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /******************************************************************************
2  
3 USB Host HID Device Driver
4  
5 This is the Human Interface Device Class driver file for a USB Embedded Host
6 device. This file should be used in a project with usb_host.c to provided the
7 USB hardware interface.
8  
9 Acronyms/abbreviations used by this class:
10 * HID - Human Interface Device
11  
12 To interface with usb_host.c, the routine USBHostHIDInitialize() should be
13 specified as the Initialize() function, and USBHostHIDEventHandler() should
14 be specified as the EventHandler() function in the usbClientDrvTable[] array
15 declared in usb_config.h.
16  
17 This driver can be configured to use transfer events from usb_host.c. Transfer
18 events require more RAM and ROM than polling, but it cuts down or even
19 eliminates the required polling of the various USBxxxTasks functions. For this
20 class, USBHostHIDTasks() is compiled out if transfer events from usb_host.c
21 are used. However, USBHostTasks() still must be called to provide attach,
22 enumeration, and detach services. If transfer events from usb_host.c
23 are going to be used, USB_ENABLE_TRANSFER_EVENT should be defined. If transfer
24 status is going to be polled, USB_ENABLE_TRANSFER_EVENT should not be defined.
25  
26 This driver can also be configured to provide HID transfer events to
27 the next layer. Generating these events requires a small amount of extra ROM,
28 but no extra RAM. The layer above this driver must be configured to receive
29 and respond to the events. If HID transfer events are going to be
30 sent to the next layer, USB_HID_ENABLE_TRANSFER_EVENT should be defined. If
31 HID transfer status is going to be polled, USB_HID_ENABLE_TRANSFER_EVENT
32 should not be defined. In any case transfer event EVENT_HID_RPT_DESC_PARSED
33 will be sent to interface layer. Application must provide a function
34 to collect the report descriptor information. Report descriptor information will
35 be overwritten with new report descriptor(in case multiple interface are present)
36 information when cotrol returns to HID driver . This is done to avoid using
37 extra RAM.
38  
39 Since HID transfers are performed with interrupt taransfers,
40 USB_SUPPORT_INTERRUPT_TRANSFERS must be defined.
41  
42 FileName: usb_host_hid.c
43 Dependencies: None
44 Processor: PIC24/dsPIC30/dsPIC33/PIC32MX
45 Compiler: C30/C32
46 Company: Microchip Technology, Inc.
47  
48 Software License Agreement
49  
50 The software supplied herewith by Microchip Technology Incorporated
51 (the “Company”) for its PICmicro® Microcontroller is intended and
52 supplied to you, the Company’s customer, for use solely and
53 exclusively on Microchip PICmicro Microcontroller products. The
54 software is owned by the Company and/or its supplier, and is
55 protected under applicable copyright laws. All rights are reserved.
56 Any use in violation of the foregoing restrictions may subject the
57 user to criminal sanctions under applicable laws, as well as to
58 civil liability for the breach of the terms and conditions of this
59 license.
60  
61 THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
62 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
63 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
64 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
65 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
66 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
67  
68 *******************************************************************************/
69  
70 /********************************************************************
71 File Description:
72  
73 Change History:
74 Rev Description
75 ---- -----------
76 2.2 Initial Release
77  
78 2.3-2.5a No changes
79  
80 2.5b Corrected heap allocation issue
81  
82 2.6-2.6a No changes
83  
84 2.7 Fixed issue where deviceInfoHID[i].rptDescriptor was freed
85 twice. The second free results in possible issues in future
86 malloc() calls in the C32 compiler.
87  
88 2.7a Provided macro wrapped versions of malloc() and free()
89 so that a user can override these functions easily.
90 ********************************************************************/
91  
92 #include <stdlib.h>
93 #include <string.h>
94 #include "GenericTypeDefs.h"
95 #include "HardwareProfile.h"
96 #include "usb_config.h"
97 #include "USB\usb.h"
98 #include "USB\usb_host_hid.h"
99 #include "USB\usb_host_hid_parser.h"
100  
101 //#define DEBUG_MODE
102 #ifdef DEBUG_MODE
103 #include "uart2.h"
104 #endif
105  
106  
107 // *****************************************************************************
108 // *****************************************************************************
109 // Configuration
110 // *****************************************************************************
111 // *****************************************************************************
112  
113 // *****************************************************************************
114 /* Max Number of Supported Devices
115  
116 This value represents the maximum number of attached devices this class driver
117 can support. If the user does not define a value, it will be set to 1.
118 Currently this must be set to 1, due to limitations in the USB Host layer.
119 */
120 #ifndef USB_MAX_HID_DEVICES
121 #define USB_MAX_HID_DEVICES 1
122 #endif
123  
124 // *****************************************************************************
125 // *****************************************************************************
126 // Constants
127 // *****************************************************************************
128 // *****************************************************************************
129  
130 // *****************************************************************************
131 // State Machine Constants
132 // *****************************************************************************
133  
134 #ifndef USB_ENABLE_TRANSFER_EVENT
135  
136 #define STATE_MASK 0x00F0 //
137 #define SUBSTATE_MASK 0x000F //
138  
139 #define NEXT_STATE 0x0010 //
140 #define NEXT_SUBSTATE 0x0001 //
141  
142  
143 #define STATE_DETACHED 0x0000 //
144  
145 #define STATE_INITIALIZE_DEVICE 0x0010 //
146 #define SUBSTATE_WAIT_FOR_ENUMERATION 0x0000 //
147 #define SUBSTATE_DEVICE_ENUMERATED 0x0001 //
148  
149 /* Get Report Descriptor & parse */ //
150 #define STATE_GET_REPORT_DSC 0x0020 //
151 #define SUBSTATE_SEND_GET_REPORT_DSC 0x0000 //
152 #define SUBSTATE_WAIT_FOR_REPORT_DSC 0x0001 //
153 #define SUBSTATE_GET_REPORT_DSC_COMPLETE 0x0002 //
154 #define SUBSTATE_PARSE_REPORT_DSC 0x0003 //
155 #define SUBSTATE_PARSING_COMPLETE 0x0004 //
156  
157 #define STATE_RUNNING 0x0030 //
158 #define SUBSTATE_WAITING_FOR_REQ 0x0000 //
159 #define SUBSTATE_SEND_READ_REQ 0x0001 //
160 #define SUBSTATE_READ_REQ_WAIT 0x0002 //
161 #define SUBSTATE_READ_REQ_DONE 0x0003 //
162 #define SUBSTATE_SEND_WRITE_REQ 0x0004 //
163 #define SUBSTATE_WRITE_REQ_WAIT 0x0005 //
164 #define SUBSTATE_WRITE_REQ_DONE 0x0006 //
165  
166 #define STATE_HID_RESET_RECOVERY 0x0040 //
167 #define SUBSTATE_SEND_RESET 0x0000 //
168 #define SUBSTATE_WAIT_FOR_RESET 0x0001 //
169  
170 #define STATE_HOLDING 0x0060 //
171  
172  
173 #else
174  
175  
176 #define STATE_DETACHED 0x0000 //
177  
178 #define STATE_INITIALIZE_DEVICE 0x0001 //
179 #define STATE_WAIT_FOR_REPORT_DSC 0x0002 //
180 #define STATE_PARSING_COMPLETE 0x0003 //
181  
182 #define STATE_RUNNING 0x0004 //
183 #define STATE_READ_REQ_WAIT 0x0005 //
184 #define STATE_WRITE_REQ_WAIT 0x0006 //
185  
186 #define STATE_WAIT_FOR_RESET 0x0007 //
187 #define STATE_RESET_COMPLETE 0x0008 //
188  
189 #define STATE_HOLDING 0x0009 //
190  
191 #endif
192  
193 // *****************************************************************************
194 // Other Constants
195 // *****************************************************************************
196  
197 #define USB_HID_RESET (0xFF) // Device Request code to reset the device.
198 #define MARK_RESET_RECOVERY (0x0E) // Maintain with USB_MSD_DEVICE_INFO
199  
200 /* Class-Specific Requests */
201 #define USB_HID_GET_REPORT (0x01) //
202 #define USB_HID_GET_IDLE (0x02) //
203 #define USB_HID_GET_PROTOCOL (0x03) //
204 #define USB_HID_SET_REPORT (0x09) //
205 #define USB_HID_SET_IDLE (0x0A) //
206 #define USB_HID_SET_PROTOCOL (0x0B) //
207  
208 #define USB_HID_INPUT_REPORT (0x01) //
209 #define USB_HID_OUTPUT_REPORT (0x02) //
210 #define USB_HID_FEATURE_REPORT (0x00) //
211  
212  
213 //******************************************************************************
214 //******************************************************************************
215 // Data Structures
216 //******************************************************************************
217 //******************************************************************************
218 /* USB HID Device Information
219 This structure is used to hold information of all the interfaces in a device that unique
220 */
221 typedef struct _USB_HID_INTERFACE_DETAILS
222 {
223 struct _USB_HID_INTERFACE_DETAILS *next; // Pointer to next interface in the list.
224 WORD sizeOfRptDescriptor; // Size of report descriptor of a particular interface.
225 WORD endpointMaxDataSize; // Max data size for a interface.
226 BYTE endpointIN; // HID IN endpoint for corresponding interface.
227 BYTE endpointOUT; // HID OUT endpoint for corresponding interface.
228 BYTE interfaceNumber; // Interface number.
229 BYTE endpointPollInterval; // Polling rate of corresponding interface.
230 } USB_HID_INTERFACE_DETAILS;
231  
232 /*
233 This structure is used to hold information about device common to all the interfaces
234 */
235 typedef struct _USB_HID_DEVICE_INFO
236 {
237 USB_HID_DEVICE_ID ID; // Information about the device.
238 WORD reportId; // Report ID of the current transfer.
239 BYTE* userData; // Data pointer to application buffer.
240 BYTE* rptDescriptor; // Common pointer to report descritor for all the interfaces.
241 USB_HID_RPT_DESC_ERROR HIDparserError; // Error code incase report descriptor is not in proper format.
242 union
243 {
244 struct
245 {
246 BYTE bfDirection : 1; // Direction of current transfer (0=OUT, 1=IN).
247 BYTE bfReset : 1; // Flag indicating to perform HID Reset.
248 BYTE bfClearDataIN : 1; // Flag indicating to clear the IN endpoint.
249 BYTE bfClearDataOUT : 1; // Flag indicating to clear the OUT endpoint.
250 BYTE breportDataCollected : 1; // Flag indicationg report data is collected ny application
251 };
252 BYTE val;
253 } flags;
254 BYTE driverSupported; // If HID driver supports requested Class,Subclass & Protocol.
255 BYTE errorCode; // Error code of last error.
256 BYTE state; // State machine state of the device.
257 BYTE returnState; // State to return to after performing error handling.
258 BYTE noOfInterfaces; // Total number of interfaces in the device.
259 BYTE interface; // Interface number of current transfer.
260 BYTE bytesTransferred; // Number of bytes transferred to/from the user's data buffer.
261 BYTE reportSize; // Size of report currently requested for transfer.
262 BYTE endpointDATA; // Endpoint to use for the current transfer.
263 } USB_HID_DEVICE_INFO;
264  
265  
266  
267  
268 //******************************************************************************
269 //******************************************************************************
270 // Section: Local Prototypes
271 //******************************************************************************
272 //******************************************************************************
273 void _USBHostHID_FreeRptDecriptorDataMem(BYTE deviceAddress);
274 void _USBHostHID_ResetStateJump( BYTE i );
275  
276  
277 //******************************************************************************
278 //******************************************************************************
279 // Macros
280 //******************************************************************************
281 //******************************************************************************
282 #ifndef USB_MALLOC
283 #define USB_MALLOC(size) malloc(size)
284 #endif
285  
286 #ifndef USB_FREE
287 #define USB_FREE(ptr) free(ptr)
288 #endif
289  
290 #define USB_FREE_AND_CLEAR(ptr) {USB_FREE(ptr); ptr = NULL;}
291  
292 #define _USBHostHID_LockDevice(x) { \
293 deviceInfoHID[i].errorCode = x; \
294 deviceInfoHID[i].state = STATE_HOLDING; \
295 }
296  
297 #ifndef USB_ENABLE_TRANSFER_EVENT
298  
299 #define _USBHostHID_SetNextState() { deviceInfoHID[i].state = (deviceInfoHID[i].state & STATE_MASK) + NEXT_STATE; }
300 #define _USBHostHID_SetNextSubState() { deviceInfoHID[i].state += NEXT_SUBSTATE; }
301 #define _USBHostHID_TerminateReadTransfer( error ) { \
302 deviceInfoHID[i].errorCode = error; \
303 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;\
304 }
305 #define _USBHostHID_TerminateWriteTransfer( error ) { \
306 deviceInfoHID[i].errorCode = error; \
307 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;\
308 }
309 #else
310 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
311 #define _USBHostHID_TerminateReadTransfer( error ) { \
312 deviceInfoHID[i].errorCode = error; \
313 deviceInfoHID[i].state = STATE_RUNNING;\
314 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_READ_DONE, &transferEventData, sizeof(HID_TRANSFER_DATA) );\
315 }
316 #define _USBHostHID_TerminateWriteTransfer( error ) { \
317 deviceInfoHID[i].errorCode = error; \
318 deviceInfoHID[i].state = STATE_RUNNING;\
319 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_WRITE_DONE, &transferEventData, sizeof(HID_TRANSFER_DATA) );\
320 }
321 #else
322 #define _USBHostHID_TerminateReadTransfer( error ) { \
323 deviceInfoHID[i].errorCode = error; \
324 deviceInfoHID[i].state = STATE_RUNNING;\
325 }
326 #define _USBHostHID_TerminateWriteTransfer( error ) { \
327 deviceInfoHID[i].errorCode = error; \
328 deviceInfoHID[i].state = STATE_RUNNING;\
329 }
330 #endif
331 #endif
332  
333 //******************************************************************************
334 //******************************************************************************
335 // Section: HID Host Global Variables
336 //******************************************************************************
337 //******************************************************************************
338  
339 static USB_HID_DEVICE_INFO deviceInfoHID[USB_MAX_HID_DEVICES] __attribute__ ((aligned));
340 static USB_HID_INTERFACE_DETAILS* pInterfaceDetails = NULL;
341 static USB_HID_INTERFACE_DETAILS* pCurrInterfaceDetails = NULL;
342  
343 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
344 static HID_TRANSFER_DATA transferEventData;
345 #endif
346  
347 //******************************************************************************
348 //******************************************************************************
349 // Section: HID Host External Variables
350 //******************************************************************************
351 //******************************************************************************
352  
353 extern BYTE* parsedDataMem;
354  
355 extern USB_HID_RPT_DESC_ERROR _USBHostHID_Parse_Report(BYTE*, WORD, WORD, BYTE);
356  
357 // *****************************************************************************
358 // *****************************************************************************
359 // Application Callable Functions
360 // *****************************************************************************
361 // *****************************************************************************
362  
363 /*******************************************************************************
364 Function:
365 BOOL USBHostHIDDeviceDetect( BYTE deviceAddress )
366  
367 Description:
368 This function determines if a HID device is attached and ready to use.
369  
370 Precondition:
371 None
372  
373 Parameters:
374 BYTE deviceAddress - Address of the attached device.
375  
376 Return Values:
377 TRUE - HID present and ready
378 FALSE - HID not present or not ready
379  
380 Remarks:
381 This function replaces the USBHostHID_ApiDeviceDetect() function.
382 *******************************************************************************/
383 BOOL USBHostHIDDeviceDetect( BYTE deviceAddress )
384 {
385 BYTE i;
386  
387 // Find the correct device.
388 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
389 if (i == USB_MAX_HID_DEVICES)
390 {
391 return FALSE;
392 }
393  
394 if ((USBHostHIDDeviceStatus(deviceAddress) == USB_HID_NORMAL_RUNNING) &&
395 (deviceAddress != 0))
396 {
397 return TRUE;
398 }
399  
400 return FALSE;
401 }
402  
403  
404 /*******************************************************************************
405 Function:
406 BYTE USBHostHIDDeviceStatus( BYTE deviceAddress )
407  
408 Summary:
409  
410 Description:
411 This function determines the status of a HID device.
412  
413 Preconditions: None
414  
415 Parameters:
416 BYTE deviceAddress - address of device to query
417  
418 Return Values:
419 USB_HID_DEVICE_NOT_FOUND - Illegal device address, or the
420 device is not an HID
421 USB_HID_INITIALIZING - HID is attached and in the
422 process of initializing
423 USB_PROCESSING_REPORT_DESCRIPTOR - HID device is detected and report
424 descriptor is being parsed
425 USB_HID_NORMAL_RUNNING - HID Device is running normal,
426 ready to send and receive reports
427 USB_HID_DEVICE_HOLDING -
428 USB_HID_DEVICE_DETACHED - HID detached.
429  
430 Remarks:
431 None
432 *******************************************************************************/
433 BYTE USBHostHIDDeviceStatus( BYTE deviceAddress )
434 {
435 BYTE i;
436 BYTE status;
437  
438 // Find the correct device.
439 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
440 if (i == USB_MAX_HID_DEVICES)
441 {
442 return USB_HID_DEVICE_NOT_FOUND;
443 }
444  
445 status = USBHostDeviceStatus( deviceAddress );
446 if (status != USB_DEVICE_ATTACHED)
447 {
448 return status;
449 }
450 else
451 {
452 // The device is attached and done enumerating. We can get more specific now.
453 #ifndef USB_ENABLE_TRANSFER_EVENT
454 switch (deviceInfoHID[i].state & STATE_MASK)
455 {
456 case STATE_INITIALIZE_DEVICE:
457 return USB_HID_INITIALIZING;
458 break;
459 case STATE_GET_REPORT_DSC:
460 return USB_PROCESSING_REPORT_DESCRIPTOR;
461 break;
462  
463 case STATE_RUNNING:
464 return USB_HID_NORMAL_RUNNING;
465 break;
466 case STATE_HOLDING:
467 return USB_HID_DEVICE_HOLDING;
468 break;
469  
470 case STATE_HID_RESET_RECOVERY:
471 return USB_HID_RESETTING_DEVICE;
472 break;
473  
474 default:
475 return USB_HID_DEVICE_DETACHED;
476 break;
477 }
478 #else
479 switch (deviceInfoHID[i].state)
480 {
481 case STATE_INITIALIZE_DEVICE:
482 case STATE_PARSING_COMPLETE:
483 return USB_HID_INITIALIZING;
484 break;
485  
486 case STATE_RUNNING:
487 case STATE_READ_REQ_WAIT:
488 case STATE_WRITE_REQ_WAIT:
489 return USB_HID_NORMAL_RUNNING;
490 break;
491  
492 case STATE_HOLDING:
493 return USB_HID_DEVICE_HOLDING;
494 break;
495  
496 case STATE_WAIT_FOR_RESET:
497 case STATE_RESET_COMPLETE:
498 return USB_HID_RESETTING_DEVICE;
499 break;
500  
501 default:
502 return USB_HID_DEVICE_DETACHED;
503 break;
504 }
505 #endif
506 }
507 }
508  
509 /*******************************************************************************
510 Function:
511 BYTE USBHostHIDResetDevice( BYTE deviceAddress )
512  
513 Summary:
514 This function starts a HID reset.
515  
516 Description:
517 This function starts a HID reset. A reset can be
518 issued only if the device is attached and not being initialized.
519  
520 Precondition:
521 None
522  
523 Parameters:
524 BYTE deviceAddress - Device address
525  
526 Return Values:
527 USB_SUCCESS - Reset started
528 USB_MSD_DEVICE_NOT_FOUND - No device with specified address
529 USB_MSD_ILLEGAL_REQUEST - Device is in an illegal state for reset
530  
531 Remarks:
532 None
533 *******************************************************************************/
534 BYTE USBHostHIDResetDevice( BYTE deviceAddress )
535 {
536 BYTE i;
537  
538 // Find the correct device.
539 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
540 if (i == USB_MAX_HID_DEVICES)
541 {
542 return USB_HID_DEVICE_NOT_FOUND;
543 }
544  
545 #ifndef USB_ENABLE_TRANSFER_EVENT
546 if (((deviceInfoHID[i].state & STATE_MASK) != STATE_DETACHED) &&
547 ((deviceInfoHID[i].state & STATE_MASK) != STATE_INITIALIZE_DEVICE))
548 #else
549 if ((deviceInfoHID[i].state != STATE_DETACHED) &&
550 (deviceInfoHID[i].state != STATE_INITIALIZE_DEVICE))
551 #endif
552 {
553 deviceInfoHID[i].flags.val |= MARK_RESET_RECOVERY;
554 deviceInfoHID[i].flags.bfReset = 1;
555 #ifndef USB_ENABLE_TRANSFER_EVENT
556 deviceInfoHID[i].returnState = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
557 #else
558 deviceInfoHID[i].returnState = STATE_RUNNING;
559 #endif
560  
561 _USBHostHID_ResetStateJump( i );
562 return USB_SUCCESS;
563 }
564  
565 return USB_HID_ILLEGAL_REQUEST;
566 }
567  
568  
569 /****************************************************************************
570 Function:
571 BOOL USBHostHIDResetDeviceWithWait( BYTE deviceAddress )
572  
573 Description:
574 This function resets a HID device, and waits until the reset is complete.
575  
576 Precondition:
577 None
578  
579 Parameters:
580 BYTE deviceAddress - Address of the device to reset.
581  
582 Return Values:
583 USB_SUCCESS - Reset successful
584 USB_HID_RESET_ERROR - Error while resetting device
585 Others - See return values for USBHostHIDResetDevice()
586 and error codes that can be returned
587 in the errorCode parameter of
588 USBHostHIDTransferIsComplete();
589  
590 Remarks:
591 None
592 ***************************************************************************/
593  
594 BYTE USBHostHIDResetDeviceWithWait( BYTE deviceAddress )
595 {
596 BYTE byteCount;
597 BYTE errorCode;
598  
599 errorCode = USBHostHIDResetDevice( deviceAddress );
600 if (errorCode)
601 {
602 return errorCode;
603 }
604  
605 do
606 {
607 USBTasks();
608 errorCode = USBHostHIDDeviceStatus( deviceAddress );
609 } while (errorCode == USB_HID_RESETTING_DEVICE);
610  
611  
612 if (USBHostHIDTransferIsComplete( deviceAddress, &errorCode, (BYTE*)&byteCount ))
613 {
614 return errorCode;
615 }
616  
617 return USB_HID_RESET_ERROR;
618 }
619  
620 /*******************************************************************************
621 Function:
622 void USBHostHIDTasks( void )
623  
624 Summary:
625 This function performs the maintenance tasks required by HID class
626  
627 Description:
628 This function performs the maintenance tasks required by the HID
629 class. If transfer events from the host layer are not being used, then
630 it should be called on a regular basis by the application. If transfer
631 events from the host layer are being used, this function is compiled out,
632 and does not need to be called.
633  
634 Precondition:
635 USBHostHIDInitialize() has been called.
636  
637 Parameters:
638 None - None
639  
640 Returns:
641 None
642  
643 Remarks:
644 None
645 *******************************************************************************/
646 void USBHostHIDTasks( void )
647 {
648 #ifndef USB_ENABLE_TRANSFER_EVENT
649 DWORD byteCount;
650 BYTE errorCode;
651 BYTE i;
652  
653 for (i=0; i<USB_MAX_HID_DEVICES; i++)
654 {
655 if (deviceInfoHID[i].ID.deviceAddress == 0) /* device address updated by lower layer */
656 {
657 deviceInfoHID[i].state = STATE_DETACHED;
658 }
659  
660 switch (deviceInfoHID[i].state & STATE_MASK)
661 {
662 case STATE_DETACHED:
663 // No device attached.
664 break;
665  
666 case STATE_INITIALIZE_DEVICE:
667 switch (deviceInfoHID[i].state & SUBSTATE_MASK)
668 {
669 case SUBSTATE_WAIT_FOR_ENUMERATION:
670 if (USBHostDeviceStatus( deviceInfoHID[i].ID.deviceAddress ) == USB_DEVICE_ATTACHED)
671 {
672 _USBHostHID_SetNextSubState();
673 pCurrInterfaceDetails = pInterfaceDetails; // assign current interface to top of list
674 }
675 break;
676  
677 case SUBSTATE_DEVICE_ENUMERATED:
678 _USBHostHID_SetNextState(); /* need to add sub states to Set Config, Get LANGID & String Descriptors */
679 break;
680  
681 default :
682 break;
683 }
684 break;
685  
686 case STATE_GET_REPORT_DSC:
687 switch (deviceInfoHID[i].state & SUBSTATE_MASK)
688 {
689 case SUBSTATE_SEND_GET_REPORT_DSC:
690 // If we are currently sending a token, we cannot do anything.
691 if (U1CONbits.TOKBUSY)
692 break;
693  
694 if(pCurrInterfaceDetails != NULL) // end of interface list
695 {
696 if(pCurrInterfaceDetails->sizeOfRptDescriptor !=0) // interface must have a Report Descriptor
697 {
698 if((deviceInfoHID[i].rptDescriptor = (BYTE *)USB_MALLOC(pCurrInterfaceDetails->sizeOfRptDescriptor)) == NULL)
699 {
700 _USBHostHID_LockDevice( USB_MEMORY_ALLOCATION_ERROR );
701 break;
702 }
703 // send new interface request
704 if (!USBHostIssueDeviceRequest( deviceInfoHID[i].ID.deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_INTERFACE,
705 USB_REQUEST_GET_DESCRIPTOR, DSC_RPT, pCurrInterfaceDetails->interfaceNumber,pCurrInterfaceDetails->sizeOfRptDescriptor, deviceInfoHID[i].rptDescriptor,
706 USB_DEVICE_REQUEST_GET, deviceInfoHID[i].ID.clientDriverID ))
707 {
708 _USBHostHID_SetNextSubState();
709 }
710 else
711 {
712 USB_FREE_AND_CLEAR(deviceInfoHID[i].rptDescriptor);
713 }
714 }
715 }
716 else
717 {
718 _USBHostHID_LockDevice( USB_HID_INTERFACE_ERROR );
719 }
720 break;
721  
722 case SUBSTATE_WAIT_FOR_REPORT_DSC:
723 if (USBHostTransferIsComplete( deviceInfoHID[i].ID.deviceAddress, 0, &errorCode, &byteCount ))
724 {
725 if (errorCode)
726 {
727 /* Set error code */
728 _USBHostHID_LockDevice( errorCode );
729 }
730 else
731 {
732 // Clear the STALL. Since it is EP0, we do not have to clear the stall.
733 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, 0 );
734 }
735 _USBHostHID_SetNextSubState();
736 }
737 break;
738  
739 case SUBSTATE_GET_REPORT_DSC_COMPLETE:
740 _USBHostHID_SetNextSubState();
741 break;
742  
743 case SUBSTATE_PARSE_REPORT_DSC:
744 /* Invoke HID Parser ,, validate for all the errors in report Descriptor */
745 deviceInfoHID[i].HIDparserError = _USBHostHID_Parse_Report((BYTE*)deviceInfoHID[i].rptDescriptor , (WORD)pCurrInterfaceDetails->sizeOfRptDescriptor,
746 (WORD)pCurrInterfaceDetails->endpointPollInterval, pCurrInterfaceDetails->interfaceNumber);
747 if(deviceInfoHID[i].HIDparserError)
748 {
749 /* Report Descriptor is flawed , flag error and free memory ,
750 retry by requesting again */
751 #ifdef DEBUG_MODE
752 UART2PrintString("\r\nHID Error Reported : ");
753 UART2PutHex(deviceInfoHID[i].HIDparserError);
754 #endif
755 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
756 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
757 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
758 }
759 else
760 {
761 _USBHostHID_SetNextSubState();
762 }
763 break;
764  
765 case SUBSTATE_PARSING_COMPLETE:
766 if (USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RPT_DESC_PARSED, NULL, 0 ))
767 {
768 deviceInfoHID[i].flags.breportDataCollected = 1;
769 }
770 else
771 {
772 // At least once report must be present. If not, flag error.
773 if((pCurrInterfaceDetails->interfaceNumber == (deviceInfoHID[i].noOfInterfaces-1)) &&
774 (deviceInfoHID[i].flags.breportDataCollected == 0))
775 {
776 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
777 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
778 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
779 }
780 }
781 // free the previous allocated memory, reallocate for new interface if needed
782 USB_FREE_AND_CLEAR(deviceInfoHID[i].rptDescriptor);
783 pCurrInterfaceDetails = pCurrInterfaceDetails->next;
784 if(pCurrInterfaceDetails != NULL)
785 {
786 deviceInfoHID[i].state = STATE_GET_REPORT_DSC;
787 }
788 else
789 {
790 if(deviceInfoHID[i].flags.breportDataCollected == 0)
791 {
792 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
793 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
794 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
795 }
796 else
797 {
798 deviceInfoHID[i].state = STATE_RUNNING;
799 }
800 }
801 break;
802  
803 default :
804 break;
805 }
806 break;
807  
808 case STATE_RUNNING:
809 switch (deviceInfoHID[i].state & SUBSTATE_MASK)
810 {
811 case SUBSTATE_WAITING_FOR_REQ:
812 /* waiting for request from application */
813 break;
814  
815 case SUBSTATE_SEND_READ_REQ:
816 // State removed with new architecture. Functionality is consolidated elsewhere.
817 break;
818  
819 case SUBSTATE_READ_REQ_WAIT:
820 if (USBHostTransferIsComplete( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA, &errorCode, &byteCount ))
821 {
822 if (errorCode)
823 {
824 if(USB_ENDPOINT_STALLED == errorCode)
825 {
826 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
827 deviceInfoHID[i].returnState = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
828 deviceInfoHID[i].flags.bfReset = 1;
829 _USBHostHID_ResetStateJump( i );
830 }
831 else
832 {
833 /* Set proper error code as per HID guideline */
834 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
835 transferEventData.dataCount = byteCount;
836 transferEventData.bErrorCode = errorCode;
837 #endif
838 _USBHostHID_TerminateReadTransfer(errorCode);
839 }
840 }
841 else
842 {
843 // Clear the STALL. Since it is EP0, we do not have to clear the stall.
844 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
845 deviceInfoHID[i].bytesTransferred = byteCount; /* Can compare with report size and flag error ???*/
846 _USBHostHID_SetNextSubState();
847 }
848 }
849 #ifdef DEBUG_MODE
850 UART2PrintString("|");
851 #endif
852 break;
853  
854 case SUBSTATE_READ_REQ_DONE:
855 /* Next transfer */
856 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
857 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
858 USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_READ_DONE, NULL, 0 );
859 #endif
860 #ifdef DEBUG_MODE
861 UART2PrintString("}");
862 #endif
863 break;
864  
865 case SUBSTATE_SEND_WRITE_REQ:
866 // State removed with new architecture. Functionality is consolidated elsewhere.
867 break;
868  
869 case SUBSTATE_WRITE_REQ_WAIT:
870 if (USBHostTransferIsComplete( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA, &errorCode, &byteCount ))
871 {
872 if (errorCode)
873 {
874 if(USB_ENDPOINT_STALLED == errorCode)
875 {
876 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
877 deviceInfoHID[i].returnState = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
878 deviceInfoHID[i].flags.bfReset = 1;
879 _USBHostHID_ResetStateJump( i );
880 }
881 else
882 {
883 /* Set proper error code as per HID guideline */
884 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
885 transferEventData.dataCount = byteCount;
886 transferEventData.bErrorCode = errorCode;
887 #endif
888 _USBHostHID_TerminateWriteTransfer(errorCode);
889 }
890 }
891 else
892 {
893 // TODO assuming only a STALL here
894 // Clear the STALL. Since it is EP0, we do not have to clear the stall.
895 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA);
896 _USBHostHID_SetNextSubState();
897 }
898 }
899 break;
900  
901 case SUBSTATE_WRITE_REQ_DONE:
902 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
903 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
904 USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_WRITE_DONE, NULL, 0 );
905 #endif
906 break;
907  
908 }
909 break;
910  
911 case STATE_HID_RESET_RECOVERY:
912 switch (deviceInfoHID[i].state & SUBSTATE_MASK)
913 {
914 case SUBSTATE_SEND_RESET: /* Not sure of rest request */
915 // State removed with new architecture. Functionality is consolidated elsewhere.
916 break;
917  
918 case SUBSTATE_WAIT_FOR_RESET:
919 if (USBHostTransferIsComplete( deviceInfoHID[i].ID.deviceAddress, 0, &errorCode, &byteCount ))
920 {
921 deviceInfoHID[i].flags.bfReset = 0;
922 if (errorCode)
923 {
924 if(USB_ENDPOINT_STALLED == errorCode)
925 {
926 // If it stalled, try again.
927 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, 0 );
928 deviceInfoHID[i].returnState = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ;
929 deviceInfoHID[i].flags.bfReset = 1;
930 }
931 }
932 deviceInfoHID[i].errorCode = errorCode;
933 _USBHostHID_ResetStateJump( i );
934 }
935 break;
936 }
937 break;
938  
939 case STATE_HOLDING:
940 break;
941  
942 default :
943 break;
944 }
945 }
946 #endif
947 }
948  
949  
950 /*******************************************************************************
951 Function:
952 BYTE USBHostHIDTerminateTransfer( BYTE deviceAddress, BYTE direction, BYTE interfaceNum )
953  
954 Summary:
955 This function terminates a transfer that is in progress.
956  
957 Description:
958 This function terminates a transfer that is in progress.
959  
960 Precondition:
961 None
962  
963 Parameters:
964 BYTE deviceAddress - Device address
965 BYTE direction - Transfer direction. Valid values are:
966 * 1 = In (Read)
967 * 0 = Out (Write)
968 BYTE interfaceNum - Interface number
969  
970 Return Values:
971 USB_SUCCESS - Transfer terminated
972 USB_HID_DEVICE_NOT_FOUND - No device with specified address
973  
974 Remarks:
975 None
976 *******************************************************************************/
977 BYTE USBHostHIDTerminateTransfer( BYTE deviceAddress, BYTE direction, BYTE interfaceNum )
978 {
979 BYTE i;
980 BYTE endpoint;
981  
982 // Find the correct device.
983 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
984 if (i == USB_MAX_HID_DEVICES)
985 {
986 return USB_HID_DEVICE_NOT_FOUND;
987 }
988  
989 pCurrInterfaceDetails = pInterfaceDetails;
990 while((pCurrInterfaceDetails != NULL) && (pCurrInterfaceDetails->interfaceNumber != interfaceNum))
991 {
992 pCurrInterfaceDetails = pCurrInterfaceDetails->next;
993 }
994  
995 endpoint = pCurrInterfaceDetails->endpointIN;
996 if (direction == 0)
997 {
998 endpoint = pCurrInterfaceDetails->endpointOUT;
999 }
1000  
1001 USBHostTerminateTransfer( deviceAddress, endpoint );
1002  
1003 // Reset the state.
1004 deviceInfoHID[i].state = STATE_RUNNING;
1005  
1006 return USB_SUCCESS;
1007 }
1008  
1009  
1010 /*******************************************************************************
1011 Function:
1012 USBHostHIDTransfer( BYTE deviceAddress, BYTE direction, BYTE reportid,
1013 BYTE size, BYTE *data)
1014  
1015 Summary:
1016 This function starts a HID transfer.
1017  
1018 Description:
1019 This function starts a HID transfer. A read/write wrapper is provided in
1020 application interface file to access this function.
1021  
1022 Preconditions:
1023 None
1024  
1025 Parameters:
1026 BYTE deviceAddress - Device address
1027 BYTE direction - 1=read, 0=write
1028 BYTE interfaceNum - Interface number of the device
1029 BYTE reportid - Report ID of the requested report
1030 BYTE size - Byte size of the data buffer
1031 BYTE *data - Pointer to the data buffer
1032  
1033 Return Values:
1034 USB_SUCCESS - Request started successfully
1035 USB_HID_DEVICE_NOT_FOUND - No device with specified address
1036 USB_HID_DEVICE_BUSY - Device not in proper state for
1037 performing a transfer
1038 Others - Return values from USBHostIssueDeviceRequest(),
1039 USBHostRead(), and USBHostWrite()
1040  
1041 Remarks:
1042 None
1043 *******************************************************************************/
1044  
1045 BYTE USBHostHIDTransfer( BYTE deviceAddress, BYTE direction, BYTE interfaceNum, WORD reportid, WORD size, BYTE *data)
1046 {
1047 BYTE i;
1048 BYTE errorCode;
1049  
1050 #ifdef DEBUG_MODE
1051 // UART2PrintString( "HID: Transfer: " );
1052 if (direction)
1053 {
1054 // UART2PrintString( "Read, " );
1055 }
1056 else
1057 {
1058 // UART2PrintString( "Write, " );
1059 }
1060 #endif
1061  
1062 // Find the correct device.
1063 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
1064 if (i == USB_MAX_HID_DEVICES)
1065 {
1066 return USB_HID_DEVICE_NOT_FOUND;
1067 }
1068  
1069 pCurrInterfaceDetails = pInterfaceDetails;
1070 while((pCurrInterfaceDetails != NULL) && (pCurrInterfaceDetails->interfaceNumber != interfaceNum))
1071 {
1072 pCurrInterfaceDetails = pCurrInterfaceDetails->next;
1073 }
1074 // Make sure the device is in a state ready to read/write.
1075 // Make sure the device is in a state ready to read/write.
1076 #ifndef USB_ENABLE_TRANSFER_EVENT
1077 if (deviceInfoHID[i].state != (STATE_RUNNING) &&
1078 (deviceInfoHID[i].state & SUBSTATE_MASK) != (SUBSTATE_WAITING_FOR_REQ))
1079 #else
1080 if (deviceInfoHID[i].state != STATE_RUNNING)
1081 #endif
1082 {
1083 return USB_HID_DEVICE_BUSY;
1084 }
1085  
1086 // Initialize the transfer information.
1087 deviceInfoHID[i].bytesTransferred = 0;
1088 deviceInfoHID[i].errorCode = USB_SUCCESS;
1089 deviceInfoHID[i].userData = data;
1090 deviceInfoHID[i].endpointDATA = pCurrInterfaceDetails->endpointIN;
1091 deviceInfoHID[i].reportSize = size;
1092 deviceInfoHID[i].reportId = reportid;
1093 deviceInfoHID[i].interface = interfaceNum;
1094  
1095 if (!direction) // OUT
1096 {
1097 deviceInfoHID[i].endpointDATA = pCurrInterfaceDetails->endpointOUT;
1098 deviceInfoHID[i].reportId = (reportid |((WORD)USB_HID_OUTPUT_REPORT<<8));
1099 }
1100 else
1101 {
1102 deviceInfoHID[i].endpointDATA = pCurrInterfaceDetails->endpointIN;
1103 }
1104 #ifdef DEBUG_MODE
1105 // UART2PrintString( "Data EP: " );
1106 // UART2PutHex( deviceInfoHID[i].endpointDATA );
1107 // UART2PrintString( "\r\n" );
1108 #endif
1109  
1110 if(!direction)
1111 {
1112 if(deviceInfoHID[i].endpointDATA == 0x00)// if endpoint 0 then use control transfer
1113 {
1114 errorCode = USBHostIssueDeviceRequest( deviceInfoHID[i].ID.deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE,
1115 USB_HID_SET_REPORT, deviceInfoHID[i].reportId, deviceInfoHID[i].interface,deviceInfoHID[i].reportSize, deviceInfoHID[i].userData,
1116 USB_DEVICE_REQUEST_SET, deviceInfoHID[i].ID.clientDriverID );
1117 }
1118 else
1119 {
1120 errorCode = USBHostWrite( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA,
1121 deviceInfoHID[i].userData, deviceInfoHID[i].reportSize );
1122 }
1123  
1124 if (!errorCode)
1125 {
1126 #ifndef USB_ENABLE_TRANSFER_EVENT
1127 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_WRITE_REQ_WAIT;
1128 #else
1129 deviceInfoHID[i].state = STATE_WRITE_REQ_WAIT;
1130 #endif
1131 }
1132 else
1133 {
1134 deviceInfoHID[i].errorCode = errorCode;
1135 deviceInfoHID[i].state = STATE_RUNNING;
1136 }
1137 }
1138 else
1139 {
1140 errorCode = USBHostRead( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA,
1141 deviceInfoHID[i].userData, deviceInfoHID[i].reportSize );
1142 if (!errorCode)
1143 {
1144 #ifndef USB_ENABLE_TRANSFER_EVENT
1145 deviceInfoHID[i].state = STATE_RUNNING | SUBSTATE_READ_REQ_WAIT;
1146 #else
1147 deviceInfoHID[i].state = STATE_READ_REQ_WAIT;
1148 #endif
1149 }
1150 else
1151 {
1152 deviceInfoHID[i].errorCode = errorCode;
1153 deviceInfoHID[i].state = STATE_RUNNING;
1154 }
1155 }
1156  
1157 return errorCode;
1158 }
1159  
1160  
1161 /*******************************************************************************
1162 Function:
1163 BOOL USBHostHIDTransferIsComplete( BYTE deviceAddress,
1164 BYTE *errorCode, DWORD *byteCount )
1165  
1166 Summary:
1167 This function indicates whether or not the last transfer is complete.
1168  
1169 Description:
1170 This function indicates whether or not the last transfer is complete.
1171 If the functions returns TRUE, the returned byte count and error
1172 code are valid. Since only one transfer can be performed at once
1173 and only one endpoint can be used, we only need to know the
1174 device address.
1175  
1176 Precondition:
1177 None
1178  
1179 Parameters:
1180 BYTE deviceAddress - Device address
1181 BYTE *errorCode - Error code from last transfer
1182 DWORD *byteCount - Number of bytes transferred
1183  
1184 Return Values:
1185 TRUE - Transfer is complete, errorCode is valid
1186 FALSE - Transfer is not complete, errorCode is not valid
1187 *******************************************************************************/
1188  
1189 BOOL USBHostHIDTransferIsComplete( BYTE deviceAddress, BYTE *errorCode, BYTE *byteCount )
1190 {
1191 BYTE i;
1192  
1193 // Find the correct device.
1194 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
1195 if ((i == USB_MAX_HID_DEVICES) || (deviceInfoHID[i].state == STATE_DETACHED))
1196 {
1197 *errorCode = USB_HID_DEVICE_NOT_FOUND;
1198 *byteCount = 0;
1199 return TRUE;
1200 }
1201  
1202 *byteCount = deviceInfoHID[i].bytesTransferred;
1203 *errorCode = deviceInfoHID[i].errorCode;
1204  
1205 #ifndef USB_ENABLE_TRANSFER_EVENT
1206 if(deviceInfoHID[i].state == (STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ))
1207 #else
1208 if(deviceInfoHID[i].state == STATE_RUNNING)
1209 #endif
1210 {
1211 return TRUE;
1212 }
1213 else
1214 {
1215 return FALSE;
1216 }
1217 }
1218  
1219  
1220 /*******************************************************************************
1221 Function:
1222 BOOL USBHostHID_ApiFindBit(WORD usagePage,WORD usage,HIDReportTypeEnum type,
1223 BYTE* Report_ID, BYTE* Report_Length, BYTE* Start_Bit)
1224  
1225 Description:
1226 This function is used to locate a specific button or indicator.
1227 Once the report descriptor is parsed by the HID layer without any error,
1228 data from the report descriptor is stored in pre defined dat structures.
1229 This function traverses these data structure and exract data required
1230 by application
1231  
1232 Precondition:
1233 None
1234  
1235 Parameters:
1236 WORD usagePage - usage page supported by application
1237 WORD usage - usage supported by application
1238 HIDReportTypeEnum type - report type Input/Output for the particular
1239 usage
1240 BYTE* Report_ID - returns the report ID of the required usage
1241 BYTE* Report_Length - returns the report length of the required usage
1242 BYTE* Start_Bit - returns the start bit of the usage in a
1243 particular report
1244  
1245 Return Values:
1246 TRUE - If the required usage is located in the report descriptor
1247 FALSE - If the application required usage is not supported by the
1248 device(i.e report descriptor).
1249  
1250 Remarks:
1251 Application event handler with event 'EVENT_HID_RPT_DESC_PARSED' is called.
1252 Application is suppose to fill in data details structure 'HID_DATA_DETAILS'
1253 This function can be used to the get the details of the required usages.
1254 *******************************************************************************/
1255 BOOL USBHostHID_ApiFindBit(WORD usagePage,WORD usage,HIDReportTypeEnum type,BYTE* Report_ID,
1256 BYTE* Report_Length, BYTE* Start_Bit)
1257 {
1258 WORD iR;
1259 WORD index;
1260 WORD reportIndex;
1261 HID_REPORTITEM *reportItem;
1262 BYTE* count;
1263  
1264 // Disallow Null Pointers
1265  
1266 if((Report_ID == NULL)|(Report_Length == NULL)|(Start_Bit == NULL))
1267 return FALSE;
1268  
1269 // Search through all the report items
1270  
1271 for (iR=0; iR < deviceRptInfo.reportItems; iR++)
1272 {
1273 reportItem = &itemListPtrs.reportItemList[iR];
1274  
1275 // Search only reports of the proper type
1276  
1277 if ((reportItem->reportType==type))// && (reportItem->globals.reportsize == 1))
1278 {
1279 if (USBHostHID_HasUsage(reportItem,usagePage,usage,(WORD*)&index,(BYTE*)&count))
1280 {
1281 reportIndex = reportItem->globals.reportIndex;
1282 *Report_ID = itemListPtrs.reportList[reportIndex].reportID;
1283 *Start_Bit = reportItem->startBit + index;
1284 if (type == hidReportInput)
1285 *Report_Length = (itemListPtrs.reportList[reportIndex].inputBits + 7)/8;
1286 else if (type == hidReportOutput)
1287 *Report_Length = (itemListPtrs.reportList[reportIndex].outputBits + 7)/8;
1288 else
1289 *Report_Length = (itemListPtrs.reportList[reportIndex].featureBits + 7)/8;
1290 return TRUE;
1291 }
1292 }
1293 }
1294 return FALSE;
1295 }
1296  
1297 /*******************************************************************************
1298 Function:
1299 BOOL USBHostHID_ApiFindValue(WORD usagePage,WORD usage,
1300 HIDReportTypeEnum type,BYTE* Report_ID,BYTE* Report_Length,BYTE*
1301 Start_Bit, BYTE* Bit_Length)
1302  
1303 Description:
1304 Find a specific Usage Value. Once the report descriptor is parsed by the HID
1305 layer without any error, data from the report descriptor is stored in
1306 pre defined dat structures. This function traverses these data structure and
1307 exract data required by application.
1308  
1309 Precondition:
1310 None
1311  
1312 Parameters:
1313 WORD usagePage - usage page supported by application
1314 WORD usage - usage supported by application
1315 HIDReportTypeEnum type - report type Input/Output for the particular
1316 usage
1317 BYTE* Report_ID - returns the report ID of the required usage
1318 BYTE* Report_Length - returns the report length of the required usage
1319 BYTE* Start_Bit - returns the start bit of the usage in a
1320 particular report
1321 BYTE* Bit_Length - returns size of requested usage type data in bits
1322  
1323 Return Values:
1324 TRUE - If the required usage is located in the report descriptor
1325 FALSE - If the application required usage is not supported by the
1326 device(i.e report descriptor).
1327  
1328 Remarks:
1329 Application event handler with event 'EVENT_HID_RPT_DESC_PARSED' is called.
1330 Application is suppose to fill in data details structure 'HID_DATA_DETAILS'
1331 This function can be used to the get the details of the required usages.
1332 *******************************************************************************/
1333 BOOL USBHostHID_ApiFindValue(WORD usagePage,WORD usage,HIDReportTypeEnum type,BYTE* Report_ID,
1334 BYTE* Report_Length,BYTE* Start_Bit, BYTE* Bit_Length)
1335 {
1336 WORD index;
1337 WORD reportIndex;
1338 BYTE iR;
1339 BYTE count;
1340 HID_REPORTITEM *reportItem;
1341  
1342 // Disallow Null Pointers
1343  
1344 if((Report_ID == NULL)|(Report_Length == NULL)|(Start_Bit == NULL)|(Bit_Length == NULL))
1345 return FALSE;
1346  
1347 // Search through all the report items
1348  
1349 for (iR=0; iR < deviceRptInfo.reportItems; iR++)
1350 {
1351 reportItem = &itemListPtrs.reportItemList[iR];
1352  
1353 // Search only reports of the proper type
1354  
1355 if ((reportItem->reportType==type)&& ((reportItem->dataModes & HIDData_ArrayBit) != HIDData_Array)
1356 && (reportItem->globals.reportsize != 1))
1357 {
1358 if (USBHostHID_HasUsage(reportItem,usagePage,usage,&index,&count))
1359 {
1360 reportIndex = reportItem->globals.reportIndex;
1361 *Report_ID = itemListPtrs.reportList[reportIndex].reportID;
1362 *Bit_Length = reportItem->globals.reportsize;
1363 *Start_Bit = reportItem->startBit + index * (reportItem->globals.reportsize);
1364 if (type == hidReportInput)
1365 *Report_Length = (itemListPtrs.reportList[reportIndex].inputBits + 7)/8;
1366 else if (type == hidReportOutput)
1367 *Report_Length = (itemListPtrs.reportList[reportIndex].outputBits + 7)/8;
1368 else
1369 *Report_Length = (itemListPtrs.reportList[reportIndex].featureBits + 7)/8;
1370 return TRUE;
1371 }
1372 }
1373 }
1374 return FALSE;
1375 }
1376  
1377  
1378 /*******************************************************************************
1379 Function:
1380 BYTE USBHostHID_ApiGetCurrentInterfaceNum(void)
1381  
1382 Description:
1383 This function reurns the interface number of the cuurent report descriptor
1384 parsed. This function must be called to fill data interface detail data
1385 structure and passed as parameter when requesinf for report transfers.
1386  
1387 Precondition:
1388 None
1389  
1390 Parameters:
1391 BYTE *errorCode - Error code from last transfer
1392 DWORD *byteCount - Number of bytes transferred
1393  
1394 Return Values:
1395 TRUE - Transfer is complete, errorCode is valid
1396 FALSE - Transfer is not complete, errorCode is not valid
1397  
1398 Remarks:
1399 None
1400 *******************************************************************************/
1401 BYTE USBHostHID_ApiGetCurrentInterfaceNum(void)
1402 {
1403 return(deviceRptInfo.interfaceNumber);
1404 }
1405  
1406  
1407 /*******************************************************************************
1408 Function:
1409 BOOL USBHostHID_ApiImportData(BYTE *report, WORD reportLength,
1410 HID_USER_DATA_SIZE *buffer,HID_DATA_DETAILS *pDataDetails)
1411 Description:
1412 This function can be used by application to extract data from the input
1413 reports. On receiving the input report from the device application can call
1414 the function with required inputs 'HID_DATA_DETAILS'.
1415  
1416 Precondition:
1417 None
1418  
1419 Parameters:
1420 BYTE *report - Input report received from device
1421 WORD reportLength - Length of input report report
1422 HID_USER_DATA_SIZE *buffer - Buffer into which data needs to be
1423 populated
1424 HID_DATA_DETAILS *pDataDetails - data details extracted from report
1425 descriptor
1426 Return Values:
1427 TRUE - If the required data is retrieved from the report
1428 FALSE - If required data is not found.
1429  
1430 Remarks:
1431 None
1432 *******************************************************************************/
1433 BOOL USBHostHID_ApiImportData(BYTE *report, WORD reportLength, HID_USER_DATA_SIZE *buffer, HID_DATA_DETAILS *pDataDetails)
1434 {
1435 WORD data;
1436 WORD signBit;
1437 WORD mask;
1438 WORD extendMask;
1439 WORD start;
1440 WORD startByte;
1441 WORD startBit;
1442 WORD lastByte;
1443 WORD i;
1444  
1445 // Report must be ok
1446  
1447 if (report == NULL) return FALSE;
1448  
1449 // Must be the right report
1450  
1451 if ((pDataDetails->reportID != 0) && (pDataDetails->reportID != report[0])) return FALSE;
1452  
1453 // Length must be ok
1454  
1455 if (pDataDetails->reportLength != reportLength) return FALSE;
1456 lastByte = (pDataDetails->bitOffset + (pDataDetails->bitLength * pDataDetails->count) - 1)/8;
1457 if (lastByte > reportLength) return FALSE;
1458  
1459 // Extract data one count at a time
1460  
1461 start = pDataDetails->bitOffset;
1462 for (i=0; i<pDataDetails->count; i++) {
1463 startByte = start/8;
1464 startBit = start&7;
1465 lastByte = (start + pDataDetails->bitLength - 1)/8;
1466  
1467 // Pick up the data bytes backwards
1468  
1469 data = 0;
1470 do {
1471 data <<= 8;
1472 data |= (int) report[lastByte];
1473 }
1474 while (lastByte-- > startByte);
1475  
1476 // Shift to the right to byte align the least significant bit
1477  
1478 if (startBit > 0) data >>= startBit;
1479  
1480 // Done if 16 bits long
1481  
1482 if (pDataDetails->bitLength < 16) {
1483  
1484 // Mask off the other bits
1485  
1486 mask = 1 << pDataDetails->bitLength;
1487 mask--;
1488 data &= mask;
1489  
1490 // Sign extend the report item
1491  
1492 if (pDataDetails->signExtend) {
1493 signBit = 1;
1494 if (pDataDetails->bitLength > 1) signBit <<= (pDataDetails->bitLength-1);
1495 extendMask = (signBit << 1) - 1;
1496 if ((data & signBit)==0) data &= extendMask;
1497 else data |= ~extendMask;
1498 }
1499 }
1500  
1501 // Save the value
1502  
1503 *buffer++ = data;
1504  
1505 // Next one
1506  
1507 start += pDataDetails->bitLength;
1508 }
1509 return TRUE;
1510 }
1511  
1512  
1513 // *****************************************************************************
1514 // *****************************************************************************
1515 // Section: Host Stack Interface Functions
1516 // *****************************************************************************
1517 // *****************************************************************************
1518  
1519 /*******************************************************************************
1520 Function:
1521 BOOL USBHostHIDEventHandler( BYTE address, USB_EVENT event,
1522 void *data, DWORD size )
1523  
1524 Precondition:
1525 The device has been initialized.
1526  
1527 Summary:
1528 This function is the event handler for this client driver.
1529  
1530 Description:
1531 This function is the event handler for this client driver. It is called
1532 by the host layer when various events occur.
1533  
1534 Parameters:
1535 BYTE address - Address of the device
1536 USB_EVENT event - Event that has occurred
1537 void *data - Pointer to data pertinent to the event
1538 DWORD size - Size of the data
1539  
1540 Return Values:
1541 TRUE - Event was handled
1542 FALSE - Event was not handled
1543  
1544 Remarks:
1545 None
1546 *******************************************************************************/
1547 BOOL USBHostHIDEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size )
1548 {
1549 BYTE i;
1550 switch (event)
1551 {
1552 case EVENT_NONE: // No event occured (NULL event)
1553 return TRUE;
1554 break;
1555  
1556 case EVENT_DETACH: // USB cable has been detached (data: BYTE, address of device)
1557 #ifdef DEBUG_MODE
1558 UART2PrintString( "HID: Detach\r\n" );
1559 #endif
1560 // Find the device in the table. If found, clear the important fields.
1561 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != address); i++);
1562 if (i < USB_MAX_HID_DEVICES)
1563 {
1564 // Notify that application that the device has been detached.
1565 USB_HOST_APP_EVENT_HANDLER( address, EVENT_HID_DETACH,
1566 &deviceInfoHID[i].ID.deviceAddress, sizeof(BYTE) );
1567  
1568 /* Free the memory used by the HID device */
1569 _USBHostHID_FreeRptDecriptorDataMem(address);
1570 deviceInfoHID[i].ID.deviceAddress = 0;
1571 deviceInfoHID[i].state = STATE_DETACHED;
1572 }
1573  
1574 return TRUE;
1575 break;
1576  
1577 case EVENT_TRANSFER: // A USB transfer has completed
1578 #if defined( USB_ENABLE_TRANSFER_EVENT )
1579 #ifdef DEBUG_MODE
1580 UART2PrintString( "HID: transfer event\r\n" );
1581 #endif
1582  
1583 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != address); i++) {}
1584 if (i == USB_MAX_HID_DEVICES)
1585 {
1586 #ifdef DEBUG_MODE
1587 UART2PrintString( "HID: Unknown device\r\n" );
1588 #endif
1589 return FALSE;
1590 }
1591 #ifdef DEBUG_MODE
1592 // UART2PrintString( "HID: Device state: " );
1593 // UART2PutHex( deviceInfoHID[i].state );
1594 // UART2PrintString( "\r\n" );
1595 #endif
1596  
1597 switch (deviceInfoHID[i].state)
1598 {
1599 case STATE_WAIT_FOR_REPORT_DSC:
1600 #ifdef DEBUG_MODE
1601 UART2PrintString( "HID: Got Report Descriptor\r\n" );
1602 #endif
1603 deviceInfoHID[i].bytesTransferred = ((HOST_TRANSFER_DATA *)data)->dataCount;
1604 if ((!((HOST_TRANSFER_DATA *)data)->bErrorCode) && (deviceInfoHID[i].bytesTransferred == pCurrInterfaceDetails->sizeOfRptDescriptor ))
1605 {
1606 /* Invoke HID Parser ,, validate for all the errors in report Descriptor */
1607 // deviceInfoHID[i].bytesTransferred = ((HOST_TRANSFER_DATA *)data)->dataCount;
1608 deviceInfoHID[i].HIDparserError = _USBHostHID_Parse_Report((BYTE*)deviceInfoHID[i].rptDescriptor , (WORD)pCurrInterfaceDetails->sizeOfRptDescriptor,
1609 (WORD)pCurrInterfaceDetails->endpointPollInterval, pCurrInterfaceDetails->interfaceNumber);
1610  
1611 if(deviceInfoHID[i].HIDparserError)
1612 {
1613 /* Report Descriptor is flawed , flag error and free memory ,
1614 retry by requesting again */
1615 #ifdef DEBUG_MODE
1616 UART2PrintString("\r\nHID Error Reported : ");
1617 UART2PutHex(deviceInfoHID[i].HIDparserError);
1618 #endif
1619  
1620 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
1621 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
1622 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
1623 }
1624 else
1625 {
1626 /* Inform Application layer of new device attached */
1627 #ifdef DEBUG_MODE
1628 UART2PrintString( "HID: Sending Report Descriptor Parsed event\r\n" );
1629 #endif
1630 if (USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RPT_DESC_PARSED, NULL, 0 ))
1631 {
1632 deviceInfoHID[i].flags.breportDataCollected = 1;
1633 }
1634 else
1635 {
1636 if ((pCurrInterfaceDetails->interfaceNumber == (deviceInfoHID[i].noOfInterfaces-1)) &&
1637 (deviceInfoHID[i].flags.breportDataCollected == 0))
1638 {
1639 #ifdef DEBUG_MODE
1640 UART2PrintString( "HID: Error parsing descriptor\r\n" );
1641 #endif
1642 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
1643 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
1644 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
1645 }
1646 }
1647 USB_FREE_AND_CLEAR(deviceInfoHID[i].rptDescriptor);
1648 pCurrInterfaceDetails = pCurrInterfaceDetails->next;
1649  
1650 if(pCurrInterfaceDetails != NULL)
1651 {
1652 if(pCurrInterfaceDetails->sizeOfRptDescriptor !=0)
1653 {
1654 if((deviceInfoHID[i].rptDescriptor = (BYTE *)USB_MALLOC(pCurrInterfaceDetails->sizeOfRptDescriptor)) == NULL)
1655 {
1656 #ifdef DEBUG_MODE
1657 UART2PrintString( "HID: Out of memory\r\n" );
1658 #endif
1659 _USBHostHID_LockDevice( USB_MEMORY_ALLOCATION_ERROR );
1660 break;
1661 }
1662 }
1663 if(USBHostIssueDeviceRequest( deviceInfoHID[i].ID.deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_INTERFACE,
1664 USB_REQUEST_GET_DESCRIPTOR, DSC_RPT, pCurrInterfaceDetails->interfaceNumber, pCurrInterfaceDetails->sizeOfRptDescriptor, deviceInfoHID[i].rptDescriptor,
1665 USB_DEVICE_REQUEST_GET, deviceInfoHID[i].ID.clientDriverID ))
1666 {
1667 #ifdef DEBUG_MODE
1668 UART2PrintString( "HID: Error getting descriptor\r\n" );
1669 #endif
1670 USB_FREE_AND_CLEAR(deviceInfoHID[i].rptDescriptor);
1671 break;
1672 }
1673 deviceInfoHID[i].state = STATE_WAIT_FOR_REPORT_DSC;
1674 }
1675 else
1676 {
1677 if(deviceInfoHID[i].flags.breportDataCollected == 0)
1678 {
1679 #ifdef DEBUG_MODE
1680 UART2PrintString( "HID: Problem collecting report data\r\n" );
1681 #endif
1682 _USBHostHID_FreeRptDecriptorDataMem(deviceInfoHID[i].ID.deviceAddress);
1683 _USBHostHID_LockDevice( USB_HID_REPORT_DESCRIPTOR_BAD );
1684 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_BAD_REPORT_DESCRIPTOR, NULL, 0 );
1685 }
1686 else
1687 {
1688 #ifdef DEBUG_MODE
1689 UART2PrintString( "HID: Proceeding to run state\r\n" );
1690 #endif
1691 deviceInfoHID[i].state = STATE_RUNNING;
1692  
1693 // Tell the application layer that we have a device.
1694 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_ATTACH, &(deviceInfoHID[i].ID), sizeof(USB_HID_DEVICE_ID) );
1695 }
1696 }
1697 }
1698 }
1699 else
1700 {
1701 // Assuming only a STALL here. Since it is EP0, we do not have to clear the stall.
1702 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, 0 );
1703 }
1704 break;
1705  
1706 case STATE_RUNNING:
1707 // These will be events from application issued requests. Just pass them up.
1708 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1709 transferEventData.dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount;
1710 transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1711 if (((HOST_TRANSFER_DATA *)data)->bEndpointAddress & 0x80)
1712 {
1713 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_READ_DONE, &transferEventData, sizeof(HID_TRANSFER_DATA) );
1714 }
1715 else
1716 {
1717 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_WRITE_DONE, &transferEventData, sizeof(HID_TRANSFER_DATA) );
1718 }
1719 #endif
1720 break;
1721  
1722 case STATE_READ_REQ_WAIT:
1723 #ifdef DEBUG_MODE
1724 UART2PrintString( "HID: Read Event\r\n" );
1725 #endif
1726 if (((HOST_TRANSFER_DATA *)data)->bErrorCode)
1727 {
1728 if (USB_ENDPOINT_STALLED == ((HOST_TRANSFER_DATA *)data)->bErrorCode)
1729 {
1730 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1731 deviceInfoHID[i].returnState = STATE_RUNNING;
1732 deviceInfoHID[i].flags.bfReset = 1;
1733 _USBHostHID_ResetStateJump( i );
1734 }
1735 else
1736 {
1737 /* Set proper error code as per HID guideline */
1738 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1739 transferEventData.dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount;
1740 transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1741 #endif
1742 _USBHostHID_TerminateReadTransfer(((HOST_TRANSFER_DATA *)data)->bErrorCode);
1743 }
1744 }
1745 else
1746 {
1747 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1748 deviceInfoHID[i].bytesTransferred = ((HOST_TRANSFER_DATA *)data)->dataCount; /* Can compare with report size and flag error ???*/
1749 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1750 transferEventData.dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount;
1751 transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1752 #endif
1753 _USBHostHID_TerminateReadTransfer(USB_SUCCESS);
1754 return TRUE;
1755 }
1756 break;
1757  
1758 case STATE_WRITE_REQ_WAIT:
1759 #ifdef DEBUG_MODE
1760 UART2PrintString( "HID: Write Event\r\n" );
1761 #endif
1762 if (((HOST_TRANSFER_DATA *)data)->bErrorCode)
1763 {
1764 if (USB_ENDPOINT_STALLED == ((HOST_TRANSFER_DATA *)data)->bErrorCode)
1765 {
1766 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1767 deviceInfoHID[i].returnState = STATE_RUNNING ;
1768 deviceInfoHID[i].flags.bfReset = 1;
1769 _USBHostHID_ResetStateJump( i );
1770 }
1771 else
1772 {
1773 /* Set proper error code as per HID guideline */
1774 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1775 transferEventData.dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount;
1776 transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1777 #endif
1778 _USBHostHID_TerminateWriteTransfer(((HOST_TRANSFER_DATA *)data)->bErrorCode);
1779 }
1780 }
1781 else
1782 {
1783 USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1784 deviceInfoHID[i].bytesTransferred = ((HOST_TRANSFER_DATA *)data)->dataCount; /* Can compare with report size and flag error ???*/
1785 #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1786 transferEventData.dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount;
1787 transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1788 #endif
1789 _USBHostHID_TerminateWriteTransfer(USB_SUCCESS);
1790 return TRUE;
1791 }
1792 break;
1793  
1794 case STATE_WAIT_FOR_RESET:
1795 #ifdef DEBUG_MODE
1796 UART2PrintString( "HID: Reset Event\r\n" );
1797 #endif
1798 deviceInfoHID[i].errorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1799 deviceInfoHID[i].flags.bfReset = 0;
1800 _USBHostHID_ResetStateJump( i );
1801 return TRUE;
1802 break;
1803  
1804 case STATE_HOLDING:
1805 return TRUE;
1806 break;
1807  
1808 default:
1809 return FALSE;
1810 }
1811 #endif
1812  
1813 // This addition has not been fully tested yet. It may be included in the future.
1814 // #if defined( USB_ENABLE_TRANSFER_EVENT )
1815 // case EVENT_BUS_ERROR:
1816 // // We will get this error if we have a problem, like too many NAK's
1817 // // on a write, so we know the write failed.
1818 // switch (deviceInfoHID[i].state)
1819 // {
1820 // case STATE_READ_REQ_WAIT:
1821 // USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1822 // deviceInfoHID[i].bytesTransferred = 0;
1823 // #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1824 // transferEventData.dataCount = 0;
1825 // transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1826 // #endif
1827 // _USBHostHID_TerminateReadTransfer( ((HOST_TRANSFER_DATA *)data)->bErrorCode );
1828 // return TRUE;
1829 // break;
1830 //
1831 // case STATE_WRITE_REQ_WAIT:
1832 // USBHostClearEndpointErrors( deviceInfoHID[i].ID.deviceAddress, deviceInfoHID[i].endpointDATA );
1833 // deviceInfoHID[i].bytesTransferred = 0;
1834 // #ifdef USB_HID_ENABLE_TRANSFER_EVENT
1835 // transferEventData.dataCount = 0;
1836 // transferEventData.bErrorCode = ((HOST_TRANSFER_DATA *)data)->bErrorCode;
1837 // #endif
1838 // _USBHostHID_TerminateWriteTransfer( ((HOST_TRANSFER_DATA *)data)->bErrorCode );
1839 // return TRUE;
1840 // break;
1841 // }
1842 // break;
1843 // #endif
1844  
1845 case EVENT_SOF: // Start of frame - NOT NEEDED
1846 case EVENT_RESUME: // Device-mode resume received
1847 case EVENT_SUSPEND: // Device-mode suspend/idle event received
1848 case EVENT_RESET: // Device-mode bus reset received
1849 case EVENT_STALL: // A stall has occured
1850 return TRUE;
1851 break;
1852  
1853 default:
1854 return FALSE;
1855 break;
1856 }
1857 return FALSE;
1858 }
1859  
1860 /*******************************************************************************
1861 Function:
1862 BOOL USBHostHIDInitialize( BYTE address, DWORD flags, BYTE clientDriverID )
1863  
1864 Summary:
1865 This function is the initialization routine for this client driver.
1866  
1867 Description:
1868 This function is the initialization routine for this client driver. It
1869 is called by the host layer when the USB device is being enumerated.For a
1870 HID device we need to look into HID descriptor, interface descriptor and
1871 endpoint descriptor.
1872  
1873 Precondition:
1874 None
1875  
1876 Parameters:
1877 BYTE address - Address of the new device
1878 DWORD flags - Initialization flags
1879 BYTE clientDriverID - Client driver identification for device requests
1880  
1881 Return Values:
1882 TRUE - We can support the device.
1883 FALSE - We cannot support the device.
1884  
1885 Remarks:
1886 None
1887 *******************************************************************************/
1888 BOOL USBHostHIDInitialize( BYTE address, DWORD flags, BYTE clientDriverID )
1889 {
1890 BYTE *descriptor;
1891 BOOL validConfiguration = FALSE;
1892 BOOL rptDescriptorfound = FALSE;
1893 WORD i;
1894 BYTE endpointIN = 0;
1895 BYTE endpointOUT = 0;
1896 BYTE device;
1897 BYTE numofinterfaces = 0;
1898 BYTE temp_i = 0;
1899 USB_HID_INTERFACE_DETAILS *pNewInterfaceDetails = NULL;
1900  
1901 #ifdef DEBUG_MODE
1902 UART2PrintString( "HID: USBHostHIDInitialize(0x" );
1903 UART2PutHex( flags );
1904 UART2PrintString( ")\r\n" );
1905 #endif
1906  
1907 // Find the device in the table. If it's there, we have already initialized this device.
1908 for (device = 0; (device < USB_MAX_HID_DEVICES) ; device++)
1909 {
1910 if(deviceInfoHID[device].ID.deviceAddress == address)
1911 return TRUE;
1912 }
1913  
1914 // See if we have room for another device
1915 for (device = 0; (device < USB_MAX_HID_DEVICES) && (deviceInfoHID[device].ID.deviceAddress != 0); device++);
1916 if (device == USB_MAX_HID_DEVICES)
1917 {
1918 #ifdef DEBUG_MODE
1919 UART2PrintString("HID: Out of space\r\n" );
1920 #endif
1921 return FALSE;
1922 }
1923  
1924 // Fill in the VID, PID, and client driver ID. They are not not valid unless deviceAddress is non-zero.
1925 descriptor = USBHostGetDeviceDescriptor( address );
1926 deviceInfoHID[device].ID.vid = ((USB_DEVICE_DESCRIPTOR *)descriptor)->idVendor;
1927 deviceInfoHID[device].ID.pid = ((USB_DEVICE_DESCRIPTOR *)descriptor)->idProduct;
1928 deviceInfoHID[device].ID.clientDriverID = clientDriverID;
1929  
1930 // Get ready to parse the configuration descriptor.
1931 descriptor = USBHostGetCurrentConfigurationDescriptor( address );
1932  
1933 // pCurrInterfaceDetails = pInterfaceDetails;
1934 i = 0;
1935  
1936 #ifdef DEBUG_MODE
1937 UART2PrintString("HID: Checking descriptor " );
1938 UART2PutDec( descriptor[i+5] );
1939 UART2PrintString(" ...\r\n" );
1940 #endif
1941  
1942 // Total no of interfaces
1943 deviceInfoHID[device].noOfInterfaces = descriptor[i+4] ;
1944  
1945 // Set current configuration to this configuration. We can change it later.
1946  
1947 // TODO Check power requirement
1948  
1949 // Find the next interface descriptor.
1950 while (i < ((USB_CONFIGURATION_DESCRIPTOR *)descriptor)->wTotalLength)
1951 {
1952 #ifdef DEBUG_MODE
1953 UART2PrintString("HID: Checking interface...\r\n" );
1954 #endif
1955 // See if we are pointing to an interface descriptor.
1956 if (descriptor[i+1] == USB_DESCRIPTOR_INTERFACE)
1957 {
1958 // See if the interface is a HID interface.
1959 if ((descriptor[i+5] == DEVICE_CLASS_HID)||(descriptor[i+5] == 0))
1960 {
1961 deviceInfoHID[device].driverSupported = 1;
1962 if (deviceInfoHID[device].driverSupported)
1963 {
1964 if ((pNewInterfaceDetails = (USB_HID_INTERFACE_DETAILS*)USB_MALLOC(sizeof(USB_HID_INTERFACE_DETAILS))) == NULL)
1965 {
1966 #ifdef DEBUG_MODE
1967 UART2PrintString("HID: Out of memory for interface details\r\n" );
1968 #endif
1969 return FALSE;
1970 }
1971 numofinterfaces ++ ;
1972  
1973 // Create new entry into interface list
1974 if(pInterfaceDetails == NULL)
1975 {
1976 pInterfaceDetails = pNewInterfaceDetails;
1977 pCurrInterfaceDetails = pNewInterfaceDetails;
1978 pInterfaceDetails->next = NULL;
1979 }
1980 else
1981 {
1982 pCurrInterfaceDetails->next = pNewInterfaceDetails;
1983 pCurrInterfaceDetails = pNewInterfaceDetails;
1984 pCurrInterfaceDetails->next = NULL;
1985 }
1986  
1987 // USB_FREE_AND_CLEAR( pNewInterfaceDetails );
1988 pCurrInterfaceDetails->interfaceNumber = descriptor[i+2];
1989  
1990 // Scan for hid descriptors.
1991 i += descriptor[i];
1992 if(descriptor[i+1] == DSC_HID)
1993 {
1994 if(descriptor[i+5] == 0)
1995 {
1996 // At least one report descriptor must be present - flag error
1997 #ifdef DEBUG_MODE
1998 UART2PrintString("HID: No report descriptor\r\n" );
1999 #endif
2000 rptDescriptorfound = FALSE;
2001 }
2002 else
2003 {
2004 rptDescriptorfound = TRUE;
2005 pCurrInterfaceDetails->sizeOfRptDescriptor = ((descriptor[i+7]) |
2006 ((descriptor[i+8]) << 8));
2007  
2008 // Look for IN and OUT endpoints.
2009 // TODO what if there are no endpoints?
2010 endpointIN = 0;
2011 endpointOUT = 0;
2012 temp_i = 0;
2013 // Scan for endpoint descriptors.
2014 i += descriptor[i];
2015 while(descriptor[i+1] == USB_DESCRIPTOR_ENDPOINT)
2016 {
2017 if (descriptor[i+3] == 0x03) // Interrupt
2018 {
2019 if (((descriptor[i+2] & 0x80) == 0x80) && (endpointIN == 0))
2020 {
2021 endpointIN = descriptor[i+2];
2022 }
2023 if (((descriptor[i+2] & 0x80) == 0x00) && (endpointOUT == 0))
2024 {
2025 endpointOUT = descriptor[i+2];
2026 }
2027 }
2028 temp_i = descriptor[i];
2029 i += descriptor[i];
2030 }
2031  
2032 i -= temp_i;
2033 // if ((endpointIN != 0) || (endpointOUT != 0))
2034 // Some devices use EP0 as the OUT endpoint
2035 if (endpointIN != 0)
2036 {
2037 // Initialize the remaining device information.
2038 #ifdef DEBUG_MODE
2039 UART2PrintString("HID: Valid device info\r\n" );
2040 #endif
2041 deviceInfoHID[device].ID.deviceAddress = address;
2042 //
2043 pCurrInterfaceDetails->endpointIN = endpointIN;
2044 pCurrInterfaceDetails->endpointOUT = endpointOUT;
2045 pCurrInterfaceDetails->endpointMaxDataSize = ((descriptor[i+4]) |
2046 (descriptor[i+5] << 8));
2047 pCurrInterfaceDetails->endpointPollInterval = descriptor[i+6];
2048 validConfiguration = TRUE;
2049  
2050 /* By default NAK time out is disabled for HID class */
2051 /* If timeout is required then pass '1' instead '0' in below function call */
2052 // USBHostSetNAKTimeout( address, endpointIN, 0, USB_NUM_INTERRUPT_NAKS );
2053 // USBHostSetNAKTimeout( address, endpointOUT, 0, USB_NUM_INTERRUPT_NAKS );
2054 }
2055 // i -= temp_i;
2056  
2057 }
2058 }
2059 else
2060 {
2061 /* HID Descriptor not found */
2062 }
2063 }
2064 }
2065 }
2066  
2067 // Jump to the next descriptor in this configuration.
2068 i += descriptor[i];
2069 }
2070  
2071 // This check doesn't account for the fact that some of the interface descriptors are for
2072 // alternate settings of the same interface. It's not really necessary - sometimes
2073 // descriptors have errors in them.
2074 // if(numofinterfaces != deviceInfoHID[device].noOfInterfaces)
2075 // {
2076 // #ifdef DEBUG_MODE
2077 // UART2PrintString("HID: Interface count mismatch\r\n" );
2078 // #endif
2079 // validConfiguration = FALSE;
2080 // }
2081  
2082 if(validConfiguration)
2083 {
2084 #ifndef USB_ENABLE_TRANSFER_EVENT
2085 deviceInfoHID[device].state = STATE_INITIALIZE_DEVICE;
2086 #else
2087 pCurrInterfaceDetails = pInterfaceDetails;
2088 if (pCurrInterfaceDetails->sizeOfRptDescriptor !=0)
2089 {
2090 if (deviceInfoHID[device].rptDescriptor != NULL)
2091 {
2092 USB_FREE_AND_CLEAR( deviceInfoHID[device].rptDescriptor );
2093 }
2094  
2095 if((deviceInfoHID[device].rptDescriptor = (BYTE *)USB_MALLOC(pCurrInterfaceDetails->sizeOfRptDescriptor)) == NULL)
2096 {
2097 #ifdef DEBUG_MODE
2098 UART2PrintString("HID: Out of memory for report descriptor\r\n" );
2099 #endif
2100 return FALSE;
2101 }
2102 }
2103 if (USBHostIssueDeviceRequest( deviceInfoHID[device].ID.deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_INTERFACE,
2104 USB_REQUEST_GET_DESCRIPTOR, DSC_RPT, pCurrInterfaceDetails->interfaceNumber, pCurrInterfaceDetails->sizeOfRptDescriptor, deviceInfoHID[device].rptDescriptor,
2105 USB_DEVICE_REQUEST_GET, deviceInfoHID[device].ID.clientDriverID ))
2106 {
2107 #ifdef DEBUG_MODE
2108 UART2PrintString("HID: Error getting report descriptor\r\n" );
2109 #endif
2110 USB_FREE_AND_CLEAR(deviceInfoHID[device].rptDescriptor);
2111 return FALSE;
2112 }
2113 #ifdef DEBUG_MODE
2114 UART2PrintString("HID: Getting report descriptor\r\n" );
2115 #endif
2116 deviceInfoHID[device].state = STATE_WAIT_FOR_REPORT_DSC;
2117 #endif
2118  
2119 #ifdef DEBUG_MODE
2120 UART2PrintString( "HID: Interrupt endpoint IN: " );
2121 UART2PutHex( endpointIN );
2122 UART2PrintString( " Interrupt endpoint OUT: " );
2123 UART2PutHex( endpointOUT );
2124 UART2PrintString( "\r\n" );
2125 #endif
2126  
2127 return TRUE;
2128 }
2129 else
2130 {
2131 return FALSE;
2132 }
2133 }
2134  
2135  
2136 // *****************************************************************************
2137 // *****************************************************************************
2138 // Internal Functions
2139 // *****************************************************************************
2140 // *****************************************************************************
2141  
2142 /****************************************************************************
2143 Function:
2144 void _USBHostHID_FreeRptDecriptorDataMem(BYTE deviceAddress)
2145  
2146 Summary:
2147  
2148  
2149 Description:
2150 This function is called in case of error is encountered . This function
2151 frees the memory allocated for report descriptor and interface
2152 descriptors.
2153  
2154 Precondition:
2155 None.
2156  
2157 Parameters:
2158 BYTE address - Address of the device
2159  
2160 Returns:
2161 None
2162  
2163 Remarks:
2164 None
2165 ***************************************************************************/
2166 void _USBHostHID_FreeRptDecriptorDataMem(BYTE deviceAddress)
2167 {
2168 USB_HID_INTERFACE_DETAILS* ptempInterface;
2169 BYTE i;
2170  
2171 // Find the device in the table. If found, clear the important fields.
2172 for (i=0; (i<USB_MAX_HID_DEVICES) && (deviceInfoHID[i].ID.deviceAddress != deviceAddress); i++);
2173 if (i < USB_MAX_HID_DEVICES)
2174 {
2175 /* free memory allocated to HID parser */
2176 if(parsedDataMem != NULL)
2177 {
2178 USB_FREE_AND_CLEAR(parsedDataMem); /* will be indexed once multiple device support is added */
2179 }
2180 if(deviceInfoHID[i].rptDescriptor != NULL)
2181 {
2182 USB_FREE_AND_CLEAR(deviceInfoHID[i].rptDescriptor);
2183 }
2184  
2185 /* free memory allocated to report descriptor in deviceInfoHID */
2186 while(pInterfaceDetails != NULL)
2187 {
2188 ptempInterface = pInterfaceDetails->next;
2189 USB_FREE_AND_CLEAR(pInterfaceDetails);
2190 pInterfaceDetails = ptempInterface;
2191 }
2192 }
2193 }
2194  
2195 /*******************************************************************************
2196 Function:
2197 void _USBHostHID_ResetStateJump( BYTE i )
2198  
2199 Summary:
2200  
2201 Description:
2202 This function determines which portion of the reset processing needs to
2203 be executed next and jumps to that state.
2204  
2205 Precondition:
2206 The device information must be in the deviceInfoHID array.
2207  
2208 Parameters:
2209 BYTE i - Index into the deviceInfoHIDMSD structure for the device to reset.
2210  
2211 Returns:
2212 None
2213  
2214 Remarks:
2215 None
2216 *******************************************************************************/
2217 void _USBHostHID_ResetStateJump( BYTE i )
2218 {
2219 BYTE errorCode;
2220  
2221 if (deviceInfoHID[i].flags.bfReset)
2222 {
2223 errorCode = USBHostIssueDeviceRequest( deviceInfoHID[i].ID.deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE,
2224 USB_HID_RESET, 0, deviceInfoHID[i].interface, 0, NULL, USB_DEVICE_REQUEST_SET, deviceInfoHID[i].ID.clientDriverID );
2225  
2226 if (errorCode)
2227 {
2228 deviceInfoHID[i].errorCode = USB_HID_RESET_ERROR;
2229 deviceInfoHID[i].state = STATE_RUNNING;
2230 USB_HOST_APP_EVENT_HANDLER( deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RESET_ERROR, NULL, 0 );
2231 }
2232 else
2233 {
2234 #ifndef USB_ENABLE_TRANSFER_EVENT
2235 deviceInfoHID[i].state = STATE_HID_RESET_RECOVERY | SUBSTATE_WAIT_FOR_RESET;
2236 #else
2237 deviceInfoHID[i].state = STATE_WAIT_FOR_RESET;
2238 #endif
2239 }
2240 }
2241 else
2242 {
2243 if (!deviceInfoHID[i].errorCode)
2244 {
2245 USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RESET, NULL, 0 );
2246 }
2247 else
2248 {
2249 USB_HOST_APP_EVENT_HANDLER(deviceInfoHID[i].ID.deviceAddress, EVENT_HID_RESET_ERROR, NULL, 0 );
2250 }
2251  
2252 deviceInfoHID[i].state = deviceInfoHID[i].returnState;
2253 }
2254 }
2255  
2256  
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3