Statistics
| Branch: | Revision:

root / hw / virtio-serial.h @ 160600fd

History | View | Annotate | Download (4.6 kB)

1
/*
2
 * Virtio Serial / Console Support
3
 *
4
 * Copyright IBM, Corp. 2008
5
 * Copyright Red Hat, Inc. 2009
6
 *
7
 * Authors:
8
 *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9
 *  Amit Shah <amit.shah@redhat.com>
10
 *
11
 * This work is licensed under the terms of the GNU GPL, version 2.  See
12
 * the COPYING file in the top-level directory.
13
 *
14
 */
15
#ifndef _QEMU_VIRTIO_SERIAL_H
16
#define _QEMU_VIRTIO_SERIAL_H
17

    
18
#include <stdbool.h>
19
#include "qdev.h"
20
#include "virtio.h"
21

    
22
/* == Interface shared between the guest kernel and qemu == */
23

    
24
/* The Virtio ID for virtio console / serial ports */
25
#define VIRTIO_ID_CONSOLE                3
26

    
27
/* Features supported */
28
#define VIRTIO_CONSOLE_F_MULTIPORT        1
29

    
30
struct virtio_console_config {
31
    /*
32
     * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
33
     * isn't implemented here yet
34
     */
35
    uint16_t cols;
36
    uint16_t rows;
37

    
38
    uint32_t max_nr_ports;
39
    uint32_t nr_ports;
40
} __attribute__((packed));
41

    
42
struct virtio_console_control {
43
    uint32_t id;                /* Port number */
44
    uint16_t event;                /* The kind of control event (see below) */
45
    uint16_t value;                /* Extra information for the key */
46
};
47

    
48
/* Some events for the internal messages (control packets) */
49
#define VIRTIO_CONSOLE_PORT_READY        0
50
#define VIRTIO_CONSOLE_CONSOLE_PORT        1
51
#define VIRTIO_CONSOLE_RESIZE                2
52
#define VIRTIO_CONSOLE_PORT_OPEN        3
53
#define VIRTIO_CONSOLE_PORT_NAME        4
54

    
55
/* == In-qemu interface == */
56

    
57
typedef struct VirtIOSerial VirtIOSerial;
58
typedef struct VirtIOSerialBus VirtIOSerialBus;
59
typedef struct VirtIOSerialPort VirtIOSerialPort;
60
typedef struct VirtIOSerialPortInfo VirtIOSerialPortInfo;
61

    
62
typedef struct VirtIOSerialDevice {
63
    DeviceState qdev;
64
    VirtIOSerialPortInfo *info;
65
} VirtIOSerialDevice;
66

    
67
/*
68
 * This is the state that's shared between all the ports.  Some of the
69
 * state is configurable via command-line options. Some of it can be
70
 * set by individual devices in their initfn routines. Some of the
71
 * state is set by the generic qdev device init routine.
72
 */
73
struct VirtIOSerialPort {
74
    DeviceState dev;
75
    VirtIOSerialPortInfo *info;
76

    
77
    QTAILQ_ENTRY(VirtIOSerialPort) next;
78

    
79
    /*
80
     * This field gives us the virtio device as well as the qdev bus
81
     * that we are associated with
82
     */
83
    VirtIOSerial *vser;
84

    
85
    VirtQueue *ivq, *ovq;
86

    
87
    /*
88
     * This name is sent to the guest and exported via sysfs.
89
     * The guest could create symlinks based on this information.
90
     * The name is in the reverse fqdn format, like org.qemu.console.0
91
     */
92
    char *name;
93

    
94
    /*
95
     * This id helps identify ports between the guest and the host.
96
     * The guest sends a "header" with this id with each data packet
97
     * that it sends and the host can then find out which associated
98
     * device to send out this data to
99
     */
100
    uint32_t id;
101

    
102
    /* Identify if this is a port that binds with hvc in the guest */
103
    uint8_t is_console;
104

    
105
    /* Is the corresponding guest device open? */
106
    bool guest_connected;
107
    /* Is this device open for IO on the host? */
108
    bool host_connected;
109
};
110

    
111
struct VirtIOSerialPortInfo {
112
    DeviceInfo qdev;
113
    /*
114
     * The per-port (or per-app) init function that's called when a
115
     * new device is found on the bus.
116
     */
117
    int (*init)(VirtIOSerialDevice *dev);
118
    /*
119
     * Per-port exit function that's called when a port gets
120
     * hot-unplugged or removed.
121
     */
122
    int (*exit)(VirtIOSerialDevice *dev);
123

    
124
    /* Callbacks for guest events */
125
        /* Guest opened device. */
126
    void (*guest_open)(VirtIOSerialPort *port);
127
        /* Guest closed device. */
128
    void (*guest_close)(VirtIOSerialPort *port);
129

    
130
        /* Guest is now ready to accept data (virtqueues set up). */
131
    void (*guest_ready)(VirtIOSerialPort *port);
132

    
133
    /*
134
     * Guest wrote some data to the port. This data is handed over to
135
     * the app via this callback. The app should return the number of
136
     * bytes it successfully consumed.
137
     */
138
    size_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, size_t len);
139
};
140

    
141
/* Interface to the virtio-serial bus */
142

    
143
/*
144
 * Individual ports/apps should call this function to register the port
145
 * with the virtio-serial bus
146
 */
147
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info);
148

    
149
/*
150
 * Open a connection to the port
151
 *   Returns 0 on success (always).
152
 */
153
int virtio_serial_open(VirtIOSerialPort *port);
154

    
155
/*
156
 * Close the connection to the port
157
 *   Returns 0 on success (always).
158
 */
159
int virtio_serial_close(VirtIOSerialPort *port);
160

    
161
/*
162
 * Send data to Guest
163
 */
164
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
165
                            size_t size);
166

    
167
/*
168
 * Query whether a guest is ready to receive data.
169
 */
170
size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
171

    
172
#endif