usbwrite.c 2.05 KB
#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);
}