usbhandle.c 1.98 KB
#include "os_bb.h"
#include "osint.h"
#include "host.h"

_usb_ext_handle	_usb_ext_handles[OS_USB_MAX_HANDLES];

/*
 * osBbUsbDevGetHandle
 *
 * Description:
 *	Open or attach to a particular USB device.
 *
 * Returns:
 * 	0 for success
 * 	< 0 for failure
 */
s32
osBbUsbDevGetHandle(s32 which, OSBbUsbInfo *ip, OSBbUsbHandle *hp)
{
#ifdef USB_HOST
	HOST_GLOBAL_STRUCT *hgp;
	int i;

	if (which < 0 || which >= OS_USB_MAX_CONTROLLERS) {
		PRINTF("osBbUsbDevGetHandle called with bad arg %d\n", which);
		return(-1);
	}

	for (i = 0; i < OS_USB_MAX_HANDLES; i++) {
		if (_usb_ext_handles[i].uh_host_handle == NULL) {
			bzero((void *) &_usb_ext_handles[i],
				sizeof (_usb_ext_handle));
			_usb_ext_handles[i].uh_which = which;
			_usb_ext_handles[i].uh_mq = 
				&__osBbUsbCtlrQ[which];
			_usb_ext_handles[i].uh_host_handle =
				__osArcHostHandle[which];
			*hp = (OSBbUsbHandle) &_usb_ext_handles[i];
			hgp = &host_global_struct[which];
			if (hgp->driver != NULL) {
				hgp->driver->funcs->open
					(_usb_ext_handles[i].uh_host_handle);
				_usb_ext_handles[i].uh_blksize =
				       	hgp->driver->desc->BMAXPACKETSIZE;
			} else {
				_usb_ext_handles[i].uh_blksize = 64; /* XXX */
			}
			return(0);
		}
	}

	PRINTF("osBbUsbDevGetHandle: out of handles\n");
	return(EUSBAGAIN);
#else	/* !USB_HOST */
	return(-1);
#endif	/* !USB_HOST */
}

s32
osBbUsbDevFreeHandle(OSBbUsbHandle h)
{
	bzero((void *)h, sizeof (_usb_ext_handle));
	return(0);
}

/*
 * Called from the host detach callback routine when a device is
 * disconnected.  Subsequent calls using any handles for the
 * host will get ENXIO.
 */
void
__usbInvalidateHandles(s32 which)
{
	int i;

	if (which < 0 || which >= OS_USB_MAX_CONTROLLERS) {
		PRINTF("osBbUsbDevGetHandle called with bad arg %d\n", which);
		return;
	}

	for (i = 0; i < OS_USB_MAX_HANDLES; i++) {
		/*
		 * Find handles for the given host and clear them
		 */
		if (_usb_ext_handles[i].uh_host_handle == __osArcHostHandle[which]) {
			_usb_ext_handles[i].uh_host_handle = NULL;
		}
	}

}