d0d66f89c04872146c56ac9b3cd369fb81f26ccc
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / RNDIS.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \ingroup Group_USBDeviceClassDrivers
32 * @defgroup Group_USBClassRNDISDevice RNDIS Device Class Driver - LUFA/Drivers/Class/Device/RNDIS.h
33 *
34 * \section Sec_Dependencies Module Source Dependencies
35 * The following files must be built with any user project that uses this module:
36 * - LUFA/Drivers/USB/Class/Device/RNDIS.c
37 *
38 * \section Module Description
39 * Functions, macros, variables, enums and types related to the management of USB RNDIS Ethernet
40 * interfaces within a USB device.
41 *
42 * @{
43 */
44
45 #ifndef _RNDIS_CLASS_H_
46 #define _RNDIS_CLASS_H_
47
48 /* Includes: */
49 #include <string.h>
50
51 #include "../../USB.h"
52 #include "RNDISConstants.h"
53
54 /* Enable C linkage for C++ Compilers: */
55 #if defined(__cplusplus)
56 extern "C" {
57 #endif
58
59 /* Macros: */
60 /** Implemented RNDIS Version Major */
61 #define REMOTE_NDIS_VERSION_MAJOR 0x01
62
63 /** Implemented RNDIS Version Minor */
64 #define REMOTE_NDIS_VERSION_MINOR 0x00
65
66 /** RNDIS request to issue a host-to-device NDIS command */
67 #define REQ_SendEncapsulatedCommand 0x00
68
69 /** RNDIS request to issue a device-to-host NDIS response */
70 #define REQ_GetEncapsulatedResponse 0x01
71
72 /** Maximum size in bytes of a RNDIS control message which can be sent or received */
73 #define RNDIS_MESSAGE_BUFFER_SIZE 128
74
75 /** Maximum size in bytes of an Ethernet frame which can be sent or received */
76 #define ETHERNET_FRAME_SIZE_MAX 1500
77
78 /** Notification request value for a RNDIS Response Available notification */
79 #define NOTIF_ResponseAvailable 1
80
81 /* Enums: */
82 /** Enum for the possible NDIS adapter states. */
83 enum RNDIS_States_t
84 {
85 RNDIS_Uninitialized = 0, /**< Adapter currently uninitialized */
86 RNDIS_Initialized = 1, /**< Adapter currently initialized but not ready for data transfers */
87 RNDIS_Data_Initialized = 2, /**< Adapter currently initialized and ready for data transfers */
88 };
89
90 /** Enum for the NDIS hardware states */
91 enum NDIS_Hardware_Status_t
92 {
93 NDIS_HardwareStatus_Ready, /**< Hardware Ready to accept commands from the host */
94 NDIS_HardwareStatus_Initializing, /**< Hardware busy initializing */
95 NDIS_HardwareStatus_Reset, /**< Hardware reset */
96 NDIS_HardwareStatus_Closing, /**< Hardware currently closing */
97 NDIS_HardwareStatus_NotReady /**< Hardware not ready to accept commands from the host */
98 };
99
100 /* Type Defines: */
101 /** Type define for a physical MAC address of a device on a network */
102 typedef struct
103 {
104 uint8_t Octets[6]; /**< Individual bytes of a MAC address */
105 } MAC_Address_t;
106
107 /** Type define for a RNDIS message header, sent before RNDIS messages */
108 typedef struct
109 {
110 uint32_t MessageType; /**< RNDIS message type, a REMOTE_NDIS_*_MSG constant */
111 uint32_t MessageLength; /**< Total length of the RNDIS message, in bytes */
112 } RNDIS_Message_Header_t;
113
114 /** Type define for an Ethernet frame buffer. */
115 typedef struct
116 {
117 uint8_t FrameData[ETHERNET_FRAME_SIZE_MAX]; /**< Ethernet frame contents */
118 uint16_t FrameLength; /**< Length in bytes of the Ethernet frame stored in the buffer */
119 bool FrameInBuffer; /**< Indicates if a frame is currently stored in the buffer */
120 } Ethernet_Frame_Info_t;
121
122 /** Type define for a RNDIS packet message, used to encapsulate Ethernet packets sent to and from the adapter */
123 typedef struct
124 {
125 uint32_t MessageType;
126 uint32_t MessageLength;
127 uint32_t DataOffset;
128 uint32_t DataLength;
129 uint32_t OOBDataOffset;
130 uint32_t OOBDataLength;
131 uint32_t NumOOBDataElements;
132 uint32_t PerPacketInfoOffset;
133 uint32_t PerPacketInfoLength;
134 uint32_t VcHandle;
135 uint32_t Reserved;
136 } RNDIS_PACKET_MSG_t;
137
138 /** Type define for a RNDIS Initialize command message */
139 typedef struct
140 {
141 uint32_t MessageType;
142 uint32_t MessageLength;
143 uint32_t RequestId;
144
145 uint32_t MajorVersion;
146 uint32_t MinorVersion;
147 uint32_t MaxTransferSize;
148 } RNDIS_INITIALIZE_MSG_t;
149
150 /** Type define for a RNDIS Initialize complete response message */
151 typedef struct
152 {
153 uint32_t MessageType;
154 uint32_t MessageLength;
155 uint32_t RequestId;
156 uint32_t Status;
157
158 uint32_t MajorVersion;
159 uint32_t MinorVersion;
160 uint32_t DeviceFlags;
161 uint32_t Medium;
162 uint32_t MaxPacketsPerTransfer;
163 uint32_t MaxTransferSize;
164 uint32_t PacketAlignmentFactor;
165 uint32_t AFListOffset;
166 uint32_t AFListSize;
167 } RNDIS_INITIALIZE_CMPLT_t;
168
169 /** Type define for a RNDIS Keepalive command message */
170 typedef struct
171 {
172 uint32_t MessageType;
173 uint32_t MessageLength;
174 uint32_t RequestId;
175 } RNDIS_KEEPALIVE_MSG_t;
176
177 /** Type define for a RNDIS Keepalive complete message */
178 typedef struct
179 {
180 uint32_t MessageType;
181 uint32_t MessageLength;
182 uint32_t RequestId;
183 uint32_t Status;
184 } RNDIS_KEEPALIVE_CMPLT_t;
185
186 /** Type define for a RNDIS Reset complete message */
187 typedef struct
188 {
189 uint32_t MessageType;
190 uint32_t MessageLength;
191 uint32_t Status;
192
193 uint32_t AddressingReset;
194 } RNDIS_RESET_CMPLT_t;
195
196 /** Type define for a RNDIS Set command message */
197 typedef struct
198 {
199 uint32_t MessageType;
200 uint32_t MessageLength;
201 uint32_t RequestId;
202
203 uint32_t Oid;
204 uint32_t InformationBufferLength;
205 uint32_t InformationBufferOffset;
206 uint32_t DeviceVcHandle;
207 } RNDIS_SET_MSG_t;
208
209 /** Type define for a RNDIS Set complete response message */
210 typedef struct
211 {
212 uint32_t MessageType;
213 uint32_t MessageLength;
214 uint32_t RequestId;
215 uint32_t Status;
216 } RNDIS_SET_CMPLT_t;
217
218 /** Type define for a RNDIS Query command message */
219 typedef struct
220 {
221 uint32_t MessageType;
222 uint32_t MessageLength;
223 uint32_t RequestId;
224
225 uint32_t Oid;
226 uint32_t InformationBufferLength;
227 uint32_t InformationBufferOffset;
228 uint32_t DeviceVcHandle;
229 } RNDIS_QUERY_MSG_t;
230
231 /** Type define for a RNDIS Query complete response message */
232 typedef struct
233 {
234 uint32_t MessageType;
235 uint32_t MessageLength;
236 uint32_t RequestId;
237 uint32_t Status;
238
239 uint32_t InformationBufferLength;
240 uint32_t InformationBufferOffset;
241 } RNDIS_QUERY_CMPLT_t;
242
243 /** Class state structure. An instance of this structure should be made for each RNDIS interface
244 * within the user application, and passed to each of the RNDIS class driver functions as the
245 * RNDISInterfaceInfo parameter. The contents of this structure should be set to their correct
246 * values when used, or ommitted to force the library to use default values.
247 */
248 typedef struct
249 {
250 uint8_t ControlInterfaceNumber; /**< Interface number of the CDC control interface within the device */
251
252 uint8_t DataINEndpointNumber; /**< Endpoint number of the CDC interface's IN data endpoint */
253 uint16_t DataINEndpointSize; /**< Size in bytes of the CDC interface's IN data endpoint */
254
255 uint8_t DataOUTEndpointNumber; /**< Endpoint number of the CDC interface's OUT data endpoint */
256 uint16_t DataOUTEndpointSize; /**< Size in bytes of the CDC interface's OUT data endpoint */
257
258 uint8_t NotificationEndpointNumber; /**< Endpoint number of the CDC interface's IN notification endpoint, if used */
259 uint16_t NotificationEndpointSize; /**< Size in bytes of the CDC interface's IN notification endpoint, if used */
260
261 char* AdapterVendorDescription; /**< String description of the adapter vendor */
262 MAC_Address_t AdapterMACAddress; /**< MAC address of the adapter */
263
264 uint8_t RNDISMessageBuffer[RNDIS_MESSAGE_BUFFER_SIZE]; /**< Buffer to hold RNDIS messages to and from the host,
265 * managed by the class driver
266 */
267 bool ResponseReady; /**< Internal flag indicating if a RNDIS message is waiting to be returned to the host */
268 uint8_t CurrRNDISState; /**< Current RNDIS state of the adapter, a value from the RNDIS_States_t enum */
269 uint32_t CurrPacketFilter; /**< Current packet filter mode, used internally by the class driver */
270 Ethernet_Frame_Info_t FrameIN; /**< Structure holding the last received Ethernet frame from the host, for user
271 * processing
272 */
273 Ethernet_Frame_Info_t FrameOUT; /**< Structure holding the next Ethernet frame to send to the host, populated by the
274 * user application
275 */
276 } USB_ClassInfo_RNDIS_t;
277
278 /* Function Prototypes: */
279 #if defined(INCLUDE_FROM_RNDIS_CLASS_C)
280 static void RNDIS_Device_ProcessRNDISControlMessage(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo);
281 static bool RNDIS_Device_ProcessNDISQuery(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo,
282 uint32_t OId, void* QueryData, uint16_t QuerySize,
283 void* ResponseData, uint16_t* ResponseSize);
284 static bool RNDIS_Device_ProcessNDISSet(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo, uint32_t OId,
285 void* SetData, uint16_t SetSize);
286 #endif
287
288 /** Configures the endpoints of a given RNDIS interface, ready for use. This should be linked to the library
289 * \ref EVENT_USB_ConfigurationChanged() event so that the endpoints are configured when the configuration
290 * containing the given HID interface is selected.
291 *
292 * \param RNDISInterfaceInfo Pointer to a structure containing a RNDIS Class configuration and state.
293 *
294 * \return Boolean true if the endpoints were sucessfully configured, false otherwise
295 */
296 bool RNDIS_Device_ConfigureEndpoints(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo);
297
298 /** Processes incomming control requests from the host, that are directed to the given RNDIS class interface. This should be
299 * linked to the library \ref EVENT_USB_UnhandledControlPacket() event.
300 *
301 * \param RNDISInterfaceInfo Pointer to a structure containing a RNDIS Class configuration and state.
302 */
303 void RNDIS_Device_ProcessControlPacket(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo);
304
305 /** General management task for a given HID class interface, required for the correct operation of the interface. This should
306 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
307 *
308 * \param RNDISInterfaceInfo Pointer to a structure containing a RNDIS Class configuration and state.
309 */
310 void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_t* RNDISInterfaceInfo);
311
312 /* Disable C linkage for C++ Compilers: */
313 #if defined(__cplusplus)
314 }
315 #endif
316
317 #endif
318
319 /** @} */