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 | */ |
| 66 | #define USBTTY_BUFFER_SIZE 256 |
| 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 | |
| 136 | /* |
| 137 | * Static CDC ACM specific descriptors |
| 138 | */ |
| 139 | |
| 140 | struct acm_config_desc { |
| 141 | struct usb_configuration_descriptor configuration_desc; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 142 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 143 | /* Master Interface */ |
| 144 | struct usb_interface_descriptor interface_desc; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 145 | |
Wolfgang Denk | 3f0137b | 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 | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 154 | struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1]; |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 160 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 161 | sizeof(struct usb_configuration_descriptor), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 162 | .bDescriptorType = USB_DT_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 163 | .wTotalLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 164 | cpu_to_le16(sizeof(struct acm_config_desc)), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 165 | .bNumInterfaces = NUM_ACM_INTERFACES, |
| 166 | .bConfigurationValue = 1, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 167 | .iConfiguration = STR_CONFIG, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 168 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 179 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 186 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 187 | sizeof(struct usb_class_header_function_descriptor), |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 188 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 193 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 197 | .bmCapabilities = 0x00, |
| 198 | .bDataInterface = 0x01, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 199 | }, |
| 200 | .usb_class_acm = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 201 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 202 | sizeof(struct usb_class_abstract_control_descriptor), |
| 203 | .bDescriptorType = CS_INTERFACE, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 204 | .bDescriptorSubtype = USB_ST_ACMF, |
| 205 | .bmCapabilities = 0x00, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 206 | }, |
| 207 | .usb_class_union = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 208 | .bFunctionLength = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 212 | .bMasterInterface = 0x00, |
| 213 | .bSlaveInterface0 = 0x01, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 214 | }, |
| 215 | .notification_endpoint = { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 216 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 217 | sizeof(struct usb_endpoint_descriptor), |
| 218 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 219 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 220 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 221 | .wMaxPacketSize |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 228 | .bLength = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 234 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 242 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 243 | sizeof(struct usb_endpoint_descriptor), |
| 244 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 245 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 246 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 247 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 248 | .wMaxPacketSize = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 253 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 254 | sizeof(struct usb_endpoint_descriptor), |
| 255 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 256 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 257 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 258 | USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 259 | .wMaxPacketSize = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 265 | }; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 266 | |
| 267 | static struct rs232_emu rs232_desc={ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 268 | .dter = 115200, |
| 269 | .stop_bits = 0x00, |
| 270 | .parity = 0x00, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 271 | .data_bits = 0x08 |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 274 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 275 | /* |
| 276 | * Static Generic Serial specific data |
| 277 | */ |
| 278 | |
| 279 | |
| 280 | struct gserial_config_desc { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 281 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 282 | struct usb_configuration_descriptor configuration_desc; |
Atin Malaviya | 11a9df9 | 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 | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 285 | |
| 286 | } __attribute__((packed)); |
| 287 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 288 | static struct gserial_config_desc |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 294 | .wTotalLength = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 299 | .bmAttributes = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 305 | .bLength = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 311 | .bInterfaceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 312 | COMMUNICATIONS_INTERFACE_CLASS_VENDOR, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 313 | .bInterfaceSubClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 314 | COMMUNICATIONS_NO_SUBCLASS, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 315 | .bInterfaceProtocol = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 316 | COMMUNICATIONS_NO_PROTOCOL, |
| 317 | .iInterface = STR_DATA_INTERFACE |
| 318 | }, |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 319 | }, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 320 | .data_endpoints = { |
| 321 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 322 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 323 | sizeof(struct usb_endpoint_descriptor), |
| 324 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 325 | .bEndpointAddress = UDC_OUT_ENDPOINT | USB_DIR_OUT, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 326 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 327 | .wMaxPacketSize = |
Wolfgang Denk | 3f0137b | 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 | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 332 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 333 | sizeof(struct usb_endpoint_descriptor), |
| 334 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 335 | .bEndpointAddress = UDC_IN_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 336 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 337 | .wMaxPacketSize = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 338 | cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE), |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 339 | .bInterval = 0xFF, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 340 | }, |
| 341 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 342 | .bLength = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 343 | sizeof(struct usb_endpoint_descriptor), |
| 344 | .bDescriptorType = USB_DT_ENDPOINT, |
Vivek Kutal | 5b77438 | 2009-02-23 21:35:11 +0530 | [diff] [blame] | 345 | .bEndpointAddress = UDC_INT_ENDPOINT | USB_DIR_IN, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 346 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 347 | .wMaxPacketSize = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 348 | cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE), |
| 349 | .bInterval = 0xFF, |
| 350 | }, |
| 351 | }, |
| 352 | }, |
| 353 | }; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 354 | |
| 355 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 356 | * Static Function Prototypes |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 357 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 358 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 359 | static void usbtty_init_strings (void); |
| 360 | static void usbtty_init_instances (void); |
| 361 | static void usbtty_init_endpoints (void); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 362 | static void usbtty_init_terminal_type(short type); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 363 | static void usbtty_event_handler (struct usb_device_instance *device, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 364 | usb_device_event_t event, int data); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 365 | static int usbtty_cdc_setup(struct usb_device_request *request, |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 366 | struct urb *urb); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 367 | static int usbtty_configured (void); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 368 | static int write_buffer (circbuf_t * buf); |
| 369 | static int fill_buffer (circbuf_t * buf); |
| 370 | |
| 371 | void usbtty_poll (void); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 372 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 373 | /* utility function for converting char* to wide string used by USB */ |
| 374 | static void str2wide (char *str, u16 * wide) |
| 375 | { |
| 376 | int i; |
| 377 | for (i = 0; i < strlen (str) && str[i]; i++){ |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 378 | #if defined(__LITTLE_ENDIAN) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 379 | wide[i] = (u16) str[i]; |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 380 | #elif defined(__BIG_ENDIAN) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 381 | wide[i] = ((u16)(str[i])<<8); |
| 382 | #else |
Rodolfo Giometti | 703bda9 | 2007-06-06 10:08:14 +0200 | [diff] [blame] | 383 | #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 384 | #endif |
| 385 | } |
| 386 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Test whether a character is in the RX buffer |
| 390 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 391 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 392 | int usbtty_tstc (void) |
| 393 | { |
Wolfgang Denk | 3f0137b | 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 | 29e7f5a | 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 | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 411 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 412 | int usbtty_getc (void) |
| 413 | { |
| 414 | char c; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 415 | struct usb_endpoint_instance *endpoint = |
| 416 | &endpoint_instance[rx_endpoint]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 417 | |
| 418 | while (usbtty_input.size <= 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 419 | udc_unset_nak(endpoint->endpoint_address&0x03); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 420 | usbtty_poll (); |
| 421 | } |
| 422 | |
| 423 | buf_pop (&usbtty_input, &c, 1); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 424 | udc_set_nak(endpoint->endpoint_address&0x03); |
| 425 | |
wdenk | 29e7f5a | 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 | */ |
| 432 | void usbtty_putc (const char c) |
| 433 | { |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 434 | if (!usbtty_configured ()) |
| 435 | return; |
| 436 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 437 | buf_push (&usbtty_output, &c, 1); |
| 438 | /* If \n, also do \r */ |
| 439 | if (c == '\n') |
| 440 | buf_push (&usbtty_output, "\r", 1); |
| 441 | |
| 442 | /* Poll at end to handle new data... */ |
| 443 | if ((usbtty_output.size + 2) >= usbtty_output.totalsize) { |
| 444 | usbtty_poll (); |
| 445 | } |
| 446 | } |
| 447 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 448 | /* usbtty_puts() helper function for finding the next '\n' in a string */ |
| 449 | static int next_nl_pos (const char *s) |
| 450 | { |
| 451 | int i; |
| 452 | |
| 453 | for (i = 0; s[i] != '\0'; i++) { |
| 454 | if (s[i] == '\n') |
| 455 | return i; |
| 456 | } |
| 457 | return i; |
| 458 | } |
| 459 | |
| 460 | /* |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 461 | * Output a string to the usb client port - implementing flow control |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 462 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 463 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 464 | static void __usbtty_puts (const char *str, int len) |
| 465 | { |
| 466 | int maxlen = usbtty_output.totalsize; |
| 467 | int space, n; |
| 468 | |
| 469 | /* break str into chunks < buffer size, if needed */ |
| 470 | while (len > 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 471 | usbtty_poll (); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 472 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 473 | space = maxlen - usbtty_output.size; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 474 | /* Empty buffer here, if needed, to ensure space... */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 475 | if (space) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 476 | write_buffer (&usbtty_output); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 477 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 478 | n = MIN (space, MIN (len, maxlen)); |
| 479 | buf_push (&usbtty_output, str, n); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 480 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 481 | str += n; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 482 | len -= n; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 483 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | void usbtty_puts (const char *str) |
| 488 | { |
| 489 | int n; |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 490 | int len; |
| 491 | |
| 492 | if (!usbtty_configured ()) |
| 493 | return; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 494 | |
Atin Malaviya | 11a9df9 | 2009-02-03 15:17:10 -0500 | [diff] [blame] | 495 | len = strlen (str); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 496 | /* add '\r' for each '\n' */ |
| 497 | while (len > 0) { |
| 498 | n = next_nl_pos (str); |
| 499 | |
| 500 | if (str[n] == '\n') { |
| 501 | __usbtty_puts (str, n + 1); |
| 502 | __usbtty_puts ("\r", 1); |
| 503 | str += (n + 1); |
| 504 | len -= (n + 1); |
| 505 | } else { |
| 506 | /* No \n found. All done. */ |
| 507 | __usbtty_puts (str, n); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* Poll at end to handle new data... */ |
| 513 | usbtty_poll (); |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Initialize the usb client port. |
| 518 | * |
| 519 | */ |
| 520 | int drv_usbtty_init (void) |
| 521 | { |
| 522 | int rc; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 523 | char * sn; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 524 | char * tt; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 525 | int snlen; |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 526 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 527 | /* Ger seiral number */ |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 528 | if (!(sn = getenv("serial#"))) { |
| 529 | sn = "000000000000"; |
| 530 | } |
| 531 | snlen = strlen(sn); |
| 532 | if (snlen > sizeof(serial_number) - 1) { |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 533 | printf ("Warning: serial number %s is too long (%d > %lu)\n", |
| 534 | sn, snlen, (ulong)(sizeof(serial_number) - 1)); |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 535 | snlen = sizeof(serial_number) - 1; |
| 536 | } |
| 537 | memcpy (serial_number, sn, snlen); |
| 538 | serial_number[snlen] = '\0'; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 539 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 540 | /* Decide on which type of UDC device to be. |
| 541 | */ |
| 542 | |
| 543 | if(!(tt = getenv("usbtty"))) { |
| 544 | tt = "generic"; |
| 545 | } |
| 546 | usbtty_init_terminal_type(strcmp(tt,"cdc_acm")); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 547 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 548 | /* prepare buffers... */ |
| 549 | buf_init (&usbtty_input, USBTTY_BUFFER_SIZE); |
| 550 | buf_init (&usbtty_output, USBTTY_BUFFER_SIZE); |
| 551 | |
| 552 | /* Now, set up USB controller and infrastructure */ |
| 553 | udc_init (); /* Basic USB initialization */ |
| 554 | |
| 555 | usbtty_init_strings (); |
| 556 | usbtty_init_instances (); |
| 557 | |
Stefan Herbrechtsmeier | 49914b6 | 2011-10-17 17:22:49 +0200 | [diff] [blame] | 558 | usbtty_init_endpoints (); |
| 559 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 560 | udc_startup_events (device_instance);/* Enable dev, init udc pointers */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 561 | udc_connect (); /* Enable pullup for host detection */ |
| 562 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 563 | /* Device initialization */ |
| 564 | memset (&usbttydev, 0, sizeof (usbttydev)); |
| 565 | |
| 566 | strcpy (usbttydev.name, "usbtty"); |
| 567 | usbttydev.ext = 0; /* No extensions */ |
| 568 | usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT; |
| 569 | usbttydev.tstc = usbtty_tstc; /* 'tstc' function */ |
| 570 | usbttydev.getc = usbtty_getc; /* 'getc' function */ |
| 571 | usbttydev.putc = usbtty_putc; /* 'putc' function */ |
| 572 | usbttydev.puts = usbtty_puts; /* 'puts' function */ |
| 573 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 574 | rc = stdio_register (&usbttydev); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 575 | |
| 576 | return (rc == 0) ? 1 : rc; |
| 577 | } |
| 578 | |
| 579 | static void usbtty_init_strings (void) |
| 580 | { |
| 581 | struct usb_string_descriptor *string; |
| 582 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 583 | usbtty_string_table[STR_LANG] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 584 | (struct usb_string_descriptor*)wstrLang; |
| 585 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 586 | string = (struct usb_string_descriptor *) wstrManufacturer; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 587 | string->bLength = sizeof(wstrManufacturer); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 588 | string->bDescriptorType = USB_DT_STRING; |
| 589 | str2wide (CONFIG_USBD_MANUFACTURER, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 590 | usbtty_string_table[STR_MANUFACTURER]=string; |
| 591 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 592 | |
| 593 | string = (struct usb_string_descriptor *) wstrProduct; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 594 | string->bLength = sizeof(wstrProduct); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 595 | string->bDescriptorType = USB_DT_STRING; |
| 596 | str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 597 | usbtty_string_table[STR_PRODUCT]=string; |
| 598 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 599 | |
| 600 | string = (struct usb_string_descriptor *) wstrSerial; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 601 | string->bLength = sizeof(serial_number); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 602 | string->bDescriptorType = USB_DT_STRING; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 603 | str2wide (serial_number, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 604 | usbtty_string_table[STR_SERIAL]=string; |
| 605 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 606 | |
| 607 | string = (struct usb_string_descriptor *) wstrConfiguration; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 608 | string->bLength = sizeof(wstrConfiguration); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 609 | string->bDescriptorType = USB_DT_STRING; |
| 610 | str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 611 | usbtty_string_table[STR_CONFIG]=string; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 612 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 613 | |
| 614 | string = (struct usb_string_descriptor *) wstrDataInterface; |
| 615 | string->bLength = sizeof(wstrDataInterface); |
| 616 | string->bDescriptorType = USB_DT_STRING; |
| 617 | str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData); |
| 618 | usbtty_string_table[STR_DATA_INTERFACE]=string; |
| 619 | |
| 620 | string = (struct usb_string_descriptor *) wstrCtrlInterface; |
| 621 | string->bLength = sizeof(wstrCtrlInterface); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 622 | string->bDescriptorType = USB_DT_STRING; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 623 | str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData); |
| 624 | usbtty_string_table[STR_CTRL_INTERFACE]=string; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 625 | |
| 626 | /* Now, initialize the string table for ep0 handling */ |
| 627 | usb_strings = usbtty_string_table; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 628 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 629 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 630 | #define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\ |
| 631 | &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize)); |
| 632 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 633 | static void usbtty_init_instances (void) |
| 634 | { |
| 635 | int i; |
| 636 | |
| 637 | /* initialize device instance */ |
| 638 | memset (device_instance, 0, sizeof (struct usb_device_instance)); |
| 639 | device_instance->device_state = STATE_INIT; |
| 640 | device_instance->device_descriptor = &device_descriptor; |
| 641 | device_instance->event = usbtty_event_handler; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 642 | device_instance->cdc_recv_setup = usbtty_cdc_setup; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 643 | device_instance->bus = bus_instance; |
| 644 | device_instance->configurations = NUM_CONFIGS; |
| 645 | device_instance->configuration_instance_array = config_instance; |
| 646 | |
| 647 | /* initialize bus instance */ |
| 648 | memset (bus_instance, 0, sizeof (struct usb_bus_instance)); |
| 649 | bus_instance->device = device_instance; |
| 650 | bus_instance->endpoint_array = endpoint_instance; |
| 651 | bus_instance->max_endpoints = 1; |
| 652 | bus_instance->maxpacketsize = 64; |
wdenk | 05822bf | 2004-03-12 15:38:25 +0000 | [diff] [blame] | 653 | bus_instance->serial_number_str = serial_number; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 654 | |
| 655 | /* configuration instance */ |
| 656 | memset (config_instance, 0, |
| 657 | sizeof (struct usb_configuration_instance)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 658 | config_instance->interfaces = interface_count; |
| 659 | config_instance->configuration_descriptor = configuration_descriptor; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 660 | config_instance->interface_instance_array = interface_instance; |
| 661 | |
| 662 | /* interface instance */ |
| 663 | memset (interface_instance, 0, |
| 664 | sizeof (struct usb_interface_instance)); |
| 665 | interface_instance->alternates = 1; |
| 666 | interface_instance->alternates_instance_array = alternate_instance; |
| 667 | |
| 668 | /* alternates instance */ |
| 669 | memset (alternate_instance, 0, |
| 670 | sizeof (struct usb_alternate_instance)); |
| 671 | alternate_instance->interface_descriptor = interface_descriptors; |
| 672 | alternate_instance->endpoints = NUM_ENDPOINTS; |
| 673 | alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs; |
| 674 | |
| 675 | /* endpoint instances */ |
| 676 | memset (&endpoint_instance[0], 0, |
| 677 | sizeof (struct usb_endpoint_instance)); |
| 678 | endpoint_instance[0].endpoint_address = 0; |
| 679 | endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE; |
| 680 | endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL; |
| 681 | endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE; |
| 682 | endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL; |
| 683 | udc_setup_ep (device_instance, 0, &endpoint_instance[0]); |
| 684 | |
| 685 | for (i = 1; i <= NUM_ENDPOINTS; i++) { |
| 686 | memset (&endpoint_instance[i], 0, |
| 687 | sizeof (struct usb_endpoint_instance)); |
| 688 | |
| 689 | endpoint_instance[i].endpoint_address = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 690 | ep_descriptor_ptrs[i - 1]->bEndpointAddress; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 691 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 692 | endpoint_instance[i].rcv_attributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 693 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
| 694 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 695 | endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 696 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 697 | endpoint_instance[i].tx_attributes = |
| 698 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 699 | |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 700 | endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 701 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 702 | endpoint_instance[i].tx_attributes = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 703 | ep_descriptor_ptrs[i - 1]->bmAttributes; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 704 | |
| 705 | urb_link_init (&endpoint_instance[i].rcv); |
| 706 | urb_link_init (&endpoint_instance[i].rdy); |
| 707 | urb_link_init (&endpoint_instance[i].tx); |
| 708 | urb_link_init (&endpoint_instance[i].done); |
| 709 | |
| 710 | if (endpoint_instance[i].endpoint_address & USB_DIR_IN) |
| 711 | endpoint_instance[i].tx_urb = |
| 712 | usbd_alloc_urb (device_instance, |
| 713 | &endpoint_instance[i]); |
| 714 | else |
| 715 | endpoint_instance[i].rcv_urb = |
| 716 | usbd_alloc_urb (device_instance, |
| 717 | &endpoint_instance[i]); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static void usbtty_init_endpoints (void) |
| 722 | { |
| 723 | int i; |
| 724 | |
| 725 | bus_instance->max_endpoints = NUM_ENDPOINTS + 1; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 726 | for (i = 1; i <= NUM_ENDPOINTS; i++) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 727 | udc_setup_ep (device_instance, i, &endpoint_instance[i]); |
| 728 | } |
| 729 | } |
| 730 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 731 | /* usbtty_init_terminal_type |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 732 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 733 | * Do some late binding for our device type. |
| 734 | */ |
| 735 | static void usbtty_init_terminal_type(short type) |
| 736 | { |
| 737 | switch(type){ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 738 | /* CDC ACM */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 739 | case 0: |
| 740 | /* Assign endpoint descriptors */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 741 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 742 | &acm_configuration_descriptors[0].notification_endpoint; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 743 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 744 | &acm_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 745 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 746 | &acm_configuration_descriptors[0].data_endpoints[1]; |
| 747 | |
| 748 | /* Enumerate Device Descriptor */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 749 | device_descriptor.bDeviceClass = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 750 | COMMUNICATIONS_DEVICE_CLASS; |
| 751 | device_descriptor.idProduct = |
| 752 | cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM); |
| 753 | |
| 754 | /* Assign endpoint indices */ |
| 755 | tx_endpoint = ACM_TX_ENDPOINT; |
| 756 | rx_endpoint = ACM_RX_ENDPOINT; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 757 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 758 | /* Configuration Descriptor */ |
| 759 | configuration_descriptor = |
| 760 | (struct usb_configuration_descriptor*) |
| 761 | &acm_configuration_descriptors; |
| 762 | |
| 763 | /* Interface count */ |
| 764 | interface_count = NUM_ACM_INTERFACES; |
| 765 | break; |
| 766 | |
| 767 | /* BULK IN/OUT & Default */ |
| 768 | case 1: |
| 769 | default: |
| 770 | /* Assign endpoint descriptors */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 771 | ep_descriptor_ptrs[0] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 772 | &gserial_configuration_descriptors[0].data_endpoints[0]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 773 | ep_descriptor_ptrs[1] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 774 | &gserial_configuration_descriptors[0].data_endpoints[1]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 775 | ep_descriptor_ptrs[2] = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 776 | &gserial_configuration_descriptors[0].data_endpoints[2]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 777 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 778 | /* Enumerate Device Descriptor */ |
| 779 | device_descriptor.bDeviceClass = 0xFF; |
| 780 | device_descriptor.idProduct = |
| 781 | cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL); |
| 782 | |
| 783 | /* Assign endpoint indices */ |
| 784 | tx_endpoint = GSERIAL_TX_ENDPOINT; |
| 785 | rx_endpoint = GSERIAL_RX_ENDPOINT; |
| 786 | |
| 787 | /* Configuration Descriptor */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 788 | configuration_descriptor = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 789 | (struct usb_configuration_descriptor*) |
| 790 | &gserial_configuration_descriptors; |
| 791 | |
| 792 | /* Interface count */ |
| 793 | interface_count = NUM_GSERIAL_INTERFACES; |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 799 | |
| 800 | static struct urb *next_urb (struct usb_device_instance *device, |
| 801 | struct usb_endpoint_instance *endpoint) |
| 802 | { |
| 803 | struct urb *current_urb = NULL; |
| 804 | int space; |
| 805 | |
| 806 | /* If there's a queue, then we should add to the last urb */ |
| 807 | if (!endpoint->tx_queue) { |
| 808 | current_urb = endpoint->tx_urb; |
| 809 | } else { |
| 810 | /* Last urb from tx chain */ |
| 811 | current_urb = |
| 812 | p2surround (struct urb, link, endpoint->tx.prev); |
| 813 | } |
| 814 | |
| 815 | /* Make sure this one has enough room */ |
| 816 | space = current_urb->buffer_length - current_urb->actual_length; |
| 817 | if (space > 0) { |
| 818 | return current_urb; |
| 819 | } else { /* No space here */ |
| 820 | /* First look at done list */ |
| 821 | current_urb = first_urb_detached (&endpoint->done); |
| 822 | if (!current_urb) { |
| 823 | current_urb = usbd_alloc_urb (device, endpoint); |
| 824 | } |
| 825 | |
| 826 | urb_append (&endpoint->tx, current_urb); |
| 827 | endpoint->tx_queue++; |
| 828 | } |
| 829 | return current_urb; |
| 830 | } |
| 831 | |
| 832 | static int write_buffer (circbuf_t * buf) |
| 833 | { |
| 834 | if (!usbtty_configured ()) { |
| 835 | return 0; |
| 836 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 837 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 838 | struct usb_endpoint_instance *endpoint = |
| 839 | &endpoint_instance[tx_endpoint]; |
| 840 | struct urb *current_urb = NULL; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 841 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 842 | current_urb = next_urb (device_instance, endpoint); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 843 | /* TX data still exists - send it now |
| 844 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 845 | if(endpoint->sent < current_urb->actual_length){ |
| 846 | if(udc_endpoint_write (endpoint)){ |
| 847 | /* Write pre-empted by RX */ |
| 848 | return -1; |
| 849 | } |
| 850 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 851 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 852 | if (buf->size) { |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 853 | char *dest; |
| 854 | |
| 855 | int space_avail; |
| 856 | int popnum, popped; |
| 857 | int total = 0; |
| 858 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 859 | /* Break buffer into urb sized pieces, |
| 860 | * and link each to the endpoint |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 861 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 862 | while (buf->size > 0) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 863 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 864 | if (!current_urb) { |
| 865 | TTYERR ("current_urb is NULL, buf->size %d\n", |
| 866 | buf->size); |
| 867 | return total; |
| 868 | } |
| 869 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 870 | dest = (char*)current_urb->buffer + |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 871 | current_urb->actual_length; |
| 872 | |
| 873 | space_avail = |
| 874 | current_urb->buffer_length - |
| 875 | current_urb->actual_length; |
| 876 | popnum = MIN (space_avail, buf->size); |
| 877 | if (popnum == 0) |
| 878 | break; |
| 879 | |
| 880 | popped = buf_pop (buf, dest, popnum); |
| 881 | if (popped == 0) |
| 882 | break; |
| 883 | current_urb->actual_length += popped; |
| 884 | total += popped; |
| 885 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 886 | /* If endpoint->last == 0, then transfers have |
| 887 | * not started on this endpoint |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 888 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 889 | if (endpoint->last == 0) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 890 | if(udc_endpoint_write (endpoint)){ |
| 891 | /* Write pre-empted by RX */ |
| 892 | return -1; |
| 893 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 896 | }/* end while */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 897 | return total; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 898 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static int fill_buffer (circbuf_t * buf) |
| 904 | { |
| 905 | struct usb_endpoint_instance *endpoint = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 906 | &endpoint_instance[rx_endpoint]; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 907 | |
| 908 | if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 909 | unsigned int nb = 0; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 910 | char *src = (char *) endpoint->rcv_urb->buffer; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 911 | unsigned int rx_avail = buf->totalsize - buf->size; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 912 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 913 | if(rx_avail >= endpoint->rcv_urb->actual_length){ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 914 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 915 | nb = endpoint->rcv_urb->actual_length; |
| 916 | buf_push (buf, src, nb); |
| 917 | endpoint->rcv_urb->actual_length = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 918 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 919 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 920 | return nb; |
| 921 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | static int usbtty_configured (void) |
| 926 | { |
| 927 | return usbtty_configured_flag; |
| 928 | } |
| 929 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 930 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 931 | |
| 932 | static void usbtty_event_handler (struct usb_device_instance *device, |
| 933 | usb_device_event_t event, int data) |
| 934 | { |
| 935 | switch (event) { |
| 936 | case DEVICE_RESET: |
| 937 | case DEVICE_BUS_INACTIVE: |
| 938 | usbtty_configured_flag = 0; |
| 939 | break; |
| 940 | case DEVICE_CONFIGURED: |
| 941 | usbtty_configured_flag = 1; |
| 942 | break; |
| 943 | |
| 944 | case DEVICE_ADDRESS_ASSIGNED: |
| 945 | usbtty_init_endpoints (); |
| 946 | |
| 947 | default: |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 952 | /******************************************************************************/ |
| 953 | |
| 954 | int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb) |
| 955 | { |
| 956 | switch (request->bRequest){ |
| 957 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 958 | case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 959 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 960 | case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 961 | break; |
| 962 | case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 963 | * per character */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 964 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 965 | case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 966 | break; |
| 967 | case ACM_GET_LINE_ENCODING : /* request DTE rate, |
| 968 | * stop/parity bits */ |
| 969 | memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc)); |
| 970 | urb->actual_length = sizeof(rs232_desc); |
| 971 | |
| 972 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 973 | default: |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 974 | return 1; |
| 975 | } |
| 976 | return 0; |
| 977 | } |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 978 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 979 | /******************************************************************************/ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * Since interrupt handling has not yet been implemented, we use this function |
| 983 | * to handle polling. This is called by the tstc,getc,putc,puts routines to |
| 984 | * update the USB state. |
| 985 | */ |
| 986 | void usbtty_poll (void) |
| 987 | { |
| 988 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 989 | udc_irq(); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 990 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 991 | /* Write any output data to host buffer |
| 992 | * (do this before checking interrupts to avoid missing one) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 993 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 994 | if (usbtty_configured ()) { |
| 995 | write_buffer (&usbtty_output); |
| 996 | } |
| 997 | |
| 998 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 999 | udc_irq(); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* Check for new data from host.. |
| 1002 | * (do this after checking interrupts to get latest data) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1003 | */ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1004 | if (usbtty_configured ()) { |
| 1005 | fill_buffer (&usbtty_input); |
| 1006 | } |
| 1007 | |
| 1008 | /* New interrupts? */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1009 | udc_irq(); |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1010 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 1011 | } |