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