2 * QEMU Parallel PORT emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 * Copyright (c) 2007 Marko Kohtala
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/module.h"
29 #include "chardev/char-parallel.h"
30 #include "chardev/char-fe.h"
32 #include "hw/isa/isa.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
35 #include "hw/char/parallel.h"
36 #include "sysemu/reset.h"
37 #include "sysemu/sysemu.h"
40 //#define DEBUG_PARALLEL
43 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
45 #define pdebug(fmt, ...) ((void)0)
48 #define PARA_REG_DATA 0
49 #define PARA_REG_STS 1
50 #define PARA_REG_CTR 2
51 #define PARA_REG_EPP_ADDR 3
52 #define PARA_REG_EPP_DATA 4
55 * These are the definitions for the Printer Status Register
57 #define PARA_STS_BUSY 0x80 /* Busy complement */
58 #define PARA_STS_ACK 0x40 /* Acknowledge */
59 #define PARA_STS_PAPER 0x20 /* Out of paper */
60 #define PARA_STS_ONLINE 0x10 /* Online */
61 #define PARA_STS_ERROR 0x08 /* Error complement */
62 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
65 * These are the definitions for the Printer Control Register
67 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
68 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
69 #define PARA_CTR_SELECT 0x08 /* Select In complement */
70 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
71 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
72 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
74 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
76 typedef struct ParallelState {
87 uint32_t last_read_offset; /* For debugging */
88 /* Memory-mapped interface */
90 PortioList portio_list;
93 #define TYPE_ISA_PARALLEL "isa-parallel"
94 #define ISA_PARALLEL(obj) \
95 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
97 typedef struct ISAParallelState {
106 static void parallel_update_irq(ParallelState *s)
109 qemu_irq_raise(s->irq);
111 qemu_irq_lower(s->irq);
115 parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
117 ParallelState *s = opaque;
120 trace_parallel_ioport_write("SW", addr, val);
124 parallel_update_irq(s);
128 if ((val & PARA_CTR_INIT) == 0 ) {
129 s->status = PARA_STS_BUSY;
130 s->status |= PARA_STS_ACK;
131 s->status |= PARA_STS_ONLINE;
132 s->status |= PARA_STS_ERROR;
134 else if (val & PARA_CTR_SELECT) {
135 if (val & PARA_CTR_STROBE) {
136 s->status &= ~PARA_STS_BUSY;
137 if ((s->control & PARA_CTR_STROBE) == 0)
138 /* XXX this blocks entire thread. Rewrite to use
139 * qemu_chr_fe_write and background I/O callbacks */
140 qemu_chr_fe_write_all(&s->chr, &s->dataw, 1);
142 if (s->control & PARA_CTR_INTEN) {
147 parallel_update_irq(s);
153 static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
155 ParallelState *s = opaque;
159 /* Sometimes programs do several writes for timing purposes on old
160 HW. Take care not to waste time on writes that do nothing. */
162 s->last_read_offset = ~0U;
165 trace_parallel_ioport_write("HW", addr, val);
170 pdebug("wd%02x\n", val);
171 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
175 pdebug("ws%02x\n", val);
176 if (val & PARA_STS_TMOUT)
181 if (s->control == val)
183 pdebug("wc%02x\n", val);
185 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
186 if (val & PARA_CTR_DIR) {
191 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
192 parm &= ~PARA_CTR_DIR;
195 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
198 case PARA_REG_EPP_ADDR:
199 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
200 /* Controls not correct for EPP address cycle, so do nothing */
201 pdebug("wa%02x s\n", val);
203 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
204 if (qemu_chr_fe_ioctl(&s->chr,
205 CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
207 pdebug("wa%02x t\n", val);
210 pdebug("wa%02x\n", val);
213 case PARA_REG_EPP_DATA:
214 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
215 /* Controls not correct for EPP data cycle, so do nothing */
216 pdebug("we%02x s\n", val);
218 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
219 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
221 pdebug("we%02x t\n", val);
224 pdebug("we%02x\n", val);
231 parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
233 ParallelState *s = opaque;
234 uint16_t eppdata = cpu_to_le16(val);
236 struct ParallelIOArg ioarg = {
237 .buffer = &eppdata, .count = sizeof(eppdata)
240 trace_parallel_ioport_write("EPP", addr, val);
241 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
242 /* Controls not correct for EPP data cycle, so do nothing */
243 pdebug("we%04x s\n", val);
246 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
249 pdebug("we%04x t\n", val);
252 pdebug("we%04x\n", val);
256 parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
258 ParallelState *s = opaque;
259 uint32_t eppdata = cpu_to_le32(val);
261 struct ParallelIOArg ioarg = {
262 .buffer = &eppdata, .count = sizeof(eppdata)
265 trace_parallel_ioport_write("EPP", addr, val);
266 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
267 /* Controls not correct for EPP data cycle, so do nothing */
268 pdebug("we%08x s\n", val);
271 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
274 pdebug("we%08x t\n", val);
277 pdebug("we%08x\n", val);
280 static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
282 ParallelState *s = opaque;
288 if (s->control & PARA_CTR_DIR)
296 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
297 /* XXX Fixme: wait 5 microseconds */
298 if (s->status & PARA_STS_ACK)
299 s->status &= ~PARA_STS_ACK;
301 /* XXX Fixme: wait 5 microseconds */
302 s->status |= PARA_STS_ACK;
303 s->status |= PARA_STS_BUSY;
306 parallel_update_irq(s);
312 trace_parallel_ioport_read("SW", addr, ret);
316 static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
318 ParallelState *s = opaque;
323 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
324 if (s->last_read_offset != addr || s->datar != ret)
325 pdebug("rd%02x\n", ret);
329 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
330 ret &= ~PARA_STS_TMOUT;
332 ret |= PARA_STS_TMOUT;
333 if (s->last_read_offset != addr || s->status != ret)
334 pdebug("rs%02x\n", ret);
338 /* s->control has some bits fixed to 1. It is zero only when
339 it has not been yet written to. */
340 if (s->control == 0) {
341 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
342 if (s->last_read_offset != addr)
343 pdebug("rc%02x\n", ret);
348 if (s->last_read_offset != addr)
349 pdebug("rc%02x\n", ret);
352 case PARA_REG_EPP_ADDR:
353 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
354 (PARA_CTR_DIR | PARA_CTR_INIT))
355 /* Controls not correct for EPP addr cycle, so do nothing */
356 pdebug("ra%02x s\n", ret);
358 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
359 if (qemu_chr_fe_ioctl(&s->chr,
360 CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
362 pdebug("ra%02x t\n", ret);
365 pdebug("ra%02x\n", ret);
368 case PARA_REG_EPP_DATA:
369 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
370 (PARA_CTR_DIR | PARA_CTR_INIT))
371 /* Controls not correct for EPP data cycle, so do nothing */
372 pdebug("re%02x s\n", ret);
374 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
375 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
377 pdebug("re%02x t\n", ret);
380 pdebug("re%02x\n", ret);
384 trace_parallel_ioport_read("HW", addr, ret);
385 s->last_read_offset = addr;
390 parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
392 ParallelState *s = opaque;
394 uint16_t eppdata = ~0;
396 struct ParallelIOArg ioarg = {
397 .buffer = &eppdata, .count = sizeof(eppdata)
399 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
400 /* Controls not correct for EPP data cycle, so do nothing */
401 pdebug("re%04x s\n", eppdata);
404 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
405 ret = le16_to_cpu(eppdata);
409 pdebug("re%04x t\n", ret);
412 pdebug("re%04x\n", ret);
413 trace_parallel_ioport_read("EPP", addr, ret);
418 parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
420 ParallelState *s = opaque;
422 uint32_t eppdata = ~0U;
424 struct ParallelIOArg ioarg = {
425 .buffer = &eppdata, .count = sizeof(eppdata)
427 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
428 /* Controls not correct for EPP data cycle, so do nothing */
429 pdebug("re%08x s\n", eppdata);
432 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
433 ret = le32_to_cpu(eppdata);
437 pdebug("re%08x t\n", ret);
440 pdebug("re%08x\n", ret);
441 trace_parallel_ioport_read("EPP", addr, ret);
445 static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
447 trace_parallel_ioport_write("ECP", addr & 7, val);
448 pdebug("wecp%d=%02x\n", addr & 7, val);
451 static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
455 trace_parallel_ioport_read("ECP", addr & 7, ret);
456 pdebug("recp%d:%02x\n", addr & 7, ret);
460 static void parallel_reset(void *opaque)
462 ParallelState *s = opaque;
466 s->status = PARA_STS_BUSY;
467 s->status |= PARA_STS_ACK;
468 s->status |= PARA_STS_ONLINE;
469 s->status |= PARA_STS_ERROR;
470 s->status |= PARA_STS_TMOUT;
471 s->control = PARA_CTR_SELECT;
472 s->control |= PARA_CTR_INIT;
477 s->last_read_offset = ~0U;
480 static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
482 static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
484 .read = parallel_ioport_read_hw,
485 .write = parallel_ioport_write_hw },
487 .read = parallel_ioport_eppdata_read_hw2,
488 .write = parallel_ioport_eppdata_write_hw2 },
490 .read = parallel_ioport_eppdata_read_hw4,
491 .write = parallel_ioport_eppdata_write_hw4 },
493 .read = parallel_ioport_ecp_read,
494 .write = parallel_ioport_ecp_write },
495 PORTIO_END_OF_LIST(),
498 static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
500 .read = parallel_ioport_read_sw,
501 .write = parallel_ioport_write_sw },
502 PORTIO_END_OF_LIST(),
506 static const VMStateDescription vmstate_parallel_isa = {
507 .name = "parallel_isa",
509 .minimum_version_id = 1,
510 .fields = (VMStateField[]) {
511 VMSTATE_UINT8(state.dataw, ISAParallelState),
512 VMSTATE_UINT8(state.datar, ISAParallelState),
513 VMSTATE_UINT8(state.status, ISAParallelState),
514 VMSTATE_UINT8(state.control, ISAParallelState),
515 VMSTATE_INT32(state.irq_pending, ISAParallelState),
516 VMSTATE_INT32(state.epp_timeout, ISAParallelState),
517 VMSTATE_END_OF_LIST()
521 static int parallel_can_receive(void *opaque)
526 static void parallel_isa_realizefn(DeviceState *dev, Error **errp)
529 ISADevice *isadev = ISA_DEVICE(dev);
530 ISAParallelState *isa = ISA_PARALLEL(dev);
531 ParallelState *s = &isa->state;
535 if (!qemu_chr_fe_backend_connected(&s->chr)) {
536 error_setg(errp, "Can't create parallel device, empty char device");
540 if (isa->index == -1) {
543 if (isa->index >= MAX_PARALLEL_PORTS) {
544 error_setg(errp, "Max. supported number of parallel ports is %d.",
548 if (isa->iobase == -1) {
549 isa->iobase = isa_parallel_io[isa->index];
554 isa_init_irq(isadev, &s->irq, isa->isairq);
555 qemu_register_reset(parallel_reset, s);
557 qemu_chr_fe_set_handlers(&s->chr, parallel_can_receive, NULL,
558 NULL, NULL, s, NULL, true);
559 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
564 isa_register_portio_list(isadev, &s->portio_list, base,
566 ? &isa_parallel_portio_hw_list[0]
567 : &isa_parallel_portio_sw_list[0]),
571 /* Memory mapped interface */
572 static uint64_t parallel_mm_readfn(void *opaque, hwaddr addr, unsigned size)
574 ParallelState *s = opaque;
576 return parallel_ioport_read_sw(s, addr >> s->it_shift) &
577 MAKE_64BIT_MASK(0, size * 8);
580 static void parallel_mm_writefn(void *opaque, hwaddr addr,
581 uint64_t value, unsigned size)
583 ParallelState *s = opaque;
585 parallel_ioport_write_sw(s, addr >> s->it_shift,
586 value & MAKE_64BIT_MASK(0, size * 8));
589 static const MemoryRegionOps parallel_mm_ops = {
590 .read = parallel_mm_readfn,
591 .write = parallel_mm_writefn,
592 .valid.min_access_size = 1,
593 .valid.max_access_size = 4,
594 .endianness = DEVICE_NATIVE_ENDIAN,
597 /* If fd is zero, it means that the parallel device uses the console */
598 bool parallel_mm_init(MemoryRegion *address_space,
599 hwaddr base, int it_shift, qemu_irq irq,
604 s = g_malloc0(sizeof(ParallelState));
606 qemu_chr_fe_init(&s->chr, chr, &error_abort);
607 s->it_shift = it_shift;
608 qemu_register_reset(parallel_reset, s);
610 memory_region_init_io(&s->iomem, NULL, ¶llel_mm_ops, s,
611 "parallel", 8 << it_shift);
612 memory_region_add_subregion(address_space, base, &s->iomem);
616 static Property parallel_isa_properties[] = {
617 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
618 DEFINE_PROP_UINT32("iobase", ISAParallelState, iobase, -1),
619 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
620 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
621 DEFINE_PROP_END_OF_LIST(),
624 static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
626 DeviceClass *dc = DEVICE_CLASS(klass);
628 dc->realize = parallel_isa_realizefn;
629 dc->vmsd = &vmstate_parallel_isa;
630 dc->props = parallel_isa_properties;
631 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
634 static const TypeInfo parallel_isa_info = {
635 .name = TYPE_ISA_PARALLEL,
636 .parent = TYPE_ISA_DEVICE,
637 .instance_size = sizeof(ISAParallelState),
638 .class_init = parallel_isa_class_initfn,
641 static void parallel_register_types(void)
643 type_register_static(¶llel_isa_info);
646 type_init(parallel_register_types)