]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/serial/usbtty.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / drivers / serial / usbtty.c
index a3b50131dfc1bc47c20f58a3927364e6dd12c6f3..8e0815334431a1c99b115e3f8a38bdb16a5fa869 100644 (file)
  */
 
 #include <common.h>
-
-#ifdef CONFIG_USB_TTY
-
+#include <config.h>
 #include <circbuf.h>
-#include <devices.h>
+#include <stdio_dev.h>
+#include <asm/unaligned.h>
 #include "usbtty.h"
 #include "usb_cdc_acm.h"
 #include "usbdescriptors.h"
-#include <config.h>            /* If defined, override Linux identifiers with
-                                * vendor specific ones */
 
-#if 0
+#ifdef DEBUG
 #define TTYDBG(fmt,args...)\
        serial_printf("[%s] %s %d: "fmt, __FILE__,__FUNCTION__,__LINE__,##args)
 #else
@@ -66,7 +63,7 @@
 /*
  * Buffers to hold input and output data
  */
-#define USBTTY_BUFFER_SIZE 256
+#define USBTTY_BUFFER_SIZE 2048
 static circbuf_t usbtty_input;
 static circbuf_t usbtty_output;
 
@@ -74,7 +71,7 @@ static circbuf_t usbtty_output;
 /*
  * Instance variables
  */
-static device_t usbttydev;
+static struct stdio_dev usbttydev;
 static struct usb_device_instance device_instance[1];
 static struct usb_bus_instance bus_instance[1];
 static struct usb_configuration_instance config_instance[NUM_CONFIGS];
@@ -98,7 +95,7 @@ static char serial_number[16];
  * Descriptors, Strings, Local variables.
  */
 
-/* defined and used by usbdcore_ep0.c */
+/* defined and used by gadget/ep0.c */
 extern struct usb_string_descriptor **usb_strings;
 
 /* Indicies, References */
@@ -123,7 +120,7 @@ static struct usb_configuration_descriptor  *configuration_descriptor = 0;
 static struct usb_device_descriptor device_descriptor = {
        .bLength = sizeof(struct usb_device_descriptor),
        .bDescriptorType =      USB_DT_DEVICE,
-       .bcdUSB =               cpu_to_le16(USB_BCD_VERSION),
+       .bcdUSB =               cpu_to_le16(USB_BCD_VERSION),
        .bDeviceSubClass =      0x00,
        .bDeviceProtocol =      0x00,
        .bMaxPacketSize0 =      EP0_MAX_PACKET_SIZE,
@@ -136,6 +133,19 @@ static struct usb_device_descriptor device_descriptor = {
 };
 
 
+#if defined(CONFIG_USBD_HS)
+static struct usb_qualifier_descriptor qualifier_descriptor = {
+       .bLength = sizeof(struct usb_qualifier_descriptor),
+       .bDescriptorType =      USB_DT_QUAL,
+       .bcdUSB =               cpu_to_le16(USB_BCD_VERSION),
+       .bDeviceClass =         COMMUNICATIONS_DEVICE_CLASS,
+       .bDeviceSubClass =      0x00,
+       .bDeviceProtocol =      0x00,
+       .bMaxPacketSize0 =      EP0_MAX_PACKET_SIZE,
+       .bNumConfigurations =   NUM_CONFIGS
+};
+#endif
+
 /*
  * Static CDC ACM specific descriptors
  */
@@ -154,8 +164,7 @@ struct acm_config_desc {
 
        /* Slave Interface */
        struct usb_interface_descriptor data_class_interface;
-       struct usb_endpoint_descriptor
-               data_endpoints[NUM_ENDPOINTS-1] __attribute__((packed));
+       struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS-1];
 } __attribute__((packed));
 
 static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
@@ -163,11 +172,11 @@ static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
                .configuration_desc ={
                        .bLength =
                                sizeof(struct usb_configuration_descriptor),
-                       .bDescriptorType = USB_DT_CONFIG,
+                       .bDescriptorType = USB_DT_CONFIG,
                        .wTotalLength =
                                cpu_to_le16(sizeof(struct acm_config_desc)),
-                       .bNumInterfaces = NUM_ACM_INTERFACES,
-                       .bConfigurationValue = 1,
+                       .bNumInterfaces = NUM_ACM_INTERFACES,
+                       .bConfigurationValue = 1,
                        .iConfiguration = STR_CONFIG,
                        .bmAttributes =
                                BMATTRIBUTE_SELF_POWERED|BMATTRIBUTE_RESERVED,
@@ -220,7 +229,7 @@ static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
                        .bLength =
                                sizeof(struct usb_endpoint_descriptor),
                        .bDescriptorType        = USB_DT_ENDPOINT,
-                       .bEndpointAddress       = 0x01 | USB_DIR_IN,
+                       .bEndpointAddress       = UDC_INT_ENDPOINT | USB_DIR_IN,
                        .bmAttributes           = USB_ENDPOINT_XFER_INT,
                        .wMaxPacketSize
                                = cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
@@ -246,7 +255,7 @@ static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
                                .bLength                =
                                        sizeof(struct usb_endpoint_descriptor),
                                .bDescriptorType        = USB_DT_ENDPOINT,
-                               .bEndpointAddress       = 0x02 | USB_DIR_OUT,
+                               .bEndpointAddress       = UDC_OUT_ENDPOINT | USB_DIR_OUT,
                                .bmAttributes           =
                                        USB_ENDPOINT_XFER_BULK,
                                .wMaxPacketSize         =
@@ -257,7 +266,7 @@ static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
                                .bLength                =
                                        sizeof(struct usb_endpoint_descriptor),
                                .bDescriptorType        = USB_DT_ENDPOINT,
-                               .bEndpointAddress       = 0x03 | USB_DIR_IN,
+                               .bEndpointAddress       = UDC_IN_ENDPOINT | USB_DIR_IN,
                                .bmAttributes           =
                                        USB_ENDPOINT_XFER_BULK,
                                .wMaxPacketSize         =
@@ -269,9 +278,9 @@ static struct acm_config_desc acm_configuration_descriptors[NUM_CONFIGS] = {
 };
 
 static struct rs232_emu rs232_desc={
-               .dter           =       115200,
-               .stop_bits      =       0x00,
-               .parity         =       0x00,
+               .dter           =       115200,
+               .stop_bits      =       0x00,
+               .parity         =       0x00,
                .data_bits      =       0x08
 };
 
@@ -284,10 +293,8 @@ static struct rs232_emu rs232_desc={
 struct gserial_config_desc {
 
        struct usb_configuration_descriptor configuration_desc;
-       struct usb_interface_descriptor
-               interface_desc[NUM_GSERIAL_INTERFACES] __attribute__((packed));
-       struct usb_endpoint_descriptor
-               data_endpoints[NUM_ENDPOINTS] __attribute__((packed));
+       struct usb_interface_descriptor interface_desc[NUM_GSERIAL_INTERFACES];
+       struct usb_endpoint_descriptor data_endpoints[NUM_ENDPOINTS];
 
 } __attribute__((packed));
 
@@ -322,13 +329,13 @@ gserial_configuration_descriptors[NUM_CONFIGS] ={
                                        COMMUNICATIONS_NO_PROTOCOL,
                                .iInterface = STR_DATA_INTERFACE
                        },
-               },
+               },
                .data_endpoints  = {
                        {
                                .bLength =
                                        sizeof(struct usb_endpoint_descriptor),
                                .bDescriptorType =      USB_DT_ENDPOINT,
-                               .bEndpointAddress =     0x01 | USB_DIR_OUT,
+                               .bEndpointAddress =     UDC_OUT_ENDPOINT | USB_DIR_OUT,
                                .bmAttributes =         USB_ENDPOINT_XFER_BULK,
                                .wMaxPacketSize =
                                        cpu_to_le16(CONFIG_USBD_SERIAL_OUT_PKTSIZE),
@@ -338,19 +345,19 @@ gserial_configuration_descriptors[NUM_CONFIGS] ={
                                .bLength =
                                        sizeof(struct usb_endpoint_descriptor),
                                .bDescriptorType =      USB_DT_ENDPOINT,
-                               .bEndpointAddress =     0x02 | USB_DIR_IN,
+                               .bEndpointAddress =     UDC_IN_ENDPOINT | USB_DIR_IN,
                                .bmAttributes =         USB_ENDPOINT_XFER_BULK,
                                .wMaxPacketSize =
                                        cpu_to_le16(CONFIG_USBD_SERIAL_IN_PKTSIZE),
-                               .bInterval =            0xFF,
+                               .bInterval =            0xFF,
                        },
                        {
                                .bLength =
                                        sizeof(struct usb_endpoint_descriptor),
                                .bDescriptorType =      USB_DT_ENDPOINT,
-                               .bEndpointAddress =     0x03 | USB_DIR_IN,
+                               .bEndpointAddress =     UDC_INT_ENDPOINT | USB_DIR_IN,
                                .bmAttributes =         USB_ENDPOINT_XFER_INT,
-                               .wMaxPacketSize =
+                               .wMaxPacketSize =
                                        cpu_to_le16(CONFIG_USBD_SERIAL_INT_PKTSIZE),
                                .bInterval =            0xFF,
                        },
@@ -437,6 +444,9 @@ int usbtty_getc (void)
  */
 void usbtty_putc (const char c)
 {
+       if (!usbtty_configured ())
+               return;
+
        buf_push (&usbtty_output, &c, 1);
        /* If \n, also do \r */
        if (c == '\n')
@@ -490,8 +500,12 @@ static void __usbtty_puts (const char *str, int len)
 void usbtty_puts (const char *str)
 {
        int n;
-       int len = strlen (str);
+       int len;
 
+       if (!usbtty_configured ())
+               return;
+
+       len = strlen (str);
        /* add '\r' for each '\n' */
        while (len > 0) {
                n = next_nl_pos (str);
@@ -529,8 +543,8 @@ int drv_usbtty_init (void)
        }
        snlen = strlen(sn);
        if (snlen > sizeof(serial_number) - 1) {
-               printf ("Warning: serial number %s is too long (%d > %d)\n",
-                       sn, snlen, sizeof(serial_number) - 1);
+               printf ("Warning: serial number %s is too long (%d > %lu)\n",
+                       sn, snlen, (ulong)(sizeof(serial_number) - 1));
                snlen = sizeof(serial_number) - 1;
        }
        memcpy (serial_number, sn, snlen);
@@ -554,11 +568,11 @@ int drv_usbtty_init (void)
        usbtty_init_strings ();
        usbtty_init_instances ();
 
+       usbtty_init_endpoints ();
+
        udc_startup_events (device_instance);/* Enable dev, init udc pointers */
        udc_connect ();         /* Enable pullup for host detection */
 
-       usbtty_init_endpoints ();
-
        /* Device initialization */
        memset (&usbttydev, 0, sizeof (usbttydev));
 
@@ -570,7 +584,7 @@ int drv_usbtty_init (void)
        usbttydev.putc = usbtty_putc;   /* 'putc' function */
        usbttydev.puts = usbtty_puts;   /* 'puts' function */
 
-       rc = device_register (&usbttydev);
+       rc = stdio_register (&usbttydev);
 
        return (rc == 0) ? 1 : rc;
 }
@@ -626,6 +640,9 @@ static void usbtty_init_strings (void)
        usb_strings = usbtty_string_table;
 }
 
+#define init_wMaxPacketSize(x) le16_to_cpu(get_unaligned(\
+                       &ep_descriptor_ptrs[(x) - 1]->wMaxPacketSize));
+
 static void usbtty_init_instances (void)
 {
        int i;
@@ -634,6 +651,9 @@ static void usbtty_init_instances (void)
        memset (device_instance, 0, sizeof (struct usb_device_instance));
        device_instance->device_state = STATE_INIT;
        device_instance->device_descriptor = &device_descriptor;
+#if defined(CONFIG_USBD_HS)
+       device_instance->qualifier_descriptor = &qualifier_descriptor;
+#endif
        device_instance->event = usbtty_event_handler;
        device_instance->cdc_recv_setup = usbtty_cdc_setup;
        device_instance->bus = bus_instance;
@@ -688,14 +708,12 @@ static void usbtty_init_instances (void)
                endpoint_instance[i].rcv_attributes =
                        ep_descriptor_ptrs[i - 1]->bmAttributes;
 
-               endpoint_instance[i].rcv_packetSize =
-                       le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
+               endpoint_instance[i].rcv_packetSize = init_wMaxPacketSize(i);
 
                endpoint_instance[i].tx_attributes =
                        ep_descriptor_ptrs[i - 1]->bmAttributes;
 
-               endpoint_instance[i].tx_packetSize =
-                       le16_to_cpu(ep_descriptor_ptrs[i - 1]->wMaxPacketSize);
+               endpoint_instance[i].tx_packetSize = init_wMaxPacketSize(i);
 
                endpoint_instance[i].tx_attributes =
                        ep_descriptor_ptrs[i - 1]->bmAttributes;
@@ -749,6 +767,10 @@ static void usbtty_init_terminal_type(short type)
                        device_descriptor.idProduct =
                                cpu_to_le16(CONFIG_USBD_PRODUCTID_CDCACM);
 
+#if defined(CONFIG_USBD_HS)
+                       qualifier_descriptor.bDeviceClass =
+                               COMMUNICATIONS_DEVICE_CLASS;
+#endif
                        /* Assign endpoint indices */
                        tx_endpoint = ACM_TX_ENDPOINT;
                        rx_endpoint = ACM_RX_ENDPOINT;
@@ -777,7 +799,9 @@ static void usbtty_init_terminal_type(short type)
                        device_descriptor.bDeviceClass = 0xFF;
                        device_descriptor.idProduct =
                                cpu_to_le16(CONFIG_USBD_PRODUCTID_GSERIAL);
-
+#if defined(CONFIG_USBD_HS)
+                       qualifier_descriptor.bDeviceClass = 0xFF;
+#endif
                        /* Assign endpoint indices */
                        tx_endpoint = GSERIAL_TX_ENDPOINT;
                        rx_endpoint = GSERIAL_RX_ENDPOINT;
@@ -930,6 +954,9 @@ static int usbtty_configured (void)
 static void usbtty_event_handler (struct usb_device_instance *device,
                                  usb_device_event_t event, int data)
 {
+#if defined(CONFIG_USBD_HS)
+       int i;
+#endif
        switch (event) {
        case DEVICE_RESET:
        case DEVICE_BUS_INACTIVE:
@@ -940,6 +967,29 @@ static void usbtty_event_handler (struct usb_device_instance *device,
                break;
 
        case DEVICE_ADDRESS_ASSIGNED:
+#if defined(CONFIG_USBD_HS)
+               /*
+                * is_usbd_high_speed routine needs to be defined by
+                * specific gadget driver
+                * It returns true if device enumerates at High speed
+                * Retuns false otherwise
+                */
+               for (i = 0; i < NUM_ENDPOINTS; i++) {
+                       if (((ep_descriptor_ptrs[i]->bmAttributes &
+                             USB_ENDPOINT_XFERTYPE_MASK) ==
+                             USB_ENDPOINT_XFER_BULK)
+                           && is_usbd_high_speed()) {
+
+                               ep_descriptor_ptrs[i]->wMaxPacketSize =
+                                       CONFIG_USBD_SERIAL_BULK_HS_PKTSIZE;
+                       }
+
+                       endpoint_instance[i + 1].tx_packetSize =
+                               ep_descriptor_ptrs[i]->wMaxPacketSize;
+                       endpoint_instance[i + 1].rcv_packetSize =
+                               ep_descriptor_ptrs[i]->wMaxPacketSize;
+               }
+#endif
                usbtty_init_endpoints ();
 
        default:
@@ -953,14 +1003,14 @@ int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
 {
        switch (request->bRequest){
 
-               case ACM_SET_CONTROL_LINE_STATE:        /* Implies DTE ready */
+               case ACM_SET_CONTROL_LINE_STATE:        /* Implies DTE ready */
                        break;
-               case ACM_SEND_ENCAPSULATED_COMMAND :    /* Required */
+               case ACM_SEND_ENCAPSULATED_COMMAND :    /* Required */
                        break;
                case ACM_SET_LINE_ENCODING :            /* DTE stop/parity bits
                                                         * per character */
                        break;
-               case ACM_GET_ENCAPSULATED_RESPONSE :    /* request response */
+               case ACM_GET_ENCAPSULATED_RESPONSE :    /* request response */
                        break;
                case ACM_GET_LINE_ENCODING :            /* request DTE rate,
                                                         * stop/parity bits */
@@ -968,7 +1018,7 @@ int usbtty_cdc_setup(struct usb_device_request *request, struct urb *urb)
                        urb->actual_length = sizeof(rs232_desc);
 
                        break;
-               default:
+               default:
                        return 1;
        }
        return 0;
@@ -1007,6 +1057,3 @@ void usbtty_poll (void)
        udc_irq();
 
 }
-
-
-#endif