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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
28 #include "chardev/char-parallel.h"
29 #include "chardev/char-fe.h"
30 #include "hw/isa/isa.h"
31 #include "hw/char/parallel.h"
32 #include "sysemu/sysemu.h"
35 //#define DEBUG_PARALLEL
38 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
40 #define pdebug(fmt, ...) ((void)0)
43 #define PARA_REG_DATA 0
44 #define PARA_REG_STS 1
45 #define PARA_REG_CTR 2
46 #define PARA_REG_EPP_ADDR 3
47 #define PARA_REG_EPP_DATA 4
50 * These are the definitions for the Printer Status Register
52 #define PARA_STS_BUSY 0x80 /* Busy complement */
53 #define PARA_STS_ACK 0x40 /* Acknowledge */
54 #define PARA_STS_PAPER 0x20 /* Out of paper */
55 #define PARA_STS_ONLINE 0x10 /* Online */
56 #define PARA_STS_ERROR 0x08 /* Error complement */
57 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
60 * These are the definitions for the Printer Control Register
62 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
63 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
64 #define PARA_CTR_SELECT 0x08 /* Select In complement */
65 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
66 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
67 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
69 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
71 typedef struct ParallelState {
82 uint32_t last_read_offset; /* For debugging */
83 /* Memory-mapped interface */
85 PortioList portio_list;
88 #define TYPE_ISA_PARALLEL "isa-parallel"
89 #define ISA_PARALLEL(obj) \
90 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
92 typedef struct ISAParallelState {
101 static void parallel_update_irq(ParallelState *s)
104 qemu_irq_raise(s->irq);
106 qemu_irq_lower(s->irq);
110 parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
112 ParallelState *s = opaque;
115 trace_parallel_ioport_write("SW", addr, val);
119 parallel_update_irq(s);
123 if ((val & PARA_CTR_INIT) == 0 ) {
124 s->status = PARA_STS_BUSY;
125 s->status |= PARA_STS_ACK;
126 s->status |= PARA_STS_ONLINE;
127 s->status |= PARA_STS_ERROR;
129 else if (val & PARA_CTR_SELECT) {
130 if (val & PARA_CTR_STROBE) {
131 s->status &= ~PARA_STS_BUSY;
132 if ((s->control & PARA_CTR_STROBE) == 0)
133 /* XXX this blocks entire thread. Rewrite to use
134 * qemu_chr_fe_write and background I/O callbacks */
135 qemu_chr_fe_write_all(&s->chr, &s->dataw, 1);
137 if (s->control & PARA_CTR_INTEN) {
142 parallel_update_irq(s);
148 static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
150 ParallelState *s = opaque;
154 /* Sometimes programs do several writes for timing purposes on old
155 HW. Take care not to waste time on writes that do nothing. */
157 s->last_read_offset = ~0U;
160 trace_parallel_ioport_write("HW", addr, val);
165 pdebug("wd%02x\n", val);
166 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
170 pdebug("ws%02x\n", val);
171 if (val & PARA_STS_TMOUT)
176 if (s->control == val)
178 pdebug("wc%02x\n", val);
180 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
181 if (val & PARA_CTR_DIR) {
186 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
187 parm &= ~PARA_CTR_DIR;
190 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
193 case PARA_REG_EPP_ADDR:
194 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
195 /* Controls not correct for EPP address cycle, so do nothing */
196 pdebug("wa%02x s\n", val);
198 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
199 if (qemu_chr_fe_ioctl(&s->chr,
200 CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
202 pdebug("wa%02x t\n", val);
205 pdebug("wa%02x\n", val);
208 case PARA_REG_EPP_DATA:
209 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
210 /* Controls not correct for EPP data cycle, so do nothing */
211 pdebug("we%02x s\n", val);
213 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
214 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
216 pdebug("we%02x t\n", val);
219 pdebug("we%02x\n", val);
226 parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
228 ParallelState *s = opaque;
229 uint16_t eppdata = cpu_to_le16(val);
231 struct ParallelIOArg ioarg = {
232 .buffer = &eppdata, .count = sizeof(eppdata)
235 trace_parallel_ioport_write("EPP", addr, val);
236 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
237 /* Controls not correct for EPP data cycle, so do nothing */
238 pdebug("we%04x s\n", val);
241 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
244 pdebug("we%04x t\n", val);
247 pdebug("we%04x\n", val);
251 parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
253 ParallelState *s = opaque;
254 uint32_t eppdata = cpu_to_le32(val);
256 struct ParallelIOArg ioarg = {
257 .buffer = &eppdata, .count = sizeof(eppdata)
260 trace_parallel_ioport_write("EPP", addr, val);
261 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
262 /* Controls not correct for EPP data cycle, so do nothing */
263 pdebug("we%08x s\n", val);
266 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
269 pdebug("we%08x t\n", val);
272 pdebug("we%08x\n", val);
275 static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
277 ParallelState *s = opaque;
283 if (s->control & PARA_CTR_DIR)
291 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
292 /* XXX Fixme: wait 5 microseconds */
293 if (s->status & PARA_STS_ACK)
294 s->status &= ~PARA_STS_ACK;
296 /* XXX Fixme: wait 5 microseconds */
297 s->status |= PARA_STS_ACK;
298 s->status |= PARA_STS_BUSY;
301 parallel_update_irq(s);
307 trace_parallel_ioport_read("SW", addr, ret);
311 static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
313 ParallelState *s = opaque;
318 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
319 if (s->last_read_offset != addr || s->datar != ret)
320 pdebug("rd%02x\n", ret);
324 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
325 ret &= ~PARA_STS_TMOUT;
327 ret |= PARA_STS_TMOUT;
328 if (s->last_read_offset != addr || s->status != ret)
329 pdebug("rs%02x\n", ret);
333 /* s->control has some bits fixed to 1. It is zero only when
334 it has not been yet written to. */
335 if (s->control == 0) {
336 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
337 if (s->last_read_offset != addr)
338 pdebug("rc%02x\n", ret);
343 if (s->last_read_offset != addr)
344 pdebug("rc%02x\n", ret);
347 case PARA_REG_EPP_ADDR:
348 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
349 (PARA_CTR_DIR | PARA_CTR_INIT))
350 /* Controls not correct for EPP addr cycle, so do nothing */
351 pdebug("ra%02x s\n", ret);
353 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
354 if (qemu_chr_fe_ioctl(&s->chr,
355 CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
357 pdebug("ra%02x t\n", ret);
360 pdebug("ra%02x\n", ret);
363 case PARA_REG_EPP_DATA:
364 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
365 (PARA_CTR_DIR | PARA_CTR_INIT))
366 /* Controls not correct for EPP data cycle, so do nothing */
367 pdebug("re%02x s\n", ret);
369 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
370 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
372 pdebug("re%02x t\n", ret);
375 pdebug("re%02x\n", ret);
379 trace_parallel_ioport_read("HW", addr, ret);
380 s->last_read_offset = addr;
385 parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
387 ParallelState *s = opaque;
389 uint16_t eppdata = ~0;
391 struct ParallelIOArg ioarg = {
392 .buffer = &eppdata, .count = sizeof(eppdata)
394 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
395 /* Controls not correct for EPP data cycle, so do nothing */
396 pdebug("re%04x s\n", eppdata);
399 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
400 ret = le16_to_cpu(eppdata);
404 pdebug("re%04x t\n", ret);
407 pdebug("re%04x\n", ret);
408 trace_parallel_ioport_read("EPP", addr, ret);
413 parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
415 ParallelState *s = opaque;
417 uint32_t eppdata = ~0U;
419 struct ParallelIOArg ioarg = {
420 .buffer = &eppdata, .count = sizeof(eppdata)
422 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
423 /* Controls not correct for EPP data cycle, so do nothing */
424 pdebug("re%08x s\n", eppdata);
427 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
428 ret = le32_to_cpu(eppdata);
432 pdebug("re%08x t\n", ret);
435 pdebug("re%08x\n", ret);
436 trace_parallel_ioport_read("EPP", addr, ret);
440 static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
442 trace_parallel_ioport_write("ECP", addr & 7, val);
443 pdebug("wecp%d=%02x\n", addr & 7, val);
446 static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
450 trace_parallel_ioport_read("ECP", addr & 7, ret);
451 pdebug("recp%d:%02x\n", addr & 7, ret);
455 static void parallel_reset(void *opaque)
457 ParallelState *s = opaque;
461 s->status = PARA_STS_BUSY;
462 s->status |= PARA_STS_ACK;
463 s->status |= PARA_STS_ONLINE;
464 s->status |= PARA_STS_ERROR;
465 s->status |= PARA_STS_TMOUT;
466 s->control = PARA_CTR_SELECT;
467 s->control |= PARA_CTR_INIT;
472 s->last_read_offset = ~0U;
475 static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
477 static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
479 .read = parallel_ioport_read_hw,
480 .write = parallel_ioport_write_hw },
482 .read = parallel_ioport_eppdata_read_hw2,
483 .write = parallel_ioport_eppdata_write_hw2 },
485 .read = parallel_ioport_eppdata_read_hw4,
486 .write = parallel_ioport_eppdata_write_hw4 },
488 .read = parallel_ioport_ecp_read,
489 .write = parallel_ioport_ecp_write },
490 PORTIO_END_OF_LIST(),
493 static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
495 .read = parallel_ioport_read_sw,
496 .write = parallel_ioport_write_sw },
497 PORTIO_END_OF_LIST(),
501 static const VMStateDescription vmstate_parallel_isa = {
502 .name = "parallel_isa",
504 .minimum_version_id = 1,
505 .fields = (VMStateField[]) {
506 VMSTATE_UINT8(state.dataw, ISAParallelState),
507 VMSTATE_UINT8(state.datar, ISAParallelState),
508 VMSTATE_UINT8(state.status, ISAParallelState),
509 VMSTATE_UINT8(state.control, ISAParallelState),
510 VMSTATE_INT32(state.irq_pending, ISAParallelState),
511 VMSTATE_INT32(state.epp_timeout, ISAParallelState),
512 VMSTATE_END_OF_LIST()
516 static int parallel_can_receive(void *opaque)
521 static void parallel_isa_realizefn(DeviceState *dev, Error **errp)
524 ISADevice *isadev = ISA_DEVICE(dev);
525 ISAParallelState *isa = ISA_PARALLEL(dev);
526 ParallelState *s = &isa->state;
530 if (!qemu_chr_fe_backend_connected(&s->chr)) {
531 error_setg(errp, "Can't create parallel device, empty char device");
535 if (isa->index == -1) {
538 if (isa->index >= MAX_PARALLEL_PORTS) {
539 error_setg(errp, "Max. supported number of parallel ports is %d.",
543 if (isa->iobase == -1) {
544 isa->iobase = isa_parallel_io[isa->index];
549 isa_init_irq(isadev, &s->irq, isa->isairq);
550 qemu_register_reset(parallel_reset, s);
552 qemu_chr_fe_set_handlers(&s->chr, parallel_can_receive, NULL,
553 NULL, NULL, s, NULL, true);
554 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
559 isa_register_portio_list(isadev, &s->portio_list, base,
561 ? &isa_parallel_portio_hw_list[0]
562 : &isa_parallel_portio_sw_list[0]),
566 /* Memory mapped interface */
567 static uint64_t parallel_mm_readfn(void *opaque, hwaddr addr, unsigned size)
569 ParallelState *s = opaque;
571 return parallel_ioport_read_sw(s, addr >> s->it_shift) &
572 MAKE_64BIT_MASK(0, size * 8);
575 static void parallel_mm_writefn(void *opaque, hwaddr addr,
576 uint64_t value, unsigned size)
578 ParallelState *s = opaque;
580 parallel_ioport_write_sw(s, addr >> s->it_shift,
581 value & MAKE_64BIT_MASK(0, size * 8));
584 static const MemoryRegionOps parallel_mm_ops = {
585 .read = parallel_mm_readfn,
586 .write = parallel_mm_writefn,
587 .valid.min_access_size = 1,
588 .valid.max_access_size = 4,
589 .endianness = DEVICE_NATIVE_ENDIAN,
592 /* If fd is zero, it means that the parallel device uses the console */
593 bool parallel_mm_init(MemoryRegion *address_space,
594 hwaddr base, int it_shift, qemu_irq irq,
599 s = g_malloc0(sizeof(ParallelState));
601 qemu_chr_fe_init(&s->chr, chr, &error_abort);
602 s->it_shift = it_shift;
603 qemu_register_reset(parallel_reset, s);
605 memory_region_init_io(&s->iomem, NULL, ¶llel_mm_ops, s,
606 "parallel", 8 << it_shift);
607 memory_region_add_subregion(address_space, base, &s->iomem);
611 static Property parallel_isa_properties[] = {
612 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
613 DEFINE_PROP_UINT32("iobase", ISAParallelState, iobase, -1),
614 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
615 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
616 DEFINE_PROP_END_OF_LIST(),
619 static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
621 DeviceClass *dc = DEVICE_CLASS(klass);
623 dc->realize = parallel_isa_realizefn;
624 dc->vmsd = &vmstate_parallel_isa;
625 dc->props = parallel_isa_properties;
626 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
629 static const TypeInfo parallel_isa_info = {
630 .name = TYPE_ISA_PARALLEL,
631 .parent = TYPE_ISA_DEVICE,
632 .instance_size = sizeof(ISAParallelState),
633 .class_init = parallel_isa_class_initfn,
636 static void parallel_register_types(void)
638 type_register_static(¶llel_isa_info);
641 type_init(parallel_register_types)