blob: ae3ac8070d3478f2e96461666236c8e3687f6b40 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk29e7f5a2004-03-12 00:14:09 +00002/*
3 * (C) Copyright 2003
4 * Gerry Hamel, geh@ti.com, Texas Instruments
Wolfgang Denke2601822006-06-14 18:14:56 +02005 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02006 * (C) Copyright 2006
7 * Bryan O'Donoghue, bodonoghue@codehermit.ie
wdenk29e7f5a2004-03-12 00:14:09 +00008 */
9
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010010#include <config.h>
wdenk29e7f5a2004-03-12 00:14:09 +000011#include <circbuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Simon Glass23fe4492019-11-14 12:57:22 -070013#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020014#include <stdio_dev.h>
Tom Rini73424522011-12-15 08:40:51 -070015#include <asm/unaligned.h>
wdenk29e7f5a2004-03-12 00:14:09 +000016#include "usbtty.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020017#include "usb_cdc_acm.h"
18#include "usbdescriptors.h"
wdenk29e7f5a2004-03-12 00:14:09 +000019
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010020#ifdef DEBUG
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020021#define TTYDBG(fmt,args...)\
22 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000023#else
24#define TTYDBG(fmt,args...) do{}while(0)
25#endif
26
Wolfgang Denk3f0137b2006-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)
wdenk29e7f5a2004-03-12 00:14:09 +000031#else
32#define TTYERR(fmt,args...) do{}while(0)
33#endif
34
35/*
Wolfgang Denk3f0137b2006-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
Tom Rini364d0022023-01-10 11:19:45 -050047#define CFG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
48#define CFG_USBD_CTRL_INTERFACE_STR "Control Interface"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020049
50/*
wdenk29e7f5a2004-03-12 00:14:09 +000051 * Buffers to hold input and output data
52 */
Shiraz Hashim96decf62012-12-17 14:19:37 +053053#define USBTTY_BUFFER_SIZE 2048
wdenk29e7f5a2004-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-VILLARD2a7a0312009-05-16 12:14:54 +020061static struct stdio_dev usbttydev;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020062static struct usb_device_instance device_instance[1];
63static struct usb_bus_instance bus_instance[1];
wdenk29e7f5a2004-03-12 00:14:09 +000064static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk3f0137b2006-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];
wdenk29e7f5a2004-03-12 00:14:09 +000069
70/*
wdenk29e7f5a2004-03-12 00:14:09 +000071 * Global flag
72 */
73int usbtty_configured_flag = 0;
74
wdenk29e7f5a2004-03-12 00:14:09 +000075/*
wdenk05822bf2004-03-12 15:38:25 +000076 * Serial number
77 */
78static char serial_number[16];
79
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020080
wdenk05822bf2004-03-12 15:38:25 +000081/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020082 * Descriptors, Strings, Local variables.
wdenk29e7f5a2004-03-12 00:14:09 +000083 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020084
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020085/* defined and used by gadget/ep0.c */
Wolfgang Denk3f0137b2006-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 */
wdenk29e7f5a2004-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)];
wdenk05822bf2004-03-12 15:38:25 +000098static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
Tom Rini364d0022023-01-10 11:19:45 -050099static u8 wstrConfiguration[2 + 2*(sizeof(CFG_USBD_CONFIGURATION_STR)-1)];
100static u8 wstrDataInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
101static u8 wstrCtrlInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000102
Wolfgang Denk3f0137b2006-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;
wdenk29e7f5a2004-03-12 00:14:09 +0000107static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200108 .bLength = sizeof(struct usb_device_descriptor),
109 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200110 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk3f0137b2006-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
wdenk29e7f5a2004-03-12 00:14:09 +0000120};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200121
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200122/*
123 * Static CDC ACM specific descriptors
124 */
125
126struct acm_config_desc {
127 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200128
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200129 /* Master Interface */
130 struct usb_interface_descriptor interface_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200131
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200132 struct usb_class_header_function_descriptor usb_class_header;
133 struct usb_class_call_management_descriptor usb_class_call_mgt;
134 struct usb_class_abstract_control_descriptor usb_class_acm;
135 struct usb_class_union_function_descriptor usb_class_union;
136 struct usb_endpoint_descriptor notification_endpoint;
137
138 /* Slave Interface */
139 struct usb_interface_descriptor data_class_interface;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500140 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200141} __attribute__((packed));
142
143static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
144 {
145 .configuration_desc ={
Wolfgang Denke2601822006-06-14 18:14:56 +0200146 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200147 sizeof(struct usb_configuration_descriptor),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200148 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200149 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200150 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200151 .bNumInterfaces = NUM_ACM_INTERFACES,
152 .bConfigurationValue = 1,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200153 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200154 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200155 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
156 .bMaxPower = USBTTY_MAXPOWER
157 },
158 /* Interface 1 */
159 .interface_desc = {
160 .bLength = sizeof(struct usb_interface_descriptor),
161 .bDescriptorType = USB_DT_INTERFACE,
162 .bInterfaceNumber = 0,
163 .bAlternateSetting = 0,
164 .bNumEndpoints = 0x01,
Wolfgang Denke2601822006-06-14 18:14:56 +0200165 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200166 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
167 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
168 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
169 .iInterface = STR_CTRL_INTERFACE,
170 },
171 .usb_class_header = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200172 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200173 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denke2601822006-06-14 18:14:56 +0200174 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200175 .bDescriptorSubtype = USB_ST_HEADER,
176 .bcdCDC = cpu_to_le16(110),
177 },
178 .usb_class_call_mgt = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200179 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200180 sizeof(struct usb_class_call_management_descriptor),
181 .bDescriptorType = CS_INTERFACE,
182 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200183 .bmCapabilities = 0x00,
184 .bDataInterface = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200185 },
186 .usb_class_acm = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200187 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200188 sizeof(struct usb_class_abstract_control_descriptor),
189 .bDescriptorType = CS_INTERFACE,
Wolfgang Denke2601822006-06-14 18:14:56 +0200190 .bDescriptorSubtype = USB_ST_ACMF,
191 .bmCapabilities = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200192 },
193 .usb_class_union = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200194 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200195 sizeof(struct usb_class_union_function_descriptor),
196 .bDescriptorType = CS_INTERFACE,
197 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200198 .bMasterInterface = 0x00,
199 .bSlaveInterface0 = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200200 },
201 .notification_endpoint = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200202 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200203 sizeof(struct usb_endpoint_descriptor),
204 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530205 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200206 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200207 .wMaxPacketSize
Tom Rini364d0022023-01-10 11:19:45 -0500208 = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200209 .bInterval = 0xFF,
210 },
211
212 /* Interface 2 */
213 .data_class_interface = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200214 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200215 sizeof(struct usb_interface_descriptor),
216 .bDescriptorType = USB_DT_INTERFACE,
217 .bInterfaceNumber = 0x01,
218 .bAlternateSetting = 0x00,
219 .bNumEndpoints = 0x02,
Wolfgang Denke2601822006-06-14 18:14:56 +0200220 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200221 COMMUNICATIONS_INTERFACE_CLASS_DATA,
222 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
223 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
224 .iInterface = STR_DATA_INTERFACE,
225 },
226 .data_endpoints = {
227 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200228 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200229 sizeof(struct usb_endpoint_descriptor),
230 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530231 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200232 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200233 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200234 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500235 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200236 .bInterval = 0xFF,
237 },
238 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200239 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200240 sizeof(struct usb_endpoint_descriptor),
241 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530242 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denke2601822006-06-14 18:14:56 +0200243 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200244 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200245 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500246 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200247 .bInterval = 0xFF,
248 },
249 },
250 },
Wolfgang Denke2601822006-06-14 18:14:56 +0200251};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200252
253static struct rs232_emu rs232_desc={
Wolfgang Denka1be4762008-05-20 16:00:29 +0200254 .dter = 115200,
255 .stop_bits = 0x00,
256 .parity = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200257 .data_bits = 0x08
wdenk29e7f5a2004-03-12 00:14:09 +0000258};
259
wdenk29e7f5a2004-03-12 00:14:09 +0000260
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200261/*
262 * Static Generic Serial specific data
263 */
264
265
266struct gserial_config_desc {
Wolfgang Denke2601822006-06-14 18:14:56 +0200267
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200268 struct usb_configuration_descriptor configuration_desc;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500269 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
270 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200271
272} __attribute__((packed));
273
Wolfgang Denke2601822006-06-14 18:14:56 +0200274static struct gserial_config_desc
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200275gserial_configuration_descriptors[NUM_CONFIGS] ={
276 {
277 .configuration_desc ={
278 .bLength = sizeof(struct usb_configuration_descriptor),
279 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200280 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200281 cpu_to_le16(sizeof(struct gserial_config_desc)),
282 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
283 .bConfigurationValue = 1,
284 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200285 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200286 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
287 .bMaxPower = USBTTY_MAXPOWER
288 },
289 .interface_desc = {
290 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200291 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200292 sizeof(struct usb_interface_descriptor),
293 .bDescriptorType = USB_DT_INTERFACE,
294 .bInterfaceNumber = 0,
295 .bAlternateSetting = 0,
296 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200297 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200298 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denke2601822006-06-14 18:14:56 +0200299 .bInterfaceSubClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200300 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200301 .bInterfaceProtocol =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200302 COMMUNICATIONS_NO_PROTOCOL,
303 .iInterface = STR_DATA_INTERFACE
304 },
Wolfgang Denka1be4762008-05-20 16:00:29 +0200305 },
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200306 .data_endpoints = {
307 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200308 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200309 sizeof(struct usb_endpoint_descriptor),
310 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530311 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200313 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500314 cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200315 .bInterval= 0xFF,
316 },
317 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200318 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200319 sizeof(struct usb_endpoint_descriptor),
320 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530321 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200322 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200323 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500324 cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200325 .bInterval = 0xFF,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200326 },
327 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200328 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200329 sizeof(struct usb_endpoint_descriptor),
330 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530331 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200332 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200333 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500334 cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200335 .bInterval = 0xFF,
336 },
337 },
338 },
339};
wdenk29e7f5a2004-03-12 00:14:09 +0000340
341/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200342 * Static Function Prototypes
wdenk29e7f5a2004-03-12 00:14:09 +0000343 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200344
wdenk29e7f5a2004-03-12 00:14:09 +0000345static void usbtty_init_strings (void);
346static void usbtty_init_instances (void);
347static void usbtty_init_endpoints (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200348static void usbtty_init_terminal_type(short type);
wdenk29e7f5a2004-03-12 00:14:09 +0000349static void usbtty_event_handler (struct usb_device_instance *device,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200350 usb_device_event_t event, int data);
Wolfgang Denke2601822006-06-14 18:14:56 +0200351static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200352 struct urb *urb);
wdenk29e7f5a2004-03-12 00:14:09 +0000353static int usbtty_configured (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000354static int write_buffer (circbuf_t * buf);
355static int fill_buffer (circbuf_t * buf);
356
357void usbtty_poll (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000358
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200359/* utility function for converting char* to wide string used by USB */
360static void str2wide (char *str, u16 * wide)
361{
362 int i;
363 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200364 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200365 wide[i] = (u16) str[i];
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200366 #elif defined(__BIG_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200367 wide[i] = ((u16)(str[i])<<8);
368 #else
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200369 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200370 #endif
371 }
372}
wdenk29e7f5a2004-03-12 00:14:09 +0000373
374/*
375 * Test whether a character is in the RX buffer
376 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200377
Simon Glass0d1e1f72014-07-23 06:54:59 -0600378int usbtty_tstc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000379{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200380 struct usb_endpoint_instance *endpoint =
381 &endpoint_instance[rx_endpoint];
382
383 /* If no input data exists, allow more RX to be accepted */
384 if(usbtty_input.size <= 0){
385 udc_unset_nak(endpoint->endpoint_address&0x03);
386 }
387
wdenk29e7f5a2004-03-12 00:14:09 +0000388 usbtty_poll ();
389 return (usbtty_input.size > 0);
390}
391
392/*
393 * Read a single byte from the usb client port. Returns 1 on success, 0
394 * otherwise. When the function is succesfull, the character read is
395 * written into its argument c.
396 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200397
Simon Glass0d1e1f72014-07-23 06:54:59 -0600398int usbtty_getc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000399{
400 char c;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200401 struct usb_endpoint_instance *endpoint =
402 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000403
404 while (usbtty_input.size <= 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200405 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk29e7f5a2004-03-12 00:14:09 +0000406 usbtty_poll ();
407 }
408
409 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200410 udc_set_nak(endpoint->endpoint_address&0x03);
411
wdenk29e7f5a2004-03-12 00:14:09 +0000412 return c;
413}
414
415/*
416 * Output a single byte to the usb client port.
417 */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600418void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk29e7f5a2004-03-12 00:14:09 +0000419{
Atin Malaviya11a9df92009-02-03 15:17:10 -0500420 if (!usbtty_configured ())
421 return;
422
wdenk29e7f5a2004-03-12 00:14:09 +0000423 /* If \n, also do \r */
424 if (c == '\n')
425 buf_push (&usbtty_output, "\r", 1);
426
Alison Wang23e06b52016-03-02 11:00:37 +0800427 buf_push(&usbtty_output, &c, 1);
428
wdenk29e7f5a2004-03-12 00:14:09 +0000429 /* Poll at end to handle new data... */
430 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
431 usbtty_poll ();
432 }
433}
434
wdenk29e7f5a2004-03-12 00:14:09 +0000435/* usbtty_puts() helper function for finding the next '\n' in a string */
436static int next_nl_pos (const char *s)
437{
438 int i;
439
440 for (i = 0; s[i] != '\0'; i++) {
441 if (s[i] == '\n')
442 return i;
443 }
444 return i;
445}
446
447/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200448 * Output a string to the usb client port - implementing flow control
wdenk29e7f5a2004-03-12 00:14:09 +0000449 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200450
wdenk29e7f5a2004-03-12 00:14:09 +0000451static void __usbtty_puts (const char *str, int len)
452{
453 int maxlen = usbtty_output.totalsize;
454 int space, n;
455
456 /* break str into chunks < buffer size, if needed */
457 while (len > 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200458 usbtty_poll ();
wdenk29e7f5a2004-03-12 00:14:09 +0000459
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200460 space = maxlen - usbtty_output.size;
wdenk29e7f5a2004-03-12 00:14:09 +0000461 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200462 if (space) {
wdenk29e7f5a2004-03-12 00:14:09 +0000463 write_buffer (&usbtty_output);
Wolfgang Denke2601822006-06-14 18:14:56 +0200464
Masahiro Yamadab62b39b2014-09-18 13:28:06 +0900465 n = min(space, min(len, maxlen));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200466 buf_push (&usbtty_output, str, n);
wdenk29e7f5a2004-03-12 00:14:09 +0000467
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200468 str += n;
Wolfgang Denke2601822006-06-14 18:14:56 +0200469 len -= n;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200470 }
wdenk29e7f5a2004-03-12 00:14:09 +0000471 }
472}
473
Simon Glass0d1e1f72014-07-23 06:54:59 -0600474void usbtty_puts(struct stdio_dev *dev, const char *str)
wdenk29e7f5a2004-03-12 00:14:09 +0000475{
476 int n;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500477 int len;
478
479 if (!usbtty_configured ())
480 return;
wdenk29e7f5a2004-03-12 00:14:09 +0000481
Atin Malaviya11a9df92009-02-03 15:17:10 -0500482 len = strlen (str);
wdenk29e7f5a2004-03-12 00:14:09 +0000483 /* add '\r' for each '\n' */
484 while (len > 0) {
485 n = next_nl_pos (str);
486
487 if (str[n] == '\n') {
Pali Rohár84567c32021-02-07 14:50:01 +0100488 __usbtty_puts(str, n);
489 __usbtty_puts("\r\n", 2);
wdenk29e7f5a2004-03-12 00:14:09 +0000490 str += (n + 1);
491 len -= (n + 1);
492 } else {
493 /* No \n found. All done. */
494 __usbtty_puts (str, n);
495 break;
496 }
497 }
498
499 /* Poll at end to handle new data... */
500 usbtty_poll ();
501}
502
503/*
504 * Initialize the usb client port.
505 *
506 */
507int drv_usbtty_init (void)
508{
509 int rc;
wdenk05822bf2004-03-12 15:38:25 +0000510 char * sn;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200511 char * tt;
wdenk05822bf2004-03-12 15:38:25 +0000512 int snlen;
wdenkc35ba4e2004-03-14 22:25:36 +0000513
Heinrich Schuchardta8d4d312017-10-12 23:37:37 +0200514 /* Get serial number */
Simon Glass64b723f2017-08-03 12:22:12 -0600515 sn = env_get("serial#");
516 if (!sn)
wdenk05822bf2004-03-12 15:38:25 +0000517 sn = "000000000000";
wdenk05822bf2004-03-12 15:38:25 +0000518 snlen = strlen(sn);
519 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denk509cd072008-07-14 15:06:35 +0200520 printf ("Warning: serial number %s is too long (%d > %lu)\n",
521 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk05822bf2004-03-12 15:38:25 +0000522 snlen = sizeof(serial_number) - 1;
523 }
524 memcpy (serial_number, sn, snlen);
525 serial_number[snlen] = '\0';
wdenk29e7f5a2004-03-12 00:14:09 +0000526
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200527 /* Decide on which type of UDC device to be.
528 */
Simon Glass64b723f2017-08-03 12:22:12 -0600529 tt = env_get("usbtty");
530 if (!tt)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200531 tt = "generic";
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200532 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denke2601822006-06-14 18:14:56 +0200533
wdenk29e7f5a2004-03-12 00:14:09 +0000534 /* prepare buffers... */
535 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
536 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
537
538 /* Now, set up USB controller and infrastructure */
539 udc_init (); /* Basic USB initialization */
540
541 usbtty_init_strings ();
542 usbtty_init_instances ();
543
Stefan Herbrechtsmeier49914b62011-10-17 17:22:49 +0200544 usbtty_init_endpoints ();
545
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200546 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk29e7f5a2004-03-12 00:14:09 +0000547 udc_connect (); /* Enable pullup for host detection */
548
wdenk29e7f5a2004-03-12 00:14:09 +0000549 /* Device initialization */
550 memset (&usbttydev, 0, sizeof (usbttydev));
551
552 strcpy (usbttydev.name, "usbtty");
553 usbttydev.ext = 0; /* No extensions */
554 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
555 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
556 usbttydev.getc = usbtty_getc; /* 'getc' function */
557 usbttydev.putc = usbtty_putc; /* 'putc' function */
558 usbttydev.puts = usbtty_puts; /* 'puts' function */
559
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200560 rc = stdio_register (&usbttydev);
wdenk29e7f5a2004-03-12 00:14:09 +0000561
562 return (rc == 0) ? 1 : rc;
563}
564
565static void usbtty_init_strings (void)
566{
567 struct usb_string_descriptor *string;
568
Wolfgang Denke2601822006-06-14 18:14:56 +0200569 usbtty_string_table[STR_LANG] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200570 (struct usb_string_descriptor*)wstrLang;
571
wdenk29e7f5a2004-03-12 00:14:09 +0000572 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200573 string->bLength = sizeof(wstrManufacturer);
wdenk29e7f5a2004-03-12 00:14:09 +0000574 string->bDescriptorType = USB_DT_STRING;
575 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200576 usbtty_string_table[STR_MANUFACTURER]=string;
577
wdenk29e7f5a2004-03-12 00:14:09 +0000578
579 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200580 string->bLength = sizeof(wstrProduct);
wdenk29e7f5a2004-03-12 00:14:09 +0000581 string->bDescriptorType = USB_DT_STRING;
582 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200583 usbtty_string_table[STR_PRODUCT]=string;
584
wdenk29e7f5a2004-03-12 00:14:09 +0000585
586 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200587 string->bLength = sizeof(serial_number);
wdenk29e7f5a2004-03-12 00:14:09 +0000588 string->bDescriptorType = USB_DT_STRING;
wdenk05822bf2004-03-12 15:38:25 +0000589 str2wide (serial_number, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200590 usbtty_string_table[STR_SERIAL]=string;
591
wdenk29e7f5a2004-03-12 00:14:09 +0000592
593 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200594 string->bLength = sizeof(wstrConfiguration);
wdenk29e7f5a2004-03-12 00:14:09 +0000595 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500596 str2wide (CFG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200597 usbtty_string_table[STR_CONFIG]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000598
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200599
600 string = (struct usb_string_descriptor *) wstrDataInterface;
601 string->bLength = sizeof(wstrDataInterface);
602 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500603 str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200604 usbtty_string_table[STR_DATA_INTERFACE]=string;
605
606 string = (struct usb_string_descriptor *) wstrCtrlInterface;
607 string->bLength = sizeof(wstrCtrlInterface);
wdenk29e7f5a2004-03-12 00:14:09 +0000608 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500609 str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200610 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000611
612 /* Now, initialize the string table for ep0 handling */
613 usb_strings = usbtty_string_table;
Wolfgang Denke2601822006-06-14 18:14:56 +0200614}
wdenk29e7f5a2004-03-12 00:14:09 +0000615
Tom Rini73424522011-12-15 08:40:51 -0700616#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
617 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
618
wdenk29e7f5a2004-03-12 00:14:09 +0000619static void usbtty_init_instances (void)
620{
621 int i;
622
623 /* initialize device instance */
624 memset (device_instance, 0, sizeof (struct usb_device_instance));
625 device_instance->device_state = STATE_INIT;
626 device_instance->device_descriptor = &device_descriptor;
627 device_instance->event = usbtty_event_handler;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200628 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk29e7f5a2004-03-12 00:14:09 +0000629 device_instance->bus = bus_instance;
630 device_instance->configurations = NUM_CONFIGS;
631 device_instance->configuration_instance_array = config_instance;
632
633 /* initialize bus instance */
634 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
635 bus_instance->device = device_instance;
636 bus_instance->endpoint_array = endpoint_instance;
637 bus_instance->max_endpoints = 1;
638 bus_instance->maxpacketsize = 64;
wdenk05822bf2004-03-12 15:38:25 +0000639 bus_instance->serial_number_str = serial_number;
wdenk29e7f5a2004-03-12 00:14:09 +0000640
641 /* configuration instance */
642 memset (config_instance, 0,
643 sizeof (struct usb_configuration_instance));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200644 config_instance->interfaces = interface_count;
645 config_instance->configuration_descriptor = configuration_descriptor;
wdenk29e7f5a2004-03-12 00:14:09 +0000646 config_instance->interface_instance_array = interface_instance;
647
648 /* interface instance */
649 memset (interface_instance, 0,
650 sizeof (struct usb_interface_instance));
651 interface_instance->alternates = 1;
652 interface_instance->alternates_instance_array = alternate_instance;
653
654 /* alternates instance */
655 memset (alternate_instance, 0,
656 sizeof (struct usb_alternate_instance));
657 alternate_instance->interface_descriptor = interface_descriptors;
658 alternate_instance->endpoints = NUM_ENDPOINTS;
659 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
660
661 /* endpoint instances */
662 memset (&endpoint_instance[0], 0,
663 sizeof (struct usb_endpoint_instance));
664 endpoint_instance[0].endpoint_address = 0;
665 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
666 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
667 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
668 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
669 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
670
671 for (i = 1; i <= NUM_ENDPOINTS; i++) {
672 memset (&endpoint_instance[i], 0,
673 sizeof (struct usb_endpoint_instance));
674
675 endpoint_instance[i].endpoint_address =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200676 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
wdenk29e7f5a2004-03-12 00:14:09 +0000677
wdenk29e7f5a2004-03-12 00:14:09 +0000678 endpoint_instance[i].rcv_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200679 ep_descriptor_ptrs[i - 1]->bmAttributes;
680
Tom Rini73424522011-12-15 08:40:51 -0700681 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denke2601822006-06-14 18:14:56 +0200682
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200683 endpoint_instance[i].tx_attributes =
684 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000685
Tom Rini73424522011-12-15 08:40:51 -0700686 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200687
wdenk29e7f5a2004-03-12 00:14:09 +0000688 endpoint_instance[i].tx_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200689 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000690
691 urb_link_init (&endpoint_instance[i].rcv);
692 urb_link_init (&endpoint_instance[i].rdy);
693 urb_link_init (&endpoint_instance[i].tx);
694 urb_link_init (&endpoint_instance[i].done);
695
696 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
697 endpoint_instance[i].tx_urb =
698 usbd_alloc_urb (device_instance,
699 &endpoint_instance[i]);
700 else
701 endpoint_instance[i].rcv_urb =
702 usbd_alloc_urb (device_instance,
703 &endpoint_instance[i]);
704 }
705}
706
707static void usbtty_init_endpoints (void)
708{
709 int i;
710
711 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200712 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk29e7f5a2004-03-12 00:14:09 +0000713 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
714 }
715}
716
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200717/* usbtty_init_terminal_type
Wolfgang Denke2601822006-06-14 18:14:56 +0200718 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200719 * Do some late binding for our device type.
720 */
721static void usbtty_init_terminal_type(short type)
722{
723 switch(type){
Wolfgang Denke2601822006-06-14 18:14:56 +0200724 /* CDC ACM */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200725 case 0:
726 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200727 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200728 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denke2601822006-06-14 18:14:56 +0200729 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200730 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200731 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200732 &acm_configuration_descriptors[0].data_endpoints[1];
733
734 /* Enumerate Device Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200735 device_descriptor.bDeviceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200736 COMMUNICATIONS_DEVICE_CLASS;
737 device_descriptor.idProduct =
738 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
739
740 /* Assign endpoint indices */
741 tx_endpoint = ACM_TX_ENDPOINT;
742 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denke2601822006-06-14 18:14:56 +0200743
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200744 /* Configuration Descriptor */
745 configuration_descriptor =
746 (struct usb_configuration_descriptor*)
747 &acm_configuration_descriptors;
748
749 /* Interface count */
750 interface_count = NUM_ACM_INTERFACES;
751 break;
752
753 /* BULK IN/OUT & Default */
754 case 1:
755 default:
756 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200757 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200758 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200759 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200760 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denke2601822006-06-14 18:14:56 +0200761 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200762 &gserial_configuration_descriptors[0].data_endpoints[2];
wdenk29e7f5a2004-03-12 00:14:09 +0000763
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200764 /* Enumerate Device Descriptor */
765 device_descriptor.bDeviceClass = 0xFF;
766 device_descriptor.idProduct =
767 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200768 /* Assign endpoint indices */
769 tx_endpoint = GSERIAL_TX_ENDPOINT;
770 rx_endpoint = GSERIAL_RX_ENDPOINT;
771
772 /* Configuration Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200773 configuration_descriptor =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200774 (struct usb_configuration_descriptor*)
775 &gserial_configuration_descriptors;
776
777 /* Interface count */
778 interface_count = NUM_GSERIAL_INTERFACES;
779 break;
780 }
781}
782
783/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000784
785static struct urb *next_urb (struct usb_device_instance *device,
786 struct usb_endpoint_instance *endpoint)
787{
788 struct urb *current_urb = NULL;
789 int space;
790
791 /* If there's a queue, then we should add to the last urb */
792 if (!endpoint->tx_queue) {
793 current_urb = endpoint->tx_urb;
794 } else {
795 /* Last urb from tx chain */
796 current_urb =
797 p2surround (struct urb, link, endpoint->tx.prev);
798 }
799
800 /* Make sure this one has enough room */
801 space = current_urb->buffer_length - current_urb->actual_length;
802 if (space > 0) {
803 return current_urb;
804 } else { /* No space here */
805 /* First look at done list */
806 current_urb = first_urb_detached (&endpoint->done);
807 if (!current_urb) {
808 current_urb = usbd_alloc_urb (device, endpoint);
809 }
810
811 urb_append (&endpoint->tx, current_urb);
812 endpoint->tx_queue++;
813 }
814 return current_urb;
815}
816
817static int write_buffer (circbuf_t * buf)
818{
819 if (!usbtty_configured ()) {
820 return 0;
821 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200822
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200823 struct usb_endpoint_instance *endpoint =
824 &endpoint_instance[tx_endpoint];
825 struct urb *current_urb = NULL;
wdenk29e7f5a2004-03-12 00:14:09 +0000826
Wolfgang Denke2601822006-06-14 18:14:56 +0200827 /* TX data still exists - send it now
828 */
Pali Rohár29825e92021-02-07 14:50:02 +0100829 if(endpoint->sent < endpoint->tx_urb->actual_length){
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200830 if(udc_endpoint_write (endpoint)){
831 /* Write pre-empted by RX */
832 return -1;
833 }
834 }
wdenk29e7f5a2004-03-12 00:14:09 +0000835
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200836 if (buf->size) {
wdenk29e7f5a2004-03-12 00:14:09 +0000837 char *dest;
838
839 int space_avail;
840 int popnum, popped;
841 int total = 0;
842
Wolfgang Denke2601822006-06-14 18:14:56 +0200843 /* Break buffer into urb sized pieces,
844 * and link each to the endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200845 */
wdenk29e7f5a2004-03-12 00:14:09 +0000846 while (buf->size > 0) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200847
Pali Rohár29825e92021-02-07 14:50:02 +0100848 current_urb = next_urb (device_instance, endpoint);
849
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200850 dest = (char*)current_urb->buffer +
wdenk29e7f5a2004-03-12 00:14:09 +0000851 current_urb->actual_length;
852
853 space_avail =
854 current_urb->buffer_length -
855 current_urb->actual_length;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900856 popnum = min(space_avail, (int)buf->size);
wdenk29e7f5a2004-03-12 00:14:09 +0000857 if (popnum == 0)
858 break;
859
860 popped = buf_pop (buf, dest, popnum);
861 if (popped == 0)
862 break;
863 current_urb->actual_length += popped;
864 total += popped;
865
Wolfgang Denke2601822006-06-14 18:14:56 +0200866 /* If endpoint->last == 0, then transfers have
867 * not started on this endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200868 */
wdenk29e7f5a2004-03-12 00:14:09 +0000869 if (endpoint->last == 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200870 if(udc_endpoint_write (endpoint)){
871 /* Write pre-empted by RX */
872 return -1;
873 }
wdenk29e7f5a2004-03-12 00:14:09 +0000874 }
875
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200876 }/* end while */
wdenk29e7f5a2004-03-12 00:14:09 +0000877 return total;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200878 }
wdenk29e7f5a2004-03-12 00:14:09 +0000879
880 return 0;
881}
882
883static int fill_buffer (circbuf_t * buf)
884{
885 struct usb_endpoint_instance *endpoint =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200886 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000887
888 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200889 unsigned int nb = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000890 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200891 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk29e7f5a2004-03-12 00:14:09 +0000892
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200893 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk29e7f5a2004-03-12 00:14:09 +0000894
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200895 nb = endpoint->rcv_urb->actual_length;
896 buf_push (buf, src, nb);
897 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200898
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200899 }
wdenk29e7f5a2004-03-12 00:14:09 +0000900 return nb;
901 }
wdenk29e7f5a2004-03-12 00:14:09 +0000902 return 0;
903}
904
905static int usbtty_configured (void)
906{
907 return usbtty_configured_flag;
908}
909
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200910/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000911
912static void usbtty_event_handler (struct usb_device_instance *device,
913 usb_device_event_t event, int data)
914{
915 switch (event) {
916 case DEVICE_RESET:
917 case DEVICE_BUS_INACTIVE:
918 usbtty_configured_flag = 0;
919 break;
920 case DEVICE_CONFIGURED:
921 usbtty_configured_flag = 1;
922 break;
923
924 case DEVICE_ADDRESS_ASSIGNED:
925 usbtty_init_endpoints ();
926
927 default:
928 break;
929 }
930}
931
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200932/******************************************************************************/
933
934int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
935{
936 switch (request->bRequest){
937
Wolfgang Denka1be4762008-05-20 16:00:29 +0200938 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200939 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200940 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200941 break;
942 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denke2601822006-06-14 18:14:56 +0200943 * per character */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200944 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200945 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200946 break;
947 case ACM_GET_LINE_ENCODING : /* request DTE rate,
948 * stop/parity bits */
949 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
950 urb->actual_length = sizeof(rs232_desc);
951
952 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200953 default:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200954 return 1;
955 }
956 return 0;
957}
wdenk29e7f5a2004-03-12 00:14:09 +0000958
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200959/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000960
961/*
962 * Since interrupt handling has not yet been implemented, we use this function
963 * to handle polling. This is called by the tstc,getc,putc,puts routines to
964 * update the USB state.
965 */
966void usbtty_poll (void)
967{
968 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200969 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000970
Wolfgang Denke2601822006-06-14 18:14:56 +0200971 /* Write any output data to host buffer
972 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200973 */
wdenk29e7f5a2004-03-12 00:14:09 +0000974 if (usbtty_configured ()) {
975 write_buffer (&usbtty_output);
976 }
977
978 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200979 udc_irq();
Wolfgang Denke2601822006-06-14 18:14:56 +0200980
981 /* Check for new data from host..
982 * (do this after checking interrupts to get latest data)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200983 */
wdenk29e7f5a2004-03-12 00:14:09 +0000984 if (usbtty_configured ()) {
985 fill_buffer (&usbtty_input);
986 }
987
988 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200989 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000990
wdenk29e7f5a2004-03-12 00:14:09 +0000991}