1 /*
2 ********************************************************************************
3 *                                USB Hid Driver
4 *
5 *                (c) Copyright 2006-2010, All winners Co,Ld.
6 *                        All Right Reserved
7 *
8 * FileName      :  Hid_i.h
9 *
10 * Author        :  Javen
11 *
12 * Date          :  2010/06/02
13 *
14 * Description   :  Hid Driver中对USB接口设备的描述
15 *
16 * Others        :  NULL
17 *
18 * History:
19 *       <time>          <author>     <version >     <desc>
20 *      2010.06.02       Javen           1.0         build this file
21 *
22 ********************************************************************************
23 */
24 #ifndef  __HID_I_H__
25 #define  __HID_I_H__
26 #include <hal_osal.h>
27 
28 //---------------------------------------------------------
29 //  预定义
30 //---------------------------------------------------------
31 struct _HidRequest;
32 struct _HidDev;
33 struct _usbHidReport;
34 
35 //----------------------------------------------------------------------
36 //
37 //
38 //----------------------------------------------------------------------
39 /* input, output, feature */
40 #define USB_HID_MAX_FIELDS      64
41 typedef struct _usbHidField{
42     /* Field用途 */
43     unsigned int physical;              /* physical usage for this field                    */
44     unsigned int logical;               /* logical usage for this field                     */
45     unsigned int application;           /* application usage for this field                 */
46     usbHidUsage_t *usage;       /* usage table for this function                    */
47     unsigned int maxusage;              /* maximum usage index                              */
48     unsigned int flags;             /* main-item flags (i.e. volatile,array,constant)   */
49     unsigned int report_offset;     /* bit offset in the report                         */
50     unsigned int report_size;           /* size of this field in the report                 */
51     unsigned int report_count;          /* number of this field in the report               */
52     unsigned int report_type;           /* (input,output,feature)                           */
53     unsigned int *value;                /* last known value(s)                              */
54     int logical_minimum;        /* 最小逻辑值                                        */
55     int logical_maximum;        /* 最大逻辑值                                        */
56     int physical_minimum;       /* 最小物理值                                        */
57     int physical_maximum;       /* 最大物理值                                        */
58     int unit_exponent;      /* 单位指数                                         */
59     unsigned int unit;                  /* 单位                                           */
60 
61     /* Field属性 */
62     unsigned int Index;             /* ndex into report->field[]                        */
63     struct _usbHidReport *HidReport; /* field 所属的 HID report                    */
64 }usbHidField_t;
65 
66 #define  USB_HID_REPORT_TYPES       3   /* 报告的种类        */
67 #define  USB_HID_REPORT_MAX_NUM     256 /* 报告的最大个数  */
68 
69 /* 设备报告定义,有input, output, feature等3种 */
70 typedef struct _usbHidReport{
71     unsigned int Id;                                    /* id of this report            */
72     unsigned int Type;                                  /* report type,                 */
73 
74     unsigned int Maxfield;                              /* maximum valid field index    */
75     usbHidField_t *Field[USB_HID_MAX_FIELDS];   /* fields of the report         */
76 
77     unsigned int Size;                                  /* size of the report (bits)    */
78 }usbHidReport_t;
79 
80 /* 设备的所有报告 */
81 typedef struct _usbHidReportEnum{
82     unsigned int numbered;   /* reprot是否存在 */
83 
84     unsigned int ReportNum;  /* 有效的Report的个数 */
85     usbHidReport_t *Report[USB_HID_REPORT_MAX_NUM];
86 }usbHidReportEnum_t;
87 
88 #define  USB_HID_GLOBAL_STACK_SIZE          4
89 #define  USB_HID_COLLECTION_STACK_SIZE      4
90 typedef struct _usbHidParser {
91     usbHidGlobalItems_t global;
92     usbHidGlobalItems_t global_stack[USB_HID_GLOBAL_STACK_SIZE];
93     unsigned int  global_stack_ptr;
94 
95     usbHidLocalItems_t local;
96 
97     unsigned int collection_stack[USB_HID_COLLECTION_STACK_SIZE];
98     unsigned int collection_stack_ptr;
99 
100     struct _HidDev *HidDev;
101 }usbHidParser_t;
102 
103 
104 /* hid事件在hid DATA里的偏移量 */
105 typedef struct _usbHidEvnetExcursion{
106     unsigned int BitOffset;
107     unsigned int BitCount;
108 }usbHidEvnetExcursion_t;
109 
110 //---------------------------------------------------------
111 //
112 //---------------------------------------------------------
113 
114 /* Hid device state */
115 typedef enum _HidDev_state{
116     HID_DEV_OFFLINE= 0,         /* HidDev已经拔出       */
117     HID_DEV_ONLINE,             /* HidDev已经添加       */
118     HID_DEV_DIED,               /* HidDev不可用        */
119     HID_DEV_RESET               /* HidDev正在被reset   */
120 }HidDev_State_t;
121 
122 /* USB Hid device type */
123 //#define  USB_HID_DEVICE_TYPE_UNKOWN       0x00    /* 未知设备 */
124 //#define  USB_HID_DEVICE_TYPE_KEYBOARD 0x01    /* 键盘   */
125 //#define  USB_HID_DEVICE_TYPE_MOUSE        0x02    /* 鼠标   */
126 
127 typedef int (* Hid_SoftReset)(struct _HidDev *HidDev);
128 typedef int (* Hid_ResetRecovery)(struct _HidDev *HidDev);
129 typedef int (* Hid_Transport)(struct _HidDev *HidDev, struct _HidRequest *HidReq);
130 typedef int (* Hid_StopTransport)(struct _HidDev *HidDev);
131 
132 typedef int (* HidClientProbe)(struct _HidDev *);
133 typedef int (* HidClientRemove)(struct _HidDev *);
134 
135 /* 描述USB接口的信息 */
136 typedef struct _HidDev{
137     struct usb_host_virt_dev *pusb_dev;     /* mscDev 对应的Public USB Device  */
138     struct usb_interface     *pusb_intf;    /* Public usb interface             */
139 
140     /* device information */
141     unsigned char InterfaceNo;                      /* 接口号                          */
142     unsigned char SubClass;                             /* 子类                           */
143     unsigned char Protocol;                             /* 传输协议                         */
144     unsigned int DevType;                           /* 设备类型                         */
145     unsigned int DevNo;                             /* 设备在 hid 管理中的编号           */
146 
147     /* device manager */
148     HidDev_State_t State;                   /* Dev当前所处的连接状态             */
149 
150     unsigned char *ReportDesc;                      /* 装载这Hid设备的report描述符       */
151     unsigned int ReportSize;                        /* report描述符的大小                 */
152 
153     usbHidCollectionItems_t *collection;        /* List of HID collections              */
154     unsigned collection_size;                   /* Number of allocated hid_collections  */
155     unsigned maxcollection;                     /* Number of parsed collections         */
156     unsigned maxapplication;                    /* Number of applications               */
157     usbHidReportEnum_t HidReportEnum[USB_HID_REPORT_TYPES];     /* 设备的报告信息      */
158 
159     /* transport */
160     unsigned int CtrlIn;                            /* ctrl in  pipe                    */
161     unsigned int CtrlOut;                           /* ctrl out pipe                    */
162     unsigned int IntIn;                         /* interrupt in pipe                */
163     unsigned char  EpInterval;                      /* int 传输主机查询设备的周期      */
164     unsigned int OnceTransferLength;                /* 中断ep的最大传输包大小             */
165 
166     unsigned int busy;                              /* 主机是否正在处理命令           */
167     struct urb *CurrentUrb;                 /* USB requests                     */
168     hal_sem_t UrbWait;          /* wait for Urb done                */
169     struct usb_ctrlrequest *CtrlReq;        /* control requests                 */
170 
171     /* USB接口操作 */
172     Hid_SoftReset     SoftReset;            /* 软复位,只是清除 hid device 的状态 */
173     Hid_ResetRecovery ResetRecovery;        /* reset device                     */
174     Hid_Transport     Transport;            /* 传输                           */
175     Hid_StopTransport StopTransport;        /* 中止传输                         */
176 
177     /* Hid设备操作 */
178     HidClientProbe  ClientProbe;
179     HidClientRemove ClientRemove;
180 
181     void *Extern;                           /* 对应具体的hid设备, 如mouse, keyboard */
182 }HidDev_t;
183 
184 typedef void (* HidClientDone)(struct _HidRequest *);
185 
186 /* Hid 传输请求 */
187 typedef struct _HidRequest{
188     HidDev_t *HidDev;
189 
190     void *buffer;                           /* Data buffer                  */
191     unsigned int DataTransferLength;                /* Size of data buffer          */
192     unsigned int ActualLength;                      /* actual transport length      */
193 
194     HidClientDone Done;
195     unsigned int Result;                            /* 执行结果                     */
196 
197     void *Extern;                           /* 对应具体的hid设备, 如mouse, keyboard */
198 }HidRequest_t;
199 
200 //-----------------------------------------------------
201 //  Hid 传输结果
202 //-----------------------------------------------------
203 #define  USB_HID_TRANSPORT_SUCCESS              0x00  /* 传输成功           */
204 
205 #define  USB_HID_TRANSPORT_DEVICE_DISCONNECT    0x01  /* 设备断开           */
206 #define  USB_HID_TRANSPORT_DEVICE_RESET         0x02  /* 设备复位           */
207 #define  USB_HID_TRANSPORT_PIPE_HALT            0x03  /* 传输管道异常         */
208 #define  USB_HID_TRANSPORT_CANCEL_CMD           0x04  /* 软件中止此次传输   */
209 
210 #define  USB_HID_TRANSPORT_UNKOWN_ERR           0xFF  /* 未知错误           */
211 
212 
213 //-----------------------------------------------------
214 //
215 //-----------------------------------------------------
216 int HidSentRequest(HidRequest_t *HidReq);
217 int HidGetInputReport(HidDev_t *HidDev, unsigned int Usagepage, unsigned int Usage, unsigned int *BitOffset, unsigned int *BitCount);
218 
219 
220 #endif   //__HID_I_H__
221 
222