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