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