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