Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / wlan / include / htc.h
1 /*
2  * Copyright (c) 2007 Atheros Communications Inc.
3  * All rights reserved.
4  */
5
6 #ifndef __HTC_H__
7 #define __HTC_H__
8
9 #ifndef ATH_TARGET
10 #endif
11
12 #define A_OFFSETOF(type,field) ((int)(&(((type *)NULL)->field)))
13
14 #define ASSEMBLE_UNALIGNED_UINT16(p,highbyte,lowbyte) \
15         (((a_uint16_t)(((a_uint8_t *)(p))[(highbyte)])) << 8 | (a_uint16_t)(((a_uint8_t *)(p))[(lowbyte)]))
16         
17 /* alignment independent macros (little-endian) to fetch UINT16s or UINT8s from a 
18  * structure using only the type and field name.
19  * Use these macros if there is the potential for unaligned buffer accesses. */
20 #define A_GET_UINT16_FIELD(p,type,field)                        \
21         ASSEMBLE_UNALIGNED_UINT16(p,                            \
22                                   A_OFFSETOF(type,field) + 1,   \
23                                   A_OFFSETOF(type,field))
24
25 #define A_SET_UINT16_FIELD(p,type,field,value)                          \
26         {                                                               \
27                 ((a_uint8_t *)(p))[A_OFFSETOF(type,field)] = (a_uint8_t)((value) >> 8); \
28                 ((a_uint8_t *)(p))[A_OFFSETOF(type,field) + 1] = (a_uint8_t)(value); \
29         }
30   
31 #define A_GET_UINT8_FIELD(p,type,field) \
32             ((a_uint8_t *)(p))[A_OFFSETOF(type,field)]
33             
34 #define A_SET_UINT8_FIELD(p,type,field,value) \
35     ((a_uint8_t *)(p))[A_OFFSETOF(type,field)] = (value)
36
37 /****** DANGER DANGER ***************
38  * 
39  *   The frame header length and message formats defined herein were
40  *   selected to accommodate optimal alignment for target processing.  This reduces code
41  *   size and improves performance.
42  * 
43  *   Any changes to the header length may alter the alignment and cause exceptions
44  *   on the target. When adding to the message structures insure that fields are
45  *   properly aligned.
46  * 
47  */
48
49 /* endpoint defines */
50 typedef enum
51 {
52         ENDPOINT_UNUSED = -1,
53         ENDPOINT0 = 0, /* this is reserved for the control endpoint */
54         ENDPOINT1 = 1,  
55         ENDPOINT2 = 2,   
56         ENDPOINT3 = 3,
57         ENDPOINT4,
58         ENDPOINT5,
59         ENDPOINT6,
60         ENDPOINT7,
61         ENDPOINT8,
62         ENDPOINT_MAX = 22 /* maximum number of endpoints for this firmware build, max application
63                              endpoints = (ENDPOINT_MAX - 1) */
64 } HTC_ENDPOINT_ID;
65
66 /* HTC frame header */
67 typedef PREPACK struct _HTC_FRAME_HDR{
68         /* do not remove or re-arrange these fields, these are minimally required
69          * to take advantage of 4-byte lookaheads in some hardware implementations */
70         a_uint8_t   EndpointID;
71         a_uint8_t   Flags;
72         a_uint16_t  PayloadLen;       /* length of data (including trailer) that follows the header */
73     
74         /***** end of 4-byte lookahead ****/
75     
76         a_uint8_t   ControlBytes[4];
77     
78         /* message payload starts after the header */
79     
80 } POSTPACK HTC_FRAME_HDR;
81
82 /* frame header flags */
83 #define HTC_FLAGS_NEED_CREDIT_UPDATE (1 << 0)
84 #define HTC_FLAGS_RECV_TRAILER       (1 << 1)
85 #define HTC_FLAGS_CREDIT_REDISTRIBUTION (1 << 2)
86
87 #define HTC_HDR_LENGTH  (sizeof(HTC_FRAME_HDR))
88 #define HTC_MAX_TRAILER_LENGTH   255
89 #define HTC_MAX_PAYLOAD_LENGTH   (2048 - sizeof(HTC_FRAME_HDR))
90
91 /* HTC control message IDs */
92 typedef enum {
93         HTC_MSG_READY_ID = 1,
94         HTC_MSG_CONNECT_SERVICE_ID = 2,
95         HTC_MSG_CONNECT_SERVICE_RESPONSE_ID = 3,   
96         HTC_MSG_SETUP_COMPLETE_ID = 4,
97         HTC_MSG_CONFIG_PIPE_ID = 5,
98         HTC_MSG_CONFIG_PIPE_RESPONSE_ID = 6,
99 } HTC_MSG_IDS;
100  
101 #define HTC_MAX_CONTROL_MESSAGE_LENGTH  256
102          
103 /* base message ID header */
104 typedef PREPACK struct {
105         a_uint16_t MessageID;    
106 } POSTPACK HTC_UNKNOWN_MSG;
107                                                      
108 /* HTC ready message
109  * direction : target-to-host  */
110 typedef PREPACK struct {
111         a_uint16_t  MessageID;    /* ID */
112         a_uint16_t  CreditCount;  /* number of credits the target can offer */       
113         a_uint16_t  CreditSize;   /* size of each credit */
114         a_uint8_t   MaxEndpoints; /* maximum number of endpoints the target has resources for */
115         a_uint8_t   _Pad1;
116 } POSTPACK HTC_READY_MSG;
117
118 #define HTC_SERVICE_META_DATA_MAX_LENGTH 128
119
120 /* connect service
121  * direction : host-to-target */
122 typedef PREPACK struct {
123         a_uint16_t  MessageID;
124         a_uint16_t  ServiceID;           /* service ID of the service to connect to */       
125         a_uint16_t  ConnectionFlags;     /* connection flags */
126         a_uint8_t   DownLinkPipeID;
127         a_uint8_t   UpLinkPipeID;
128
129 #define HTC_CONNECT_FLAGS_REDUCE_CREDIT_DRIBBLE (1 << 2)  /* reduce credit dribbling when 
130                                                              the host needs credits */  
131 #define HTC_CONNECT_FLAGS_THRESHOLD_LEVEL_MASK             (0x3)  
132 #define HTC_CONNECT_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH        0x0
133 #define HTC_CONNECT_FLAGS_THRESHOLD_LEVEL_ONE_HALF          0x1
134 #define HTC_CONNECT_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS     0x2
135 #define HTC_CONNECT_FLAGS_THRESHOLD_LEVEL_UNITY             0x3
136                                                              
137         a_uint8_t   ServiceMetaLength;   /* length of meta data that follows */
138         a_uint8_t   _Pad1;
139     
140         /* service-specific meta data starts after the header */
141     
142 } POSTPACK HTC_CONNECT_SERVICE_MSG;
143
144 /* connect response
145  * direction : target-to-host */
146 typedef PREPACK struct {
147         a_uint16_t  MessageID;
148         a_uint16_t  ServiceID;            /* service ID that the connection request was made */
149         a_uint8_t   Status;               /* service connection status */ 
150         a_uint8_t   EndpointID;           /* assigned endpoint ID */
151         a_uint16_t  MaxMsgSize;           /* maximum expected message size on this endpoint */
152         a_uint8_t   ServiceMetaLength;    /* length of meta data that follows */
153         a_uint8_t   _Pad1;               
154     
155         /* service-specific meta data starts after the header */
156     
157 } POSTPACK HTC_CONNECT_SERVICE_RESPONSE_MSG;
158
159 typedef PREPACK struct {
160         a_uint16_t  MessageID;
161         /* currently, no other fields */
162 } POSTPACK HTC_SETUP_COMPLETE_MSG;
163
164 /* config pipe
165  * direction : host-to-target */
166 typedef PREPACK struct {
167         a_uint16_t  MessageID;
168         a_uint8_t   PipeID;           /* Pipe ID of the service to connect to */       
169         a_uint8_t   CreditCount;      /* CreditCount */                                                            
170         //a_uint8_t   _Pad1;        
171 } POSTPACK HTC_CONFIG_PIPE_MSG;
172
173 /* config pipe
174  * direction : host-to-target */
175 typedef PREPACK struct {
176         a_uint16_t  MessageID;
177         a_uint8_t   PipeID;           /* Pipe ID of the service to connect to */       
178         a_uint8_t   Status;           /* status */                                                            
179         //a_uint8_t   _Pad1;        
180 } POSTPACK HTC_CONFIG_PIPE_RESPONSE_MSG;
181
182 /* connect response status codes */
183 #define HTC_SERVICE_SUCCESS      0  /* success */
184 #define HTC_SERVICE_NOT_FOUND    1  /* service could not be found */
185 #define HTC_SERVICE_FAILED       2  /* specific service failed the connect */
186 #define HTC_SERVICE_NO_RESOURCES 3  /* no resources (i.e. no more endpoints) */  
187 #define HTC_SERVICE_NO_MORE_EP   4  /* specific service is not allowing any more 
188                                        endpoints */
189
190 /* shihhung: config pipe response status code */
191 #define HTC_CONFIGPIPE_SUCCESS    0
192 #define HTC_CONFIGPIPE_NOSUCHPIPE 1
193 #define HTC_CONFIGPIPE_NORESOURCE 2
194
195 /* report record IDs */
196 typedef enum {
197         HTC_RECORD_NULL  = 0,
198         HTC_RECORD_CREDITS   = 1,
199         HTC_RECORD_LOOKAHEAD = 2,   
200 } HTC_RPT_IDS;
201
202 typedef PREPACK struct {
203         a_uint8_t RecordID;     /* Record ID */
204         a_uint8_t Length;       /* Length of record */
205 } POSTPACK HTC_RECORD_HDR;
206
207 typedef PREPACK struct {
208         a_uint8_t EndpointID;     /* Endpoint that owns these credits */
209         a_uint8_t Credits;        /* credits to report since last report */
210 } POSTPACK HTC_CREDIT_REPORT;
211
212 typedef PREPACK struct {    
213         a_uint8_t PreValid;         /* pre valid guard */
214         a_uint8_t LookAhead[4];     /* 4 byte lookahead */
215         a_uint8_t PostValid;        /* post valid guard */
216     
217         /* NOTE: the LookAhead array is guarded by a PreValid and Post Valid guard bytes.
218          * The PreValid bytes must equal the inverse of the PostValid byte */
219     
220 } POSTPACK HTC_LOOKAHEAD_REPORT;
221
222 #ifndef ATH_TARGET
223 //#include "athendpack.h"
224 #endif
225
226 #endif /* __HTC_H__ */