blob: cc2bdac479efad185d9169eabf931ac2c5abde1f [file] [log] [blame]
wdenk29e7f5a2004-03-12 00:14:09 +00001/*
2 * (C) Copyright 2003
3 * Gerry Hamel, geh@ti.com, Texas Instruments
Wolfgang Denke2601822006-06-14 18:14:56 +02004 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02005 * (C) Copyright 2006
6 * Bryan O'Donoghue, bodonoghue@codehermit.ie
wdenk29e7f5a2004-03-12 00:14:09 +00007 *
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>
25
26#ifdef CONFIG_USB_TTY
27
28#include <circbuf.h>
29#include <devices.h>
30#include "usbtty.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020031#include "usb_cdc_acm.h"
32#include "usbdescriptors.h"
33#include <config.h> /* If defined, override Linux identifiers with
Wolfgang Denka1be4762008-05-20 16:00:29 +020034 * vendor specific ones */
wdenk29e7f5a2004-03-12 00:14:09 +000035
36#if 0
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020037#define TTYDBG(fmt,args...)\
38 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000039#else
40#define TTYDBG(fmt,args...) do{}while(0)
41#endif
42
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020043#if 1
44#define TTYERR(fmt,args...)\
45 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
46 __LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000047#else
48#define TTYERR(fmt,args...) do{}while(0)
49#endif
50
51/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020052 * Defines
53 */
54#define NUM_CONFIGS 1
55#define MAX_INTERFACES 2
56#define NUM_ENDPOINTS 3
57#define ACM_TX_ENDPOINT 3
58#define ACM_RX_ENDPOINT 2
59#define GSERIAL_TX_ENDPOINT 2
60#define GSERIAL_RX_ENDPOINT 1
61#define NUM_ACM_INTERFACES 2
62#define NUM_GSERIAL_INTERFACES 1
63#define CONFIG_USBD_DATA_INTERFACE_STR "Bulk Data Interface"
64#define CONFIG_USBD_CTRL_INTERFACE_STR "Control Interface"
65
66/*
wdenk29e7f5a2004-03-12 00:14:09 +000067 * Buffers to hold input and output data
68 */
69#define USBTTY_BUFFER_SIZE 256
70static circbuf_t usbtty_input;
71static circbuf_t usbtty_output;
72
73
74/*
75 * Instance variables
76 */
77static device_t usbttydev;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020078static struct usb_device_instance device_instance[1];
79static struct usb_bus_instance bus_instance[1];
wdenk29e7f5a2004-03-12 00:14:09 +000080static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020081static struct usb_interface_instance interface_instance[MAX_INTERFACES];
82static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
83/* one extra for control endpoint */
84static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk29e7f5a2004-03-12 00:14:09 +000085
86/*
wdenk29e7f5a2004-03-12 00:14:09 +000087 * Global flag
88 */
89int usbtty_configured_flag = 0;
90
wdenk29e7f5a2004-03-12 00:14:09 +000091/*
wdenk05822bf2004-03-12 15:38:25 +000092 * Serial number
93 */
94static char serial_number[16];
95
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020096
wdenk05822bf2004-03-12 15:38:25 +000097/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020098 * Descriptors, Strings, Local variables.
wdenk29e7f5a2004-03-12 00:14:09 +000099 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200100
101/* defined and used by usbdcore_ep0.c */
102extern struct usb_string_descriptor **usb_strings;
103
104/* Indicies, References */
105static unsigned short rx_endpoint = 0;
106static unsigned short tx_endpoint = 0;
107static unsigned short interface_count = 0;
108static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
109
110/* USB Descriptor Strings */
wdenk29e7f5a2004-03-12 00:14:09 +0000111static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
112static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
113static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk05822bf2004-03-12 15:38:25 +0000114static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000115static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200116static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
117static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000118
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200119/* Standard USB Data Structures */
120static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
121static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
122static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000123static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200124 .bLength = sizeof(struct usb_device_descriptor),
125 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200126 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200127 .bDeviceSubClass = 0x00,
128 .bDeviceProtocol = 0x00,
129 .bMaxPacketSize0 = EP0_MAX_PACKET_SIZE,
130 .idVendor = cpu_to_le16(CONFIG_USBD_VENDORID),
131 .bcdDevice = cpu_to_le16(USBTTY_BCD_DEVICE),
132 .iManufacturer = STR_MANUFACTURER,
133 .iProduct = STR_PRODUCT,
134 .iSerialNumber = STR_SERIAL,
135 .bNumConfigurations = NUM_CONFIGS
wdenk29e7f5a2004-03-12 00:14:09 +0000136};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200137
138
139/*
140 * Static CDC ACM specific descriptors
141 */
142
143struct acm_config_desc {
144 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200145
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200146 /* Master Interface */
147 struct usb_interface_descriptor interface_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200148
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200149 struct usb_class_header_function_descriptor usb_class_header;
150 struct usb_class_call_management_descriptor usb_class_call_mgt;
151 struct usb_class_abstract_control_descriptor usb_class_acm;
152 struct usb_class_union_function_descriptor usb_class_union;
153 struct usb_endpoint_descriptor notification_endpoint;
154
155 /* Slave Interface */
156 struct usb_interface_descriptor data_class_interface;
Wolfgang Denke2601822006-06-14 18:14:56 +0200157 struct usb_endpoint_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200158 data_endpoints[NUM_ENDPOINTS-1] __attribute__((packed));
159} __attribute__((packed));
160
161static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
162 {
163 .configuration_desc ={
Wolfgang Denke2601822006-06-14 18:14:56 +0200164 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200165 sizeof(struct usb_configuration_descriptor),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200166 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200167 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200168 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200169 .bNumInterfaces = NUM_ACM_INTERFACES,
170 .bConfigurationValue = 1,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200171 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200172 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200173 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
174 .bMaxPower = USBTTY_MAXPOWER
175 },
176 /* Interface 1 */
177 .interface_desc = {
178 .bLength = sizeof(struct usb_interface_descriptor),
179 .bDescriptorType = USB_DT_INTERFACE,
180 .bInterfaceNumber = 0,
181 .bAlternateSetting = 0,
182 .bNumEndpoints = 0x01,
Wolfgang Denke2601822006-06-14 18:14:56 +0200183 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200184 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
185 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
186 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
187 .iInterface = STR_CTRL_INTERFACE,
188 },
189 .usb_class_header = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200190 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200191 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denke2601822006-06-14 18:14:56 +0200192 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200193 .bDescriptorSubtype = USB_ST_HEADER,
194 .bcdCDC = cpu_to_le16(110),
195 },
196 .usb_class_call_mgt = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200197 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200198 sizeof(struct usb_class_call_management_descriptor),
199 .bDescriptorType = CS_INTERFACE,
200 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200201 .bmCapabilities = 0x00,
202 .bDataInterface = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200203 },
204 .usb_class_acm = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200205 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200206 sizeof(struct usb_class_abstract_control_descriptor),
207 .bDescriptorType = CS_INTERFACE,
Wolfgang Denke2601822006-06-14 18:14:56 +0200208 .bDescriptorSubtype = USB_ST_ACMF,
209 .bmCapabilities = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200210 },
211 .usb_class_union = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200212 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200213 sizeof(struct usb_class_union_function_descriptor),
214 .bDescriptorType = CS_INTERFACE,
215 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200216 .bMasterInterface = 0x00,
217 .bSlaveInterface0 = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200218 },
219 .notification_endpoint = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200220 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200221 sizeof(struct usb_endpoint_descriptor),
222 .bDescriptorType = USB_DT_ENDPOINT,
223 .bEndpointAddress = 0x01 | USB_DIR_IN,
224 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200225 .wMaxPacketSize
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200226 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
227 .bInterval = 0xFF,
228 },
229
230 /* Interface 2 */
231 .data_class_interface = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200232 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200233 sizeof(struct usb_interface_descriptor),
234 .bDescriptorType = USB_DT_INTERFACE,
235 .bInterfaceNumber = 0x01,
236 .bAlternateSetting = 0x00,
237 .bNumEndpoints = 0x02,
Wolfgang Denke2601822006-06-14 18:14:56 +0200238 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200239 COMMUNICATIONS_INTERFACE_CLASS_DATA,
240 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
241 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
242 .iInterface = STR_DATA_INTERFACE,
243 },
244 .data_endpoints = {
245 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200246 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200247 sizeof(struct usb_endpoint_descriptor),
248 .bDescriptorType = USB_DT_ENDPOINT,
249 .bEndpointAddress = 0x02 | USB_DIR_OUT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200250 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200251 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200252 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200253 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
254 .bInterval = 0xFF,
255 },
256 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200257 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200258 sizeof(struct usb_endpoint_descriptor),
259 .bDescriptorType = USB_DT_ENDPOINT,
260 .bEndpointAddress = 0x03 | USB_DIR_IN,
Wolfgang Denke2601822006-06-14 18:14:56 +0200261 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200262 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200263 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200264 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
265 .bInterval = 0xFF,
266 },
267 },
268 },
Wolfgang Denke2601822006-06-14 18:14:56 +0200269};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200270
271static struct rs232_emu rs232_desc={
Wolfgang Denka1be4762008-05-20 16:00:29 +0200272 .dter = 115200,
273 .stop_bits = 0x00,
274 .parity = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200275 .data_bits = 0x08
wdenk29e7f5a2004-03-12 00:14:09 +0000276};
277
wdenk29e7f5a2004-03-12 00:14:09 +0000278
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200279/*
280 * Static Generic Serial specific data
281 */
282
283
284struct gserial_config_desc {
Wolfgang Denke2601822006-06-14 18:14:56 +0200285
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200286 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200287 struct usb_interface_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200288 interface_desc[NUM_GSERIAL_INTERFACES] __attribute__((packed));
Wolfgang Denke2601822006-06-14 18:14:56 +0200289 struct usb_endpoint_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200290 data_endpoints[NUM_ENDPOINTS] __attribute__((packed));
291
292} __attribute__((packed));
293
Wolfgang Denke2601822006-06-14 18:14:56 +0200294static struct gserial_config_desc
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200295gserial_configuration_descriptors[NUM_CONFIGS] ={
296 {
297 .configuration_desc ={
298 .bLength = sizeof(struct usb_configuration_descriptor),
299 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200300 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200301 cpu_to_le16(sizeof(struct gserial_config_desc)),
302 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
303 .bConfigurationValue = 1,
304 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200305 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200306 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
307 .bMaxPower = USBTTY_MAXPOWER
308 },
309 .interface_desc = {
310 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200311 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200312 sizeof(struct usb_interface_descriptor),
313 .bDescriptorType = USB_DT_INTERFACE,
314 .bInterfaceNumber = 0,
315 .bAlternateSetting = 0,
316 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200317 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200318 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denke2601822006-06-14 18:14:56 +0200319 .bInterfaceSubClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200320 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200321 .bInterfaceProtocol =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200322 COMMUNICATIONS_NO_PROTOCOL,
323 .iInterface = STR_DATA_INTERFACE
324 },
Wolfgang Denka1be4762008-05-20 16:00:29 +0200325 },
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200326 .data_endpoints = {
327 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200328 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200329 sizeof(struct usb_endpoint_descriptor),
330 .bDescriptorType = USB_DT_ENDPOINT,
331 .bEndpointAddress = 0x01 | USB_DIR_OUT,
332 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200333 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200334 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
335 .bInterval= 0xFF,
336 },
337 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200338 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200339 sizeof(struct usb_endpoint_descriptor),
340 .bDescriptorType = USB_DT_ENDPOINT,
341 .bEndpointAddress = 0x02 | USB_DIR_IN,
342 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200343 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200344 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200345 .bInterval = 0xFF,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200346 },
347 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200348 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200349 sizeof(struct usb_endpoint_descriptor),
350 .bDescriptorType = USB_DT_ENDPOINT,
351 .bEndpointAddress = 0x03 | USB_DIR_IN,
352 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200353 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200354 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
355 .bInterval = 0xFF,
356 },
357 },
358 },
359};
wdenk29e7f5a2004-03-12 00:14:09 +0000360
361/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200362 * Static Function Prototypes
wdenk29e7f5a2004-03-12 00:14:09 +0000363 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200364
wdenk29e7f5a2004-03-12 00:14:09 +0000365static void usbtty_init_strings (void);
366static void usbtty_init_instances (void);
367static void usbtty_init_endpoints (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200368static void usbtty_init_terminal_type(short type);
wdenk29e7f5a2004-03-12 00:14:09 +0000369static void usbtty_event_handler (struct usb_device_instance *device,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200370 usb_device_event_t event, int data);
Wolfgang Denke2601822006-06-14 18:14:56 +0200371static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200372 struct urb *urb);
wdenk29e7f5a2004-03-12 00:14:09 +0000373static int usbtty_configured (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000374static int write_buffer (circbuf_t * buf);
375static int fill_buffer (circbuf_t * buf);
376
377void usbtty_poll (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000378
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200379/* utility function for converting char* to wide string used by USB */
380static void str2wide (char *str, u16 * wide)
381{
382 int i;
383 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200384 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200385 wide[i] = (u16) str[i];
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200386 #elif defined(__BIG_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200387 wide[i] = ((u16)(str[i])<<8);
388 #else
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200389 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200390 #endif
391 }
392}
wdenk29e7f5a2004-03-12 00:14:09 +0000393
394/*
395 * Test whether a character is in the RX buffer
396 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200397
wdenk29e7f5a2004-03-12 00:14:09 +0000398int usbtty_tstc (void)
399{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200400 struct usb_endpoint_instance *endpoint =
401 &endpoint_instance[rx_endpoint];
402
403 /* If no input data exists, allow more RX to be accepted */
404 if(usbtty_input.size <= 0){
405 udc_unset_nak(endpoint->endpoint_address&0x03);
406 }
407
wdenk29e7f5a2004-03-12 00:14:09 +0000408 usbtty_poll ();
409 return (usbtty_input.size > 0);
410}
411
412/*
413 * Read a single byte from the usb client port. Returns 1 on success, 0
414 * otherwise. When the function is succesfull, the character read is
415 * written into its argument c.
416 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200417
wdenk29e7f5a2004-03-12 00:14:09 +0000418int usbtty_getc (void)
419{
420 char c;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200421 struct usb_endpoint_instance *endpoint =
422 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000423
424 while (usbtty_input.size <= 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200425 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk29e7f5a2004-03-12 00:14:09 +0000426 usbtty_poll ();
427 }
428
429 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200430 udc_set_nak(endpoint->endpoint_address&0x03);
431
wdenk29e7f5a2004-03-12 00:14:09 +0000432 return c;
433}
434
435/*
436 * Output a single byte to the usb client port.
437 */
438void usbtty_putc (const char c)
439{
440 buf_push (&usbtty_output, &c, 1);
441 /* If \n, also do \r */
442 if (c == '\n')
443 buf_push (&usbtty_output, "\r", 1);
444
445 /* Poll at end to handle new data... */
446 if ((usbtty_output.size + 2) >= usbtty_output.totalsize) {
447 usbtty_poll ();
448 }
449}
450
wdenk29e7f5a2004-03-12 00:14:09 +0000451/* usbtty_puts() helper function for finding the next '\n' in a string */
452static int next_nl_pos (const char *s)
453{
454 int i;
455
456 for (i = 0; s[i] != '\0'; i++) {
457 if (s[i] == '\n')
458 return i;
459 }
460 return i;
461}
462
463/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200464 * Output a string to the usb client port - implementing flow control
wdenk29e7f5a2004-03-12 00:14:09 +0000465 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200466
wdenk29e7f5a2004-03-12 00:14:09 +0000467static void __usbtty_puts (const char *str, int len)
468{
469 int maxlen = usbtty_output.totalsize;
470 int space, n;
471
472 /* break str into chunks < buffer size, if needed */
473 while (len > 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200474 usbtty_poll ();
wdenk29e7f5a2004-03-12 00:14:09 +0000475
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200476 space = maxlen - usbtty_output.size;
wdenk29e7f5a2004-03-12 00:14:09 +0000477 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200478 if (space) {
wdenk29e7f5a2004-03-12 00:14:09 +0000479 write_buffer (&usbtty_output);
Wolfgang Denke2601822006-06-14 18:14:56 +0200480
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200481 n = MIN (space, MIN (len, maxlen));
482 buf_push (&usbtty_output, str, n);
wdenk29e7f5a2004-03-12 00:14:09 +0000483
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200484 str += n;
Wolfgang Denke2601822006-06-14 18:14:56 +0200485 len -= n;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200486 }
wdenk29e7f5a2004-03-12 00:14:09 +0000487 }
488}
489
490void usbtty_puts (const char *str)
491{
492 int n;
493 int len = strlen (str);
494
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 */
519int drv_usbtty_init (void)
520{
521 int rc;
wdenk05822bf2004-03-12 15:38:25 +0000522 char * sn;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200523 char * tt;
wdenk05822bf2004-03-12 15:38:25 +0000524 int snlen;
wdenkc35ba4e2004-03-14 22:25:36 +0000525
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200526 /* Ger seiral number */
wdenk05822bf2004-03-12 15:38:25 +0000527 if (!(sn = getenv("serial#"))) {
528 sn = "000000000000";
529 }
530 snlen = strlen(sn);
531 if (snlen > sizeof(serial_number) - 1) {
532 printf ("Warning: serial number %s is too long (%d > %d)\n",
533 sn, snlen, sizeof(serial_number) - 1);
534 snlen = sizeof(serial_number) - 1;
535 }
536 memcpy (serial_number, sn, snlen);
537 serial_number[snlen] = '\0';
wdenk29e7f5a2004-03-12 00:14:09 +0000538
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200539 /* 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 Denke2601822006-06-14 18:14:56 +0200546
wdenk29e7f5a2004-03-12 00:14:09 +0000547 /* 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 Denk3f0137b2006-06-14 17:45:53 +0200557 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk29e7f5a2004-03-12 00:14:09 +0000558 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
578static void usbtty_init_strings (void)
579{
580 struct usb_string_descriptor *string;
581
Wolfgang Denke2601822006-06-14 18:14:56 +0200582 usbtty_string_table[STR_LANG] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200583 (struct usb_string_descriptor*)wstrLang;
584
wdenk29e7f5a2004-03-12 00:14:09 +0000585 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200586 string->bLength = sizeof(wstrManufacturer);
wdenk29e7f5a2004-03-12 00:14:09 +0000587 string->bDescriptorType = USB_DT_STRING;
588 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200589 usbtty_string_table[STR_MANUFACTURER]=string;
590
wdenk29e7f5a2004-03-12 00:14:09 +0000591
592 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200593 string->bLength = sizeof(wstrProduct);
wdenk29e7f5a2004-03-12 00:14:09 +0000594 string->bDescriptorType = USB_DT_STRING;
595 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200596 usbtty_string_table[STR_PRODUCT]=string;
597
wdenk29e7f5a2004-03-12 00:14:09 +0000598
599 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200600 string->bLength = sizeof(serial_number);
wdenk29e7f5a2004-03-12 00:14:09 +0000601 string->bDescriptorType = USB_DT_STRING;
wdenk05822bf2004-03-12 15:38:25 +0000602 str2wide (serial_number, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200603 usbtty_string_table[STR_SERIAL]=string;
604
wdenk29e7f5a2004-03-12 00:14:09 +0000605
606 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200607 string->bLength = sizeof(wstrConfiguration);
wdenk29e7f5a2004-03-12 00:14:09 +0000608 string->bDescriptorType = USB_DT_STRING;
609 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200610 usbtty_string_table[STR_CONFIG]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000611
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200612
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);
wdenk29e7f5a2004-03-12 00:14:09 +0000621 string->bDescriptorType = USB_DT_STRING;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200622 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
623 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000624
625 /* Now, initialize the string table for ep0 handling */
626 usb_strings = usbtty_string_table;
Wolfgang Denke2601822006-06-14 18:14:56 +0200627}
wdenk29e7f5a2004-03-12 00:14:09 +0000628
629static 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 Denk3f0137b2006-06-14 17:45:53 +0200638 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk29e7f5a2004-03-12 00:14:09 +0000639 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;
wdenk05822bf2004-03-12 15:38:25 +0000649 bus_instance->serial_number_str = serial_number;
wdenk29e7f5a2004-03-12 00:14:09 +0000650
651 /* configuration instance */
652 memset (config_instance, 0,
653 sizeof (struct usb_configuration_instance));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200654 config_instance->interfaces = interface_count;
655 config_instance->configuration_descriptor = configuration_descriptor;
wdenk29e7f5a2004-03-12 00:14:09 +0000656 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 Denk3f0137b2006-06-14 17:45:53 +0200686 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
wdenk29e7f5a2004-03-12 00:14:09 +0000687
wdenk29e7f5a2004-03-12 00:14:09 +0000688 endpoint_instance[i].rcv_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200689 ep_descriptor_ptrs[i - 1]->bmAttributes;
690
691 endpoint_instance[i].rcv_packetSize =
692 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
Wolfgang Denke2601822006-06-14 18:14:56 +0200693
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200694 endpoint_instance[i].tx_attributes =
695 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000696
697 endpoint_instance[i].tx_packetSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200698 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
699
wdenk29e7f5a2004-03-12 00:14:09 +0000700 endpoint_instance[i].tx_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200701 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000702
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
719static void usbtty_init_endpoints (void)
720{
721 int i;
722
723 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200724 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk29e7f5a2004-03-12 00:14:09 +0000725 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
726 }
727}
728
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200729/* usbtty_init_terminal_type
Wolfgang Denke2601822006-06-14 18:14:56 +0200730 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200731 * Do some late binding for our device type.
732 */
733static void usbtty_init_terminal_type(short type)
734{
735 switch(type){
Wolfgang Denke2601822006-06-14 18:14:56 +0200736 /* CDC ACM */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200737 case 0:
738 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200739 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200740 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denke2601822006-06-14 18:14:56 +0200741 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200742 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200743 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200744 &acm_configuration_descriptors[0].data_endpoints[1];
745
746 /* Enumerate Device Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200747 device_descriptor.bDeviceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200748 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 Denke2601822006-06-14 18:14:56 +0200755
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200756 /* 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 Denke2601822006-06-14 18:14:56 +0200769 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200770 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200771 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200772 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denke2601822006-06-14 18:14:56 +0200773 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200774 &gserial_configuration_descriptors[0].data_endpoints[2];
wdenk29e7f5a2004-03-12 00:14:09 +0000775
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200776 /* 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 Denke2601822006-06-14 18:14:56 +0200786 configuration_descriptor =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200787 (struct usb_configuration_descriptor*)
788 &gserial_configuration_descriptors;
789
790 /* Interface count */
791 interface_count = NUM_GSERIAL_INTERFACES;
792 break;
793 }
794}
795
796/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000797
798static 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
830static int write_buffer (circbuf_t * buf)
831{
832 if (!usbtty_configured ()) {
833 return 0;
834 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200835
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200836 struct usb_endpoint_instance *endpoint =
837 &endpoint_instance[tx_endpoint];
838 struct urb *current_urb = NULL;
wdenk29e7f5a2004-03-12 00:14:09 +0000839
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200840 current_urb = next_urb (device_instance, endpoint);
Wolfgang Denke2601822006-06-14 18:14:56 +0200841 /* TX data still exists - send it now
842 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200843 if(endpoint->sent < current_urb->actual_length){
844 if(udc_endpoint_write (endpoint)){
845 /* Write pre-empted by RX */
846 return -1;
847 }
848 }
wdenk29e7f5a2004-03-12 00:14:09 +0000849
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200850 if (buf->size) {
wdenk29e7f5a2004-03-12 00:14:09 +0000851 char *dest;
852
853 int space_avail;
854 int popnum, popped;
855 int total = 0;
856
Wolfgang Denke2601822006-06-14 18:14:56 +0200857 /* Break buffer into urb sized pieces,
858 * and link each to the endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200859 */
wdenk29e7f5a2004-03-12 00:14:09 +0000860 while (buf->size > 0) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200861
wdenk29e7f5a2004-03-12 00:14:09 +0000862 if (!current_urb) {
863 TTYERR ("current_urb is NULL, buf->size %d\n",
864 buf->size);
865 return total;
866 }
867
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200868 dest = (char*)current_urb->buffer +
wdenk29e7f5a2004-03-12 00:14:09 +0000869 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 Denke2601822006-06-14 18:14:56 +0200884 /* If endpoint->last == 0, then transfers have
885 * not started on this endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200886 */
wdenk29e7f5a2004-03-12 00:14:09 +0000887 if (endpoint->last == 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200888 if(udc_endpoint_write (endpoint)){
889 /* Write pre-empted by RX */
890 return -1;
891 }
wdenk29e7f5a2004-03-12 00:14:09 +0000892 }
893
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200894 }/* end while */
wdenk29e7f5a2004-03-12 00:14:09 +0000895 return total;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200896 }
wdenk29e7f5a2004-03-12 00:14:09 +0000897
898 return 0;
899}
900
901static int fill_buffer (circbuf_t * buf)
902{
903 struct usb_endpoint_instance *endpoint =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200904 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000905
906 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200907 unsigned int nb = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000908 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200909 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk29e7f5a2004-03-12 00:14:09 +0000910
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200911 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk29e7f5a2004-03-12 00:14:09 +0000912
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200913 nb = endpoint->rcv_urb->actual_length;
914 buf_push (buf, src, nb);
915 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200916
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200917 }
wdenk29e7f5a2004-03-12 00:14:09 +0000918 return nb;
919 }
wdenk29e7f5a2004-03-12 00:14:09 +0000920 return 0;
921}
922
923static int usbtty_configured (void)
924{
925 return usbtty_configured_flag;
926}
927
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200928/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000929
930static 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 Denk3f0137b2006-06-14 17:45:53 +0200950/******************************************************************************/
951
952int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
953{
954 switch (request->bRequest){
955
Wolfgang Denka1be4762008-05-20 16:00:29 +0200956 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200957 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200958 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200959 break;
960 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denke2601822006-06-14 18:14:56 +0200961 * per character */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200962 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200963 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200964 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 Denka1be4762008-05-20 16:00:29 +0200971 default:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200972 return 1;
973 }
974 return 0;
975}
wdenk29e7f5a2004-03-12 00:14:09 +0000976
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200977/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000978
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 */
984void usbtty_poll (void)
985{
986 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200987 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000988
Wolfgang Denke2601822006-06-14 18:14:56 +0200989 /* Write any output data to host buffer
990 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200991 */
wdenk29e7f5a2004-03-12 00:14:09 +0000992 if (usbtty_configured ()) {
993 write_buffer (&usbtty_output);
994 }
995
996 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200997 udc_irq();
Wolfgang Denke2601822006-06-14 18:14:56 +0200998
999 /* Check for new data from host..
1000 * (do this after checking interrupts to get latest data)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001001 */
wdenk29e7f5a2004-03-12 00:14:09 +00001002 if (usbtty_configured ()) {
1003 fill_buffer (&usbtty_input);
1004 }
1005
1006 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001007 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +00001008
wdenk29e7f5a2004-03-12 00:14:09 +00001009}
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001010
1011
wdenk29e7f5a2004-03-12 00:14:09 +00001012#endif