root / hw / usb-serial.c @ f36672ae
History | View | Annotate | Download (16.7 kB)
1 | a7954218 | balrog | /*
|
---|---|---|---|
2 | a7954218 | balrog | * FTDI FT232BM Device emulation
|
3 | a7954218 | balrog | *
|
4 | a7954218 | balrog | * Copyright (c) 2006 CodeSourcery.
|
5 | a7954218 | balrog | * Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
|
6 | a7954218 | balrog | * Written by Paul Brook, reused for FTDI by Samuel Thibault
|
7 | a7954218 | balrog | *
|
8 | a7954218 | balrog | * This code is licenced under the LGPL.
|
9 | a7954218 | balrog | */
|
10 | a7954218 | balrog | |
11 | a7954218 | balrog | #include "qemu-common.h" |
12 | a7954218 | balrog | #include "usb.h" |
13 | a7954218 | balrog | #include "qemu-char.h" |
14 | a7954218 | balrog | |
15 | a7954218 | balrog | //#define DEBUG_Serial
|
16 | a7954218 | balrog | |
17 | a7954218 | balrog | #ifdef DEBUG_Serial
|
18 | a7954218 | balrog | #define DPRINTF(fmt, args...) \
|
19 | a7954218 | balrog | do { printf("usb-serial: " fmt , ##args); } while (0) |
20 | a7954218 | balrog | #else
|
21 | a7954218 | balrog | #define DPRINTF(fmt, args...) do {} while(0) |
22 | a7954218 | balrog | #endif
|
23 | a7954218 | balrog | |
24 | a7954218 | balrog | #define RECV_BUF 384 |
25 | a7954218 | balrog | #define SEND_BUF 128 // Not used for now |
26 | a7954218 | balrog | |
27 | a7954218 | balrog | /* Commands */
|
28 | a7954218 | balrog | #define FTDI_RESET 0 |
29 | a7954218 | balrog | #define FTDI_SET_MDM_CTRL 1 |
30 | a7954218 | balrog | #define FTDI_SET_FLOW_CTRL 2 |
31 | a7954218 | balrog | #define FTDI_SET_BAUD 3 |
32 | a7954218 | balrog | #define FTDI_SET_DATA 4 |
33 | a7954218 | balrog | #define FTDI_GET_MDM_ST 5 |
34 | a7954218 | balrog | #define FTDI_SET_EVENT_CHR 6 |
35 | a7954218 | balrog | #define FTDI_SET_ERROR_CHR 7 |
36 | a7954218 | balrog | #define FTDI_SET_LATENCY 9 |
37 | a7954218 | balrog | #define FTDI_GET_LATENCY 10 |
38 | a7954218 | balrog | |
39 | a7954218 | balrog | #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) |
40 | a7954218 | balrog | #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) |
41 | a7954218 | balrog | |
42 | a7954218 | balrog | /* RESET */
|
43 | a7954218 | balrog | |
44 | a7954218 | balrog | #define FTDI_RESET_SIO 0 |
45 | a7954218 | balrog | #define FTDI_RESET_RX 1 |
46 | a7954218 | balrog | #define FTDI_RESET_TX 2 |
47 | a7954218 | balrog | |
48 | a7954218 | balrog | /* SET_MDM_CTRL */
|
49 | a7954218 | balrog | |
50 | a7954218 | balrog | #define FTDI_DTR 1 |
51 | abb8a139 | aurel32 | #define FTDI_SET_DTR (FTDI_DTR << 8) |
52 | a7954218 | balrog | #define FTDI_RTS 2 |
53 | abb8a139 | aurel32 | #define FTDI_SET_RTS (FTDI_RTS << 8) |
54 | a7954218 | balrog | |
55 | a7954218 | balrog | /* SET_FLOW_CTRL */
|
56 | a7954218 | balrog | |
57 | a7954218 | balrog | #define FTDI_RTS_CTS_HS 1 |
58 | a7954218 | balrog | #define FTDI_DTR_DSR_HS 2 |
59 | a7954218 | balrog | #define FTDI_XON_XOFF_HS 4 |
60 | a7954218 | balrog | |
61 | a7954218 | balrog | /* SET_DATA */
|
62 | a7954218 | balrog | |
63 | a7954218 | balrog | #define FTDI_PARITY (0x7 << 8) |
64 | a7954218 | balrog | #define FTDI_ODD (0x1 << 8) |
65 | a7954218 | balrog | #define FTDI_EVEN (0x2 << 8) |
66 | a7954218 | balrog | #define FTDI_MARK (0x3 << 8) |
67 | a7954218 | balrog | #define FTDI_SPACE (0x4 << 8) |
68 | a7954218 | balrog | |
69 | a7954218 | balrog | #define FTDI_STOP (0x3 << 11) |
70 | a7954218 | balrog | #define FTDI_STOP1 (0x0 << 11) |
71 | a7954218 | balrog | #define FTDI_STOP15 (0x1 << 11) |
72 | a7954218 | balrog | #define FTDI_STOP2 (0x2 << 11) |
73 | a7954218 | balrog | |
74 | a7954218 | balrog | /* GET_MDM_ST */
|
75 | a7954218 | balrog | /* TODO: should be sent every 40ms */
|
76 | a7954218 | balrog | #define FTDI_CTS (1<<4) // CTS line status |
77 | a7954218 | balrog | #define FTDI_DSR (1<<5) // DSR line status |
78 | a7954218 | balrog | #define FTDI_RI (1<<6) // RI line status |
79 | a7954218 | balrog | #define FTDI_RLSD (1<<7) // Receive Line Signal Detect |
80 | a7954218 | balrog | |
81 | a7954218 | balrog | /* Status */
|
82 | a7954218 | balrog | |
83 | a7954218 | balrog | #define FTDI_DR (1<<0) // Data Ready |
84 | a7954218 | balrog | #define FTDI_OE (1<<1) // Overrun Err |
85 | a7954218 | balrog | #define FTDI_PE (1<<2) // Parity Err |
86 | a7954218 | balrog | #define FTDI_FE (1<<3) // Framing Err |
87 | a7954218 | balrog | #define FTDI_BI (1<<4) // Break Interrupt |
88 | a7954218 | balrog | #define FTDI_THRE (1<<5) // Transmitter Holding Register |
89 | a7954218 | balrog | #define FTDI_TEMT (1<<6) // Transmitter Empty |
90 | a7954218 | balrog | #define FTDI_FIFO (1<<7) // Error in FIFO |
91 | a7954218 | balrog | |
92 | a7954218 | balrog | typedef struct { |
93 | a7954218 | balrog | USBDevice dev; |
94 | a7954218 | balrog | uint16_t vendorid; |
95 | a7954218 | balrog | uint16_t productid; |
96 | a7954218 | balrog | uint8_t recv_buf[RECV_BUF]; |
97 | a7954218 | balrog | uint8_t recv_ptr; |
98 | a7954218 | balrog | uint8_t recv_used; |
99 | a7954218 | balrog | uint8_t send_buf[SEND_BUF]; |
100 | a7954218 | balrog | uint8_t event_chr; |
101 | a7954218 | balrog | uint8_t error_chr; |
102 | a7954218 | balrog | uint8_t event_trigger; |
103 | a7954218 | balrog | QEMUSerialSetParams params; |
104 | a7954218 | balrog | int latency; /* ms */ |
105 | a7954218 | balrog | CharDriverState *cs; |
106 | a7954218 | balrog | } USBSerialState; |
107 | a7954218 | balrog | |
108 | a7954218 | balrog | static const uint8_t qemu_serial_dev_descriptor[] = { |
109 | a7954218 | balrog | 0x12, /* u8 bLength; */ |
110 | a7954218 | balrog | 0x01, /* u8 bDescriptorType; Device */ |
111 | a7954218 | balrog | 0x00, 0x02, /* u16 bcdUSB; v2.0 */ |
112 | a7954218 | balrog | |
113 | a7954218 | balrog | 0x00, /* u8 bDeviceClass; */ |
114 | a7954218 | balrog | 0x00, /* u8 bDeviceSubClass; */ |
115 | a7954218 | balrog | 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */ |
116 | a7954218 | balrog | 0x08, /* u8 bMaxPacketSize0; 8 Bytes */ |
117 | a7954218 | balrog | |
118 | a7954218 | balrog | /* Vendor and product id are arbitrary. */
|
119 | a7954218 | balrog | 0x03, 0x04, /* u16 idVendor; */ |
120 | a7954218 | balrog | 0x00, 0xFF, /* u16 idProduct; */ |
121 | a7954218 | balrog | 0x00, 0x04, /* u16 bcdDevice */ |
122 | a7954218 | balrog | |
123 | a7954218 | balrog | 0x01, /* u8 iManufacturer; */ |
124 | a7954218 | balrog | 0x02, /* u8 iProduct; */ |
125 | a7954218 | balrog | 0x03, /* u8 iSerialNumber; */ |
126 | a7954218 | balrog | 0x01 /* u8 bNumConfigurations; */ |
127 | a7954218 | balrog | }; |
128 | a7954218 | balrog | |
129 | a7954218 | balrog | static const uint8_t qemu_serial_config_descriptor[] = { |
130 | a7954218 | balrog | |
131 | a7954218 | balrog | /* one configuration */
|
132 | a7954218 | balrog | 0x09, /* u8 bLength; */ |
133 | a7954218 | balrog | 0x02, /* u8 bDescriptorType; Configuration */ |
134 | a7954218 | balrog | 0x20, 0x00, /* u16 wTotalLength; */ |
135 | a7954218 | balrog | 0x01, /* u8 bNumInterfaces; (1) */ |
136 | a7954218 | balrog | 0x01, /* u8 bConfigurationValue; */ |
137 | a7954218 | balrog | 0x00, /* u8 iConfiguration; */ |
138 | a7954218 | balrog | 0x80, /* u8 bmAttributes; |
139 | a7954218 | balrog | Bit 7: must be set,
|
140 | a7954218 | balrog | 6: Self-powered,
|
141 | a7954218 | balrog | 5: Remote wakeup,
|
142 | a7954218 | balrog | 4..0: resvd */
|
143 | a7954218 | balrog | 100/2, /* u8 MaxPower; */ |
144 | a7954218 | balrog | |
145 | a7954218 | balrog | /* one interface */
|
146 | a7954218 | balrog | 0x09, /* u8 if_bLength; */ |
147 | a7954218 | balrog | 0x04, /* u8 if_bDescriptorType; Interface */ |
148 | a7954218 | balrog | 0x00, /* u8 if_bInterfaceNumber; */ |
149 | a7954218 | balrog | 0x00, /* u8 if_bAlternateSetting; */ |
150 | a7954218 | balrog | 0x02, /* u8 if_bNumEndpoints; */ |
151 | a7954218 | balrog | 0xff, /* u8 if_bInterfaceClass; Vendor Specific */ |
152 | a7954218 | balrog | 0xff, /* u8 if_bInterfaceSubClass; Vendor Specific */ |
153 | a7954218 | balrog | 0xff, /* u8 if_bInterfaceProtocol; Vendor Specific */ |
154 | a7954218 | balrog | 0x02, /* u8 if_iInterface; */ |
155 | a7954218 | balrog | |
156 | a7954218 | balrog | /* Bulk-In endpoint */
|
157 | a7954218 | balrog | 0x07, /* u8 ep_bLength; */ |
158 | a7954218 | balrog | 0x05, /* u8 ep_bDescriptorType; Endpoint */ |
159 | a7954218 | balrog | 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */ |
160 | a7954218 | balrog | 0x02, /* u8 ep_bmAttributes; Bulk */ |
161 | a7954218 | balrog | 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ |
162 | a7954218 | balrog | 0x00, /* u8 ep_bInterval; */ |
163 | a7954218 | balrog | |
164 | a7954218 | balrog | /* Bulk-Out endpoint */
|
165 | a7954218 | balrog | 0x07, /* u8 ep_bLength; */ |
166 | a7954218 | balrog | 0x05, /* u8 ep_bDescriptorType; Endpoint */ |
167 | a7954218 | balrog | 0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */ |
168 | a7954218 | balrog | 0x02, /* u8 ep_bmAttributes; Bulk */ |
169 | a7954218 | balrog | 0x40, 0x00, /* u16 ep_wMaxPacketSize; */ |
170 | a7954218 | balrog | 0x00 /* u8 ep_bInterval; */ |
171 | a7954218 | balrog | }; |
172 | a7954218 | balrog | |
173 | a7954218 | balrog | static void usb_serial_reset(USBSerialState *s) |
174 | a7954218 | balrog | { |
175 | a7954218 | balrog | /* TODO: Set flow control to none */
|
176 | a7954218 | balrog | s->event_chr = 0x0d;
|
177 | a7954218 | balrog | s->event_trigger = 0;
|
178 | a7954218 | balrog | s->recv_ptr = 0;
|
179 | a7954218 | balrog | s->recv_used = 0;
|
180 | a7954218 | balrog | /* TODO: purge in char driver */
|
181 | a7954218 | balrog | } |
182 | a7954218 | balrog | |
183 | a7954218 | balrog | static void usb_serial_handle_reset(USBDevice *dev) |
184 | a7954218 | balrog | { |
185 | a7954218 | balrog | USBSerialState *s = (USBSerialState *)dev; |
186 | a7954218 | balrog | |
187 | a7954218 | balrog | DPRINTF("Reset\n");
|
188 | a7954218 | balrog | |
189 | a7954218 | balrog | usb_serial_reset(s); |
190 | a7954218 | balrog | /* TODO: Reset char device, send BREAK? */
|
191 | a7954218 | balrog | } |
192 | a7954218 | balrog | |
193 | abb8a139 | aurel32 | static uint8_t usb_get_modem_lines(USBSerialState *s)
|
194 | abb8a139 | aurel32 | { |
195 | abb8a139 | aurel32 | int flags;
|
196 | abb8a139 | aurel32 | uint8_t ret; |
197 | abb8a139 | aurel32 | |
198 | abb8a139 | aurel32 | if (qemu_chr_ioctl(s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP)
|
199 | abb8a139 | aurel32 | return FTDI_CTS|FTDI_DSR|FTDI_RLSD;
|
200 | abb8a139 | aurel32 | |
201 | abb8a139 | aurel32 | ret = 0;
|
202 | abb8a139 | aurel32 | if (flags & CHR_TIOCM_CTS)
|
203 | abb8a139 | aurel32 | ret |= FTDI_CTS; |
204 | abb8a139 | aurel32 | if (flags & CHR_TIOCM_DSR)
|
205 | abb8a139 | aurel32 | ret |= FTDI_DSR; |
206 | abb8a139 | aurel32 | if (flags & CHR_TIOCM_RI)
|
207 | abb8a139 | aurel32 | ret |= FTDI_RI; |
208 | abb8a139 | aurel32 | if (flags & CHR_TIOCM_CAR)
|
209 | abb8a139 | aurel32 | ret |= FTDI_RLSD; |
210 | abb8a139 | aurel32 | |
211 | abb8a139 | aurel32 | return ret;
|
212 | abb8a139 | aurel32 | } |
213 | abb8a139 | aurel32 | |
214 | a7954218 | balrog | static int usb_serial_handle_control(USBDevice *dev, int request, int value, |
215 | a7954218 | balrog | int index, int length, uint8_t *data) |
216 | a7954218 | balrog | { |
217 | a7954218 | balrog | USBSerialState *s = (USBSerialState *)dev; |
218 | a7954218 | balrog | int ret = 0; |
219 | a7954218 | balrog | |
220 | a7954218 | balrog | //DPRINTF("got control %x, value %x\n",request, value);
|
221 | a7954218 | balrog | switch (request) {
|
222 | a7954218 | balrog | case DeviceRequest | USB_REQ_GET_STATUS:
|
223 | a7954218 | balrog | data[0] = (0 << USB_DEVICE_SELF_POWERED) | |
224 | a7954218 | balrog | (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP); |
225 | a7954218 | balrog | data[1] = 0x00; |
226 | a7954218 | balrog | ret = 2;
|
227 | a7954218 | balrog | break;
|
228 | a7954218 | balrog | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
229 | a7954218 | balrog | if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
230 | a7954218 | balrog | dev->remote_wakeup = 0;
|
231 | a7954218 | balrog | } else {
|
232 | a7954218 | balrog | goto fail;
|
233 | a7954218 | balrog | } |
234 | a7954218 | balrog | ret = 0;
|
235 | a7954218 | balrog | break;
|
236 | a7954218 | balrog | case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
237 | a7954218 | balrog | if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
238 | a7954218 | balrog | dev->remote_wakeup = 1;
|
239 | a7954218 | balrog | } else {
|
240 | a7954218 | balrog | goto fail;
|
241 | a7954218 | balrog | } |
242 | a7954218 | balrog | ret = 0;
|
243 | a7954218 | balrog | break;
|
244 | a7954218 | balrog | case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
245 | a7954218 | balrog | dev->addr = value; |
246 | a7954218 | balrog | ret = 0;
|
247 | a7954218 | balrog | break;
|
248 | a7954218 | balrog | case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
249 | a7954218 | balrog | switch(value >> 8) { |
250 | a7954218 | balrog | case USB_DT_DEVICE:
|
251 | a7954218 | balrog | memcpy(data, qemu_serial_dev_descriptor, |
252 | a7954218 | balrog | sizeof(qemu_serial_dev_descriptor));
|
253 | a7954218 | balrog | data[8] = s->vendorid & 0xff; |
254 | a7954218 | balrog | data[9] = ((s->vendorid) >> 8) & 0xff; |
255 | a7954218 | balrog | data[10] = s->productid & 0xff; |
256 | a7954218 | balrog | data[11] = ((s->productid) >> 8) & 0xff; |
257 | a7954218 | balrog | ret = sizeof(qemu_serial_dev_descriptor);
|
258 | a7954218 | balrog | break;
|
259 | a7954218 | balrog | case USB_DT_CONFIG:
|
260 | a7954218 | balrog | memcpy(data, qemu_serial_config_descriptor, |
261 | a7954218 | balrog | sizeof(qemu_serial_config_descriptor));
|
262 | a7954218 | balrog | ret = sizeof(qemu_serial_config_descriptor);
|
263 | a7954218 | balrog | break;
|
264 | a7954218 | balrog | case USB_DT_STRING:
|
265 | a7954218 | balrog | switch(value & 0xff) { |
266 | a7954218 | balrog | case 0: |
267 | a7954218 | balrog | /* language ids */
|
268 | a7954218 | balrog | data[0] = 4; |
269 | a7954218 | balrog | data[1] = 3; |
270 | a7954218 | balrog | data[2] = 0x09; |
271 | a7954218 | balrog | data[3] = 0x04; |
272 | a7954218 | balrog | ret = 4;
|
273 | a7954218 | balrog | break;
|
274 | a7954218 | balrog | case 1: |
275 | a7954218 | balrog | /* vendor description */
|
276 | a7954218 | balrog | ret = set_usb_string(data, "QEMU " QEMU_VERSION);
|
277 | a7954218 | balrog | break;
|
278 | a7954218 | balrog | case 2: |
279 | a7954218 | balrog | /* product description */
|
280 | a7954218 | balrog | ret = set_usb_string(data, "QEMU USB SERIAL");
|
281 | a7954218 | balrog | break;
|
282 | a7954218 | balrog | case 3: |
283 | a7954218 | balrog | /* serial number */
|
284 | a7954218 | balrog | ret = set_usb_string(data, "1");
|
285 | a7954218 | balrog | break;
|
286 | a7954218 | balrog | default:
|
287 | a7954218 | balrog | goto fail;
|
288 | a7954218 | balrog | } |
289 | a7954218 | balrog | break;
|
290 | a7954218 | balrog | default:
|
291 | a7954218 | balrog | goto fail;
|
292 | a7954218 | balrog | } |
293 | a7954218 | balrog | break;
|
294 | a7954218 | balrog | case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
295 | a7954218 | balrog | data[0] = 1; |
296 | a7954218 | balrog | ret = 1;
|
297 | a7954218 | balrog | break;
|
298 | a7954218 | balrog | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
299 | a7954218 | balrog | ret = 0;
|
300 | a7954218 | balrog | break;
|
301 | a7954218 | balrog | case DeviceRequest | USB_REQ_GET_INTERFACE:
|
302 | a7954218 | balrog | data[0] = 0; |
303 | a7954218 | balrog | ret = 1;
|
304 | a7954218 | balrog | break;
|
305 | a7954218 | balrog | case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
306 | a7954218 | balrog | ret = 0;
|
307 | a7954218 | balrog | break;
|
308 | a7954218 | balrog | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
309 | a7954218 | balrog | ret = 0;
|
310 | a7954218 | balrog | break;
|
311 | a7954218 | balrog | |
312 | a7954218 | balrog | /* Class specific requests. */
|
313 | a7954218 | balrog | case DeviceOutVendor | FTDI_RESET:
|
314 | a7954218 | balrog | switch (value) {
|
315 | a7954218 | balrog | case FTDI_RESET_SIO:
|
316 | a7954218 | balrog | usb_serial_reset(s); |
317 | a7954218 | balrog | break;
|
318 | a7954218 | balrog | case FTDI_RESET_RX:
|
319 | a7954218 | balrog | s->recv_ptr = 0;
|
320 | a7954218 | balrog | s->recv_used = 0;
|
321 | a7954218 | balrog | /* TODO: purge from char device */
|
322 | a7954218 | balrog | break;
|
323 | a7954218 | balrog | case FTDI_RESET_TX:
|
324 | a7954218 | balrog | /* TODO: purge from char device */
|
325 | a7954218 | balrog | break;
|
326 | a7954218 | balrog | } |
327 | a7954218 | balrog | break;
|
328 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_MDM_CTRL:
|
329 | abb8a139 | aurel32 | { |
330 | abb8a139 | aurel32 | static int flags; |
331 | abb8a139 | aurel32 | qemu_chr_ioctl(s->cs,CHR_IOCTL_SERIAL_GET_TIOCM, &flags); |
332 | abb8a139 | aurel32 | if (value & FTDI_SET_RTS) {
|
333 | abb8a139 | aurel32 | if (value & FTDI_RTS)
|
334 | abb8a139 | aurel32 | flags |= CHR_TIOCM_RTS; |
335 | abb8a139 | aurel32 | else
|
336 | abb8a139 | aurel32 | flags &= ~CHR_TIOCM_RTS; |
337 | abb8a139 | aurel32 | } |
338 | abb8a139 | aurel32 | if (value & FTDI_SET_DTR) {
|
339 | abb8a139 | aurel32 | if (value & FTDI_DTR)
|
340 | abb8a139 | aurel32 | flags |= CHR_TIOCM_DTR; |
341 | abb8a139 | aurel32 | else
|
342 | abb8a139 | aurel32 | flags &= ~CHR_TIOCM_DTR; |
343 | abb8a139 | aurel32 | } |
344 | abb8a139 | aurel32 | qemu_chr_ioctl(s->cs,CHR_IOCTL_SERIAL_SET_TIOCM, &flags); |
345 | a7954218 | balrog | break;
|
346 | abb8a139 | aurel32 | } |
347 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_FLOW_CTRL:
|
348 | a7954218 | balrog | /* TODO: ioctl */
|
349 | a7954218 | balrog | break;
|
350 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_BAUD: {
|
351 | a7954218 | balrog | static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 }; |
352 | a7954218 | balrog | int subdivisor8 = subdivisors8[((value & 0xc000) >> 14) |
353 | a7954218 | balrog | | ((index & 1) << 2)]; |
354 | a7954218 | balrog | int divisor = value & 0x3fff; |
355 | a7954218 | balrog | |
356 | a7954218 | balrog | /* chip special cases */
|
357 | a7954218 | balrog | if (divisor == 1 && subdivisor8 == 0) |
358 | a7954218 | balrog | subdivisor8 = 4;
|
359 | a7954218 | balrog | if (divisor == 0 && subdivisor8 == 0) |
360 | a7954218 | balrog | divisor = 1;
|
361 | a7954218 | balrog | |
362 | a7954218 | balrog | s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8); |
363 | a7954218 | balrog | qemu_chr_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); |
364 | a7954218 | balrog | break;
|
365 | a7954218 | balrog | } |
366 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_DATA:
|
367 | a7954218 | balrog | switch (value & FTDI_PARITY) {
|
368 | a7954218 | balrog | case 0: |
369 | a7954218 | balrog | s->params.parity = 'N';
|
370 | a7954218 | balrog | break;
|
371 | a7954218 | balrog | case FTDI_ODD:
|
372 | a7954218 | balrog | s->params.parity = 'O';
|
373 | a7954218 | balrog | break;
|
374 | a7954218 | balrog | case FTDI_EVEN:
|
375 | a7954218 | balrog | s->params.parity = 'E';
|
376 | a7954218 | balrog | break;
|
377 | a7954218 | balrog | default:
|
378 | a7954218 | balrog | DPRINTF("unsupported parity %d\n", value & FTDI_PARITY);
|
379 | a7954218 | balrog | goto fail;
|
380 | a7954218 | balrog | } |
381 | a7954218 | balrog | switch (value & FTDI_STOP) {
|
382 | a7954218 | balrog | case FTDI_STOP1:
|
383 | a7954218 | balrog | s->params.stop_bits = 1;
|
384 | a7954218 | balrog | break;
|
385 | a7954218 | balrog | case FTDI_STOP2:
|
386 | a7954218 | balrog | s->params.stop_bits = 2;
|
387 | a7954218 | balrog | break;
|
388 | a7954218 | balrog | default:
|
389 | a7954218 | balrog | DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP);
|
390 | a7954218 | balrog | goto fail;
|
391 | a7954218 | balrog | } |
392 | a7954218 | balrog | qemu_chr_ioctl(s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); |
393 | a7954218 | balrog | /* TODO: TX ON/OFF */
|
394 | a7954218 | balrog | break;
|
395 | a7954218 | balrog | case DeviceInVendor | FTDI_GET_MDM_ST:
|
396 | abb8a139 | aurel32 | data[0] = usb_get_modem_lines(s) | 1; |
397 | abb8a139 | aurel32 | data[1] = 0; |
398 | abb8a139 | aurel32 | ret = 2;
|
399 | a7954218 | balrog | break;
|
400 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_EVENT_CHR:
|
401 | a7954218 | balrog | /* TODO: handle it */
|
402 | a7954218 | balrog | s->event_chr = value; |
403 | a7954218 | balrog | break;
|
404 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_ERROR_CHR:
|
405 | a7954218 | balrog | /* TODO: handle it */
|
406 | a7954218 | balrog | s->error_chr = value; |
407 | a7954218 | balrog | break;
|
408 | a7954218 | balrog | case DeviceOutVendor | FTDI_SET_LATENCY:
|
409 | a7954218 | balrog | s->latency = value; |
410 | a7954218 | balrog | break;
|
411 | a7954218 | balrog | case DeviceInVendor | FTDI_GET_LATENCY:
|
412 | a7954218 | balrog | data[0] = s->latency;
|
413 | a7954218 | balrog | ret = 1;
|
414 | a7954218 | balrog | break;
|
415 | a7954218 | balrog | default:
|
416 | a7954218 | balrog | fail:
|
417 | a7954218 | balrog | DPRINTF("got unsupported/bogus control %x, value %x\n", request, value);
|
418 | a7954218 | balrog | ret = USB_RET_STALL; |
419 | a7954218 | balrog | break;
|
420 | a7954218 | balrog | } |
421 | a7954218 | balrog | return ret;
|
422 | a7954218 | balrog | } |
423 | a7954218 | balrog | |
424 | a7954218 | balrog | static int usb_serial_handle_data(USBDevice *dev, USBPacket *p) |
425 | a7954218 | balrog | { |
426 | a7954218 | balrog | USBSerialState *s = (USBSerialState *)dev; |
427 | a7954218 | balrog | int ret = 0; |
428 | a7954218 | balrog | uint8_t devep = p->devep; |
429 | a7954218 | balrog | uint8_t *data = p->data; |
430 | a7954218 | balrog | int len = p->len;
|
431 | a7954218 | balrog | int first_len;
|
432 | a7954218 | balrog | |
433 | a7954218 | balrog | switch (p->pid) {
|
434 | a7954218 | balrog | case USB_TOKEN_OUT:
|
435 | a7954218 | balrog | if (devep != 2) |
436 | a7954218 | balrog | goto fail;
|
437 | a7954218 | balrog | qemu_chr_write(s->cs, data, len); |
438 | a7954218 | balrog | break;
|
439 | a7954218 | balrog | |
440 | a7954218 | balrog | case USB_TOKEN_IN:
|
441 | a7954218 | balrog | if (devep != 1) |
442 | a7954218 | balrog | goto fail;
|
443 | a7954218 | balrog | first_len = RECV_BUF - s->recv_ptr; |
444 | a7954218 | balrog | if (len <= 2) { |
445 | a7954218 | balrog | ret = USB_RET_NAK; |
446 | a7954218 | balrog | break;
|
447 | a7954218 | balrog | } |
448 | abb8a139 | aurel32 | *data++ = usb_get_modem_lines(s) | 1;
|
449 | abb8a139 | aurel32 | /* We do not have the uart details */
|
450 | a7954218 | balrog | *data++ = 0;
|
451 | a7954218 | balrog | len -= 2;
|
452 | a7954218 | balrog | if (len > s->recv_used)
|
453 | a7954218 | balrog | len = s->recv_used; |
454 | a7954218 | balrog | if (!len) {
|
455 | a7954218 | balrog | ret = USB_RET_NAK; |
456 | a7954218 | balrog | break;
|
457 | a7954218 | balrog | } |
458 | a7954218 | balrog | if (first_len > len)
|
459 | a7954218 | balrog | first_len = len; |
460 | a7954218 | balrog | memcpy(data, s->recv_buf + s->recv_ptr, first_len); |
461 | a7954218 | balrog | if (len > first_len)
|
462 | a7954218 | balrog | memcpy(data + first_len, s->recv_buf, len - first_len); |
463 | a7954218 | balrog | s->recv_used -= len; |
464 | a7954218 | balrog | s->recv_ptr = (s->recv_ptr + len) % RECV_BUF; |
465 | a7954218 | balrog | ret = len + 2;
|
466 | a7954218 | balrog | break;
|
467 | a7954218 | balrog | |
468 | a7954218 | balrog | default:
|
469 | a7954218 | balrog | DPRINTF("Bad token\n");
|
470 | a7954218 | balrog | fail:
|
471 | a7954218 | balrog | ret = USB_RET_STALL; |
472 | a7954218 | balrog | break;
|
473 | a7954218 | balrog | } |
474 | a7954218 | balrog | |
475 | a7954218 | balrog | return ret;
|
476 | a7954218 | balrog | } |
477 | a7954218 | balrog | |
478 | a7954218 | balrog | static void usb_serial_handle_destroy(USBDevice *dev) |
479 | a7954218 | balrog | { |
480 | a7954218 | balrog | USBSerialState *s = (USBSerialState *)dev; |
481 | a7954218 | balrog | |
482 | a7954218 | balrog | qemu_chr_close(s->cs); |
483 | a7954218 | balrog | qemu_free(s); |
484 | a7954218 | balrog | } |
485 | a7954218 | balrog | |
486 | 8fcd3692 | blueswir1 | static int usb_serial_can_read(void *opaque) |
487 | a7954218 | balrog | { |
488 | a7954218 | balrog | USBSerialState *s = opaque; |
489 | a7954218 | balrog | return RECV_BUF - s->recv_used;
|
490 | a7954218 | balrog | } |
491 | a7954218 | balrog | |
492 | 8fcd3692 | blueswir1 | static void usb_serial_read(void *opaque, const uint8_t *buf, int size) |
493 | a7954218 | balrog | { |
494 | a7954218 | balrog | USBSerialState *s = opaque; |
495 | a7954218 | balrog | int first_size = RECV_BUF - s->recv_ptr;
|
496 | a7954218 | balrog | if (first_size > size)
|
497 | a7954218 | balrog | first_size = size; |
498 | a7954218 | balrog | memcpy(s->recv_buf + s->recv_ptr + s->recv_used, buf, first_size); |
499 | a7954218 | balrog | if (size > first_size)
|
500 | a7954218 | balrog | memcpy(s->recv_buf, buf + first_size, size - first_size); |
501 | a7954218 | balrog | s->recv_used += size; |
502 | a7954218 | balrog | } |
503 | a7954218 | balrog | |
504 | 8fcd3692 | blueswir1 | static void usb_serial_event(void *opaque, int event) |
505 | a7954218 | balrog | { |
506 | a7954218 | balrog | USBSerialState *s = opaque; |
507 | a7954218 | balrog | |
508 | a7954218 | balrog | switch (event) {
|
509 | a7954218 | balrog | case CHR_EVENT_BREAK:
|
510 | a7954218 | balrog | /* TODO: Send Break to USB */
|
511 | a7954218 | balrog | break;
|
512 | a7954218 | balrog | case CHR_EVENT_FOCUS:
|
513 | a7954218 | balrog | break;
|
514 | a7954218 | balrog | case CHR_EVENT_RESET:
|
515 | a7954218 | balrog | usb_serial_reset(s); |
516 | a7954218 | balrog | /* TODO: Reset USB port */
|
517 | a7954218 | balrog | break;
|
518 | a7954218 | balrog | } |
519 | a7954218 | balrog | } |
520 | a7954218 | balrog | |
521 | a7954218 | balrog | USBDevice *usb_serial_init(const char *filename) |
522 | a7954218 | balrog | { |
523 | a7954218 | balrog | USBSerialState *s; |
524 | a7954218 | balrog | CharDriverState *cdrv; |
525 | a11d070e | balrog | unsigned short vendorid = 0x0403, productid = 0x6001; |
526 | a7954218 | balrog | |
527 | a7954218 | balrog | while (*filename && *filename != ':') { |
528 | a7954218 | balrog | const char *p; |
529 | a7954218 | balrog | char *e;
|
530 | a7954218 | balrog | if (strstart(filename, "vendorid=", &p)) { |
531 | a7954218 | balrog | vendorid = strtol(p, &e, 16);
|
532 | a7954218 | balrog | if (e == p || (*e && *e != ',' && *e != ':')) { |
533 | a7954218 | balrog | printf("bogus vendor ID %s\n", p);
|
534 | a7954218 | balrog | return NULL; |
535 | a7954218 | balrog | } |
536 | a7954218 | balrog | filename = e; |
537 | a7954218 | balrog | } else if (strstart(filename, "productid=", &p)) { |
538 | a7954218 | balrog | productid = strtol(p, &e, 16);
|
539 | a7954218 | balrog | if (e == p || (*e && *e != ',' && *e != ':')) { |
540 | a7954218 | balrog | printf("bogus product ID %s\n", p);
|
541 | a7954218 | balrog | return NULL; |
542 | a7954218 | balrog | } |
543 | a7954218 | balrog | filename = e; |
544 | a7954218 | balrog | } else {
|
545 | a7954218 | balrog | printf("unrecognized serial USB option %s\n", filename);
|
546 | a7954218 | balrog | return NULL; |
547 | a7954218 | balrog | } |
548 | a7954218 | balrog | while(*filename == ',') |
549 | a7954218 | balrog | filename++; |
550 | a7954218 | balrog | } |
551 | a7954218 | balrog | if (!*filename) {
|
552 | a7954218 | balrog | printf("character device specification needed\n");
|
553 | a7954218 | balrog | return NULL; |
554 | a7954218 | balrog | } |
555 | a7954218 | balrog | filename++; |
556 | a7954218 | balrog | s = qemu_mallocz(sizeof(USBSerialState));
|
557 | a7954218 | balrog | if (!s)
|
558 | a7954218 | balrog | return NULL; |
559 | a7954218 | balrog | |
560 | a7954218 | balrog | cdrv = qemu_chr_open(filename); |
561 | a7954218 | balrog | if (!cdrv)
|
562 | a7954218 | balrog | goto fail;
|
563 | a7954218 | balrog | s->cs = cdrv; |
564 | a7954218 | balrog | qemu_chr_add_handlers(cdrv, usb_serial_can_read, usb_serial_read, usb_serial_event, s); |
565 | a7954218 | balrog | |
566 | a7954218 | balrog | s->dev.speed = USB_SPEED_FULL; |
567 | a7954218 | balrog | s->dev.handle_packet = usb_generic_handle_packet; |
568 | a7954218 | balrog | |
569 | a7954218 | balrog | s->dev.handle_reset = usb_serial_handle_reset; |
570 | a7954218 | balrog | s->dev.handle_control = usb_serial_handle_control; |
571 | a7954218 | balrog | s->dev.handle_data = usb_serial_handle_data; |
572 | a7954218 | balrog | s->dev.handle_destroy = usb_serial_handle_destroy; |
573 | a7954218 | balrog | |
574 | a7954218 | balrog | s->vendorid = vendorid; |
575 | a7954218 | balrog | s->productid = productid; |
576 | a7954218 | balrog | |
577 | a7954218 | balrog | snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Serial(%.16s)", |
578 | a7954218 | balrog | filename); |
579 | a7954218 | balrog | |
580 | a7954218 | balrog | usb_serial_handle_reset((USBDevice *)s); |
581 | a7954218 | balrog | return (USBDevice *)s;
|
582 | a7954218 | balrog | fail:
|
583 | a7954218 | balrog | qemu_free(s); |
584 | a7954218 | balrog | return NULL; |
585 | a7954218 | balrog | } |