usbwrite.c
2.05 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
#include "os_bb.h"
#include "osint.h"
#include "host.h"
/*
* osBbUsbDevWrite
*
* Description:
* Write data to a USB device
*
* Returns:
* Number of characters written
* < 0 for failure
*/
s32
osBbUsbDevWrite(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 wsize, wtotal;
//PRINTF("----osBbUsbDevWrite: 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
* buffer, since the BBPLAYER hw is not io coherent.
* XXX this is probably unnecessary since this cache
* XXX is write-through.
*/
osWritebackDCache(buf, len);
/*
* Main write loop:
* Hand the data in chunks to the manager thread for
* processing. Return only when all data is written
* or an error occurs.
*/
wtotal = 0;
while (len > 0) {
//wsize = MIN(len, uhp->uh_blksize);
wsize = len;
um.u.umrw.umrw_buffer = buf;
um.u.umrw.umrw_len = wsize;
um.u.umrw.umrw_offset = off;
um.um_type = OS_USB_MESG_WRITE;
/*
* 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("--osBbUsbDevWrite: reply msg not the same (0x%x)\n", msg);
}
if (um.um_type != OS_USB_REPLY_WRITE) {
PRINTF("--osBbUsbDevWrite: reply msg type wrong (0x%x)\n", um.um_type);
}
if (um.um_ret < 0) {
PRINTF("--osBbUsbDevWrite: reply ret err (%d)\n", um.um_ret);
return(um.um_ret);
}
wtotal += wsize;
len -= wsize;
off += wsize;
buf += wsize;
}
//PRINTF("--osBbUsbDevWrite: returns %d\n", wtotal);
return(wtotal);
}