usbread.c
1.96 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
#include "os_bb.h"
#include "osint.h"
#include "host.h"
/*
* osBbUsbDevRead
*
* Description:
* Read data from a USB device
*
* Returns:
* Number of characters read
* < 0 for failure
*/
s32
osBbUsbDevRead(OSBbUsbHandle h, u8 *buf, s32 len, u64 off)
{
__OSBbUsbMesg um;
OSMesg msg = (OSMesg)&um;
OSMesg rmsg;
OSMesgQueue rq;
_usb_ext_handle *uhp = (_usb_ext_handle *) h;
s32 rsize, rtotal;
//PRINTF("----osBbUsbDevRead: h 0x%x, buf 0x%x, len %d\n", h, buf, len);
/*
* Check for detached devices
*/
if (uhp->uh_host_handle == NULL)
return(-EUSBNXIO);
/*
* Formulate query message
*/
bzero(&um, sizeof um);
um.u.umrw.umrw_handle = h;
um.um_rq = &rq;
/*
* Initialize the private queue to receive the reply
*/
osCreateMesgQueue(&rq, &rmsg, 1);
/*
* Invalidate and writeback the DCache lines for the
* read buffer, since the BBPLAYER hw is not io coherent.
*/
osInvalDCache(buf, len);
/*
* Main read loop:
* Request the data in chunks from the manager thread for
* processing. Return only when all data is read
* or an error occurs.
*/
rtotal = 0;
while (len > 0) {
// rsize = MIN(len, uhp->uh_blksize);
rsize = len;
um.u.umrw.umrw_buffer = buf;
um.u.umrw.umrw_len = rsize;
um.u.umrw.umrw_offset = off;
um.um_type = OS_USB_MESG_READ;
/*
* Send message to the manager thread and wait for the response
*/
(void) osSendMesg(uhp->uh_mq, msg, OS_MESG_BLOCK);
(void) osRecvMesg(&rq, &msg, OS_MESG_BLOCK);
/*
* Validate the response message
*/
if (msg != &um) {
PRINTF("--osBbUsbDevRead: reply msg not the same (0x%x)\n", msg);
}
if (um.um_type != OS_USB_REPLY_READ) {
PRINTF("--osBbUsbDevRead: reply msg type wrong (0x%x)\n", um.um_type);
}
if (um.um_ret < 0) {
PRINTF("--osBbUsbDevRead: reply ret err (%d)\n", um.um_ret);
return(um.um_ret);
}
rtotal += rsize;
len -= rsize;
off += rsize;
buf += rsize;
}
//PRINTF("--osBbUsbDevRead: returns %d\n", rtotal);
return(rtotal);
}