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

📄 serial.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 4 页
字号:
 * gs_send_packet * * If there is data to send, a packet is built in the given * buffer and the size is returned.  If there is no data to * send, 0 is returned.  If there is any error a negative * error number is returned. * * Called during USB completion routine, on interrupt time. * * We assume that disconnect will not happen until all completion * routines have completed, so we can assume that the dev_port * array does not change during the lifetime of this function. */static int gs_send_packet(struct gs_dev *dev, char *packet, unsigned int size){	unsigned int len;	struct gs_port *port;	/* TEMPORARY -- only port 0 is supported right now */	port = dev->dev_port[0];	if (port == NULL) {		printk(KERN_ERR			"gs_send_packet: port=%d, NULL port pointer\n",			0);		return -EIO;	}	spin_lock(&port->port_lock);	len = gs_buf_data_avail(port->port_write_buf);	if (len < size)		size = len;	if (size == 0)		goto exit;	size = gs_buf_get(port->port_write_buf, packet, size);	if (port->port_tty)		wake_up_interruptible(&port->port_tty->write_wait);exit:	spin_unlock(&port->port_lock);	return size;}/* * gs_recv_packet * * Called for each USB packet received.  Reads the packet * header and stuffs the data in the appropriate tty buffer. * Returns 0 if successful, or a negative error number. * * Called during USB completion routine, on interrupt time. * * We assume that disconnect will not happen until all completion * routines have completed, so we can assume that the dev_port * array does not change during the lifetime of this function. */static int gs_recv_packet(struct gs_dev *dev, char *packet, unsigned int size){	unsigned int len;	struct gs_port *port;	int ret;	/* TEMPORARY -- only port 0 is supported right now */	port = dev->dev_port[0];	if (port == NULL) {		printk(KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",			port->port_num);		return -EIO;	}	spin_lock(&port->port_lock);	if (port->port_open_count == 0) {		printk(KERN_ERR "gs_recv_packet: port=%d, port is closed\n",			port->port_num);		ret = -EIO;		goto exit;	}	if (port->port_tty == NULL) {		printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",			port->port_num);		ret = -EIO;		goto exit;	}	if (port->port_tty->magic != TTY_MAGIC) {		printk(KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",			port->port_num);		ret = -EIO;		goto exit;	}	len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);	if (len < size)		size = len;	if (size > 0) {		memcpy(port->port_tty->flip.char_buf_ptr, packet, size);		port->port_tty->flip.char_buf_ptr += size;		port->port_tty->flip.count += size;		tty_flip_buffer_push(port->port_tty);		wake_up_interruptible(&port->port_tty->read_wait);	}	ret = 0;exit:	spin_unlock(&port->port_lock);	return ret;}/** gs_read_complete*/static void gs_read_complete(struct usb_ep *ep, struct usb_request *req){	int ret;	struct gs_dev *dev = ep->driver_data;	if (dev == NULL) {		printk(KERN_ERR "gs_read_complete: NULL device pointer\n");		return;	}	switch(req->status) {	case 0: 		/* normal completion */		gs_recv_packet(dev, req->buf, req->actual);requeue:		req->length = ep->maxpacket;		if ((ret=usb_ep_queue(ep, req, GFP_ATOMIC))) {			printk(KERN_ERR			"gs_read_complete: cannot queue read request, ret=%d\n",				ret);		}		break;	case -ESHUTDOWN:		/* disconnect */		gs_debug("gs_read_complete: shutdown\n");		gs_free_req(ep, req);		break;	default:		/* unexpected */		printk(KERN_ERR		"gs_read_complete: unexpected status error, status=%d\n",			req->status);		goto requeue;		break;	}}/** gs_write_complete*/static void gs_write_complete(struct usb_ep *ep, struct usb_request *req){	struct gs_dev *dev = ep->driver_data;	struct gs_req_entry *gs_req = req->context;	if (dev == NULL) {		printk(KERN_ERR "gs_write_complete: NULL device pointer\n");		return;	}	switch(req->status) {	case 0:		/* normal completion */requeue:		if (gs_req == NULL) {			printk(KERN_ERR				"gs_write_complete: NULL request pointer\n");			return;		}		spin_lock(&dev->dev_lock);		list_add(&gs_req->re_entry, &dev->dev_req_list);		spin_unlock(&dev->dev_lock);		gs_send(dev);		break;	case -ESHUTDOWN:		/* disconnect */		gs_debug("gs_write_complete: shutdown\n");		gs_free_req(ep, req);		break;	default:		printk(KERN_ERR		"gs_write_complete: unexpected status error, status=%d\n",			req->status);		goto requeue;		break;	}}/* Gadget Driver *//* * gs_bind * * Called on module load.  Allocates and initializes the device * structure and a control request. */static int gs_bind(struct usb_gadget *gadget){	int ret;	struct usb_ep *ep;	struct gs_dev *dev;	int gcnum;	/* Some controllers can't support CDC ACM:	 * - sh doesn't support multiple interfaces or configs;	 * - sa1100 doesn't have a third interrupt endpoint	 */	if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))		use_acm = 0;	gcnum = usb_gadget_controller_number(gadget);	if (gcnum >= 0)		gs_device_desc.bcdDevice =				cpu_to_le16(GS_VERSION_NUM | gcnum);	else {		printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",			gadget->name);		/* unrecognized, but safe unless bulk is REALLY quirky */		gs_device_desc.bcdDevice =			__constant_cpu_to_le16(GS_VERSION_NUM|0x0099);	}	usb_ep_autoconfig_reset(gadget);	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);	if (!ep)		goto autoconf_fail;	EP_IN_NAME = ep->name;	ep->driver_data = ep;	/* claim the endpoint */	ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);	if (!ep)		goto autoconf_fail;	EP_OUT_NAME = ep->name;	ep->driver_data = ep;	/* claim the endpoint */	if (use_acm) {		ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);		if (!ep) {			printk(KERN_ERR "gs_bind: cannot run ACM on %s\n", gadget->name);			goto autoconf_fail;		}		gs_device_desc.idProduct = __constant_cpu_to_le16(						GS_CDC_PRODUCT_ID),		EP_NOTIFY_NAME = ep->name;		ep->driver_data = ep;	/* claim the endpoint */	}	gs_device_desc.bDeviceClass = use_acm		? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;	gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;#ifdef CONFIG_USB_GADGET_DUALSPEED	gs_qualifier_desc.bDeviceClass = use_acm		? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;	/* assume ep0 uses the same packet size for both speeds */	gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;	/* assume endpoints are dual-speed */	gs_highspeed_notify_desc.bEndpointAddress =		gs_fullspeed_notify_desc.bEndpointAddress;	gs_highspeed_in_desc.bEndpointAddress =		gs_fullspeed_in_desc.bEndpointAddress;	gs_highspeed_out_desc.bEndpointAddress =		gs_fullspeed_out_desc.bEndpointAddress;#endif /* CONFIG_USB_GADGET_DUALSPEED */	usb_gadget_set_selfpowered(gadget);	if (gadget->is_otg) {		gs_otg_descriptor.bmAttributes |= USB_OTG_HNP,		gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;		gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;	}	gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);	if (dev == NULL)		return -ENOMEM;	snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",		system_utsname.sysname, system_utsname.release,		gadget->name);	memset(dev, 0, sizeof(struct gs_dev));	dev->dev_gadget = gadget;	spin_lock_init(&dev->dev_lock);	INIT_LIST_HEAD(&dev->dev_req_list);	set_gadget_data(gadget, dev);	if ((ret=gs_alloc_ports(dev, GFP_KERNEL)) != 0) {		printk(KERN_ERR "gs_bind: cannot allocate ports\n");		gs_unbind(gadget);		return ret;	}	/* preallocate control response and buffer */	dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,		GFP_KERNEL);	if (dev->dev_ctrl_req == NULL) {		gs_unbind(gadget);		return -ENOMEM;	}	dev->dev_ctrl_req->complete = gs_setup_complete;	gadget->ep0->driver_data = dev;	printk(KERN_INFO "gs_bind: %s %s bound\n",		GS_LONG_NAME, GS_VERSION_STR);	return 0;autoconf_fail:	printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);	return -ENODEV;}/* * gs_unbind * * Called on module unload.  Frees the control request and device * structure. */static void gs_unbind(struct usb_gadget *gadget){	struct gs_dev *dev = get_gadget_data(gadget);	gs_device = NULL;	/* read/write requests already freed, only control request remains */	if (dev != NULL) {		if (dev->dev_ctrl_req != NULL) {			gs_free_req(gadget->ep0, dev->dev_ctrl_req);			dev->dev_ctrl_req = NULL;		}		gs_free_ports(dev);		kfree(dev);		set_gadget_data(gadget, NULL);	}	printk(KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,		GS_VERSION_STR);}/* * gs_setup * * Implements all the control endpoint functionality that's not * handled in hardware or the hardware driver. * * Returns the size of the data sent to the host, or a negative * error number. */static int gs_setup(struct usb_gadget *gadget,	const struct usb_ctrlrequest *ctrl){	int ret = -EOPNOTSUPP;	struct gs_dev *dev = get_gadget_data(gadget);	struct usb_request *req = dev->dev_ctrl_req;	u16 wIndex = le16_to_cpu(ctrl->wIndex);	u16 wValue = le16_to_cpu(ctrl->wValue);	u16 wLength = le16_to_cpu(ctrl->wLength);	switch (ctrl->bRequestType & USB_TYPE_MASK) {	case USB_TYPE_STANDARD:		ret = gs_setup_standard(gadget,ctrl);		break;	case USB_TYPE_CLASS:		ret = gs_setup_class(gadget,ctrl);		break;	default:		printk(KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",			ctrl->bRequestType, ctrl->bRequest,			wValue, wIndex, wLength);		break;	}	/* respond with data transfer before status phase? */	if (ret >= 0) {		req->length = ret;		req->zero = ret < wLength				&& (ret % gadget->ep0->maxpacket) == 0;		ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);		if (ret < 0) {			printk(KERN_ERR "gs_setup: cannot queue response, ret=%d\n",				ret);			req->status = 0;			gs_setup_complete(gadget->ep0, req);		}	}	/* device either stalls (ret < 0) or reports success */	return ret;}static int gs_setup_standard(struct usb_gadget *gadget,	const struct usb_ctrlrequest *ctrl){	int ret = -EOPNOTSUPP;	struct gs_dev *dev = get_gadget_data(gadget);	struct usb_request *req = dev->dev_ctrl_req;	u16 wIndex = le16_to_cpu(ctrl->wIndex);	u16 wValue = le16_to_cpu(ctrl->wValue);	u16 wLength = le16_to_cpu(ctrl->wLength);	switch (ctrl->bRequest) {	case USB_REQ_GET_DESCRIPTOR:		if (ctrl->bRequestType != USB_DIR_IN)			break;		switch (wValue >> 8) {		case USB_DT_DEVICE:			ret = min(wLength,				(u16)sizeof(struct usb_device_descriptor));			memcpy(req->buf, &gs_device_desc, ret);			break;#ifdef CONFIG_USB_GADGET_DUALSPEED		case USB_DT_DEVICE_QUALIFIER:			if (!gadget->is_dualspeed)				break;			ret = min(wLength,				(u16)sizeof(struct usb_qualifier_descriptor));			memcpy(req->buf, &gs_qualifier_desc, ret);			break;		case USB_DT_OTHER_SPEED_CONFIG:			if (!gadget->is_dualspeed)				break;			/* fall through */#endif /* CONFIG_USB_GADGET_DUALSPEED */		case USB_DT_CONFIG:			ret = gs_build_config_buf(req->buf, gadget->speed,				wValue >> 8, wValue & 0xff,				gadget->is_otg);			if (ret >= 0)				ret = min(wLength, (u16)ret);			break;		case USB_DT_STRING:			/* wIndex == language code. */			ret = usb_gadget_get_string(&gs_string_table,				wValue & 0xff, req->buf);			if (ret >= 0)				ret = min(wLength, (u16)ret);			break;		}		break;	case USB_REQ_SET_CONFIGURATION:		if (ctrl->bRequestType != 0)			break;		spin_lock(&dev->dev_lock);		ret = gs_set_config(dev, wValue);		spin_unlock(&dev->dev_lock);		break;	case USB_REQ_GET_CONFIGURATION:		if (ctrl->bRequestType != USB_DIR_IN)			break;		*(u8 *)req->buf = dev->dev_config;		ret = min(wLength, (u16)1);		break;	case USB_REQ_SET_INTERFACE:		if (ctrl->bRequestType != USB_RECIP_INTERFACE				|| !dev->dev_config				|| wIndex >= GS_MAX_NUM_INTERFACES)			break;		if (dev->dev_config == GS_BULK_CONFIG_ID				&& wIndex != GS_BULK_INTERFACE_ID)			break;		/* no alternate interface settings */		if (wValue != 0)			break;		spin_lock(&dev->dev_lock);		/* PXA hardware partially handles SET_INTERFACE;		 * we need to kluge around that interference.  */		if (gadget_is_pxa(gadget)) {			ret = gs_set_config(dev, use_acm ?				GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID);			goto set_interface_done;		}		if (dev->dev_config != GS_BULK_CONFIG_ID				&& wIndex == GS_CONTROL_INTERFACE_ID) {			if (dev->dev_notify_ep) {				usb_ep_disable(dev->dev_notify_ep);				usb_ep_enable(dev->dev_notify_ep, dev->dev_notify_ep_desc);			}		} else {			usb_ep_disable(dev->dev_in_ep);			usb_ep_disable(dev->dev_out_ep);			usb_ep_enable(dev->dev_in_ep, dev->dev_in_ep_desc);			usb_ep_enable(dev->dev_out_ep, dev->dev_out_ep_desc);		}		ret = 0;set_interface_done:		spin_unlock(&dev->dev_lock);		break;	case USB_REQ_GET_INTERFACE:		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)		|| dev->dev_config == GS_NO_CONFIG_ID)			break;		if (wIndex >= GS_MAX_NUM_INTERFACES				|| (dev->dev_config == GS_BULK_CONFIG_ID				&& wIndex != GS_BULK_INTERFACE_ID)) {			ret = -EDOM;			break;		}		/* no alternate interface settings */		*(u8 *)req->buf = 0;		ret = min(wLength, (u16)1);		break;	default:		printk(KERN_ERR "gs_setup: unknown standard request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",			ctrl->bRequestType, ctrl->bRequest,			wValue, wIndex, wLength);		break;	}	return ret;}static int gs_setup_class(struct usb_gadget *gadget,	const struct usb_ctrlrequest *ctrl){	int ret = -EOPNOTSUPP;	struct gs_dev *dev = get_gadget_data(gadget);	struct gs_port *port = dev->dev_port[0];	/* ACM only has one port */	struct usb_request *req = dev->dev_ctrl_req;	u16 wIndex = le16_to_cpu(ctrl->wIndex);	u16 wValue = le16_to_cpu(ctrl->wValue);	u16 wLength = le16_to_cpu(ctrl->wLength);	switch (ctrl->bRequest) {	case USB_CDC_REQ_SET_LINE_CODING:		ret = min(wLength,			(u16)sizeof(struct usb_cdc_line_coding));		if (port) {			spin_lock(&port->port_lock);			memcpy(&port->port_line_coding, req->buf, ret);			spin_unlock(&port->port_lock);		}		break;	case USB_CDC_REQ_GET_LINE_CODING:		port = dev->dev_port[0];	/* ACM only has one port */		ret = min(wLength,			(u16)sizeof(struct usb_cdc_line_coding));		if (port) {			spin_lock(&port->port_lock);			memcpy(req->buf, &port->port_line_coding, ret);			spin_unlock(&port->port_lock);		}		break;	case USB_CDC_REQ_SET_CONTROL_LINE_STATE:		ret = 0;		break;	default:		printk(KERN_ERR "gs_setup: unknown class request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",			ctrl->bRequestType, ctrl->bRequest,			wValue, wIndex, wLength);		break;	}	return ret;}/* * gs_setup_complete */static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req){	if (req->status || req->actual != req->length) {		printk(KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",			req->status, req->actual, req->length);	}}/* * gs_disconnect *

⌨️ 快捷键说明

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