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