blob: e47cb9a9e724bbb560a3b0f42f0c844dd0bcb867 [file] [log] [blame]
wdenk232c1502004-03-12 00:14:09 +00001/*
2 * (C) Copyright 2003
3 * Gerry Hamel, geh@ti.com, Texas Instruments
Wolfgang Denk386eda02006-06-14 18:14:56 +02004 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02005 * (C) Copyright 2006
6 * Bryan O'Donoghue, bodonoghue@codehermit.ie
wdenk232c1502004-03-12 00:14:09 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010025#include <config.h>
wdenk232c1502004-03-12 00:14:09 +000026#include <circbuf.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020027#include <stdio_dev.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070028#include <asm/unaligned.h>
wdenk232c1502004-03-12 00:14:09 +000029#include "usbtty.h"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020030#include "usb_cdc_acm.h"
31#include "usbdescriptors.h"
wdenk232c1502004-03-12 00:14:09 +000032
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010033#ifdef DEBUG
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020034#define TTYDBG(fmt,args...)\
35 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000036#else
37#define TTYDBG(fmt,args...) do{}while(0)
38#endif
39
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020040#if 1
41#define TTYERR(fmt,args...)\
42 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
43 __LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000044#else
45#define TTYERR(fmt,args...) do{}while(0)
46#endif
47
48/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020049 * Defines
50 */
51#define NUM_CONFIGS 1
52#define MAX_INTERFACES 2
53#define NUM_ENDPOINTS 3
54#define ACM_TX_ENDPOINT 3
55#define ACM_RX_ENDPOINT 2
56#define GSERIAL_TX_ENDPOINT 2
57#define GSERIAL_RX_ENDPOINT 1
58#define NUM_ACM_INTERFACES 2
59#define NUM_GSERIAL_INTERFACES 1
60#define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
61#define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
62
63/*
wdenk232c1502004-03-12 00:14:09 +000064 * Buffers to hold input and output data
65 */
66#define USBTTY_BUFFER_SIZE 256
67static circbuf_t usbtty_input;
68static circbuf_t usbtty_output;
69
70
71/*
72 * Instance variables
73 */
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020074static struct stdio_dev usbttydev;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020075static struct usb_device_instance device_instance[1];
76static struct usb_bus_instance bus_instance[1];
wdenk232c1502004-03-12 00:14:09 +000077static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020078static struct usb_interface_instance interface_instance[MAX_INTERFACES];
79static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
80/* one extra for control endpoint */
81static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk232c1502004-03-12 00:14:09 +000082
83/*
84 * Global flag
85 */
86int usbtty_configured_flag = 0;
87
wdenk232c1502004-03-12 00:14:09 +000088/*
wdenk6629d2f2004-03-12 15:38:25 +000089 * Serial number
90 */
91static char serial_number[16];
92
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020093
wdenk6629d2f2004-03-12 15:38:25 +000094/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020095 * Descriptors, Strings, Local variables.
wdenk232c1502004-03-12 00:14:09 +000096 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020097
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020098/* defined and used by gadget/ep0.c */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020099extern struct usb_string_descriptor **usb_strings;
100
101/* Indicies, References */
102static unsigned short rx_endpoint = 0;
103static unsigned short tx_endpoint = 0;
104static unsigned short interface_count = 0;
105static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
106
107/* USB Descriptor Strings */
wdenk232c1502004-03-12 00:14:09 +0000108static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
109static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
110static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk6629d2f2004-03-12 15:38:25 +0000111static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
wdenk232c1502004-03-12 00:14:09 +0000112static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200113static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
114static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
wdenk232c1502004-03-12 00:14:09 +0000115
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200116/* Standard USB Data Structures */
117static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
118static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
119static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk232c1502004-03-12 00:14:09 +0000120static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200121 .bLength = sizeof(struct usb_device_descriptor),
122 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200123 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200124 .bDeviceSubClass = 0x00,
125 .bDeviceProtocol = 0x00,
126 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
127 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
128 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
129 .iManufacturer = STR_MANUFACTURER,
130 .iProduct = STR_PRODUCT,
131 .iSerialNumber = STR_SERIAL,
132 .bNumConfigurations = NUM_CONFIGS
wdenk232c1502004-03-12 00:14:09 +0000133};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200134
135
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530136#if defined(CONFIG_USBD_HS)
137static struct usb_qualifier_descriptor qualifier_descriptor = {
138 .bLength = sizeof(struct usb_qualifier_descriptor),
139 .bDescriptorType = USB_DT_QUAL,
140 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
141 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
142 .bDeviceSubClass = 0x00,
143 .bDeviceProtocol = 0x00,
144 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
145 .bNumConfigurations = NUM_CONFIGS
146};
147#endif
148
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200149/*
150 * Static CDC ACM specific descriptors
151 */
152
153struct acm_config_desc {
154 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200155
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200156 /* Master Interface */
157 struct usb_interface_descriptor interface_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200158
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200159 struct usb_class_header_function_descriptor usb_class_header;
160 struct usb_class_call_management_descriptor usb_class_call_mgt;
161 struct usb_class_abstract_control_descriptor usb_class_acm;
162 struct usb_class_union_function_descriptor usb_class_union;
163 struct usb_endpoint_descriptor notification_endpoint;
164
165 /* Slave Interface */
166 struct usb_interface_descriptor data_class_interface;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500167 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200168} __attribute__((packed));
169
170static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
171 {
172 .configuration_desc ={
Wolfgang Denk386eda02006-06-14 18:14:56 +0200173 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200174 sizeof(struct usb_configuration_descriptor),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200175 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200176 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200177 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200178 .bNumInterfaces = NUM_ACM_INTERFACES,
179 .bConfigurationValue = 1,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200180 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200181 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200182 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
183 .bMaxPower = USBTTY_MAXPOWER
184 },
185 /* Interface 1 */
186 .interface_desc = {
187 .bLength = sizeof(struct usb_interface_descriptor),
188 .bDescriptorType = USB_DT_INTERFACE,
189 .bInterfaceNumber = 0,
190 .bAlternateSetting = 0,
191 .bNumEndpoints = 0x01,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200192 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200193 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
194 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
195 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
196 .iInterface = STR_CTRL_INTERFACE,
197 },
198 .usb_class_header = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200199 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200200 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denk386eda02006-06-14 18:14:56 +0200201 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200202 .bDescriptorSubtype = USB_ST_HEADER,
203 .bcdCDC = cpu_to_le16(110),
204 },
205 .usb_class_call_mgt = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200206 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200207 sizeof(struct usb_class_call_management_descriptor),
208 .bDescriptorType = CS_INTERFACE,
209 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200210 .bmCapabilities = 0x00,
211 .bDataInterface = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200212 },
213 .usb_class_acm = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200214 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200215 sizeof(struct usb_class_abstract_control_descriptor),
216 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200217 .bDescriptorSubtype = USB_ST_ACMF,
218 .bmCapabilities = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200219 },
220 .usb_class_union = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200221 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200222 sizeof(struct usb_class_union_function_descriptor),
223 .bDescriptorType = CS_INTERFACE,
224 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200225 .bMasterInterface = 0x00,
226 .bSlaveInterface0 = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200227 },
228 .notification_endpoint = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200229 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200230 sizeof(struct usb_endpoint_descriptor),
231 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530232 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200233 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200234 .wMaxPacketSize
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200235 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
236 .bInterval = 0xFF,
237 },
238
239 /* Interface 2 */
240 .data_class_interface = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200241 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200242 sizeof(struct usb_interface_descriptor),
243 .bDescriptorType = USB_DT_INTERFACE,
244 .bInterfaceNumber = 0x01,
245 .bAlternateSetting = 0x00,
246 .bNumEndpoints = 0x02,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200247 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200248 COMMUNICATIONS_INTERFACE_CLASS_DATA,
249 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
250 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
251 .iInterface = STR_DATA_INTERFACE,
252 },
253 .data_endpoints = {
254 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200255 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200256 sizeof(struct usb_endpoint_descriptor),
257 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530258 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200259 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200260 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200261 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200262 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
263 .bInterval = 0xFF,
264 },
265 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200266 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200267 sizeof(struct usb_endpoint_descriptor),
268 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530269 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200270 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200271 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200272 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200273 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
274 .bInterval = 0xFF,
275 },
276 },
277 },
Wolfgang Denk386eda02006-06-14 18:14:56 +0200278};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200279
280static struct rs232_emu rs232_desc={
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200281 .dter = 115200,
282 .stop_bits = 0x00,
283 .parity = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200284 .data_bits = 0x08
wdenk232c1502004-03-12 00:14:09 +0000285};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200286
287
288/*
289 * Static Generic Serial specific data
290 */
291
292
293struct gserial_config_desc {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200294
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200295 struct usb_configuration_descriptor configuration_desc;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500296 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
297 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200298
299} __attribute__((packed));
300
Wolfgang Denk386eda02006-06-14 18:14:56 +0200301static struct gserial_config_desc
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200302gserial_configuration_descriptors[NUM_CONFIGS] ={
303 {
304 .configuration_desc ={
305 .bLength = sizeof(struct usb_configuration_descriptor),
306 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200307 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200308 cpu_to_le16(sizeof(struct gserial_config_desc)),
309 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
310 .bConfigurationValue = 1,
311 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200312 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200313 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
314 .bMaxPower = USBTTY_MAXPOWER
315 },
316 .interface_desc = {
317 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200318 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200319 sizeof(struct usb_interface_descriptor),
320 .bDescriptorType = USB_DT_INTERFACE,
321 .bInterfaceNumber = 0,
322 .bAlternateSetting = 0,
323 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200324 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200325 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200326 .bInterfaceSubClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200327 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200328 .bInterfaceProtocol =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200329 COMMUNICATIONS_NO_PROTOCOL,
330 .iInterface = STR_DATA_INTERFACE
331 },
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200332 },
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200333 .data_endpoints = {
334 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200335 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200336 sizeof(struct usb_endpoint_descriptor),
337 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530338 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200339 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200340 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200341 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
342 .bInterval= 0xFF,
343 },
344 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200345 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200346 sizeof(struct usb_endpoint_descriptor),
347 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530348 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200349 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200350 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200351 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200352 .bInterval = 0xFF,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200353 },
354 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200355 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200356 sizeof(struct usb_endpoint_descriptor),
357 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530358 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200359 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200360 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200361 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
362 .bInterval = 0xFF,
363 },
364 },
365 },
wdenk232c1502004-03-12 00:14:09 +0000366};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200367
368/*
369 * Static Function Prototypes
370 */
371
372static void usbtty_init_strings (void);
373static void usbtty_init_instances (void);
374static void usbtty_init_endpoints (void);
375static void usbtty_init_terminal_type(short type);
376static void usbtty_event_handler (struct usb_device_instance *device,
377 usb_device_event_t event, int data);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200378static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200379 struct urb *urb);
380static int usbtty_configured (void);
381static int write_buffer (circbuf_t * buf);
382static int fill_buffer (circbuf_t * buf);
383
384void usbtty_poll (void);
wdenk232c1502004-03-12 00:14:09 +0000385
386/* utility function for converting char* to wide string used by USB */
387static void str2wide (char *str, u16 * wide)
388{
389 int i;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200390 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti18135122007-06-06 10:08:14 +0200391 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200392 wide[i] = (u16) str[i];
Rodolfo Giometti18135122007-06-06 10:08:14 +0200393 #elif defined(__BIG_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200394 wide[i] = ((u16)(str[i])<<8);
395 #else
Rodolfo Giometti18135122007-06-06 10:08:14 +0200396 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200397 #endif
398 }
wdenk232c1502004-03-12 00:14:09 +0000399}
400
401/*
wdenk232c1502004-03-12 00:14:09 +0000402 * Test whether a character is in the RX buffer
403 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200404
wdenk232c1502004-03-12 00:14:09 +0000405int usbtty_tstc (void)
406{
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200407 struct usb_endpoint_instance *endpoint =
408 &endpoint_instance[rx_endpoint];
409
410 /* If no input data exists, allow more RX to be accepted */
411 if(usbtty_input.size <= 0){
412 udc_unset_nak(endpoint->endpoint_address&0x03);
413 }
414
wdenk232c1502004-03-12 00:14:09 +0000415 usbtty_poll ();
416 return (usbtty_input.size > 0);
417}
418
419/*
420 * Read a single byte from the usb client port. Returns 1 on success, 0
421 * otherwise. When the function is succesfull, the character read is
422 * written into its argument c.
423 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200424
wdenk232c1502004-03-12 00:14:09 +0000425int usbtty_getc (void)
426{
427 char c;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200428 struct usb_endpoint_instance *endpoint =
429 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000430
431 while (usbtty_input.size <= 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200432 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk232c1502004-03-12 00:14:09 +0000433 usbtty_poll ();
434 }
435
436 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200437 udc_set_nak(endpoint->endpoint_address&0x03);
438
wdenk232c1502004-03-12 00:14:09 +0000439 return c;
440}
441
442/*
443 * Output a single byte to the usb client port.
444 */
445void usbtty_putc (const char c)
446{
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500447 if (!usbtty_configured ())
448 return;
449
wdenk232c1502004-03-12 00:14:09 +0000450 buf_push (&usbtty_output, &c, 1);
451 /* If \n, also do \r */
452 if (c == '\n')
453 buf_push (&usbtty_output, "\r", 1);
454
455 /* Poll at end to handle new data... */
456 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
457 usbtty_poll ();
458 }
459}
460
wdenk232c1502004-03-12 00:14:09 +0000461/* usbtty_puts() helper function for finding the next '\n' in a string */
462static int next_nl_pos (const char *s)
463{
464 int i;
465
466 for (i = 0; s[i] != '\0'; i++) {
467 if (s[i] == '\n')
468 return i;
469 }
470 return i;
471}
472
473/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200474 * Output a string to the usb client port - implementing flow control
wdenk232c1502004-03-12 00:14:09 +0000475 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200476
wdenk232c1502004-03-12 00:14:09 +0000477static void __usbtty_puts (const char *str, int len)
478{
479 int maxlen = usbtty_output.totalsize;
480 int space, n;
481
482 /* break str into chunks < buffer size, if needed */
483 while (len > 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200484 usbtty_poll ();
485
wdenk232c1502004-03-12 00:14:09 +0000486 space = maxlen - usbtty_output.size;
wdenk232c1502004-03-12 00:14:09 +0000487 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200488 if (space) {
wdenk232c1502004-03-12 00:14:09 +0000489 write_buffer (&usbtty_output);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200490
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200491 n = MIN (space, MIN (len, maxlen));
492 buf_push (&usbtty_output, str, n);
493
494 str += n;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200495 len -= n;
wdenk232c1502004-03-12 00:14:09 +0000496 }
wdenk232c1502004-03-12 00:14:09 +0000497 }
498}
499
500void usbtty_puts (const char *str)
501{
502 int n;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500503 int len;
wdenk232c1502004-03-12 00:14:09 +0000504
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500505 if (!usbtty_configured ())
506 return;
507
508 len = strlen (str);
wdenk232c1502004-03-12 00:14:09 +0000509 /* add '\r' for each '\n' */
510 while (len > 0) {
511 n = next_nl_pos (str);
512
513 if (str[n] == '\n') {
514 __usbtty_puts (str, n + 1);
515 __usbtty_puts ("\r", 1);
516 str += (n + 1);
517 len -= (n + 1);
518 } else {
519 /* No \n found. All done. */
520 __usbtty_puts (str, n);
521 break;
522 }
523 }
524
525 /* Poll at end to handle new data... */
526 usbtty_poll ();
527}
528
529/*
530 * Initialize the usb client port.
531 *
532 */
533int drv_usbtty_init (void)
534{
535 int rc;
wdenk6629d2f2004-03-12 15:38:25 +0000536 char * sn;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200537 char * tt;
wdenk6629d2f2004-03-12 15:38:25 +0000538 int snlen;
wdenk42dfe7a2004-03-14 22:25:36 +0000539
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200540 /* Ger seiral number */
wdenk6629d2f2004-03-12 15:38:25 +0000541 if (!(sn = getenv("serial#"))) {
542 sn = "000000000000";
543 }
544 snlen = strlen(sn);
545 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denkb64f1902008-07-14 15:06:35 +0200546 printf ("Warning: serial number %s is too long (%d > %lu)\n",
547 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk6629d2f2004-03-12 15:38:25 +0000548 snlen = sizeof(serial_number) - 1;
549 }
550 memcpy (serial_number, sn, snlen);
551 serial_number[snlen] = '\0';
wdenk232c1502004-03-12 00:14:09 +0000552
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200553 /* Decide on which type of UDC device to be.
554 */
555
556 if(!(tt = getenv("usbtty"))) {
557 tt = "generic";
558 }
559 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denk386eda02006-06-14 18:14:56 +0200560
wdenk232c1502004-03-12 00:14:09 +0000561 /* prepare buffers... */
562 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
563 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
564
565 /* Now, set up USB controller and infrastructure */
566 udc_init (); /* Basic USB initialization */
567
568 usbtty_init_strings ();
569 usbtty_init_instances ();
570
Stefan Herbrechtsmeier241d9a62011-10-17 17:22:49 +0200571 usbtty_init_endpoints ();
572
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200573 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk232c1502004-03-12 00:14:09 +0000574 udc_connect (); /* Enable pullup for host detection */
575
wdenk232c1502004-03-12 00:14:09 +0000576 /* Device initialization */
577 memset (&usbttydev, 0, sizeof (usbttydev));
578
579 strcpy (usbttydev.name, "usbtty");
580 usbttydev.ext = 0; /* No extensions */
581 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
582 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
583 usbttydev.getc = usbtty_getc; /* 'getc' function */
584 usbttydev.putc = usbtty_putc; /* 'putc' function */
585 usbttydev.puts = usbtty_puts; /* 'puts' function */
586
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200587 rc = stdio_register (&usbttydev);
wdenk232c1502004-03-12 00:14:09 +0000588
589 return (rc == 0) ? 1 : rc;
590}
591
592static void usbtty_init_strings (void)
593{
594 struct usb_string_descriptor *string;
595
Wolfgang Denk386eda02006-06-14 18:14:56 +0200596 usbtty_string_table[STR_LANG] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200597 (struct usb_string_descriptor*)wstrLang;
598
wdenk232c1502004-03-12 00:14:09 +0000599 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200600 string->bLength = sizeof(wstrManufacturer);
wdenk232c1502004-03-12 00:14:09 +0000601 string->bDescriptorType = USB_DT_STRING;
602 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200603 usbtty_string_table[STR_MANUFACTURER]=string;
604
wdenk232c1502004-03-12 00:14:09 +0000605
606 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200607 string->bLength = sizeof(wstrProduct);
wdenk232c1502004-03-12 00:14:09 +0000608 string->bDescriptorType = USB_DT_STRING;
609 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200610 usbtty_string_table[STR_PRODUCT]=string;
611
wdenk232c1502004-03-12 00:14:09 +0000612
613 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200614 string->bLength = sizeof(serial_number);
wdenk232c1502004-03-12 00:14:09 +0000615 string->bDescriptorType = USB_DT_STRING;
wdenk6629d2f2004-03-12 15:38:25 +0000616 str2wide (serial_number, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200617 usbtty_string_table[STR_SERIAL]=string;
618
wdenk232c1502004-03-12 00:14:09 +0000619
620 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200621 string->bLength = sizeof(wstrConfiguration);
wdenk232c1502004-03-12 00:14:09 +0000622 string->bDescriptorType = USB_DT_STRING;
623 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200624 usbtty_string_table[STR_CONFIG]=string;
wdenk232c1502004-03-12 00:14:09 +0000625
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200626
627 string = (struct usb_string_descriptor *) wstrDataInterface;
628 string->bLength = sizeof(wstrDataInterface);
wdenk232c1502004-03-12 00:14:09 +0000629 string->bDescriptorType = USB_DT_STRING;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200630 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
631 usbtty_string_table[STR_DATA_INTERFACE]=string;
632
633 string = (struct usb_string_descriptor *) wstrCtrlInterface;
634 string->bLength = sizeof(wstrCtrlInterface);
635 string->bDescriptorType = USB_DT_STRING;
636 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
637 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk232c1502004-03-12 00:14:09 +0000638
639 /* Now, initialize the string table for ep0 handling */
640 usb_strings = usbtty_string_table;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200641}
wdenk232c1502004-03-12 00:14:09 +0000642
Tom Rinib2fb47f2011-12-15 08:40:51 -0700643#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
644 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
645
wdenk232c1502004-03-12 00:14:09 +0000646static void usbtty_init_instances (void)
647{
648 int i;
649
650 /* initialize device instance */
651 memset (device_instance, 0, sizeof (struct usb_device_instance));
652 device_instance->device_state = STATE_INIT;
653 device_instance->device_descriptor = &device_descriptor;
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530654#if defined(CONFIG_USBD_HS)
655 device_instance->qualifier_descriptor = &qualifier_descriptor;
656#endif
wdenk232c1502004-03-12 00:14:09 +0000657 device_instance->event = usbtty_event_handler;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200658 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk232c1502004-03-12 00:14:09 +0000659 device_instance->bus = bus_instance;
660 device_instance->configurations = NUM_CONFIGS;
661 device_instance->configuration_instance_array = config_instance;
662
663 /* initialize bus instance */
664 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
665 bus_instance->device = device_instance;
666 bus_instance->endpoint_array = endpoint_instance;
667 bus_instance->max_endpoints = 1;
668 bus_instance->maxpacketsize = 64;
wdenk6629d2f2004-03-12 15:38:25 +0000669 bus_instance->serial_number_str = serial_number;
wdenk232c1502004-03-12 00:14:09 +0000670
671 /* configuration instance */
672 memset (config_instance, 0,
673 sizeof (struct usb_configuration_instance));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200674 config_instance->interfaces = interface_count;
675 config_instance->configuration_descriptor = configuration_descriptor;
wdenk232c1502004-03-12 00:14:09 +0000676 config_instance->interface_instance_array = interface_instance;
677
678 /* interface instance */
679 memset (interface_instance, 0,
680 sizeof (struct usb_interface_instance));
681 interface_instance->alternates = 1;
682 interface_instance->alternates_instance_array = alternate_instance;
683
684 /* alternates instance */
685 memset (alternate_instance, 0,
686 sizeof (struct usb_alternate_instance));
687 alternate_instance->interface_descriptor = interface_descriptors;
688 alternate_instance->endpoints = NUM_ENDPOINTS;
689 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
690
691 /* endpoint instances */
692 memset (&endpoint_instance[0], 0,
693 sizeof (struct usb_endpoint_instance));
694 endpoint_instance[0].endpoint_address = 0;
695 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
696 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
697 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
698 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
699 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
700
701 for (i = 1; i <= NUM_ENDPOINTS; i++) {
702 memset (&endpoint_instance[i], 0,
703 sizeof (struct usb_endpoint_instance));
704
705 endpoint_instance[i].endpoint_address =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200706 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
707
708 endpoint_instance[i].rcv_attributes =
709 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000710
Tom Rinib2fb47f2011-12-15 08:40:51 -0700711 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200712
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200713 endpoint_instance[i].tx_attributes =
714 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000715
Tom Rinib2fb47f2011-12-15 08:40:51 -0700716 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200717
wdenk232c1502004-03-12 00:14:09 +0000718 endpoint_instance[i].tx_attributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200719 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000720
721 urb_link_init (&endpoint_instance[i].rcv);
722 urb_link_init (&endpoint_instance[i].rdy);
723 urb_link_init (&endpoint_instance[i].tx);
724 urb_link_init (&endpoint_instance[i].done);
725
726 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
727 endpoint_instance[i].tx_urb =
728 usbd_alloc_urb (device_instance,
729 &endpoint_instance[i]);
730 else
731 endpoint_instance[i].rcv_urb =
732 usbd_alloc_urb (device_instance,
733 &endpoint_instance[i]);
734 }
735}
736
737static void usbtty_init_endpoints (void)
738{
739 int i;
740
741 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200742 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk232c1502004-03-12 00:14:09 +0000743 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
744 }
745}
746
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200747/* usbtty_init_terminal_type
Wolfgang Denk386eda02006-06-14 18:14:56 +0200748 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200749 * Do some late binding for our device type.
750 */
751static void usbtty_init_terminal_type(short type)
752{
753 switch(type){
Wolfgang Denk386eda02006-06-14 18:14:56 +0200754 /* CDC ACM */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200755 case 0:
756 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200757 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200758 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200759 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200760 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200761 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200762 &acm_configuration_descriptors[0].data_endpoints[1];
wdenk232c1502004-03-12 00:14:09 +0000763
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200764 /* Enumerate Device Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200765 device_descriptor.bDeviceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200766 COMMUNICATIONS_DEVICE_CLASS;
767 device_descriptor.idProduct =
768 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
769
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530770#if defined(CONFIG_USBD_HS)
771 qualifier_descriptor.bDeviceClass =
772 COMMUNICATIONS_DEVICE_CLASS;
773#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200774 /* Assign endpoint indices */
775 tx_endpoint = ACM_TX_ENDPOINT;
776 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200777
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200778 /* Configuration Descriptor */
779 configuration_descriptor =
780 (struct usb_configuration_descriptor*)
781 &acm_configuration_descriptors;
782
783 /* Interface count */
784 interface_count = NUM_ACM_INTERFACES;
785 break;
786
787 /* BULK IN/OUT & Default */
788 case 1:
789 default:
790 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200791 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200792 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200793 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200794 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200795 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200796 &gserial_configuration_descriptors[0].data_endpoints[2];
797
798 /* Enumerate Device Descriptor */
799 device_descriptor.bDeviceClass = 0xFF;
800 device_descriptor.idProduct =
801 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530802#if defined(CONFIG_USBD_HS)
803 qualifier_descriptor.bDeviceClass = 0xFF;
804#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200805 /* Assign endpoint indices */
806 tx_endpoint = GSERIAL_TX_ENDPOINT;
807 rx_endpoint = GSERIAL_RX_ENDPOINT;
808
809 /* Configuration Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200810 configuration_descriptor =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200811 (struct usb_configuration_descriptor*)
812 &gserial_configuration_descriptors;
813
814 /* Interface count */
815 interface_count = NUM_GSERIAL_INTERFACES;
816 break;
817 }
818}
819
820/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000821
822static struct urb *next_urb (struct usb_device_instance *device,
823 struct usb_endpoint_instance *endpoint)
824{
825 struct urb *current_urb = NULL;
826 int space;
827
828 /* If there's a queue, then we should add to the last urb */
829 if (!endpoint->tx_queue) {
830 current_urb = endpoint->tx_urb;
831 } else {
832 /* Last urb from tx chain */
833 current_urb =
834 p2surround (struct urb, link, endpoint->tx.prev);
835 }
836
837 /* Make sure this one has enough room */
838 space = current_urb->buffer_length - current_urb->actual_length;
839 if (space > 0) {
840 return current_urb;
841 } else { /* No space here */
842 /* First look at done list */
843 current_urb = first_urb_detached (&endpoint->done);
844 if (!current_urb) {
845 current_urb = usbd_alloc_urb (device, endpoint);
846 }
847
848 urb_append (&endpoint->tx, current_urb);
849 endpoint->tx_queue++;
850 }
851 return current_urb;
852}
853
854static int write_buffer (circbuf_t * buf)
855{
856 if (!usbtty_configured ()) {
857 return 0;
858 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200859
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200860 struct usb_endpoint_instance *endpoint =
861 &endpoint_instance[tx_endpoint];
862 struct urb *current_urb = NULL;
863
864 current_urb = next_urb (device_instance, endpoint);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200865 /* TX data still exists - send it now
866 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200867 if(endpoint->sent < current_urb->actual_length){
868 if(udc_endpoint_write (endpoint)){
869 /* Write pre-empted by RX */
870 return -1;
871 }
872 }
wdenk232c1502004-03-12 00:14:09 +0000873
874 if (buf->size) {
wdenk232c1502004-03-12 00:14:09 +0000875 char *dest;
876
877 int space_avail;
878 int popnum, popped;
879 int total = 0;
880
Wolfgang Denk386eda02006-06-14 18:14:56 +0200881 /* Break buffer into urb sized pieces,
882 * and link each to the endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200883 */
wdenk232c1502004-03-12 00:14:09 +0000884 while (buf->size > 0) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200885
wdenk232c1502004-03-12 00:14:09 +0000886 if (!current_urb) {
887 TTYERR ("current_urb is NULL, buf->size %d\n",
888 buf->size);
889 return total;
890 }
891
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200892 dest = (char*)current_urb->buffer +
wdenk232c1502004-03-12 00:14:09 +0000893 current_urb->actual_length;
894
895 space_avail =
896 current_urb->buffer_length -
897 current_urb->actual_length;
898 popnum = MIN (space_avail, buf->size);
899 if (popnum == 0)
900 break;
901
902 popped = buf_pop (buf, dest, popnum);
903 if (popped == 0)
904 break;
905 current_urb->actual_length += popped;
906 total += popped;
907
Wolfgang Denk386eda02006-06-14 18:14:56 +0200908 /* If endpoint->last == 0, then transfers have
909 * not started on this endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200910 */
wdenk232c1502004-03-12 00:14:09 +0000911 if (endpoint->last == 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200912 if(udc_endpoint_write (endpoint)){
913 /* Write pre-empted by RX */
914 return -1;
915 }
wdenk232c1502004-03-12 00:14:09 +0000916 }
917
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200918 }/* end while */
wdenk232c1502004-03-12 00:14:09 +0000919 return total;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200920 }
wdenk232c1502004-03-12 00:14:09 +0000921
922 return 0;
923}
924
925static int fill_buffer (circbuf_t * buf)
926{
927 struct usb_endpoint_instance *endpoint =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200928 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000929
930 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200931 unsigned int nb = 0;
wdenk232c1502004-03-12 00:14:09 +0000932 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200933 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk232c1502004-03-12 00:14:09 +0000934
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200935 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk232c1502004-03-12 00:14:09 +0000936
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200937 nb = endpoint->rcv_urb->actual_length;
938 buf_push (buf, src, nb);
939 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200940
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200941 }
wdenk232c1502004-03-12 00:14:09 +0000942 return nb;
943 }
wdenk232c1502004-03-12 00:14:09 +0000944 return 0;
945}
946
947static int usbtty_configured (void)
948{
949 return usbtty_configured_flag;
950}
951
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200952/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000953
954static void usbtty_event_handler (struct usb_device_instance *device,
955 usb_device_event_t event, int data)
956{
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530957#if defined(CONFIG_USBD_HS)
958 int i;
959#endif
wdenk232c1502004-03-12 00:14:09 +0000960 switch (event) {
961 case DEVICE_RESET:
962 case DEVICE_BUS_INACTIVE:
963 usbtty_configured_flag = 0;
964 break;
965 case DEVICE_CONFIGURED:
966 usbtty_configured_flag = 1;
967 break;
968
969 case DEVICE_ADDRESS_ASSIGNED:
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530970#if defined(CONFIG_USBD_HS)
971 /*
972 * is_usbd_high_speed routine needs to be defined by
973 * specific gadget driver
974 * It returns TRUE if device enumerates at High speed
975 * Retuns FALSE otherwise
976 */
977 for (i = 0; i < NUM_ENDPOINTS; i++) {
978 if (((ep_descriptor_ptrs[i]->bmAttributes &
979 USB_ENDPOINT_XFERTYPE_MASK) ==
980 USB_ENDPOINT_XFER_BULK)
981 && is_usbd_high_speed()) {
982
983 ep_descriptor_ptrs[i]->wMaxPacketSize =
984 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
985 }
986
987 endpoint_instance[i + 1].tx_packetSize =
988 ep_descriptor_ptrs[i]->wMaxPacketSize;
989 endpoint_instance[i + 1].rcv_packetSize =
990 ep_descriptor_ptrs[i]->wMaxPacketSize;
991 }
992#endif
wdenk232c1502004-03-12 00:14:09 +0000993 usbtty_init_endpoints ();
994
995 default:
996 break;
997 }
998}
999
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001000/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +00001001
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001002int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
1003{
1004 switch (request->bRequest){
1005
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001006 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001007 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001008 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001009 break;
1010 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denk386eda02006-06-14 18:14:56 +02001011 * per character */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001012 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001013 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001014 break;
1015 case ACM_GET_LINE_ENCODING : /* request DTE rate,
1016 * stop/parity bits */
1017 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1018 urb->actual_length = sizeof(rs232_desc);
1019
1020 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001021 default:
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001022 return 1;
1023 }
1024 return 0;
1025}
1026
1027/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +00001028
1029/*
1030 * Since interrupt handling has not yet been implemented, we use this function
1031 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1032 * update the USB state.
1033 */
1034void usbtty_poll (void)
1035{
1036 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001037 udc_irq();
wdenk232c1502004-03-12 00:14:09 +00001038
Wolfgang Denk386eda02006-06-14 18:14:56 +02001039 /* Write any output data to host buffer
1040 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001041 */
wdenk232c1502004-03-12 00:14:09 +00001042 if (usbtty_configured ()) {
1043 write_buffer (&usbtty_output);
1044 }
1045
1046 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001047 udc_irq();
Wolfgang Denk386eda02006-06-14 18:14:56 +02001048
1049 /* Check for new data from host..
1050 * (do this after checking interrupts to get latest data)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001051 */
wdenk232c1502004-03-12 00:14:09 +00001052 if (usbtty_configured ()) {
1053 fill_buffer (&usbtty_input);
1054 }
1055
1056 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001057 udc_irq();
1058
wdenk232c1502004-03-12 00:14:09 +00001059}