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

📄 usbdcore_ep0.c

📁 U-boot源码 ARM7启动代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * (C) Copyright 2003 * Gerry Hamel, geh@ti.com, Texas Instruments * * (C) Copyright 2006 * Bryan O'Donoghue, deckard@CodeHermit.ie * * Based on * linux/drivers/usbd/ep0.c * * Copyright (c) 2000, 2001, 2002 Lineo * Copyright (c) 2001 Hewlett Packard * * By: *	Stuart Lynne <sl@lineo.com>, *	Tom Rushworth <tbr@lineo.com>, *	Bruce Balden <balden@lineo.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *//* * This is the builtin ep0 control function. It implements all required functionality * for responding to control requests (SETUP packets). * * XXX * * Currently we do not pass any SETUP packets (or other) to the configured * function driver. This may need to change. * * XXX * * As alluded to above, a simple callback cdc_recv_setup has been implemented * in the usb_device data structure to facilicate passing * Common Device Class packets to a function driver. * * XXX */#include <common.h>#if defined(CONFIG_USB_DEVICE)#include "usbdcore.h"#if 0#define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args)#else#define dbg_ep0(lvl,fmt,args...)#endif/* EP0 Configuration Set ********************************************************************* *//** * ep0_get_status - fill in URB data with appropriate status * @device: * @urb: * @index: * @requesttype: * */static int ep0_get_status (struct usb_device_instance *device,			   struct urb *urb, int index, int requesttype){	char *cp;	urb->actual_length = 2;	cp = (char*)urb->buffer;	cp[0] = cp[1] = 0;	switch (requesttype) {	case USB_REQ_RECIPIENT_DEVICE:		cp[0] = USB_STATUS_SELFPOWERED;		break;	case USB_REQ_RECIPIENT_INTERFACE:		break;	case USB_REQ_RECIPIENT_ENDPOINT:		cp[0] = usbd_endpoint_halted (device, index);		break;	case USB_REQ_RECIPIENT_OTHER:		urb->actual_length = 0;	default:		break;	}	dbg_ep0 (2, "%02x %02x", cp[0], cp[1]);	return 0;}/** * ep0_get_one * @device: * @urb: * @result: * * Set a single byte value in the urb send buffer. Return non-zero to signal * a request error. */static int ep0_get_one (struct usb_device_instance *device, struct urb *urb,			__u8 result){	urb->actual_length = 1;	/* XXX 2? */	((char *) urb->buffer)[0] = result;	return 0;}/** * copy_config * @urb: pointer to urb * @data: pointer to configuration data * @length: length of data * * Copy configuration data to urb transfer buffer if there is room for it. */void copy_config (struct urb *urb, void *data, int max_length,			 int max_buf){	int available;	int length;	/*dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p", */	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, data); */	if (!data) {		dbg_ep0 (1, "data is NULL");		return;	}	length = max_length;	if (length > max_length) {		dbg_ep0 (1, "length: %d >= max_length: %d", length,			 max_length);		return;	}	/*dbg_ep0(1, "   actual: %d buf: %d max_buf: %d max_length: %d length: %d", */	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, length); */	if ((available =	     /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) {		return;	}	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */	if (length > available) {		length = available;	}	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */	memcpy (urb->buffer + urb->actual_length, data, length);	urb->actual_length += length;	dbg_ep0 (3,		 "copy_config: <- actual: %d buf: %d max_buf: %d max_length: %d available: %d",		 urb->actual_length, urb->buffer_length, max_buf, max_length,		 available);}/** * ep0_get_descriptor * @device: * @urb: * @max: * @descriptor_type: * @index: * * Called by ep0_rx_process for a get descriptor device command. Determine what * descriptor is being requested, copy to send buffer. Return zero if ok to send, * return non-zero to signal a request error. */static int ep0_get_descriptor (struct usb_device_instance *device,			       struct urb *urb, int max, int descriptor_type,			       int index){	int port = 0;		/* XXX compound device */	char *cp;	/*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */	if (!urb || !urb->buffer || !urb->buffer_length	    || (urb->buffer_length < 255)) {		dbg_ep0 (2, "invalid urb %p", urb);		return -1L;	}	/* setup tx urb */	urb->actual_length = 0;	cp = (char*)urb->buffer;	dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));	switch (descriptor_type) {	case USB_DESCRIPTOR_TYPE_DEVICE:		{			struct usb_device_descriptor *device_descriptor;			if (!			    (device_descriptor =			     usbd_device_device_descriptor (device, port))) {				return -1;			}			/* copy descriptor for this device */			copy_config (urb, device_descriptor,				     sizeof (struct usb_device_descriptor),				     max);			/* correct the correct control endpoint 0 max packet size into the descriptor */			device_descriptor =				(struct usb_device_descriptor *) urb->buffer;		}		dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length);		break;	case USB_DESCRIPTOR_TYPE_CONFIGURATION:		{			struct usb_configuration_descriptor				*configuration_descriptor;			struct usb_device_descriptor *device_descriptor;			if (!			    (device_descriptor =			     usbd_device_device_descriptor (device, port))) {				return -1;			}			/*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */			if (index > device_descriptor->bNumConfigurations) {				dbg_ep0 (0, "index too large: %d > %d", index,					 device_descriptor->					 bNumConfigurations);				return -1;			}			if (!			    (configuration_descriptor =			     usbd_device_configuration_descriptor (device,								   port,								   index))) {				dbg_ep0 (0,					 "usbd_device_configuration_descriptor failed: %d",					 index);				return -1;			}			dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength));			copy_config (urb, configuration_descriptor,					cpu_to_le16(configuration_descriptor->wTotalLength),				     max);		}		break;	case USB_DESCRIPTOR_TYPE_STRING:		{			struct usb_string_descriptor *string_descriptor;			if (!(string_descriptor = usbd_get_string (index))) {				serial_printf("Invalid string index %d\n", index);				return -1;			}			dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength);			copy_config (urb, string_descriptor, string_descriptor->bLength, max);		}		break;	case USB_DESCRIPTOR_TYPE_INTERFACE:	serial_printf("USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n");		return -1;	case USB_DESCRIPTOR_TYPE_ENDPOINT:		serial_printf("USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n");		return -1;	case USB_DESCRIPTOR_TYPE_HID:		{			serial_printf("USB_DESCRIPTOR_TYPE_HID - error not implemented\n");			return -1;	/* unsupported at this time */#if 0			int bNumInterface =				le16_to_cpu (urb->device_request.wIndex);			int bAlternateSetting = 0;			int class = 0;			struct usb_class_descriptor *class_descriptor;			if (!(class_descriptor =			      usbd_device_class_descriptor_index (device,								  port, 0,								  bNumInterface,								  bAlternateSetting,								  class))			    || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {				dbg_ep0 (3, "[%d] interface is not HID",					 bNumInterface);				return -1;			}			/* copy descriptor for this class */			copy_config (urb, class_descriptor,

⌨️ 快捷键说明

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