Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Gerry Hamel, geh@ti.com, Texas Instruments |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 5 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 6 | * (C) Copyright 2006 |
| 7 | * Bryan O'Donoghue, bodonoghue@codehermit.ie |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Jean-Christophe PLAGNIOL-VILLARD | 6dfaee7 | 2008-12-07 09:45:35 +0100 | [diff] [blame] | 10 | #include <config.h> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 11 | #include <circbuf.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 23fe449 | 2019-11-14 12:57:22 -0700 | [diff] [blame] | 13 | #include <serial.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 14 | #include <stdio_dev.h> |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 15 | #include <asm/unaligned.h> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 16 | #include "usbtty.h" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 17 | #include "usb_cdc_acm.h" |
| 18 | #include "usbdescriptors.h" |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 19 | |
Jean-Christophe PLAGNIOL-VILLARD | 6dfaee7 | 2008-12-07 09:45:35 +0100 | [diff] [blame] | 20 | #ifdef DEBUG |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 21 | #define TTYDBG(fmt,args...)\ |
| 22 | serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 23 | #else |
| 24 | #define TTYDBG(fmt,args...) do{}while(0) |
| 25 | #endif |
| 26 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 27 | #if 1 |
| 28 | #define TTYERR(fmt,args...)\ |
| 29 | serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\ |
| 30 | __LINE__,##args) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 31 | #else |
| 32 | #define TTYERR(fmt,args...) do{}while(0) |
| 33 | #endif |
| 34 | |
| 35 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 36 | * 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 Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 47 | #define CFG_USBD_DATA_INTERFACE_STR "Bulk Data Interface" |
| 48 | #define CFG_USBD_CTRL_INTERFACE_STR "Control Interface" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 49 | |
| 50 | /* |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 51 | * Buffers to hold input and output data |
| 52 | */ |
Shiraz Hashim | 96decf6 | 2012-12-17 14:19:37 +0530 | [diff] [blame] | 53 | #define USBTTY_BUFFER_SIZE 2048 |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 54 | static circbuf_t usbtty_input; |
| 55 | static circbuf_t usbtty_output; |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * Instance variables |
| 60 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 61 | static struct stdio_dev usbttydev; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 62 | static struct usb_device_instance device_instance[1]; |
| 63 | static struct usb_bus_instance bus_instance[1]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 64 | static struct usb_configuration_instance config_instance[NUM_CONFIGS]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 65 | static struct usb_interface_instance interface_instance[MAX_INTERFACES]; |
| 66 | static struct usb_alternate_instance alternate_instance[MAX_INTERFACES]; |
| 67 | /* one extra for control endpoint */ |
| 68 | static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 71 | * Global flag |
| 72 | */ |
| 73 | int usbtty_configured_flag = 0; |
| 74 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 75 | /* |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 76 | * Serial number |
| 77 | */ |
| 78 | static char serial_number[16]; |
| 79 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 80 | |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 81 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 82 | * Descriptors, Strings, Local variables. |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 83 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 84 | |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 85 | /* defined and used by gadget/ep0.c */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 86 | extern struct usb_string_descriptor **usb_strings; |
| 87 | |
| 88 | /* Indicies, References */ |
| 89 | static unsigned short rx_endpoint = 0; |
| 90 | static unsigned short tx_endpoint = 0; |
| 91 | static unsigned short interface_count = 0; |
| 92 | static struct usb_string_descriptor *usbtty_string_table[STR_COUNT]; |
| 93 | |
| 94 | /* USB Descriptor Strings */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 95 | static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4}; |
| 96 | static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)]; |
| 97 | static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)]; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 98 | static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)]; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 99 | static u8 wstrConfiguration[2 + 2*(sizeof(CFG_USBD_CONFIGURATION_STR)-1)]; |
| 100 | static u8 wstrDataInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)]; |
| 101 | static u8 wstrCtrlInterface[2 + 2*(sizeof(CFG_USBD_DATA_INTERFACE_STR)-1)]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 102 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 103 | /* Standard USB Data Structures */ |
| 104 | static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES]; |
| 105 | static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS]; |
| 106 | static struct usb_configuration_descriptor *configuration_descriptor = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 107 | static struct usb_device_descriptor device_descriptor = { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 108 | .bLength = sizeof(struct usb_device_descriptor), |
| 109 | .bDescriptorType = USB_DT_DEVICE, |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 110 | .bcdUSB = cpu_to_le16(USB_BCD_VERSION), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 111 | .bDeviceSubClass = 0x00, |
| 112 | .bDeviceProtocol = 0x00, |
| 113 | .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE, |
| 114 | .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID), |
| 115 | .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE), |
| 116 | .iManufacturer = STR_MANUFACTURER, |
| 117 | .iProduct = STR_PRODUCT, |
| 118 | .iSerialNumber = STR_SERIAL, |
| 119 | .bNumConfigurations = NUM_CONFIGS |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 120 | }; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 121 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Static CDC ACM specific descriptors |
| 124 | */ |
| 125 | |
| 126 | struct acm_config_desc { |
| 127 | struct usb_configuration_descriptor configuration_desc; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 128 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 129 | /* Master Interface */ |
| 130 | struct usb_interface_descriptor interface_desc; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 131 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 132 | struct usb_class_header_function_descriptor usb_class_header; |
| 133 | struct usb_class_call_management_descriptor usb_class_call_mgt; |
| 134 | struct usb_class_abstract_control_descriptor usb_class_acm; |
| 135 | struct usb_class_union_function_descriptor usb_class_union; |
| 136 | struct usb_endpoint_descriptor notification_endpoint; |
| 137 | |
| 138 | /* Slave Interface */ |
| 139 | struct usb_interface_descriptor data_class_interface; |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 140 | struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 141 | } __attribute__((packed)); |
| 142 | |
| 143 | static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = { |
| 144 | { |
| 145 | .configuration_desc ={ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 146 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 147 | sizeof(struct usb_configuration_descriptor), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 148 | .bDescriptorType = USB_DT_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 149 | .wTotalLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 150 | cpu_to_le16(sizeof(struct acm_config_desc)), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 151 | .bNumInterfaces = NUM_ACM_INTERFACES, |
| 152 | .bConfigurationValue = 1, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 153 | .iConfiguration = STR_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 154 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 155 | BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED, |
| 156 | .bMaxPower = USBTTY_MAXPOWER |
| 157 | }, |
| 158 | /* Interface 1 */ |
| 159 | .interface_desc = { |
| 160 | .bLength = sizeof(struct usb_interface_descriptor), |
| 161 | .bDescriptorType = USB_DT_INTERFACE, |
| 162 | .bInterfaceNumber = 0, |
| 163 | .bAlternateSetting = 0, |
| 164 | .bNumEndpoints = 0x01, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 165 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 166 | COMMUNICATIONS_INTERFACE_CLASS_CONTROL, |
| 167 | .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS, |
| 168 | .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL, |
| 169 | .iInterface = STR_CTRL_INTERFACE, |
| 170 | }, |
| 171 | .usb_class_header = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 172 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 173 | sizeof(struct usb_class_header_function_descriptor), |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 174 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 175 | .bDescriptorSubtype = USB_ST_HEADER, |
| 176 | .bcdCDC = cpu_to_le16(110), |
| 177 | }, |
| 178 | .usb_class_call_mgt = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 179 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 180 | sizeof(struct usb_class_call_management_descriptor), |
| 181 | .bDescriptorType = CS_INTERFACE, |
| 182 | .bDescriptorSubtype = USB_ST_CMF, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 183 | .bmCapabilities = 0x00, |
| 184 | .bDataInterface = 0x01, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 185 | }, |
| 186 | .usb_class_acm = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 187 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 188 | sizeof(struct usb_class_abstract_control_descriptor), |
| 189 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 190 | .bDescriptorSubtype = USB_ST_ACMF, |
| 191 | .bmCapabilities = 0x00, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 192 | }, |
| 193 | .usb_class_union = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 194 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 195 | sizeof(struct usb_class_union_function_descriptor), |
| 196 | .bDescriptorType = CS_INTERFACE, |
| 197 | .bDescriptorSubtype = USB_ST_UF, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 198 | .bMasterInterface = 0x00, |
| 199 | .bSlaveInterface0 = 0x01, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 200 | }, |
| 201 | .notification_endpoint = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 202 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 203 | sizeof(struct usb_endpoint_descriptor), |
| 204 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 205 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 206 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 207 | .wMaxPacketSize |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 208 | = cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 209 | .bInterval = 0xFF, |
| 210 | }, |
| 211 | |
| 212 | /* Interface 2 */ |
| 213 | .data_class_interface = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 214 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 215 | sizeof(struct usb_interface_descriptor), |
| 216 | .bDescriptorType = USB_DT_INTERFACE, |
| 217 | .bInterfaceNumber = 0x01, |
| 218 | .bAlternateSetting = 0x00, |
| 219 | .bNumEndpoints = 0x02, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 220 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 221 | COMMUNICATIONS_INTERFACE_CLASS_DATA, |
| 222 | .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE, |
| 223 | .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE, |
| 224 | .iInterface = STR_DATA_INTERFACE, |
| 225 | }, |
| 226 | .data_endpoints = { |
| 227 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 228 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 229 | sizeof(struct usb_endpoint_descriptor), |
| 230 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 231 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 232 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 233 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 234 | .wMaxPacketSize = |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 235 | cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 236 | .bInterval = 0xFF, |
| 237 | }, |
| 238 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 239 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 240 | sizeof(struct usb_endpoint_descriptor), |
| 241 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 242 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 243 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 244 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 245 | .wMaxPacketSize = |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 246 | cpu_to_le16(CFG_USBD_SERIAL_BULK_PKTSIZE), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 247 | .bInterval = 0xFF, |
| 248 | }, |
| 249 | }, |
| 250 | }, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 251 | }; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 252 | |
| 253 | static struct rs232_emu rs232_desc={ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 254 | .dter = 115200, |
| 255 | .stop_bits = 0x00, |
| 256 | .parity = 0x00, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 257 | .data_bits = 0x08 |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 260 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 261 | /* |
| 262 | * Static Generic Serial specific data |
| 263 | */ |
| 264 | |
| 265 | |
| 266 | struct gserial_config_desc { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 267 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 268 | struct usb_configuration_descriptor configuration_desc; |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 269 | struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES]; |
| 270 | struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 271 | |
| 272 | } __attribute__((packed)); |
| 273 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 274 | static struct gserial_config_desc |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 275 | gserial_configuration_descriptors[NUM_CONFIGS] ={ |
| 276 | { |
| 277 | .configuration_desc ={ |
| 278 | .bLength = sizeof(struct usb_configuration_descriptor), |
| 279 | .bDescriptorType = USB_DT_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 280 | .wTotalLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 281 | cpu_to_le16(sizeof(struct gserial_config_desc)), |
| 282 | .bNumInterfaces = NUM_GSERIAL_INTERFACES, |
| 283 | .bConfigurationValue = 1, |
| 284 | .iConfiguration = STR_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 285 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 286 | BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED, |
| 287 | .bMaxPower = USBTTY_MAXPOWER |
| 288 | }, |
| 289 | .interface_desc = { |
| 290 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 291 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 292 | sizeof(struct usb_interface_descriptor), |
| 293 | .bDescriptorType = USB_DT_INTERFACE, |
| 294 | .bInterfaceNumber = 0, |
| 295 | .bAlternateSetting = 0, |
| 296 | .bNumEndpoints = NUM_ENDPOINTS, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 297 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 298 | COMMUNICATIONS_INTERFACE_CLASS_VENDOR, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 299 | .bInterfaceSubClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 300 | COMMUNICATIONS_NO_SUBCLASS, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 301 | .bInterfaceProtocol = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 302 | COMMUNICATIONS_NO_PROTOCOL, |
| 303 | .iInterface = STR_DATA_INTERFACE |
| 304 | }, |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 305 | }, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 306 | .data_endpoints = { |
| 307 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 308 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 309 | sizeof(struct usb_endpoint_descriptor), |
| 310 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 311 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 312 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 313 | .wMaxPacketSize = |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 314 | cpu_to_le16(CFG_USBD_SERIAL_OUT_PKTSIZE), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 315 | .bInterval= 0xFF, |
| 316 | }, |
| 317 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 318 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 319 | sizeof(struct usb_endpoint_descriptor), |
| 320 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 321 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 322 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 323 | .wMaxPacketSize = |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 324 | cpu_to_le16(CFG_USBD_SERIAL_IN_PKTSIZE), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 325 | .bInterval = 0xFF, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 326 | }, |
| 327 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 328 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 329 | sizeof(struct usb_endpoint_descriptor), |
| 330 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 331 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 332 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 333 | .wMaxPacketSize = |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 334 | cpu_to_le16(CFG_USBD_SERIAL_INT_PKTSIZE), |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 335 | .bInterval = 0xFF, |
| 336 | }, |
| 337 | }, |
| 338 | }, |
| 339 | }; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 342 | * Static Function Prototypes |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 343 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 344 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 345 | static void usbtty_init_strings (void); |
| 346 | static void usbtty_init_instances (void); |
| 347 | static void usbtty_init_endpoints (void); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 348 | static void usbtty_init_terminal_type(short type); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 349 | static void usbtty_event_handler (struct usb_device_instance *device, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 350 | usb_device_event_t event, int data); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 351 | static int usbtty_cdc_setup(struct usb_device_request *request, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 352 | struct urb *urb); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 353 | static int usbtty_configured (void); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 354 | static int write_buffer (circbuf_t * buf); |
| 355 | static int fill_buffer (circbuf_t * buf); |
| 356 | |
| 357 | void usbtty_poll (void); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 358 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 359 | /* utility function for converting char* to wide string used by USB */ |
| 360 | static void str2wide (char *str, u16 * wide) |
| 361 | { |
| 362 | int i; |
| 363 | for (i = 0; i < strlen (str) && str[i]; i++){ |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 364 | #if defined(__LITTLE_ENDIAN) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 365 | wide[i] = (u16) str[i]; |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 366 | #elif defined(__BIG_ENDIAN) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 367 | wide[i] = ((u16)(str[i])<<8); |
| 368 | #else |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 369 | #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 370 | #endif |
| 371 | } |
| 372 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * Test whether a character is in the RX buffer |
| 376 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 377 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 378 | int usbtty_tstc(struct stdio_dev *dev) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 379 | { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 380 | struct usb_endpoint_instance *endpoint = |
| 381 | &endpoint_instance[rx_endpoint]; |
| 382 | |
| 383 | /* If no input data exists, allow more RX to be accepted */ |
| 384 | if(usbtty_input.size <= 0){ |
| 385 | udc_unset_nak(endpoint->endpoint_address&0x03); |
| 386 | } |
| 387 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 388 | usbtty_poll (); |
| 389 | return (usbtty_input.size > 0); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Read a single byte from the usb client port. Returns 1 on success, 0 |
| 394 | * otherwise. When the function is succesfull, the character read is |
| 395 | * written into its argument c. |
| 396 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 397 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 398 | int usbtty_getc(struct stdio_dev *dev) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 399 | { |
| 400 | char c; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 401 | struct usb_endpoint_instance *endpoint = |
| 402 | &endpoint_instance[rx_endpoint]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 403 | |
| 404 | while (usbtty_input.size <= 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 405 | udc_unset_nak(endpoint->endpoint_address&0x03); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 406 | usbtty_poll (); |
| 407 | } |
| 408 | |
| 409 | buf_pop (&usbtty_input, &c, 1); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 410 | udc_set_nak(endpoint->endpoint_address&0x03); |
| 411 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 412 | return c; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Output a single byte to the usb client port. |
| 417 | */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 418 | void usbtty_putc(struct stdio_dev *dev, const char c) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 419 | { |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 420 | if (!usbtty_configured ()) |
| 421 | return; |
| 422 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 423 | /* If \n, also do \r */ |
| 424 | if (c == '\n') |
| 425 | buf_push (&usbtty_output, "\r", 1); |
| 426 | |
Alison Wang | 23e06b5 | 2016-03-02 11:00:37 +0800 | [diff] [blame] | 427 | buf_push(&usbtty_output, &c, 1); |
| 428 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 429 | /* Poll at end to handle new data... */ |
| 430 | if ((usbtty_output.size + 2) >= usbtty_output.totalsize) { |
| 431 | usbtty_poll (); |
| 432 | } |
| 433 | } |
| 434 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 435 | /* usbtty_puts() helper function for finding the next '\n' in a string */ |
| 436 | static int next_nl_pos (const char *s) |
| 437 | { |
| 438 | int i; |
| 439 | |
| 440 | for (i = 0; s[i] != '\0'; i++) { |
| 441 | if (s[i] == '\n') |
| 442 | return i; |
| 443 | } |
| 444 | return i; |
| 445 | } |
| 446 | |
| 447 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 448 | * Output a string to the usb client port - implementing flow control |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 449 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 450 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 451 | static void __usbtty_puts (const char *str, int len) |
| 452 | { |
| 453 | int maxlen = usbtty_output.totalsize; |
| 454 | int space, n; |
| 455 | |
| 456 | /* break str into chunks < buffer size, if needed */ |
| 457 | while (len > 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 458 | usbtty_poll (); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 459 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 460 | space = maxlen - usbtty_output.size; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 461 | /* Empty buffer here, if needed, to ensure space... */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 462 | if (space) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 463 | write_buffer (&usbtty_output); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 464 | |
Masahiro Yamada | b62b39b | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 465 | n = min(space, min(len, maxlen)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 466 | buf_push (&usbtty_output, str, n); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 467 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 468 | str += n; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 469 | len -= n; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 470 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 474 | void usbtty_puts(struct stdio_dev *dev, const char *str) |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 475 | { |
| 476 | int n; |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 477 | int len; |
| 478 | |
| 479 | if (!usbtty_configured ()) |
| 480 | return; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 481 | |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 482 | len = strlen (str); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 483 | /* add '\r' for each '\n' */ |
| 484 | while (len > 0) { |
| 485 | n = next_nl_pos (str); |
| 486 | |
| 487 | if (str[n] == '\n') { |
Pali Rohár | 84567c3 | 2021-02-07 14:50:01 +0100 | [diff] [blame] | 488 | __usbtty_puts(str, n); |
| 489 | __usbtty_puts("\r\n", 2); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 490 | str += (n + 1); |
| 491 | len -= (n + 1); |
| 492 | } else { |
| 493 | /* No \n found. All done. */ |
| 494 | __usbtty_puts (str, n); |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* Poll at end to handle new data... */ |
| 500 | usbtty_poll (); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Initialize the usb client port. |
| 505 | * |
| 506 | */ |
| 507 | int drv_usbtty_init (void) |
| 508 | { |
| 509 | int rc; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 510 | char * sn; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 511 | char * tt; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 512 | int snlen; |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 513 | |
Heinrich Schuchardt | a8d4d31 | 2017-10-12 23:37:37 +0200 | [diff] [blame] | 514 | /* Get serial number */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 515 | sn = env_get("serial#"); |
| 516 | if (!sn) |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 517 | sn = "000000000000"; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 518 | snlen = strlen(sn); |
| 519 | if (snlen > sizeof(serial_number) - 1) { |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 520 | printf ("Warning: serial number %s is too long (%d > %lu)\n", |
| 521 | sn, snlen, (ulong)(sizeof(serial_number) - 1)); |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 522 | snlen = sizeof(serial_number) - 1; |
| 523 | } |
| 524 | memcpy (serial_number, sn, snlen); |
| 525 | serial_number[snlen] = '\0'; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 526 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 527 | /* Decide on which type of UDC device to be. |
| 528 | */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 529 | tt = env_get("usbtty"); |
| 530 | if (!tt) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 531 | tt = "generic"; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 532 | usbtty_init_terminal_type(strcmp(tt,"cdc_acm")); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 533 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 534 | /* prepare buffers... */ |
| 535 | buf_init (&usbtty_input, USBTTY_BUFFER_SIZE); |
| 536 | buf_init (&usbtty_output, USBTTY_BUFFER_SIZE); |
| 537 | |
| 538 | /* Now, set up USB controller and infrastructure */ |
| 539 | udc_init (); /* Basic USB initialization */ |
| 540 | |
| 541 | usbtty_init_strings (); |
| 542 | usbtty_init_instances (); |
| 543 | |
Stefan Herbrechtsmeier | 49914b6 | 2011-10-17 17:22:49 +0200 | [diff] [blame] | 544 | usbtty_init_endpoints (); |
| 545 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 546 | udc_startup_events (device_instance);/* Enable dev, init udc pointers */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 547 | udc_connect (); /* Enable pullup for host detection */ |
| 548 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 549 | /* Device initialization */ |
| 550 | memset (&usbttydev, 0, sizeof (usbttydev)); |
| 551 | |
| 552 | strcpy (usbttydev.name, "usbtty"); |
| 553 | usbttydev.ext = 0; /* No extensions */ |
| 554 | usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT; |
| 555 | usbttydev.tstc = usbtty_tstc; /* 'tstc' function */ |
| 556 | usbttydev.getc = usbtty_getc; /* 'getc' function */ |
| 557 | usbttydev.putc = usbtty_putc; /* 'putc' function */ |
| 558 | usbttydev.puts = usbtty_puts; /* 'puts' function */ |
| 559 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 560 | rc = stdio_register (&usbttydev); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 561 | |
| 562 | return (rc == 0) ? 1 : rc; |
| 563 | } |
| 564 | |
| 565 | static void usbtty_init_strings (void) |
| 566 | { |
| 567 | struct usb_string_descriptor *string; |
| 568 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 569 | usbtty_string_table[STR_LANG] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 570 | (struct usb_string_descriptor*)wstrLang; |
| 571 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 572 | string = (struct usb_string_descriptor *) wstrManufacturer; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 573 | string->bLength = sizeof(wstrManufacturer); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 574 | string->bDescriptorType = USB_DT_STRING; |
| 575 | str2wide (CONFIG_USBD_MANUFACTURER, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 576 | usbtty_string_table[STR_MANUFACTURER]=string; |
| 577 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 578 | |
| 579 | string = (struct usb_string_descriptor *) wstrProduct; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 580 | string->bLength = sizeof(wstrProduct); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 581 | string->bDescriptorType = USB_DT_STRING; |
| 582 | str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 583 | usbtty_string_table[STR_PRODUCT]=string; |
| 584 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 585 | |
| 586 | string = (struct usb_string_descriptor *) wstrSerial; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 587 | string->bLength = sizeof(serial_number); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 588 | string->bDescriptorType = USB_DT_STRING; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 589 | str2wide (serial_number, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 590 | usbtty_string_table[STR_SERIAL]=string; |
| 591 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 592 | |
| 593 | string = (struct usb_string_descriptor *) wstrConfiguration; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 594 | string->bLength = sizeof(wstrConfiguration); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 595 | string->bDescriptorType = USB_DT_STRING; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 596 | str2wide (CFG_USBD_CONFIGURATION_STR, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 597 | usbtty_string_table[STR_CONFIG]=string; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 598 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 599 | |
| 600 | string = (struct usb_string_descriptor *) wstrDataInterface; |
| 601 | string->bLength = sizeof(wstrDataInterface); |
| 602 | string->bDescriptorType = USB_DT_STRING; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 603 | str2wide (CFG_USBD_DATA_INTERFACE_STR, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 604 | usbtty_string_table[STR_DATA_INTERFACE]=string; |
| 605 | |
| 606 | string = (struct usb_string_descriptor *) wstrCtrlInterface; |
| 607 | string->bLength = sizeof(wstrCtrlInterface); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 608 | string->bDescriptorType = USB_DT_STRING; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 609 | str2wide (CFG_USBD_CTRL_INTERFACE_STR, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 610 | usbtty_string_table[STR_CTRL_INTERFACE]=string; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 611 | |
| 612 | /* Now, initialize the string table for ep0 handling */ |
| 613 | usb_strings = usbtty_string_table; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 614 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 615 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 616 | #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\ |
| 617 | &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize)); |
| 618 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 619 | static void usbtty_init_instances (void) |
| 620 | { |
| 621 | int i; |
| 622 | |
| 623 | /* initialize device instance */ |
| 624 | memset (device_instance, 0, sizeof (struct usb_device_instance)); |
| 625 | device_instance->device_state = STATE_INIT; |
| 626 | device_instance->device_descriptor = &device_descriptor; |
| 627 | device_instance->event = usbtty_event_handler; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 628 | device_instance->cdc_recv_setup = usbtty_cdc_setup; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 629 | device_instance->bus = bus_instance; |
| 630 | device_instance->configurations = NUM_CONFIGS; |
| 631 | device_instance->configuration_instance_array = config_instance; |
| 632 | |
| 633 | /* initialize bus instance */ |
| 634 | memset (bus_instance, 0, sizeof (struct usb_bus_instance)); |
| 635 | bus_instance->device = device_instance; |
| 636 | bus_instance->endpoint_array = endpoint_instance; |
| 637 | bus_instance->max_endpoints = 1; |
| 638 | bus_instance->maxpacketsize = 64; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 639 | bus_instance->serial_number_str = serial_number; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 640 | |
| 641 | /* configuration instance */ |
| 642 | memset (config_instance, 0, |
| 643 | sizeof (struct usb_configuration_instance)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 644 | config_instance->interfaces = interface_count; |
| 645 | config_instance->configuration_descriptor = configuration_descriptor; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 646 | config_instance->interface_instance_array = interface_instance; |
| 647 | |
| 648 | /* interface instance */ |
| 649 | memset (interface_instance, 0, |
| 650 | sizeof (struct usb_interface_instance)); |
| 651 | interface_instance->alternates = 1; |
| 652 | interface_instance->alternates_instance_array = alternate_instance; |
| 653 | |
| 654 | /* alternates instance */ |
| 655 | memset (alternate_instance, 0, |
| 656 | sizeof (struct usb_alternate_instance)); |
| 657 | alternate_instance->interface_descriptor = interface_descriptors; |
| 658 | alternate_instance->endpoints = NUM_ENDPOINTS; |
| 659 | alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs; |
| 660 | |
| 661 | /* endpoint instances */ |
| 662 | memset (&endpoint_instance[0], 0, |
| 663 | sizeof (struct usb_endpoint_instance)); |
| 664 | endpoint_instance[0].endpoint_address = 0; |
| 665 | endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE; |
| 666 | endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL; |
| 667 | endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE; |
| 668 | endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL; |
| 669 | udc_setup_ep (device_instance, 0, &endpoint_instance[0]); |
| 670 | |
| 671 | for (i = 1; i <= NUM_ENDPOINTS; i++) { |
| 672 | memset (&endpoint_instance[i], 0, |
| 673 | sizeof (struct usb_endpoint_instance)); |
| 674 | |
| 675 | endpoint_instance[i].endpoint_address = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 676 | ep_descriptor_ptrs[i - 1]->bEndpointAddress; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 677 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 678 | endpoint_instance[i].rcv_attributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 679 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
| 680 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 681 | endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 682 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 683 | endpoint_instance[i].tx_attributes = |
| 684 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 685 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 686 | endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 687 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 688 | endpoint_instance[i].tx_attributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 689 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 690 | |
| 691 | urb_link_init (&endpoint_instance[i].rcv); |
| 692 | urb_link_init (&endpoint_instance[i].rdy); |
| 693 | urb_link_init (&endpoint_instance[i].tx); |
| 694 | urb_link_init (&endpoint_instance[i].done); |
| 695 | |
| 696 | if (endpoint_instance[i].endpoint_address & USB_DIR_IN) |
| 697 | endpoint_instance[i].tx_urb = |
| 698 | usbd_alloc_urb (device_instance, |
| 699 | &endpoint_instance[i]); |
| 700 | else |
| 701 | endpoint_instance[i].rcv_urb = |
| 702 | usbd_alloc_urb (device_instance, |
| 703 | &endpoint_instance[i]); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | static void usbtty_init_endpoints (void) |
| 708 | { |
| 709 | int i; |
| 710 | |
| 711 | bus_instance->max_endpoints = NUM_ENDPOINTS + 1; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 712 | for (i = 1; i <= NUM_ENDPOINTS; i++) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 713 | udc_setup_ep (device_instance, i, &endpoint_instance[i]); |
| 714 | } |
| 715 | } |
| 716 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 717 | /* usbtty_init_terminal_type |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 718 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 719 | * Do some late binding for our device type. |
| 720 | */ |
| 721 | static void usbtty_init_terminal_type(short type) |
| 722 | { |
| 723 | switch(type){ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 724 | /* CDC ACM */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 725 | case 0: |
| 726 | /* Assign endpoint descriptors */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 727 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 728 | &acm_configuration_descriptors[0].notification_endpoint; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 729 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 730 | &acm_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 731 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 732 | &acm_configuration_descriptors[0].data_endpoints[1]; |
| 733 | |
| 734 | /* Enumerate Device Descriptor */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 735 | device_descriptor.bDeviceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 736 | COMMUNICATIONS_DEVICE_CLASS; |
| 737 | device_descriptor.idProduct = |
| 738 | cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM); |
| 739 | |
| 740 | /* Assign endpoint indices */ |
| 741 | tx_endpoint = ACM_TX_ENDPOINT; |
| 742 | rx_endpoint = ACM_RX_ENDPOINT; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 743 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 744 | /* Configuration Descriptor */ |
| 745 | configuration_descriptor = |
| 746 | (struct usb_configuration_descriptor*) |
| 747 | &acm_configuration_descriptors; |
| 748 | |
| 749 | /* Interface count */ |
| 750 | interface_count = NUM_ACM_INTERFACES; |
| 751 | break; |
| 752 | |
| 753 | /* BULK IN/OUT & Default */ |
| 754 | case 1: |
| 755 | default: |
| 756 | /* Assign endpoint descriptors */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 757 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 758 | &gserial_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 759 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 760 | &gserial_configuration_descriptors[0].data_endpoints[1]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 761 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 762 | &gserial_configuration_descriptors[0].data_endpoints[2]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 763 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 764 | /* Enumerate Device Descriptor */ |
| 765 | device_descriptor.bDeviceClass = 0xFF; |
| 766 | device_descriptor.idProduct = |
| 767 | cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 768 | /* Assign endpoint indices */ |
| 769 | tx_endpoint = GSERIAL_TX_ENDPOINT; |
| 770 | rx_endpoint = GSERIAL_RX_ENDPOINT; |
| 771 | |
| 772 | /* Configuration Descriptor */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 773 | configuration_descriptor = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 774 | (struct usb_configuration_descriptor*) |
| 775 | &gserial_configuration_descriptors; |
| 776 | |
| 777 | /* Interface count */ |
| 778 | interface_count = NUM_GSERIAL_INTERFACES; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 784 | |
| 785 | static struct urb *next_urb (struct usb_device_instance *device, |
| 786 | struct usb_endpoint_instance *endpoint) |
| 787 | { |
| 788 | struct urb *current_urb = NULL; |
| 789 | int space; |
| 790 | |
| 791 | /* If there's a queue, then we should add to the last urb */ |
| 792 | if (!endpoint->tx_queue) { |
| 793 | current_urb = endpoint->tx_urb; |
| 794 | } else { |
| 795 | /* Last urb from tx chain */ |
| 796 | current_urb = |
| 797 | p2surround (struct urb, link, endpoint->tx.prev); |
| 798 | } |
| 799 | |
| 800 | /* Make sure this one has enough room */ |
| 801 | space = current_urb->buffer_length - current_urb->actual_length; |
| 802 | if (space > 0) { |
| 803 | return current_urb; |
| 804 | } else { /* No space here */ |
| 805 | /* First look at done list */ |
| 806 | current_urb = first_urb_detached (&endpoint->done); |
| 807 | if (!current_urb) { |
| 808 | current_urb = usbd_alloc_urb (device, endpoint); |
| 809 | } |
| 810 | |
| 811 | urb_append (&endpoint->tx, current_urb); |
| 812 | endpoint->tx_queue++; |
| 813 | } |
| 814 | return current_urb; |
| 815 | } |
| 816 | |
| 817 | static int write_buffer (circbuf_t * buf) |
| 818 | { |
| 819 | if (!usbtty_configured ()) { |
| 820 | return 0; |
| 821 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 822 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 823 | struct usb_endpoint_instance *endpoint = |
| 824 | &endpoint_instance[tx_endpoint]; |
| 825 | struct urb *current_urb = NULL; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 826 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 827 | /* TX data still exists - send it now |
| 828 | */ |
Pali Rohár | 29825e9 | 2021-02-07 14:50:02 +0100 | [diff] [blame] | 829 | if(endpoint->sent < endpoint->tx_urb->actual_length){ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 830 | if(udc_endpoint_write (endpoint)){ |
| 831 | /* Write pre-empted by RX */ |
| 832 | return -1; |
| 833 | } |
| 834 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 835 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 836 | if (buf->size) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 837 | char *dest; |
| 838 | |
| 839 | int space_avail; |
| 840 | int popnum, popped; |
| 841 | int total = 0; |
| 842 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 843 | /* Break buffer into urb sized pieces, |
| 844 | * and link each to the endpoint |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 845 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 846 | while (buf->size > 0) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 847 | |
Pali Rohár | 29825e9 | 2021-02-07 14:50:02 +0100 | [diff] [blame] | 848 | current_urb = next_urb (device_instance, endpoint); |
| 849 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 850 | dest = (char*)current_urb->buffer + |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 851 | current_urb->actual_length; |
| 852 | |
| 853 | space_avail = |
| 854 | current_urb->buffer_length - |
| 855 | current_urb->actual_length; |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 856 | popnum = min(space_avail, (int)buf->size); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 857 | if (popnum == 0) |
| 858 | break; |
| 859 | |
| 860 | popped = buf_pop (buf, dest, popnum); |
| 861 | if (popped == 0) |
| 862 | break; |
| 863 | current_urb->actual_length += popped; |
| 864 | total += popped; |
| 865 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 866 | /* If endpoint->last == 0, then transfers have |
| 867 | * not started on this endpoint |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 868 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 869 | if (endpoint->last == 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 870 | if(udc_endpoint_write (endpoint)){ |
| 871 | /* Write pre-empted by RX */ |
| 872 | return -1; |
| 873 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 876 | }/* end while */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 877 | return total; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 878 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | static int fill_buffer (circbuf_t * buf) |
| 884 | { |
| 885 | struct usb_endpoint_instance *endpoint = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 886 | &endpoint_instance[rx_endpoint]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 887 | |
| 888 | if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 889 | unsigned int nb = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 890 | char *src = (char *) endpoint->rcv_urb->buffer; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 891 | unsigned int rx_avail = buf->totalsize - buf->size; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 892 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 893 | if(rx_avail >= endpoint->rcv_urb->actual_length){ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 894 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 895 | nb = endpoint->rcv_urb->actual_length; |
| 896 | buf_push (buf, src, nb); |
| 897 | endpoint->rcv_urb->actual_length = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 898 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 899 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 900 | return nb; |
| 901 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static int usbtty_configured (void) |
| 906 | { |
| 907 | return usbtty_configured_flag; |
| 908 | } |
| 909 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 910 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 911 | |
| 912 | static void usbtty_event_handler (struct usb_device_instance *device, |
| 913 | usb_device_event_t event, int data) |
| 914 | { |
| 915 | switch (event) { |
| 916 | case DEVICE_RESET: |
| 917 | case DEVICE_BUS_INACTIVE: |
| 918 | usbtty_configured_flag = 0; |
| 919 | break; |
| 920 | case DEVICE_CONFIGURED: |
| 921 | usbtty_configured_flag = 1; |
| 922 | break; |
| 923 | |
| 924 | case DEVICE_ADDRESS_ASSIGNED: |
| 925 | usbtty_init_endpoints (); |
| 926 | |
| 927 | default: |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 932 | /******************************************************************************/ |
| 933 | |
| 934 | int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb) |
| 935 | { |
| 936 | switch (request->bRequest){ |
| 937 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 938 | case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 939 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 940 | case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 941 | break; |
| 942 | case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 943 | * per character */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 944 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 945 | case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 946 | break; |
| 947 | case ACM_GET_LINE_ENCODING : /* request DTE rate, |
| 948 | * stop/parity bits */ |
| 949 | memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc)); |
| 950 | urb->actual_length = sizeof(rs232_desc); |
| 951 | |
| 952 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 953 | default: |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 954 | return 1; |
| 955 | } |
| 956 | return 0; |
| 957 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 958 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 959 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 960 | |
| 961 | /* |
| 962 | * Since interrupt handling has not yet been implemented, we use this function |
| 963 | * to handle polling. This is called by the tstc,getc,putc,puts routines to |
| 964 | * update the USB state. |
| 965 | */ |
| 966 | void usbtty_poll (void) |
| 967 | { |
| 968 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 969 | udc_irq(); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 970 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 971 | /* Write any output data to host buffer |
| 972 | * (do this before checking interrupts to avoid missing one) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 973 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 974 | if (usbtty_configured ()) { |
| 975 | write_buffer (&usbtty_output); |
| 976 | } |
| 977 | |
| 978 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 979 | udc_irq(); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 980 | |
| 981 | /* Check for new data from host.. |
| 982 | * (do this after checking interrupts to get latest data) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 983 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 984 | if (usbtty_configured ()) { |
| 985 | fill_buffer (&usbtty_input); |
| 986 | } |
| 987 | |
| 988 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 989 | udc_irq(); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 990 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 991 | } |