blob: 7eba470e49633cb995bc1d196883592a3dcf139f [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>
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010025#include <config.h>
wdenk29e7f5a2004-03-12 00:14:09 +000026#include <circbuf.h>
27#include <devices.h>
28#include "usbtty.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020029#include "usb_cdc_acm.h"
30#include "usbdescriptors.h"
wdenk29e7f5a2004-03-12 00:14:09 +000031
Jean-Christophe PLAGNIOL-VILLARD6dfaee72008-12-07 09:45:35 +010032#ifdef DEBUG
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020033#define TTYDBG(fmt,args...)\
34 serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000035#else
36#define TTYDBG(fmt,args...) do{}while(0)
37#endif
38
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020039#if 1
40#define TTYERR(fmt,args...)\
41 serial_printf("ERROR![%s] %s %d: "fmt, __FILE__,__FUNCTION__,\
42 __LINE__,##args)
wdenk29e7f5a2004-03-12 00:14:09 +000043#else
44#define TTYERR(fmt,args...) do{}while(0)
45#endif
46
47/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020048 * 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/*
wdenk29e7f5a2004-03-12 00:14:09 +000063 * Buffers to hold input and output data
64 */
65#define USBTTY_BUFFER_SIZE 256
66static circbuf_t usbtty_input;
67static circbuf_t usbtty_output;
68
69
70/*
71 * Instance variables
72 */
73static device_t usbttydev;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020074static struct usb_device_instance device_instance[1];
75static struct usb_bus_instance bus_instance[1];
wdenk29e7f5a2004-03-12 00:14:09 +000076static struct usb_configuration_instance config_instance[NUM_CONFIGS];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020077static struct usb_interface_instance interface_instance[MAX_INTERFACES];
78static struct usb_alternate_instance alternate_instance[MAX_INTERFACES];
79/* one extra for control endpoint */
80static struct usb_endpoint_instance endpoint_instance[NUM_ENDPOINTS+1];
wdenk29e7f5a2004-03-12 00:14:09 +000081
82/*
wdenk29e7f5a2004-03-12 00:14:09 +000083 * Global flag
84 */
85int usbtty_configured_flag = 0;
86
wdenk29e7f5a2004-03-12 00:14:09 +000087/*
wdenk05822bf2004-03-12 15:38:25 +000088 * Serial number
89 */
90static char serial_number[16];
91
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020092
wdenk05822bf2004-03-12 15:38:25 +000093/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020094 * Descriptors, Strings, Local variables.
wdenk29e7f5a2004-03-12 00:14:09 +000095 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020096
97/* defined and used by usbdcore_ep0.c */
98extern struct usb_string_descriptor **usb_strings;
99
100/* Indicies, References */
101static unsigned short rx_endpoint = 0;
102static unsigned short tx_endpoint = 0;
103static unsigned short interface_count = 0;
104static struct usb_string_descriptor *usbtty_string_table[STR_COUNT];
105
106/* USB Descriptor Strings */
wdenk29e7f5a2004-03-12 00:14:09 +0000107static u8 wstrLang[4] = {4,USB_DT_STRING,0x9,0x4};
108static u8 wstrManufacturer[2 + 2*(sizeof(CONFIG_USBD_MANUFACTURER)-1)];
109static u8 wstrProduct[2 + 2*(sizeof(CONFIG_USBD_PRODUCT_NAME)-1)];
wdenk05822bf2004-03-12 15:38:25 +0000110static u8 wstrSerial[2 + 2*(sizeof(serial_number) - 1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000111static u8 wstrConfiguration[2 + 2*(sizeof(CONFIG_USBD_CONFIGURATION_STR)-1)];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200112static u8 wstrDataInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
113static u8 wstrCtrlInterface[2 + 2*(sizeof(CONFIG_USBD_DATA_INTERFACE_STR)-1)];
wdenk29e7f5a2004-03-12 00:14:09 +0000114
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200115/* Standard USB Data Structures */
116static struct usb_interface_descriptor interface_descriptors[MAX_INTERFACES];
117static struct usb_endpoint_descriptor *ep_descriptor_ptrs[NUM_ENDPOINTS];
118static struct usb_configuration_descriptor *configuration_descriptor = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000119static struct usb_device_descriptor device_descriptor = {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200120 .bLength = sizeof(struct usb_device_descriptor),
121 .bDescriptorType = USB_DT_DEVICE,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200122 .bcdUSB = cpu_to_le16(USB_BCD_VERSION),
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200123 .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
wdenk29e7f5a2004-03-12 00:14:09 +0000132};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200133
134
135/*
136 * Static CDC ACM specific descriptors
137 */
138
139struct acm_config_desc {
140 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200141
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200142 /* Master Interface */
143 struct usb_interface_descriptor interface_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200144
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200145 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;
Wolfgang Denke2601822006-06-14 18:14:56 +0200153 struct usb_endpoint_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200154 data_endpoints[NUM_ENDPOINTS-1] __attribute__((packed));
155} __attribute__((packed));
156
157static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
158 {
159 .configuration_desc ={
Wolfgang Denke2601822006-06-14 18:14:56 +0200160 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200161 sizeof(struct usb_configuration_descriptor),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200162 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200163 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200164 cpu_to_le16(sizeof(struct acm_config_desc)),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200165 .bNumInterfaces = NUM_ACM_INTERFACES,
166 .bConfigurationValue = 1,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200167 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200168 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200169 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
170 .bMaxPower = USBTTY_MAXPOWER
171 },
172 /* Interface 1 */
173 .interface_desc = {
174 .bLength = sizeof(struct usb_interface_descriptor),
175 .bDescriptorType = USB_DT_INTERFACE,
176 .bInterfaceNumber = 0,
177 .bAlternateSetting = 0,
178 .bNumEndpoints = 0x01,
Wolfgang Denke2601822006-06-14 18:14:56 +0200179 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200180 COMMUNICATIONS_INTERFACE_CLASS_CONTROL,
181 .bInterfaceSubClass = COMMUNICATIONS_ACM_SUBCLASS,
182 .bInterfaceProtocol = COMMUNICATIONS_V25TER_PROTOCOL,
183 .iInterface = STR_CTRL_INTERFACE,
184 },
185 .usb_class_header = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200186 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200187 sizeof(struct usb_class_header_function_descriptor),
Wolfgang Denke2601822006-06-14 18:14:56 +0200188 .bDescriptorType = CS_INTERFACE,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200189 .bDescriptorSubtype = USB_ST_HEADER,
190 .bcdCDC = cpu_to_le16(110),
191 },
192 .usb_class_call_mgt = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200193 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200194 sizeof(struct usb_class_call_management_descriptor),
195 .bDescriptorType = CS_INTERFACE,
196 .bDescriptorSubtype = USB_ST_CMF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200197 .bmCapabilities = 0x00,
198 .bDataInterface = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200199 },
200 .usb_class_acm = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200201 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200202 sizeof(struct usb_class_abstract_control_descriptor),
203 .bDescriptorType = CS_INTERFACE,
Wolfgang Denke2601822006-06-14 18:14:56 +0200204 .bDescriptorSubtype = USB_ST_ACMF,
205 .bmCapabilities = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200206 },
207 .usb_class_union = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200208 .bFunctionLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200209 sizeof(struct usb_class_union_function_descriptor),
210 .bDescriptorType = CS_INTERFACE,
211 .bDescriptorSubtype = USB_ST_UF,
Wolfgang Denke2601822006-06-14 18:14:56 +0200212 .bMasterInterface = 0x00,
213 .bSlaveInterface0 = 0x01,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200214 },
215 .notification_endpoint = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200216 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200217 sizeof(struct usb_endpoint_descriptor),
218 .bDescriptorType = USB_DT_ENDPOINT,
219 .bEndpointAddress = 0x01 | USB_DIR_IN,
220 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200221 .wMaxPacketSize
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200222 = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
223 .bInterval = 0xFF,
224 },
225
226 /* Interface 2 */
227 .data_class_interface = {
Wolfgang Denke2601822006-06-14 18:14:56 +0200228 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200229 sizeof(struct usb_interface_descriptor),
230 .bDescriptorType = USB_DT_INTERFACE,
231 .bInterfaceNumber = 0x01,
232 .bAlternateSetting = 0x00,
233 .bNumEndpoints = 0x02,
Wolfgang Denke2601822006-06-14 18:14:56 +0200234 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200235 COMMUNICATIONS_INTERFACE_CLASS_DATA,
236 .bInterfaceSubClass = DATA_INTERFACE_SUBCLASS_NONE,
237 .bInterfaceProtocol = DATA_INTERFACE_PROTOCOL_NONE,
238 .iInterface = STR_DATA_INTERFACE,
239 },
240 .data_endpoints = {
241 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200242 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200243 sizeof(struct usb_endpoint_descriptor),
244 .bDescriptorType = USB_DT_ENDPOINT,
245 .bEndpointAddress = 0x02 | USB_DIR_OUT,
Wolfgang Denke2601822006-06-14 18:14:56 +0200246 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200247 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200248 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200249 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
250 .bInterval = 0xFF,
251 },
252 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200253 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200254 sizeof(struct usb_endpoint_descriptor),
255 .bDescriptorType = USB_DT_ENDPOINT,
256 .bEndpointAddress = 0x03 | USB_DIR_IN,
Wolfgang Denke2601822006-06-14 18:14:56 +0200257 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200258 USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200259 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200260 cpu_to_le16(CONFIG_USBD_SERIAL_BULK_PKTSIZE),
261 .bInterval = 0xFF,
262 },
263 },
264 },
Wolfgang Denke2601822006-06-14 18:14:56 +0200265};
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200266
267static struct rs232_emu rs232_desc={
Wolfgang Denka1be4762008-05-20 16:00:29 +0200268 .dter = 115200,
269 .stop_bits = 0x00,
270 .parity = 0x00,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200271 .data_bits = 0x08
wdenk29e7f5a2004-03-12 00:14:09 +0000272};
273
wdenk29e7f5a2004-03-12 00:14:09 +0000274
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200275/*
276 * Static Generic Serial specific data
277 */
278
279
280struct gserial_config_desc {
Wolfgang Denke2601822006-06-14 18:14:56 +0200281
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200282 struct usb_configuration_descriptor configuration_desc;
Wolfgang Denke2601822006-06-14 18:14:56 +0200283 struct usb_interface_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200284 interface_desc[NUM_GSERIAL_INTERFACES] __attribute__((packed));
Wolfgang Denke2601822006-06-14 18:14:56 +0200285 struct usb_endpoint_descriptor
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200286 data_endpoints[NUM_ENDPOINTS] __attribute__((packed));
287
288} __attribute__((packed));
289
Wolfgang Denke2601822006-06-14 18:14:56 +0200290static struct gserial_config_desc
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200291gserial_configuration_descriptors[NUM_CONFIGS] ={
292 {
293 .configuration_desc ={
294 .bLength = sizeof(struct usb_configuration_descriptor),
295 .bDescriptorType = USB_DT_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200296 .wTotalLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200297 cpu_to_le16(sizeof(struct gserial_config_desc)),
298 .bNumInterfaces = NUM_GSERIAL_INTERFACES,
299 .bConfigurationValue = 1,
300 .iConfiguration = STR_CONFIG,
Wolfgang Denke2601822006-06-14 18:14:56 +0200301 .bmAttributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200302 BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
303 .bMaxPower = USBTTY_MAXPOWER
304 },
305 .interface_desc = {
306 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200307 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200308 sizeof(struct usb_interface_descriptor),
309 .bDescriptorType = USB_DT_INTERFACE,
310 .bInterfaceNumber = 0,
311 .bAlternateSetting = 0,
312 .bNumEndpoints = NUM_ENDPOINTS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200313 .bInterfaceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200314 COMMUNICATIONS_INTERFACE_CLASS_VENDOR,
Wolfgang Denke2601822006-06-14 18:14:56 +0200315 .bInterfaceSubClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200316 COMMUNICATIONS_NO_SUBCLASS,
Wolfgang Denke2601822006-06-14 18:14:56 +0200317 .bInterfaceProtocol =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200318 COMMUNICATIONS_NO_PROTOCOL,
319 .iInterface = STR_DATA_INTERFACE
320 },
Wolfgang Denka1be4762008-05-20 16:00:29 +0200321 },
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200322 .data_endpoints = {
323 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200324 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200325 sizeof(struct usb_endpoint_descriptor),
326 .bDescriptorType = USB_DT_ENDPOINT,
327 .bEndpointAddress = 0x01 | USB_DIR_OUT,
328 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200329 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200330 cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
331 .bInterval= 0xFF,
332 },
333 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200334 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200335 sizeof(struct usb_endpoint_descriptor),
336 .bDescriptorType = USB_DT_ENDPOINT,
337 .bEndpointAddress = 0x02 | USB_DIR_IN,
338 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Wolfgang Denke2601822006-06-14 18:14:56 +0200339 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200340 cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
Wolfgang Denka1be4762008-05-20 16:00:29 +0200341 .bInterval = 0xFF,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200342 },
343 {
Wolfgang Denke2601822006-06-14 18:14:56 +0200344 .bLength =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200345 sizeof(struct usb_endpoint_descriptor),
346 .bDescriptorType = USB_DT_ENDPOINT,
347 .bEndpointAddress = 0x03 | USB_DIR_IN,
348 .bmAttributes = USB_ENDPOINT_XFER_INT,
Wolfgang Denka1be4762008-05-20 16:00:29 +0200349 .wMaxPacketSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200350 cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
351 .bInterval = 0xFF,
352 },
353 },
354 },
355};
wdenk29e7f5a2004-03-12 00:14:09 +0000356
357/*
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200358 * Static Function Prototypes
wdenk29e7f5a2004-03-12 00:14:09 +0000359 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200360
wdenk29e7f5a2004-03-12 00:14:09 +0000361static void usbtty_init_strings (void);
362static void usbtty_init_instances (void);
363static void usbtty_init_endpoints (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200364static void usbtty_init_terminal_type(short type);
wdenk29e7f5a2004-03-12 00:14:09 +0000365static void usbtty_event_handler (struct usb_device_instance *device,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200366 usb_device_event_t event, int data);
Wolfgang Denke2601822006-06-14 18:14:56 +0200367static int usbtty_cdc_setup(struct usb_device_request *request,
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200368 struct urb *urb);
wdenk29e7f5a2004-03-12 00:14:09 +0000369static int usbtty_configured (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000370static int write_buffer (circbuf_t * buf);
371static int fill_buffer (circbuf_t * buf);
372
373void usbtty_poll (void);
wdenk29e7f5a2004-03-12 00:14:09 +0000374
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200375/* utility function for converting char* to wide string used by USB */
376static void str2wide (char *str, u16 * wide)
377{
378 int i;
379 for (i = 0; i < strlen (str) && str[i]; i++){
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200380 #if defined(__LITTLE_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200381 wide[i] = (u16) str[i];
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200382 #elif defined(__BIG_ENDIAN)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200383 wide[i] = ((u16)(str[i])<<8);
384 #else
Rodolfo Giometti703bda92007-06-06 10:08:14 +0200385 #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200386 #endif
387 }
388}
wdenk29e7f5a2004-03-12 00:14:09 +0000389
390/*
391 * Test whether a character is in the RX buffer
392 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200393
wdenk29e7f5a2004-03-12 00:14:09 +0000394int usbtty_tstc (void)
395{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200396 struct usb_endpoint_instance *endpoint =
397 &endpoint_instance[rx_endpoint];
398
399 /* If no input data exists, allow more RX to be accepted */
400 if(usbtty_input.size <= 0){
401 udc_unset_nak(endpoint->endpoint_address&0x03);
402 }
403
wdenk29e7f5a2004-03-12 00:14:09 +0000404 usbtty_poll ();
405 return (usbtty_input.size > 0);
406}
407
408/*
409 * Read a single byte from the usb client port. Returns 1 on success, 0
410 * otherwise. When the function is succesfull, the character read is
411 * written into its argument c.
412 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200413
wdenk29e7f5a2004-03-12 00:14:09 +0000414int usbtty_getc (void)
415{
416 char c;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200417 struct usb_endpoint_instance *endpoint =
418 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000419
420 while (usbtty_input.size <= 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200421 udc_unset_nak(endpoint->endpoint_address&0x03);
wdenk29e7f5a2004-03-12 00:14:09 +0000422 usbtty_poll ();
423 }
424
425 buf_pop (&usbtty_input, &c, 1);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200426 udc_set_nak(endpoint->endpoint_address&0x03);
427
wdenk29e7f5a2004-03-12 00:14:09 +0000428 return c;
429}
430
431/*
432 * Output a single byte to the usb client port.
433 */
434void usbtty_putc (const char c)
435{
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
wdenk29e7f5a2004-03-12 00:14:09 +0000447/* usbtty_puts() helper function for finding the next '\n' in a string */
448static 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 Denk3f0137b2006-06-14 17:45:53 +0200460 * Output a string to the usb client port - implementing flow control
wdenk29e7f5a2004-03-12 00:14:09 +0000461 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200462
wdenk29e7f5a2004-03-12 00:14:09 +0000463static 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 Denk3f0137b2006-06-14 17:45:53 +0200470 usbtty_poll ();
wdenk29e7f5a2004-03-12 00:14:09 +0000471
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200472 space = maxlen - usbtty_output.size;
wdenk29e7f5a2004-03-12 00:14:09 +0000473 /* Empty buffer here, if needed, to ensure space... */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200474 if (space) {
wdenk29e7f5a2004-03-12 00:14:09 +0000475 write_buffer (&usbtty_output);
Wolfgang Denke2601822006-06-14 18:14:56 +0200476
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200477 n = MIN (space, MIN (len, maxlen));
478 buf_push (&usbtty_output, str, n);
wdenk29e7f5a2004-03-12 00:14:09 +0000479
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200480 str += n;
Wolfgang Denke2601822006-06-14 18:14:56 +0200481 len -= n;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200482 }
wdenk29e7f5a2004-03-12 00:14:09 +0000483 }
484}
485
486void usbtty_puts (const char *str)
487{
488 int n;
489 int len = strlen (str);
490
491 /* add '\r' for each '\n' */
492 while (len > 0) {
493 n = next_nl_pos (str);
494
495 if (str[n] == '\n') {
496 __usbtty_puts (str, n + 1);
497 __usbtty_puts ("\r", 1);
498 str += (n + 1);
499 len -= (n + 1);
500 } else {
501 /* No \n found. All done. */
502 __usbtty_puts (str, n);
503 break;
504 }
505 }
506
507 /* Poll at end to handle new data... */
508 usbtty_poll ();
509}
510
511/*
512 * Initialize the usb client port.
513 *
514 */
515int drv_usbtty_init (void)
516{
517 int rc;
wdenk05822bf2004-03-12 15:38:25 +0000518 char * sn;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200519 char * tt;
wdenk05822bf2004-03-12 15:38:25 +0000520 int snlen;
wdenkc35ba4e2004-03-14 22:25:36 +0000521
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200522 /* Ger seiral number */
wdenk05822bf2004-03-12 15:38:25 +0000523 if (!(sn = getenv("serial#"))) {
524 sn = "000000000000";
525 }
526 snlen = strlen(sn);
527 if (snlen > sizeof(serial_number) - 1) {
Wolfgang Denk509cd072008-07-14 15:06:35 +0200528 printf ("Warning: serial number %s is too long (%d > %lu)\n",
529 sn, snlen, (ulong)(sizeof(serial_number) - 1));
wdenk05822bf2004-03-12 15:38:25 +0000530 snlen = sizeof(serial_number) - 1;
531 }
532 memcpy (serial_number, sn, snlen);
533 serial_number[snlen] = '\0';
wdenk29e7f5a2004-03-12 00:14:09 +0000534
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200535 /* Decide on which type of UDC device to be.
536 */
537
538 if(!(tt = getenv("usbtty"))) {
539 tt = "generic";
540 }
541 usbtty_init_terminal_type(strcmp(tt,"cdc_acm"));
Wolfgang Denke2601822006-06-14 18:14:56 +0200542
wdenk29e7f5a2004-03-12 00:14:09 +0000543 /* prepare buffers... */
544 buf_init (&usbtty_input, USBTTY_BUFFER_SIZE);
545 buf_init (&usbtty_output, USBTTY_BUFFER_SIZE);
546
547 /* Now, set up USB controller and infrastructure */
548 udc_init (); /* Basic USB initialization */
549
550 usbtty_init_strings ();
551 usbtty_init_instances ();
552
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200553 udc_startup_events (device_instance);/* Enable dev, init udc pointers */
wdenk29e7f5a2004-03-12 00:14:09 +0000554 udc_connect (); /* Enable pullup for host detection */
555
556 usbtty_init_endpoints ();
557
558 /* Device initialization */
559 memset (&usbttydev, 0, sizeof (usbttydev));
560
561 strcpy (usbttydev.name, "usbtty");
562 usbttydev.ext = 0; /* No extensions */
563 usbttydev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
564 usbttydev.tstc = usbtty_tstc; /* 'tstc' function */
565 usbttydev.getc = usbtty_getc; /* 'getc' function */
566 usbttydev.putc = usbtty_putc; /* 'putc' function */
567 usbttydev.puts = usbtty_puts; /* 'puts' function */
568
569 rc = device_register (&usbttydev);
570
571 return (rc == 0) ? 1 : rc;
572}
573
574static void usbtty_init_strings (void)
575{
576 struct usb_string_descriptor *string;
577
Wolfgang Denke2601822006-06-14 18:14:56 +0200578 usbtty_string_table[STR_LANG] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200579 (struct usb_string_descriptor*)wstrLang;
580
wdenk29e7f5a2004-03-12 00:14:09 +0000581 string = (struct usb_string_descriptor *) wstrManufacturer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200582 string->bLength = sizeof(wstrManufacturer);
wdenk29e7f5a2004-03-12 00:14:09 +0000583 string->bDescriptorType = USB_DT_STRING;
584 str2wide (CONFIG_USBD_MANUFACTURER, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200585 usbtty_string_table[STR_MANUFACTURER]=string;
586
wdenk29e7f5a2004-03-12 00:14:09 +0000587
588 string = (struct usb_string_descriptor *) wstrProduct;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200589 string->bLength = sizeof(wstrProduct);
wdenk29e7f5a2004-03-12 00:14:09 +0000590 string->bDescriptorType = USB_DT_STRING;
591 str2wide (CONFIG_USBD_PRODUCT_NAME, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200592 usbtty_string_table[STR_PRODUCT]=string;
593
wdenk29e7f5a2004-03-12 00:14:09 +0000594
595 string = (struct usb_string_descriptor *) wstrSerial;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200596 string->bLength = sizeof(serial_number);
wdenk29e7f5a2004-03-12 00:14:09 +0000597 string->bDescriptorType = USB_DT_STRING;
wdenk05822bf2004-03-12 15:38:25 +0000598 str2wide (serial_number, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200599 usbtty_string_table[STR_SERIAL]=string;
600
wdenk29e7f5a2004-03-12 00:14:09 +0000601
602 string = (struct usb_string_descriptor *) wstrConfiguration;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200603 string->bLength = sizeof(wstrConfiguration);
wdenk29e7f5a2004-03-12 00:14:09 +0000604 string->bDescriptorType = USB_DT_STRING;
605 str2wide (CONFIG_USBD_CONFIGURATION_STR, string->wData);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200606 usbtty_string_table[STR_CONFIG]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000607
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200608
609 string = (struct usb_string_descriptor *) wstrDataInterface;
610 string->bLength = sizeof(wstrDataInterface);
611 string->bDescriptorType = USB_DT_STRING;
612 str2wide (CONFIG_USBD_DATA_INTERFACE_STR, string->wData);
613 usbtty_string_table[STR_DATA_INTERFACE]=string;
614
615 string = (struct usb_string_descriptor *) wstrCtrlInterface;
616 string->bLength = sizeof(wstrCtrlInterface);
wdenk29e7f5a2004-03-12 00:14:09 +0000617 string->bDescriptorType = USB_DT_STRING;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200618 str2wide (CONFIG_USBD_CTRL_INTERFACE_STR, string->wData);
619 usbtty_string_table[STR_CTRL_INTERFACE]=string;
wdenk29e7f5a2004-03-12 00:14:09 +0000620
621 /* Now, initialize the string table for ep0 handling */
622 usb_strings = usbtty_string_table;
Wolfgang Denke2601822006-06-14 18:14:56 +0200623}
wdenk29e7f5a2004-03-12 00:14:09 +0000624
625static void usbtty_init_instances (void)
626{
627 int i;
628
629 /* initialize device instance */
630 memset (device_instance, 0, sizeof (struct usb_device_instance));
631 device_instance->device_state = STATE_INIT;
632 device_instance->device_descriptor = &device_descriptor;
633 device_instance->event = usbtty_event_handler;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200634 device_instance->cdc_recv_setup = usbtty_cdc_setup;
wdenk29e7f5a2004-03-12 00:14:09 +0000635 device_instance->bus = bus_instance;
636 device_instance->configurations = NUM_CONFIGS;
637 device_instance->configuration_instance_array = config_instance;
638
639 /* initialize bus instance */
640 memset (bus_instance, 0, sizeof (struct usb_bus_instance));
641 bus_instance->device = device_instance;
642 bus_instance->endpoint_array = endpoint_instance;
643 bus_instance->max_endpoints = 1;
644 bus_instance->maxpacketsize = 64;
wdenk05822bf2004-03-12 15:38:25 +0000645 bus_instance->serial_number_str = serial_number;
wdenk29e7f5a2004-03-12 00:14:09 +0000646
647 /* configuration instance */
648 memset (config_instance, 0,
649 sizeof (struct usb_configuration_instance));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200650 config_instance->interfaces = interface_count;
651 config_instance->configuration_descriptor = configuration_descriptor;
wdenk29e7f5a2004-03-12 00:14:09 +0000652 config_instance->interface_instance_array = interface_instance;
653
654 /* interface instance */
655 memset (interface_instance, 0,
656 sizeof (struct usb_interface_instance));
657 interface_instance->alternates = 1;
658 interface_instance->alternates_instance_array = alternate_instance;
659
660 /* alternates instance */
661 memset (alternate_instance, 0,
662 sizeof (struct usb_alternate_instance));
663 alternate_instance->interface_descriptor = interface_descriptors;
664 alternate_instance->endpoints = NUM_ENDPOINTS;
665 alternate_instance->endpoints_descriptor_array = ep_descriptor_ptrs;
666
667 /* endpoint instances */
668 memset (&endpoint_instance[0], 0,
669 sizeof (struct usb_endpoint_instance));
670 endpoint_instance[0].endpoint_address = 0;
671 endpoint_instance[0].rcv_packetSize = EP0_MAX_PACKET_SIZE;
672 endpoint_instance[0].rcv_attributes = USB_ENDPOINT_XFER_CONTROL;
673 endpoint_instance[0].tx_packetSize = EP0_MAX_PACKET_SIZE;
674 endpoint_instance[0].tx_attributes = USB_ENDPOINT_XFER_CONTROL;
675 udc_setup_ep (device_instance, 0, &endpoint_instance[0]);
676
677 for (i = 1; i <= NUM_ENDPOINTS; i++) {
678 memset (&endpoint_instance[i], 0,
679 sizeof (struct usb_endpoint_instance));
680
681 endpoint_instance[i].endpoint_address =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200682 ep_descriptor_ptrs[i - 1]->bEndpointAddress;
wdenk29e7f5a2004-03-12 00:14:09 +0000683
wdenk29e7f5a2004-03-12 00:14:09 +0000684 endpoint_instance[i].rcv_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200685 ep_descriptor_ptrs[i - 1]->bmAttributes;
686
687 endpoint_instance[i].rcv_packetSize =
688 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
Wolfgang Denke2601822006-06-14 18:14:56 +0200689
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200690 endpoint_instance[i].tx_attributes =
691 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000692
693 endpoint_instance[i].tx_packetSize =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200694 le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
695
wdenk29e7f5a2004-03-12 00:14:09 +0000696 endpoint_instance[i].tx_attributes =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200697 ep_descriptor_ptrs[i - 1]->bmAttributes;
wdenk29e7f5a2004-03-12 00:14:09 +0000698
699 urb_link_init (&endpoint_instance[i].rcv);
700 urb_link_init (&endpoint_instance[i].rdy);
701 urb_link_init (&endpoint_instance[i].tx);
702 urb_link_init (&endpoint_instance[i].done);
703
704 if (endpoint_instance[i].endpoint_address & USB_DIR_IN)
705 endpoint_instance[i].tx_urb =
706 usbd_alloc_urb (device_instance,
707 &endpoint_instance[i]);
708 else
709 endpoint_instance[i].rcv_urb =
710 usbd_alloc_urb (device_instance,
711 &endpoint_instance[i]);
712 }
713}
714
715static void usbtty_init_endpoints (void)
716{
717 int i;
718
719 bus_instance->max_endpoints = NUM_ENDPOINTS + 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200720 for (i = 1; i <= NUM_ENDPOINTS; i++) {
wdenk29e7f5a2004-03-12 00:14:09 +0000721 udc_setup_ep (device_instance, i, &endpoint_instance[i]);
722 }
723}
724
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200725/* usbtty_init_terminal_type
Wolfgang Denke2601822006-06-14 18:14:56 +0200726 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200727 * Do some late binding for our device type.
728 */
729static void usbtty_init_terminal_type(short type)
730{
731 switch(type){
Wolfgang Denke2601822006-06-14 18:14:56 +0200732 /* CDC ACM */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200733 case 0:
734 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200735 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200736 &acm_configuration_descriptors[0].notification_endpoint;
Wolfgang Denke2601822006-06-14 18:14:56 +0200737 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200738 &acm_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200739 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200740 &acm_configuration_descriptors[0].data_endpoints[1];
741
742 /* Enumerate Device Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200743 device_descriptor.bDeviceClass =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200744 COMMUNICATIONS_DEVICE_CLASS;
745 device_descriptor.idProduct =
746 cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
747
748 /* Assign endpoint indices */
749 tx_endpoint = ACM_TX_ENDPOINT;
750 rx_endpoint = ACM_RX_ENDPOINT;
Wolfgang Denke2601822006-06-14 18:14:56 +0200751
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200752 /* Configuration Descriptor */
753 configuration_descriptor =
754 (struct usb_configuration_descriptor*)
755 &acm_configuration_descriptors;
756
757 /* Interface count */
758 interface_count = NUM_ACM_INTERFACES;
759 break;
760
761 /* BULK IN/OUT & Default */
762 case 1:
763 default:
764 /* Assign endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200765 ep_descriptor_ptrs[0] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200766 &gserial_configuration_descriptors[0].data_endpoints[0];
Wolfgang Denke2601822006-06-14 18:14:56 +0200767 ep_descriptor_ptrs[1] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200768 &gserial_configuration_descriptors[0].data_endpoints[1];
Wolfgang Denke2601822006-06-14 18:14:56 +0200769 ep_descriptor_ptrs[2] =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200770 &gserial_configuration_descriptors[0].data_endpoints[2];
wdenk29e7f5a2004-03-12 00:14:09 +0000771
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200772 /* Enumerate Device Descriptor */
773 device_descriptor.bDeviceClass = 0xFF;
774 device_descriptor.idProduct =
775 cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
776
777 /* Assign endpoint indices */
778 tx_endpoint = GSERIAL_TX_ENDPOINT;
779 rx_endpoint = GSERIAL_RX_ENDPOINT;
780
781 /* Configuration Descriptor */
Wolfgang Denke2601822006-06-14 18:14:56 +0200782 configuration_descriptor =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200783 (struct usb_configuration_descriptor*)
784 &gserial_configuration_descriptors;
785
786 /* Interface count */
787 interface_count = NUM_GSERIAL_INTERFACES;
788 break;
789 }
790}
791
792/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000793
794static struct urb *next_urb (struct usb_device_instance *device,
795 struct usb_endpoint_instance *endpoint)
796{
797 struct urb *current_urb = NULL;
798 int space;
799
800 /* If there's a queue, then we should add to the last urb */
801 if (!endpoint->tx_queue) {
802 current_urb = endpoint->tx_urb;
803 } else {
804 /* Last urb from tx chain */
805 current_urb =
806 p2surround (struct urb, link, endpoint->tx.prev);
807 }
808
809 /* Make sure this one has enough room */
810 space = current_urb->buffer_length - current_urb->actual_length;
811 if (space > 0) {
812 return current_urb;
813 } else { /* No space here */
814 /* First look at done list */
815 current_urb = first_urb_detached (&endpoint->done);
816 if (!current_urb) {
817 current_urb = usbd_alloc_urb (device, endpoint);
818 }
819
820 urb_append (&endpoint->tx, current_urb);
821 endpoint->tx_queue++;
822 }
823 return current_urb;
824}
825
826static int write_buffer (circbuf_t * buf)
827{
828 if (!usbtty_configured ()) {
829 return 0;
830 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200831
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200832 struct usb_endpoint_instance *endpoint =
833 &endpoint_instance[tx_endpoint];
834 struct urb *current_urb = NULL;
wdenk29e7f5a2004-03-12 00:14:09 +0000835
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200836 current_urb = next_urb (device_instance, endpoint);
Wolfgang Denke2601822006-06-14 18:14:56 +0200837 /* TX data still exists - send it now
838 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200839 if(endpoint->sent < current_urb->actual_length){
840 if(udc_endpoint_write (endpoint)){
841 /* Write pre-empted by RX */
842 return -1;
843 }
844 }
wdenk29e7f5a2004-03-12 00:14:09 +0000845
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200846 if (buf->size) {
wdenk29e7f5a2004-03-12 00:14:09 +0000847 char *dest;
848
849 int space_avail;
850 int popnum, popped;
851 int total = 0;
852
Wolfgang Denke2601822006-06-14 18:14:56 +0200853 /* Break buffer into urb sized pieces,
854 * and link each to the endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200855 */
wdenk29e7f5a2004-03-12 00:14:09 +0000856 while (buf->size > 0) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200857
wdenk29e7f5a2004-03-12 00:14:09 +0000858 if (!current_urb) {
859 TTYERR ("current_urb is NULL, buf->size %d\n",
860 buf->size);
861 return total;
862 }
863
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200864 dest = (char*)current_urb->buffer +
wdenk29e7f5a2004-03-12 00:14:09 +0000865 current_urb->actual_length;
866
867 space_avail =
868 current_urb->buffer_length -
869 current_urb->actual_length;
870 popnum = MIN (space_avail, buf->size);
871 if (popnum == 0)
872 break;
873
874 popped = buf_pop (buf, dest, popnum);
875 if (popped == 0)
876 break;
877 current_urb->actual_length += popped;
878 total += popped;
879
Wolfgang Denke2601822006-06-14 18:14:56 +0200880 /* If endpoint->last == 0, then transfers have
881 * not started on this endpoint
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200882 */
wdenk29e7f5a2004-03-12 00:14:09 +0000883 if (endpoint->last == 0) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200884 if(udc_endpoint_write (endpoint)){
885 /* Write pre-empted by RX */
886 return -1;
887 }
wdenk29e7f5a2004-03-12 00:14:09 +0000888 }
889
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200890 }/* end while */
wdenk29e7f5a2004-03-12 00:14:09 +0000891 return total;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200892 }
wdenk29e7f5a2004-03-12 00:14:09 +0000893
894 return 0;
895}
896
897static int fill_buffer (circbuf_t * buf)
898{
899 struct usb_endpoint_instance *endpoint =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200900 &endpoint_instance[rx_endpoint];
wdenk29e7f5a2004-03-12 00:14:09 +0000901
902 if (endpoint->rcv_urb && endpoint->rcv_urb->actual_length) {
Wolfgang Denke2601822006-06-14 18:14:56 +0200903 unsigned int nb = 0;
wdenk29e7f5a2004-03-12 00:14:09 +0000904 char *src = (char *) endpoint->rcv_urb->buffer;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200905 unsigned int rx_avail = buf->totalsize - buf->size;
wdenk29e7f5a2004-03-12 00:14:09 +0000906
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200907 if(rx_avail >= endpoint->rcv_urb->actual_length){
wdenk29e7f5a2004-03-12 00:14:09 +0000908
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200909 nb = endpoint->rcv_urb->actual_length;
910 buf_push (buf, src, nb);
911 endpoint->rcv_urb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200912
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200913 }
wdenk29e7f5a2004-03-12 00:14:09 +0000914 return nb;
915 }
wdenk29e7f5a2004-03-12 00:14:09 +0000916 return 0;
917}
918
919static int usbtty_configured (void)
920{
921 return usbtty_configured_flag;
922}
923
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200924/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000925
926static void usbtty_event_handler (struct usb_device_instance *device,
927 usb_device_event_t event, int data)
928{
929 switch (event) {
930 case DEVICE_RESET:
931 case DEVICE_BUS_INACTIVE:
932 usbtty_configured_flag = 0;
933 break;
934 case DEVICE_CONFIGURED:
935 usbtty_configured_flag = 1;
936 break;
937
938 case DEVICE_ADDRESS_ASSIGNED:
939 usbtty_init_endpoints ();
940
941 default:
942 break;
943 }
944}
945
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200946/******************************************************************************/
947
948int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
949{
950 switch (request->bRequest){
951
Wolfgang Denka1be4762008-05-20 16:00:29 +0200952 case ACM_SET_CONTROL_LINE_STATE: /* Implies DTE ready */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200953 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200954 case ACM_SEND_ENCAPSULATED_COMMAND : /* Required */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200955 break;
956 case ACM_SET_LINE_ENCODING : /* DTE stop/parity bits
Wolfgang Denke2601822006-06-14 18:14:56 +0200957 * per character */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200958 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200959 case ACM_GET_ENCAPSULATED_RESPONSE : /* request response */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200960 break;
961 case ACM_GET_LINE_ENCODING : /* request DTE rate,
962 * stop/parity bits */
963 memcpy (urb->buffer , &rs232_desc, sizeof(rs232_desc));
964 urb->actual_length = sizeof(rs232_desc);
965
966 break;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200967 default:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200968 return 1;
969 }
970 return 0;
971}
wdenk29e7f5a2004-03-12 00:14:09 +0000972
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200973/******************************************************************************/
wdenk29e7f5a2004-03-12 00:14:09 +0000974
975/*
976 * Since interrupt handling has not yet been implemented, we use this function
977 * to handle polling. This is called by the tstc,getc,putc,puts routines to
978 * update the USB state.
979 */
980void usbtty_poll (void)
981{
982 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200983 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +0000984
Wolfgang Denke2601822006-06-14 18:14:56 +0200985 /* Write any output data to host buffer
986 * (do this before checking interrupts to avoid missing one)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200987 */
wdenk29e7f5a2004-03-12 00:14:09 +0000988 if (usbtty_configured ()) {
989 write_buffer (&usbtty_output);
990 }
991
992 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200993 udc_irq();
Wolfgang Denke2601822006-06-14 18:14:56 +0200994
995 /* Check for new data from host..
996 * (do this after checking interrupts to get latest data)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200997 */
wdenk29e7f5a2004-03-12 00:14:09 +0000998 if (usbtty_configured ()) {
999 fill_buffer (&usbtty_input);
1000 }
1001
1002 /* New interrupts? */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001003 udc_irq();
wdenk29e7f5a2004-03-12 00:14:09 +00001004
wdenk29e7f5a2004-03-12 00:14:09 +00001005}