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