⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 一个USB主机核的驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		return ALLOC_DEVICE_ADDRESS_ERR;
	device->DevAddress = address;

	/* send set address request */
	err = UsbControlMsg(device,
						device->PipeCtrlOut,
						USB_REQ_SET_ADDRESS,
						(USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
						address,
						0,
						0,
						NULL
						);
	
	if (err == NO_ERROR)
	{
		/* update control pipe */
		device->PipeCtrlIn |= (address << 8);		
		device->PipeCtrlOut |= (address << 8);
	}

	return err;
}

/*
 *	Protocol get usb device standard configuration descriptor
 *	USB get configuration descriptor
 */
extern BYTE				/* Return: usb operation error code */
UsbGetDescriptor(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	BYTE			type,				/*        descriptor type */
	BYTE			index,				/*        descriptor index */
	VOID			*buf,				/*        descriptor data buffer */
	WORD			size				/*        descriptor size */
	)
{
	WORD		value;

	/* set wValue of request */
	value = type;
	value <<= 8;
	value += index;

	/* set get descriptor request */
	return UsbControlMsg(device,
						device->PipeCtrlIn,
						USB_REQ_GET_DESCRIPTOR,
						(USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
						value,
						0,
						size,
						buf
						);
}

/*
 *	Protocol get usb device string descriptor
 */
extern BYTE				/* Return: usb operation error code */
UsbGetString(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	WORD			langid,				/*        language ID */
	BYTE			index,				/*        string descriptor index */
	VOID			*buf,				/*        descriptor receive buffer */
	WORD			size				/*        descriptor length */
	)
{
	WORD		value;

	/* set wValue of request */
	value = USB_DT_STRING;
	value <<= 8;
	value += index;

	/* set get string descriptor request */
	return UsbControlMsg(device,
						device->PipeCtrlIn,
						USB_REQ_GET_DESCRIPTOR,
						(USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
						value,
						langid,
						size,
						buf
						);
}

/*
 *	Protocol get usb device standard device descriptor
 */
extern BYTE				/* Return: usb operation error code */
UsbGetDeviceDescriptor(
	USB_DEVICE		*device				/* Input: usb device structure pointer */
	)
{
	WORD		value;
	WORD		size;
	
	/* set request value */
	value = USB_DT_DEVICE;
	value <<= 8;

	/* set request device descirptor size */
	size = (device->DevAddress != 0) ? device->DeviceDescriptor.bLength : 8;

	/* send get device descriptor request */
	return UsbControlMsg(device,
						device->PipeCtrlIn,
						USB_REQ_GET_DESCRIPTOR,
						(USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
						value,
						0,
						size,
						&device->DeviceDescriptor
						);
}

/*
 *	Protocol set usb device configuration
 */
extern BYTE				/* Return: usb operation error code */
UsbSetConfiguration(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	BYTE			configuration		/*        active configuration number for this device */
	)
{
	/* send set configuration request */
	return UsbControlMsg(device,
						device->PipeCtrlOut,
						USB_REQ_SET_CONFIGURATION,
						(USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
						configuration,
						0,
						0,
						NULL
						);
}

/*
 *	Protocol set usb device interface
 */
extern BYTE				/* Return: usb operation error code */
UstSetInterface(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	BYTE			interface,
	BYTE			alternate	
	)
{
	/* send set interface request */
	return UsbControlMsg(device,
						device->PipeCtrlOut,
						USB_REQ_SET_INTERFACE,
						(USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_INTERFACE),
						alternate,
						interface,
						0,
						NULL
						);
}

/*
 *	Start a usb device
 */
extern BYTE				/* Return: usb operation error code */
UsbStartDevice(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	USB_BUS			*usb				/*        usb bus structure pointer */
	)
{
	BYTE			buffer[64];
	BYTE			*p;
	USB_CONFIG		*config;
	USB_INTERFACE	*interface;
	USB_ENDPOINT	*endpoint;
	BYTE			configIndex, interfaceIndex, endpointIndex;

	/* mount device to usb bus */
	device->Usb = usb;

	/* get device descriptor first time */
	if (UsbGetDeviceDescriptor(device) != NO_ERROR)
		return GET_DEVICE_DESCRIPTOR_ERR;

	ResetUsb(usb);

	/* usb allocate device address */
	if (UsbSetAddress(device) != NO_ERROR)
		return SET_ADDRESS_ERR;

	/* get device descriptor the second time */
	if (UsbGetDeviceDescriptor(device) != NO_ERROR)
		return GET_DEVICE_DESCRIPTOR_ERR;

	/* get all config descriptor */
	for (configIndex = 0; configIndex < device->DeviceDescriptor.bNumConfigurations; ++configIndex)
	{
		/* allocate configuration source */
		config = AllocUsbConfig();
		if (config == NULL)
			return ALLOC_CONFIG_ERR;
		device->Config[configIndex] = config;

		/* read configuration descriptor first time */
		if (UsbGetDescriptor(device, 
							USB_DT_CONFIG, 
							configIndex, 
							&config->ConfigDescriptor, 
							USB_DT_CONFIG_SIZE) != NO_ERROR)
			return GET_CONFIG_DESCRIPTOR_ERR;
		
		/* read all configuration descriptor */
		if (UsbGetDescriptor(device, 
							USB_DT_CONFIG, 
							configIndex, 
							buffer, 
							config->ConfigDescriptor.wTotalLength) != NO_ERROR)
			return GET_CONFIG_DESCRIPTOR_ERR;

		p = buffer;
		p += USB_DT_CONFIG_SIZE;
		/* load interface and endpoint descriptor */
		for (interfaceIndex = 0; interfaceIndex < config->ConfigDescriptor.bNumInterfaces; ++interfaceIndex)
		{
			/* load interface descriptor */
			interface = AllocUsbInterface();
			if (interface == NULL)
				return ALLOC_INTETFACE_ERR;
			config->Interface[interfaceIndex] = interface;
			memcpy(&interface->InterfaceDescriptor, p, USB_DT_INTERFACE_SIZE);
			p += USB_DT_INTERFACE_SIZE;

			for (endpointIndex = 0; endpointIndex < interface->InterfaceDescriptor.bNumEndpoints; ++endpointIndex)
			{
				/* loac endpoint descriptor */
				endpoint = AllocUsbEndpoint();
				if (endpoint == NULL)
					return ALLOC_ENDPOINT_ERR;
				interface->Endpoint[endpointIndex] = endpoint;
				memcpy(&endpoint->EndpointDescriptor, p, USB_DT_ENDPOINT_SIZE);
				p += USB_DT_ENDPOINT_SIZE;
			}/* end for (endpointIndex = 0; endpointIndex < intDesp->bNumEndpoints; ++endpointIndex) */
		}/* end for (interfaceIndex = 0; interfaceIndex < cfgDesp->bNumInterfaces; ++interfaceIndex) */
	}/* end for (configIndex = 0; configIndex < device->DeviceDescriptor.bNumConfigurations; ++configIndex) */

	return NO_ERROR;
}

/*
 *	Set device configuration
 */
extern BYTE				/* Return: usb operation error code */
UsbSetDeviceConfig(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	USB_CONFIG		*config,			/*        usb configuration structure pointer */
	USB_INTERFACE	*interface			/*        usb interface structure pointer */
	)
{
	/* set config */
	if (UsbSetConfiguration(device, config->ConfigDescriptor.bConfigurationValue) != NO_ERROR)
		return SET_CONFIG_ERR;

	config->ActInterface = interface;
	device->ActConfig = config;

	return NO_ERROR;
}
/*---------------------------------------------------------------------------*/


/*--------------------------USB root hub interface---------------------------*/
/*
 *	Get usb root hub port status
 */
extern DWORD			/* Return: root hub port status map */
UstGetRootHubPortStatus(
	USB_BUS		*usb,				/* Input: usb bus structure pointer */
	BYTE		portNum				/*        root hub port number */
	)
{
	return HciGetRootHubPortStatus(usb->HcPriv, portNum);
}

/*
 *	Usb enable root hub port
 */
extern BOOL				/* Return: success return 1, else return 0 */
UsbEnableRootHubPort(
	USB_BUS		*usb,				/* Input: usb bus structure pointer */
	BYTE		portNum				/*        root hub port number */
	)
{
	return HciEnableRootHubPort(usb->HcPriv, portNum);
}
/*---------------------------------------------------------------------------*/


/*------------------------------USB init-------------------------------*/
/*
 *	Open usb and init it
 */
extern BYTE				/* Return: usb operation error code */
OpenUsb(
	VOID		*usbpriv			/* Input: VOID* of usb structure */
	)
{
	USB_BUS		*usb = (USB_BUS *)usbpriv;
	VOID		*hci;

	/* allocate and init usb hardware source */
	hci = AllocHci();
	if (hci == NULL)
		return ALLOC_HCI_ERR;
	usb->HcPriv = hci;
	OpenHci(hci);

	/* init usb device map information */
	usb->UsbDevAddressMap = 0;

	return NO_ERROR;
}
/*----------------------------------------------------------------------*/


/*----------------------------USB bus control---------------------------*/
/*
 *	Reset usb bus
 */
extern VOID
ResetUsb(
	USB_BUS		*usb					/* Input: usb bus structure pointer */
	)
{
	HciResetUsb(usb->HcPriv);
}

/*
 *	Suspend usb bus
 */
extern VOID
SuspendUsb(
	USB_BUS		*usb					/* Input: usb bus structure pointer */
	)
{
	HciSuspendUsb(usb->HcPriv);
}

/*
 *	Resume usb bus
 */
extern VOID
ResumeUsb(
	USB_BUS		*usb					/* Input: usb bus structure pointer */
	)
{
	HciResumeUsb(usb->HcPriv);
}
/*-----------------------------------------------------------------------*/


/*----------------------------USB bulk pipe---------------------------*/
/*
 *	USB bulk transfer massage operation
 *	This is the interface for device bulk transfer
 */
extern BYTE				/* Return: usb operation error code */
UsbBulkMsg(
	USB_DEVICE		*device,			/* Input: usb device structure pointer */
	DWORD			pipe,				/*        usb bulk pipe define */
	VOID			*dat,				/*        bulk transfer data buffer pointer */
	WORD			length,				/*        bulk transfer data length */
	BOOL			isUseDMA,			/*        use DMA set 1, else set 0 */
	WORD			*actualLength		/* Output: actual transfer data length */
	)
{
	URB			urb;
	BYTE		err;

	urb.Pipe = pipe;
	urb.MaxPacketSize = PipeMaxPacketSize(pipe);
	urb.Interval = 0;
	urb.SetupPacket = NULL;
	urb.DataBuffer = dat;
	urb.DataLength = length;
	urb.IsUseDMA = isUseDMA;
	urb.Err = NO_ERROR;
	urb.HcPriv = device->Usb->HcPriv;

	/* send control transfer urb */
	err = HciSendUrb(&urb);
	*actualLength = urb.DataLength;

	return err;
}
/*-----------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -