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