]>
Commit | Line | Data |
---|---|---|
a7954218 AZ |
1 | /* |
2 | * FTDI FT232BM Device emulation | |
3 | * | |
4 | * Copyright (c) 2006 CodeSourcery. | |
5 | * Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org> | |
6 | * Written by Paul Brook, reused for FTDI by Samuel Thibault | |
7 | * | |
8e31bf38 | 8 | * This code is licensed under the LGPL. |
a7954218 AZ |
9 | */ |
10 | ||
e532b2e0 | 11 | #include "qemu/osdep.h" |
da34e65c | 12 | #include "qapi/error.h" |
a7954218 | 13 | #include "qemu-common.h" |
f348b6d1 | 14 | #include "qemu/cutils.h" |
d49b6836 | 15 | #include "qemu/error-report.h" |
f1ae32a1 GH |
16 | #include "hw/usb.h" |
17 | #include "hw/usb/desc.h" | |
dccfcd0e | 18 | #include "sysemu/char.h" |
a7954218 AZ |
19 | |
20 | //#define DEBUG_Serial | |
21 | ||
22 | #ifdef DEBUG_Serial | |
001faf32 BS |
23 | #define DPRINTF(fmt, ...) \ |
24 | do { printf("usb-serial: " fmt , ## __VA_ARGS__); } while (0) | |
a7954218 | 25 | #else |
001faf32 | 26 | #define DPRINTF(fmt, ...) do {} while(0) |
a7954218 AZ |
27 | #endif |
28 | ||
29 | #define RECV_BUF 384 | |
a7954218 AZ |
30 | |
31 | /* Commands */ | |
32 | #define FTDI_RESET 0 | |
33 | #define FTDI_SET_MDM_CTRL 1 | |
34 | #define FTDI_SET_FLOW_CTRL 2 | |
35 | #define FTDI_SET_BAUD 3 | |
36 | #define FTDI_SET_DATA 4 | |
37 | #define FTDI_GET_MDM_ST 5 | |
38 | #define FTDI_SET_EVENT_CHR 6 | |
39 | #define FTDI_SET_ERROR_CHR 7 | |
40 | #define FTDI_SET_LATENCY 9 | |
41 | #define FTDI_GET_LATENCY 10 | |
42 | ||
43 | #define DeviceOutVendor ((USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) | |
44 | #define DeviceInVendor ((USB_DIR_IN |USB_TYPE_VENDOR|USB_RECIP_DEVICE)<<8) | |
45 | ||
46 | /* RESET */ | |
47 | ||
48 | #define FTDI_RESET_SIO 0 | |
49 | #define FTDI_RESET_RX 1 | |
50 | #define FTDI_RESET_TX 2 | |
51 | ||
52 | /* SET_MDM_CTRL */ | |
53 | ||
a7954218 | 54 | #define FTDI_DTR 1 |
abb8a139 | 55 | #define FTDI_SET_DTR (FTDI_DTR << 8) |
a7954218 | 56 | #define FTDI_RTS 2 |
abb8a139 | 57 | #define FTDI_SET_RTS (FTDI_RTS << 8) |
a7954218 AZ |
58 | |
59 | /* SET_FLOW_CTRL */ | |
60 | ||
61 | #define FTDI_RTS_CTS_HS 1 | |
62 | #define FTDI_DTR_DSR_HS 2 | |
63 | #define FTDI_XON_XOFF_HS 4 | |
64 | ||
65 | /* SET_DATA */ | |
66 | ||
67 | #define FTDI_PARITY (0x7 << 8) | |
68 | #define FTDI_ODD (0x1 << 8) | |
69 | #define FTDI_EVEN (0x2 << 8) | |
70 | #define FTDI_MARK (0x3 << 8) | |
71 | #define FTDI_SPACE (0x4 << 8) | |
72 | ||
73 | #define FTDI_STOP (0x3 << 11) | |
74 | #define FTDI_STOP1 (0x0 << 11) | |
75 | #define FTDI_STOP15 (0x1 << 11) | |
76 | #define FTDI_STOP2 (0x2 << 11) | |
77 | ||
78 | /* GET_MDM_ST */ | |
79 | /* TODO: should be sent every 40ms */ | |
80 | #define FTDI_CTS (1<<4) // CTS line status | |
81 | #define FTDI_DSR (1<<5) // DSR line status | |
82 | #define FTDI_RI (1<<6) // RI line status | |
83 | #define FTDI_RLSD (1<<7) // Receive Line Signal Detect | |
84 | ||
85 | /* Status */ | |
86 | ||
87 | #define FTDI_DR (1<<0) // Data Ready | |
88 | #define FTDI_OE (1<<1) // Overrun Err | |
89 | #define FTDI_PE (1<<2) // Parity Err | |
90 | #define FTDI_FE (1<<3) // Framing Err | |
91 | #define FTDI_BI (1<<4) // Break Interrupt | |
92 | #define FTDI_THRE (1<<5) // Transmitter Holding Register | |
93 | #define FTDI_TEMT (1<<6) // Transmitter Empty | |
94 | #define FTDI_FIFO (1<<7) // Error in FIFO | |
95 | ||
96 | typedef struct { | |
97 | USBDevice dev; | |
a7954218 | 98 | uint8_t recv_buf[RECV_BUF]; |
8109b9b6 AJ |
99 | uint16_t recv_ptr; |
100 | uint16_t recv_used; | |
a7954218 AZ |
101 | uint8_t event_chr; |
102 | uint8_t error_chr; | |
103 | uint8_t event_trigger; | |
a7954218 AZ |
104 | QEMUSerialSetParams params; |
105 | int latency; /* ms */ | |
becdfa00 | 106 | CharBackend cs; |
a7954218 AZ |
107 | } USBSerialState; |
108 | ||
cdf0d769 GA |
109 | #define TYPE_USB_SERIAL "usb-serial-dev" |
110 | #define USB_SERIAL_DEV(obj) OBJECT_CHECK(USBSerialState, (obj), TYPE_USB_SERIAL) | |
111 | ||
f29783f7 GH |
112 | enum { |
113 | STR_MANUFACTURER = 1, | |
114 | STR_PRODUCT_SERIAL, | |
115 | STR_PRODUCT_BRAILLE, | |
116 | STR_SERIALNUMBER, | |
a7954218 AZ |
117 | }; |
118 | ||
f29783f7 | 119 | static const USBDescStrings desc_strings = { |
93bfef4c | 120 | [STR_MANUFACTURER] = "QEMU", |
f29783f7 | 121 | [STR_PRODUCT_SERIAL] = "QEMU USB SERIAL", |
2964cd9b | 122 | [STR_PRODUCT_BRAILLE] = "QEMU USB BAUM BRAILLE", |
f29783f7 GH |
123 | [STR_SERIALNUMBER] = "1", |
124 | }; | |
125 | ||
126 | static const USBDescIface desc_iface0 = { | |
127 | .bInterfaceNumber = 0, | |
128 | .bNumEndpoints = 2, | |
129 | .bInterfaceClass = 0xff, | |
130 | .bInterfaceSubClass = 0xff, | |
131 | .bInterfaceProtocol = 0xff, | |
132 | .eps = (USBDescEndpoint[]) { | |
133 | { | |
134 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
135 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
136 | .wMaxPacketSize = 64, | |
137 | },{ | |
138 | .bEndpointAddress = USB_DIR_OUT | 0x02, | |
139 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
140 | .wMaxPacketSize = 64, | |
141 | }, | |
142 | } | |
143 | }; | |
144 | ||
145 | static const USBDescDevice desc_device = { | |
146 | .bcdUSB = 0x0200, | |
147 | .bMaxPacketSize0 = 8, | |
148 | .bNumConfigurations = 1, | |
149 | .confs = (USBDescConfig[]) { | |
150 | { | |
151 | .bNumInterfaces = 1, | |
152 | .bConfigurationValue = 1, | |
bd93976a | 153 | .bmAttributes = USB_CFG_ATT_ONE, |
f29783f7 | 154 | .bMaxPower = 50, |
add75088 | 155 | .nif = 1, |
f29783f7 GH |
156 | .ifs = &desc_iface0, |
157 | }, | |
158 | }, | |
159 | }; | |
160 | ||
161 | static const USBDesc desc_serial = { | |
162 | .id = { | |
163 | .idVendor = 0x0403, | |
164 | .idProduct = 0x6001, | |
165 | .bcdDevice = 0x0400, | |
166 | .iManufacturer = STR_MANUFACTURER, | |
167 | .iProduct = STR_PRODUCT_SERIAL, | |
168 | .iSerialNumber = STR_SERIALNUMBER, | |
169 | }, | |
170 | .full = &desc_device, | |
171 | .str = desc_strings, | |
172 | }; | |
173 | ||
174 | static const USBDesc desc_braille = { | |
175 | .id = { | |
176 | .idVendor = 0x0403, | |
177 | .idProduct = 0xfe72, | |
178 | .bcdDevice = 0x0400, | |
179 | .iManufacturer = STR_MANUFACTURER, | |
180 | .iProduct = STR_PRODUCT_BRAILLE, | |
181 | .iSerialNumber = STR_SERIALNUMBER, | |
182 | }, | |
183 | .full = &desc_device, | |
184 | .str = desc_strings, | |
a7954218 AZ |
185 | }; |
186 | ||
187 | static void usb_serial_reset(USBSerialState *s) | |
188 | { | |
189 | /* TODO: Set flow control to none */ | |
190 | s->event_chr = 0x0d; | |
191 | s->event_trigger = 0; | |
192 | s->recv_ptr = 0; | |
193 | s->recv_used = 0; | |
194 | /* TODO: purge in char driver */ | |
a7954218 AZ |
195 | } |
196 | ||
197 | static void usb_serial_handle_reset(USBDevice *dev) | |
198 | { | |
199 | USBSerialState *s = (USBSerialState *)dev; | |
200 | ||
201 | DPRINTF("Reset\n"); | |
202 | ||
203 | usb_serial_reset(s); | |
204 | /* TODO: Reset char device, send BREAK? */ | |
205 | } | |
206 | ||
abb8a139 AJ |
207 | static uint8_t usb_get_modem_lines(USBSerialState *s) |
208 | { | |
209 | int flags; | |
210 | uint8_t ret; | |
211 | ||
5345fdb4 | 212 | if (qemu_chr_fe_ioctl(&s->cs, |
becdfa00 | 213 | CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP) { |
abb8a139 | 214 | return FTDI_CTS|FTDI_DSR|FTDI_RLSD; |
becdfa00 | 215 | } |
abb8a139 AJ |
216 | |
217 | ret = 0; | |
218 | if (flags & CHR_TIOCM_CTS) | |
219 | ret |= FTDI_CTS; | |
220 | if (flags & CHR_TIOCM_DSR) | |
221 | ret |= FTDI_DSR; | |
222 | if (flags & CHR_TIOCM_RI) | |
223 | ret |= FTDI_RI; | |
224 | if (flags & CHR_TIOCM_CAR) | |
225 | ret |= FTDI_RLSD; | |
226 | ||
227 | return ret; | |
228 | } | |
229 | ||
9a77a0f5 | 230 | static void usb_serial_handle_control(USBDevice *dev, USBPacket *p, |
007fd62f | 231 | int request, int value, int index, int length, uint8_t *data) |
a7954218 AZ |
232 | { |
233 | USBSerialState *s = (USBSerialState *)dev; | |
f29783f7 GH |
234 | int ret; |
235 | ||
236 | DPRINTF("got control %x, value %x\n",request, value); | |
007fd62f | 237 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
f29783f7 | 238 | if (ret >= 0) { |
9a77a0f5 | 239 | return; |
f29783f7 | 240 | } |
a7954218 | 241 | |
a7954218 | 242 | switch (request) { |
a7954218 | 243 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
a7954218 AZ |
244 | break; |
245 | ||
246 | /* Class specific requests. */ | |
247 | case DeviceOutVendor | FTDI_RESET: | |
248 | switch (value) { | |
249 | case FTDI_RESET_SIO: | |
250 | usb_serial_reset(s); | |
251 | break; | |
252 | case FTDI_RESET_RX: | |
253 | s->recv_ptr = 0; | |
254 | s->recv_used = 0; | |
255 | /* TODO: purge from char device */ | |
256 | break; | |
257 | case FTDI_RESET_TX: | |
258 | /* TODO: purge from char device */ | |
259 | break; | |
260 | } | |
261 | break; | |
262 | case DeviceOutVendor | FTDI_SET_MDM_CTRL: | |
abb8a139 AJ |
263 | { |
264 | static int flags; | |
5345fdb4 | 265 | qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_GET_TIOCM, &flags); |
abb8a139 AJ |
266 | if (value & FTDI_SET_RTS) { |
267 | if (value & FTDI_RTS) | |
268 | flags |= CHR_TIOCM_RTS; | |
269 | else | |
270 | flags &= ~CHR_TIOCM_RTS; | |
271 | } | |
272 | if (value & FTDI_SET_DTR) { | |
273 | if (value & FTDI_DTR) | |
274 | flags |= CHR_TIOCM_DTR; | |
275 | else | |
276 | flags &= ~CHR_TIOCM_DTR; | |
277 | } | |
5345fdb4 | 278 | qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_TIOCM, &flags); |
a7954218 | 279 | break; |
abb8a139 | 280 | } |
a7954218 AZ |
281 | case DeviceOutVendor | FTDI_SET_FLOW_CTRL: |
282 | /* TODO: ioctl */ | |
283 | break; | |
284 | case DeviceOutVendor | FTDI_SET_BAUD: { | |
285 | static const int subdivisors8[8] = { 0, 4, 2, 1, 3, 5, 6, 7 }; | |
286 | int subdivisor8 = subdivisors8[((value & 0xc000) >> 14) | |
287 | | ((index & 1) << 2)]; | |
288 | int divisor = value & 0x3fff; | |
289 | ||
290 | /* chip special cases */ | |
291 | if (divisor == 1 && subdivisor8 == 0) | |
292 | subdivisor8 = 4; | |
293 | if (divisor == 0 && subdivisor8 == 0) | |
294 | divisor = 1; | |
295 | ||
296 | s->params.speed = (48000000 / 2) / (8 * divisor + subdivisor8); | |
5345fdb4 | 297 | qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); |
a7954218 AZ |
298 | break; |
299 | } | |
300 | case DeviceOutVendor | FTDI_SET_DATA: | |
301 | switch (value & FTDI_PARITY) { | |
302 | case 0: | |
303 | s->params.parity = 'N'; | |
304 | break; | |
305 | case FTDI_ODD: | |
306 | s->params.parity = 'O'; | |
307 | break; | |
308 | case FTDI_EVEN: | |
309 | s->params.parity = 'E'; | |
310 | break; | |
311 | default: | |
312 | DPRINTF("unsupported parity %d\n", value & FTDI_PARITY); | |
313 | goto fail; | |
314 | } | |
315 | switch (value & FTDI_STOP) { | |
316 | case FTDI_STOP1: | |
317 | s->params.stop_bits = 1; | |
318 | break; | |
319 | case FTDI_STOP2: | |
320 | s->params.stop_bits = 2; | |
321 | break; | |
322 | default: | |
323 | DPRINTF("unsupported stop bits %d\n", value & FTDI_STOP); | |
324 | goto fail; | |
325 | } | |
5345fdb4 | 326 | qemu_chr_fe_ioctl(&s->cs, CHR_IOCTL_SERIAL_SET_PARAMS, &s->params); |
a7954218 AZ |
327 | /* TODO: TX ON/OFF */ |
328 | break; | |
329 | case DeviceInVendor | FTDI_GET_MDM_ST: | |
abb8a139 AJ |
330 | data[0] = usb_get_modem_lines(s) | 1; |
331 | data[1] = 0; | |
9a77a0f5 | 332 | p->actual_length = 2; |
a7954218 AZ |
333 | break; |
334 | case DeviceOutVendor | FTDI_SET_EVENT_CHR: | |
335 | /* TODO: handle it */ | |
336 | s->event_chr = value; | |
337 | break; | |
338 | case DeviceOutVendor | FTDI_SET_ERROR_CHR: | |
339 | /* TODO: handle it */ | |
340 | s->error_chr = value; | |
341 | break; | |
342 | case DeviceOutVendor | FTDI_SET_LATENCY: | |
343 | s->latency = value; | |
344 | break; | |
345 | case DeviceInVendor | FTDI_GET_LATENCY: | |
346 | data[0] = s->latency; | |
9a77a0f5 | 347 | p->actual_length = 1; |
a7954218 AZ |
348 | break; |
349 | default: | |
350 | fail: | |
351 | DPRINTF("got unsupported/bogus control %x, value %x\n", request, value); | |
9a77a0f5 | 352 | p->status = USB_RET_STALL; |
a7954218 AZ |
353 | break; |
354 | } | |
a7954218 AZ |
355 | } |
356 | ||
9a77a0f5 | 357 | static void usb_serial_handle_data(USBDevice *dev, USBPacket *p) |
a7954218 AZ |
358 | { |
359 | USBSerialState *s = (USBSerialState *)dev; | |
079d0b7f | 360 | uint8_t devep = p->ep->nr; |
9440b7e5 GH |
361 | struct iovec *iov; |
362 | uint8_t header[2]; | |
9a77a0f5 | 363 | int i, first_len, len; |
a7954218 AZ |
364 | |
365 | switch (p->pid) { | |
366 | case USB_TOKEN_OUT: | |
367 | if (devep != 2) | |
368 | goto fail; | |
9440b7e5 GH |
369 | for (i = 0; i < p->iov.niov; i++) { |
370 | iov = p->iov.iov + i; | |
6ab3fc32 DB |
371 | /* XXX this blocks entire thread. Rewrite to use |
372 | * qemu_chr_fe_write and background I/O callbacks */ | |
5345fdb4 | 373 | qemu_chr_fe_write_all(&s->cs, iov->iov_base, iov->iov_len); |
9440b7e5 | 374 | } |
9a77a0f5 | 375 | p->actual_length = p->iov.size; |
a7954218 AZ |
376 | break; |
377 | ||
378 | case USB_TOKEN_IN: | |
379 | if (devep != 1) | |
380 | goto fail; | |
381 | first_len = RECV_BUF - s->recv_ptr; | |
9440b7e5 | 382 | len = p->iov.size; |
a7954218 | 383 | if (len <= 2) { |
9a77a0f5 | 384 | p->status = USB_RET_NAK; |
a7954218 AZ |
385 | break; |
386 | } | |
9440b7e5 | 387 | header[0] = usb_get_modem_lines(s) | 1; |
abb8a139 | 388 | /* We do not have the uart details */ |
7e57f049 JW |
389 | /* handle serial break */ |
390 | if (s->event_trigger && s->event_trigger & FTDI_BI) { | |
391 | s->event_trigger &= ~FTDI_BI; | |
9440b7e5 GH |
392 | header[1] = FTDI_BI; |
393 | usb_packet_copy(p, header, 2); | |
7e57f049 JW |
394 | break; |
395 | } else { | |
9440b7e5 | 396 | header[1] = 0; |
7e57f049 | 397 | } |
a7954218 AZ |
398 | len -= 2; |
399 | if (len > s->recv_used) | |
400 | len = s->recv_used; | |
401 | if (!len) { | |
9a77a0f5 | 402 | p->status = USB_RET_NAK; |
a7954218 AZ |
403 | break; |
404 | } | |
405 | if (first_len > len) | |
406 | first_len = len; | |
9440b7e5 GH |
407 | usb_packet_copy(p, header, 2); |
408 | usb_packet_copy(p, s->recv_buf + s->recv_ptr, first_len); | |
a7954218 | 409 | if (len > first_len) |
9440b7e5 | 410 | usb_packet_copy(p, s->recv_buf, len - first_len); |
a7954218 AZ |
411 | s->recv_used -= len; |
412 | s->recv_ptr = (s->recv_ptr + len) % RECV_BUF; | |
a7954218 AZ |
413 | break; |
414 | ||
415 | default: | |
416 | DPRINTF("Bad token\n"); | |
417 | fail: | |
9a77a0f5 | 418 | p->status = USB_RET_STALL; |
a7954218 AZ |
419 | break; |
420 | } | |
a7954218 AZ |
421 | } |
422 | ||
8fcd3692 | 423 | static int usb_serial_can_read(void *opaque) |
a7954218 AZ |
424 | { |
425 | USBSerialState *s = opaque; | |
da124e62 GH |
426 | |
427 | if (!s->dev.attached) { | |
428 | return 0; | |
429 | } | |
a7954218 AZ |
430 | return RECV_BUF - s->recv_used; |
431 | } | |
432 | ||
8fcd3692 | 433 | static void usb_serial_read(void *opaque, const uint8_t *buf, int size) |
a7954218 AZ |
434 | { |
435 | USBSerialState *s = opaque; | |
4ab4183d DA |
436 | int first_size, start; |
437 | ||
438 | /* room in the buffer? */ | |
439 | if (size > (RECV_BUF - s->recv_used)) | |
440 | size = RECV_BUF - s->recv_used; | |
441 | ||
442 | start = s->recv_ptr + s->recv_used; | |
443 | if (start < RECV_BUF) { | |
444 | /* copy data to end of buffer */ | |
445 | first_size = RECV_BUF - start; | |
446 | if (first_size > size) | |
447 | first_size = size; | |
448 | ||
449 | memcpy(s->recv_buf + start, buf, first_size); | |
450 | ||
451 | /* wrap around to front if needed */ | |
452 | if (size > first_size) | |
453 | memcpy(s->recv_buf, buf + first_size, size - first_size); | |
454 | } else { | |
455 | start -= RECV_BUF; | |
456 | memcpy(s->recv_buf + start, buf, size); | |
457 | } | |
a7954218 AZ |
458 | s->recv_used += size; |
459 | } | |
460 | ||
8fcd3692 | 461 | static void usb_serial_event(void *opaque, int event) |
a7954218 AZ |
462 | { |
463 | USBSerialState *s = opaque; | |
464 | ||
465 | switch (event) { | |
466 | case CHR_EVENT_BREAK: | |
7e57f049 | 467 | s->event_trigger |= FTDI_BI; |
a7954218 AZ |
468 | break; |
469 | case CHR_EVENT_FOCUS: | |
470 | break; | |
b6b8df56 | 471 | case CHR_EVENT_OPENED: |
da124e62 | 472 | if (!s->dev.attached) { |
7334d650 | 473 | usb_device_attach(&s->dev, &error_abort); |
da124e62 GH |
474 | } |
475 | break; | |
476 | case CHR_EVENT_CLOSED: | |
477 | if (s->dev.attached) { | |
478 | usb_device_detach(&s->dev); | |
479 | } | |
a7954218 AZ |
480 | break; |
481 | } | |
482 | } | |
483 | ||
38fff2c9 | 484 | static void usb_serial_realize(USBDevice *dev, Error **errp) |
806b6024 | 485 | { |
cdf0d769 | 486 | USBSerialState *s = USB_SERIAL_DEV(dev); |
7334d650 | 487 | Error *local_err = NULL; |
5345fdb4 | 488 | CharDriverState *chr = qemu_chr_fe_get_driver(&s->cs); |
a980a065 | 489 | |
9d55d1ad | 490 | usb_desc_create_serial(dev); |
a980a065 | 491 | usb_desc_init(dev); |
da124e62 | 492 | dev->auto_attach = 0; |
2b0efdc3 | 493 | |
5345fdb4 | 494 | if (!chr) { |
38fff2c9 GA |
495 | error_setg(errp, "Property chardev is required"); |
496 | return; | |
81bf96d3 MA |
497 | } |
498 | ||
7334d650 GA |
499 | usb_check_attach(dev, &local_err); |
500 | if (local_err) { | |
501 | error_propagate(errp, local_err); | |
502 | return; | |
503 | } | |
504 | ||
5345fdb4 MAL |
505 | qemu_chr_fe_set_handlers(&s->cs, usb_serial_can_read, usb_serial_read, |
506 | usb_serial_event, s, NULL); | |
2b0efdc3 | 507 | usb_serial_handle_reset(dev); |
da124e62 | 508 | |
5345fdb4 | 509 | if (chr->be_open && !dev->attached) { |
7334d650 | 510 | usb_device_attach(dev, &error_abort); |
da124e62 | 511 | } |
806b6024 GH |
512 | } |
513 | ||
3741715c | 514 | static USBDevice *usb_serial_init(USBBus *bus, const char *filename) |
a7954218 | 515 | { |
806b6024 | 516 | USBDevice *dev; |
a7954218 | 517 | CharDriverState *cdrv; |
2b0efdc3 | 518 | uint32_t vendorid = 0, productid = 0; |
5ccfae10 AL |
519 | char label[32]; |
520 | static int index; | |
a7954218 AZ |
521 | |
522 | while (*filename && *filename != ':') { | |
523 | const char *p; | |
524 | char *e; | |
525 | if (strstart(filename, "vendorid=", &p)) { | |
526 | vendorid = strtol(p, &e, 16); | |
527 | if (e == p || (*e && *e != ',' && *e != ':')) { | |
1ecda02b | 528 | error_report("bogus vendor ID %s", p); |
a7954218 AZ |
529 | return NULL; |
530 | } | |
531 | filename = e; | |
532 | } else if (strstart(filename, "productid=", &p)) { | |
533 | productid = strtol(p, &e, 16); | |
534 | if (e == p || (*e && *e != ',' && *e != ':')) { | |
1ecda02b | 535 | error_report("bogus product ID %s", p); |
a7954218 AZ |
536 | return NULL; |
537 | } | |
538 | filename = e; | |
539 | } else { | |
1ecda02b | 540 | error_report("unrecognized serial USB option %s", filename); |
a7954218 AZ |
541 | return NULL; |
542 | } | |
543 | while(*filename == ',') | |
544 | filename++; | |
545 | } | |
546 | if (!*filename) { | |
1ecda02b | 547 | error_report("character device specification needed"); |
a7954218 AZ |
548 | return NULL; |
549 | } | |
550 | filename++; | |
a7954218 | 551 | |
5ccfae10 | 552 | snprintf(label, sizeof(label), "usbserial%d", index++); |
b4948be9 | 553 | cdrv = qemu_chr_new(label, filename); |
a7954218 | 554 | if (!cdrv) |
806b6024 | 555 | return NULL; |
a7954218 | 556 | |
3741715c | 557 | dev = usb_create(bus, "usb-serial"); |
2b0efdc3 GH |
558 | qdev_prop_set_chr(&dev->qdev, "chardev", cdrv); |
559 | if (vendorid) | |
560 | qdev_prop_set_uint16(&dev->qdev, "vendorid", vendorid); | |
561 | if (productid) | |
562 | qdev_prop_set_uint16(&dev->qdev, "productid", productid); | |
2b0efdc3 GH |
563 | return dev; |
564 | } | |
565 | ||
3741715c | 566 | static USBDevice *usb_braille_init(USBBus *bus, const char *unused) |
2b0efdc3 GH |
567 | { |
568 | USBDevice *dev; | |
569 | CharDriverState *cdrv; | |
570 | ||
b4948be9 | 571 | cdrv = qemu_chr_new("braille", "braille"); |
2b0efdc3 GH |
572 | if (!cdrv) |
573 | return NULL; | |
806b6024 | 574 | |
3741715c | 575 | dev = usb_create(bus, "usb-braille"); |
2b0efdc3 | 576 | qdev_prop_set_chr(&dev->qdev, "chardev", cdrv); |
2b0efdc3 | 577 | return dev; |
a7954218 | 578 | } |
806b6024 | 579 | |
98e51ec9 GH |
580 | static const VMStateDescription vmstate_usb_serial = { |
581 | .name = "usb-serial", | |
582 | .unmigratable = 1, | |
583 | }; | |
584 | ||
39bffca2 AL |
585 | static Property serial_properties[] = { |
586 | DEFINE_PROP_CHR("chardev", USBSerialState, cs), | |
587 | DEFINE_PROP_END_OF_LIST(), | |
588 | }; | |
589 | ||
cdf0d769 | 590 | static void usb_serial_dev_class_init(ObjectClass *klass, void *data) |
62aed765 | 591 | { |
39bffca2 | 592 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
593 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
594 | ||
cdf0d769 | 595 | uc->realize = usb_serial_realize; |
62aed765 AL |
596 | uc->handle_reset = usb_serial_handle_reset; |
597 | uc->handle_control = usb_serial_handle_control; | |
598 | uc->handle_data = usb_serial_handle_data; | |
39bffca2 | 599 | dc->vmsd = &vmstate_usb_serial; |
125ee0ed | 600 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
62aed765 AL |
601 | } |
602 | ||
cdf0d769 GA |
603 | static const TypeInfo usb_serial_dev_type_info = { |
604 | .name = TYPE_USB_SERIAL, | |
605 | .parent = TYPE_USB_DEVICE, | |
606 | .instance_size = sizeof(USBSerialState), | |
607 | .abstract = true, | |
608 | .class_init = usb_serial_dev_class_init, | |
609 | }; | |
610 | ||
611 | static void usb_serial_class_initfn(ObjectClass *klass, void *data) | |
612 | { | |
613 | DeviceClass *dc = DEVICE_CLASS(klass); | |
614 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
615 | ||
616 | uc->product_desc = "QEMU USB Serial"; | |
617 | uc->usb_desc = &desc_serial; | |
618 | dc->props = serial_properties; | |
619 | } | |
620 | ||
8c43a6f0 | 621 | static const TypeInfo serial_info = { |
39bffca2 | 622 | .name = "usb-serial", |
cdf0d769 | 623 | .parent = TYPE_USB_SERIAL, |
39bffca2 AL |
624 | .class_init = usb_serial_class_initfn, |
625 | }; | |
626 | ||
627 | static Property braille_properties[] = { | |
628 | DEFINE_PROP_CHR("chardev", USBSerialState, cs), | |
629 | DEFINE_PROP_END_OF_LIST(), | |
2b0efdc3 GH |
630 | }; |
631 | ||
62aed765 AL |
632 | static void usb_braille_class_initfn(ObjectClass *klass, void *data) |
633 | { | |
39bffca2 | 634 | DeviceClass *dc = DEVICE_CLASS(klass); |
62aed765 AL |
635 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
636 | ||
62aed765 AL |
637 | uc->product_desc = "QEMU USB Braille"; |
638 | uc->usb_desc = &desc_braille; | |
39bffca2 | 639 | dc->props = braille_properties; |
62aed765 AL |
640 | } |
641 | ||
8c43a6f0 | 642 | static const TypeInfo braille_info = { |
39bffca2 | 643 | .name = "usb-braille", |
cdf0d769 | 644 | .parent = TYPE_USB_SERIAL, |
39bffca2 | 645 | .class_init = usb_braille_class_initfn, |
806b6024 GH |
646 | }; |
647 | ||
83f7d43a | 648 | static void usb_serial_register_types(void) |
806b6024 | 649 | { |
cdf0d769 | 650 | type_register_static(&usb_serial_dev_type_info); |
39bffca2 | 651 | type_register_static(&serial_info); |
ba02430f | 652 | usb_legacy_register("usb-serial", "serial", usb_serial_init); |
39bffca2 | 653 | type_register_static(&braille_info); |
ba02430f | 654 | usb_legacy_register("usb-braille", "braille", usb_braille_init); |
806b6024 | 655 | } |
83f7d43a AF |
656 | |
657 | type_init(usb_serial_register_types) |