blob: b7d77fbb6a9c604fea55ece39721735421cd042a [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
wdenk29e7f5a2004-03-12 00:14:09 +000057/*
58 * Instance variables
59 */
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020060static struct stdio_dev usbttydev;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020061static struct usb_device_instance device_instance[1];
62static struct usb_bus_instance bus_instance[1];
wdenk29e7f5a2004-03-12 00:14:09 +000063static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk3f0137b2006-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];
wdenk29e7f5a2004-03-12 00:14:09 +000068
69/*
wdenk29e7f5a2004-03-12 00:14:09 +000070 * Global flag
71 */
72int usbtty_configured_flag = 0;
73
wdenk29e7f5a2004-03-12 00:14:09 +000074/*
wdenk05822bf2004-03-12 15:38:25 +000075 * Serial number
76 */
77static char serial_number[16];
78
79/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020080 * Descriptors, Strings, Local variables.
wdenk29e7f5a2004-03-12 00:14:09 +000081 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020082
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020083/* defined and used by gadget/ep0.c */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020084extern struct usb_string_descriptor **usb_strings;
85
86/* Indicies, References */
87static unsigned short rx_endpoint = 0;
88static unsigned short tx_endpoint = 0;
89static unsigned short interface_count = 0;
90static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
91
92/* USB Descriptor Strings */
wdenk29e7f5a2004-03-12 00:14:09 +000093static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
94static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
95static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk05822bf2004-03-12 15:38:25 +000096static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
Tom Rini364d0022023-01-10 11:19:45 -050097static u8 wstrConfiguration[2 + 2*(sizeof(CFG_USBD_CONFIGURATION_STR)-1)];
98static u8 wstrDataInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
99static u8 wstrCtrlInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000100
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200101/* Standard USB Data Structures */
102static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
103static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
104static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000105static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200106 .bLength = sizeof(struct usb_device_descriptor),
107 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200108 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200109 .bDeviceSubClass = 0x00,
110 .bDeviceProtocol = 0x00,
111 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
112 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
113 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
114 .iManufacturer = STR_MANUFACTURER,
115 .iProduct = STR_PRODUCT,
116 .iSerialNumber = STR_SERIAL,
117 .bNumConfigurations = NUM_CONFIGS
wdenk29e7f5a2004-03-12 00:14:09 +0000118};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200119
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200120/*
121 * Static CDC ACM specific descriptors
122 */
123
124struct acm_config_desc {
125 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200126
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200127 /* Master Interface */
128 struct usb_interface_descriptor interface_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200129
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200130 struct usb_class_header_function_descriptor usb_class_header;
131 struct usb_class_call_management_descriptor usb_class_call_mgt;
132 struct usb_class_abstract_control_descriptor usb_class_acm;
133 struct usb_class_union_function_descriptor usb_class_union;
134 struct usb_endpoint_descriptor notification_endpoint;
135
136 /* Slave Interface */
137 struct usb_interface_descriptor data_class_interface;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500138 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200139} __attribute__((packed));
140
141static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
142 {
143 .configuration_desc ={
Wolfgang Denke2601822006-06-14 18:14:56 +0200144 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200145 sizeof(struct usb_configuration_descriptor),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200146 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200147 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200148 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200149 .bNumInterfaces = NUM_ACM_INTERFACES,
150 .bConfigurationValue = 1,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200151 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200152 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200153 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
154 .bMaxPower = USBTTY_MAXPOWER
155 },
156 /* Interface 1 */
157 .interface_desc = {
158 .bLength = sizeof(struct usb_interface_descriptor),
159 .bDescriptorType = USB_DT_INTERFACE,
160 .bInterfaceNumber = 0,
161 .bAlternateSetting = 0,
162 .bNumEndpoints = 0x01,
Wolfgang Denke2601822006-06-14 18:14:56 +0200163 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200164 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
165 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
166 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
167 .iInterface = STR_CTRL_INTERFACE,
168 },
169 .usb_class_header = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200170 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200171 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denke2601822006-06-14 18:14:56 +0200172 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200173 .bDescriptorSubtype = USB_ST_HEADER,
174 .bcdCDC = cpu_to_le16(110),
175 },
176 .usb_class_call_mgt = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200177 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200178 sizeof(struct usb_class_call_management_descriptor),
179 .bDescriptorType = CS_INTERFACE,
180 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200181 .bmCapabilities = 0x00,
182 .bDataInterface = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200183 },
184 .usb_class_acm = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200185 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200186 sizeof(struct usb_class_abstract_control_descriptor),
187 .bDescriptorType = CS_INTERFACE,
Wolfgang Denke2601822006-06-14 18:14:56 +0200188 .bDescriptorSubtype = USB_ST_ACMF,
189 .bmCapabilities = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200190 },
191 .usb_class_union = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200192 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200193 sizeof(struct usb_class_union_function_descriptor),
194 .bDescriptorType = CS_INTERFACE,
195 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200196 .bMasterInterface = 0x00,
197 .bSlaveInterface0 = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200198 },
199 .notification_endpoint = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200200 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200201 sizeof(struct usb_endpoint_descriptor),
202 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530203 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200204 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200205 .wMaxPacketSize
Tom Rini364d0022023-01-10 11:19:45 -0500206 = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200207 .bInterval = 0xFF,
208 },
209
210 /* Interface 2 */
211 .data_class_interface = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200212 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200213 sizeof(struct usb_interface_descriptor),
214 .bDescriptorType = USB_DT_INTERFACE,
215 .bInterfaceNumber = 0x01,
216 .bAlternateSetting = 0x00,
217 .bNumEndpoints = 0x02,
Wolfgang Denke2601822006-06-14 18:14:56 +0200218 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200219 COMMUNICATIONS_INTERFACE_CLASS_DATA,
220 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
221 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
222 .iInterface = STR_DATA_INTERFACE,
223 },
224 .data_endpoints = {
225 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200226 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200227 sizeof(struct usb_endpoint_descriptor),
228 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530229 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200230 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200231 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200232 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500233 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200234 .bInterval = 0xFF,
235 },
236 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200237 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200238 sizeof(struct usb_endpoint_descriptor),
239 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530240 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denke2601822006-06-14 18:14:56 +0200241 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200242 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200243 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500244 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200245 .bInterval = 0xFF,
246 },
247 },
248 },
Wolfgang Denke2601822006-06-14 18:14:56 +0200249};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200250
251static struct rs232_emu rs232_desc={
Wolfgang Denka1be4762008-05-20 16:00:29 +0200252 .dter = 115200,
253 .stop_bits = 0x00,
254 .parity = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200255 .data_bits = 0x08
wdenk29e7f5a2004-03-12 00:14:09 +0000256};
257
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200258/*
259 * Static Generic Serial specific data
260 */
261
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200262struct gserial_config_desc {
Wolfgang Denke2601822006-06-14 18:14:56 +0200263
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200264 struct usb_configuration_descriptor configuration_desc;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500265 struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
266 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200267
268} __attribute__((packed));
269
Wolfgang Denke2601822006-06-14 18:14:56 +0200270static struct gserial_config_desc
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200271gserial_configuration_descriptors[NUM_CONFIGS] ={
272 {
273 .configuration_desc ={
274 .bLength = sizeof(struct usb_configuration_descriptor),
275 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200276 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200277 cpu_to_le16(sizeof(struct gserial_config_desc)),
278 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
279 .bConfigurationValue = 1,
280 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200281 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200282 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
283 .bMaxPower = USBTTY_MAXPOWER
284 },
285 .interface_desc = {
286 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200287 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200288 sizeof(struct usb_interface_descriptor),
289 .bDescriptorType = USB_DT_INTERFACE,
290 .bInterfaceNumber = 0,
291 .bAlternateSetting = 0,
292 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200293 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200294 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denke2601822006-06-14 18:14:56 +0200295 .bInterfaceSubClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200296 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200297 .bInterfaceProtocol =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200298 COMMUNICATIONS_NO_PROTOCOL,
299 .iInterface = STR_DATA_INTERFACE
300 },
Wolfgang Denka1be4762008-05-20 16:00:29 +0200301 },
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200302 .data_endpoints = {
303 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200304 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200305 sizeof(struct usb_endpoint_descriptor),
306 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530307 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200308 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200309 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500310 cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200311 .bInterval= 0xFF,
312 },
313 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200314 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200315 sizeof(struct usb_endpoint_descriptor),
316 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530317 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200318 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200319 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500320 cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200321 .bInterval = 0xFF,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200322 },
323 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200324 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200325 sizeof(struct usb_endpoint_descriptor),
326 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530327 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200328 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200329 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500330 cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200331 .bInterval = 0xFF,
332 },
333 },
334 },
335};
wdenk29e7f5a2004-03-12 00:14:09 +0000336
337/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200338 * Static Function Prototypes
wdenk29e7f5a2004-03-12 00:14:09 +0000339 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200340
wdenk29e7f5a2004-03-12 00:14:09 +0000341static void usbtty_init_strings (void);
342static void usbtty_init_instances (void);
343static void usbtty_init_endpoints (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200344static void usbtty_init_terminal_type(short type);
wdenk29e7f5a2004-03-12 00:14:09 +0000345static void usbtty_event_handler (struct usb_device_instance *device,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200346 usb_device_event_t event, int data);
Wolfgang Denke2601822006-06-14 18:14:56 +0200347static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200348 struct urb *urb);
wdenk29e7f5a2004-03-12 00:14:09 +0000349static int usbtty_configured (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000350static int write_buffer (circbuf_t * buf);
351static int fill_buffer (circbuf_t * buf);
352
353void usbtty_poll (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000354
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200355/* utility function for converting char* to wide string used by USB */
356static void str2wide (char *str, u16 * wide)
357{
358 int i;
359 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200360 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200361 wide[i] = (u16) str[i];
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200362 #elif defined(__BIG_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200363 wide[i] = ((u16)(str[i])<<8);
364 #else
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200365 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200366 #endif
367 }
368}
wdenk29e7f5a2004-03-12 00:14:09 +0000369
370/*
371 * Test whether a character is in the RX buffer
372 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200373
Simon Glass0d1e1f72014-07-23 06:54:59 -0600374int usbtty_tstc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000375{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200376 struct usb_endpoint_instance *endpoint =
377 &endpoint_instance[rx_endpoint];
378
379 /* If no input data exists, allow more RX to be accepted */
380 if(usbtty_input.size <= 0){
381 udc_unset_nak(endpoint->endpoint_address&0x03);
382 }
383
wdenk29e7f5a2004-03-12 00:14:09 +0000384 usbtty_poll ();
385 return (usbtty_input.size > 0);
386}
387
388/*
389 * Read a single byte from the usb client port. Returns 1 on success, 0
390 * otherwise. When the function is succesfull, the character read is
391 * written into its argument c.
392 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200393
Simon Glass0d1e1f72014-07-23 06:54:59 -0600394int usbtty_getc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000395{
396 char c;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200397 struct usb_endpoint_instance *endpoint =
398 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000399
400 while (usbtty_input.size <= 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200401 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk29e7f5a2004-03-12 00:14:09 +0000402 usbtty_poll ();
403 }
404
405 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200406 udc_set_nak(endpoint->endpoint_address&0x03);
407
wdenk29e7f5a2004-03-12 00:14:09 +0000408 return c;
409}
410
411/*
412 * Output a single byte to the usb client port.
413 */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600414void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk29e7f5a2004-03-12 00:14:09 +0000415{
Atin Malaviya11a9df92009-02-03 15:17:10 -0500416 if (!usbtty_configured ())
417 return;
418
wdenk29e7f5a2004-03-12 00:14:09 +0000419 /* If \n, also do \r */
420 if (c == '\n')
421 buf_push (&usbtty_output, "\r", 1);
422
Alison Wang23e06b52016-03-02 11:00:37 +0800423 buf_push(&usbtty_output, &c, 1);
424
wdenk29e7f5a2004-03-12 00:14:09 +0000425 /* Poll at end to handle new data... */
426 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
427 usbtty_poll ();
428 }
429}
430
wdenk29e7f5a2004-03-12 00:14:09 +0000431/* usbtty_puts() helper function for finding the next '\n' in a string */
432static int next_nl_pos (const char *s)
433{
434 int i;
435
436 for (i = 0; s[i] != '\0'; i++) {
437 if (s[i] == '\n')
438 return i;
439 }
440 return i;
441}
442
443/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200444 * Output a string to the usb client port - implementing flow control
wdenk29e7f5a2004-03-12 00:14:09 +0000445 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200446
wdenk29e7f5a2004-03-12 00:14:09 +0000447static void __usbtty_puts (const char *str, int len)
448{
449 int maxlen = usbtty_output.totalsize;
450 int space, n;
451
452 /* break str into chunks < buffer size, if needed */
453 while (len > 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200454 usbtty_poll ();
wdenk29e7f5a2004-03-12 00:14:09 +0000455
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200456 space = maxlen - usbtty_output.size;
wdenk29e7f5a2004-03-12 00:14:09 +0000457 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200458 if (space) {
wdenk29e7f5a2004-03-12 00:14:09 +0000459 write_buffer (&usbtty_output);
Wolfgang Denke2601822006-06-14 18:14:56 +0200460
Masahiro Yamadab62b39b2014-09-18 13:28:06 +0900461 n = min(space, min(len, maxlen));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200462 buf_push (&usbtty_output, str, n);
wdenk29e7f5a2004-03-12 00:14:09 +0000463
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200464 str += n;
Wolfgang Denke2601822006-06-14 18:14:56 +0200465 len -= n;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200466 }
wdenk29e7f5a2004-03-12 00:14:09 +0000467 }
468}
469
Simon Glass0d1e1f72014-07-23 06:54:59 -0600470void usbtty_puts(struct stdio_dev *dev, const char *str)
wdenk29e7f5a2004-03-12 00:14:09 +0000471{
472 int n;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500473 int len;
474
475 if (!usbtty_configured ())
476 return;
wdenk29e7f5a2004-03-12 00:14:09 +0000477
Atin Malaviya11a9df92009-02-03 15:17:10 -0500478 len = strlen (str);
wdenk29e7f5a2004-03-12 00:14:09 +0000479 /* add '\r' for each '\n' */
480 while (len > 0) {
481 n = next_nl_pos (str);
482
483 if (str[n] == '\n') {
Pali Rohár84567c32021-02-07 14:50:01 +0100484 __usbtty_puts(str, n);
485 __usbtty_puts("\r\n", 2);
wdenk29e7f5a2004-03-12 00:14:09 +0000486 str += (n + 1);
487 len -= (n + 1);
488 } else {
489 /* No \n found. All done. */
490 __usbtty_puts (str, n);
491 break;
492 }
493 }
494
495 /* Poll at end to handle new data... */
496 usbtty_poll ();
497}
498
499/*
500 * Initialize the usb client port.
501 *
502 */
503int drv_usbtty_init (void)
504{
505 int rc;
wdenk05822bf2004-03-12 15:38:25 +0000506 char * sn;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200507 char * tt;
wdenk05822bf2004-03-12 15:38:25 +0000508 int snlen;
wdenkc35ba4e2004-03-14 22:25:36 +0000509
Heinrich Schuchardta8d4d312017-10-12 23:37:37 +0200510 /* Get serial number */
Simon Glass64b723f2017-08-03 12:22:12 -0600511 sn = env_get("serial#");
512 if (!sn)
wdenk05822bf2004-03-12 15:38:25 +0000513 sn = "000000000000";
wdenk05822bf2004-03-12 15:38:25 +0000514 snlen = strlen(sn);
515 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denk509cd072008-07-14 15:06:35 +0200516 printf ("Warning: serial number %s is too long (%d > %lu)\n",
517 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk05822bf2004-03-12 15:38:25 +0000518 snlen = sizeof(serial_number) - 1;
519 }
520 memcpy (serial_number, sn, snlen);
521 serial_number[snlen] = '\0';
wdenk29e7f5a2004-03-12 00:14:09 +0000522
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200523 /* Decide on which type of UDC device to be.
524 */
Simon Glass64b723f2017-08-03 12:22:12 -0600525 tt = env_get("usbtty");
526 if (!tt)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200527 tt = "generic";
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200528 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denke2601822006-06-14 18:14:56 +0200529
wdenk29e7f5a2004-03-12 00:14:09 +0000530 /* prepare buffers... */
531 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
532 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
533
534 /* Now, set up USB controller and infrastructure */
535 udc_init (); /* Basic USB initialization */
536
537 usbtty_init_strings ();
538 usbtty_init_instances ();
539
Stefan Herbrechtsmeier49914b62011-10-17 17:22:49 +0200540 usbtty_init_endpoints ();
541
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200542 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk29e7f5a2004-03-12 00:14:09 +0000543 udc_connect (); /* Enable pullup for host detection */
544
wdenk29e7f5a2004-03-12 00:14:09 +0000545 /* Device initialization */
546 memset (&usbttydev, 0, sizeof (usbttydev));
547
548 strcpy (usbttydev.name, "usbtty");
549 usbttydev.ext = 0; /* No extensions */
550 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
551 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
552 usbttydev.getc = usbtty_getc; /* 'getc' function */
553 usbttydev.putc = usbtty_putc; /* 'putc' function */
554 usbttydev.puts = usbtty_puts; /* 'puts' function */
555
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200556 rc = stdio_register (&usbttydev);
wdenk29e7f5a2004-03-12 00:14:09 +0000557
558 return (rc == 0) ? 1 : rc;
559}
560
561static void usbtty_init_strings (void)
562{
563 struct usb_string_descriptor *string;
564
Wolfgang Denke2601822006-06-14 18:14:56 +0200565 usbtty_string_table[STR_LANG] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200566 (struct usb_string_descriptor*)wstrLang;
567
wdenk29e7f5a2004-03-12 00:14:09 +0000568 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200569 string->bLength = sizeof(wstrManufacturer);
wdenk29e7f5a2004-03-12 00:14:09 +0000570 string->bDescriptorType = USB_DT_STRING;
571 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200572 usbtty_string_table[STR_MANUFACTURER]=string;
573
wdenk29e7f5a2004-03-12 00:14:09 +0000574 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200575 string->bLength = sizeof(wstrProduct);
wdenk29e7f5a2004-03-12 00:14:09 +0000576 string->bDescriptorType = USB_DT_STRING;
577 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200578 usbtty_string_table[STR_PRODUCT]=string;
579
wdenk29e7f5a2004-03-12 00:14:09 +0000580 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200581 string->bLength = sizeof(serial_number);
wdenk29e7f5a2004-03-12 00:14:09 +0000582 string->bDescriptorType = USB_DT_STRING;
wdenk05822bf2004-03-12 15:38:25 +0000583 str2wide (serial_number, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200584 usbtty_string_table[STR_SERIAL]=string;
585
wdenk29e7f5a2004-03-12 00:14:09 +0000586 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200587 string->bLength = sizeof(wstrConfiguration);
wdenk29e7f5a2004-03-12 00:14:09 +0000588 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500589 str2wide (CFG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200590 usbtty_string_table[STR_CONFIG]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000591
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200592 string = (struct usb_string_descriptor *) wstrDataInterface;
593 string->bLength = sizeof(wstrDataInterface);
594 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500595 str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200596 usbtty_string_table[STR_DATA_INTERFACE]=string;
597
598 string = (struct usb_string_descriptor *) wstrCtrlInterface;
599 string->bLength = sizeof(wstrCtrlInterface);
wdenk29e7f5a2004-03-12 00:14:09 +0000600 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500601 str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200602 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000603
604 /* Now, initialize the string table for ep0 handling */
605 usb_strings = usbtty_string_table;
Wolfgang Denke2601822006-06-14 18:14:56 +0200606}
wdenk29e7f5a2004-03-12 00:14:09 +0000607
Tom Rini73424522011-12-15 08:40:51 -0700608#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
609 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
610
wdenk29e7f5a2004-03-12 00:14:09 +0000611static void usbtty_init_instances (void)
612{
613 int i;
614
615 /* initialize device instance */
616 memset (device_instance, 0, sizeof (struct usb_device_instance));
617 device_instance->device_state = STATE_INIT;
618 device_instance->device_descriptor = &device_descriptor;
619 device_instance->event = usbtty_event_handler;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200620 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk29e7f5a2004-03-12 00:14:09 +0000621 device_instance->bus = bus_instance;
622 device_instance->configurations = NUM_CONFIGS;
623 device_instance->configuration_instance_array = config_instance;
624
625 /* initialize bus instance */
626 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
627 bus_instance->device = device_instance;
628 bus_instance->endpoint_array = endpoint_instance;
629 bus_instance->max_endpoints = 1;
630 bus_instance->maxpacketsize = 64;
wdenk05822bf2004-03-12 15:38:25 +0000631 bus_instance->serial_number_str = serial_number;
wdenk29e7f5a2004-03-12 00:14:09 +0000632
633 /* configuration instance */
634 memset (config_instance, 0,
635 sizeof (struct usb_configuration_instance));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200636 config_instance->interfaces = interface_count;
637 config_instance->configuration_descriptor = configuration_descriptor;
wdenk29e7f5a2004-03-12 00:14:09 +0000638 config_instance->interface_instance_array = interface_instance;
639
640 /* interface instance */
641 memset (interface_instance, 0,
642 sizeof (struct usb_interface_instance));
643 interface_instance->alternates = 1;
644 interface_instance->alternates_instance_array = alternate_instance;
645
646 /* alternates instance */
647 memset (alternate_instance, 0,
648 sizeof (struct usb_alternate_instance));
649 alternate_instance->interface_descriptor = interface_descriptors;
650 alternate_instance->endpoints = NUM_ENDPOINTS;
651 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
652
653 /* endpoint instances */
654 memset (&endpoint_instance[0], 0,
655 sizeof (struct usb_endpoint_instance));
656 endpoint_instance[0].endpoint_address = 0;
657 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
658 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
659 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
660 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
661 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
662
663 for (i = 1; i <= NUM_ENDPOINTS; i++) {
664 memset (&endpoint_instance[i], 0,
665 sizeof (struct usb_endpoint_instance));
666
667 endpoint_instance[i].endpoint_address =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200668 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
wdenk29e7f5a2004-03-12 00:14:09 +0000669
wdenk29e7f5a2004-03-12 00:14:09 +0000670 endpoint_instance[i].rcv_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200671 ep_descriptor_ptrs[i - 1]->bmAttributes;
672
Tom Rini73424522011-12-15 08:40:51 -0700673 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denke2601822006-06-14 18:14:56 +0200674
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200675 endpoint_instance[i].tx_attributes =
676 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000677
Tom Rini73424522011-12-15 08:40:51 -0700678 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200679
wdenk29e7f5a2004-03-12 00:14:09 +0000680 endpoint_instance[i].tx_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200681 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000682
683 urb_link_init (&endpoint_instance[i].rcv);
684 urb_link_init (&endpoint_instance[i].rdy);
685 urb_link_init (&endpoint_instance[i].tx);
686 urb_link_init (&endpoint_instance[i].done);
687
688 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
689 endpoint_instance[i].tx_urb =
690 usbd_alloc_urb (device_instance,
691 &endpoint_instance[i]);
692 else
693 endpoint_instance[i].rcv_urb =
694 usbd_alloc_urb (device_instance,
695 &endpoint_instance[i]);
696 }
697}
698
699static void usbtty_init_endpoints (void)
700{
701 int i;
702
703 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200704 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk29e7f5a2004-03-12 00:14:09 +0000705 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
706 }
707}
708
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200709/* usbtty_init_terminal_type
Wolfgang Denke2601822006-06-14 18:14:56 +0200710 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200711 * Do some late binding for our device type.
712 */
713static void usbtty_init_terminal_type(short type)
714{
715 switch(type){
Wolfgang Denke2601822006-06-14 18:14:56 +0200716 /* CDC ACM */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200717 case 0:
718 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200719 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200720 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denke2601822006-06-14 18:14:56 +0200721 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200722 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200723 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200724 &acm_configuration_descriptors[0].data_endpoints[1];
725
726 /* Enumerate Device Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200727 device_descriptor.bDeviceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200728 COMMUNICATIONS_DEVICE_CLASS;
729 device_descriptor.idProduct =
730 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
731
732 /* Assign endpoint indices */
733 tx_endpoint = ACM_TX_ENDPOINT;
734 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denke2601822006-06-14 18:14:56 +0200735
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200736 /* Configuration Descriptor */
737 configuration_descriptor =
738 (struct usb_configuration_descriptor*)
739 &acm_configuration_descriptors;
740
741 /* Interface count */
742 interface_count = NUM_ACM_INTERFACES;
743 break;
744
745 /* BULK IN/OUT & Default */
746 case 1:
747 default:
748 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200749 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200750 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200751 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200752 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denke2601822006-06-14 18:14:56 +0200753 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200754 &gserial_configuration_descriptors[0].data_endpoints[2];
wdenk29e7f5a2004-03-12 00:14:09 +0000755
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200756 /* Enumerate Device Descriptor */
757 device_descriptor.bDeviceClass = 0xFF;
758 device_descriptor.idProduct =
759 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200760 /* Assign endpoint indices */
761 tx_endpoint = GSERIAL_TX_ENDPOINT;
762 rx_endpoint = GSERIAL_RX_ENDPOINT;
763
764 /* Configuration Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200765 configuration_descriptor =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200766 (struct usb_configuration_descriptor*)
767 &gserial_configuration_descriptors;
768
769 /* Interface count */
770 interface_count = NUM_GSERIAL_INTERFACES;
771 break;
772 }
773}
774
775/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000776
777static struct urb *next_urb (struct usb_device_instance *device,
778 struct usb_endpoint_instance *endpoint)
779{
780 struct urb *current_urb = NULL;
781 int space;
782
783 /* If there's a queue, then we should add to the last urb */
784 if (!endpoint->tx_queue) {
785 current_urb = endpoint->tx_urb;
786 } else {
787 /* Last urb from tx chain */
788 current_urb =
789 p2surround (struct urb, link, endpoint->tx.prev);
790 }
791
792 /* Make sure this one has enough room */
793 space = current_urb->buffer_length - current_urb->actual_length;
794 if (space > 0) {
795 return current_urb;
796 } else { /* No space here */
797 /* First look at done list */
798 current_urb = first_urb_detached (&endpoint->done);
799 if (!current_urb) {
800 current_urb = usbd_alloc_urb (device, endpoint);
801 }
802
803 urb_append (&endpoint->tx, current_urb);
804 endpoint->tx_queue++;
805 }
806 return current_urb;
807}
808
809static int write_buffer (circbuf_t * buf)
810{
811 if (!usbtty_configured ()) {
812 return 0;
813 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200814
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200815 struct usb_endpoint_instance *endpoint =
816 &endpoint_instance[tx_endpoint];
817 struct urb *current_urb = NULL;
wdenk29e7f5a2004-03-12 00:14:09 +0000818
Wolfgang Denke2601822006-06-14 18:14:56 +0200819 /* TX data still exists - send it now
820 */
Pali Rohár29825e92021-02-07 14:50:02 +0100821 if(endpoint->sent < endpoint->tx_urb->actual_length){
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200822 if(udc_endpoint_write (endpoint)){
823 /* Write pre-empted by RX */
824 return -1;
825 }
826 }
wdenk29e7f5a2004-03-12 00:14:09 +0000827
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200828 if (buf->size) {
wdenk29e7f5a2004-03-12 00:14:09 +0000829 char *dest;
830
831 int space_avail;
832 int popnum, popped;
833 int total = 0;
834
Wolfgang Denke2601822006-06-14 18:14:56 +0200835 /* Break buffer into urb sized pieces,
836 * and link each to the endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200837 */
wdenk29e7f5a2004-03-12 00:14:09 +0000838 while (buf->size > 0) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200839
Pali Rohár29825e92021-02-07 14:50:02 +0100840 current_urb = next_urb (device_instance, endpoint);
841
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200842 dest = (char*)current_urb->buffer +
wdenk29e7f5a2004-03-12 00:14:09 +0000843 current_urb->actual_length;
844
845 space_avail =
846 current_urb->buffer_length -
847 current_urb->actual_length;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900848 popnum = min(space_avail, (int)buf->size);
wdenk29e7f5a2004-03-12 00:14:09 +0000849 if (popnum == 0)
850 break;
851
852 popped = buf_pop (buf, dest, popnum);
853 if (popped == 0)
854 break;
855 current_urb->actual_length += popped;
856 total += popped;
857
Wolfgang Denke2601822006-06-14 18:14:56 +0200858 /* If endpoint->last == 0, then transfers have
859 * not started on this endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200860 */
wdenk29e7f5a2004-03-12 00:14:09 +0000861 if (endpoint->last == 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200862 if(udc_endpoint_write (endpoint)){
863 /* Write pre-empted by RX */
864 return -1;
865 }
wdenk29e7f5a2004-03-12 00:14:09 +0000866 }
867
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200868 }/* end while */
wdenk29e7f5a2004-03-12 00:14:09 +0000869 return total;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200870 }
wdenk29e7f5a2004-03-12 00:14:09 +0000871
872 return 0;
873}
874
875static int fill_buffer (circbuf_t * buf)
876{
877 struct usb_endpoint_instance *endpoint =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200878 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000879
880 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200881 unsigned int nb = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000882 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200883 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk29e7f5a2004-03-12 00:14:09 +0000884
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200885 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk29e7f5a2004-03-12 00:14:09 +0000886
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200887 nb = endpoint->rcv_urb->actual_length;
888 buf_push (buf, src, nb);
889 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200890
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200891 }
wdenk29e7f5a2004-03-12 00:14:09 +0000892 return nb;
893 }
wdenk29e7f5a2004-03-12 00:14:09 +0000894 return 0;
895}
896
897static int usbtty_configured (void)
898{
899 return usbtty_configured_flag;
900}
901
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200902/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000903
904static void usbtty_event_handler (struct usb_device_instance *device,
905 usb_device_event_t event, int data)
906{
907 switch (event) {
908 case DEVICE_RESET:
909 case DEVICE_BUS_INACTIVE:
910 usbtty_configured_flag = 0;
911 break;
912 case DEVICE_CONFIGURED:
913 usbtty_configured_flag = 1;
914 break;
915
916 case DEVICE_ADDRESS_ASSIGNED:
917 usbtty_init_endpoints ();
918
919 default:
920 break;
921 }
922}
923
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200924/******************************************************************************/
925
926int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
927{
928 switch (request->bRequest){
929
Wolfgang Denka1be4762008-05-20 16:00:29 +0200930 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200931 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200932 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200933 break;
934 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denke2601822006-06-14 18:14:56 +0200935 * per character */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200936 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200937 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200938 break;
939 case ACM_GET_LINE_ENCODING : /* request DTE rate,
940 * stop/parity bits */
941 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
942 urb->actual_length = sizeof(rs232_desc);
943
944 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200945 default:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200946 return 1;
947 }
948 return 0;
949}
wdenk29e7f5a2004-03-12 00:14:09 +0000950
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200951/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000952
953/*
954 * Since interrupt handling has not yet been implemented, we use this function
955 * to handle polling. This is called by the tstc,getc,putc,puts routines to
956 * update the USB state.
957 */
958void usbtty_poll (void)
959{
960 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200961 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000962
Wolfgang Denke2601822006-06-14 18:14:56 +0200963 /* Write any output data to host buffer
964 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200965 */
wdenk29e7f5a2004-03-12 00:14:09 +0000966 if (usbtty_configured ()) {
967 write_buffer (&usbtty_output);
968 }
969
970 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200971 udc_irq();
Wolfgang Denke2601822006-06-14 18:14:56 +0200972
973 /* Check for new data from host..
974 * (do this after checking interrupts to get latest data)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200975 */
wdenk29e7f5a2004-03-12 00:14:09 +0000976 if (usbtty_configured ()) {
977 fill_buffer (&usbtty_input);
978 }
979
980 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200981 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000982
wdenk29e7f5a2004-03-12 00:14:09 +0000983}