blob: ecb6ba853df78f0737b405275d70a4e57f1d6b58 [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>
Simon Glass7b51b572019-08-01 09:46:52 -060013#include <env.h>
Simon Glass2310c8e2019-11-14 12:57:22 -070014#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020015#include <stdio_dev.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070016#include <asm/unaligned.h>
wdenk232c1502004-03-12 00:14:09 +000017#include "usbtty.h"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020018#include "usb_cdc_acm.h"
19#include "usbdescriptors.h"
wdenk232c1502004-03-12 00:14:09 +000020
Jean-Christophe PLAGNIOL-VILLARDdedacc12008-12-07 09:45:35 +010021#ifdef DEBUG
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020022#define TTYDBG(fmt,args...)\
23 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000024#else
25#define TTYDBG(fmt,args...) do{}while(0)
26#endif
27
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020028#if 1
29#define TTYERR(fmt,args...)\
30 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
31 __LINE__,##args)
wdenk232c1502004-03-12 00:14:09 +000032#else
33#define TTYERR(fmt,args...) do{}while(0)
34#endif
35
36/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020037 * Defines
38 */
39#define NUM_CONFIGS 1
40#define MAX_INTERFACES 2
41#define NUM_ENDPOINTS 3
42#define ACM_TX_ENDPOINT 3
43#define ACM_RX_ENDPOINT 2
44#define GSERIAL_TX_ENDPOINT 2
45#define GSERIAL_RX_ENDPOINT 1
46#define NUM_ACM_INTERFACES 2
47#define NUM_GSERIAL_INTERFACES 1
Tom Rini6e7df1d2023-01-10 11:19:45 -050048#define CFG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
49#define CFG_USBD_CTRL_INTERFACE_STR "Control Interface"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020050
51/*
wdenk232c1502004-03-12 00:14:09 +000052 * Buffers to hold input and output data
53 */
Shiraz Hashimb2caefb2012-12-17 14:19:37 +053054#define USBTTY_BUFFER_SIZE 2048
wdenk232c1502004-03-12 00:14:09 +000055static circbuf_t usbtty_input;
56static circbuf_t usbtty_output;
57
58
59/*
60 * Instance variables
61 */
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020062static struct stdio_dev usbttydev;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020063static struct usb_device_instance device_instance[1];
64static struct usb_bus_instance bus_instance[1];
wdenk232c1502004-03-12 00:14:09 +000065static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020066static struct usb_interface_instance interface_instance[MAX_INTERFACES];
67static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
68/* one extra for control endpoint */
69static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk232c1502004-03-12 00:14:09 +000070
71/*
72 * Global flag
73 */
74int usbtty_configured_flag = 0;
75
wdenk232c1502004-03-12 00:14:09 +000076/*
wdenk6629d2f2004-03-12 15:38:25 +000077 * Serial number
78 */
79static char serial_number[16];
80
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020081
wdenk6629d2f2004-03-12 15:38:25 +000082/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020083 * Descriptors, Strings, Local variables.
wdenk232c1502004-03-12 00:14:09 +000084 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020085
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020086/* defined and used by gadget/ep0.c */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020087extern struct usb_string_descriptor **usb_strings;
88
89/* Indicies, References */
90static unsigned short rx_endpoint = 0;
91static unsigned short tx_endpoint = 0;
92static unsigned short interface_count = 0;
93static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
94
95/* USB Descriptor Strings */
wdenk232c1502004-03-12 00:14:09 +000096static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
97static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
98static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk6629d2f2004-03-12 15:38:25 +000099static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
Tom Rini6e7df1d2023-01-10 11:19:45 -0500100static u8 wstrConfiguration[2 + 2*(sizeof(CFG_USBD_CONFIGURATION_STR)-1)];
101static u8 wstrDataInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
102static u8 wstrCtrlInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
wdenk232c1502004-03-12 00:14:09 +0000103
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200104/* Standard USB Data Structures */
105static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
106static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
107static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk232c1502004-03-12 00:14:09 +0000108static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200109 .bLength = sizeof(struct usb_device_descriptor),
110 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200111 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200112 .bDeviceSubClass = 0x00,
113 .bDeviceProtocol = 0x00,
114 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
115 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
116 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
117 .iManufacturer = STR_MANUFACTURER,
118 .iProduct = STR_PRODUCT,
119 .iSerialNumber = STR_SERIAL,
120 .bNumConfigurations = NUM_CONFIGS
wdenk232c1502004-03-12 00:14:09 +0000121};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200122
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200123/*
124 * Static CDC ACM specific descriptors
125 */
126
127struct acm_config_desc {
128 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200129
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200130 /* Master Interface */
131 struct usb_interface_descriptor interface_desc;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200132
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200133 struct usb_class_header_function_descriptor usb_class_header;
134 struct usb_class_call_management_descriptor usb_class_call_mgt;
135 struct usb_class_abstract_control_descriptor usb_class_acm;
136 struct usb_class_union_function_descriptor usb_class_union;
137 struct usb_endpoint_descriptor notification_endpoint;
138
139 /* Slave Interface */
140 struct usb_interface_descriptor data_class_interface;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500141 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200142} __attribute__((packed));
143
144static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
145 {
146 .configuration_desc ={
Wolfgang Denk386eda02006-06-14 18:14:56 +0200147 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200148 sizeof(struct usb_configuration_descriptor),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200149 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200150 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200151 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200152 .bNumInterfaces = NUM_ACM_INTERFACES,
153 .bConfigurationValue = 1,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200154 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200155 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200156 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
157 .bMaxPower = USBTTY_MAXPOWER
158 },
159 /* Interface 1 */
160 .interface_desc = {
161 .bLength = sizeof(struct usb_interface_descriptor),
162 .bDescriptorType = USB_DT_INTERFACE,
163 .bInterfaceNumber = 0,
164 .bAlternateSetting = 0,
165 .bNumEndpoints = 0x01,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200166 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200167 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
168 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
169 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
170 .iInterface = STR_CTRL_INTERFACE,
171 },
172 .usb_class_header = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200173 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200174 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denk386eda02006-06-14 18:14:56 +0200175 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200176 .bDescriptorSubtype = USB_ST_HEADER,
177 .bcdCDC = cpu_to_le16(110),
178 },
179 .usb_class_call_mgt = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200180 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200181 sizeof(struct usb_class_call_management_descriptor),
182 .bDescriptorType = CS_INTERFACE,
183 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200184 .bmCapabilities = 0x00,
185 .bDataInterface = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200186 },
187 .usb_class_acm = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200188 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200189 sizeof(struct usb_class_abstract_control_descriptor),
190 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200191 .bDescriptorSubtype = USB_ST_ACMF,
192 .bmCapabilities = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200193 },
194 .usb_class_union = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200195 .bFunctionLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200196 sizeof(struct usb_class_union_function_descriptor),
197 .bDescriptorType = CS_INTERFACE,
198 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200199 .bMasterInterface = 0x00,
200 .bSlaveInterface0 = 0x01,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200201 },
202 .notification_endpoint = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200203 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200204 sizeof(struct usb_endpoint_descriptor),
205 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530206 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200207 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200208 .wMaxPacketSize
Tom Rini6e7df1d2023-01-10 11:19:45 -0500209 = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200210 .bInterval = 0xFF,
211 },
212
213 /* Interface 2 */
214 .data_class_interface = {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200215 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200216 sizeof(struct usb_interface_descriptor),
217 .bDescriptorType = USB_DT_INTERFACE,
218 .bInterfaceNumber = 0x01,
219 .bAlternateSetting = 0x00,
220 .bNumEndpoints = 0x02,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200221 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200222 COMMUNICATIONS_INTERFACE_CLASS_DATA,
223 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
224 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
225 .iInterface = STR_DATA_INTERFACE,
226 },
227 .data_endpoints = {
228 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200229 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200230 sizeof(struct usb_endpoint_descriptor),
231 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530232 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200233 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200234 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200235 .wMaxPacketSize =
Tom Rini6e7df1d2023-01-10 11:19:45 -0500236 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200237 .bInterval = 0xFF,
238 },
239 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200240 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200241 sizeof(struct usb_endpoint_descriptor),
242 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530243 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200244 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200245 USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200246 .wMaxPacketSize =
Tom Rini6e7df1d2023-01-10 11:19:45 -0500247 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200248 .bInterval = 0xFF,
249 },
250 },
251 },
Wolfgang Denk386eda02006-06-14 18:14:56 +0200252};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200253
254static struct rs232_emu rs232_desc={
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200255 .dter = 115200,
256 .stop_bits = 0x00,
257 .parity = 0x00,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200258 .data_bits = 0x08
wdenk232c1502004-03-12 00:14:09 +0000259};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200260
261
262/*
263 * Static Generic Serial specific data
264 */
265
266
267struct gserial_config_desc {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200268
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200269 struct usb_configuration_descriptor configuration_desc;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500270 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
271 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200272
273} __attribute__((packed));
274
Wolfgang Denk386eda02006-06-14 18:14:56 +0200275static struct gserial_config_desc
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200276gserial_configuration_descriptors[NUM_CONFIGS] ={
277 {
278 .configuration_desc ={
279 .bLength = sizeof(struct usb_configuration_descriptor),
280 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200281 .wTotalLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200282 cpu_to_le16(sizeof(struct gserial_config_desc)),
283 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
284 .bConfigurationValue = 1,
285 .iConfiguration = STR_CONFIG,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200286 .bmAttributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200287 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
288 .bMaxPower = USBTTY_MAXPOWER
289 },
290 .interface_desc = {
291 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200292 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200293 sizeof(struct usb_interface_descriptor),
294 .bDescriptorType = USB_DT_INTERFACE,
295 .bInterfaceNumber = 0,
296 .bAlternateSetting = 0,
297 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200298 .bInterfaceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200299 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200300 .bInterfaceSubClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200301 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200302 .bInterfaceProtocol =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200303 COMMUNICATIONS_NO_PROTOCOL,
304 .iInterface = STR_DATA_INTERFACE
305 },
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200306 },
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200307 .data_endpoints = {
308 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200309 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200310 sizeof(struct usb_endpoint_descriptor),
311 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530312 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200314 .wMaxPacketSize =
Tom Rini6e7df1d2023-01-10 11:19:45 -0500315 cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200316 .bInterval= 0xFF,
317 },
318 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200319 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200320 sizeof(struct usb_endpoint_descriptor),
321 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530322 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200324 .wMaxPacketSize =
Tom Rini6e7df1d2023-01-10 11:19:45 -0500325 cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200326 .bInterval = 0xFF,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200327 },
328 {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200329 .bLength =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200330 sizeof(struct usb_endpoint_descriptor),
331 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal9e78dae2009-02-23 21:35:11 +0530332 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200333 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200334 .wMaxPacketSize =
Tom Rini6e7df1d2023-01-10 11:19:45 -0500335 cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200336 .bInterval = 0xFF,
337 },
338 },
339 },
wdenk232c1502004-03-12 00:14:09 +0000340};
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200341
342/*
343 * Static Function Prototypes
344 */
345
346static void usbtty_init_strings (void);
347static void usbtty_init_instances (void);
348static void usbtty_init_endpoints (void);
349static void usbtty_init_terminal_type(short type);
350static void usbtty_event_handler (struct usb_device_instance *device,
351 usb_device_event_t event, int data);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200352static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200353 struct urb *urb);
354static int usbtty_configured (void);
355static int write_buffer (circbuf_t * buf);
356static int fill_buffer (circbuf_t * buf);
357
358void usbtty_poll (void);
wdenk232c1502004-03-12 00:14:09 +0000359
360/* utility function for converting char* to wide string used by USB */
361static void str2wide (char *str, u16 * wide)
362{
363 int i;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200364 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti18135122007-06-06 10:08:14 +0200365 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200366 wide[i] = (u16) str[i];
Rodolfo Giometti18135122007-06-06 10:08:14 +0200367 #elif defined(__BIG_ENDIAN)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200368 wide[i] = ((u16)(str[i])<<8);
369 #else
Rodolfo Giometti18135122007-06-06 10:08:14 +0200370 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200371 #endif
372 }
wdenk232c1502004-03-12 00:14:09 +0000373}
374
375/*
wdenk232c1502004-03-12 00:14:09 +0000376 * Test whether a character is in the RX buffer
377 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200378
Simon Glass709ea542014-07-23 06:54:59 -0600379int usbtty_tstc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000380{
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200381 struct usb_endpoint_instance *endpoint =
382 &endpoint_instance[rx_endpoint];
383
384 /* If no input data exists, allow more RX to be accepted */
385 if(usbtty_input.size <= 0){
386 udc_unset_nak(endpoint->endpoint_address&0x03);
387 }
388
wdenk232c1502004-03-12 00:14:09 +0000389 usbtty_poll ();
390 return (usbtty_input.size > 0);
391}
392
393/*
394 * Read a single byte from the usb client port. Returns 1 on success, 0
395 * otherwise. When the function is succesfull, the character read is
396 * written into its argument c.
397 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200398
Simon Glass709ea542014-07-23 06:54:59 -0600399int usbtty_getc(struct stdio_dev *dev)
wdenk232c1502004-03-12 00:14:09 +0000400{
401 char c;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200402 struct usb_endpoint_instance *endpoint =
403 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000404
405 while (usbtty_input.size <= 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200406 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk232c1502004-03-12 00:14:09 +0000407 usbtty_poll ();
408 }
409
410 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200411 udc_set_nak(endpoint->endpoint_address&0x03);
412
wdenk232c1502004-03-12 00:14:09 +0000413 return c;
414}
415
416/*
417 * Output a single byte to the usb client port.
418 */
Simon Glass709ea542014-07-23 06:54:59 -0600419void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk232c1502004-03-12 00:14:09 +0000420{
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500421 if (!usbtty_configured ())
422 return;
423
wdenk232c1502004-03-12 00:14:09 +0000424 /* If \n, also do \r */
425 if (c == '\n')
426 buf_push (&usbtty_output, "\r", 1);
427
Alison Wang055457e2016-03-02 11:00:37 +0800428 buf_push(&usbtty_output, &c, 1);
429
wdenk232c1502004-03-12 00:14:09 +0000430 /* Poll at end to handle new data... */
431 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
432 usbtty_poll ();
433 }
434}
435
wdenk232c1502004-03-12 00:14:09 +0000436/* usbtty_puts() helper function for finding the next '\n' in a string */
437static int next_nl_pos (const char *s)
438{
439 int i;
440
441 for (i = 0; s[i] != '\0'; i++) {
442 if (s[i] == '\n')
443 return i;
444 }
445 return i;
446}
447
448/*
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200449 * Output a string to the usb client port - implementing flow control
wdenk232c1502004-03-12 00:14:09 +0000450 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200451
wdenk232c1502004-03-12 00:14:09 +0000452static void __usbtty_puts (const char *str, int len)
453{
454 int maxlen = usbtty_output.totalsize;
455 int space, n;
456
457 /* break str into chunks < buffer size, if needed */
458 while (len > 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200459 usbtty_poll ();
460
wdenk232c1502004-03-12 00:14:09 +0000461 space = maxlen - usbtty_output.size;
wdenk232c1502004-03-12 00:14:09 +0000462 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200463 if (space) {
wdenk232c1502004-03-12 00:14:09 +0000464 write_buffer (&usbtty_output);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200465
Masahiro Yamadac79cba32014-09-18 13:28:06 +0900466 n = min(space, min(len, maxlen));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200467 buf_push (&usbtty_output, str, n);
468
469 str += n;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200470 len -= n;
wdenk232c1502004-03-12 00:14:09 +0000471 }
wdenk232c1502004-03-12 00:14:09 +0000472 }
473}
474
Simon Glass709ea542014-07-23 06:54:59 -0600475void usbtty_puts(struct stdio_dev *dev, const char *str)
wdenk232c1502004-03-12 00:14:09 +0000476{
477 int n;
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500478 int len;
wdenk232c1502004-03-12 00:14:09 +0000479
Atin Malaviyaf3c0de62009-02-03 15:17:10 -0500480 if (!usbtty_configured ())
481 return;
482
483 len = strlen (str);
wdenk232c1502004-03-12 00:14:09 +0000484 /* add '\r' for each '\n' */
485 while (len > 0) {
486 n = next_nl_pos (str);
487
488 if (str[n] == '\n') {
Pali Rohárd3fb8fe2021-02-07 14:50:01 +0100489 __usbtty_puts(str, n);
490 __usbtty_puts("\r\n", 2);
wdenk232c1502004-03-12 00:14:09 +0000491 str += (n + 1);
492 len -= (n + 1);
493 } else {
494 /* No \n found. All done. */
495 __usbtty_puts (str, n);
496 break;
497 }
498 }
499
500 /* Poll at end to handle new data... */
501 usbtty_poll ();
502}
503
504/*
505 * Initialize the usb client port.
506 *
507 */
508int drv_usbtty_init (void)
509{
510 int rc;
wdenk6629d2f2004-03-12 15:38:25 +0000511 char * sn;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200512 char * tt;
wdenk6629d2f2004-03-12 15:38:25 +0000513 int snlen;
wdenk42dfe7a2004-03-14 22:25:36 +0000514
Heinrich Schuchardtc4093362017-10-12 23:37:37 +0200515 /* Get serial number */
Simon Glass00caae62017-08-03 12:22:12 -0600516 sn = env_get("serial#");
517 if (!sn)
wdenk6629d2f2004-03-12 15:38:25 +0000518 sn = "000000000000";
wdenk6629d2f2004-03-12 15:38:25 +0000519 snlen = strlen(sn);
520 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denkb64f1902008-07-14 15:06:35 +0200521 printf ("Warning: serial number %s is too long (%d > %lu)\n",
522 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk6629d2f2004-03-12 15:38:25 +0000523 snlen = sizeof(serial_number) - 1;
524 }
525 memcpy (serial_number, sn, snlen);
526 serial_number[snlen] = '\0';
wdenk232c1502004-03-12 00:14:09 +0000527
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200528 /* Decide on which type of UDC device to be.
529 */
Simon Glass00caae62017-08-03 12:22:12 -0600530 tt = env_get("usbtty");
531 if (!tt)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200532 tt = "generic";
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200533 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denk386eda02006-06-14 18:14:56 +0200534
wdenk232c1502004-03-12 00:14:09 +0000535 /* prepare buffers... */
536 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
537 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
538
539 /* Now, set up USB controller and infrastructure */
540 udc_init (); /* Basic USB initialization */
541
542 usbtty_init_strings ();
543 usbtty_init_instances ();
544
Stefan Herbrechtsmeier241d9a62011-10-17 17:22:49 +0200545 usbtty_init_endpoints ();
546
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200547 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk232c1502004-03-12 00:14:09 +0000548 udc_connect (); /* Enable pullup for host detection */
549
wdenk232c1502004-03-12 00:14:09 +0000550 /* Device initialization */
551 memset (&usbttydev, 0, sizeof (usbttydev));
552
553 strcpy (usbttydev.name, "usbtty");
554 usbttydev.ext = 0; /* No extensions */
555 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
556 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
557 usbttydev.getc = usbtty_getc; /* 'getc' function */
558 usbttydev.putc = usbtty_putc; /* 'putc' function */
559 usbttydev.puts = usbtty_puts; /* 'puts' function */
560
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200561 rc = stdio_register (&usbttydev);
wdenk232c1502004-03-12 00:14:09 +0000562
563 return (rc == 0) ? 1 : rc;
564}
565
566static void usbtty_init_strings (void)
567{
568 struct usb_string_descriptor *string;
569
Wolfgang Denk386eda02006-06-14 18:14:56 +0200570 usbtty_string_table[STR_LANG] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200571 (struct usb_string_descriptor*)wstrLang;
572
wdenk232c1502004-03-12 00:14:09 +0000573 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200574 string->bLength = sizeof(wstrManufacturer);
wdenk232c1502004-03-12 00:14:09 +0000575 string->bDescriptorType = USB_DT_STRING;
576 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200577 usbtty_string_table[STR_MANUFACTURER]=string;
578
wdenk232c1502004-03-12 00:14:09 +0000579
580 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200581 string->bLength = sizeof(wstrProduct);
wdenk232c1502004-03-12 00:14:09 +0000582 string->bDescriptorType = USB_DT_STRING;
583 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200584 usbtty_string_table[STR_PRODUCT]=string;
585
wdenk232c1502004-03-12 00:14:09 +0000586
587 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200588 string->bLength = sizeof(serial_number);
wdenk232c1502004-03-12 00:14:09 +0000589 string->bDescriptorType = USB_DT_STRING;
wdenk6629d2f2004-03-12 15:38:25 +0000590 str2wide (serial_number, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200591 usbtty_string_table[STR_SERIAL]=string;
592
wdenk232c1502004-03-12 00:14:09 +0000593
594 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200595 string->bLength = sizeof(wstrConfiguration);
wdenk232c1502004-03-12 00:14:09 +0000596 string->bDescriptorType = USB_DT_STRING;
Tom Rini6e7df1d2023-01-10 11:19:45 -0500597 str2wide (CFG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200598 usbtty_string_table[STR_CONFIG]=string;
wdenk232c1502004-03-12 00:14:09 +0000599
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200600
601 string = (struct usb_string_descriptor *) wstrDataInterface;
602 string->bLength = sizeof(wstrDataInterface);
wdenk232c1502004-03-12 00:14:09 +0000603 string->bDescriptorType = USB_DT_STRING;
Tom Rini6e7df1d2023-01-10 11:19:45 -0500604 str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200605 usbtty_string_table[STR_DATA_INTERFACE]=string;
606
607 string = (struct usb_string_descriptor *) wstrCtrlInterface;
608 string->bLength = sizeof(wstrCtrlInterface);
609 string->bDescriptorType = USB_DT_STRING;
Tom Rini6e7df1d2023-01-10 11:19:45 -0500610 str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200611 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk232c1502004-03-12 00:14:09 +0000612
613 /* Now, initialize the string table for ep0 handling */
614 usb_strings = usbtty_string_table;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200615}
wdenk232c1502004-03-12 00:14:09 +0000616
Tom Rinib2fb47f2011-12-15 08:40:51 -0700617#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
618 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
619
wdenk232c1502004-03-12 00:14:09 +0000620static void usbtty_init_instances (void)
621{
622 int i;
623
624 /* initialize device instance */
625 memset (device_instance, 0, sizeof (struct usb_device_instance));
626 device_instance->device_state = STATE_INIT;
627 device_instance->device_descriptor = &device_descriptor;
628 device_instance->event = usbtty_event_handler;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200629 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk232c1502004-03-12 00:14:09 +0000630 device_instance->bus = bus_instance;
631 device_instance->configurations = NUM_CONFIGS;
632 device_instance->configuration_instance_array = config_instance;
633
634 /* initialize bus instance */
635 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
636 bus_instance->device = device_instance;
637 bus_instance->endpoint_array = endpoint_instance;
638 bus_instance->max_endpoints = 1;
639 bus_instance->maxpacketsize = 64;
wdenk6629d2f2004-03-12 15:38:25 +0000640 bus_instance->serial_number_str = serial_number;
wdenk232c1502004-03-12 00:14:09 +0000641
642 /* configuration instance */
643 memset (config_instance, 0,
644 sizeof (struct usb_configuration_instance));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200645 config_instance->interfaces = interface_count;
646 config_instance->configuration_descriptor = configuration_descriptor;
wdenk232c1502004-03-12 00:14:09 +0000647 config_instance->interface_instance_array = interface_instance;
648
649 /* interface instance */
650 memset (interface_instance, 0,
651 sizeof (struct usb_interface_instance));
652 interface_instance->alternates = 1;
653 interface_instance->alternates_instance_array = alternate_instance;
654
655 /* alternates instance */
656 memset (alternate_instance, 0,
657 sizeof (struct usb_alternate_instance));
658 alternate_instance->interface_descriptor = interface_descriptors;
659 alternate_instance->endpoints = NUM_ENDPOINTS;
660 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
661
662 /* endpoint instances */
663 memset (&endpoint_instance[0], 0,
664 sizeof (struct usb_endpoint_instance));
665 endpoint_instance[0].endpoint_address = 0;
666 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
667 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
668 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
669 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
670 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
671
672 for (i = 1; i <= NUM_ENDPOINTS; i++) {
673 memset (&endpoint_instance[i], 0,
674 sizeof (struct usb_endpoint_instance));
675
676 endpoint_instance[i].endpoint_address =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200677 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
678
679 endpoint_instance[i].rcv_attributes =
680 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000681
Tom Rinib2fb47f2011-12-15 08:40:51 -0700682 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200683
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200684 endpoint_instance[i].tx_attributes =
685 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000686
Tom Rinib2fb47f2011-12-15 08:40:51 -0700687 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200688
wdenk232c1502004-03-12 00:14:09 +0000689 endpoint_instance[i].tx_attributes =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200690 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk232c1502004-03-12 00:14:09 +0000691
692 urb_link_init (&endpoint_instance[i].rcv);
693 urb_link_init (&endpoint_instance[i].rdy);
694 urb_link_init (&endpoint_instance[i].tx);
695 urb_link_init (&endpoint_instance[i].done);
696
697 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
698 endpoint_instance[i].tx_urb =
699 usbd_alloc_urb (device_instance,
700 &endpoint_instance[i]);
701 else
702 endpoint_instance[i].rcv_urb =
703 usbd_alloc_urb (device_instance,
704 &endpoint_instance[i]);
705 }
706}
707
708static void usbtty_init_endpoints (void)
709{
710 int i;
711
712 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200713 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk232c1502004-03-12 00:14:09 +0000714 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
715 }
716}
717
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200718/* usbtty_init_terminal_type
Wolfgang Denk386eda02006-06-14 18:14:56 +0200719 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200720 * Do some late binding for our device type.
721 */
722static void usbtty_init_terminal_type(short type)
723{
724 switch(type){
Wolfgang Denk386eda02006-06-14 18:14:56 +0200725 /* CDC ACM */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200726 case 0:
727 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200728 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200729 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200730 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200731 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200732 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200733 &acm_configuration_descriptors[0].data_endpoints[1];
wdenk232c1502004-03-12 00:14:09 +0000734
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200735 /* Enumerate Device Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200736 device_descriptor.bDeviceClass =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200737 COMMUNICATIONS_DEVICE_CLASS;
738 device_descriptor.idProduct =
739 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
740
741 /* Assign endpoint indices */
742 tx_endpoint = ACM_TX_ENDPOINT;
743 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200744
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200745 /* Configuration Descriptor */
746 configuration_descriptor =
747 (struct usb_configuration_descriptor*)
748 &acm_configuration_descriptors;
749
750 /* Interface count */
751 interface_count = NUM_ACM_INTERFACES;
752 break;
753
754 /* BULK IN/OUT & Default */
755 case 1:
756 default:
757 /* Assign endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200758 ep_descriptor_ptrs[0] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200759 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200760 ep_descriptor_ptrs[1] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200761 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200762 ep_descriptor_ptrs[2] =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200763 &gserial_configuration_descriptors[0].data_endpoints[2];
764
765 /* Enumerate Device Descriptor */
766 device_descriptor.bDeviceClass = 0xFF;
767 device_descriptor.idProduct =
768 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200769 /* Assign endpoint indices */
770 tx_endpoint = GSERIAL_TX_ENDPOINT;
771 rx_endpoint = GSERIAL_RX_ENDPOINT;
772
773 /* Configuration Descriptor */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200774 configuration_descriptor =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200775 (struct usb_configuration_descriptor*)
776 &gserial_configuration_descriptors;
777
778 /* Interface count */
779 interface_count = NUM_GSERIAL_INTERFACES;
780 break;
781 }
782}
783
784/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000785
786static struct urb *next_urb (struct usb_device_instance *device,
787 struct usb_endpoint_instance *endpoint)
788{
789 struct urb *current_urb = NULL;
790 int space;
791
792 /* If there's a queue, then we should add to the last urb */
793 if (!endpoint->tx_queue) {
794 current_urb = endpoint->tx_urb;
795 } else {
796 /* Last urb from tx chain */
797 current_urb =
798 p2surround (struct urb, link, endpoint->tx.prev);
799 }
800
801 /* Make sure this one has enough room */
802 space = current_urb->buffer_length - current_urb->actual_length;
803 if (space > 0) {
804 return current_urb;
805 } else { /* No space here */
806 /* First look at done list */
807 current_urb = first_urb_detached (&endpoint->done);
808 if (!current_urb) {
809 current_urb = usbd_alloc_urb (device, endpoint);
810 }
811
812 urb_append (&endpoint->tx, current_urb);
813 endpoint->tx_queue++;
814 }
815 return current_urb;
816}
817
818static int write_buffer (circbuf_t * buf)
819{
820 if (!usbtty_configured ()) {
821 return 0;
822 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200823
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200824 struct usb_endpoint_instance *endpoint =
825 &endpoint_instance[tx_endpoint];
826 struct urb *current_urb = NULL;
827
Wolfgang Denk386eda02006-06-14 18:14:56 +0200828 /* TX data still exists - send it now
829 */
Pali Rohár7f840c12021-02-07 14:50:02 +0100830 if(endpoint->sent < endpoint->tx_urb->actual_length){
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200831 if(udc_endpoint_write (endpoint)){
832 /* Write pre-empted by RX */
833 return -1;
834 }
835 }
wdenk232c1502004-03-12 00:14:09 +0000836
837 if (buf->size) {
wdenk232c1502004-03-12 00:14:09 +0000838 char *dest;
839
840 int space_avail;
841 int popnum, popped;
842 int total = 0;
843
Wolfgang Denk386eda02006-06-14 18:14:56 +0200844 /* Break buffer into urb sized pieces,
845 * and link each to the endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200846 */
wdenk232c1502004-03-12 00:14:09 +0000847 while (buf->size > 0) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200848
Pali Rohár7f840c12021-02-07 14:50:02 +0100849 current_urb = next_urb (device_instance, endpoint);
850
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200851 dest = (char*)current_urb->buffer +
wdenk232c1502004-03-12 00:14:09 +0000852 current_urb->actual_length;
853
854 space_avail =
855 current_urb->buffer_length -
856 current_urb->actual_length;
Masahiro Yamadab4141192014-11-07 03:03:31 +0900857 popnum = min(space_avail, (int)buf->size);
wdenk232c1502004-03-12 00:14:09 +0000858 if (popnum == 0)
859 break;
860
861 popped = buf_pop (buf, dest, popnum);
862 if (popped == 0)
863 break;
864 current_urb->actual_length += popped;
865 total += popped;
866
Wolfgang Denk386eda02006-06-14 18:14:56 +0200867 /* If endpoint->last == 0, then transfers have
868 * not started on this endpoint
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200869 */
wdenk232c1502004-03-12 00:14:09 +0000870 if (endpoint->last == 0) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200871 if(udc_endpoint_write (endpoint)){
872 /* Write pre-empted by RX */
873 return -1;
874 }
wdenk232c1502004-03-12 00:14:09 +0000875 }
876
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200877 }/* end while */
wdenk232c1502004-03-12 00:14:09 +0000878 return total;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200879 }
wdenk232c1502004-03-12 00:14:09 +0000880
881 return 0;
882}
883
884static int fill_buffer (circbuf_t * buf)
885{
886 struct usb_endpoint_instance *endpoint =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200887 &endpoint_instance[rx_endpoint];
wdenk232c1502004-03-12 00:14:09 +0000888
889 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200890 unsigned int nb = 0;
wdenk232c1502004-03-12 00:14:09 +0000891 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200892 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk232c1502004-03-12 00:14:09 +0000893
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200894 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk232c1502004-03-12 00:14:09 +0000895
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200896 nb = endpoint->rcv_urb->actual_length;
897 buf_push (buf, src, nb);
898 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200899
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200900 }
wdenk232c1502004-03-12 00:14:09 +0000901 return nb;
902 }
wdenk232c1502004-03-12 00:14:09 +0000903 return 0;
904}
905
906static int usbtty_configured (void)
907{
908 return usbtty_configured_flag;
909}
910
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200911/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000912
913static void usbtty_event_handler (struct usb_device_instance *device,
914 usb_device_event_t event, int data)
915{
916 switch (event) {
917 case DEVICE_RESET:
918 case DEVICE_BUS_INACTIVE:
919 usbtty_configured_flag = 0;
920 break;
921 case DEVICE_CONFIGURED:
922 usbtty_configured_flag = 1;
923 break;
924
925 case DEVICE_ADDRESS_ASSIGNED:
926 usbtty_init_endpoints ();
927
928 default:
929 break;
930 }
931}
932
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200933/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000934
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200935int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
936{
937 switch (request->bRequest){
938
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200939 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200940 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200941 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200942 break;
943 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denk386eda02006-06-14 18:14:56 +0200944 * per character */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200945 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200946 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200947 break;
948 case ACM_GET_LINE_ENCODING : /* request DTE rate,
949 * stop/parity bits */
950 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
951 urb->actual_length = sizeof(rs232_desc);
952
953 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200954 default:
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200955 return 1;
956 }
957 return 0;
958}
959
960/******************************************************************************/
wdenk232c1502004-03-12 00:14:09 +0000961
962/*
963 * Since interrupt handling has not yet been implemented, we use this function
964 * to handle polling. This is called by the tstc,getc,putc,puts routines to
965 * update the USB state.
966 */
967void usbtty_poll (void)
968{
969 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200970 udc_irq();
wdenk232c1502004-03-12 00:14:09 +0000971
Wolfgang Denk386eda02006-06-14 18:14:56 +0200972 /* Write any output data to host buffer
973 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200974 */
wdenk232c1502004-03-12 00:14:09 +0000975 if (usbtty_configured ()) {
976 write_buffer (&usbtty_output);
977 }
978
979 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200980 udc_irq();
Wolfgang Denk386eda02006-06-14 18:14:56 +0200981
982 /* Check for new data from host..
983 * (do this after checking interrupts to get latest data)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200984 */
wdenk232c1502004-03-12 00:14:09 +0000985 if (usbtty_configured ()) {
986 fill_buffer (&usbtty_input);
987 }
988
989 /* New interrupts? */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200990 udc_irq();
991
wdenk232c1502004-03-12 00:14:09 +0000992}