Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | USB Host Communication Device Class(CDC) driver |
||
4 | |||
5 | This is the Communication device Class driver file for a USB Embedded Host device. |
||
6 | This file should be used in a project with usb_host.c to provide the USB |
||
7 | hardware interface. |
||
8 | |||
9 | Acronyms/abbreviations used by this class: |
||
10 | * CDC - Communication Device Class |
||
11 | * ACM - Abstract Control Module |
||
12 | |||
13 | To interface with usb_host.c, the routine USBHostCDCInitialize() should be |
||
14 | specified as the Initialize() function, and USBHostCDCEventHandler() should |
||
15 | be specified as the EventHandler() function in the usbClientDrvTable[] array |
||
16 | declared in usb_config.h. |
||
17 | |||
18 | This driver can be configured to use transfer events from usb_host.c. Transfer |
||
19 | events require more RAM and ROM than polling, but it cuts down or even |
||
20 | eliminates the required polling of the various USBxxxTasks functions. For this |
||
21 | class, USBHostCDCTasks() is compiled out if transfer events from usb_host.c |
||
22 | are used. However, USBHostTasks() still must be called to provide attach, |
||
23 | enumeration, and detach services. If transfer events from usb_host.c |
||
24 | are going to be used, USB_ENABLE_TRANSFER_EVENT should be defined. If transfer |
||
25 | status is going to be polled, USB_ENABLE_TRANSFER_EVENT should not be defined. |
||
26 | CDC client layer transfer events are also enables once USB_ENABLE_TRANSFER_EVENT |
||
27 | is defined. The application must define USB_HOST_APP_EVENT_HANDLER in file usb_config.h. |
||
28 | All the CDC client layer event are called back using this function handler. This eliminates |
||
29 | the need of polling for transfer status by the application, however handing of these events |
||
30 | should be taken care by the application. |
||
31 | |||
32 | Transfer of Data Class Interface can be performed with BULK transfers, |
||
33 | hence USB_SUPPORT_BULK_TRANSFERS must be defined. Data Class Interface can also use |
||
34 | ISOCHRONOUS transfers,however the CDC client is not tested for ISOCHRONOUS transfers. |
||
35 | |||
36 | FileName: usb_host_cdc.c |
||
37 | Dependencies: None |
||
38 | Processor: PIC24/dsPIC30/dsPIC33/PIC32MX |
||
39 | Compiler: C30 v2.01/C32 v0.00.18 |
||
40 | Company: Microchip Technology, Inc. |
||
41 | |||
42 | Software License Agreement |
||
43 | |||
44 | The software supplied herewith by Microchip Technology Incorporated |
||
45 | (the Company) for its PICmicro® Microcontroller is intended and |
||
46 | supplied to you, the Companys customer, for use solely and |
||
47 | exclusively on Microchip PICmicro Microcontroller products. The |
||
48 | software is owned by the Company and/or its supplier, and is |
||
49 | protected under applicable copyright laws. All rights are reserved. |
||
50 | Any use in violation of the foregoing restrictions may subject the |
||
51 | user to criminal sanctions under applicable laws, as well as to |
||
52 | civil liability for the breach of the terms and conditions of this |
||
53 | license. |
||
54 | |||
55 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
56 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
57 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
58 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
59 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
60 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
61 | |||
62 | Change History: |
||
63 | Revision Description |
||
64 | v2.6 Removed state machine 'SUBSTATE_SET_CONTROL_LINE_STATE' from normal |
||
65 | enumeration process. |
||
66 | Modified code to accommodate CDC devices that do not have distinct |
||
67 | Communication and Data interfaces. |
||
68 | |||
69 | v2.6a No change |
||
70 | |||
71 | v2.7 Modified the code to allow connection of USB-RS232 dongles that do |
||
72 | not fully comply with CDC specifications |
||
73 | Modified API USBHostCDC_Api_Send_OUT_Data to allow data transfers |
||
74 | more than 256 bytes |
||
75 | |||
76 | v2.7a No change |
||
77 | ********************************************************************************/ |
||
78 | |||
79 | |||
80 | #include <stdlib.h> |
||
81 | #include <string.h> |
||
82 | #include "GenericTypeDefs.h" |
||
83 | #include "HardwareProfile.h" |
||
84 | #include "usb_config.h" |
||
85 | #include "USB\usb.h" |
||
86 | #include "USB\usb_host_cdc.h" |
||
87 | #include "USB\usb_host.h" |
||
88 | |||
89 | //#define DEBUG_MODE |
||
90 | #ifdef DEBUG_MODE |
||
91 | #include "uart2.h" |
||
92 | #endif |
||
93 | |||
94 | |||
95 | // ***************************************************************************** |
||
96 | // ***************************************************************************** |
||
97 | // Configuration |
||
98 | // ***************************************************************************** |
||
99 | // ***************************************************************************** |
||
100 | |||
101 | // ***************************************************************************** |
||
102 | /* Max Number of Supported Devices |
||
103 | |||
104 | This value represents the maximum number of attached devices this class driver |
||
105 | can support. If the user does not define a value, it will be set to 1. |
||
106 | Currently this must be set to 1, due to limitations in the USB Host layer. |
||
107 | */ |
||
108 | #ifndef USB_MAX_CDC_DEVICES |
||
109 | #define USB_MAX_CDC_DEVICES 1 |
||
110 | #endif |
||
111 | |||
112 | // ***************************************************************************** |
||
113 | // ***************************************************************************** |
||
114 | // Constants |
||
115 | // ***************************************************************************** |
||
116 | // ***************************************************************************** |
||
117 | |||
118 | // ***************************************************************************** |
||
119 | // State Machine Constants |
||
120 | // ***************************************************************************** |
||
121 | |||
122 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
123 | |||
124 | #define STATE_MASK 0x00F0 // |
||
125 | #define SUBSTATE_MASK 0x000F // |
||
126 | |||
127 | #define NEXT_STATE 0x0010 // |
||
128 | #define NEXT_SUBSTATE 0x0001 // |
||
129 | |||
130 | |||
131 | #define STATE_DETACHED 0x0000 // |
||
132 | |||
133 | #define STATE_INITIALIZE_DEVICE 0x0010 // |
||
134 | #define SUBSTATE_WAIT_FOR_ENUMERATION 0x0000 // |
||
135 | #define SUBSTATE_DEVICE_ENUMERATED 0x0001 // |
||
136 | #define SUBSTATE_GET_LINE_CODING 0x0002 // |
||
137 | #define SUBSTATE_WAIT_FOR_GET_LINE_CODING 0x0003 // |
||
138 | #define SUBSTATE_SET_LINE_CODING 0x0004 // |
||
139 | #define SUBSTATE_WAIT_FOR_SET_LINE_CODING 0x0005 // |
||
140 | #define SUBSTATE_SET_CONTROL_LINE_STATE 0x0006 // |
||
141 | #define SUBSTATE_WAIT_FOR_SET_CONTROL_LINE_STATE 0x0007 // |
||
142 | |||
143 | |||
144 | #define STATE_RUNNING 0x0020 // |
||
145 | #define SUBSTATE_WAITING_FOR_REQ 0x0000 // |
||
146 | #define SUBSTATE_SEND_READ_REQ 0x0001 // |
||
147 | #define SUBSTATE_READ_REQ_WAIT 0x0002 // |
||
148 | #define SUBSTATE_SEND_WRITE_REQ 0x0003 // |
||
149 | #define SUBSTATE_WRITE_REQ_WAIT 0x0004 // |
||
150 | |||
151 | #define STATE_CDC_RESET_RECOVERY 0x0030 // |
||
152 | #define SUBSTATE_SEND_RESET 0x0000 // |
||
153 | #define SUBSTATE_WAIT_FOR_RESET 0x0001 // |
||
154 | #define SUBSTATE_RESET_COMPLETE 0x0002 // |
||
155 | |||
156 | #define STATE_HOLDING 0x0040 // |
||
157 | |||
158 | |||
159 | #else |
||
160 | |||
161 | |||
162 | #define STATE_DETACHED 0x0000 // |
||
163 | |||
164 | #define STATE_INITIALIZE_DEVICE 0x0001 // |
||
165 | #define STATE_WAIT_FOR_GET_LINE_CODING 0x0002 // |
||
166 | #define STATE_WAIT_FOR_SET_LINE_CODING 0x0003 // |
||
167 | #define STATE_WAIT_FOR_SET_CONTROL_LINE_STATE 0x0004 // |
||
168 | |||
169 | #define STATE_RUNNING 0x0005 // |
||
170 | #define STATE_READ_REQ_WAIT 0x0006 // |
||
171 | #define STATE_WRITE_REQ_WAIT 0x0007 // |
||
172 | |||
173 | #define STATE_WAIT_FOR_RESET 0x0008 // |
||
174 | #define STATE_RESET_COMPLETE 0x0009 // |
||
175 | |||
176 | #define STATE_HOLDING 0x000A // |
||
177 | |||
178 | #endif |
||
179 | |||
180 | // ***************************************************************************** |
||
181 | // Other Constants |
||
182 | // ***************************************************************************** |
||
183 | |||
184 | #define USB_CDC_RESET (0xFF) // Device Request code to reset the device. |
||
185 | #define MARK_RESET_RECOVERY (0x0E) // |
||
186 | |||
187 | |||
188 | // Default values for RS232 configuration data |
||
189 | #ifndef USB_CDC_BAUDRATE_SUPPORTED |
||
190 | #define USB_CDC_BAUDRATE_SUPPORTED 19200UL // default BAUDRATE for the device |
||
191 | #endif |
||
192 | |||
193 | #ifndef USB_CDC_PARITY_TYPE |
||
194 | #define USB_CDC_PARITY_TYPE 0 // default Parity type |
||
195 | #endif |
||
196 | |||
197 | #ifndef USB_CDC_STOP_BITS |
||
198 | #define USB_CDC_STOP_BITS 0 // default Stop bits |
||
199 | #endif |
||
200 | |||
201 | #ifndef USB_CDC_NO_OF_DATA_BITS |
||
202 | #define USB_CDC_NO_OF_DATA_BITS 8 // default value of data width |
||
203 | #endif |
||
204 | |||
205 | //****************************************************************************** |
||
206 | //****************************************************************************** |
||
207 | // Data Structures |
||
208 | //****************************************************************************** |
||
209 | //****************************************************************************** |
||
210 | |||
211 | //****************************************************************************** |
||
212 | //****************************************************************************** |
||
213 | // Section: Local Prototypes |
||
214 | //****************************************************************************** |
||
215 | //****************************************************************************** |
||
216 | void _USBHostCDC_ResetStateJump( BYTE i ); |
||
217 | void USBHostCDC_Init_CDC_Buffers(void); |
||
218 | |||
219 | //****************************************************************************** |
||
220 | //****************************************************************************** |
||
221 | // Macros |
||
222 | //****************************************************************************** |
||
223 | //****************************************************************************** |
||
224 | |||
225 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
226 | |||
227 | #define _USBHostCDC_SetNextState() { deviceInfoCDC[i].state = (deviceInfoCDC[i].state & STATE_MASK) + NEXT_STATE; } |
||
228 | #define _USBHostCDC_SetNextSubState() { deviceInfoCDC[i].state += NEXT_SUBSTATE; } |
||
229 | #define _USBHostCDC_TerminateTransfer( error ) { \ |
||
230 | deviceInfoCDC[i].errorCode = error; \ |
||
231 | deviceInfoCDC[i].state = STATE_RUNNING;\ |
||
232 | } |
||
233 | |||
234 | #else |
||
235 | #define _USBHostCDC_TerminateTransfer( error ) { \ |
||
236 | deviceInfoCDC[i].errorCode = error; \ |
||
237 | deviceInfoCDC[i].state = STATE_RUNNING;\ |
||
238 | } |
||
239 | #endif |
||
240 | |||
241 | |||
242 | //****************************************************************************** |
||
243 | //****************************************************************************** |
||
244 | // Section: CDC Host Global Variables |
||
245 | //****************************************************************************** |
||
246 | //****************************************************************************** |
||
247 | |||
248 | USB_CDC_LINE_CODING CDC_DEV_LINE_CODING_Buffer; // Stores LINE CODING data received from device |
||
249 | USB_CDC_LINE_CODING CDC_LINE_CODING_Buffer; // Stores LINE CODING data configured at host |
||
250 | USB_CDC_CONTROL_SIGNAL_BITMAP CDC_CONTROL_SIGNAL_Buffer; // Stores CONTROL SIGNAL data configured at host |
||
251 | |||
252 | USB_CDC_DEVICE_INFO deviceInfoCDC[USB_MAX_CDC_DEVICES] __attribute__ ((aligned)); // This structure holds all the information of device collected during enumeration |
||
253 | |||
254 | BYTE CDCdeviceAddress = 0; // Holds address of the attached device |
||
255 | |||
256 | //****************************************************************************** |
||
257 | //****************************************************************************** |
||
258 | // Section: CDC Host External Variables |
||
259 | //****************************************************************************** |
||
260 | //****************************************************************************** |
||
261 | |||
262 | #define PTR_HOST_LINE_CODING_BUFFER (&CDC_LINE_CODING_Buffer) // Address of Line Coding buffer |
||
263 | #define PTR_HOST_CONTROL_LINE_BUFFER (&CDC_CONTROL_SIGNAL_Buffer) // Address of cntrol signal buffer |
||
264 | |||
265 | |||
266 | extern CLIENT_DRIVER_TABLE usbDeviceInterfaceTable; |
||
267 | |||
268 | //****************************************************************************** |
||
269 | //****************************************************************************** |
||
270 | // Section: CDC Host Global Variables |
||
271 | //****************************************************************************** |
||
272 | //****************************************************************************** |
||
273 | |||
274 | // ***************************************************************************** |
||
275 | // ***************************************************************************** |
||
276 | // CDC Host Stack Callback Functions |
||
277 | // ***************************************************************************** |
||
278 | // ***************************************************************************** |
||
279 | |||
280 | /******************************************************************************* |
||
281 | Function: |
||
282 | BOOL USBHostCDCInitAddress( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
283 | |||
284 | Precondition: |
||
285 | The device has been enumerated without any errors. |
||
286 | |||
287 | Summary: |
||
288 | This function intializes the address of the attached CDC device. |
||
289 | |||
290 | Description: |
||
291 | This function intializes the address of the attached CDC device. Once the |
||
292 | device is enumerated without any errors, the CDC client call this function. |
||
293 | For all the transfer requesets this address is used to indentify the CDC |
||
294 | device. |
||
295 | |||
296 | |||
297 | Parameters: |
||
298 | BYTE address - Address of the new device |
||
299 | DWORD flags - Initialization flags |
||
300 | BYTE clientDriverID - Client driver identification for device requests |
||
301 | |||
302 | Return Values: |
||
303 | TRUE - We can support the device. |
||
304 | FALSE - We cannot support the device. |
||
305 | |||
306 | Remarks: |
||
307 | None |
||
308 | *******************************************************************************/ |
||
309 | BOOL USBHostCDCInitAddress( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
310 | { |
||
311 | #ifndef MINIMUM_BUILD |
||
312 | // UART2PrintString( "CDC: Device attached.\r\n" ); |
||
313 | #endif |
||
314 | |||
315 | if (CDCdeviceAddress == 0) |
||
316 | { |
||
317 | // Save the address of the new device. |
||
318 | CDCdeviceAddress = address; |
||
319 | return TRUE; |
||
320 | } |
||
321 | else |
||
322 | { |
||
323 | // We can currently only handle one device. |
||
324 | return FALSE; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | |||
329 | // ***************************************************************************** |
||
330 | // ***************************************************************************** |
||
331 | // Application Callable Functions |
||
332 | // ***************************************************************************** |
||
333 | // ***************************************************************************** |
||
334 | |||
335 | /******************************************************************************* |
||
336 | Function: |
||
337 | BYTE USBHostCDCDeviceStatus( BYTE deviceAddress ) |
||
338 | |||
339 | Summary: |
||
340 | |||
341 | Description: |
||
342 | This function determines the status of a CDC device. |
||
343 | |||
344 | Preconditions: None |
||
345 | |||
346 | Parameters: |
||
347 | BYTE deviceAddress - address of device to query |
||
348 | |||
349 | Return Values: |
||
350 | USB_CDC_DEVICE_NOT_FOUND - Illegal device address, or the |
||
351 | device is not an CDC |
||
352 | USB_CDC_INITIALIZING - CDC is attached and in the |
||
353 | process of initializing |
||
354 | USB_PROCESSING_REPORT_DESCRIPTOR - CDC device is detected and report |
||
355 | descriptor is being parsed |
||
356 | USB_CDC_NORMAL_RUNNING - CDC Device is running normal, |
||
357 | ready to send and receive reports |
||
358 | USB_CDC_DEVICE_HOLDING - CDC device could not recover from the error |
||
359 | USB_CDC_DEVICE_DETACHED - CDC detached. |
||
360 | |||
361 | Remarks: |
||
362 | None |
||
363 | *******************************************************************************/ |
||
364 | BYTE USBHostCDCDeviceStatus( BYTE deviceAddress ) |
||
365 | { |
||
366 | BYTE i; |
||
367 | BYTE status; |
||
368 | |||
369 | // Find the correct device. |
||
370 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != deviceAddress); i++); |
||
371 | if (i == USB_MAX_CDC_DEVICES) |
||
372 | { |
||
373 | return USB_CDC_DEVICE_NOT_FOUND; |
||
374 | } |
||
375 | |||
376 | status = USBHostDeviceStatus( deviceAddress ); |
||
377 | if (status != USB_DEVICE_ATTACHED) |
||
378 | { |
||
379 | return status; |
||
380 | } |
||
381 | else |
||
382 | { |
||
383 | // The device is attached and done enumerating. We can get more specific now. |
||
384 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
385 | switch (deviceInfoCDC[i].state & STATE_MASK) |
||
386 | { |
||
387 | case STATE_INITIALIZE_DEVICE: |
||
388 | return USB_CDC_INITIALIZING; |
||
389 | break; |
||
390 | |||
391 | case STATE_RUNNING: |
||
392 | return USB_CDC_NORMAL_RUNNING; |
||
393 | break; |
||
394 | case STATE_HOLDING: |
||
395 | return USB_CDC_DEVICE_HOLDING; |
||
396 | break; |
||
397 | |||
398 | case STATE_CDC_RESET_RECOVERY: |
||
399 | return USB_CDC_RESETTING_DEVICE; |
||
400 | break; |
||
401 | |||
402 | default: |
||
403 | return USB_CDC_DEVICE_DETACHED; |
||
404 | break; |
||
405 | } |
||
406 | #else |
||
407 | switch (deviceInfoCDC[i].state) |
||
408 | { |
||
409 | case STATE_INITIALIZE_DEVICE: |
||
410 | return USB_CDC_INITIALIZING; |
||
411 | break; |
||
412 | |||
413 | case STATE_RUNNING: |
||
414 | case STATE_READ_REQ_WAIT: |
||
415 | case STATE_WRITE_REQ_WAIT: |
||
416 | return USB_CDC_NORMAL_RUNNING; |
||
417 | break; |
||
418 | |||
419 | case STATE_HOLDING: |
||
420 | return USB_CDC_DEVICE_HOLDING; |
||
421 | break; |
||
422 | |||
423 | case STATE_WAIT_FOR_RESET: |
||
424 | case STATE_RESET_COMPLETE: |
||
425 | return USB_CDC_RESETTING_DEVICE; |
||
426 | break; |
||
427 | |||
428 | default: |
||
429 | return USB_CDC_DEVICE_DETACHED; |
||
430 | break; |
||
431 | } |
||
432 | #endif |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /******************************************************************************* |
||
437 | Function: |
||
438 | BYTE USBHostCDCResetDevice( BYTE deviceAddress ) |
||
439 | |||
440 | Summary: |
||
441 | This function starts a CDC reset. |
||
442 | |||
443 | Description: |
||
444 | This function starts a CDC reset. A reset can be |
||
445 | issued only if the device is attached and not being initialized. |
||
446 | |||
447 | Precondition: |
||
448 | None |
||
449 | |||
450 | Parameters: |
||
451 | BYTE deviceAddress - Device address |
||
452 | |||
453 | Return Values: |
||
454 | USB_SUCCESS - Reset started |
||
455 | USB_MSD_DEVICE_NOT_FOUND - No device with specified address |
||
456 | USB_MSD_ILLEGAL_REQUEST - Device is in an illegal state for reset |
||
457 | |||
458 | Remarks: |
||
459 | None |
||
460 | *******************************************************************************/ |
||
461 | BYTE USBHostCDCResetDevice( BYTE deviceAddress ) |
||
462 | { |
||
463 | BYTE i; |
||
464 | |||
465 | // Make sure a valid device is being requested. |
||
466 | if ((deviceAddress == 0) || (deviceAddress > 127)) |
||
467 | { |
||
468 | return USB_CDC_DEVICE_NOT_FOUND; |
||
469 | } |
||
470 | |||
471 | // Find the correct device. |
||
472 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != deviceAddress); i++); |
||
473 | if (i == USB_MAX_CDC_DEVICES) |
||
474 | { |
||
475 | return USB_CDC_DEVICE_NOT_FOUND; |
||
476 | } |
||
477 | |||
478 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
479 | if (((deviceInfoCDC[i].state & STATE_MASK) != STATE_DETACHED) && |
||
480 | ((deviceInfoCDC[i].state & STATE_MASK) != STATE_INITIALIZE_DEVICE)) |
||
481 | #else |
||
482 | if ((deviceInfoCDC[i].state != STATE_DETACHED) && |
||
483 | (deviceInfoCDC[i].state != STATE_INITIALIZE_DEVICE)) |
||
484 | #endif |
||
485 | { |
||
486 | deviceInfoCDC[i].flags.val |= MARK_RESET_RECOVERY; |
||
487 | deviceInfoCDC[i].flags.bfReset = 1; |
||
488 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
489 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
490 | #else |
||
491 | deviceInfoCDC[i].returnState = STATE_RUNNING; |
||
492 | #endif |
||
493 | |||
494 | _USBHostCDC_ResetStateJump( i ); |
||
495 | return USB_SUCCESS; |
||
496 | } |
||
497 | return USB_CDC_ILLEGAL_REQUEST; |
||
498 | } |
||
499 | |||
500 | |||
501 | /******************************************************************************* |
||
502 | Function: |
||
503 | void USBHostCDCTasks( void ) |
||
504 | |||
505 | Summary: |
||
506 | This function performs the maintenance tasks required by CDC class |
||
507 | |||
508 | Description: |
||
509 | This function performs the maintenance tasks required by the CDC |
||
510 | class. If transfer events from the host layer are not being used, then |
||
511 | it should be called on a regular basis by the application. If transfer |
||
512 | events from the host layer are being used, this function is compiled out, |
||
513 | and does not need to be called. |
||
514 | |||
515 | Precondition: |
||
516 | USBHostCDCInitialize() has been called. |
||
517 | |||
518 | Parameters: |
||
519 | None - None |
||
520 | |||
521 | Returns: |
||
522 | None |
||
523 | |||
524 | Remarks: |
||
525 | None |
||
526 | *******************************************************************************/ |
||
527 | void USBHostCDCTasks( void ) |
||
528 | { |
||
529 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
530 | DWORD byteCount; |
||
531 | BYTE errorCode; |
||
532 | BYTE i; |
||
533 | |||
534 | // BYTE temp; |
||
535 | |||
536 | for (i=0; i<USB_MAX_CDC_DEVICES; i++) |
||
537 | { |
||
538 | if (deviceInfoCDC[i].deviceAddress == 0) /* device address updated by lower layer */ |
||
539 | { |
||
540 | deviceInfoCDC[i].state = STATE_DETACHED; |
||
541 | } |
||
542 | |||
543 | switch (deviceInfoCDC[i].state & STATE_MASK) |
||
544 | { |
||
545 | case STATE_DETACHED: |
||
546 | // No device attached. |
||
547 | break; |
||
548 | |||
549 | case STATE_INITIALIZE_DEVICE: |
||
550 | switch (deviceInfoCDC[i].state & SUBSTATE_MASK) |
||
551 | { |
||
552 | case SUBSTATE_WAIT_FOR_ENUMERATION: |
||
553 | if (USBHostDeviceStatus( deviceInfoCDC[i].deviceAddress ) == USB_DEVICE_ATTACHED) |
||
554 | { |
||
555 | _USBHostCDC_SetNextSubState(); |
||
556 | } |
||
557 | break; |
||
558 | |||
559 | case SUBSTATE_DEVICE_ENUMERATED: |
||
560 | USBHostCDCInitAddress( deviceInfoCDC[i].deviceAddress,0, deviceInfoCDC[i].clientDriverID ); // Initialize device address for application use |
||
561 | _USBHostCDC_SetNextSubState(); /* need to add sub states to Set Config, Get LANGID & String Descriptors */ |
||
562 | break; |
||
563 | |||
564 | |||
565 | case SUBSTATE_GET_LINE_CODING: |
||
566 | |||
567 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
568 | USB_CDC_GET_LINE_CODING, 0 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_LINE_CODING_LENGTH, (BYTE*)&CDC_DEV_LINE_CODING_Buffer, |
||
569 | USB_DEVICE_REQUEST_GET , deviceInfoCDC[i].clientDriverID )) |
||
570 | { |
||
571 | _USBHostCDC_SetNextSubState(); |
||
572 | } |
||
573 | break; |
||
574 | |||
575 | case SUBSTATE_WAIT_FOR_GET_LINE_CODING: |
||
576 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
577 | { |
||
578 | if (errorCode) |
||
579 | { |
||
580 | /* Set error code */ |
||
581 | _USBHostCDC_TerminateTransfer(errorCode); |
||
582 | } |
||
583 | else |
||
584 | { |
||
585 | // Clear the STALL. Since it is EP0, we do not have to clear the stall. |
||
586 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, 0 ); |
||
587 | // Compare with Line coding expected at Host end with the one received from device |
||
588 | if(0 == memcmp(&CDC_DEV_LINE_CODING_Buffer, PTR_HOST_LINE_CODING_BUFFER, USB_CDC_LINE_CODING_LENGTH)) |
||
589 | { |
||
590 | // if fine goto set Control line state |
||
591 | // deviceInfoCDC[i].state = STATE_INITIALIZE_DEVICE | SUBSTATE_SET_CONTROL_LINE_STATE; |
||
592 | deviceInfoCDC[i].state = STATE_RUNNING; |
||
593 | } |
||
594 | else |
||
595 | { |
||
596 | // if data not as expected goto Set Line coding |
||
597 | deviceInfoCDC[i].state = STATE_INITIALIZE_DEVICE | SUBSTATE_SET_LINE_CODING; |
||
598 | } |
||
599 | } |
||
600 | } |
||
601 | break; |
||
602 | |||
603 | case SUBSTATE_SET_LINE_CODING: |
||
604 | |||
605 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
606 | USB_CDC_SET_LINE_CODING, 1 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_LINE_CODING_LENGTH, (BYTE*)PTR_HOST_LINE_CODING_BUFFER, |
||
607 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID )) |
||
608 | { |
||
609 | _USBHostCDC_SetNextSubState(); |
||
610 | } |
||
611 | break; |
||
612 | |||
613 | case SUBSTATE_WAIT_FOR_SET_LINE_CODING: |
||
614 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
615 | { |
||
616 | if (errorCode) |
||
617 | { |
||
618 | /* Set error code */ |
||
619 | _USBHostCDC_TerminateTransfer(errorCode); |
||
620 | } |
||
621 | else |
||
622 | { // check again if line coding is correctly set |
||
623 | deviceInfoCDC[i].state = STATE_INITIALIZE_DEVICE | SUBSTATE_GET_LINE_CODING; |
||
624 | } |
||
625 | } |
||
626 | break; |
||
627 | |||
628 | case SUBSTATE_SET_CONTROL_LINE_STATE: |
||
629 | |||
630 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
631 | USB_CDC_SET_CONTROL_LINE_STATE , 0 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_CONTROL_LINE_LENGTH, (BYTE*)PTR_HOST_CONTROL_LINE_BUFFER, |
||
632 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID )) |
||
633 | { |
||
634 | _USBHostCDC_SetNextSubState(); |
||
635 | } |
||
636 | break; |
||
637 | |||
638 | case SUBSTATE_WAIT_FOR_SET_CONTROL_LINE_STATE: |
||
639 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
640 | { |
||
641 | if (errorCode) |
||
642 | { |
||
643 | /* Set error code */ |
||
644 | _USBHostCDC_TerminateTransfer(errorCode); |
||
645 | } |
||
646 | else |
||
647 | { |
||
648 | _USBHostCDC_SetNextState(); |
||
649 | } |
||
650 | } |
||
651 | break; |
||
652 | |||
653 | default : |
||
654 | break; |
||
655 | } |
||
656 | break; |
||
657 | |||
658 | case STATE_RUNNING: |
||
659 | switch (deviceInfoCDC[i].state & SUBSTATE_MASK) |
||
660 | { |
||
661 | case SUBSTATE_WAITING_FOR_REQ: |
||
662 | /* waiting for request from application */ |
||
663 | break; |
||
664 | |||
665 | case SUBSTATE_SEND_READ_REQ: |
||
666 | // if endpoint 0 then use control transfer - Communication interface request |
||
667 | // otherwise request is for data interface transfer |
||
668 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
669 | |||
670 | if(deviceInfoCDC[i].endpointDATA == 0x00) |
||
671 | { |
||
672 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
673 | deviceInfoCDC[i].commRequest, 0, deviceInfoCDC[i].interface,deviceInfoCDC[i].reportSize, deviceInfoCDC[i].userData, |
||
674 | USB_DEVICE_REQUEST_GET , deviceInfoCDC[i].clientDriverID ); |
||
675 | } |
||
676 | else |
||
677 | { |
||
678 | errorCode = USBHostRead( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, |
||
679 | deviceInfoCDC[i].userData, deviceInfoCDC[i].reportSize ); |
||
680 | } |
||
681 | |||
682 | if (errorCode) |
||
683 | { |
||
684 | if(USB_ENDPOINT_STALLED == errorCode) |
||
685 | { |
||
686 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
687 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
688 | deviceInfoCDC[i].flags.bfReset = 1; |
||
689 | _USBHostCDC_ResetStateJump( i ); |
||
690 | } |
||
691 | else |
||
692 | { |
||
693 | _USBHostCDC_TerminateTransfer(errorCode); |
||
694 | } |
||
695 | } |
||
696 | else |
||
697 | { |
||
698 | _USBHostCDC_SetNextSubState(); |
||
699 | } |
||
700 | break; |
||
701 | |||
702 | case SUBSTATE_READ_REQ_WAIT: |
||
703 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, &errorCode, &byteCount )) |
||
704 | { |
||
705 | if (errorCode) |
||
706 | { |
||
707 | if(USB_ENDPOINT_STALLED == errorCode) |
||
708 | { |
||
709 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
710 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
711 | deviceInfoCDC[i].flags.bfReset = 1; |
||
712 | _USBHostCDC_ResetStateJump( i ); |
||
713 | }else if(USB_ENDPOINT_NAK_TIMEOUT == errorCode) |
||
714 | { |
||
715 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_NAK_TIMEOUT,NULL, 0); |
||
716 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ; |
||
717 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
718 | } |
||
719 | else |
||
720 | { |
||
721 | _USBHostCDC_TerminateTransfer(errorCode); |
||
722 | } |
||
723 | } |
||
724 | else |
||
725 | { |
||
726 | // Clear the STALL. Since it is EP0, we do not have to clear the stall. |
||
727 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
728 | deviceInfoCDC[i].bytesTransferred = byteCount; /* Can compare with report size and flag error ???*/ |
||
729 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ; |
||
730 | } |
||
731 | } |
||
732 | #ifdef DEBUG_MODE |
||
733 | UART2PrintString("|"); |
||
734 | #endif |
||
735 | break; |
||
736 | |||
737 | case SUBSTATE_SEND_WRITE_REQ: |
||
738 | |||
739 | // if endpoint 0 then use control transfer - Communication interface request |
||
740 | // otherwise request is for data interface transfer |
||
741 | if(deviceInfoCDC[i].endpointDATA == 0x00) |
||
742 | { |
||
743 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
744 | deviceInfoCDC[i].commRequest, 0, deviceInfoCDC[i].interface,deviceInfoCDC[i].reportSize, deviceInfoCDC[i].userData, |
||
745 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID ); |
||
746 | } |
||
747 | else |
||
748 | { // if transfer size is more than 64 Bytes, multiple transactions are used to transfer the data |
||
749 | if(deviceInfoCDC[i].remainingBytes > USB_CDC_MAX_PACKET_SIZE) |
||
750 | { |
||
751 | deviceInfoCDC[i].bytesTransferred = USB_CDC_MAX_PACKET_SIZE; // max transaction size is 64 bytes |
||
752 | deviceInfoCDC[i].remainingBytes -= USB_CDC_MAX_PACKET_SIZE; |
||
753 | errorCode = USBHostWrite( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, |
||
754 | deviceInfoCDC[i].userData, deviceInfoCDC[i].bytesTransferred ); |
||
755 | deviceInfoCDC[i].userData = deviceInfoCDC[i].userData + USB_CDC_MAX_PACKET_SIZE; |
||
756 | } |
||
757 | else |
||
758 | { |
||
759 | deviceInfoCDC[i].bytesTransferred = deviceInfoCDC[i].remainingBytes; |
||
760 | deviceInfoCDC[i].remainingBytes = 0; |
||
761 | errorCode = USBHostWrite( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, |
||
762 | deviceInfoCDC[i].userData, deviceInfoCDC[i].bytesTransferred ); |
||
763 | } |
||
764 | |||
765 | } |
||
766 | |||
767 | if (errorCode) |
||
768 | { |
||
769 | if(USB_ENDPOINT_STALLED == errorCode) |
||
770 | { |
||
771 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
772 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
773 | deviceInfoCDC[i].flags.bfReset = 1; |
||
774 | _USBHostCDC_ResetStateJump( i ); |
||
775 | } |
||
776 | else |
||
777 | { |
||
778 | _USBHostCDC_TerminateTransfer(errorCode); |
||
779 | } |
||
780 | } |
||
781 | else |
||
782 | { |
||
783 | _USBHostCDC_SetNextSubState(); |
||
784 | } |
||
785 | |||
786 | break; |
||
787 | case SUBSTATE_WRITE_REQ_WAIT: |
||
788 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, &errorCode, &byteCount )) |
||
789 | { |
||
790 | if (errorCode) |
||
791 | { |
||
792 | if(USB_ENDPOINT_STALLED == errorCode) |
||
793 | { |
||
794 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
795 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
796 | deviceInfoCDC[i].flags.bfReset = 1; |
||
797 | _USBHostCDC_ResetStateJump( i ); |
||
798 | } |
||
799 | else |
||
800 | { |
||
801 | _USBHostCDC_TerminateTransfer(errorCode); |
||
802 | } |
||
803 | } |
||
804 | else |
||
805 | { |
||
806 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
807 | // this is to check if there are any remaining data bytes to be transferred |
||
808 | if(deviceInfoCDC[i].remainingBytes != 0) |
||
809 | { |
||
810 | // set up another OUT request |
||
811 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_SEND_WRITE_REQ; |
||
812 | } |
||
813 | else |
||
814 | { |
||
815 | // else goto wait state |
||
816 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ; |
||
817 | } |
||
818 | } |
||
819 | } |
||
820 | |||
821 | break; |
||
822 | |||
823 | default : |
||
824 | break; |
||
825 | } |
||
826 | break; |
||
827 | |||
828 | case STATE_CDC_RESET_RECOVERY: |
||
829 | switch (deviceInfoCDC[i].state & SUBSTATE_MASK) |
||
830 | { |
||
831 | case SUBSTATE_SEND_RESET: /* Not sure of rest request */ |
||
832 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
833 | USB_CDC_RESET, 0, deviceInfoCDC[i].interface, 0, NULL, USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID ); |
||
834 | |||
835 | if (errorCode) |
||
836 | { |
||
837 | if(USB_ENDPOINT_STALLED == errorCode) |
||
838 | { |
||
839 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, 0 ); |
||
840 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
841 | deviceInfoCDC[i].flags.bfReset = 1; |
||
842 | _USBHostCDC_ResetStateJump( i ); |
||
843 | } |
||
844 | else |
||
845 | { |
||
846 | _USBHostCDC_TerminateTransfer(errorCode); |
||
847 | } |
||
848 | } |
||
849 | else |
||
850 | { |
||
851 | _USBHostCDC_SetNextSubState(); |
||
852 | } |
||
853 | break; |
||
854 | case SUBSTATE_WAIT_FOR_RESET: |
||
855 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
856 | { |
||
857 | if (errorCode) |
||
858 | { |
||
859 | if(USB_ENDPOINT_STALLED == errorCode) |
||
860 | { |
||
861 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, 0 ); |
||
862 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
863 | deviceInfoCDC[i].flags.bfReset = 1; |
||
864 | _USBHostCDC_ResetStateJump( i ); |
||
865 | } |
||
866 | else |
||
867 | { |
||
868 | _USBHostCDC_TerminateTransfer(errorCode); |
||
869 | } |
||
870 | } |
||
871 | else |
||
872 | { |
||
873 | deviceInfoCDC[i].flags.bfReset = 0; |
||
874 | _USBHostCDC_ResetStateJump( i ); |
||
875 | } |
||
876 | } |
||
877 | break; |
||
878 | |||
879 | case SUBSTATE_RESET_COMPLETE: |
||
880 | _USBHostCDC_ResetStateJump( i ); |
||
881 | break; |
||
882 | } |
||
883 | break; |
||
884 | |||
885 | case STATE_HOLDING: |
||
886 | break; |
||
887 | |||
888 | default : |
||
889 | break; |
||
890 | |||
891 | } |
||
892 | } |
||
893 | #endif |
||
894 | } |
||
895 | |||
896 | |||
897 | /******************************************************************************* |
||
898 | Function: |
||
899 | USBHostCDCTransfer( BYTE deviceAddress, BYTE direction, BYTE reportid, |
||
900 | BYTE size, BYTE *data) |
||
901 | |||
902 | Summary: |
||
903 | This function starts a CDC transfer. |
||
904 | |||
905 | Description: |
||
906 | This function starts a CDC transfer. A read/write wrapper is provided in |
||
907 | application interface file to access this function. This function is used for |
||
908 | transfer requests over both Communication interface and data interface. |
||
909 | |||
910 | Preconditions: |
||
911 | None |
||
912 | |||
913 | Parameters: |
||
914 | BYTE deviceAddress - Device address |
||
915 | BYTE direction - 1=read, 0=write |
||
916 | BYTE interfaceNum - Interface number of the device |
||
917 | BYTE size - Byte size of the data buffer |
||
918 | BYTE *data - Pointer to the data buffer |
||
919 | BYTE endpointDATA - Endpoint details over which transfer is requested |
||
920 | |||
921 | Return Values: |
||
922 | USB_SUCCESS - Request started successfully |
||
923 | USB_CDC_DEVICE_NOT_FOUND - No device with specified address |
||
924 | USB_CDC_DEVICE_BUSY - Device not in proper state for |
||
925 | performing a transfer |
||
926 | Remarks: |
||
927 | None |
||
928 | *******************************************************************************/ |
||
929 | |||
930 | BYTE USBHostCDCTransfer( BYTE deviceAddress,BYTE request , BYTE direction, BYTE interfaceNum, WORD size, BYTE *data , BYTE endpointDATA) |
||
931 | { |
||
932 | BYTE i; |
||
933 | #ifdef USB_ENABLE_TRANSFER_EVENT |
||
934 | BYTE errorCode; |
||
935 | #endif |
||
936 | // Find the correct device. |
||
937 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != deviceAddress); i++); |
||
938 | if (i == USB_MAX_CDC_DEVICES) |
||
939 | { |
||
940 | return USB_CDC_DEVICE_NOT_FOUND; |
||
941 | } |
||
942 | |||
943 | // Make sure the device is in a state ready to read/write. |
||
944 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
945 | if (deviceInfoCDC[i].state != (STATE_RUNNING) && |
||
946 | (deviceInfoCDC[i].state & SUBSTATE_MASK) != (SUBSTATE_WAITING_FOR_REQ)) |
||
947 | #else |
||
948 | if (deviceInfoCDC[i].state != STATE_RUNNING) |
||
949 | #endif |
||
950 | { |
||
951 | return USB_CDC_DEVICE_BUSY; |
||
952 | } |
||
953 | |||
954 | // Initialize the transfer information. |
||
955 | deviceInfoCDC[i].bytesTransferred = 0; |
||
956 | deviceInfoCDC[i].errorCode = USB_SUCCESS; |
||
957 | deviceInfoCDC[i].userData = data; |
||
958 | deviceInfoCDC[i].reportSize = size; |
||
959 | deviceInfoCDC[i].remainingBytes = size; |
||
960 | deviceInfoCDC[i].interface = interfaceNum; |
||
961 | deviceInfoCDC[i].endpointDATA = endpointDATA; |
||
962 | deviceInfoCDC[i].commRequest = request; // invalid entry if DATA transfer is requested |
||
963 | #ifdef DEBUG_MODE |
||
964 | UART2PrintString( "Data EP: " ); |
||
965 | UART2PutHex( deviceInfoCDC[i].endpointDATA ); |
||
966 | UART2PrintString( "\r\n" ); |
||
967 | #endif |
||
968 | |||
969 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
970 | // Jump to the transfer state. |
||
971 | if(!direction) |
||
972 | { |
||
973 | /* send write req */ |
||
974 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_SEND_WRITE_REQ; |
||
975 | } |
||
976 | else |
||
977 | { |
||
978 | deviceInfoCDC[i].state = STATE_RUNNING | SUBSTATE_SEND_READ_REQ; |
||
979 | } |
||
980 | #else |
||
981 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
982 | if(!direction) |
||
983 | { |
||
984 | if(deviceInfoCDC[i].endpointDATA == 0x00)// if endpoint 0 then use control transfer for COMM interface request |
||
985 | { |
||
986 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
987 | request, 0, deviceInfoCDC[i].interface,deviceInfoCDC[i].reportSize, deviceInfoCDC[i].userData, |
||
988 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID ); |
||
989 | deviceInfoCDC[i].state = STATE_WRITE_REQ_WAIT; |
||
990 | } |
||
991 | else |
||
992 | { |
||
993 | errorCode = USBHostWrite( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, |
||
994 | deviceInfoCDC[i].userData, deviceInfoCDC[i].reportSize ); |
||
995 | deviceInfoCDC[i].state = STATE_WRITE_REQ_WAIT; |
||
996 | } |
||
997 | } |
||
998 | else |
||
999 | { |
||
1000 | if(deviceInfoCDC[i].endpointDATA == 0x00)// if endpoint 0 then use control transfer for COMM interface request |
||
1001 | { |
||
1002 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1003 | request, 0, deviceInfoCDC[i].interface,deviceInfoCDC[i].reportSize, deviceInfoCDC[i].userData, |
||
1004 | USB_DEVICE_REQUEST_GET , deviceInfoCDC[i].clientDriverID ); |
||
1005 | deviceInfoCDC[i].state = STATE_READ_REQ_WAIT; |
||
1006 | } |
||
1007 | else |
||
1008 | { |
||
1009 | errorCode = USBHostRead( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, |
||
1010 | deviceInfoCDC[i].userData, deviceInfoCDC[i].reportSize ); |
||
1011 | deviceInfoCDC[i].state = STATE_READ_REQ_WAIT; |
||
1012 | } |
||
1013 | } |
||
1014 | |||
1015 | if(errorCode) |
||
1016 | { |
||
1017 | _USBHostCDC_TerminateTransfer( USB_CDC_RESET_ERROR ); |
||
1018 | } |
||
1019 | else |
||
1020 | { |
||
1021 | deviceInfoCDC[i].flags.bfReset = 0; |
||
1022 | } |
||
1023 | #endif |
||
1024 | return USB_SUCCESS; |
||
1025 | } |
||
1026 | /******************************************************************************* |
||
1027 | Function: |
||
1028 | BOOL USBHostCDCTransferIsComplete( BYTE deviceAddress, |
||
1029 | BYTE *errorCode, DWORD *byteCount ) |
||
1030 | |||
1031 | Summary: |
||
1032 | This function indicates whether or not the last transfer is complete. |
||
1033 | |||
1034 | Description: |
||
1035 | This function indicates whether or not the last transfer is complete. |
||
1036 | If the functions returns TRUE, the returned byte count and error |
||
1037 | code are valid. Since only one transfer can be performed at once |
||
1038 | and only one endpoint can be used, we only need to know the |
||
1039 | device address. |
||
1040 | |||
1041 | Precondition: |
||
1042 | None |
||
1043 | |||
1044 | Parameters: |
||
1045 | BYTE deviceAddress - Device address |
||
1046 | BYTE *errorCode - Error code from last transfer |
||
1047 | DWORD *byteCount - Number of bytes transferred |
||
1048 | |||
1049 | Return Values: |
||
1050 | TRUE - Transfer is complete, errorCode is valid |
||
1051 | FALSE - Transfer is not complete, errorCode is not valid |
||
1052 | *******************************************************************************/ |
||
1053 | |||
1054 | BOOL USBHostCDCTransferIsComplete( BYTE deviceAddress, BYTE *errorCode, BYTE *byteCount ) |
||
1055 | { |
||
1056 | BYTE i; |
||
1057 | |||
1058 | // Find the correct device. |
||
1059 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != deviceAddress); i++); |
||
1060 | if ((i == USB_MAX_CDC_DEVICES) || (deviceInfoCDC[i].state == STATE_DETACHED)) |
||
1061 | { |
||
1062 | *errorCode = USB_CDC_DEVICE_NOT_FOUND; |
||
1063 | *byteCount = 0; |
||
1064 | return TRUE; |
||
1065 | } |
||
1066 | |||
1067 | *byteCount = deviceInfoCDC[i].bytesTransferred; |
||
1068 | *errorCode = deviceInfoCDC[i].errorCode; |
||
1069 | |||
1070 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
1071 | if(deviceInfoCDC[i].state == (STATE_RUNNING | SUBSTATE_WAITING_FOR_REQ)) |
||
1072 | #else |
||
1073 | if(deviceInfoCDC[i].state == STATE_RUNNING) |
||
1074 | #endif |
||
1075 | { |
||
1076 | return TRUE; |
||
1077 | } |
||
1078 | else |
||
1079 | { |
||
1080 | return FALSE; |
||
1081 | } |
||
1082 | } |
||
1083 | |||
1084 | // ***************************************************************************** |
||
1085 | // ***************************************************************************** |
||
1086 | // Host Stack Interface Functions |
||
1087 | // ***************************************************************************** |
||
1088 | // ***************************************************************************** |
||
1089 | |||
1090 | /******************************************************************************* |
||
1091 | Function: |
||
1092 | BOOL USBHostCDCEventHandler( BYTE address, USB_EVENT event, |
||
1093 | void *data, DWORD size ) |
||
1094 | |||
1095 | Precondition: |
||
1096 | The device has been initialized. |
||
1097 | |||
1098 | Summary: |
||
1099 | This function is the event handler for this client driver. |
||
1100 | |||
1101 | Description: |
||
1102 | This function is the event handler for this client driver. It is called |
||
1103 | by the host layer when various events occur. |
||
1104 | |||
1105 | Parameters: |
||
1106 | BYTE address - Address of the device |
||
1107 | USB_EVENT event - Event that has occurred |
||
1108 | void *data - Pointer to data pertinent to the event |
||
1109 | DWORD size - Size of the data |
||
1110 | |||
1111 | Return Values: |
||
1112 | TRUE - Event was handled |
||
1113 | FALSE - Event was not handled |
||
1114 | |||
1115 | Remarks: |
||
1116 | None |
||
1117 | *******************************************************************************/ |
||
1118 | BOOL USBHostCDCEventHandler( BYTE address, USB_EVENT event, void *data, DWORD size ) |
||
1119 | { |
||
1120 | HOST_TRANSFER_DATA* transfer_data = data ; |
||
1121 | BYTE i; |
||
1122 | #ifdef USB_ENABLE_TRANSFER_EVENT |
||
1123 | |||
1124 | BYTE errorCode = 0 ; |
||
1125 | DWORD byteCount = 0; |
||
1126 | |||
1127 | #endif |
||
1128 | |||
1129 | switch (event) |
||
1130 | { |
||
1131 | case EVENT_NONE: // No event occured (NULL event) |
||
1132 | USBTasks(); |
||
1133 | return TRUE; |
||
1134 | break; |
||
1135 | |||
1136 | case EVENT_DETACH: // USB cable has been detached (data: BYTE, address of device) |
||
1137 | #ifdef DEBUG_MODE |
||
1138 | UART2PrintString( "CDC: Detach\r\n" ); |
||
1139 | #endif |
||
1140 | // Find the device in the table. If found, clear the important fields. |
||
1141 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != address); i++); |
||
1142 | if (i < USB_MAX_CDC_DEVICES) |
||
1143 | { |
||
1144 | deviceInfoCDC[i].deviceAddress = 0; |
||
1145 | deviceInfoCDC[i].state = STATE_DETACHED; |
||
1146 | CDCdeviceAddress = 0; |
||
1147 | /* Free the memory used by the CDC device */ |
||
1148 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_DETACH,NULL, 0); |
||
1149 | } |
||
1150 | |||
1151 | return TRUE; |
||
1152 | break; |
||
1153 | |||
1154 | case EVENT_HUB_ATTACH: // USB hub has been attached |
||
1155 | case EVENT_TRANSFER: // A USB transfer has completed - NOT USED |
||
1156 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
1157 | #ifdef DEBUG_MODE |
||
1158 | UART2PrintString( "CDC: transfer event: " ); |
||
1159 | UART2PutHex( address ); |
||
1160 | UART2PrintString( "\r\n" ); |
||
1161 | #endif |
||
1162 | |||
1163 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != address); i++) {} |
||
1164 | if (i == USB_MAX_CDC_DEVICES) |
||
1165 | { |
||
1166 | #ifdef DEBUG_MODE |
||
1167 | UART2PrintString( "CDC: Unknown device\r\n" ); |
||
1168 | #endif |
||
1169 | return FALSE; |
||
1170 | } |
||
1171 | #ifdef DEBUG_MODE |
||
1172 | UART2PrintString( "CDC: Device state: " ); |
||
1173 | UART2PutHex( deviceInfoCDC[i].state ); |
||
1174 | UART2PrintString( "\r\n" ); |
||
1175 | #endif |
||
1176 | switch (deviceInfoCDC[i].state) |
||
1177 | { |
||
1178 | |||
1179 | case STATE_WAIT_FOR_GET_LINE_CODING : |
||
1180 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
1181 | { |
||
1182 | if (errorCode) |
||
1183 | { |
||
1184 | /* Set error code */ |
||
1185 | _USBHostCDC_TerminateTransfer(errorCode); |
||
1186 | } |
||
1187 | else |
||
1188 | { |
||
1189 | // Clear the STALL. Since it is EP0, we do not have to clear the stall. |
||
1190 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, 0 ); |
||
1191 | // Compare with Line coding expected at Host end with the one received from device |
||
1192 | if(0 == memcmp(&CDC_DEV_LINE_CODING_Buffer, PTR_HOST_LINE_CODING_BUFFER, USB_CDC_LINE_CODING_LENGTH)) |
||
1193 | { |
||
1194 | // if fine goto set Control line state |
||
1195 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1196 | USB_CDC_SET_CONTROL_LINE_STATE , 0 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_CONTROL_LINE_LENGTH, (BYTE*)PTR_HOST_CONTROL_LINE_BUFFER, |
||
1197 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID )) |
||
1198 | { |
||
1199 | deviceInfoCDC[i].state = STATE_WAIT_FOR_SET_CONTROL_LINE_STATE; |
||
1200 | } |
||
1201 | else |
||
1202 | { |
||
1203 | _USBHostCDC_TerminateTransfer(USB_CDC_COMMAND_FAILED); |
||
1204 | } |
||
1205 | } |
||
1206 | else |
||
1207 | { |
||
1208 | // if data not as expected goto Set Line coding |
||
1209 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1210 | USB_CDC_SET_LINE_CODING, 1 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_LINE_CODING_LENGTH, (BYTE*)PTR_HOST_LINE_CODING_BUFFER, |
||
1211 | USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID )) |
||
1212 | { |
||
1213 | deviceInfoCDC[i].state = STATE_WAIT_FOR_SET_LINE_CODING; |
||
1214 | } |
||
1215 | else |
||
1216 | { |
||
1217 | _USBHostCDC_TerminateTransfer(USB_CDC_COMMAND_FAILED); |
||
1218 | } |
||
1219 | } |
||
1220 | } |
||
1221 | } |
||
1222 | |||
1223 | break; |
||
1224 | case STATE_WAIT_FOR_SET_LINE_CODING : |
||
1225 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
1226 | { |
||
1227 | if (errorCode) |
||
1228 | { |
||
1229 | /* Set error code */ |
||
1230 | _USBHostCDC_TerminateTransfer(errorCode); |
||
1231 | } |
||
1232 | else |
||
1233 | { // check again if line coding is correctly set |
||
1234 | // Send GET Line Coding request to the device |
||
1235 | if(!USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1236 | USB_CDC_GET_LINE_CODING, 0 , deviceInfoCDC[i].commInterface.interfaceNum , USB_CDC_LINE_CODING_LENGTH, (BYTE*)&CDC_DEV_LINE_CODING_Buffer, |
||
1237 | USB_DEVICE_REQUEST_GET , deviceInfoCDC[i].clientDriverID )) |
||
1238 | { |
||
1239 | // wait for transfer event for and then decide to alter the settings or not |
||
1240 | deviceInfoCDC[i].state = STATE_WAIT_FOR_GET_LINE_CODING; |
||
1241 | } |
||
1242 | else |
||
1243 | { |
||
1244 | _USBHostCDC_TerminateTransfer(USB_CDC_COMMAND_FAILED); |
||
1245 | } |
||
1246 | } |
||
1247 | } |
||
1248 | |||
1249 | break; |
||
1250 | |||
1251 | case STATE_WAIT_FOR_SET_CONTROL_LINE_STATE : |
||
1252 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, 0, &errorCode, &byteCount )) |
||
1253 | { |
||
1254 | if (errorCode) |
||
1255 | { |
||
1256 | /* Set error code */ |
||
1257 | _USBHostCDC_TerminateTransfer(errorCode); |
||
1258 | } |
||
1259 | else |
||
1260 | { |
||
1261 | // device is ready to TX/RX data |
||
1262 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_ATTACH,NULL, 0); |
||
1263 | deviceInfoCDC[i].state = STATE_RUNNING; |
||
1264 | } |
||
1265 | } |
||
1266 | |||
1267 | break; |
||
1268 | |||
1269 | case STATE_READ_REQ_WAIT : |
||
1270 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, &errorCode, &byteCount )) |
||
1271 | { |
||
1272 | if (errorCode) |
||
1273 | { |
||
1274 | if(USB_ENDPOINT_STALLED == errorCode) |
||
1275 | { |
||
1276 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
1277 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
1278 | deviceInfoCDC[i].flags.bfReset = 1; |
||
1279 | _USBHostCDC_ResetStateJump( i ); |
||
1280 | } |
||
1281 | else |
||
1282 | { |
||
1283 | _USBHostCDC_TerminateTransfer(errorCode); |
||
1284 | } |
||
1285 | } |
||
1286 | else |
||
1287 | { |
||
1288 | // Clear the STALL. Since it is EP0, we do not have to clear the stall. |
||
1289 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
1290 | deviceInfoCDC[i].bytesTransferred = byteCount; /* Can compare with report size and flag error ???*/ |
||
1291 | deviceInfoCDC[i].state = STATE_RUNNING; |
||
1292 | if(deviceInfoCDC[i].endpointDATA == 0x00) |
||
1293 | { |
||
1294 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_COMM_READ_DONE,NULL, 0); |
||
1295 | } |
||
1296 | else |
||
1297 | { // read request was for data interface |
||
1298 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_DATA_READ_DONE,NULL, 0); |
||
1299 | } |
||
1300 | } |
||
1301 | } |
||
1302 | #ifdef DEBUG_MODE |
||
1303 | UART2PrintString("|"); |
||
1304 | #endif |
||
1305 | break; |
||
1306 | |||
1307 | case STATE_WRITE_REQ_WAIT : |
||
1308 | if (USBHostTransferIsComplete( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA, &errorCode, &byteCount )) |
||
1309 | { |
||
1310 | if (errorCode) |
||
1311 | { |
||
1312 | if(USB_ENDPOINT_STALLED == errorCode) |
||
1313 | { |
||
1314 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
1315 | deviceInfoCDC[i].returnState = STATE_HOLDING; |
||
1316 | deviceInfoCDC[i].flags.bfReset = 1; |
||
1317 | _USBHostCDC_ResetStateJump( i ); |
||
1318 | } |
||
1319 | else |
||
1320 | { |
||
1321 | _USBHostCDC_TerminateTransfer(errorCode); |
||
1322 | } |
||
1323 | } |
||
1324 | else |
||
1325 | { |
||
1326 | // Clear the STALL. Since it is EP0, we do not have to clear the stall. |
||
1327 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
1328 | deviceInfoCDC[i].bytesTransferred = byteCount; /* Can compare with report size and flag error ???*/ |
||
1329 | deviceInfoCDC[i].state = STATE_RUNNING; |
||
1330 | if(deviceInfoCDC[i].endpointDATA == 0x00) |
||
1331 | { |
||
1332 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_COMM_WRITE_DONE,NULL, 0); |
||
1333 | } |
||
1334 | else |
||
1335 | { // read request was for data interface |
||
1336 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_DATA_WRITE_DONE,NULL, 0); |
||
1337 | } |
||
1338 | } |
||
1339 | } |
||
1340 | #ifdef DEBUG_MODE |
||
1341 | UART2PrintString("|"); |
||
1342 | #endif |
||
1343 | break; |
||
1344 | |||
1345 | default: |
||
1346 | return FALSE; |
||
1347 | } |
||
1348 | #endif |
||
1349 | case EVENT_SOF: // Start of frame - NOT NEEDED |
||
1350 | case EVENT_RESUME: // Device-mode resume received |
||
1351 | case EVENT_SUSPEND: // Device-mode suspend/idle event received |
||
1352 | case EVENT_RESET: // Device-mode bus reset received |
||
1353 | case EVENT_STALL: // A stall has occured |
||
1354 | return TRUE; |
||
1355 | break; |
||
1356 | case EVENT_BUS_ERROR: // BUS error has occurred |
||
1357 | for (i=0; (i<USB_MAX_CDC_DEVICES) && (deviceInfoCDC[i].deviceAddress != address); i++); |
||
1358 | if (i < USB_MAX_CDC_DEVICES) |
||
1359 | { |
||
1360 | if(transfer_data->bErrorCode == USB_ENDPOINT_NAK_TIMEOUT) |
||
1361 | { |
||
1362 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_NAK_TIMEOUT,NULL, 0); |
||
1363 | deviceInfoCDC[i].state = STATE_RUNNING; |
||
1364 | USBHostClearEndpointErrors( deviceInfoCDC[i].deviceAddress, deviceInfoCDC[i].endpointDATA ); |
||
1365 | return TRUE; |
||
1366 | } |
||
1367 | } |
||
1368 | break; |
||
1369 | |||
1370 | default: |
||
1371 | return FALSE; |
||
1372 | break; |
||
1373 | } |
||
1374 | return FALSE; |
||
1375 | } |
||
1376 | |||
1377 | /******************************************************************************* |
||
1378 | Function: |
||
1379 | BOOL USBHostCDCInitialize( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
1380 | |||
1381 | Summary: |
||
1382 | This function is the initialization routine for this client driver. |
||
1383 | |||
1384 | Description: |
||
1385 | This function is the initialization routine for this client driver. It |
||
1386 | is called by the host layer when the USB device is being enumerated.For a |
||
1387 | CDC device we need to look into CDC descriptor, interface descriptor and |
||
1388 | endpoint descriptor. This will decode information for Communication and |
||
1389 | Data interface. |
||
1390 | |||
1391 | Precondition: |
||
1392 | None |
||
1393 | |||
1394 | Parameters: |
||
1395 | BYTE address - Address of the new device |
||
1396 | DWORD flags - Initialization flags |
||
1397 | BYTE clientDriverID - Client driver identification for device requests |
||
1398 | |||
1399 | Return Values: |
||
1400 | TRUE - We can support the device. |
||
1401 | FALSE - We cannot support the device. |
||
1402 | |||
1403 | Remarks: |
||
1404 | None |
||
1405 | *******************************************************************************/ |
||
1406 | BOOL USBHostCDCInitialize( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
1407 | { |
||
1408 | BYTE *descriptor = NULL; |
||
1409 | WORD i = 0; |
||
1410 | WORD endpointINsize = 0; |
||
1411 | WORD endpointOUTsize = 0; |
||
1412 | BYTE device = 0; |
||
1413 | BYTE endpointIN = 0; |
||
1414 | BYTE endpointOUT = 0; |
||
1415 | BYTE validCommInterface = 0; |
||
1416 | BYTE validDataInterface = 0; |
||
1417 | |||
1418 | #ifdef DEBUG_MODE |
||
1419 | UART2PrintString( "CDC: USBHostCDCInitialize(0x" ); |
||
1420 | UART2PutHex( flags ); |
||
1421 | UART2PrintString( ")\r\n" ); |
||
1422 | #endif |
||
1423 | |||
1424 | // Find the device in the table. If we cannot find it, return an error. |
||
1425 | #ifdef DEBUG_MODE |
||
1426 | UART2PrintString("CDC: Selecting configuration...\r\n" ); |
||
1427 | #endif |
||
1428 | for (device = 0; (device < USB_MAX_CDC_DEVICES) ; device++) |
||
1429 | { |
||
1430 | if(deviceInfoCDC[device].deviceAddress == address) |
||
1431 | return TRUE; |
||
1432 | } |
||
1433 | |||
1434 | for (device = 0; (device < USB_MAX_CDC_DEVICES) && (deviceInfoCDC[device].deviceAddress != 0); device++); |
||
1435 | if (device == USB_MAX_CDC_DEVICES) |
||
1436 | { |
||
1437 | #ifdef DEBUG_MODE |
||
1438 | UART2PrintString("CDC: Not in the table!\r\n" ); |
||
1439 | #endif |
||
1440 | return FALSE; |
||
1441 | } |
||
1442 | |||
1443 | USBHostCDC_Init_CDC_Buffers(); |
||
1444 | descriptor = USBHostGetCurrentConfigurationDescriptor( address ); |
||
1445 | |||
1446 | i = 0; |
||
1447 | |||
1448 | #ifdef DEBUG_MODE |
||
1449 | UART2PrintString("CDC: Checking descriptor " ); |
||
1450 | UART2PutDec( descriptor[i+5] ); |
||
1451 | UART2PrintString(" ...\r\n" ); |
||
1452 | #endif |
||
1453 | |||
1454 | // Total no of interfaces |
||
1455 | deviceInfoCDC[device].noOfInterfaces = descriptor[i+4] ; |
||
1456 | |||
1457 | i += descriptor[i]; // skip configuration descriptor |
||
1458 | |||
1459 | // Set current configuration to this configuration. We can change it later. |
||
1460 | |||
1461 | |||
1462 | // Find the next interface descriptor. |
||
1463 | while (i < ((USB_CONFIGURATION_DESCRIPTOR *)descriptor)->wTotalLength) |
||
1464 | { |
||
1465 | #ifdef DEBUG_MODE |
||
1466 | UART2PrintString("CDC: Checking interface...\r\n" ); |
||
1467 | #endif |
||
1468 | // initialize device address |
||
1469 | deviceInfoCDC[device].deviceAddress = address; |
||
1470 | |||
1471 | // See if we are pointing to an interface descriptor. |
||
1472 | if (descriptor[i+1] == USB_DESCRIPTOR_INTERFACE) |
||
1473 | { |
||
1474 | // See if the interface is a CDC - Communicaton Interface. |
||
1475 | if ((descriptor[i+5] == USB_CDC_COMM_INTF) || (descriptor[i+5] == 0xFF)) |
||
1476 | { |
||
1477 | validCommInterface = 1; |
||
1478 | deviceInfoCDC[device].commInterface.interfaceNum = descriptor[i+2]; |
||
1479 | deviceInfoCDC[device].commInterface.noOfEndpoints = descriptor[i+4]; |
||
1480 | // Since control has reached here , implies subclass & protocol is supported by the host |
||
1481 | |||
1482 | // look for class specific functional descriptors |
||
1483 | i += descriptor[i]; // skip interface descriptor |
||
1484 | |||
1485 | // parse all function descriptors |
||
1486 | while(USB_CDC_CS_INTERFACE == descriptor[i+1] ) |
||
1487 | { |
||
1488 | switch(descriptor[i+2]) |
||
1489 | { |
||
1490 | case USB_CDC_DSC_FN_HEADER : |
||
1491 | deviceInfoCDC[device].commInterface.Header_Fn_Dsc = |
||
1492 | (*((USB_CDC_HEADER_FN_DSC*)&descriptor[i])); |
||
1493 | break; |
||
1494 | |||
1495 | case USB_CDC_DSC_FN_CALL_MGT : |
||
1496 | deviceInfoCDC[device].commInterface.Call_Mgt_Fn_Desc = |
||
1497 | (*((USB_CDC_CALL_MGT_FN_DSC*)&descriptor[i])); |
||
1498 | break; |
||
1499 | |||
1500 | case USB_CDC_DSC_FN_ACM : |
||
1501 | deviceInfoCDC[device].commInterface.ACM_Fn_Desc = |
||
1502 | (*((USB_CDC_ACM_FN_DSC*)&descriptor[i])); |
||
1503 | break; |
||
1504 | |||
1505 | case USB_CDC_DSC_FN_UNION : |
||
1506 | deviceInfoCDC[device].commInterface.Union_Fn_Desc = |
||
1507 | (*((USB_CDC_UNION_FN_DSC*)&descriptor[i])); |
||
1508 | break; |
||
1509 | |||
1510 | default : // function not supported -- flag error/ return false |
||
1511 | break; |
||
1512 | } |
||
1513 | |||
1514 | i += descriptor[i]; // goto next function descriptor |
||
1515 | } |
||
1516 | |||
1517 | if(descriptor[i+1] == USB_DESCRIPTOR_ENDPOINT) |
||
1518 | { |
||
1519 | if(descriptor[i+3] == 0x03) // Communication Interface uses Interrupt endpoint |
||
1520 | { |
||
1521 | if (((descriptor[i+2] & 0x80) == 0x80) && (deviceInfoCDC[device].commInterface.endpointIN == 0)) |
||
1522 | { |
||
1523 | deviceInfoCDC[device].commInterface.endpointIN = descriptor[i+2]; |
||
1524 | deviceInfoCDC[device].commInterface.endpointMaxDataSize = ((descriptor[i+4]) | |
||
1525 | (descriptor[i+5] << 8)); |
||
1526 | deviceInfoCDC[device].commInterface.endpointPollInterval = descriptor[i+6]; |
||
1527 | } |
||
1528 | } |
||
1529 | i += descriptor[i]; |
||
1530 | } |
||
1531 | else |
||
1532 | { |
||
1533 | // communication interface normaly has one endpoint. |
||
1534 | // Notification element is optional - currently not supported. |
||
1535 | } |
||
1536 | |||
1537 | if (((descriptor[i+1] == USB_DESCRIPTOR_INTERFACE) && (descriptor[i+5] == USB_CDC_DATA_INTF)) || |
||
1538 | (descriptor[i+1] == USB_DESCRIPTOR_ENDPOINT)) |
||
1539 | |||
1540 | { |
||
1541 | if(descriptor[i+5] == USB_CDC_DATA_INTF) |
||
1542 | { |
||
1543 | validDataInterface = 1; |
||
1544 | deviceInfoCDC[device].dataInterface.interfaceNum = descriptor[i+2]; |
||
1545 | deviceInfoCDC[device].dataInterface.noOfEndpoints = descriptor[i+4]; |
||
1546 | |||
1547 | i += descriptor[i]; // goto endpoint descriptors |
||
1548 | } |
||
1549 | else |
||
1550 | { |
||
1551 | // Some CDC devices do not list DATA interface descriptor seperately |
||
1552 | // DATA endpoints are mentioned along with Communicaton interface |
||
1553 | validDataInterface = 1; |
||
1554 | deviceInfoCDC[device].dataInterface.interfaceNum = deviceInfoCDC[device].commInterface.interfaceNum; |
||
1555 | deviceInfoCDC[device].dataInterface.noOfEndpoints = deviceInfoCDC[device].commInterface.noOfEndpoints - 1; |
||
1556 | } |
||
1557 | // Look for bulk or isochronous , IN and OUT endpoints. |
||
1558 | endpointIN = 0; |
||
1559 | endpointOUT = 0; |
||
1560 | |||
1561 | while(descriptor[i+1] == USB_DESCRIPTOR_ENDPOINT) |
||
1562 | { // Data Interface uses Bulk or Iscochronous endpoint |
||
1563 | deviceInfoCDC[device].dataInterface.endpointType = descriptor[i+3]; |
||
1564 | if((descriptor[i+3] == 0x01)||(descriptor[i+3] == 0x02)) |
||
1565 | { |
||
1566 | if (((descriptor[i+2] & 0x80) == 0x80) && (endpointIN == 0)) |
||
1567 | { |
||
1568 | endpointIN = descriptor[i+2]; |
||
1569 | endpointINsize = ((descriptor[i+4])|(descriptor[i+5] << 8)); |
||
1570 | } |
||
1571 | if (((descriptor[i+2] & 0x80) == 0x00) && (endpointOUT == 0)) |
||
1572 | { |
||
1573 | endpointOUT = descriptor[i+2]; |
||
1574 | endpointOUTsize = ((descriptor[i+4])|(descriptor[i+5] << 8)); |
||
1575 | } |
||
1576 | |||
1577 | } |
||
1578 | i += descriptor[i]; |
||
1579 | } |
||
1580 | |||
1581 | if ((endpointIN != 0) || (endpointOUT != 0)) // normally endpoint should be in pair of same type |
||
1582 | { |
||
1583 | deviceInfoCDC[device].dataInterface.endpointIN = endpointIN; |
||
1584 | deviceInfoCDC[device].dataInterface.endpointInDataSize = endpointINsize; |
||
1585 | |||
1586 | deviceInfoCDC[device].dataInterface.endpointOUT = endpointOUT; |
||
1587 | deviceInfoCDC[device].dataInterface.endpointOutDataSize = endpointOUTsize; |
||
1588 | |||
1589 | USBHostSetNAKTimeout( address, endpointIN, 1, USB_NUM_BULK_NAKS ); |
||
1590 | USBHostSetNAKTimeout( address, endpointOUT, 1, USB_NUM_BULK_NAKS ); |
||
1591 | } |
||
1592 | else |
||
1593 | { |
||
1594 | validDataInterface = 0; |
||
1595 | } |
||
1596 | } // if data interface |
||
1597 | } // communication interface loop |
||
1598 | |||
1599 | } // if interface descriptor |
||
1600 | |||
1601 | // Jump to the next descriptor in this configuration. |
||
1602 | i += descriptor[i]; |
||
1603 | } // main loop - to parse descriptors |
||
1604 | // ACM subclass must support a valid communication and data interfaces |
||
1605 | if((validDataInterface == 0) ||(validCommInterface == 0)) |
||
1606 | { |
||
1607 | return FALSE; |
||
1608 | } |
||
1609 | |||
1610 | deviceInfoCDC[device].clientDriverID = clientDriverID; |
||
1611 | |||
1612 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
1613 | deviceInfoCDC[device].state = STATE_INITIALIZE_DEVICE; |
||
1614 | #else |
||
1615 | // Initialize device address for application use |
||
1616 | USBHostCDCInitAddress( deviceInfoCDC[device].deviceAddress,0,deviceInfoCDC[device].clientDriverID ); |
||
1617 | // Send GET Line Coding request to the device |
||
1618 | if(USBHostIssueDeviceRequest( deviceInfoCDC[device].deviceAddress, USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1619 | USB_CDC_GET_LINE_CODING, 0 , deviceInfoCDC[device].commInterface.interfaceNum , USB_CDC_LINE_CODING_LENGTH, (BYTE*)&CDC_DEV_LINE_CODING_Buffer, |
||
1620 | USB_DEVICE_REQUEST_GET , deviceInfoCDC[device].clientDriverID )) |
||
1621 | { |
||
1622 | return FALSE; |
||
1623 | } |
||
1624 | // wait for transfer event for and then decide to alter the settings or not |
||
1625 | deviceInfoCDC[device].state = STATE_WAIT_FOR_GET_LINE_CODING; |
||
1626 | #endif |
||
1627 | return TRUE; |
||
1628 | } |
||
1629 | |||
1630 | |||
1631 | // ***************************************************************************** |
||
1632 | // ***************************************************************************** |
||
1633 | // Internal Functions |
||
1634 | // ***************************************************************************** |
||
1635 | // ***************************************************************************** |
||
1636 | |||
1637 | /******************************************************************************* |
||
1638 | Function: |
||
1639 | void _USBHostCDC_ResetStateJump( BYTE i ) |
||
1640 | |||
1641 | Summary: |
||
1642 | |||
1643 | Description: |
||
1644 | This function determines which portion of the reset processing needs to |
||
1645 | be executed next and jumps to that state. |
||
1646 | |||
1647 | Precondition: |
||
1648 | The device information must be in the deviceInfo array. |
||
1649 | |||
1650 | Parameters: |
||
1651 | BYTE i - Index into the deviceInfoMSD structure for the device to reset. |
||
1652 | |||
1653 | Returns: |
||
1654 | None |
||
1655 | |||
1656 | Remarks: |
||
1657 | None |
||
1658 | *******************************************************************************/ |
||
1659 | void _USBHostCDC_ResetStateJump( BYTE i ) |
||
1660 | { |
||
1661 | #ifdef USB_ENABLE_TRANSFER_EVENT |
||
1662 | BYTE errorCode; |
||
1663 | #endif |
||
1664 | if (deviceInfoCDC[i].flags.bfReset) |
||
1665 | { |
||
1666 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
1667 | deviceInfoCDC[i].state = STATE_CDC_RESET_RECOVERY; |
||
1668 | #else |
||
1669 | errorCode = USBHostIssueDeviceRequest( deviceInfoCDC[i].deviceAddress, USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_CLASS | USB_SETUP_RECIPIENT_INTERFACE, |
||
1670 | USB_CDC_RESET, 0, deviceInfoCDC[i].interface, 0, NULL, USB_DEVICE_REQUEST_SET , deviceInfoCDC[i].clientDriverID ); |
||
1671 | |||
1672 | if (errorCode) |
||
1673 | { |
||
1674 | //TODO Now what?? |
||
1675 | _USBHostCDC_TerminateTransfer( USB_CDC_RESET_ERROR ); |
||
1676 | } |
||
1677 | else |
||
1678 | { |
||
1679 | deviceInfoCDC[i].state = STATE_WAIT_FOR_RESET; |
||
1680 | } |
||
1681 | #endif |
||
1682 | } |
||
1683 | else |
||
1684 | { |
||
1685 | USB_HOST_APP_EVENT_HANDLER(deviceInfoCDC[i].deviceAddress,EVENT_CDC_RESET,NULL, 0); |
||
1686 | |||
1687 | deviceInfoCDC[i].state = deviceInfoCDC[i].returnState; |
||
1688 | } |
||
1689 | } |
||
1690 | |||
1691 | |||
1692 | |||
1693 | /******************************************************************************* |
||
1694 | Function: |
||
1695 | void USBHostCDC_Init_CDC_Buffers(void) |
||
1696 | |||
1697 | Summary: |
||
1698 | |||
1699 | Description: |
||
1700 | This function intializes the Line Coding buffer with values configured by |
||
1701 | application in file usb_config.h . In case application does not delcare the |
||
1702 | macros then the default values will be assigned. Baudrate, Stop Bits, Data |
||
1703 | bits & parity bits are the settings expected in the device. During enumeration |
||
1704 | if the CDC Client finds that the device is not configured to these values then |
||
1705 | CDC Client will configure the device to the required values. |
||
1706 | |||
1707 | Precondition: |
||
1708 | Configuration macros should be defined in file usb_config.h. USBConfig tool |
||
1709 | can be used to configure the CDC in ACM mode. |
||
1710 | |||
1711 | Parameters: |
||
1712 | None |
||
1713 | |||
1714 | Returns: |
||
1715 | None |
||
1716 | |||
1717 | Remarks: |
||
1718 | If the apllication does not define macros in file usb_config.h then default |
||
1719 | values defined in file usb_host_cdc.c are cosidered. |
||
1720 | *******************************************************************************/ |
||
1721 | void USBHostCDC_Init_CDC_Buffers(void) |
||
1722 | { |
||
1723 | CDC_LINE_CODING_Buffer._byte[0] = (BYTE)USB_CDC_BAUDRATE_SUPPORTED; // Initialize supported Bit rate |
||
1724 | CDC_LINE_CODING_Buffer._byte[1] = (BYTE)(USB_CDC_BAUDRATE_SUPPORTED >> 8); |
||
1725 | CDC_LINE_CODING_Buffer._byte[2] = (BYTE)(USB_CDC_BAUDRATE_SUPPORTED >> 16); |
||
1726 | CDC_LINE_CODING_Buffer._byte[3] = (BYTE)(USB_CDC_BAUDRATE_SUPPORTED >> 24); |
||
1727 | |||
1728 | CDC_LINE_CODING_Buffer.bCharFormat = USB_CDC_STOP_BITS; |
||
1729 | CDC_LINE_CODING_Buffer.bParityType = USB_CDC_PARITY_TYPE; |
||
1730 | CDC_LINE_CODING_Buffer.bDataBits = (BYTE)USB_CDC_NO_OF_DATA_BITS; // No of data bits |
||
1731 | } |
Powered by WebSVN v2.8.3