dev_mand.c
14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*HEADER******************************************************************
**************************************************************************
***
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved
***
*** This software embodies materials and concepts which are
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license
*** agreement with ARC International
***
*** $Workfile:devapi.c$
*** $Revision: 1.1 $
*** $Date: 2003/02/17 20:49:01 $
***
*** Comments:
*** This file contains the USB device API.
***
**************************************************************************
*END*********************************************************************/
#ifdef __USB_OS_MQX__
#include "mqx.h"
#include "bsp.h"
#else
#include "devapi.h"
#include "usb.h"
#include "vusb11.h"
#endif
#include "usbprv.h"
#include "usbdprv.h"
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_init
* Returned Value : USB_OK or error code
* Comments :
* The _usb_device_init routine initializes the USB API data structures.
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_init
(
/* [IN] Device number to initialize */
uint_8 device_number,
/* [OUT] the usb device handle */
_usb_device_handle _PTR_ handle,
/* [IN] number of endpoints to initialize */
uint_8 number_of_endpoints
)
{ /* Body */
uint_8 returncode;
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
#ifdef __USB_OS_MQX__
USB_CALLBACK_FUNCTIONS_STRUCT_PTR call_back_table_ptr;
#endif
if (number_of_endpoints < 1) {
return USBERR_INVALID_NUM_OF_ENDPOINTS;
} /* Endif */
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)
USB_memalloc(sizeof(USB_DEV_STATE_STRUCT));
if (usb_dev_ptr == NULL) {
return USBERR_ALLOC_STATE;
} /* Endif */
USB_memzero(usb_dev_ptr, sizeof(USB_DEV_STATE_STRUCT));
usb_dev_ptr->MAX_ENDPOINTS = number_of_endpoints;
usb_dev_ptr->DEV_NUM = device_number;
#ifdef __USB_OS_MQX__
call_back_table_ptr = (USB_CALLBACK_FUNCTIONS_STRUCT_PTR)
_mqx_get_io_component_handle(IO_USB_COMPONENT);
usb_dev_ptr->CALLBACK_STRUCT_PTR = (call_back_table_ptr + device_number);
if (!usb_dev_ptr->CALLBACK_STRUCT_PTR) {
return USBERR_DRIVER_NOT_INSTALLED;
} /* Endif */
#endif
usb_dev_ptr->XDSEND = USB_memalloc(sizeof(XD_STRUCT) * number_of_endpoints);
if (usb_dev_ptr->XDSEND == NULL) {
return USBERR_ALLOC_TRANSFER_DESCRIPTORS;
} /* Endif */
usb_dev_ptr->XDRECV = USB_memalloc(sizeof(XD_STRUCT) * number_of_endpoints);
if (usb_dev_ptr->XDRECV == NULL) {
return USBERR_ALLOC_TRANSFER_DESCRIPTORS;
} /* Endif */
usb_dev_ptr->USB_STATE = USB_STATE_POWERED;
*handle = (_usb_device_handle)usb_dev_ptr;
#ifdef __USB_OS_MQX__
returncode = ((USB_CALLBACK_FUNCTIONS_STRUCT_PTR)\
usb_dev_ptr->CALLBACK_STRUCT_PTR)->DEV_INIT(usb_dev_ptr);
#else
returncode = _usb_dci_vusb11_init(*handle);
#endif
if (returncode != USB_OK) {
/* There was an error during initialization. Free all the
** allocated memory
*/
USB_memfree((uchar_ptr)usb_dev_ptr->XDSEND);
USB_memfree((uchar_ptr)usb_dev_ptr->XDRECV);
USB_memfree((uchar_ptr)usb_dev_ptr);
return returncode;
} /* Endif */
return USB_OK;
} /* Endbody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_register_service
* Returned Value : USB_OK or error code
* Comments :
* Registers a callback routine for a specified event or endpoint.
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_register_service
(
/* [IN] Handle to the USB device */
_usb_device_handle handle,
/* [IN] type of event or endpoint number to service */
uint_8 event_endpoint,
/* [IN] Pointer to the service's callback function */
void(_CODE_PTR_ service)(pointer, boolean, uint_8, uint_8_ptr, uint_32)
)
{ /* Body */
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
SERVICE_STRUCT_PTR service_ptr;
SERVICE_STRUCT_PTR _PTR_ search_ptr;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
/* Needs mutual exclusion */
USB_lock();
/* Search for an existing entry for type */
for (search_ptr = &usb_dev_ptr->SERVICE_HEAD_PTR;
*search_ptr;
search_ptr = &(*search_ptr)->NEXT)
{
if ((*search_ptr)->TYPE == event_endpoint) {
/* Found an existing entry */
USB_unlock();
return USBERR_OPEN_SERVICE;
} /* Endif */
} /* Endfor */
/* No existing entry found - create a new one */
service_ptr = (SERVICE_STRUCT_PTR)USB_memalloc(sizeof(SERVICE_STRUCT));
if (!service_ptr) {
USB_unlock();
return USBERR_ALLOC;
} /* Endif */
service_ptr->TYPE = event_endpoint;
service_ptr->SERVICE = service;
service_ptr->NEXT = NULL;
*search_ptr = service_ptr;
USB_unlock();
return USB_OK;
} /* EndBody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_init_endpoint
* Returned Value : USB_OK or error code
* Comments :
* Initialises an endpoint
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_init_endpoint
(
/* [IN] Handle to the USB device */
_usb_device_handle handle,
/* [IN] The endpoint to initialise */
uint_8 endpoint_num,
/* [IN] The max packet size for this endpoint */
uint_16 max_packet_size,
/* [IN] the direction: unused for USB 1.1 */
uint_8 direction,
/* [IN] the endpoint type */
uint_8 endpoint_type,
/* [IN] After all data is transfered, should we terminate the transfer
** with a zero length packet if the last packet size == MAX_PACKET_SIZE?
*/
uint_8 flag
)
{ /* Body */
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
XD_STRUCT_PTR pxd;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
USB_lock();
if (direction) {
pxd = &usb_dev_ptr->XDSEND[endpoint_num];
} else {
pxd = &usb_dev_ptr->XDRECV[endpoint_num];
} /* Endif */
pxd->MAXPACKET = max_packet_size;
pxd->TYPE = endpoint_type;
pxd->STATUS = USB_STATUS_IDLE;
pxd->DONT_ZERO_TERMINATE = flag;
#ifdef __USB_OS_MQX__
((USB_CALLBACK_FUNCTIONS_STRUCT_PTR)usb_dev_ptr->CALLBACK_STRUCT_PTR)->\
DEV_INIT_ENDPOINT(handle, endpoint_num, pxd);
#else
_usb_dci_vusb11_init_endpoint(handle, endpoint_num, pxd);
#endif
USB_unlock();
return USB_OK;
} /* EndBody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_get_transfer_status
* Returned Value : Status of the requested endpoint and direction
* Comments :
* Returns the status of the specified transfer. USB_OK indicates
* there is no activity on the specified endpoint and direction.
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_get_transfer_status
(
/* [IN] Handle to the USB device */
_usb_device_handle handle,
/* [IN] Endpoint to get the status of */
uint_8 endpoint_number,
/* [IN] Direction of transfer. Is it transmit? */
uint_8 direction
)
{ /* Body */
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
uint_32 status;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
USB_lock();
if (direction == USB_SEND) {
status = usb_dev_ptr->XDSEND[endpoint_number].STATUS;
} else {
status = usb_dev_ptr->XDRECV[endpoint_number].STATUS;
} /* Endif */
USB_unlock();
return status;
} /* EndBody */
/*FUNCTION*-------------------------------------------------------------
*
* Function Name : _usb_device_read_setup_data
* Returned Value : USB_OK or error code
* Comments :
* Reads the setup data
*
*END*-----------------------------------------------------------------*/
void _usb_device_read_setup_data
(
/* [IN] the USB_USB_dev_initialize state structure */
_usb_device_handle handle,
/* [IN] the Endpoint number */
uint_8 endpoint_num,
/* [IN] buffer for receiving Setup packet */
uchar_ptr buff_ptr
)
{ /* Body */
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
XD_STRUCT_PTR pxd;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
pxd = &usb_dev_ptr->XDRECV[endpoint_num];
if (buff_ptr) {
/* Copy the setup packet from the BD to the user's buffer */
USB_memcopy((uchar_ptr)pxd->STARTADDRESS +
(pxd->SOFAR ? (pxd->SOFAR - 8) : 0), buff_ptr, 8);
} /* Endif */
} /* EndBody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_recv_data
* Returned Value : USB_OK or error code
* Comments :
* The Receive Data routine is non-blocking routine that causes a buffer of
* data up to 64k bytes long to be made available for data recieved from the
* USB host. The inputs to send data are bEP a byte endpoint number in the
* range 0-15. Recv_Data returns a status byte indicating whether
* the transfer will be initiated.
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_recv_data
(
/* [IN] handle to USB device */
_usb_device_handle handle,
/* [IN] Endpoint to receive data on */
uint_8 endpoint_number,
/* [IN] Pointer to receive buffer */
uchar_ptr buff_ptr,
/* [IN] Maximum length of transmission */
uint_32 size
)
{ /* Body */
XD_STRUCT_PTR pxd;
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
USB_lock();
pxd = &usb_dev_ptr->XDRECV[endpoint_number];
/*
** Check if the endpoint is disabled
*/
if (pxd->STATUS == USB_STATUS_DISABLED) {
USB_unlock();
return USBERR_ENDPOINT_DISABLED;
} /* Endif */
if ((pxd->TYPE == USB_CONTROL_ENDPOINT) && (pxd->SETUP_BUFFER_QUEUED)) {
pxd->SETUP_BUFFER_QUEUED = FALSE;
usb_dev_ptr->XDSEND[endpoint_number].SETUP_BUFFER_QUEUED = FALSE;
if (size) {
_usb_device_cancel_transfer(handle, endpoint_number, USB_RECV);
} else {
return USB_OK;
} /* Endif */
} /* Endif */
/*
** Check if any transfer is in progress on this endpoint
*/
if ((pxd->STATUS == USB_STATUS_TRANSFER_IN_PROGRESS) ||
(pxd->STATUS == USB_STATUS_TRANSFER_PENDING))
{
USB_unlock();
return USBERR_TRANSFER_IN_PROGRESS;
} /* Endif */
if (pxd->TYPE == USB_ISOCHRONOUS_ENDPOINT) {
/* if Isochronous */
if (size > pxd->MAXPACKET) {
/*
** if asked for more than maxPacket
** limit request to maxPacket
*/
size = pxd->MAXPACKET;
} /* Endif */
} /* Endif */
/*
** Queue the transfer if the endpoint is enabled and is not busy.
** Note that in the case of a stalled endpoint the transfer will
** be queued even though it can not complete without host
** intervention.
*/
pxd->STARTADDRESS = buff_ptr;
pxd->NEXTADDRESS = buff_ptr;
pxd->TODO = size;
pxd->SOFAR = 0;
pxd->UNACKNOWLEDGEDBYTES = size;
pxd->DIRECTION = USB_RECV;
/* return successful transfer initiation status. */
pxd->STATUS = USB_STATUS_TRANSFER_PENDING;
#ifdef __USB_OS_MQX__
((USB_CALLBACK_FUNCTIONS_STRUCT_PTR)usb_dev_ptr->CALLBACK_STRUCT_PTR)->\
DEV_RECV(handle, USB_RECV, endpoint_number);
#else
_usb_dci_vusb11_submit_transfer(handle, USB_RECV, endpoint_number);
#endif
/*
** If the endpoint is currently stalled notify the user after the
** transfer has been queued. By queuing the transfer the
** operation to revert the stall condition may be separated from
** the the data transfer code.
*/
if (pxd->STATUS == USB_STATUS_STALLED) {
USB_unlock();
return USBERR_ENDPOINT_STALLED;
} /* Endif */
USB_unlock();
return USB_OK;
} /* EndBody */
/*FUNCTION*----------------------------------------------------------------
*
* Function Name : _usb_device_call_service
* Returned Value : USB_OK or error code
* Comments :
* Calls the appropriate service for the specified type, if one is
* registered.
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_call_service
(
/* [IN] Handle to the USB device */
_usb_device_handle handle,
/* [IN] Type of service or endpoint */
uint_8 type,
/* [IN] Is it a Setup transfer? */
boolean setup,
/* [IN] Direction of transmission; is it a Transmit? */
boolean direction,
/* [IN] Pointer to the data */
uint_8_ptr buffer_ptr,
/* [IN] Number of bytes in transmission */
uint_32 length
)
{ /* Body */
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
SERVICE_STRUCT_PTR service_ptr;
SERVICE_STRUCT_PTR _PTR_ search_ptr;
usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
/* Needs mutual exclusion */
USB_lock();
/* Search for an existing entry for type */
for (search_ptr = (SERVICE_STRUCT_PTR _PTR_)&usb_dev_ptr->SERVICE_HEAD_PTR;
*search_ptr;
search_ptr = &(*search_ptr)->NEXT)
{
if ((*search_ptr)->TYPE == type) {
service_ptr = *search_ptr;
service_ptr->SERVICE(handle, setup, direction, buffer_ptr, length);
USB_unlock();
return USB_OK;
} /* Endif */
} /* Endfor */
USB_unlock();
return USBERR_CLOSED_SERVICE;
} /* EndBody */
/* EOF */