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"
31 #include "hw/acpi/aml-build.h"
33 #include "hw/isa/isa.h"
34 #include "hw/qdev-properties.h"
35 #include "migration/vmstate.h"
36 #include "hw/char/parallel.h"
37 #include "sysemu/reset.h"
38 #include "sysemu/sysemu.h"
41 //#define DEBUG_PARALLEL
44 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
46 #define pdebug(fmt, ...) ((void)0)
49 #define PARA_REG_DATA 0
50 #define PARA_REG_STS 1
51 #define PARA_REG_CTR 2
52 #define PARA_REG_EPP_ADDR 3
53 #define PARA_REG_EPP_DATA 4
56 * These are the definitions for the Printer Status Register
58 #define PARA_STS_BUSY 0x80 /* Busy complement */
59 #define PARA_STS_ACK 0x40 /* Acknowledge */
60 #define PARA_STS_PAPER 0x20 /* Out of paper */
61 #define PARA_STS_ONLINE 0x10 /* Online */
62 #define PARA_STS_ERROR 0x08 /* Error complement */
63 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
66 * These are the definitions for the Printer Control Register
68 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
69 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
70 #define PARA_CTR_SELECT 0x08 /* Select In complement */
71 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
72 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
73 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
75 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
77 typedef struct ParallelState
{
88 uint32_t last_read_offset
; /* For debugging */
89 /* Memory-mapped interface */
91 PortioList portio_list
;
94 #define TYPE_ISA_PARALLEL "isa-parallel"
95 #define ISA_PARALLEL(obj) \
96 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
98 typedef struct ISAParallelState
{
107 static void parallel_update_irq(ParallelState
*s
)
110 qemu_irq_raise(s
->irq
);
112 qemu_irq_lower(s
->irq
);
116 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
118 ParallelState
*s
= opaque
;
121 trace_parallel_ioport_write("SW", addr
, val
);
125 parallel_update_irq(s
);
129 if ((val
& PARA_CTR_INIT
) == 0 ) {
130 s
->status
= PARA_STS_BUSY
;
131 s
->status
|= PARA_STS_ACK
;
132 s
->status
|= PARA_STS_ONLINE
;
133 s
->status
|= PARA_STS_ERROR
;
135 else if (val
& PARA_CTR_SELECT
) {
136 if (val
& PARA_CTR_STROBE
) {
137 s
->status
&= ~PARA_STS_BUSY
;
138 if ((s
->control
& PARA_CTR_STROBE
) == 0)
139 /* XXX this blocks entire thread. Rewrite to use
140 * qemu_chr_fe_write and background I/O callbacks */
141 qemu_chr_fe_write_all(&s
->chr
, &s
->dataw
, 1);
143 if (s
->control
& PARA_CTR_INTEN
) {
148 parallel_update_irq(s
);
154 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
156 ParallelState
*s
= opaque
;
160 /* Sometimes programs do several writes for timing purposes on old
161 HW. Take care not to waste time on writes that do nothing. */
163 s
->last_read_offset
= ~0U;
166 trace_parallel_ioport_write("HW", addr
, val
);
171 pdebug("wd%02x\n", val
);
172 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
176 pdebug("ws%02x\n", val
);
177 if (val
& PARA_STS_TMOUT
)
182 if (s
->control
== val
)
184 pdebug("wc%02x\n", val
);
186 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
187 if (val
& PARA_CTR_DIR
) {
192 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
193 parm
&= ~PARA_CTR_DIR
;
196 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
199 case PARA_REG_EPP_ADDR
:
200 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
201 /* Controls not correct for EPP address cycle, so do nothing */
202 pdebug("wa%02x s\n", val
);
204 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
205 if (qemu_chr_fe_ioctl(&s
->chr
,
206 CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
208 pdebug("wa%02x t\n", val
);
211 pdebug("wa%02x\n", val
);
214 case PARA_REG_EPP_DATA
:
215 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
216 /* Controls not correct for EPP data cycle, so do nothing */
217 pdebug("we%02x s\n", val
);
219 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
220 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
222 pdebug("we%02x t\n", val
);
225 pdebug("we%02x\n", val
);
232 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
234 ParallelState
*s
= opaque
;
235 uint16_t eppdata
= cpu_to_le16(val
);
237 struct ParallelIOArg ioarg
= {
238 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
241 trace_parallel_ioport_write("EPP", addr
, val
);
242 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
243 /* Controls not correct for EPP data cycle, so do nothing */
244 pdebug("we%04x s\n", val
);
247 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
250 pdebug("we%04x t\n", val
);
253 pdebug("we%04x\n", val
);
257 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
259 ParallelState
*s
= opaque
;
260 uint32_t eppdata
= cpu_to_le32(val
);
262 struct ParallelIOArg ioarg
= {
263 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
266 trace_parallel_ioport_write("EPP", addr
, val
);
267 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
268 /* Controls not correct for EPP data cycle, so do nothing */
269 pdebug("we%08x s\n", val
);
272 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
275 pdebug("we%08x t\n", val
);
278 pdebug("we%08x\n", val
);
281 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
283 ParallelState
*s
= opaque
;
289 if (s
->control
& PARA_CTR_DIR
)
297 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
298 /* XXX Fixme: wait 5 microseconds */
299 if (s
->status
& PARA_STS_ACK
)
300 s
->status
&= ~PARA_STS_ACK
;
302 /* XXX Fixme: wait 5 microseconds */
303 s
->status
|= PARA_STS_ACK
;
304 s
->status
|= PARA_STS_BUSY
;
307 parallel_update_irq(s
);
313 trace_parallel_ioport_read("SW", addr
, ret
);
317 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
319 ParallelState
*s
= opaque
;
324 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
325 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
326 pdebug("rd%02x\n", ret
);
330 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
331 ret
&= ~PARA_STS_TMOUT
;
333 ret
|= PARA_STS_TMOUT
;
334 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
335 pdebug("rs%02x\n", ret
);
339 /* s->control has some bits fixed to 1. It is zero only when
340 it has not been yet written to. */
341 if (s
->control
== 0) {
342 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
343 if (s
->last_read_offset
!= addr
)
344 pdebug("rc%02x\n", ret
);
349 if (s
->last_read_offset
!= addr
)
350 pdebug("rc%02x\n", ret
);
353 case PARA_REG_EPP_ADDR
:
354 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
355 (PARA_CTR_DIR
| PARA_CTR_INIT
))
356 /* Controls not correct for EPP addr cycle, so do nothing */
357 pdebug("ra%02x s\n", ret
);
359 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
360 if (qemu_chr_fe_ioctl(&s
->chr
,
361 CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
363 pdebug("ra%02x t\n", ret
);
366 pdebug("ra%02x\n", ret
);
369 case PARA_REG_EPP_DATA
:
370 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
371 (PARA_CTR_DIR
| PARA_CTR_INIT
))
372 /* Controls not correct for EPP data cycle, so do nothing */
373 pdebug("re%02x s\n", ret
);
375 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
376 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
378 pdebug("re%02x t\n", ret
);
381 pdebug("re%02x\n", ret
);
385 trace_parallel_ioport_read("HW", addr
, ret
);
386 s
->last_read_offset
= addr
;
391 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
393 ParallelState
*s
= opaque
;
395 uint16_t eppdata
= ~0;
397 struct ParallelIOArg ioarg
= {
398 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
400 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
401 /* Controls not correct for EPP data cycle, so do nothing */
402 pdebug("re%04x s\n", eppdata
);
405 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
406 ret
= le16_to_cpu(eppdata
);
410 pdebug("re%04x t\n", ret
);
413 pdebug("re%04x\n", ret
);
414 trace_parallel_ioport_read("EPP", addr
, ret
);
419 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
421 ParallelState
*s
= opaque
;
423 uint32_t eppdata
= ~0U;
425 struct ParallelIOArg ioarg
= {
426 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
428 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
429 /* Controls not correct for EPP data cycle, so do nothing */
430 pdebug("re%08x s\n", eppdata
);
433 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
434 ret
= le32_to_cpu(eppdata
);
438 pdebug("re%08x t\n", ret
);
441 pdebug("re%08x\n", ret
);
442 trace_parallel_ioport_read("EPP", addr
, ret
);
446 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
448 trace_parallel_ioport_write("ECP", addr
& 7, val
);
449 pdebug("wecp%d=%02x\n", addr
& 7, val
);
452 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
456 trace_parallel_ioport_read("ECP", addr
& 7, ret
);
457 pdebug("recp%d:%02x\n", addr
& 7, ret
);
461 static void parallel_reset(void *opaque
)
463 ParallelState
*s
= opaque
;
467 s
->status
= PARA_STS_BUSY
;
468 s
->status
|= PARA_STS_ACK
;
469 s
->status
|= PARA_STS_ONLINE
;
470 s
->status
|= PARA_STS_ERROR
;
471 s
->status
|= PARA_STS_TMOUT
;
472 s
->control
= PARA_CTR_SELECT
;
473 s
->control
|= PARA_CTR_INIT
;
478 s
->last_read_offset
= ~0U;
481 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
483 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
485 .read
= parallel_ioport_read_hw
,
486 .write
= parallel_ioport_write_hw
},
488 .read
= parallel_ioport_eppdata_read_hw2
,
489 .write
= parallel_ioport_eppdata_write_hw2
},
491 .read
= parallel_ioport_eppdata_read_hw4
,
492 .write
= parallel_ioport_eppdata_write_hw4
},
494 .read
= parallel_ioport_ecp_read
,
495 .write
= parallel_ioport_ecp_write
},
496 PORTIO_END_OF_LIST(),
499 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
501 .read
= parallel_ioport_read_sw
,
502 .write
= parallel_ioport_write_sw
},
503 PORTIO_END_OF_LIST(),
507 static const VMStateDescription vmstate_parallel_isa
= {
508 .name
= "parallel_isa",
510 .minimum_version_id
= 1,
511 .fields
= (VMStateField
[]) {
512 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
513 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
514 VMSTATE_UINT8(state
.status
, ISAParallelState
),
515 VMSTATE_UINT8(state
.control
, ISAParallelState
),
516 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
517 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
518 VMSTATE_END_OF_LIST()
522 static int parallel_can_receive(void *opaque
)
527 static void parallel_isa_realizefn(DeviceState
*dev
, Error
**errp
)
530 ISADevice
*isadev
= ISA_DEVICE(dev
);
531 ISAParallelState
*isa
= ISA_PARALLEL(dev
);
532 ParallelState
*s
= &isa
->state
;
536 if (!qemu_chr_fe_backend_connected(&s
->chr
)) {
537 error_setg(errp
, "Can't create parallel device, empty char device");
541 if (isa
->index
== -1) {
544 if (isa
->index
>= MAX_PARALLEL_PORTS
) {
545 error_setg(errp
, "Max. supported number of parallel ports is %d.",
549 if (isa
->iobase
== -1) {
550 isa
->iobase
= isa_parallel_io
[isa
->index
];
555 isa_init_irq(isadev
, &s
->irq
, isa
->isairq
);
556 qemu_register_reset(parallel_reset
, s
);
558 qemu_chr_fe_set_handlers(&s
->chr
, parallel_can_receive
, NULL
,
559 NULL
, NULL
, s
, NULL
, true);
560 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
565 isa_register_portio_list(isadev
, &s
->portio_list
, base
,
567 ? &isa_parallel_portio_hw_list
[0]
568 : &isa_parallel_portio_sw_list
[0]),
572 static void parallel_isa_build_aml(ISADevice
*isadev
, Aml
*scope
)
574 ISAParallelState
*isa
= ISA_PARALLEL(isadev
);
578 crs
= aml_resource_template();
579 aml_append(crs
, aml_io(AML_DECODE16
, isa
->iobase
, isa
->iobase
, 0x08, 0x08));
580 aml_append(crs
, aml_irq_no_flags(isa
->isairq
));
582 dev
= aml_device("LPT%d", isa
->index
+ 1);
583 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0400")));
584 aml_append(dev
, aml_name_decl("_UID", aml_int(isa
->index
+ 1)));
585 aml_append(dev
, aml_name_decl("_STA", aml_int(0xf)));
586 aml_append(dev
, aml_name_decl("_CRS", crs
));
588 aml_append(scope
, dev
);
591 /* Memory mapped interface */
592 static uint64_t parallel_mm_readfn(void *opaque
, hwaddr addr
, unsigned size
)
594 ParallelState
*s
= opaque
;
596 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) &
597 MAKE_64BIT_MASK(0, size
* 8);
600 static void parallel_mm_writefn(void *opaque
, hwaddr addr
,
601 uint64_t value
, unsigned size
)
603 ParallelState
*s
= opaque
;
605 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
,
606 value
& MAKE_64BIT_MASK(0, size
* 8));
609 static const MemoryRegionOps parallel_mm_ops
= {
610 .read
= parallel_mm_readfn
,
611 .write
= parallel_mm_writefn
,
612 .valid
.min_access_size
= 1,
613 .valid
.max_access_size
= 4,
614 .endianness
= DEVICE_NATIVE_ENDIAN
,
617 /* If fd is zero, it means that the parallel device uses the console */
618 bool parallel_mm_init(MemoryRegion
*address_space
,
619 hwaddr base
, int it_shift
, qemu_irq irq
,
624 s
= g_malloc0(sizeof(ParallelState
));
626 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
627 s
->it_shift
= it_shift
;
628 qemu_register_reset(parallel_reset
, s
);
630 memory_region_init_io(&s
->iomem
, NULL
, ¶llel_mm_ops
, s
,
631 "parallel", 8 << it_shift
);
632 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
636 static Property parallel_isa_properties
[] = {
637 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
638 DEFINE_PROP_UINT32("iobase", ISAParallelState
, iobase
, -1),
639 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
640 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
641 DEFINE_PROP_END_OF_LIST(),
644 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
646 DeviceClass
*dc
= DEVICE_CLASS(klass
);
647 ISADeviceClass
*isa
= ISA_DEVICE_CLASS(klass
);
649 dc
->realize
= parallel_isa_realizefn
;
650 dc
->vmsd
= &vmstate_parallel_isa
;
651 isa
->build_aml
= parallel_isa_build_aml
;
652 device_class_set_props(dc
, parallel_isa_properties
);
653 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
656 static const TypeInfo parallel_isa_info
= {
657 .name
= TYPE_ISA_PARALLEL
,
658 .parent
= TYPE_ISA_DEVICE
,
659 .instance_size
= sizeof(ISAParallelState
),
660 .class_init
= parallel_isa_class_initfn
,
663 static void parallel_register_types(void)
665 type_register_static(¶llel_isa_info
);
668 type_init(parallel_register_types
)