remove noise for demo
[qemu] / hw / usb-serial.c
index b666c99..5208482 100644 (file)
@@ -22,7 +22,6 @@ do { printf("usb-serial: " fmt , ##args); } while (0)
 #endif
 
 #define RECV_BUF 384
-#define SEND_BUF 128        // Not used for now
 
 /* Commands */
 #define FTDI_RESET             0
@@ -47,9 +46,10 @@ do { printf("usb-serial: " fmt , ##args); } while (0)
 
 /* SET_MDM_CTRL */
 
-#define FTDI_MDM_CTRL  3
 #define FTDI_DTR       1
+#define FTDI_SET_DTR   (FTDI_DTR << 8)
 #define FTDI_RTS       2
+#define FTDI_SET_RTS   (FTDI_RTS << 8)
 
 /* SET_FLOW_CTRL */
 
@@ -93,13 +93,11 @@ typedef struct {
     uint16_t vendorid;
     uint16_t productid;
     uint8_t recv_buf[RECV_BUF];
-    uint8_t recv_ptr;
-    uint8_t recv_used;
-    uint8_t send_buf[SEND_BUF];
+    uint16_t recv_ptr;
+    uint16_t recv_used;
     uint8_t event_chr;
     uint8_t error_chr;
     uint8_t event_trigger;
-    uint8_t lines;
     QEMUSerialSetParams params;
     int latency;        /* ms */
     CharDriverState *cs;
@@ -178,7 +176,6 @@ static void usb_serial_reset(USBSerialState *s)
     s->recv_ptr = 0;
     s->recv_used = 0;
     /* TODO: purge in char driver */
-    s->lines &= ~(FTDI_DTR|FTDI_RTS);
 }
 
 static void usb_serial_handle_reset(USBDevice *dev)
@@ -191,6 +188,27 @@ static void usb_serial_handle_reset(USBDevice *dev)
     /* TODO: Reset char device, send BREAK? */
 }
 
+static uint8_t usb_get_modem_lines(USBSerialState *s)
+{
+    int flags;
+    uint8_t ret;
+
+    if (qemu_chr_ioctl(s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP)
+        return FTDI_CTS|FTDI_DSR|FTDI_RLSD;
+
+    ret = 0;
+    if (flags & CHR_TIOCM_CTS)
+        ret |= FTDI_CTS;
+    if (flags & CHR_TIOCM_DSR)
+        ret |= FTDI_DSR;
+    if (flags & CHR_TIOCM_RI)
+        ret |= FTDI_RI;
+    if (flags & CHR_TIOCM_CAR)
+        ret |= FTDI_RLSD;
+
+    return ret;
+}
+
 static int usb_serial_handle_control(USBDevice *dev, int request, int value,
                                   int index, int length, uint8_t *data)
 {
@@ -306,8 +324,24 @@ static int usb_serial_handle_control(USBDevice *dev, int request, int value,
         }
         break;
     case DeviceOutVendor | FTDI_SET_MDM_CTRL:
-        s->lines = value & FTDI_MDM_CTRL;
+    {
+        static int flags;
+        qemu_chr_ioctl(s->cs,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
+        if (value & FTDI_SET_RTS) {
+            if (value & FTDI_RTS)
+                flags |= CHR_TIOCM_RTS;
+            else
+                flags &= ~CHR_TIOCM_RTS;
+        }
+        if (value & FTDI_SET_DTR) {
+            if (value & FTDI_DTR)
+                flags |= CHR_TIOCM_DTR;
+            else
+                flags &= ~CHR_TIOCM_DTR;
+        }
+        qemu_chr_ioctl(s->cs,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
         break;
+    }
     case DeviceOutVendor | FTDI_SET_FLOW_CTRL:
         /* TODO: ioctl */
         break;
@@ -357,9 +391,9 @@ static int usb_serial_handle_control(USBDevice *dev, int request, int value,
         /* TODO: TX ON/OFF */
         break;
     case DeviceInVendor | FTDI_GET_MDM_ST:
-        /* TODO: return modem status */
-        data[0] = 0;
-        ret = 1;
+        data[0] = usb_get_modem_lines(s) | 1;
+        data[1] = 0;
+        ret = 2;
         break;
     case DeviceOutVendor | FTDI_SET_EVENT_CHR:
         /* TODO: handle it */
@@ -409,8 +443,8 @@ static int usb_serial_handle_data(USBDevice *dev, USBPacket *p)
             ret = USB_RET_NAK;
             break;
         }
-        /* TODO: Report serial line status */
-        *data++ = 0;
+        *data++ = usb_get_modem_lines(s) | 1;
+        /* We do not have the uart details */
         *data++ = 0;
         len -= 2;
         if (len > s->recv_used)
@@ -447,13 +481,13 @@ static void usb_serial_handle_destroy(USBDevice *dev)
     qemu_free(s);
 }
 
-int usb_serial_can_read(void *opaque)
+static int usb_serial_can_read(void *opaque)
 {
     USBSerialState *s = opaque;
     return RECV_BUF - s->recv_used;
 }
 
-void usb_serial_read(void *opaque, const uint8_t *buf, int size)
+static void usb_serial_read(void *opaque, const uint8_t *buf, int size)
 {
     USBSerialState *s = opaque;
     int first_size = RECV_BUF - s->recv_ptr;
@@ -465,7 +499,7 @@ void usb_serial_read(void *opaque, const uint8_t *buf, int size)
     s->recv_used += size;
 }
 
-void usb_serial_event(void *opaque, int event)
+static void usb_serial_event(void *opaque, int event)
 {
     USBSerialState *s = opaque;
 
@@ -487,6 +521,8 @@ USBDevice *usb_serial_init(const char *filename)
     USBSerialState *s;
     CharDriverState *cdrv;
     unsigned short vendorid = 0x0403, productid = 0x6001;
+    char label[32];
+    static int index;
 
     while (*filename && *filename != ':') {
         const char *p;
@@ -518,10 +554,9 @@ USBDevice *usb_serial_init(const char *filename)
     }
     filename++;
     s = qemu_mallocz(sizeof(USBSerialState));
-    if (!s)
-        return NULL;
 
-    cdrv = qemu_chr_open(filename);
+    snprintf(label, sizeof(label), "usbserial%d", index++);
+    cdrv = qemu_chr_open(label, filename, NULL);
     if (!cdrv)
         goto fail;
     s->cs = cdrv;