Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbhid.h Source File

usbhid.h

00001 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
00002 
00003 This software may be distributed and modified under the terms of the GNU
00004 General Public License version 2 (GPL2) as published by the Free Software
00005 Foundation and appearing in the file GPL2.TXT included in the packaging of
00006 this file. Please note that GPL2 Section 2[b] requires that all works based
00007 on this software must also be made publicly available under the terms of
00008 the GPL2 ("Copyleft").
00009 
00010 Contact information
00011 -------------------
00012 
00013 Circuits At Home, LTD
00014 Web      :  http://www.circuitsathome.com
00015 e-mail   :  support@circuitsathome.com
00016  */
00017 #if !defined(__USBHID_H__)
00018 #define __USBHID_H__
00019 
00020 #include "Usb.h"
00021 #include "hidusagestr.h"
00022 
00023 #define MAX_REPORT_PARSERS                      2
00024 #define HID_MAX_HID_CLASS_DESCRIPTORS           5
00025 
00026 #define DATA_SIZE_MASK                          0x03
00027 #define TYPE_MASK                               0x0C
00028 #define TAG_MASK                                0xF0
00029 
00030 #define DATA_SIZE_0                             0x00
00031 #define DATA_SIZE_1                             0x01
00032 #define DATA_SIZE_2                             0x02
00033 #define DATA_SIZE_4                             0x03
00034 
00035 #define TYPE_MAIN                               0x00
00036 #define TYPE_GLOBAL                             0x04
00037 #define TYPE_LOCAL                              0x08
00038 
00039 #define TAG_MAIN_INPUT                          0x80
00040 #define TAG_MAIN_OUTPUT                         0x90
00041 #define TAG_MAIN_COLLECTION                     0xA0
00042 #define TAG_MAIN_FEATURE                        0xB0
00043 #define TAG_MAIN_ENDCOLLECTION                  0xC0
00044 
00045 #define TAG_GLOBAL_USAGEPAGE                    0x00
00046 #define TAG_GLOBAL_LOGICALMIN                   0x10
00047 #define TAG_GLOBAL_LOGICALMAX                   0x20
00048 #define TAG_GLOBAL_PHYSMIN                      0x30
00049 #define TAG_GLOBAL_PHYSMAX                      0x40
00050 #define TAG_GLOBAL_UNITEXP                      0x50
00051 #define TAG_GLOBAL_UNIT                         0x60
00052 #define TAG_GLOBAL_REPORTSIZE                   0x70
00053 #define TAG_GLOBAL_REPORTID                     0x80
00054 #define TAG_GLOBAL_REPORTCOUNT                  0x90
00055 #define TAG_GLOBAL_PUSH                         0xA0
00056 #define TAG_GLOBAL_POP                          0xB0
00057 
00058 #define TAG_LOCAL_USAGE                         0x00
00059 #define TAG_LOCAL_USAGEMIN                      0x10
00060 #define TAG_LOCAL_USAGEMAX                      0x20
00061 
00062 /* HID requests */
00063 #define bmREQ_HID_OUT                           USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00064 #define bmREQ_HID_IN                            USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
00065 #define bmREQ_HID_REPORT                        USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
00066 
00067 /* HID constants. Not part of chapter 9 */
00068 /* Class-Specific Requests */
00069 #define HID_REQUEST_GET_REPORT                  0x01
00070 #define HID_REQUEST_GET_IDLE                    0x02
00071 #define HID_REQUEST_GET_PROTOCOL                0x03
00072 #define HID_REQUEST_SET_REPORT                  0x09
00073 #define HID_REQUEST_SET_IDLE                    0x0A
00074 #define HID_REQUEST_SET_PROTOCOL                0x0B
00075 
00076 /* Class Descriptor Types */
00077 #define HID_DESCRIPTOR_HID                      0x21
00078 #define HID_DESCRIPTOR_REPORT                   0x22
00079 #define HID_DESRIPTOR_PHY                       0x23
00080 
00081 /* Protocol Selection */
00082 #define USB_HID_BOOT_PROTOCOL                   0x00
00083 #define HID_RPT_PROTOCOL                        0x01
00084 
00085 /* HID Interface Class Code */
00086 #define HID_INTF                                0x03
00087 
00088 /* HID Interface Class SubClass Codes */
00089 #define HID_BOOT_INTF_SUBCLASS                  0x01
00090 
00091 /* HID Interface Class Protocol Codes */
00092 #define USB_HID_PROTOCOL_NONE                       0x00
00093 #define USB_HID_PROTOCOL_KEYBOARD                   0x01
00094 #define USB_HID_PROTOCOL_MOUSE                      0x02
00095 
00096 #define HID_ITEM_TYPE_MAIN                      0
00097 #define HID_ITEM_TYPE_GLOBAL                    1
00098 #define HID_ITEM_TYPE_LOCAL                     2
00099 #define HID_ITEM_TYPE_RESERVED                  3
00100 
00101 #define HID_LONG_ITEM_PREFIX                    0xfe    // Long item prefix value
00102 
00103 #define bmHID_MAIN_ITEM_TAG                     0xfc    // Main item tag mask
00104 
00105 #define bmHID_MAIN_ITEM_INPUT                   0x80    // Main item Input tag value
00106 #define bmHID_MAIN_ITEM_OUTPUT                  0x90    // Main item Output tag value
00107 #define bmHID_MAIN_ITEM_FEATURE                 0xb0    // Main item Feature tag value
00108 #define bmHID_MAIN_ITEM_COLLECTION              0xa0    // Main item Collection tag value
00109 #define bmHID_MAIN_ITEM_END_COLLECTION          0xce    // Main item End Collection tag value
00110 
00111 #define HID_MAIN_ITEM_COLLECTION_PHYSICAL       0
00112 #define HID_MAIN_ITEM_COLLECTION_APPLICATION    1
00113 #define HID_MAIN_ITEM_COLLECTION_LOGICAL        2
00114 #define HID_MAIN_ITEM_COLLECTION_REPORT         3
00115 #define HID_MAIN_ITEM_COLLECTION_NAMED_ARRAY    4
00116 #define HID_MAIN_ITEM_COLLECTION_USAGE_SWITCH   5
00117 #define HID_MAIN_ITEM_COLLECTION_USAGE_MODIFIER 6
00118 
00119 struct HidItemPrefix {
00120         uint8_t bSize : 2;
00121         uint8_t bType : 2;
00122         uint8_t bTag : 4;
00123 };
00124 
00125 struct MainItemIOFeature {
00126         uint8_t bmIsConstantOrData : 1;
00127         uint8_t bmIsArrayOrVariable : 1;
00128         uint8_t bmIsRelativeOrAbsolute : 1;
00129         uint8_t bmIsWrapOrNoWrap : 1;
00130         uint8_t bmIsNonLonearOrLinear : 1;
00131         uint8_t bmIsNoPreferedOrPrefered : 1;
00132         uint8_t bmIsNullOrNoNull : 1;
00133         uint8_t bmIsVolatileOrNonVolatile : 1;
00134 };
00135 
00136 class USBHID;
00137 
00138 class HIDReportParser {
00139 public:
00140         virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) = 0;
00141 };
00142 
00143 class USBHID : public USBDeviceConfig, public UsbConfigXtracter {
00144 protected:
00145         Usb *pUsb; // USB class instance pointer
00146         uint8_t bAddress; // address
00147 
00148 protected:
00149         static const uint8_t epInterruptInIndex = 1; // InterruptIN  endpoint index
00150         static const uint8_t epInterruptOutIndex = 2; // InterruptOUT endpoint index
00151 
00152         static const uint8_t maxHidInterfaces = 3;
00153         static const uint8_t maxEpPerInterface = 2;
00154         static const uint8_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1); // We need to make room for the control endpoint
00155 
00156         void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
00157         void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
00158 
00159         virtual HIDReportParser* GetReportParser(uint8_t id __attribute__((unused))) {
00160                 return NULL;
00161         };
00162 
00163 public:
00164 
00165         USBHID(Usb *pusb) : pUsb(pusb) {
00166         };
00167 
00168         const Usb* GetUsb() {
00169                 return pUsb;
00170         };
00171 
00172         virtual bool SetReportParser(uint8_t id __attribute__((unused)), HIDReportParser *prs __attribute__((unused))) {
00173                 return false;
00174         };
00175 
00176         uint8_t SetProtocol(uint8_t iface, uint8_t protocol);
00177         uint8_t GetProtocol(uint8_t iface, uint8_t* dataptr);
00178         uint8_t GetIdle(uint8_t iface, uint8_t reportID, uint8_t* dataptr);
00179         uint8_t SetIdle(uint8_t iface, uint8_t reportID, uint8_t duration);
00180 
00181         uint8_t GetReportDescr(uint16_t wIndex, USBReadParser *parser = NULL);
00182 
00183         uint8_t GetHidDescr(uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
00184         uint8_t GetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
00185         uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
00186 };
00187 
00188 #endif // __USBHID_H__