usbread.c 1.96 KB
#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);
}