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