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"
27 #include "sysemu/char.h"
28 #include "hw/isa/isa.h"
29 #include "hw/i386/pc.h"
30 #include "sysemu/sysemu.h"
32 //#define DEBUG_PARALLEL
35 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
37 #define pdebug(fmt, ...) ((void)0)
40 #define PARA_REG_DATA 0
41 #define PARA_REG_STS 1
42 #define PARA_REG_CTR 2
43 #define PARA_REG_EPP_ADDR 3
44 #define PARA_REG_EPP_DATA 4
47 * These are the definitions for the Printer Status Register
49 #define PARA_STS_BUSY 0x80 /* Busy complement */
50 #define PARA_STS_ACK 0x40 /* Acknowledge */
51 #define PARA_STS_PAPER 0x20 /* Out of paper */
52 #define PARA_STS_ONLINE 0x10 /* Online */
53 #define PARA_STS_ERROR 0x08 /* Error complement */
54 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
57 * These are the definitions for the Printer Control Register
59 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
60 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
61 #define PARA_CTR_SELECT 0x08 /* Select In complement */
62 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
63 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
64 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
66 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
68 typedef struct ParallelState {
79 uint32_t last_read_offset; /* For debugging */
80 /* Memory-mapped interface */
84 #define TYPE_ISA_PARALLEL "isa-parallel"
85 #define ISA_PARALLEL(obj) \
86 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
88 typedef struct ISAParallelState {
97 static void parallel_update_irq(ParallelState *s)
100 qemu_irq_raise(s->irq);
102 qemu_irq_lower(s->irq);
106 parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
108 ParallelState *s = opaque;
110 pdebug("write addr=0x%02x val=0x%02x\n", addr, val);
116 parallel_update_irq(s);
120 if ((val & PARA_CTR_INIT) == 0 ) {
121 s->status = PARA_STS_BUSY;
122 s->status |= PARA_STS_ACK;
123 s->status |= PARA_STS_ONLINE;
124 s->status |= PARA_STS_ERROR;
126 else if (val & PARA_CTR_SELECT) {
127 if (val & PARA_CTR_STROBE) {
128 s->status &= ~PARA_STS_BUSY;
129 if ((s->control & PARA_CTR_STROBE) == 0)
130 qemu_chr_fe_write(s->chr, &s->dataw, 1);
132 if (s->control & PARA_CTR_INTEN) {
137 parallel_update_irq(s);
143 static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
145 ParallelState *s = opaque;
149 /* Sometimes programs do several writes for timing purposes on old
150 HW. Take care not to waste time on writes that do nothing. */
152 s->last_read_offset = ~0U;
159 pdebug("wd%02x\n", val);
160 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
164 pdebug("ws%02x\n", val);
165 if (val & PARA_STS_TMOUT)
170 if (s->control == val)
172 pdebug("wc%02x\n", val);
174 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
175 if (val & PARA_CTR_DIR) {
180 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
181 parm &= ~PARA_CTR_DIR;
184 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
187 case PARA_REG_EPP_ADDR:
188 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
189 /* Controls not correct for EPP address cycle, so do nothing */
190 pdebug("wa%02x s\n", val);
192 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
193 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
195 pdebug("wa%02x t\n", val);
198 pdebug("wa%02x\n", val);
201 case PARA_REG_EPP_DATA:
202 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
203 /* Controls not correct for EPP data cycle, so do nothing */
204 pdebug("we%02x s\n", val);
206 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
207 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
209 pdebug("we%02x t\n", val);
212 pdebug("we%02x\n", val);
219 parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
221 ParallelState *s = opaque;
222 uint16_t eppdata = cpu_to_le16(val);
224 struct ParallelIOArg ioarg = {
225 .buffer = &eppdata, .count = sizeof(eppdata)
227 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
228 /* Controls not correct for EPP data cycle, so do nothing */
229 pdebug("we%04x s\n", val);
232 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
235 pdebug("we%04x t\n", val);
238 pdebug("we%04x\n", val);
242 parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
244 ParallelState *s = opaque;
245 uint32_t eppdata = cpu_to_le32(val);
247 struct ParallelIOArg ioarg = {
248 .buffer = &eppdata, .count = sizeof(eppdata)
250 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
251 /* Controls not correct for EPP data cycle, so do nothing */
252 pdebug("we%08x s\n", val);
255 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
258 pdebug("we%08x t\n", val);
261 pdebug("we%08x\n", val);
264 static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
266 ParallelState *s = opaque;
272 if (s->control & PARA_CTR_DIR)
280 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
281 /* XXX Fixme: wait 5 microseconds */
282 if (s->status & PARA_STS_ACK)
283 s->status &= ~PARA_STS_ACK;
285 /* XXX Fixme: wait 5 microseconds */
286 s->status |= PARA_STS_ACK;
287 s->status |= PARA_STS_BUSY;
290 parallel_update_irq(s);
296 pdebug("read addr=0x%02x val=0x%02x\n", addr, ret);
300 static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
302 ParallelState *s = opaque;
307 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
308 if (s->last_read_offset != addr || s->datar != ret)
309 pdebug("rd%02x\n", ret);
313 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
314 ret &= ~PARA_STS_TMOUT;
316 ret |= PARA_STS_TMOUT;
317 if (s->last_read_offset != addr || s->status != ret)
318 pdebug("rs%02x\n", ret);
322 /* s->control has some bits fixed to 1. It is zero only when
323 it has not been yet written to. */
324 if (s->control == 0) {
325 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
326 if (s->last_read_offset != addr)
327 pdebug("rc%02x\n", ret);
332 if (s->last_read_offset != addr)
333 pdebug("rc%02x\n", ret);
336 case PARA_REG_EPP_ADDR:
337 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
338 /* Controls not correct for EPP addr cycle, so do nothing */
339 pdebug("ra%02x s\n", ret);
341 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
342 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
344 pdebug("ra%02x t\n", ret);
347 pdebug("ra%02x\n", ret);
350 case PARA_REG_EPP_DATA:
351 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT))
352 /* Controls not correct for EPP data cycle, so do nothing */
353 pdebug("re%02x s\n", ret);
355 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
356 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
358 pdebug("re%02x t\n", ret);
361 pdebug("re%02x\n", ret);
365 s->last_read_offset = addr;
370 parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
372 ParallelState *s = opaque;
374 uint16_t eppdata = ~0;
376 struct ParallelIOArg ioarg = {
377 .buffer = &eppdata, .count = sizeof(eppdata)
379 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
380 /* Controls not correct for EPP data cycle, so do nothing */
381 pdebug("re%04x s\n", eppdata);
384 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
385 ret = le16_to_cpu(eppdata);
389 pdebug("re%04x t\n", ret);
392 pdebug("re%04x\n", ret);
397 parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
399 ParallelState *s = opaque;
401 uint32_t eppdata = ~0U;
403 struct ParallelIOArg ioarg = {
404 .buffer = &eppdata, .count = sizeof(eppdata)
406 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
407 /* Controls not correct for EPP data cycle, so do nothing */
408 pdebug("re%08x s\n", eppdata);
411 err = qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
412 ret = le32_to_cpu(eppdata);
416 pdebug("re%08x t\n", ret);
419 pdebug("re%08x\n", ret);
423 static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
425 pdebug("wecp%d=%02x\n", addr & 7, val);
428 static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
432 pdebug("recp%d:%02x\n", addr & 7, ret);
436 static void parallel_reset(void *opaque)
438 ParallelState *s = opaque;
442 s->status = PARA_STS_BUSY;
443 s->status |= PARA_STS_ACK;
444 s->status |= PARA_STS_ONLINE;
445 s->status |= PARA_STS_ERROR;
446 s->status |= PARA_STS_TMOUT;
447 s->control = PARA_CTR_SELECT;
448 s->control |= PARA_CTR_INIT;
453 s->last_read_offset = ~0U;
456 static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
458 static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
460 .read = parallel_ioport_read_hw,
461 .write = parallel_ioport_write_hw },
463 .read = parallel_ioport_eppdata_read_hw2,
464 .write = parallel_ioport_eppdata_write_hw2 },
466 .read = parallel_ioport_eppdata_read_hw4,
467 .write = parallel_ioport_eppdata_write_hw4 },
469 .read = parallel_ioport_ecp_read,
470 .write = parallel_ioport_ecp_write },
471 PORTIO_END_OF_LIST(),
474 static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
476 .read = parallel_ioport_read_sw,
477 .write = parallel_ioport_write_sw },
478 PORTIO_END_OF_LIST(),
482 static const VMStateDescription vmstate_parallel_isa = {
483 .name = "parallel_isa",
485 .minimum_version_id = 1,
486 .fields = (VMStateField[]) {
487 VMSTATE_UINT8(state.dataw, ISAParallelState),
488 VMSTATE_UINT8(state.datar, ISAParallelState),
489 VMSTATE_UINT8(state.status, ISAParallelState),
490 VMSTATE_UINT8(state.control, ISAParallelState),
491 VMSTATE_INT32(state.irq_pending, ISAParallelState),
492 VMSTATE_INT32(state.epp_timeout, ISAParallelState),
493 VMSTATE_END_OF_LIST()
498 static void parallel_isa_realizefn(DeviceState *dev, Error **errp)
501 ISADevice *isadev = ISA_DEVICE(dev);
502 ISAParallelState *isa = ISA_PARALLEL(dev);
503 ParallelState *s = &isa->state;
508 error_setg(errp, "Can't create parallel device, empty char device");
512 if (isa->index == -1) {
515 if (isa->index >= MAX_PARALLEL_PORTS) {
516 error_setg(errp, "Max. supported number of parallel ports is %d.",
520 if (isa->iobase == -1) {
521 isa->iobase = isa_parallel_io[isa->index];
526 isa_init_irq(isadev, &s->irq, isa->isairq);
527 qemu_register_reset(parallel_reset, s);
529 if (qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
534 isa_register_portio_list(isadev, base,
536 ? &isa_parallel_portio_hw_list[0]
537 : &isa_parallel_portio_sw_list[0]),
541 /* Memory mapped interface */
542 static uint32_t parallel_mm_readb (void *opaque, hwaddr addr)
544 ParallelState *s = opaque;
546 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFF;
549 static void parallel_mm_writeb (void *opaque,
550 hwaddr addr, uint32_t value)
552 ParallelState *s = opaque;
554 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFF);
557 static uint32_t parallel_mm_readw (void *opaque, hwaddr addr)
559 ParallelState *s = opaque;
561 return parallel_ioport_read_sw(s, addr >> s->it_shift) & 0xFFFF;
564 static void parallel_mm_writew (void *opaque,
565 hwaddr addr, uint32_t value)
567 ParallelState *s = opaque;
569 parallel_ioport_write_sw(s, addr >> s->it_shift, value & 0xFFFF);
572 static uint32_t parallel_mm_readl (void *opaque, hwaddr addr)
574 ParallelState *s = opaque;
576 return parallel_ioport_read_sw(s, addr >> s->it_shift);
579 static void parallel_mm_writel (void *opaque,
580 hwaddr addr, uint32_t value)
582 ParallelState *s = opaque;
584 parallel_ioport_write_sw(s, addr >> s->it_shift, value);
587 static const MemoryRegionOps parallel_mm_ops = {
589 .read = { parallel_mm_readb, parallel_mm_readw, parallel_mm_readl },
590 .write = { parallel_mm_writeb, parallel_mm_writew, parallel_mm_writel },
592 .endianness = DEVICE_NATIVE_ENDIAN,
595 /* If fd is zero, it means that the parallel device uses the console */
596 bool parallel_mm_init(MemoryRegion *address_space,
597 hwaddr base, int it_shift, qemu_irq irq,
598 CharDriverState *chr)
602 s = g_malloc0(sizeof(ParallelState));
605 s->it_shift = it_shift;
606 qemu_register_reset(parallel_reset, s);
608 memory_region_init_io(&s->iomem, NULL, ¶llel_mm_ops, s,
609 "parallel", 8 << it_shift);
610 memory_region_add_subregion(address_space, base, &s->iomem);
614 static Property parallel_isa_properties[] = {
615 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
616 DEFINE_PROP_UINT32("iobase", ISAParallelState, iobase, -1),
617 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
618 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
619 DEFINE_PROP_END_OF_LIST(),
622 static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
624 DeviceClass *dc = DEVICE_CLASS(klass);
626 dc->realize = parallel_isa_realizefn;
627 dc->vmsd = &vmstate_parallel_isa;
628 dc->props = parallel_isa_properties;
629 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
632 static const TypeInfo parallel_isa_info = {
633 .name = TYPE_ISA_PARALLEL,
634 .parent = TYPE_ISA_DEVICE,
635 .instance_size = sizeof(ISAParallelState),
636 .class_init = parallel_isa_class_initfn,
639 static void parallel_register_types(void)
641 type_register_static(¶llel_isa_info);
644 type_init(parallel_register_types)