blob: ecb6ba853df78f0737b405275d70a4e57f1d6b58 [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
10#include <common.h>
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010011#include <config.h>
wdenk29e7f5a2004-03-12 00:14:09 +000012#include <circbuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Simon Glass23fe4492019-11-14 12:57:22 -070014#include <serial.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020015#include <stdio_dev.h>
Tom Rini73424522011-12-15 08:40:51 -070016#include <asm/unaligned.h>
wdenk29e7f5a2004-03-12 00:14:09 +000017#include "usbtty.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020018#include "usb_cdc_acm.h"
19#include "usbdescriptors.h"
wdenk29e7f5a2004-03-12 00:14:09 +000020
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010021#ifdef DEBUG
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020022#define TTYDBG(fmt,args...)\
23 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000024#else
25#define TTYDBG(fmt,args...) do{}while(0)
26#endif
27
Wolfgang Denk3f0137b2006-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)
wdenk29e7f5a2004-03-12 00:14:09 +000032#else
33#define TTYERR(fmt,args...) do{}while(0)
34#endif
35
36/*
Wolfgang Denk3f0137b2006-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 Rini364d0022023-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 Denk3f0137b2006-06-14 17:45:53 +020050
51/*
wdenk29e7f5a2004-03-12 00:14:09 +000052 * Buffers to hold input and output data
53 */
Shiraz Hashim96decf62012-12-17 14:19:37 +053054#define USBTTY_BUFFER_SIZE 2048
wdenk29e7f5a2004-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-VILLARD2a7a0312009-05-16 12:14:54 +020062static struct stdio_dev usbttydev;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020063static struct usb_device_instance device_instance[1];
64static struct usb_bus_instance bus_instance[1];
wdenk29e7f5a2004-03-12 00:14:09 +000065static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk3f0137b2006-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];
wdenk29e7f5a2004-03-12 00:14:09 +000070
71/*
wdenk29e7f5a2004-03-12 00:14:09 +000072 * Global flag
73 */
74int usbtty_configured_flag = 0;
75
wdenk29e7f5a2004-03-12 00:14:09 +000076/*
wdenk05822bf2004-03-12 15:38:25 +000077 * Serial number
78 */
79static char serial_number[16];
80
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020081
wdenk05822bf2004-03-12 15:38:25 +000082/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020083 * Descriptors, Strings, Local variables.
wdenk29e7f5a2004-03-12 00:14:09 +000084 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020085
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020086/* defined and used by gadget/ep0.c */
Wolfgang Denk3f0137b2006-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 */
wdenk29e7f5a2004-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)];
wdenk05822bf2004-03-12 15:38:25 +000099static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
Tom Rini364d0022023-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)];
wdenk29e7f5a2004-03-12 00:14:09 +0000103
Wolfgang Denk3f0137b2006-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;
wdenk29e7f5a2004-03-12 00:14:09 +0000108static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200109 .bLength = sizeof(struct usb_device_descriptor),
110 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200111 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk3f0137b2006-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
wdenk29e7f5a2004-03-12 00:14:09 +0000121};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200122
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200129
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200130 /* Master Interface */
131 struct usb_interface_descriptor interface_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200132
Wolfgang Denk3f0137b2006-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 Malaviya11a9df92009-02-03 15:17:10 -0500141 struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200142} __attribute__((packed));
143
144static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
145 {
146 .configuration_desc ={
Wolfgang Denke2601822006-06-14 18:14:56 +0200147 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200148 sizeof(struct usb_configuration_descriptor),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200149 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200150 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200151 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200152 .bNumInterfaces = NUM_ACM_INTERFACES,
153 .bConfigurationValue = 1,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200154 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200155 .bmAttributes =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200166 .bInterfaceClass =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200173 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200174 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denke2601822006-06-14 18:14:56 +0200175 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200176 .bDescriptorSubtype = USB_ST_HEADER,
177 .bcdCDC = cpu_to_le16(110),
178 },
179 .usb_class_call_mgt = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200180 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200181 sizeof(struct usb_class_call_management_descriptor),
182 .bDescriptorType = CS_INTERFACE,
183 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200184 .bmCapabilities = 0x00,
185 .bDataInterface = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200186 },
187 .usb_class_acm = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200188 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200189 sizeof(struct usb_class_abstract_control_descriptor),
190 .bDescriptorType = CS_INTERFACE,
Wolfgang Denke2601822006-06-14 18:14:56 +0200191 .bDescriptorSubtype = USB_ST_ACMF,
192 .bmCapabilities = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200193 },
194 .usb_class_union = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200195 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200196 sizeof(struct usb_class_union_function_descriptor),
197 .bDescriptorType = CS_INTERFACE,
198 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200199 .bMasterInterface = 0x00,
200 .bSlaveInterface0 = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200201 },
202 .notification_endpoint = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200203 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200204 sizeof(struct usb_endpoint_descriptor),
205 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530206 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200207 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200208 .wMaxPacketSize
Tom Rini364d0022023-01-10 11:19:45 -0500209 = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200210 .bInterval = 0xFF,
211 },
212
213 /* Interface 2 */
214 .data_class_interface = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200215 .bLength =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200221 .bInterfaceClass =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200229 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200230 sizeof(struct usb_endpoint_descriptor),
231 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530232 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200233 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200234 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200235 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500236 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200237 .bInterval = 0xFF,
238 },
239 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200240 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200241 sizeof(struct usb_endpoint_descriptor),
242 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530243 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denke2601822006-06-14 18:14:56 +0200244 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200245 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200246 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500247 cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200248 .bInterval = 0xFF,
249 },
250 },
251 },
Wolfgang Denke2601822006-06-14 18:14:56 +0200252};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200253
254static struct rs232_emu rs232_desc={
Wolfgang Denka1be4762008-05-20 16:00:29 +0200255 .dter = 115200,
256 .stop_bits = 0x00,
257 .parity = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200258 .data_bits = 0x08
wdenk29e7f5a2004-03-12 00:14:09 +0000259};
260
wdenk29e7f5a2004-03-12 00:14:09 +0000261
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200262/*
263 * Static Generic Serial specific data
264 */
265
266
267struct gserial_config_desc {
Wolfgang Denke2601822006-06-14 18:14:56 +0200268
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200269 struct usb_configuration_descriptor configuration_desc;
Atin Malaviya11a9df92009-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 Denk3f0137b2006-06-14 17:45:53 +0200272
273} __attribute__((packed));
274
Wolfgang Denke2601822006-06-14 18:14:56 +0200275static struct gserial_config_desc
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200281 .wTotalLength =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200286 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200287 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
288 .bMaxPower = USBTTY_MAXPOWER
289 },
290 .interface_desc = {
291 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200292 .bLength =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200298 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200299 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denke2601822006-06-14 18:14:56 +0200300 .bInterfaceSubClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200301 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200302 .bInterfaceProtocol =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200303 COMMUNICATIONS_NO_PROTOCOL,
304 .iInterface = STR_DATA_INTERFACE
305 },
Wolfgang Denka1be4762008-05-20 16:00:29 +0200306 },
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200307 .data_endpoints = {
308 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200309 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200310 sizeof(struct usb_endpoint_descriptor),
311 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530312 .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200313 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200314 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500315 cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200316 .bInterval= 0xFF,
317 },
318 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200319 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200320 sizeof(struct usb_endpoint_descriptor),
321 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530322 .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200324 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500325 cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200326 .bInterval = 0xFF,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200327 },
328 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200329 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200330 sizeof(struct usb_endpoint_descriptor),
331 .bDescriptorType = USB_DT_ENDPOINT,
Vivek Kutal5b774382009-02-23 21:35:11 +0530332 .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200333 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200334 .wMaxPacketSize =
Tom Rini364d0022023-01-10 11:19:45 -0500335 cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200336 .bInterval = 0xFF,
337 },
338 },
339 },
340};
wdenk29e7f5a2004-03-12 00:14:09 +0000341
342/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200343 * Static Function Prototypes
wdenk29e7f5a2004-03-12 00:14:09 +0000344 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200345
wdenk29e7f5a2004-03-12 00:14:09 +0000346static void usbtty_init_strings (void);
347static void usbtty_init_instances (void);
348static void usbtty_init_endpoints (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200349static void usbtty_init_terminal_type(short type);
wdenk29e7f5a2004-03-12 00:14:09 +0000350static void usbtty_event_handler (struct usb_device_instance *device,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200351 usb_device_event_t event, int data);
Wolfgang Denke2601822006-06-14 18:14:56 +0200352static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200353 struct urb *urb);
wdenk29e7f5a2004-03-12 00:14:09 +0000354static int usbtty_configured (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000355static int write_buffer (circbuf_t * buf);
356static int fill_buffer (circbuf_t * buf);
357
358void usbtty_poll (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000359
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200360/* utility function for converting char* to wide string used by USB */
361static void str2wide (char *str, u16 * wide)
362{
363 int i;
364 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200365 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200366 wide[i] = (u16) str[i];
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200367 #elif defined(__BIG_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200368 wide[i] = ((u16)(str[i])<<8);
369 #else
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200370 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200371 #endif
372 }
373}
wdenk29e7f5a2004-03-12 00:14:09 +0000374
375/*
376 * Test whether a character is in the RX buffer
377 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200378
Simon Glass0d1e1f72014-07-23 06:54:59 -0600379int usbtty_tstc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000380{
Wolfgang Denk3f0137b2006-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
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200398
Simon Glass0d1e1f72014-07-23 06:54:59 -0600399int usbtty_getc(struct stdio_dev *dev)
wdenk29e7f5a2004-03-12 00:14:09 +0000400{
401 char c;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200402 struct usb_endpoint_instance *endpoint =
403 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000404
405 while (usbtty_input.size <= 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200406 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk29e7f5a2004-03-12 00:14:09 +0000407 usbtty_poll ();
408 }
409
410 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200411 udc_set_nak(endpoint->endpoint_address&0x03);
412
wdenk29e7f5a2004-03-12 00:14:09 +0000413 return c;
414}
415
416/*
417 * Output a single byte to the usb client port.
418 */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600419void usbtty_putc(struct stdio_dev *dev, const char c)
wdenk29e7f5a2004-03-12 00:14:09 +0000420{
Atin Malaviya11a9df92009-02-03 15:17:10 -0500421 if (!usbtty_configured ())
422 return;
423
wdenk29e7f5a2004-03-12 00:14:09 +0000424 /* If \n, also do \r */
425 if (c == '\n')
426 buf_push (&usbtty_output, "\r", 1);
427
Alison Wang23e06b52016-03-02 11:00:37 +0800428 buf_push(&usbtty_output, &c, 1);
429
wdenk29e7f5a2004-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
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200449 * Output a string to the usb client port - implementing flow control
wdenk29e7f5a2004-03-12 00:14:09 +0000450 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200451
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200459 usbtty_poll ();
wdenk29e7f5a2004-03-12 00:14:09 +0000460
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200461 space = maxlen - usbtty_output.size;
wdenk29e7f5a2004-03-12 00:14:09 +0000462 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200463 if (space) {
wdenk29e7f5a2004-03-12 00:14:09 +0000464 write_buffer (&usbtty_output);
Wolfgang Denke2601822006-06-14 18:14:56 +0200465
Masahiro Yamadab62b39b2014-09-18 13:28:06 +0900466 n = min(space, min(len, maxlen));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200467 buf_push (&usbtty_output, str, n);
wdenk29e7f5a2004-03-12 00:14:09 +0000468
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200469 str += n;
Wolfgang Denke2601822006-06-14 18:14:56 +0200470 len -= n;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200471 }
wdenk29e7f5a2004-03-12 00:14:09 +0000472 }
473}
474
Simon Glass0d1e1f72014-07-23 06:54:59 -0600475void usbtty_puts(struct stdio_dev *dev, const char *str)
wdenk29e7f5a2004-03-12 00:14:09 +0000476{
477 int n;
Atin Malaviya11a9df92009-02-03 15:17:10 -0500478 int len;
479
480 if (!usbtty_configured ())
481 return;
wdenk29e7f5a2004-03-12 00:14:09 +0000482
Atin Malaviya11a9df92009-02-03 15:17:10 -0500483 len = strlen (str);
wdenk29e7f5a2004-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ár84567c32021-02-07 14:50:01 +0100489 __usbtty_puts(str, n);
490 __usbtty_puts("\r\n", 2);
wdenk29e7f5a2004-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;
wdenk05822bf2004-03-12 15:38:25 +0000511 char * sn;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200512 char * tt;
wdenk05822bf2004-03-12 15:38:25 +0000513 int snlen;
wdenkc35ba4e2004-03-14 22:25:36 +0000514
Heinrich Schuchardta8d4d312017-10-12 23:37:37 +0200515 /* Get serial number */
Simon Glass64b723f2017-08-03 12:22:12 -0600516 sn = env_get("serial#");
517 if (!sn)
wdenk05822bf2004-03-12 15:38:25 +0000518 sn = "000000000000";
wdenk05822bf2004-03-12 15:38:25 +0000519 snlen = strlen(sn);
520 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denk509cd072008-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));
wdenk05822bf2004-03-12 15:38:25 +0000523 snlen = sizeof(serial_number) - 1;
524 }
525 memcpy (serial_number, sn, snlen);
526 serial_number[snlen] = '\0';
wdenk29e7f5a2004-03-12 00:14:09 +0000527
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200528 /* Decide on which type of UDC device to be.
529 */
Simon Glass64b723f2017-08-03 12:22:12 -0600530 tt = env_get("usbtty");
531 if (!tt)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200532 tt = "generic";
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200533 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denke2601822006-06-14 18:14:56 +0200534
wdenk29e7f5a2004-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 Herbrechtsmeier49914b62011-10-17 17:22:49 +0200545 usbtty_init_endpoints ();
546
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200547 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk29e7f5a2004-03-12 00:14:09 +0000548 udc_connect (); /* Enable pullup for host detection */
549
wdenk29e7f5a2004-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-VILLARD2a7a0312009-05-16 12:14:54 +0200561 rc = stdio_register (&usbttydev);
wdenk29e7f5a2004-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 Denke2601822006-06-14 18:14:56 +0200570 usbtty_string_table[STR_LANG] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200571 (struct usb_string_descriptor*)wstrLang;
572
wdenk29e7f5a2004-03-12 00:14:09 +0000573 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200574 string->bLength = sizeof(wstrManufacturer);
wdenk29e7f5a2004-03-12 00:14:09 +0000575 string->bDescriptorType = USB_DT_STRING;
576 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200577 usbtty_string_table[STR_MANUFACTURER]=string;
578
wdenk29e7f5a2004-03-12 00:14:09 +0000579
580 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200581 string->bLength = sizeof(wstrProduct);
wdenk29e7f5a2004-03-12 00:14:09 +0000582 string->bDescriptorType = USB_DT_STRING;
583 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200584 usbtty_string_table[STR_PRODUCT]=string;
585
wdenk29e7f5a2004-03-12 00:14:09 +0000586
587 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200588 string->bLength = sizeof(serial_number);
wdenk29e7f5a2004-03-12 00:14:09 +0000589 string->bDescriptorType = USB_DT_STRING;
wdenk05822bf2004-03-12 15:38:25 +0000590 str2wide (serial_number, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200591 usbtty_string_table[STR_SERIAL]=string;
592
wdenk29e7f5a2004-03-12 00:14:09 +0000593
594 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200595 string->bLength = sizeof(wstrConfiguration);
wdenk29e7f5a2004-03-12 00:14:09 +0000596 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500597 str2wide (CFG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200598 usbtty_string_table[STR_CONFIG]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000599
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200600
601 string = (struct usb_string_descriptor *) wstrDataInterface;
602 string->bLength = sizeof(wstrDataInterface);
603 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500604 str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-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);
wdenk29e7f5a2004-03-12 00:14:09 +0000609 string->bDescriptorType = USB_DT_STRING;
Tom Rini364d0022023-01-10 11:19:45 -0500610 str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200611 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000612
613 /* Now, initialize the string table for ep0 handling */
614 usb_strings = usbtty_string_table;
Wolfgang Denke2601822006-06-14 18:14:56 +0200615}
wdenk29e7f5a2004-03-12 00:14:09 +0000616
Tom Rini73424522011-12-15 08:40:51 -0700617#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
618 &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
619
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200629 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk29e7f5a2004-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;
wdenk05822bf2004-03-12 15:38:25 +0000640 bus_instance->serial_number_str = serial_number;
wdenk29e7f5a2004-03-12 00:14:09 +0000641
642 /* configuration instance */
643 memset (config_instance, 0,
644 sizeof (struct usb_configuration_instance));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200645 config_instance->interfaces = interface_count;
646 config_instance->configuration_descriptor = configuration_descriptor;
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200677 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
wdenk29e7f5a2004-03-12 00:14:09 +0000678
wdenk29e7f5a2004-03-12 00:14:09 +0000679 endpoint_instance[i].rcv_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200680 ep_descriptor_ptrs[i - 1]->bmAttributes;
681
Tom Rini73424522011-12-15 08:40:51 -0700682 endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
Wolfgang Denke2601822006-06-14 18:14:56 +0200683
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200684 endpoint_instance[i].tx_attributes =
685 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000686
Tom Rini73424522011-12-15 08:40:51 -0700687 endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200688
wdenk29e7f5a2004-03-12 00:14:09 +0000689 endpoint_instance[i].tx_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200690 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-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 Denke2601822006-06-14 18:14:56 +0200713 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk29e7f5a2004-03-12 00:14:09 +0000714 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
715 }
716}
717
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200718/* usbtty_init_terminal_type
Wolfgang Denke2601822006-06-14 18:14:56 +0200719 *
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200725 /* CDC ACM */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200726 case 0:
727 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200728 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200729 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denke2601822006-06-14 18:14:56 +0200730 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200731 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200732 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200733 &acm_configuration_descriptors[0].data_endpoints[1];
734
735 /* Enumerate Device Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200736 device_descriptor.bDeviceClass =
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200744
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200758 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200759 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200760 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200761 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denke2601822006-06-14 18:14:56 +0200762 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200763 &gserial_configuration_descriptors[0].data_endpoints[2];
wdenk29e7f5a2004-03-12 00:14:09 +0000764
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200765 /* Enumerate Device Descriptor */
766 device_descriptor.bDeviceClass = 0xFF;
767 device_descriptor.idProduct =
768 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200774 configuration_descriptor =
Wolfgang Denk3f0137b2006-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/******************************************************************************/
wdenk29e7f5a2004-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 Denke2601822006-06-14 18:14:56 +0200823
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200824 struct usb_endpoint_instance *endpoint =
825 &endpoint_instance[tx_endpoint];
826 struct urb *current_urb = NULL;
wdenk29e7f5a2004-03-12 00:14:09 +0000827
Wolfgang Denke2601822006-06-14 18:14:56 +0200828 /* TX data still exists - send it now
829 */
Pali Rohár29825e92021-02-07 14:50:02 +0100830 if(endpoint->sent < endpoint->tx_urb->actual_length){
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200831 if(udc_endpoint_write (endpoint)){
832 /* Write pre-empted by RX */
833 return -1;
834 }
835 }
wdenk29e7f5a2004-03-12 00:14:09 +0000836
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200837 if (buf->size) {
wdenk29e7f5a2004-03-12 00:14:09 +0000838 char *dest;
839
840 int space_avail;
841 int popnum, popped;
842 int total = 0;
843
Wolfgang Denke2601822006-06-14 18:14:56 +0200844 /* Break buffer into urb sized pieces,
845 * and link each to the endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200846 */
wdenk29e7f5a2004-03-12 00:14:09 +0000847 while (buf->size > 0) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200848
Pali Rohár29825e92021-02-07 14:50:02 +0100849 current_urb = next_urb (device_instance, endpoint);
850
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200851 dest = (char*)current_urb->buffer +
wdenk29e7f5a2004-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 Yamadadb204642014-11-07 03:03:31 +0900857 popnum = min(space_avail, (int)buf->size);
wdenk29e7f5a2004-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 Denke2601822006-06-14 18:14:56 +0200867 /* If endpoint->last == 0, then transfers have
868 * not started on this endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200869 */
wdenk29e7f5a2004-03-12 00:14:09 +0000870 if (endpoint->last == 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200871 if(udc_endpoint_write (endpoint)){
872 /* Write pre-empted by RX */
873 return -1;
874 }
wdenk29e7f5a2004-03-12 00:14:09 +0000875 }
876
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200877 }/* end while */
wdenk29e7f5a2004-03-12 00:14:09 +0000878 return total;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200879 }
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200887 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000888
889 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200890 unsigned int nb = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000891 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200892 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk29e7f5a2004-03-12 00:14:09 +0000893
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200894 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk29e7f5a2004-03-12 00:14:09 +0000895
Wolfgang Denk3f0137b2006-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 Denke2601822006-06-14 18:14:56 +0200899
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200900 }
wdenk29e7f5a2004-03-12 00:14:09 +0000901 return nb;
902 }
wdenk29e7f5a2004-03-12 00:14:09 +0000903 return 0;
904}
905
906static int usbtty_configured (void)
907{
908 return usbtty_configured_flag;
909}
910
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200911/******************************************************************************/
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200933/******************************************************************************/
934
935int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
936{
937 switch (request->bRequest){
938
Wolfgang Denka1be4762008-05-20 16:00:29 +0200939 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200940 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200941 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200942 break;
943 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denke2601822006-06-14 18:14:56 +0200944 * per character */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200945 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200946 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk3f0137b2006-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 Denka1be4762008-05-20 16:00:29 +0200954 default:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200955 return 1;
956 }
957 return 0;
958}
wdenk29e7f5a2004-03-12 00:14:09 +0000959
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200960/******************************************************************************/
wdenk29e7f5a2004-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 Denk3f0137b2006-06-14 17:45:53 +0200970 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000971
Wolfgang Denke2601822006-06-14 18:14:56 +0200972 /* Write any output data to host buffer
973 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200974 */
wdenk29e7f5a2004-03-12 00:14:09 +0000975 if (usbtty_configured ()) {
976 write_buffer (&usbtty_output);
977 }
978
979 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200980 udc_irq();
Wolfgang Denke2601822006-06-14 18:14:56 +0200981
982 /* Check for new data from host..
983 * (do this after checking interrupts to get latest data)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200984 */
wdenk29e7f5a2004-03-12 00:14:09 +0000985 if (usbtty_configured ()) {
986 fill_buffer (&usbtty_input);
987 }
988
989 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200990 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000991
wdenk29e7f5a2004-03-12 00:14:09 +0000992}