blob: b030526b6a1f51f182b1fd6a7558c641b5f0b32a [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk232c1502004-03-12 00:14:09 +00009 */
10
11#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010012#include <config.h>
wdenk232c1502004-03-12 00:14:09 +000013#include <circbuf.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020014#include <stdio_dev.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070015#include <asm/unaligned.h>
wdenk232c1502004-03-12 00:14:09 +000016#include "usbtty.h"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020017#include "usb_cdc_acm.h"
18#include "usbdescriptors.h"
wdenk232c1502004-03-12 00:14:09 +000019
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010020#ifdef DEBUG
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020021#define TTYDBG(fmt,args...)\
22 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000023#else
24#define TTYDBG(fmt,args...) do{}while(0)
25#endif
26
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020027#if 1
28#define TTYERR(fmt,args...)\
29 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
30 __LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000031#else
32#define TTYERR(fmt,args...) do{}while(0)
33#endif
34
35/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020036 * Defines
37 */
38#define NUM_CONFIGS 1
39#define MAX_INTERFACES 2
40#define NUM_ENDPOINTS 3
41#define ACM_TX_ENDPOINT 3
42#define ACM_RX_ENDPOINT 2
43#define GSERIAL_TX_ENDPOINT 2
44#define GSERIAL_RX_ENDPOINT 1
45#define NUM_ACM_INTERFACES 2
46#define NUM_GSERIAL_INTERFACES 1
47#define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
48#define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
49
50/*
wdenk232c1502004-03-12 00:14:09 +000051 * Buffers to hold input and output data
52 */
Shiraz Hashimb2caefb2012-12-17 14:19:37 +053053#define USBTTY_BUFFER_SIZE 2048
wdenk232c1502004-03-12 00:14:09 +000054static circbuf_t usbtty_input;
55static circbuf_t usbtty_output;
56
57
58/*
59 * Instance variables
60 */
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020061static struct stdio_dev usbttydev;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020062static struct usb_device_instance device_instance[1];
63static struct usb_bus_instance bus_instance[1];
wdenk232c1502004-03-12 00:14:09 +000064static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020065static struct usb_interface_instance interface_instance[MAX_INTERFACES];
66static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
67/* one extra for control endpoint */
68static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk232c1502004-03-12 00:14:09 +000069
70/*
71 * Global flag
72 */
73int usbtty_configured_flag = 0;
74
wdenk232c1502004-03-12 00:14:09 +000075/*
wdenk6629d2f2004-03-12 15:38:25 +000076 * Serial number
77 */
78static char serial_number[16];
79
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020080
wdenk6629d2f2004-03-12 15:38:25 +000081/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020082 * Descriptors, Strings, Local variables.
wdenk232c1502004-03-12 00:14:09 +000083 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020084
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020085/* defined and used by gadget/ep0.c */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020086extern struct usb_string_descriptor **usb_strings;
87
88/* Indicies, References */
89static unsigned short rx_endpoint = 0;
90static unsigned short tx_endpoint = 0;
91static unsigned short interface_count = 0;
92static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
93
94/* USB Descriptor Strings */
wdenk232c1502004-03-12 00:14:09 +000095static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
96static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
97static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk6629d2f2004-03-12 15:38:25 +000098static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
wdenk232c1502004-03-12 00:14:09 +000099static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200100static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
101static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
wdenk232c1502004-03-12 00:14:09 +0000102
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200103/* Standard USB Data Structures */
104static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
105static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
106static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk232c1502004-03-12 00:14:09 +0000107static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200108 .bLength = sizeof(struct usb_device_descriptor),
109 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200110 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200111 .bDeviceSubClass = 0x00,
112 .bDeviceProtocol = 0x00,
113 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
114 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
115 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
116 .iManufacturer = STR_MANUFACTURER,
117 .iProduct = STR_PRODUCT,
118 .iSerialNumber = STR_SERIAL,
119 .bNumConfigurations = NUM_CONFIGS
wdenk232c1502004-03-12 00:14:09 +0000120};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200121
122
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530123#if defined(CONFIG_USBD_HS)
124static struct usb_qualifier_descriptor qualifier_descriptor = {
125 .bLength = sizeof(struct usb_qualifier_descriptor),
126 .bDescriptorType = USB_DT_QUAL,
127 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
128 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
129 .bDeviceSubClass = 0x00,
130 .bDeviceProtocol = 0x00,
131 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
132 .bNumConfigurations = NUM_CONFIGS
133};
134#endif
135
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200136/*
137 * Static CDC ACM specific descriptors
138 */
139
140struct acm_config_desc {
141 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200142
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200143 /* Master Interface */
144 struct usb_interface_descriptor interface_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200145
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200146 struct usb_class_header_function_descriptor usb_class_header;
147 struct usb_class_call_management_descriptor usb_class_call_mgt;
148 struct usb_class_abstract_control_descriptor usb_class_acm;
149 struct usb_class_union_function_descriptor usb_class_union;
150 struct usb_endpoint_descriptor notification_endpoint;
151
152 /* Slave Interface */
153 struct usb_interface_descriptor data_class_interface;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500154 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200155} __attribute__((packed));
156
157static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
158 {
159 .configuration_desc ={
Wolfgang Denk386eda02006-06-14 18:14:56 +0200160 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200161 sizeof(struct usb_configuration_descriptor),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200162 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200163 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200164 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200165 .bNumInterfaces = NUM_ACM_INTERFACES,
166 .bConfigurationValue = 1,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200167 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200168 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200169 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
170 .bMaxPower = USBTTY_MAXPOWER
171 },
172 /* Interface 1 */
173 .interface_desc = {
174 .bLength = sizeof(struct usb_interface_descriptor),
175 .bDescriptorType = USB_DT_INTERFACE,
176 .bInterfaceNumber = 0,
177 .bAlternateSetting = 0,
178 .bNumEndpoints = 0x01,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200179 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200180 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
181 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
182 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
183 .iInterface = STR_CTRL_INTERFACE,
184 },
185 .usb_class_header = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200186 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200187 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denk386eda02006-06-14 18:14:56 +0200188 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200189 .bDescriptorSubtype = USB_ST_HEADER,
190 .bcdCDC = cpu_to_le16(110),
191 },
192 .usb_class_call_mgt = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200193 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200194 sizeof(struct usb_class_call_management_descriptor),
195 .bDescriptorType = CS_INTERFACE,
196 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200197 .bmCapabilities = 0x00,
198 .bDataInterface = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200199 },
200 .usb_class_acm = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200201 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200202 sizeof(struct usb_class_abstract_control_descriptor),
203 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200204 .bDescriptorSubtype = USB_ST_ACMF,
205 .bmCapabilities = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200206 },
207 .usb_class_union = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200208 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200209 sizeof(struct usb_class_union_function_descriptor),
210 .bDescriptorType = CS_INTERFACE,
211 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200212 .bMasterInterface = 0x00,
213 .bSlaveInterface0 = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200214 },
215 .notification_endpoint = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200216 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200217 sizeof(struct usb_endpoint_descriptor),
218 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530219 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200220 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200221 .wMaxPacketSize
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200222 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
223 .bInterval = 0xFF,
224 },
225
226 /* Interface 2 */
227 .data_class_interface = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200228 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200229 sizeof(struct usb_interface_descriptor),
230 .bDescriptorType = USB_DT_INTERFACE,
231 .bInterfaceNumber = 0x01,
232 .bAlternateSetting = 0x00,
233 .bNumEndpoints = 0x02,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200234 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200235 COMMUNICATIONS_INTERFACE_CLASS_DATA,
236 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
237 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
238 .iInterface = STR_DATA_INTERFACE,
239 },
240 .data_endpoints = {
241 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200242 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200243 sizeof(struct usb_endpoint_descriptor),
244 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530245 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200246 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200247 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200248 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200249 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
250 .bInterval = 0xFF,
251 },
252 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200253 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200254 sizeof(struct usb_endpoint_descriptor),
255 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530256 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200257 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200258 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200259 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200260 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
261 .bInterval = 0xFF,
262 },
263 },
264 },
Wolfgang Denk386eda02006-06-14 18:14:56 +0200265};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200266
267static struct rs232_emu rs232_desc={
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200268 .dter = 115200,
269 .stop_bits = 0x00,
270 .parity = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200271 .data_bits = 0x08
wdenk232c1502004-03-12 00:14:09 +0000272};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200273
274
275/*
276 * Static Generic Serial specific data
277 */
278
279
280struct gserial_config_desc {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200281
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200282 struct usb_configuration_descriptor configuration_desc;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500283 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
284 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200285
286} __attribute__((packed));
287
Wolfgang Denk386eda02006-06-14 18:14:56 +0200288static struct gserial_config_desc
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200289gserial_configuration_descriptors[NUM_CONFIGS] ={
290 {
291 .configuration_desc ={
292 .bLength = sizeof(struct usb_configuration_descriptor),
293 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200294 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200295 cpu_to_le16(sizeof(struct gserial_config_desc)),
296 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
297 .bConfigurationValue = 1,
298 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200299 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200300 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
301 .bMaxPower = USBTTY_MAXPOWER
302 },
303 .interface_desc = {
304 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200305 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200306 sizeof(struct usb_interface_descriptor),
307 .bDescriptorType = USB_DT_INTERFACE,
308 .bInterfaceNumber = 0,
309 .bAlternateSetting = 0,
310 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200311 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200312 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200313 .bInterfaceSubClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200314 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200315 .bInterfaceProtocol =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200316 COMMUNICATIONS_NO_PROTOCOL,
317 .iInterface = STR_DATA_INTERFACE
318 },
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200319 },
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200320 .data_endpoints = {
321 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200322 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200323 sizeof(struct usb_endpoint_descriptor),
324 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530325 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200326 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200327 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200328 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
329 .bInterval= 0xFF,
330 },
331 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200332 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200333 sizeof(struct usb_endpoint_descriptor),
334 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530335 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200337 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200338 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200339 .bInterval = 0xFF,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200340 },
341 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200342 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200343 sizeof(struct usb_endpoint_descriptor),
344 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530345 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200346 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200347 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200348 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
349 .bInterval = 0xFF,
350 },
351 },
352 },
wdenk232c1502004-03-12 00:14:09 +0000353};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200354
355/*
356 * Static Function Prototypes
357 */
358
359static void usbtty_init_strings (void);
360static void usbtty_init_instances (void);
361static void usbtty_init_endpoints (void);
362static void usbtty_init_terminal_type(short type);
363static void usbtty_event_handler (struct usb_device_instance *device,
364 usb_device_event_t event, int data);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200365static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200366 struct urb *urb);
367static int usbtty_configured (void);
368static int write_buffer (circbuf_t * buf);
369static int fill_buffer (circbuf_t * buf);
370
371void usbtty_poll (void);
wdenk232c1502004-03-12 00:14:09 +0000372
373/* utility function for converting char* to wide string used by USB */
374static void str2wide (char *str, u16 * wide)
375{
376 int i;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200377 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti18135122007-06-06 10:08:14 +0200378 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200379 wide[i] = (u16) str[i];
Rodolfo Giometti18135122007-06-06 10:08:14 +0200380 #elif defined(__BIG_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200381 wide[i] = ((u16)(str[i])<<8);
382 #else
Rodolfo Giometti18135122007-06-06 10:08:14 +0200383 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200384 #endif
385 }
wdenk232c1502004-03-12 00:14:09 +0000386}
387
388/*
wdenk232c1502004-03-12 00:14:09 +0000389 * Test whether a character is in the RX buffer
390 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200391
Simon Glass709ea542014-07-23 06:54:59 -0600392int usbtty_tstc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000393{
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200394 struct usb_endpoint_instance *endpoint =
395 &endpoint_instance[rx_endpoint];
396
397 /* If no input data exists, allow more RX to be accepted */
398 if(usbtty_input.size <= 0){
399 udc_unset_nak(endpoint->endpoint_address&0x03);
400 }
401
wdenk232c1502004-03-12 00:14:09 +0000402 usbtty_poll ();
403 return (usbtty_input.size > 0);
404}
405
406/*
407 * Read a single byte from the usb client port. Returns 1 on success, 0
408 * otherwise. When the function is succesfull, the character read is
409 * written into its argument c.
410 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200411
Simon Glass709ea542014-07-23 06:54:59 -0600412int usbtty_getc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000413{
414 char c;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200415 struct usb_endpoint_instance *endpoint =
416 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000417
418 while (usbtty_input.size <= 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200419 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk232c1502004-03-12 00:14:09 +0000420 usbtty_poll ();
421 }
422
423 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200424 udc_set_nak(endpoint->endpoint_address&0x03);
425
wdenk232c1502004-03-12 00:14:09 +0000426 return c;
427}
428
429/*
430 * Output a single byte to the usb client port.
431 */
Simon Glass709ea542014-07-23 06:54:59 -0600432void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk232c1502004-03-12 00:14:09 +0000433{
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500434 if (!usbtty_configured ())
435 return;
436
wdenk232c1502004-03-12 00:14:09 +0000437 buf_push (&usbtty_output, &c, 1);
438 /* If \n, also do \r */
439 if (c == '\n')
440 buf_push (&usbtty_output, "\r", 1);
441
442 /* Poll at end to handle new data... */
443 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
444 usbtty_poll ();
445 }
446}
447
wdenk232c1502004-03-12 00:14:09 +0000448/* usbtty_puts() helper function for finding the next '\n' in a string */
449static int next_nl_pos (const char *s)
450{
451 int i;
452
453 for (i = 0; s[i] != '\0'; i++) {
454 if (s[i] == '\n')
455 return i;
456 }
457 return i;
458}
459
460/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200461 * Output a string to the usb client port - implementing flow control
wdenk232c1502004-03-12 00:14:09 +0000462 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200463
wdenk232c1502004-03-12 00:14:09 +0000464static void __usbtty_puts (const char *str, int len)
465{
466 int maxlen = usbtty_output.totalsize;
467 int space, n;
468
469 /* break str into chunks < buffer size, if needed */
470 while (len > 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200471 usbtty_poll ();
472
wdenk232c1502004-03-12 00:14:09 +0000473 space = maxlen - usbtty_output.size;
wdenk232c1502004-03-12 00:14:09 +0000474 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200475 if (space) {
wdenk232c1502004-03-12 00:14:09 +0000476 write_buffer (&usbtty_output);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200477
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200478 n = MIN (space, MIN (len, maxlen));
479 buf_push (&usbtty_output, str, n);
480
481 str += n;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200482 len -= n;
wdenk232c1502004-03-12 00:14:09 +0000483 }
wdenk232c1502004-03-12 00:14:09 +0000484 }
485}
486
Simon Glass709ea542014-07-23 06:54:59 -0600487void usbtty_puts(struct stdio_dev *dev, const char *str)
wdenk232c1502004-03-12 00:14:09 +0000488{
489 int n;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500490 int len;
wdenk232c1502004-03-12 00:14:09 +0000491
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500492 if (!usbtty_configured ())
493 return;
494
495 len = strlen (str);
wdenk232c1502004-03-12 00:14:09 +0000496 /* add '\r' for each '\n' */
497 while (len > 0) {
498 n = next_nl_pos (str);
499
500 if (str[n] == '\n') {
501 __usbtty_puts (str, n + 1);
502 __usbtty_puts ("\r", 1);
503 str += (n + 1);
504 len -= (n + 1);
505 } else {
506 /* No \n found. All done. */
507 __usbtty_puts (str, n);
508 break;
509 }
510 }
511
512 /* Poll at end to handle new data... */
513 usbtty_poll ();
514}
515
516/*
517 * Initialize the usb client port.
518 *
519 */
520int drv_usbtty_init (void)
521{
522 int rc;
wdenk6629d2f2004-03-12 15:38:25 +0000523 char * sn;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200524 char * tt;
wdenk6629d2f2004-03-12 15:38:25 +0000525 int snlen;
wdenk42dfe7a2004-03-14 22:25:36 +0000526
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200527 /* Ger seiral number */
wdenk6629d2f2004-03-12 15:38:25 +0000528 if (!(sn = getenv("serial#"))) {
529 sn = "000000000000";
530 }
531 snlen = strlen(sn);
532 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denkb64f1902008-07-14 15:06:35 +0200533 printf ("Warning: serial number %s is too long (%d > %lu)\n",
534 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk6629d2f2004-03-12 15:38:25 +0000535 snlen = sizeof(serial_number) - 1;
536 }
537 memcpy (serial_number, sn, snlen);
538 serial_number[snlen] = '\0';
wdenk232c1502004-03-12 00:14:09 +0000539
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200540 /* Decide on which type of UDC device to be.
541 */
542
543 if(!(tt = getenv("usbtty"))) {
544 tt = "generic";
545 }
546 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denk386eda02006-06-14 18:14:56 +0200547
wdenk232c1502004-03-12 00:14:09 +0000548 /* prepare buffers... */
549 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
550 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
551
552 /* Now, set up USB controller and infrastructure */
553 udc_init (); /* Basic USB initialization */
554
555 usbtty_init_strings ();
556 usbtty_init_instances ();
557
Stefan Herbrechtsmeier241d9a62011-10-17 17:22:49 +0200558 usbtty_init_endpoints ();
559
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200560 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk232c1502004-03-12 00:14:09 +0000561 udc_connect (); /* Enable pullup for host detection */
562
wdenk232c1502004-03-12 00:14:09 +0000563 /* Device initialization */
564 memset (&usbttydev, 0, sizeof (usbttydev));
565
566 strcpy (usbttydev.name, "usbtty");
567 usbttydev.ext = 0; /* No extensions */
568 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
569 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
570 usbttydev.getc = usbtty_getc; /* 'getc' function */
571 usbttydev.putc = usbtty_putc; /* 'putc' function */
572 usbttydev.puts = usbtty_puts; /* 'puts' function */
573
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200574 rc = stdio_register (&usbttydev);
wdenk232c1502004-03-12 00:14:09 +0000575
576 return (rc == 0) ? 1 : rc;
577}
578
579static void usbtty_init_strings (void)
580{
581 struct usb_string_descriptor *string;
582
Wolfgang Denk386eda02006-06-14 18:14:56 +0200583 usbtty_string_table[STR_LANG] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200584 (struct usb_string_descriptor*)wstrLang;
585
wdenk232c1502004-03-12 00:14:09 +0000586 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200587 string->bLength = sizeof(wstrManufacturer);
wdenk232c1502004-03-12 00:14:09 +0000588 string->bDescriptorType = USB_DT_STRING;
589 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200590 usbtty_string_table[STR_MANUFACTURER]=string;
591
wdenk232c1502004-03-12 00:14:09 +0000592
593 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200594 string->bLength = sizeof(wstrProduct);
wdenk232c1502004-03-12 00:14:09 +0000595 string->bDescriptorType = USB_DT_STRING;
596 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200597 usbtty_string_table[STR_PRODUCT]=string;
598
wdenk232c1502004-03-12 00:14:09 +0000599
600 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200601 string->bLength = sizeof(serial_number);
wdenk232c1502004-03-12 00:14:09 +0000602 string->bDescriptorType = USB_DT_STRING;
wdenk6629d2f2004-03-12 15:38:25 +0000603 str2wide (serial_number, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200604 usbtty_string_table[STR_SERIAL]=string;
605
wdenk232c1502004-03-12 00:14:09 +0000606
607 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200608 string->bLength = sizeof(wstrConfiguration);
wdenk232c1502004-03-12 00:14:09 +0000609 string->bDescriptorType = USB_DT_STRING;
610 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200611 usbtty_string_table[STR_CONFIG]=string;
wdenk232c1502004-03-12 00:14:09 +0000612
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200613
614 string = (struct usb_string_descriptor *) wstrDataInterface;
615 string->bLength = sizeof(wstrDataInterface);
wdenk232c1502004-03-12 00:14:09 +0000616 string->bDescriptorType = USB_DT_STRING;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200617 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
618 usbtty_string_table[STR_DATA_INTERFACE]=string;
619
620 string = (struct usb_string_descriptor *) wstrCtrlInterface;
621 string->bLength = sizeof(wstrCtrlInterface);
622 string->bDescriptorType = USB_DT_STRING;
623 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
624 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk232c1502004-03-12 00:14:09 +0000625
626 /* Now, initialize the string table for ep0 handling */
627 usb_strings = usbtty_string_table;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200628}
wdenk232c1502004-03-12 00:14:09 +0000629
Tom Rinib2fb47f2011-12-15 08:40:51 -0700630#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
631 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
632
wdenk232c1502004-03-12 00:14:09 +0000633static void usbtty_init_instances (void)
634{
635 int i;
636
637 /* initialize device instance */
638 memset (device_instance, 0, sizeof (struct usb_device_instance));
639 device_instance->device_state = STATE_INIT;
640 device_instance->device_descriptor = &device_descriptor;
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530641#if defined(CONFIG_USBD_HS)
642 device_instance->qualifier_descriptor = &qualifier_descriptor;
643#endif
wdenk232c1502004-03-12 00:14:09 +0000644 device_instance->event = usbtty_event_handler;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200645 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk232c1502004-03-12 00:14:09 +0000646 device_instance->bus = bus_instance;
647 device_instance->configurations = NUM_CONFIGS;
648 device_instance->configuration_instance_array = config_instance;
649
650 /* initialize bus instance */
651 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
652 bus_instance->device = device_instance;
653 bus_instance->endpoint_array = endpoint_instance;
654 bus_instance->max_endpoints = 1;
655 bus_instance->maxpacketsize = 64;
wdenk6629d2f2004-03-12 15:38:25 +0000656 bus_instance->serial_number_str = serial_number;
wdenk232c1502004-03-12 00:14:09 +0000657
658 /* configuration instance */
659 memset (config_instance, 0,
660 sizeof (struct usb_configuration_instance));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200661 config_instance->interfaces = interface_count;
662 config_instance->configuration_descriptor = configuration_descriptor;
wdenk232c1502004-03-12 00:14:09 +0000663 config_instance->interface_instance_array = interface_instance;
664
665 /* interface instance */
666 memset (interface_instance, 0,
667 sizeof (struct usb_interface_instance));
668 interface_instance->alternates = 1;
669 interface_instance->alternates_instance_array = alternate_instance;
670
671 /* alternates instance */
672 memset (alternate_instance, 0,
673 sizeof (struct usb_alternate_instance));
674 alternate_instance->interface_descriptor = interface_descriptors;
675 alternate_instance->endpoints = NUM_ENDPOINTS;
676 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
677
678 /* endpoint instances */
679 memset (&endpoint_instance[0], 0,
680 sizeof (struct usb_endpoint_instance));
681 endpoint_instance[0].endpoint_address = 0;
682 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
683 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
684 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
685 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
686 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
687
688 for (i = 1; i <= NUM_ENDPOINTS; i++) {
689 memset (&endpoint_instance[i], 0,
690 sizeof (struct usb_endpoint_instance));
691
692 endpoint_instance[i].endpoint_address =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200693 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
694
695 endpoint_instance[i].rcv_attributes =
696 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000697
Tom Rinib2fb47f2011-12-15 08:40:51 -0700698 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200699
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200700 endpoint_instance[i].tx_attributes =
701 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000702
Tom Rinib2fb47f2011-12-15 08:40:51 -0700703 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200704
wdenk232c1502004-03-12 00:14:09 +0000705 endpoint_instance[i].tx_attributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200706 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000707
708 urb_link_init (&endpoint_instance[i].rcv);
709 urb_link_init (&endpoint_instance[i].rdy);
710 urb_link_init (&endpoint_instance[i].tx);
711 urb_link_init (&endpoint_instance[i].done);
712
713 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
714 endpoint_instance[i].tx_urb =
715 usbd_alloc_urb (device_instance,
716 &endpoint_instance[i]);
717 else
718 endpoint_instance[i].rcv_urb =
719 usbd_alloc_urb (device_instance,
720 &endpoint_instance[i]);
721 }
722}
723
724static void usbtty_init_endpoints (void)
725{
726 int i;
727
728 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200729 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk232c1502004-03-12 00:14:09 +0000730 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
731 }
732}
733
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200734/* usbtty_init_terminal_type
Wolfgang Denk386eda02006-06-14 18:14:56 +0200735 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200736 * Do some late binding for our device type.
737 */
738static void usbtty_init_terminal_type(short type)
739{
740 switch(type){
Wolfgang Denk386eda02006-06-14 18:14:56 +0200741 /* CDC ACM */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200742 case 0:
743 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200744 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200745 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200746 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200747 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200748 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200749 &acm_configuration_descriptors[0].data_endpoints[1];
wdenk232c1502004-03-12 00:14:09 +0000750
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200751 /* Enumerate Device Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200752 device_descriptor.bDeviceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200753 COMMUNICATIONS_DEVICE_CLASS;
754 device_descriptor.idProduct =
755 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
756
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530757#if defined(CONFIG_USBD_HS)
758 qualifier_descriptor.bDeviceClass =
759 COMMUNICATIONS_DEVICE_CLASS;
760#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200761 /* Assign endpoint indices */
762 tx_endpoint = ACM_TX_ENDPOINT;
763 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200764
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200765 /* Configuration Descriptor */
766 configuration_descriptor =
767 (struct usb_configuration_descriptor*)
768 &acm_configuration_descriptors;
769
770 /* Interface count */
771 interface_count = NUM_ACM_INTERFACES;
772 break;
773
774 /* BULK IN/OUT & Default */
775 case 1:
776 default:
777 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200778 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200779 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200780 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200781 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200782 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200783 &gserial_configuration_descriptors[0].data_endpoints[2];
784
785 /* Enumerate Device Descriptor */
786 device_descriptor.bDeviceClass = 0xFF;
787 device_descriptor.idProduct =
788 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530789#if defined(CONFIG_USBD_HS)
790 qualifier_descriptor.bDeviceClass = 0xFF;
791#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200792 /* Assign endpoint indices */
793 tx_endpoint = GSERIAL_TX_ENDPOINT;
794 rx_endpoint = GSERIAL_RX_ENDPOINT;
795
796 /* Configuration Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200797 configuration_descriptor =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200798 (struct usb_configuration_descriptor*)
799 &gserial_configuration_descriptors;
800
801 /* Interface count */
802 interface_count = NUM_GSERIAL_INTERFACES;
803 break;
804 }
805}
806
807/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000808
809static struct urb *next_urb (struct usb_device_instance *device,
810 struct usb_endpoint_instance *endpoint)
811{
812 struct urb *current_urb = NULL;
813 int space;
814
815 /* If there's a queue, then we should add to the last urb */
816 if (!endpoint->tx_queue) {
817 current_urb = endpoint->tx_urb;
818 } else {
819 /* Last urb from tx chain */
820 current_urb =
821 p2surround (struct urb, link, endpoint->tx.prev);
822 }
823
824 /* Make sure this one has enough room */
825 space = current_urb->buffer_length - current_urb->actual_length;
826 if (space > 0) {
827 return current_urb;
828 } else { /* No space here */
829 /* First look at done list */
830 current_urb = first_urb_detached (&endpoint->done);
831 if (!current_urb) {
832 current_urb = usbd_alloc_urb (device, endpoint);
833 }
834
835 urb_append (&endpoint->tx, current_urb);
836 endpoint->tx_queue++;
837 }
838 return current_urb;
839}
840
841static int write_buffer (circbuf_t * buf)
842{
843 if (!usbtty_configured ()) {
844 return 0;
845 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200846
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200847 struct usb_endpoint_instance *endpoint =
848 &endpoint_instance[tx_endpoint];
849 struct urb *current_urb = NULL;
850
851 current_urb = next_urb (device_instance, endpoint);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200852 /* TX data still exists - send it now
853 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200854 if(endpoint->sent < current_urb->actual_length){
855 if(udc_endpoint_write (endpoint)){
856 /* Write pre-empted by RX */
857 return -1;
858 }
859 }
wdenk232c1502004-03-12 00:14:09 +0000860
861 if (buf->size) {
wdenk232c1502004-03-12 00:14:09 +0000862 char *dest;
863
864 int space_avail;
865 int popnum, popped;
866 int total = 0;
867
Wolfgang Denk386eda02006-06-14 18:14:56 +0200868 /* Break buffer into urb sized pieces,
869 * and link each to the endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200870 */
wdenk232c1502004-03-12 00:14:09 +0000871 while (buf->size > 0) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200872
wdenk232c1502004-03-12 00:14:09 +0000873 if (!current_urb) {
874 TTYERR ("current_urb is NULL, buf->size %d\n",
875 buf->size);
876 return total;
877 }
878
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200879 dest = (char*)current_urb->buffer +
wdenk232c1502004-03-12 00:14:09 +0000880 current_urb->actual_length;
881
882 space_avail =
883 current_urb->buffer_length -
884 current_urb->actual_length;
885 popnum = MIN (space_avail, buf->size);
886 if (popnum == 0)
887 break;
888
889 popped = buf_pop (buf, dest, popnum);
890 if (popped == 0)
891 break;
892 current_urb->actual_length += popped;
893 total += popped;
894
Wolfgang Denk386eda02006-06-14 18:14:56 +0200895 /* If endpoint->last == 0, then transfers have
896 * not started on this endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200897 */
wdenk232c1502004-03-12 00:14:09 +0000898 if (endpoint->last == 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200899 if(udc_endpoint_write (endpoint)){
900 /* Write pre-empted by RX */
901 return -1;
902 }
wdenk232c1502004-03-12 00:14:09 +0000903 }
904
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200905 }/* end while */
wdenk232c1502004-03-12 00:14:09 +0000906 return total;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200907 }
wdenk232c1502004-03-12 00:14:09 +0000908
909 return 0;
910}
911
912static int fill_buffer (circbuf_t * buf)
913{
914 struct usb_endpoint_instance *endpoint =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200915 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000916
917 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200918 unsigned int nb = 0;
wdenk232c1502004-03-12 00:14:09 +0000919 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200920 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk232c1502004-03-12 00:14:09 +0000921
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200922 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk232c1502004-03-12 00:14:09 +0000923
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200924 nb = endpoint->rcv_urb->actual_length;
925 buf_push (buf, src, nb);
926 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200927
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200928 }
wdenk232c1502004-03-12 00:14:09 +0000929 return nb;
930 }
wdenk232c1502004-03-12 00:14:09 +0000931 return 0;
932}
933
934static int usbtty_configured (void)
935{
936 return usbtty_configured_flag;
937}
938
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200939/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000940
941static void usbtty_event_handler (struct usb_device_instance *device,
942 usb_device_event_t event, int data)
943{
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530944#if defined(CONFIG_USBD_HS)
945 int i;
946#endif
wdenk232c1502004-03-12 00:14:09 +0000947 switch (event) {
948 case DEVICE_RESET:
949 case DEVICE_BUS_INACTIVE:
950 usbtty_configured_flag = 0;
951 break;
952 case DEVICE_CONFIGURED:
953 usbtty_configured_flag = 1;
954 break;
955
956 case DEVICE_ADDRESS_ASSIGNED:
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530957#if defined(CONFIG_USBD_HS)
958 /*
959 * is_usbd_high_speed routine needs to be defined by
960 * specific gadget driver
York Sun472d5462013-04-01 11:29:11 -0700961 * It returns true if device enumerates at High speed
962 * Retuns false otherwise
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530963 */
964 for (i = 0; i < NUM_ENDPOINTS; i++) {
965 if (((ep_descriptor_ptrs[i]->bmAttributes &
966 USB_ENDPOINT_XFERTYPE_MASK) ==
967 USB_ENDPOINT_XFER_BULK)
968 && is_usbd_high_speed()) {
969
970 ep_descriptor_ptrs[i]->wMaxPacketSize =
971 CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
972 }
973
974 endpoint_instance[i + 1].tx_packetSize =
975 ep_descriptor_ptrs[i]->wMaxPacketSize;
976 endpoint_instance[i + 1].rcv_packetSize =
977 ep_descriptor_ptrs[i]->wMaxPacketSize;
978 }
979#endif
wdenk232c1502004-03-12 00:14:09 +0000980 usbtty_init_endpoints ();
981
982 default:
983 break;
984 }
985}
986
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200987/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000988
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200989int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
990{
991 switch (request->bRequest){
992
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200993 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200994 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200995 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200996 break;
997 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denk386eda02006-06-14 18:14:56 +0200998 * per character */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200999 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001000 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001001 break;
1002 case ACM_GET_LINE_ENCODING : /* request DTE rate,
1003 * stop/parity bits */
1004 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
1005 urb->actual_length = sizeof(rs232_desc);
1006
1007 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001008 default:
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001009 return 1;
1010 }
1011 return 0;
1012}
1013
1014/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +00001015
1016/*
1017 * Since interrupt handling has not yet been implemented, we use this function
1018 * to handle polling. This is called by the tstc,getc,putc,puts routines to
1019 * update the USB state.
1020 */
1021void usbtty_poll (void)
1022{
1023 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001024 udc_irq();
wdenk232c1502004-03-12 00:14:09 +00001025
Wolfgang Denk386eda02006-06-14 18:14:56 +02001026 /* Write any output data to host buffer
1027 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001028 */
wdenk232c1502004-03-12 00:14:09 +00001029 if (usbtty_configured ()) {
1030 write_buffer (&usbtty_output);
1031 }
1032
1033 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001034 udc_irq();
Wolfgang Denk386eda02006-06-14 18:14:56 +02001035
1036 /* Check for new data from host..
1037 * (do this after checking interrupts to get latest data)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001038 */
wdenk232c1502004-03-12 00:14:09 +00001039 if (usbtty_configured ()) {
1040 fill_buffer (&usbtty_input);
1041 }
1042
1043 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001044 udc_irq();
1045
wdenk232c1502004-03-12 00:14:09 +00001046}