wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Gerry Hamel, geh@ti.com, Texas Instruments |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 4 | * |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * Bryan O'Donoghue, bodonoghue@codehermit.ie |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | dedacc1 | 2008-12-07 09:45:35 +0100 | [diff] [blame] | 12 | #include <config.h> |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 13 | #include <circbuf.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 14 | #include <stdio_dev.h> |
Tom Rini | b2fb47f | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 15 | #include <asm/unaligned.h> |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 16 | #include "usbtty.h" |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 17 | #include "usb_cdc_acm.h" |
| 18 | #include "usbdescriptors.h" |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 19 | |
Jean-Christophe PLAGNIOL-VILLARD | dedacc1 | 2008-12-07 09:45:35 +0100 | [diff] [blame] | 20 | #ifdef DEBUG |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 21 | #define TTYDBG(fmt,args...)\ |
| 22 | serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 23 | #else |
| 24 | #define TTYDBG(fmt,args...) do{}while(0) |
| 25 | #endif |
| 26 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 27 | #if 1 |
| 28 | #define TTYERR(fmt,args...)\ |
| 29 | serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\ |
| 30 | __LINE__,##args) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 31 | #else |
| 32 | #define TTYERR(fmt,args...) do{}while(0) |
| 33 | #endif |
| 34 | |
| 35 | /* |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 36 | * Defines |
| 37 | */ |
| 38 | #define NUM_CONFIGS 1 |
| 39 | #define MAX_INTERFACES 2 |
| 40 | #define NUM_ENDPOINTS 3 |
| 41 | #define ACM_TX_ENDPOINT 3 |
| 42 | #define ACM_RX_ENDPOINT 2 |
| 43 | #define GSERIAL_TX_ENDPOINT 2 |
| 44 | #define GSERIAL_RX_ENDPOINT 1 |
| 45 | #define NUM_ACM_INTERFACES 2 |
| 46 | #define NUM_GSERIAL_INTERFACES 1 |
| 47 | #define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface" |
| 48 | #define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface" |
| 49 | |
| 50 | /* |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 51 | * Buffers to hold input and output data |
| 52 | */ |
Shiraz Hashim | b2caefb | 2012-12-17 14:19:37 +0530 | [diff] [blame] | 53 | #define USBTTY_BUFFER_SIZE 2048 |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 54 | static circbuf_t usbtty_input; |
| 55 | static circbuf_t usbtty_output; |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * Instance variables |
| 60 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 61 | static struct stdio_dev usbttydev; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 62 | static struct usb_device_instance device_instance[1]; |
| 63 | static struct usb_bus_instance bus_instance[1]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 64 | static struct usb_configuration_instance config_instance[NUM_CONFIGS]; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 65 | static struct usb_interface_instance interface_instance[MAX_INTERFACES]; |
| 66 | static struct usb_alternate_instance alternate_instance[MAX_INTERFACES]; |
| 67 | /* one extra for control endpoint */ |
| 68 | static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Global flag |
| 72 | */ |
| 73 | int usbtty_configured_flag = 0; |
| 74 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 75 | /* |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 76 | * Serial number |
| 77 | */ |
| 78 | static char serial_number[16]; |
| 79 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 80 | |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 81 | /* |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 82 | * Descriptors, Strings, Local variables. |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 83 | */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 84 | |
Jean-Christophe PLAGNIOL-VILLARD | 2731b9a | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 85 | /* defined and used by gadget/ep0.c */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 86 | extern struct usb_string_descriptor **usb_strings; |
| 87 | |
| 88 | /* Indicies, References */ |
| 89 | static unsigned short rx_endpoint = 0; |
| 90 | static unsigned short tx_endpoint = 0; |
| 91 | static unsigned short interface_count = 0; |
| 92 | static struct usb_string_descriptor *usbtty_string_table[STR_COUNT]; |
| 93 | |
| 94 | /* USB Descriptor Strings */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 95 | static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4}; |
| 96 | static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)]; |
| 97 | static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)]; |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 98 | static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 99 | static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)]; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 100 | static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)]; |
| 101 | static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 102 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 103 | /* Standard USB Data Structures */ |
| 104 | static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES]; |
| 105 | static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS]; |
| 106 | static struct usb_configuration_descriptor *configuration_descriptor = 0; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 107 | static struct usb_device_descriptor device_descriptor = { |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 108 | .bLength = sizeof(struct usb_device_descriptor), |
| 109 | .bDescriptorType = USB_DT_DEVICE, |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 110 | .bcdUSB = cpu_to_le16(USB_BCD_VERSION), |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 111 | .bDeviceSubClass = 0x00, |
| 112 | .bDeviceProtocol = 0x00, |
| 113 | .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE, |
| 114 | .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID), |
| 115 | .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE), |
| 116 | .iManufacturer = STR_MANUFACTURER, |
| 117 | .iProduct = STR_PRODUCT, |
| 118 | .iSerialNumber = STR_SERIAL, |
| 119 | .bNumConfigurations = NUM_CONFIGS |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 120 | }; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 121 | |
| 122 | |
Vipin KUMAR | f9da0f8 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 123 | #if defined(CONFIG_USBD_HS) |
| 124 | static struct usb_qualifier_descriptor qualifier_descriptor = { |
| 125 | .bLength = sizeof(struct usb_qualifier_descriptor), |
| 126 | .bDescriptorType = USB_DT_QUAL, |
| 127 | .bcdUSB = cpu_to_le16(USB_BCD_VERSION), |
| 128 | .bDeviceClass = COMMUNICATIONS_DEVICE_CLASS, |
| 129 | .bDeviceSubClass = 0x00, |
| 130 | .bDeviceProtocol = 0x00, |
| 131 | .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE, |
| 132 | .bNumConfigurations = NUM_CONFIGS |
| 133 | }; |
| 134 | #endif |
| 135 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Static CDC ACM specific descriptors |
| 138 | */ |
| 139 | |
| 140 | struct acm_config_desc { |
| 141 | struct usb_configuration_descriptor configuration_desc; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 142 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 143 | /* Master Interface */ |
| 144 | struct usb_interface_descriptor interface_desc; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 145 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 146 | struct usb_class_header_function_descriptor usb_class_header; |
| 147 | struct usb_class_call_management_descriptor usb_class_call_mgt; |
| 148 | struct usb_class_abstract_control_descriptor usb_class_acm; |
| 149 | struct usb_class_union_function_descriptor usb_class_union; |
| 150 | struct usb_endpoint_descriptor notification_endpoint; |
| 151 | |
| 152 | /* Slave Interface */ |
| 153 | struct usb_interface_descriptor data_class_interface; |
Atin Malaviya | f3c0de6 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 154 | struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1]; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 155 | } __attribute__((packed)); |
| 156 | |
| 157 | static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = { |
| 158 | { |
| 159 | .configuration_desc ={ |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 160 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 161 | sizeof(struct usb_configuration_descriptor), |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 162 | .bDescriptorType = USB_DT_CONFIG, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 163 | .wTotalLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 164 | cpu_to_le16(sizeof(struct acm_config_desc)), |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 165 | .bNumInterfaces = NUM_ACM_INTERFACES, |
| 166 | .bConfigurationValue = 1, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 167 | .iConfiguration = STR_CONFIG, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 168 | .bmAttributes = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 169 | BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED, |
| 170 | .bMaxPower = USBTTY_MAXPOWER |
| 171 | }, |
| 172 | /* Interface 1 */ |
| 173 | .interface_desc = { |
| 174 | .bLength = sizeof(struct usb_interface_descriptor), |
| 175 | .bDescriptorType = USB_DT_INTERFACE, |
| 176 | .bInterfaceNumber = 0, |
| 177 | .bAlternateSetting = 0, |
| 178 | .bNumEndpoints = 0x01, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 179 | .bInterfaceClass = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 180 | COMMUNICATIONS_INTERFACE_CLASS_CONTROL, |
| 181 | .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS, |
| 182 | .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL, |
| 183 | .iInterface = STR_CTRL_INTERFACE, |
| 184 | }, |
| 185 | .usb_class_header = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 186 | .bFunctionLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 187 | sizeof(struct usb_class_header_function_descriptor), |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 188 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 189 | .bDescriptorSubtype = USB_ST_HEADER, |
| 190 | .bcdCDC = cpu_to_le16(110), |
| 191 | }, |
| 192 | .usb_class_call_mgt = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 193 | .bFunctionLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 194 | sizeof(struct usb_class_call_management_descriptor), |
| 195 | .bDescriptorType = CS_INTERFACE, |
| 196 | .bDescriptorSubtype = USB_ST_CMF, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 197 | .bmCapabilities = 0x00, |
| 198 | .bDataInterface = 0x01, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 199 | }, |
| 200 | .usb_class_acm = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 201 | .bFunctionLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 202 | sizeof(struct usb_class_abstract_control_descriptor), |
| 203 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 204 | .bDescriptorSubtype = USB_ST_ACMF, |
| 205 | .bmCapabilities = 0x00, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 206 | }, |
| 207 | .usb_class_union = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 208 | .bFunctionLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 209 | sizeof(struct usb_class_union_function_descriptor), |
| 210 | .bDescriptorType = CS_INTERFACE, |
| 211 | .bDescriptorSubtype = USB_ST_UF, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 212 | .bMasterInterface = 0x00, |
| 213 | .bSlaveInterface0 = 0x01, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 214 | }, |
| 215 | .notification_endpoint = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 216 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 217 | sizeof(struct usb_endpoint_descriptor), |
| 218 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 219 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 220 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 221 | .wMaxPacketSize |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 222 | = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE), |
| 223 | .bInterval = 0xFF, |
| 224 | }, |
| 225 | |
| 226 | /* Interface 2 */ |
| 227 | .data_class_interface = { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 228 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 229 | sizeof(struct usb_interface_descriptor), |
| 230 | .bDescriptorType = USB_DT_INTERFACE, |
| 231 | .bInterfaceNumber = 0x01, |
| 232 | .bAlternateSetting = 0x00, |
| 233 | .bNumEndpoints = 0x02, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 234 | .bInterfaceClass = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 235 | COMMUNICATIONS_INTERFACE_CLASS_DATA, |
| 236 | .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE, |
| 237 | .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE, |
| 238 | .iInterface = STR_DATA_INTERFACE, |
| 239 | }, |
| 240 | .data_endpoints = { |
| 241 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 242 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 243 | sizeof(struct usb_endpoint_descriptor), |
| 244 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 245 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 246 | .bmAttributes = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 247 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 248 | .wMaxPacketSize = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 249 | cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE), |
| 250 | .bInterval = 0xFF, |
| 251 | }, |
| 252 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 253 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 254 | sizeof(struct usb_endpoint_descriptor), |
| 255 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 256 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 257 | .bmAttributes = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 258 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 259 | .wMaxPacketSize = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 260 | cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE), |
| 261 | .bInterval = 0xFF, |
| 262 | }, |
| 263 | }, |
| 264 | }, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 265 | }; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 266 | |
| 267 | static struct rs232_emu rs232_desc={ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 268 | .dter = 115200, |
| 269 | .stop_bits = 0x00, |
| 270 | .parity = 0x00, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 271 | .data_bits = 0x08 |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 272 | }; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 273 | |
| 274 | |
| 275 | /* |
| 276 | * Static Generic Serial specific data |
| 277 | */ |
| 278 | |
| 279 | |
| 280 | struct gserial_config_desc { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 281 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 282 | struct usb_configuration_descriptor configuration_desc; |
Atin Malaviya | f3c0de6 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 283 | struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES]; |
| 284 | struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS]; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 285 | |
| 286 | } __attribute__((packed)); |
| 287 | |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 288 | static struct gserial_config_desc |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 289 | gserial_configuration_descriptors[NUM_CONFIGS] ={ |
| 290 | { |
| 291 | .configuration_desc ={ |
| 292 | .bLength = sizeof(struct usb_configuration_descriptor), |
| 293 | .bDescriptorType = USB_DT_CONFIG, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 294 | .wTotalLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 295 | cpu_to_le16(sizeof(struct gserial_config_desc)), |
| 296 | .bNumInterfaces = NUM_GSERIAL_INTERFACES, |
| 297 | .bConfigurationValue = 1, |
| 298 | .iConfiguration = STR_CONFIG, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 299 | .bmAttributes = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 300 | BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED, |
| 301 | .bMaxPower = USBTTY_MAXPOWER |
| 302 | }, |
| 303 | .interface_desc = { |
| 304 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 305 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 306 | sizeof(struct usb_interface_descriptor), |
| 307 | .bDescriptorType = USB_DT_INTERFACE, |
| 308 | .bInterfaceNumber = 0, |
| 309 | .bAlternateSetting = 0, |
| 310 | .bNumEndpoints = NUM_ENDPOINTS, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 311 | .bInterfaceClass = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 312 | COMMUNICATIONS_INTERFACE_CLASS_VENDOR, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 313 | .bInterfaceSubClass = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 314 | COMMUNICATIONS_NO_SUBCLASS, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 315 | .bInterfaceProtocol = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 316 | COMMUNICATIONS_NO_PROTOCOL, |
| 317 | .iInterface = STR_DATA_INTERFACE |
| 318 | }, |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 319 | }, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 320 | .data_endpoints = { |
| 321 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 322 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 323 | sizeof(struct usb_endpoint_descriptor), |
| 324 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 325 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 326 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 327 | .wMaxPacketSize = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 328 | cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE), |
| 329 | .bInterval= 0xFF, |
| 330 | }, |
| 331 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 332 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 333 | sizeof(struct usb_endpoint_descriptor), |
| 334 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 335 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 336 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 337 | .wMaxPacketSize = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 338 | cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE), |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 339 | .bInterval = 0xFF, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 340 | }, |
| 341 | { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 342 | .bLength = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 343 | sizeof(struct usb_endpoint_descriptor), |
| 344 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 9e78dae | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 345 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 346 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 347 | .wMaxPacketSize = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 348 | cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE), |
| 349 | .bInterval = 0xFF, |
| 350 | }, |
| 351 | }, |
| 352 | }, |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 353 | }; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * Static Function Prototypes |
| 357 | */ |
| 358 | |
| 359 | static void usbtty_init_strings (void); |
| 360 | static void usbtty_init_instances (void); |
| 361 | static void usbtty_init_endpoints (void); |
| 362 | static void usbtty_init_terminal_type(short type); |
| 363 | static void usbtty_event_handler (struct usb_device_instance *device, |
| 364 | usb_device_event_t event, int data); |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 365 | static int usbtty_cdc_setup(struct usb_device_request *request, |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 366 | struct urb *urb); |
| 367 | static int usbtty_configured (void); |
| 368 | static int write_buffer (circbuf_t * buf); |
| 369 | static int fill_buffer (circbuf_t * buf); |
| 370 | |
| 371 | void usbtty_poll (void); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 372 | |
| 373 | /* utility function for converting char* to wide string used by USB */ |
| 374 | static void str2wide (char *str, u16 * wide) |
| 375 | { |
| 376 | int i; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 377 | for (i = 0; i < strlen (str) && str[i]; i++){ |
Rodolfo Giometti | 1813512 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 378 | #if defined(__LITTLE_ENDIAN) |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 379 | wide[i] = (u16) str[i]; |
Rodolfo Giometti | 1813512 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 380 | #elif defined(__BIG_ENDIAN) |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 381 | wide[i] = ((u16)(str[i])<<8); |
| 382 | #else |
Rodolfo Giometti | 1813512 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 383 | #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined" |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 384 | #endif |
| 385 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 389 | * Test whether a character is in the RX buffer |
| 390 | */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 391 | |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 392 | int usbtty_tstc(struct stdio_dev *dev) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 393 | { |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 394 | struct usb_endpoint_instance *endpoint = |
| 395 | &endpoint_instance[rx_endpoint]; |
| 396 | |
| 397 | /* If no input data exists, allow more RX to be accepted */ |
| 398 | if(usbtty_input.size <= 0){ |
| 399 | udc_unset_nak(endpoint->endpoint_address&0x03); |
| 400 | } |
| 401 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 402 | usbtty_poll (); |
| 403 | return (usbtty_input.size > 0); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Read a single byte from the usb client port. Returns 1 on success, 0 |
| 408 | * otherwise. When the function is succesfull, the character read is |
| 409 | * written into its argument c. |
| 410 | */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 411 | |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 412 | int usbtty_getc(struct stdio_dev *dev) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 413 | { |
| 414 | char c; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 415 | struct usb_endpoint_instance *endpoint = |
| 416 | &endpoint_instance[rx_endpoint]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 417 | |
| 418 | while (usbtty_input.size <= 0) { |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 419 | udc_unset_nak(endpoint->endpoint_address&0x03); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 420 | usbtty_poll (); |
| 421 | } |
| 422 | |
| 423 | buf_pop (&usbtty_input, &c, 1); |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 424 | udc_set_nak(endpoint->endpoint_address&0x03); |
| 425 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 426 | return c; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Output a single byte to the usb client port. |
| 431 | */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 432 | void usbtty_putc(struct stdio_dev *dev, const char c) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 433 | { |
Atin Malaviya | f3c0de6 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 434 | if (!usbtty_configured ()) |
| 435 | return; |
| 436 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 437 | /* If \n, also do \r */ |
| 438 | if (c == '\n') |
| 439 | buf_push (&usbtty_output, "\r", 1); |
| 440 | |
Alison Wang | 055457e | 2016-03-02 11:00:37 +0800 | [diff] [blame] | 441 | buf_push(&usbtty_output, &c, 1); |
| 442 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 443 | /* Poll at end to handle new data... */ |
| 444 | if ((usbtty_output.size + 2) >= usbtty_output.totalsize) { |
| 445 | usbtty_poll (); |
| 446 | } |
| 447 | } |
| 448 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 449 | /* usbtty_puts() helper function for finding the next '\n' in a string */ |
| 450 | static int next_nl_pos (const char *s) |
| 451 | { |
| 452 | int i; |
| 453 | |
| 454 | for (i = 0; s[i] != '\0'; i++) { |
| 455 | if (s[i] == '\n') |
| 456 | return i; |
| 457 | } |
| 458 | return i; |
| 459 | } |
| 460 | |
| 461 | /* |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 462 | * Output a string to the usb client port - implementing flow control |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 463 | */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 464 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 465 | static void __usbtty_puts (const char *str, int len) |
| 466 | { |
| 467 | int maxlen = usbtty_output.totalsize; |
| 468 | int space, n; |
| 469 | |
| 470 | /* break str into chunks < buffer size, if needed */ |
| 471 | while (len > 0) { |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 472 | usbtty_poll (); |
| 473 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 474 | space = maxlen - usbtty_output.size; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 475 | /* Empty buffer here, if needed, to ensure space... */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 476 | if (space) { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 477 | write_buffer (&usbtty_output); |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 478 | |
Masahiro Yamada | c79cba3 | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 479 | n = min(space, min(len, maxlen)); |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 480 | buf_push (&usbtty_output, str, n); |
| 481 | |
| 482 | str += n; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 483 | len -= n; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 484 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 488 | void usbtty_puts(struct stdio_dev *dev, const char *str) |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 489 | { |
| 490 | int n; |
Atin Malaviya | f3c0de6 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 491 | int len; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 492 | |
Atin Malaviya | f3c0de6 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 493 | if (!usbtty_configured ()) |
| 494 | return; |
| 495 | |
| 496 | len = strlen (str); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 497 | /* add '\r' for each '\n' */ |
| 498 | while (len > 0) { |
| 499 | n = next_nl_pos (str); |
| 500 | |
| 501 | if (str[n] == '\n') { |
Alison Wang | 055457e | 2016-03-02 11:00:37 +0800 | [diff] [blame] | 502 | __usbtty_puts("\r", 1); |
| 503 | __usbtty_puts(str, n + 1); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 504 | str += (n + 1); |
| 505 | len -= (n + 1); |
| 506 | } else { |
| 507 | /* No \n found. All done. */ |
| 508 | __usbtty_puts (str, n); |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* Poll at end to handle new data... */ |
| 514 | usbtty_poll (); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Initialize the usb client port. |
| 519 | * |
| 520 | */ |
| 521 | int drv_usbtty_init (void) |
| 522 | { |
| 523 | int rc; |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 524 | char * sn; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 525 | char * tt; |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 526 | int snlen; |
wdenk | 42dfe7a | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 527 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 528 | /* Ger seiral number */ |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 529 | if (!(sn = getenv("serial#"))) { |
| 530 | sn = "000000000000"; |
| 531 | } |
| 532 | snlen = strlen(sn); |
| 533 | if (snlen > sizeof(serial_number) - 1) { |
Wolfgang Denk | b64f190 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 534 | printf ("Warning: serial number %s is too long (%d > %lu)\n", |
| 535 | sn, snlen, (ulong)(sizeof(serial_number) - 1)); |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 536 | snlen = sizeof(serial_number) - 1; |
| 537 | } |
| 538 | memcpy (serial_number, sn, snlen); |
| 539 | serial_number[snlen] = '\0'; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 540 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 541 | /* Decide on which type of UDC device to be. |
| 542 | */ |
| 543 | |
| 544 | if(!(tt = getenv("usbtty"))) { |
| 545 | tt = "generic"; |
| 546 | } |
| 547 | usbtty_init_terminal_type(strcmp(tt,"cdc_acm")); |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 548 | |
wdenk | 232c150 | 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 | 241d9a6 | 2011-10-17 17:22:49 +0200 | [diff] [blame] | 559 | usbtty_init_endpoints (); |
| 560 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 561 | udc_startup_events (device_instance);/* Enable dev, init udc pointers */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 562 | udc_connect (); /* Enable pullup for host detection */ |
| 563 | |
wdenk | 232c150 | 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 | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 575 | rc = stdio_register (&usbttydev); |
wdenk | 232c150 | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 584 | usbtty_string_table[STR_LANG] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 585 | (struct usb_string_descriptor*)wstrLang; |
| 586 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 587 | string = (struct usb_string_descriptor *) wstrManufacturer; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 588 | string->bLength = sizeof(wstrManufacturer); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 589 | string->bDescriptorType = USB_DT_STRING; |
| 590 | str2wide (CONFIG_USBD_MANUFACTURER, string->wData); |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 591 | usbtty_string_table[STR_MANUFACTURER]=string; |
| 592 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 593 | |
| 594 | string = (struct usb_string_descriptor *) wstrProduct; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 595 | string->bLength = sizeof(wstrProduct); |
wdenk | 232c150 | 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 | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 598 | usbtty_string_table[STR_PRODUCT]=string; |
| 599 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 600 | |
| 601 | string = (struct usb_string_descriptor *) wstrSerial; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 602 | string->bLength = sizeof(serial_number); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 603 | string->bDescriptorType = USB_DT_STRING; |
wdenk | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 604 | str2wide (serial_number, string->wData); |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 605 | usbtty_string_table[STR_SERIAL]=string; |
| 606 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 607 | |
| 608 | string = (struct usb_string_descriptor *) wstrConfiguration; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 609 | string->bLength = sizeof(wstrConfiguration); |
wdenk | 232c150 | 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 | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 612 | usbtty_string_table[STR_CONFIG]=string; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 613 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 614 | |
| 615 | string = (struct usb_string_descriptor *) wstrDataInterface; |
| 616 | string->bLength = sizeof(wstrDataInterface); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 617 | string->bDescriptorType = USB_DT_STRING; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 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); |
| 623 | string->bDescriptorType = USB_DT_STRING; |
| 624 | str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData); |
| 625 | usbtty_string_table[STR_CTRL_INTERFACE]=string; |
wdenk | 232c150 | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 629 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 630 | |
Tom Rini | b2fb47f | 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 | 232c150 | 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 | f9da0f8 | 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 | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 645 | device_instance->event = usbtty_event_handler; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 646 | device_instance->cdc_recv_setup = usbtty_cdc_setup; |
wdenk | 232c150 | 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 | 6629d2f | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 657 | bus_instance->serial_number_str = serial_number; |
wdenk | 232c150 | 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 | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 662 | config_instance->interfaces = interface_count; |
| 663 | config_instance->configuration_descriptor = configuration_descriptor; |
wdenk | 232c150 | 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 | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 694 | ep_descriptor_ptrs[i - 1]->bEndpointAddress; |
| 695 | |
| 696 | endpoint_instance[i].rcv_attributes = |
| 697 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 698 | |
Tom Rini | b2fb47f | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 699 | endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 700 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 701 | endpoint_instance[i].tx_attributes = |
| 702 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 703 | |
Tom Rini | b2fb47f | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 704 | endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 705 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 706 | endpoint_instance[i].tx_attributes = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 707 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 232c150 | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 730 | for (i = 1; i <= NUM_ENDPOINTS; i++) { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 731 | udc_setup_ep (device_instance, i, &endpoint_instance[i]); |
| 732 | } |
| 733 | } |
| 734 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 735 | /* usbtty_init_terminal_type |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 736 | * |
Wolfgang Denk | 16c8d5e | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 742 | /* CDC ACM */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 743 | case 0: |
| 744 | /* Assign endpoint descriptors */ |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 745 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 746 | &acm_configuration_descriptors[0].notification_endpoint; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 747 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 748 | &acm_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 749 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 750 | &acm_configuration_descriptors[0].data_endpoints[1]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 751 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 752 | /* Enumerate Device Descriptor */ |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 753 | device_descriptor.bDeviceClass = |
Wolfgang Denk | 16c8d5e | 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 | f9da0f8 | 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 | 16c8d5e | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 765 | |
Wolfgang Denk | 16c8d5e | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 779 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 780 | &gserial_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 781 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 782 | &gserial_configuration_descriptors[0].data_endpoints[1]; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 783 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 784 | &gserial_configuration_descriptors[0].data_endpoints[2]; |
| 785 | |
| 786 | /* Enumerate Device Descriptor */ |
| 787 | device_descriptor.bDeviceClass = 0xFF; |
| 788 | device_descriptor.idProduct = |
| 789 | cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL); |
Vipin KUMAR | f9da0f8 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 790 | #if defined(CONFIG_USBD_HS) |
| 791 | qualifier_descriptor.bDeviceClass = 0xFF; |
| 792 | #endif |
Wolfgang Denk | 16c8d5e | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 798 | configuration_descriptor = |
Wolfgang Denk | 16c8d5e | 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 | 232c150 | 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 | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 847 | |
Wolfgang Denk | 16c8d5e | 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; |
| 851 | |
| 852 | current_urb = next_urb (device_instance, endpoint); |
xypron.glpk@gmx.de | 73be5b3 | 2017-04-15 15:05:46 +0200 | [diff] [blame] | 853 | |
| 854 | if (!current_urb) { |
| 855 | TTYERR ("current_urb is NULL, buf->size %d\n", |
| 856 | buf->size); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 860 | /* TX data still exists - send it now |
| 861 | */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 862 | if(endpoint->sent < current_urb->actual_length){ |
| 863 | if(udc_endpoint_write (endpoint)){ |
| 864 | /* Write pre-empted by RX */ |
| 865 | return -1; |
| 866 | } |
| 867 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 868 | |
| 869 | if (buf->size) { |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 870 | char *dest; |
| 871 | |
| 872 | int space_avail; |
| 873 | int popnum, popped; |
| 874 | int total = 0; |
| 875 | |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 876 | /* Break buffer into urb sized pieces, |
| 877 | * and link each to the endpoint |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 878 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 879 | while (buf->size > 0) { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 880 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 881 | dest = (char*)current_urb->buffer + |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 882 | current_urb->actual_length; |
| 883 | |
| 884 | space_avail = |
| 885 | current_urb->buffer_length - |
| 886 | current_urb->actual_length; |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 887 | popnum = min(space_avail, (int)buf->size); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 888 | if (popnum == 0) |
| 889 | break; |
| 890 | |
| 891 | popped = buf_pop (buf, dest, popnum); |
| 892 | if (popped == 0) |
| 893 | break; |
| 894 | current_urb->actual_length += popped; |
| 895 | total += popped; |
| 896 | |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 897 | /* If endpoint->last == 0, then transfers have |
| 898 | * not started on this endpoint |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 899 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 900 | if (endpoint->last == 0) { |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 901 | if(udc_endpoint_write (endpoint)){ |
| 902 | /* Write pre-empted by RX */ |
| 903 | return -1; |
| 904 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 907 | }/* end while */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 908 | return total; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 909 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int fill_buffer (circbuf_t * buf) |
| 915 | { |
| 916 | struct usb_endpoint_instance *endpoint = |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 917 | &endpoint_instance[rx_endpoint]; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 918 | |
| 919 | if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) { |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 920 | unsigned int nb = 0; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 921 | char *src = (char *) endpoint->rcv_urb->buffer; |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 922 | unsigned int rx_avail = buf->totalsize - buf->size; |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 923 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 924 | if(rx_avail >= endpoint->rcv_urb->actual_length){ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 925 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 926 | nb = endpoint->rcv_urb->actual_length; |
| 927 | buf_push (buf, src, nb); |
| 928 | endpoint->rcv_urb->actual_length = 0; |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 929 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 930 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 931 | return nb; |
| 932 | } |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static int usbtty_configured (void) |
| 937 | { |
| 938 | return usbtty_configured_flag; |
| 939 | } |
| 940 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 941 | /******************************************************************************/ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 942 | |
| 943 | static void usbtty_event_handler (struct usb_device_instance *device, |
| 944 | usb_device_event_t event, int data) |
| 945 | { |
Vipin KUMAR | f9da0f8 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 946 | #if defined(CONFIG_USBD_HS) |
| 947 | int i; |
| 948 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 949 | switch (event) { |
| 950 | case DEVICE_RESET: |
| 951 | case DEVICE_BUS_INACTIVE: |
| 952 | usbtty_configured_flag = 0; |
| 953 | break; |
| 954 | case DEVICE_CONFIGURED: |
| 955 | usbtty_configured_flag = 1; |
| 956 | break; |
| 957 | |
| 958 | case DEVICE_ADDRESS_ASSIGNED: |
Vipin KUMAR | f9da0f8 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 959 | #if defined(CONFIG_USBD_HS) |
| 960 | /* |
| 961 | * is_usbd_high_speed routine needs to be defined by |
| 962 | * specific gadget driver |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 963 | * It returns true if device enumerates at High speed |
| 964 | * Retuns false otherwise |
Vipin KUMAR | f9da0f8 | 2012-03-26 15:38:06 +0530 | [diff] [blame] | 965 | */ |
| 966 | for (i = 0; i < NUM_ENDPOINTS; i++) { |
| 967 | if (((ep_descriptor_ptrs[i]->bmAttributes & |
| 968 | USB_ENDPOINT_XFERTYPE_MASK) == |
| 969 | USB_ENDPOINT_XFER_BULK) |
| 970 | && is_usbd_high_speed()) { |
| 971 | |
| 972 | ep_descriptor_ptrs[i]->wMaxPacketSize = |
| 973 | CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE; |
| 974 | } |
| 975 | |
| 976 | endpoint_instance[i + 1].tx_packetSize = |
| 977 | ep_descriptor_ptrs[i]->wMaxPacketSize; |
| 978 | endpoint_instance[i + 1].rcv_packetSize = |
| 979 | ep_descriptor_ptrs[i]->wMaxPacketSize; |
| 980 | } |
| 981 | #endif |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 982 | usbtty_init_endpoints (); |
| 983 | |
| 984 | default: |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 989 | /******************************************************************************/ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 990 | |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 991 | int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb) |
| 992 | { |
| 993 | switch (request->bRequest){ |
| 994 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 995 | case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 996 | break; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 997 | case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 998 | break; |
| 999 | case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1000 | * per character */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1001 | break; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1002 | case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1003 | break; |
| 1004 | case ACM_GET_LINE_ENCODING : /* request DTE rate, |
| 1005 | * stop/parity bits */ |
| 1006 | memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc)); |
| 1007 | urb->actual_length = sizeof(rs232_desc); |
| 1008 | |
| 1009 | break; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1010 | default: |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | /******************************************************************************/ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * Since interrupt handling has not yet been implemented, we use this function |
| 1020 | * to handle polling. This is called by the tstc,getc,putc,puts routines to |
| 1021 | * update the USB state. |
| 1022 | */ |
| 1023 | void usbtty_poll (void) |
| 1024 | { |
| 1025 | /* New interrupts? */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1026 | udc_irq(); |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1027 | |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1028 | /* Write any output data to host buffer |
| 1029 | * (do this before checking interrupts to avoid missing one) |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1030 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1031 | if (usbtty_configured ()) { |
| 1032 | write_buffer (&usbtty_output); |
| 1033 | } |
| 1034 | |
| 1035 | /* New interrupts? */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1036 | udc_irq(); |
Wolfgang Denk | 386eda0 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* Check for new data from host.. |
| 1039 | * (do this after checking interrupts to get latest data) |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1040 | */ |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1041 | if (usbtty_configured ()) { |
| 1042 | fill_buffer (&usbtty_input); |
| 1043 | } |
| 1044 | |
| 1045 | /* New interrupts? */ |
Wolfgang Denk | 16c8d5e | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1046 | udc_irq(); |
| 1047 | |
wdenk | 232c150 | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1048 | } |