dev_mand.c
14.2 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
/*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.11 $
*** $Date: 2004/06/30 00:05:01 $
***
*** Comments:
*** This file contains the USB device API.
***
**************************************************************************
*END*********************************************************************/
#include "os_bb.h"
#include "osint.h"
#include "../host.h"
USB_DEV_STATE_STRUCT _usb_device_state[OS_USB_MAX_DEVICES];
/*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] Number of USB controller */
uint_8 controller_number,
/* [IN] Device number to initialize */
uint_8 device_number,
/* [OUT] the usb device handle */
_usb_device_handle _PTR_ handlep,
/* [IN] number of endpoints to initialize */
uint_8 number_of_endpoints
)
{ /* Body */
uint_8 returncode;
USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
PRINTF("usb_device_init%d at ticks = %d\n", controller_number, osGetCount());
if (number_of_endpoints < 1 || number_of_endpoints > OS_USB_MAX_ENDPTS) {
return USBERR_INVALID_NUM_OF_ENDPOINTS;
}
if (device_number >= OS_USB_MAX_DEVICES) {
return USBERR_INVALID_DEVICE_NUM;
}
usb_dev_ptr = &_usb_device_state[controller_number]; /* XXX? */
/*
* XXX check that the device is not already in use?
*/
bzero(usb_dev_ptr, sizeof(USB_DEV_STATE_STRUCT));
usb_dev_ptr->MAX_ENDPOINTS = number_of_endpoints;
usb_dev_ptr->CTLR_NUM = controller_number;
usb_dev_ptr->DEV_NUM = device_number;
usb_dev_ptr->XDSEND = (XD_STRUCT_PTR)osMalloc(__usb_endpt_desc_reg);
if (usb_dev_ptr->XDSEND == NULL) {
PRINTF("usb_device_init%d malloc fails\n", controller_number);
return USBERR_ALLOC_TRANSFER_DESCRIPTORS;
}
usb_dev_ptr->XDRECV = (XD_STRUCT_PTR)osMalloc(__usb_endpt_desc_reg);
if (usb_dev_ptr->XDRECV == NULL) {
/*
* XXX free the send descriptors?
*/
PRINTF("usb_device_init%d malloc fails\n", controller_number);
return USBERR_ALLOC_TRANSFER_DESCRIPTORS;
}
usb_dev_ptr->USB_STATE = USB_STATE_POWERED;
*handlep = (_usb_device_handle)usb_dev_ptr;
returncode = _usb_dci_vusb11_init(*handlep);
if (returncode != USB_OK) {
/* There was an error during initialization. Free all the
** allocated memory
*/
PRINTF("usb_device_init%d vusb11_init fails\n", controller_number);
osFree(__usb_endpt_desc_reg, (void *)usb_dev_ptr->XDSEND);
osFree(__usb_endpt_desc_reg, (void *)usb_dev_ptr->XDRECV);
return returncode;
}
return USB_OK;
}
/*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)osMalloc(__usb_svc_callback_reg);
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
)
{
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];
}
pxd->MAXPACKET = max_packet_size;
pxd->TYPE = endpoint_type;
pxd->STATUS = USB_STATUS_IDLE;
pxd->DONT_ZERO_TERMINATE = flag;
pxd->NEXTDATA01 = 0;
_usb_dci_vusb11_init_endpoint(handle, endpoint_num, pxd);
USB_unlock();
return USB_OK;
}
/*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
)
{
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;
}
USB_unlock();
return status;
}
/*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
)
{
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];
//PRINTF("read_setup_data: pxd 0x%x, START 0x%x, SOFAR %d\n", pxd, pxd->STARTADDRESS, pxd->SOFAR);
if (buff_ptr) {
uchar_ptr from = (uchar_ptr)pxd->STARTADDRESS +
(pxd->SOFAR ? (pxd->SOFAR - 8) : 0);
/* Copy the setup packet from the BD to the user's buffer */
bcopy((void *)from, (void *)buff_ptr, 8);
}
}
/*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
)
{
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];
PRINTF("dev_recv_data: ep %d, buf 0x%x, size %d\n", endpoint_number, buff_ptr, size);
/*
** Check if the endpoint is disabled
*/
if (pxd->STATUS == USB_STATUS_DISABLED) {
USB_unlock();
PRINTF("dev_recv_data: ep %d disabled!\n", endpoint_number);
return USBERR_ENDPOINT_DISABLED;
}
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 {
USB_unlock();
//PRINTF("dev_recv_data: ep %d already has buf q'd, new size 0 so do nothing\n", endpoint_number);
return USB_OK;
}
}
/*
** 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();
//PRINTF("dev_recv_data: ep %d xfer pending, so do nothing\n", endpoint_number);
return USBERR_TRANSFER_IN_PROGRESS;
}
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;
}
}
/*
** 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;
#if BBPLAYER
/*
* Make sure the cache is clean with respect to the io
* buffer, since the hardware is not io coherent.
*/
osInvalDCache(buff_ptr, size);
#endif
/* return successful transfer initiation status. */
pxd->STATUS = USB_STATUS_TRANSFER_PENDING;
_usb_dci_vusb11_submit_transfer(handle, USB_RECV, endpoint_number);
/*
** 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;
}
USB_unlock();
return USB_OK;
}
/*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 */