blob: d0465b844ed975e41336c46c91e9306dc9665f7b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk232c1502004-03-12 00:14:09 +00002/*
3 * (C) Copyright 2003
4 * Gerry Hamel, geh@ti.com, Texas Instruments
Wolfgang Denk386eda02006-06-14 18:14:56 +02005 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02006 * (C) Copyright 2006
7 * Bryan O'Donoghue, bodonoghue@codehermit.ie
wdenk232c1502004-03-12 00:14:09 +00008 */
9
10#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010011#include <config.h>
wdenk232c1502004-03-12 00:14:09 +000012#include <circbuf.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020013#include <stdio_dev.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070014#include <asm/unaligned.h>
wdenk232c1502004-03-12 00:14:09 +000015#include "usbtty.h"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020016#include "usb_cdc_acm.h"
17#include "usbdescriptors.h"
wdenk232c1502004-03-12 00:14:09 +000018
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010019#ifdef DEBUG
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020020#define TTYDBG(fmt,args...)\
21 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000022#else
23#define TTYDBG(fmt,args...) do{}while(0)
24#endif
25
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020026#if 1
27#define TTYERR(fmt,args...)\
28 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
29 __LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000030#else
31#define TTYERR(fmt,args...) do{}while(0)
32#endif
33
34/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020035 * Defines
36 */
37#define NUM_CONFIGS 1
38#define MAX_INTERFACES 2
39#define NUM_ENDPOINTS 3
40#define ACM_TX_ENDPOINT 3
41#define ACM_RX_ENDPOINT 2
42#define GSERIAL_TX_ENDPOINT 2
43#define GSERIAL_RX_ENDPOINT 1
44#define NUM_ACM_INTERFACES 2
45#define NUM_GSERIAL_INTERFACES 1
46#define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
47#define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
48
49/*
wdenk232c1502004-03-12 00:14:09 +000050 * Buffers to hold input and output data
51 */
Shiraz Hashimb2caefb2012-12-17 14:19:37 +053052#define USBTTY_BUFFER_SIZE 2048
wdenk232c1502004-03-12 00:14:09 +000053static circbuf_t usbtty_input;
54static circbuf_t usbtty_output;
55
56
57/*
58 * Instance variables
59 */
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020060static struct stdio_dev usbttydev;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020061static struct usb_device_instance device_instance[1];
62static struct usb_bus_instance bus_instance[1];
wdenk232c1502004-03-12 00:14:09 +000063static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020064static struct usb_interface_instance interface_instance[MAX_INTERFACES];
65static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
66/* one extra for control endpoint */
67static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk232c1502004-03-12 00:14:09 +000068
69/*
70 * Global flag
71 */
72int usbtty_configured_flag = 0;
73
wdenk232c1502004-03-12 00:14:09 +000074/*
wdenk6629d2f2004-03-12 15:38:25 +000075 * Serial number
76 */
77static char serial_number[16];
78
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020079
wdenk6629d2f2004-03-12 15:38:25 +000080/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020081 * Descriptors, Strings, Local variables.
wdenk232c1502004-03-12 00:14:09 +000082 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020083
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020084/* defined and used by gadget/ep0.c */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020085extern struct usb_string_descriptor **usb_strings;
86
87/* Indicies, References */
88static unsigned short rx_endpoint = 0;
89static unsigned short tx_endpoint = 0;
90static unsigned short interface_count = 0;
91static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
92
93/* USB Descriptor Strings */
wdenk232c1502004-03-12 00:14:09 +000094static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
95static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
96static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk6629d2f2004-03-12 15:38:25 +000097static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
wdenk232c1502004-03-12 00:14:09 +000098static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020099static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
100static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
wdenk232c1502004-03-12 00:14:09 +0000101
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200102/* Standard USB Data Structures */
103static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
104static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
105static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk232c1502004-03-12 00:14:09 +0000106static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200107 .bLength = sizeof(struct usb_device_descriptor),
108 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200109 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200110 .bDeviceSubClass = 0x00,
111 .bDeviceProtocol = 0x00,
112 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
113 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
114 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
115 .iManufacturer = STR_MANUFACTURER,
116 .iProduct = STR_PRODUCT,
117 .iSerialNumber = STR_SERIAL,
118 .bNumConfigurations = NUM_CONFIGS
wdenk232c1502004-03-12 00:14:09 +0000119};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200120
121
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530122#if defined(CONFIG_USBD_HS)
123static struct usb_qualifier_descriptor qualifier_descriptor = {
124 .bLength = sizeof(struct usb_qualifier_descriptor),
125 .bDescriptorType = USB_DT_QUAL,
126 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
127 .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS,
128 .bDeviceSubClass = 0x00,
129 .bDeviceProtocol = 0x00,
130 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
131 .bNumConfigurations = NUM_CONFIGS
132};
133#endif
134
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200135/*
136 * Static CDC ACM specific descriptors
137 */
138
139struct acm_config_desc {
140 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200141
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200142 /* Master Interface */
143 struct usb_interface_descriptor interface_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200144
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200145 struct usb_class_header_function_descriptor usb_class_header;
146 struct usb_class_call_management_descriptor usb_class_call_mgt;
147 struct usb_class_abstract_control_descriptor usb_class_acm;
148 struct usb_class_union_function_descriptor usb_class_union;
149 struct usb_endpoint_descriptor notification_endpoint;
150
151 /* Slave Interface */
152 struct usb_interface_descriptor data_class_interface;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500153 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200154} __attribute__((packed));
155
156static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
157 {
158 .configuration_desc ={
Wolfgang Denk386eda02006-06-14 18:14:56 +0200159 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200160 sizeof(struct usb_configuration_descriptor),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200161 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200162 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200163 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200164 .bNumInterfaces = NUM_ACM_INTERFACES,
165 .bConfigurationValue = 1,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200166 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200167 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200168 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
169 .bMaxPower = USBTTY_MAXPOWER
170 },
171 /* Interface 1 */
172 .interface_desc = {
173 .bLength = sizeof(struct usb_interface_descriptor),
174 .bDescriptorType = USB_DT_INTERFACE,
175 .bInterfaceNumber = 0,
176 .bAlternateSetting = 0,
177 .bNumEndpoints = 0x01,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200178 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200179 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
180 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
181 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
182 .iInterface = STR_CTRL_INTERFACE,
183 },
184 .usb_class_header = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200185 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200186 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denk386eda02006-06-14 18:14:56 +0200187 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200188 .bDescriptorSubtype = USB_ST_HEADER,
189 .bcdCDC = cpu_to_le16(110),
190 },
191 .usb_class_call_mgt = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200192 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200193 sizeof(struct usb_class_call_management_descriptor),
194 .bDescriptorType = CS_INTERFACE,
195 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200196 .bmCapabilities = 0x00,
197 .bDataInterface = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200198 },
199 .usb_class_acm = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200200 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200201 sizeof(struct usb_class_abstract_control_descriptor),
202 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200203 .bDescriptorSubtype = USB_ST_ACMF,
204 .bmCapabilities = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200205 },
206 .usb_class_union = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200207 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200208 sizeof(struct usb_class_union_function_descriptor),
209 .bDescriptorType = CS_INTERFACE,
210 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200211 .bMasterInterface = 0x00,
212 .bSlaveInterface0 = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200213 },
214 .notification_endpoint = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200215 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200216 sizeof(struct usb_endpoint_descriptor),
217 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530218 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200219 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200220 .wMaxPacketSize
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200221 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
222 .bInterval = 0xFF,
223 },
224
225 /* Interface 2 */
226 .data_class_interface = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200227 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200228 sizeof(struct usb_interface_descriptor),
229 .bDescriptorType = USB_DT_INTERFACE,
230 .bInterfaceNumber = 0x01,
231 .bAlternateSetting = 0x00,
232 .bNumEndpoints = 0x02,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200233 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200234 COMMUNICATIONS_INTERFACE_CLASS_DATA,
235 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
236 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
237 .iInterface = STR_DATA_INTERFACE,
238 },
239 .data_endpoints = {
240 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200241 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200242 sizeof(struct usb_endpoint_descriptor),
243 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530244 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200245 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200246 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200247 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200248 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
249 .bInterval = 0xFF,
250 },
251 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200252 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200253 sizeof(struct usb_endpoint_descriptor),
254 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530255 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200256 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200257 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200258 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200259 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
260 .bInterval = 0xFF,
261 },
262 },
263 },
Wolfgang Denk386eda02006-06-14 18:14:56 +0200264};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200265
266static struct rs232_emu rs232_desc={
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200267 .dter = 115200,
268 .stop_bits = 0x00,
269 .parity = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200270 .data_bits = 0x08
wdenk232c1502004-03-12 00:14:09 +0000271};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200272
273
274/*
275 * Static Generic Serial specific data
276 */
277
278
279struct gserial_config_desc {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200280
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200281 struct usb_configuration_descriptor configuration_desc;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500282 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
283 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200284
285} __attribute__((packed));
286
Wolfgang Denk386eda02006-06-14 18:14:56 +0200287static struct gserial_config_desc
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200288gserial_configuration_descriptors[NUM_CONFIGS] ={
289 {
290 .configuration_desc ={
291 .bLength = sizeof(struct usb_configuration_descriptor),
292 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200293 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200294 cpu_to_le16(sizeof(struct gserial_config_desc)),
295 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
296 .bConfigurationValue = 1,
297 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200298 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200299 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
300 .bMaxPower = USBTTY_MAXPOWER
301 },
302 .interface_desc = {
303 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200304 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200305 sizeof(struct usb_interface_descriptor),
306 .bDescriptorType = USB_DT_INTERFACE,
307 .bInterfaceNumber = 0,
308 .bAlternateSetting = 0,
309 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200310 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200311 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200312 .bInterfaceSubClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200313 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200314 .bInterfaceProtocol =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200315 COMMUNICATIONS_NO_PROTOCOL,
316 .iInterface = STR_DATA_INTERFACE
317 },
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200318 },
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200319 .data_endpoints = {
320 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200321 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200322 sizeof(struct usb_endpoint_descriptor),
323 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530324 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200325 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200326 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200327 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
328 .bInterval= 0xFF,
329 },
330 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200331 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200332 sizeof(struct usb_endpoint_descriptor),
333 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530334 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200335 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200336 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200337 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200338 .bInterval = 0xFF,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200339 },
340 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200341 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200342 sizeof(struct usb_endpoint_descriptor),
343 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530344 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200345 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200346 .wMaxPacketSize =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200347 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
348 .bInterval = 0xFF,
349 },
350 },
351 },
wdenk232c1502004-03-12 00:14:09 +0000352};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200353
354/*
355 * Static Function Prototypes
356 */
357
358static void usbtty_init_strings (void);
359static void usbtty_init_instances (void);
360static void usbtty_init_endpoints (void);
361static void usbtty_init_terminal_type(short type);
362static void usbtty_event_handler (struct usb_device_instance *device,
363 usb_device_event_t event, int data);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200364static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200365 struct urb *urb);
366static int usbtty_configured (void);
367static int write_buffer (circbuf_t * buf);
368static int fill_buffer (circbuf_t * buf);
369
370void usbtty_poll (void);
wdenk232c1502004-03-12 00:14:09 +0000371
372/* utility function for converting char* to wide string used by USB */
373static void str2wide (char *str, u16 * wide)
374{
375 int i;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200376 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti18135122007-06-06 10:08:14 +0200377 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200378 wide[i] = (u16) str[i];
Rodolfo Giometti18135122007-06-06 10:08:14 +0200379 #elif defined(__BIG_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200380 wide[i] = ((u16)(str[i])<<8);
381 #else
Rodolfo Giometti18135122007-06-06 10:08:14 +0200382 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200383 #endif
384 }
wdenk232c1502004-03-12 00:14:09 +0000385}
386
387/*
wdenk232c1502004-03-12 00:14:09 +0000388 * Test whether a character is in the RX buffer
389 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200390
Simon Glass709ea542014-07-23 06:54:59 -0600391int usbtty_tstc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000392{
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200393 struct usb_endpoint_instance *endpoint =
394 &endpoint_instance[rx_endpoint];
395
396 /* If no input data exists, allow more RX to be accepted */
397 if(usbtty_input.size <= 0){
398 udc_unset_nak(endpoint->endpoint_address&0x03);
399 }
400
wdenk232c1502004-03-12 00:14:09 +0000401 usbtty_poll ();
402 return (usbtty_input.size > 0);
403}
404
405/*
406 * Read a single byte from the usb client port. Returns 1 on success, 0
407 * otherwise. When the function is succesfull, the character read is
408 * written into its argument c.
409 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200410
Simon Glass709ea542014-07-23 06:54:59 -0600411int usbtty_getc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000412{
413 char c;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200414 struct usb_endpoint_instance *endpoint =
415 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000416
417 while (usbtty_input.size <= 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200418 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk232c1502004-03-12 00:14:09 +0000419 usbtty_poll ();
420 }
421
422 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200423 udc_set_nak(endpoint->endpoint_address&0x03);
424
wdenk232c1502004-03-12 00:14:09 +0000425 return c;
426}
427
428/*
429 * Output a single byte to the usb client port.
430 */
Simon Glass709ea542014-07-23 06:54:59 -0600431void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk232c1502004-03-12 00:14:09 +0000432{
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500433 if (!usbtty_configured ())
434 return;
435
wdenk232c1502004-03-12 00:14:09 +0000436 /* If \n, also do \r */
437 if (c == '\n')
438 buf_push (&usbtty_output, "\r", 1);
439
Alison Wang055457e2016-03-02 11:00:37 +0800440 buf_push(&usbtty_output, &c, 1);
441
wdenk232c1502004-03-12 00:14:09 +0000442 /* 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
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900478 n = min(space, min(len, maxlen));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200479 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') {
Alison Wang055457e2016-03-02 11:00:37 +0800501 __usbtty_puts("\r", 1);
502 __usbtty_puts(str, n + 1);
wdenk232c1502004-03-12 00:14:09 +0000503 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
Heinrich Schuchardtc4093362017-10-12 23:37:37 +0200527 /* Get serial number */
Simon Glass00caae62017-08-03 12:22:12 -0600528 sn = env_get("serial#");
529 if (!sn)
wdenk6629d2f2004-03-12 15:38:25 +0000530 sn = "000000000000";
wdenk6629d2f2004-03-12 15:38:25 +0000531 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 */
Simon Glass00caae62017-08-03 12:22:12 -0600542 tt = env_get("usbtty");
543 if (!tt)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200544 tt = "generic";
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200545 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denk386eda02006-06-14 18:14:56 +0200546
wdenk232c1502004-03-12 00:14:09 +0000547 /* prepare buffers... */
548 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
549 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
550
551 /* Now, set up USB controller and infrastructure */
552 udc_init (); /* Basic USB initialization */
553
554 usbtty_init_strings ();
555 usbtty_init_instances ();
556
Stefan Herbrechtsmeier241d9a62011-10-17 17:22:49 +0200557 usbtty_init_endpoints ();
558
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200559 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk232c1502004-03-12 00:14:09 +0000560 udc_connect (); /* Enable pullup for host detection */
561
wdenk232c1502004-03-12 00:14:09 +0000562 /* Device initialization */
563 memset (&usbttydev, 0, sizeof (usbttydev));
564
565 strcpy (usbttydev.name, "usbtty");
566 usbttydev.ext = 0; /* No extensions */
567 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
568 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
569 usbttydev.getc = usbtty_getc; /* 'getc' function */
570 usbttydev.putc = usbtty_putc; /* 'putc' function */
571 usbttydev.puts = usbtty_puts; /* 'puts' function */
572
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200573 rc = stdio_register (&usbttydev);
wdenk232c1502004-03-12 00:14:09 +0000574
575 return (rc == 0) ? 1 : rc;
576}
577
578static void usbtty_init_strings (void)
579{
580 struct usb_string_descriptor *string;
581
Wolfgang Denk386eda02006-06-14 18:14:56 +0200582 usbtty_string_table[STR_LANG] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200583 (struct usb_string_descriptor*)wstrLang;
584
wdenk232c1502004-03-12 00:14:09 +0000585 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200586 string->bLength = sizeof(wstrManufacturer);
wdenk232c1502004-03-12 00:14:09 +0000587 string->bDescriptorType = USB_DT_STRING;
588 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200589 usbtty_string_table[STR_MANUFACTURER]=string;
590
wdenk232c1502004-03-12 00:14:09 +0000591
592 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200593 string->bLength = sizeof(wstrProduct);
wdenk232c1502004-03-12 00:14:09 +0000594 string->bDescriptorType = USB_DT_STRING;
595 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200596 usbtty_string_table[STR_PRODUCT]=string;
597
wdenk232c1502004-03-12 00:14:09 +0000598
599 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200600 string->bLength = sizeof(serial_number);
wdenk232c1502004-03-12 00:14:09 +0000601 string->bDescriptorType = USB_DT_STRING;
wdenk6629d2f2004-03-12 15:38:25 +0000602 str2wide (serial_number, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200603 usbtty_string_table[STR_SERIAL]=string;
604
wdenk232c1502004-03-12 00:14:09 +0000605
606 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200607 string->bLength = sizeof(wstrConfiguration);
wdenk232c1502004-03-12 00:14:09 +0000608 string->bDescriptorType = USB_DT_STRING;
609 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200610 usbtty_string_table[STR_CONFIG]=string;
wdenk232c1502004-03-12 00:14:09 +0000611
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200612
613 string = (struct usb_string_descriptor *) wstrDataInterface;
614 string->bLength = sizeof(wstrDataInterface);
wdenk232c1502004-03-12 00:14:09 +0000615 string->bDescriptorType = USB_DT_STRING;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200616 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
617 usbtty_string_table[STR_DATA_INTERFACE]=string;
618
619 string = (struct usb_string_descriptor *) wstrCtrlInterface;
620 string->bLength = sizeof(wstrCtrlInterface);
621 string->bDescriptorType = USB_DT_STRING;
622 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
623 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk232c1502004-03-12 00:14:09 +0000624
625 /* Now, initialize the string table for ep0 handling */
626 usb_strings = usbtty_string_table;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200627}
wdenk232c1502004-03-12 00:14:09 +0000628
Tom Rinib2fb47f2011-12-15 08:40:51 -0700629#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
630 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
631
wdenk232c1502004-03-12 00:14:09 +0000632static void usbtty_init_instances (void)
633{
634 int i;
635
636 /* initialize device instance */
637 memset (device_instance, 0, sizeof (struct usb_device_instance));
638 device_instance->device_state = STATE_INIT;
639 device_instance->device_descriptor = &device_descriptor;
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530640#if defined(CONFIG_USBD_HS)
641 device_instance->qualifier_descriptor = &qualifier_descriptor;
642#endif
wdenk232c1502004-03-12 00:14:09 +0000643 device_instance->event = usbtty_event_handler;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200644 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk232c1502004-03-12 00:14:09 +0000645 device_instance->bus = bus_instance;
646 device_instance->configurations = NUM_CONFIGS;
647 device_instance->configuration_instance_array = config_instance;
648
649 /* initialize bus instance */
650 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
651 bus_instance->device = device_instance;
652 bus_instance->endpoint_array = endpoint_instance;
653 bus_instance->max_endpoints = 1;
654 bus_instance->maxpacketsize = 64;
wdenk6629d2f2004-03-12 15:38:25 +0000655 bus_instance->serial_number_str = serial_number;
wdenk232c1502004-03-12 00:14:09 +0000656
657 /* configuration instance */
658 memset (config_instance, 0,
659 sizeof (struct usb_configuration_instance));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200660 config_instance->interfaces = interface_count;
661 config_instance->configuration_descriptor = configuration_descriptor;
wdenk232c1502004-03-12 00:14:09 +0000662 config_instance->interface_instance_array = interface_instance;
663
664 /* interface instance */
665 memset (interface_instance, 0,
666 sizeof (struct usb_interface_instance));
667 interface_instance->alternates = 1;
668 interface_instance->alternates_instance_array = alternate_instance;
669
670 /* alternates instance */
671 memset (alternate_instance, 0,
672 sizeof (struct usb_alternate_instance));
673 alternate_instance->interface_descriptor = interface_descriptors;
674 alternate_instance->endpoints = NUM_ENDPOINTS;
675 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
676
677 /* endpoint instances */
678 memset (&endpoint_instance[0], 0,
679 sizeof (struct usb_endpoint_instance));
680 endpoint_instance[0].endpoint_address = 0;
681 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
682 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
683 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
684 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
685 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
686
687 for (i = 1; i <= NUM_ENDPOINTS; i++) {
688 memset (&endpoint_instance[i], 0,
689 sizeof (struct usb_endpoint_instance));
690
691 endpoint_instance[i].endpoint_address =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200692 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
693
694 endpoint_instance[i].rcv_attributes =
695 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000696
Tom Rinib2fb47f2011-12-15 08:40:51 -0700697 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200698
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200699 endpoint_instance[i].tx_attributes =
700 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000701
Tom Rinib2fb47f2011-12-15 08:40:51 -0700702 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200703
wdenk232c1502004-03-12 00:14:09 +0000704 endpoint_instance[i].tx_attributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200705 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000706
707 urb_link_init (&endpoint_instance[i].rcv);
708 urb_link_init (&endpoint_instance[i].rdy);
709 urb_link_init (&endpoint_instance[i].tx);
710 urb_link_init (&endpoint_instance[i].done);
711
712 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
713 endpoint_instance[i].tx_urb =
714 usbd_alloc_urb (device_instance,
715 &endpoint_instance[i]);
716 else
717 endpoint_instance[i].rcv_urb =
718 usbd_alloc_urb (device_instance,
719 &endpoint_instance[i]);
720 }
721}
722
723static void usbtty_init_endpoints (void)
724{
725 int i;
726
727 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200728 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk232c1502004-03-12 00:14:09 +0000729 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
730 }
731}
732
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200733/* usbtty_init_terminal_type
Wolfgang Denk386eda02006-06-14 18:14:56 +0200734 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200735 * Do some late binding for our device type.
736 */
737static void usbtty_init_terminal_type(short type)
738{
739 switch(type){
Wolfgang Denk386eda02006-06-14 18:14:56 +0200740 /* CDC ACM */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200741 case 0:
742 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200743 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200744 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200745 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200746 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200747 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200748 &acm_configuration_descriptors[0].data_endpoints[1];
wdenk232c1502004-03-12 00:14:09 +0000749
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200750 /* Enumerate Device Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200751 device_descriptor.bDeviceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200752 COMMUNICATIONS_DEVICE_CLASS;
753 device_descriptor.idProduct =
754 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
755
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530756#if defined(CONFIG_USBD_HS)
757 qualifier_descriptor.bDeviceClass =
758 COMMUNICATIONS_DEVICE_CLASS;
759#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200760 /* Assign endpoint indices */
761 tx_endpoint = ACM_TX_ENDPOINT;
762 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200763
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200764 /* Configuration Descriptor */
765 configuration_descriptor =
766 (struct usb_configuration_descriptor*)
767 &acm_configuration_descriptors;
768
769 /* Interface count */
770 interface_count = NUM_ACM_INTERFACES;
771 break;
772
773 /* BULK IN/OUT & Default */
774 case 1:
775 default:
776 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200777 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200778 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200779 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200780 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200781 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200782 &gserial_configuration_descriptors[0].data_endpoints[2];
783
784 /* Enumerate Device Descriptor */
785 device_descriptor.bDeviceClass = 0xFF;
786 device_descriptor.idProduct =
787 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Vipin KUMARf9da0f82012-03-26 15:38:06 +0530788#if defined(CONFIG_USBD_HS)
789 qualifier_descriptor.bDeviceClass = 0xFF;
790#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200791 /* Assign endpoint indices */
792 tx_endpoint = GSERIAL_TX_ENDPOINT;
793 rx_endpoint = GSERIAL_RX_ENDPOINT;
794
795 /* Configuration Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200796 configuration_descriptor =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200797 (struct usb_configuration_descriptor*)
798 &gserial_configuration_descriptors;
799
800 /* Interface count */
801 interface_count = NUM_GSERIAL_INTERFACES;
802 break;
803 }
804}
805
806/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000807
808static struct urb *next_urb (struct usb_device_instance *device,
809 struct usb_endpoint_instance *endpoint)
810{
811 struct urb *current_urb = NULL;
812 int space;
813
814 /* If there's a queue, then we should add to the last urb */
815 if (!endpoint->tx_queue) {
816 current_urb = endpoint->tx_urb;
817 } else {
818 /* Last urb from tx chain */
819 current_urb =
820 p2surround (struct urb, link, endpoint->tx.prev);
821 }
822
823 /* Make sure this one has enough room */
824 space = current_urb->buffer_length - current_urb->actual_length;
825 if (space > 0) {
826 return current_urb;
827 } else { /* No space here */
828 /* First look at done list */
829 current_urb = first_urb_detached (&endpoint->done);
830 if (!current_urb) {
831 current_urb = usbd_alloc_urb (device, endpoint);
832 }
833
834 urb_append (&endpoint->tx, current_urb);
835 endpoint->tx_queue++;
836 }
837 return current_urb;
838}
839
840static int write_buffer (circbuf_t * buf)
841{
842 if (!usbtty_configured ()) {
843 return 0;
844 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200845
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200846 struct usb_endpoint_instance *endpoint =
847 &endpoint_instance[tx_endpoint];
848 struct urb *current_urb = NULL;
849
850 current_urb = next_urb (device_instance, endpoint);
xypron.glpk@gmx.de73be5b32017-04-15 15:05:46 +0200851
852 if (!current_urb) {
853 TTYERR ("current_urb is NULL, buf->size %d\n",
854 buf->size);
855 return 0;
856 }
857
Wolfgang Denk386eda02006-06-14 18:14:56 +0200858 /* TX data still exists - send it now
859 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200860 if(endpoint->sent < current_urb->actual_length){
861 if(udc_endpoint_write (endpoint)){
862 /* Write pre-empted by RX */
863 return -1;
864 }
865 }
wdenk232c1502004-03-12 00:14:09 +0000866
867 if (buf->size) {
wdenk232c1502004-03-12 00:14:09 +0000868 char *dest;
869
870 int space_avail;
871 int popnum, popped;
872 int total = 0;
873
Wolfgang Denk386eda02006-06-14 18:14:56 +0200874 /* Break buffer into urb sized pieces,
875 * and link each to the endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200876 */
wdenk232c1502004-03-12 00:14:09 +0000877 while (buf->size > 0) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200878
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;
Masahiro Yamadab4141192014-11-07 03:03:31 +0900885 popnum = min(space_avail, (int)buf->size);
wdenk232c1502004-03-12 00:14:09 +0000886 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}