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/acpi_aml_interface.h"
33 #include "hw/isa/isa.h"
34 #include "hw/qdev-properties.h"
35 #include "hw/qdev-properties-system.h"
36 #include "migration/vmstate.h"
37 #include "hw/char/parallel.h"
38 #include "sysemu/reset.h"
39 #include "sysemu/sysemu.h"
41 #include "qom/object.h"
43 //#define DEBUG_PARALLEL
46 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
48 #define pdebug(fmt, ...) ((void)0)
51 #define PARA_REG_DATA 0
52 #define PARA_REG_STS 1
53 #define PARA_REG_CTR 2
54 #define PARA_REG_EPP_ADDR 3
55 #define PARA_REG_EPP_DATA 4
58 * These are the definitions for the Printer Status Register
60 #define PARA_STS_BUSY 0x80 /* Busy complement */
61 #define PARA_STS_ACK 0x40 /* Acknowledge */
62 #define PARA_STS_PAPER 0x20 /* Out of paper */
63 #define PARA_STS_ONLINE 0x10 /* Online */
64 #define PARA_STS_ERROR 0x08 /* Error complement */
65 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
68 * These are the definitions for the Printer Control Register
70 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
71 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
72 #define PARA_CTR_SELECT 0x08 /* Select In complement */
73 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
74 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
75 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
77 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
79 typedef struct ParallelState
{
90 uint32_t last_read_offset
; /* For debugging */
91 /* Memory-mapped interface */
93 PortioList portio_list
;
96 #define TYPE_ISA_PARALLEL "isa-parallel"
97 OBJECT_DECLARE_SIMPLE_TYPE(ISAParallelState
, ISA_PARALLEL
)
99 struct ISAParallelState
{
100 ISADevice parent_obj
;
108 static void parallel_update_irq(ParallelState
*s
)
111 qemu_irq_raise(s
->irq
);
113 qemu_irq_lower(s
->irq
);
117 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
119 ParallelState
*s
= opaque
;
122 trace_parallel_ioport_write("SW", addr
, val
);
126 parallel_update_irq(s
);
130 if ((val
& PARA_CTR_INIT
) == 0 ) {
131 s
->status
= PARA_STS_BUSY
;
132 s
->status
|= PARA_STS_ACK
;
133 s
->status
|= PARA_STS_ONLINE
;
134 s
->status
|= PARA_STS_ERROR
;
136 else if (val
& PARA_CTR_SELECT
) {
137 if (val
& PARA_CTR_STROBE
) {
138 s
->status
&= ~PARA_STS_BUSY
;
139 if ((s
->control
& PARA_CTR_STROBE
) == 0)
140 /* XXX this blocks entire thread. Rewrite to use
141 * qemu_chr_fe_write and background I/O callbacks */
142 qemu_chr_fe_write_all(&s
->chr
, &s
->dataw
, 1);
144 if (s
->control
& PARA_CTR_INTEN
) {
149 parallel_update_irq(s
);
155 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
157 ParallelState
*s
= opaque
;
161 /* Sometimes programs do several writes for timing purposes on old
162 HW. Take care not to waste time on writes that do nothing. */
164 s
->last_read_offset
= ~0U;
167 trace_parallel_ioport_write("HW", addr
, val
);
172 pdebug("wd%02x\n", val
);
173 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
177 pdebug("ws%02x\n", val
);
178 if (val
& PARA_STS_TMOUT
)
183 if (s
->control
== val
)
185 pdebug("wc%02x\n", val
);
187 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
188 if (val
& PARA_CTR_DIR
) {
193 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
194 parm
&= ~PARA_CTR_DIR
;
197 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
200 case PARA_REG_EPP_ADDR
:
201 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
202 /* Controls not correct for EPP address cycle, so do nothing */
203 pdebug("wa%02x s\n", val
);
205 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
206 if (qemu_chr_fe_ioctl(&s
->chr
,
207 CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
209 pdebug("wa%02x t\n", val
);
212 pdebug("wa%02x\n", val
);
215 case PARA_REG_EPP_DATA
:
216 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
217 /* Controls not correct for EPP data cycle, so do nothing */
218 pdebug("we%02x s\n", val
);
220 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
221 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
223 pdebug("we%02x t\n", val
);
226 pdebug("we%02x\n", val
);
233 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
235 ParallelState
*s
= opaque
;
236 uint16_t eppdata
= cpu_to_le16(val
);
238 struct ParallelIOArg ioarg
= {
239 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
242 trace_parallel_ioport_write("EPP", addr
, val
);
243 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
244 /* Controls not correct for EPP data cycle, so do nothing */
245 pdebug("we%04x s\n", val
);
248 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
251 pdebug("we%04x t\n", val
);
254 pdebug("we%04x\n", val
);
258 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
260 ParallelState
*s
= opaque
;
261 uint32_t eppdata
= cpu_to_le32(val
);
263 struct ParallelIOArg ioarg
= {
264 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
267 trace_parallel_ioport_write("EPP", addr
, val
);
268 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
269 /* Controls not correct for EPP data cycle, so do nothing */
270 pdebug("we%08x s\n", val
);
273 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
276 pdebug("we%08x t\n", val
);
279 pdebug("we%08x\n", val
);
282 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
284 ParallelState
*s
= opaque
;
290 if (s
->control
& PARA_CTR_DIR
)
298 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
299 /* XXX Fixme: wait 5 microseconds */
300 if (s
->status
& PARA_STS_ACK
)
301 s
->status
&= ~PARA_STS_ACK
;
303 /* XXX Fixme: wait 5 microseconds */
304 s
->status
|= PARA_STS_ACK
;
305 s
->status
|= PARA_STS_BUSY
;
308 parallel_update_irq(s
);
314 trace_parallel_ioport_read("SW", addr
, ret
);
318 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
320 ParallelState
*s
= opaque
;
325 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
326 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
327 pdebug("rd%02x\n", ret
);
331 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
332 ret
&= ~PARA_STS_TMOUT
;
334 ret
|= PARA_STS_TMOUT
;
335 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
336 pdebug("rs%02x\n", ret
);
340 /* s->control has some bits fixed to 1. It is zero only when
341 it has not been yet written to. */
342 if (s
->control
== 0) {
343 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
344 if (s
->last_read_offset
!= addr
)
345 pdebug("rc%02x\n", ret
);
350 if (s
->last_read_offset
!= addr
)
351 pdebug("rc%02x\n", ret
);
354 case PARA_REG_EPP_ADDR
:
355 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
356 (PARA_CTR_DIR
| PARA_CTR_INIT
))
357 /* Controls not correct for EPP addr cycle, so do nothing */
358 pdebug("ra%02x s\n", ret
);
360 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
361 if (qemu_chr_fe_ioctl(&s
->chr
,
362 CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
364 pdebug("ra%02x t\n", ret
);
367 pdebug("ra%02x\n", ret
);
370 case PARA_REG_EPP_DATA
:
371 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
372 (PARA_CTR_DIR
| PARA_CTR_INIT
))
373 /* Controls not correct for EPP data cycle, so do nothing */
374 pdebug("re%02x s\n", ret
);
376 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
377 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
379 pdebug("re%02x t\n", ret
);
382 pdebug("re%02x\n", ret
);
386 trace_parallel_ioport_read("HW", addr
, ret
);
387 s
->last_read_offset
= addr
;
392 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
394 ParallelState
*s
= opaque
;
396 uint16_t eppdata
= ~0;
398 struct ParallelIOArg ioarg
= {
399 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
401 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
402 /* Controls not correct for EPP data cycle, so do nothing */
403 pdebug("re%04x s\n", eppdata
);
406 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
407 ret
= le16_to_cpu(eppdata
);
411 pdebug("re%04x t\n", ret
);
414 pdebug("re%04x\n", ret
);
415 trace_parallel_ioport_read("EPP", addr
, ret
);
420 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
422 ParallelState
*s
= opaque
;
424 uint32_t eppdata
= ~0U;
426 struct ParallelIOArg ioarg
= {
427 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
429 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
430 /* Controls not correct for EPP data cycle, so do nothing */
431 pdebug("re%08x s\n", eppdata
);
434 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
435 ret
= le32_to_cpu(eppdata
);
439 pdebug("re%08x t\n", ret
);
442 pdebug("re%08x\n", ret
);
443 trace_parallel_ioport_read("EPP", addr
, ret
);
447 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
449 trace_parallel_ioport_write("ECP", addr
& 7, val
);
450 pdebug("wecp%d=%02x\n", addr
& 7, val
);
453 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
457 trace_parallel_ioport_read("ECP", addr
& 7, ret
);
458 pdebug("recp%d:%02x\n", addr
& 7, ret
);
462 static void parallel_reset(void *opaque
)
464 ParallelState
*s
= opaque
;
468 s
->status
= PARA_STS_BUSY
;
469 s
->status
|= PARA_STS_ACK
;
470 s
->status
|= PARA_STS_ONLINE
;
471 s
->status
|= PARA_STS_ERROR
;
472 s
->status
|= PARA_STS_TMOUT
;
473 s
->control
= PARA_CTR_SELECT
;
474 s
->control
|= PARA_CTR_INIT
;
479 s
->last_read_offset
= ~0U;
482 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
484 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
486 .read
= parallel_ioport_read_hw
,
487 .write
= parallel_ioport_write_hw
},
489 .read
= parallel_ioport_eppdata_read_hw2
,
490 .write
= parallel_ioport_eppdata_write_hw2
},
492 .read
= parallel_ioport_eppdata_read_hw4
,
493 .write
= parallel_ioport_eppdata_write_hw4
},
495 .read
= parallel_ioport_ecp_read
,
496 .write
= parallel_ioport_ecp_write
},
497 PORTIO_END_OF_LIST(),
500 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
502 .read
= parallel_ioport_read_sw
,
503 .write
= parallel_ioport_write_sw
},
504 PORTIO_END_OF_LIST(),
508 static const VMStateDescription vmstate_parallel_isa
= {
509 .name
= "parallel_isa",
511 .minimum_version_id
= 1,
512 .fields
= (VMStateField
[]) {
513 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
514 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
515 VMSTATE_UINT8(state
.status
, ISAParallelState
),
516 VMSTATE_UINT8(state
.control
, ISAParallelState
),
517 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
518 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
519 VMSTATE_END_OF_LIST()
523 static int parallel_can_receive(void *opaque
)
528 static void parallel_isa_realizefn(DeviceState
*dev
, Error
**errp
)
531 ISADevice
*isadev
= ISA_DEVICE(dev
);
532 ISAParallelState
*isa
= ISA_PARALLEL(dev
);
533 ParallelState
*s
= &isa
->state
;
537 if (!qemu_chr_fe_backend_connected(&s
->chr
)) {
538 error_setg(errp
, "Can't create parallel device, empty char device");
542 if (isa
->index
== -1) {
545 if (isa
->index
>= MAX_PARALLEL_PORTS
) {
546 error_setg(errp
, "Max. supported number of parallel ports is %d.",
550 if (isa
->iobase
== -1) {
551 isa
->iobase
= isa_parallel_io
[isa
->index
];
556 s
->irq
= isa_get_irq(isadev
, isa
->isairq
);
557 qemu_register_reset(parallel_reset
, s
);
559 qemu_chr_fe_set_handlers(&s
->chr
, parallel_can_receive
, NULL
,
560 NULL
, NULL
, s
, NULL
, true);
561 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
566 isa_register_portio_list(isadev
, &s
->portio_list
, base
,
568 ? &isa_parallel_portio_hw_list
[0]
569 : &isa_parallel_portio_sw_list
[0]),
573 static void parallel_isa_build_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
575 ISAParallelState
*isa
= ISA_PARALLEL(adev
);
579 crs
= aml_resource_template();
580 aml_append(crs
, aml_io(AML_DECODE16
, isa
->iobase
, isa
->iobase
, 0x08, 0x08));
581 aml_append(crs
, aml_irq_no_flags(isa
->isairq
));
583 dev
= aml_device("LPT%d", isa
->index
+ 1);
584 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0400")));
585 aml_append(dev
, aml_name_decl("_UID", aml_int(isa
->index
+ 1)));
586 aml_append(dev
, aml_name_decl("_STA", aml_int(0xf)));
587 aml_append(dev
, aml_name_decl("_CRS", crs
));
589 aml_append(scope
, dev
);
592 /* Memory mapped interface */
593 static uint64_t parallel_mm_readfn(void *opaque
, hwaddr addr
, unsigned size
)
595 ParallelState
*s
= opaque
;
597 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) &
598 MAKE_64BIT_MASK(0, size
* 8);
601 static void parallel_mm_writefn(void *opaque
, hwaddr addr
,
602 uint64_t value
, unsigned size
)
604 ParallelState
*s
= opaque
;
606 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
,
607 value
& MAKE_64BIT_MASK(0, size
* 8));
610 static const MemoryRegionOps parallel_mm_ops
= {
611 .read
= parallel_mm_readfn
,
612 .write
= parallel_mm_writefn
,
613 .valid
.min_access_size
= 1,
614 .valid
.max_access_size
= 4,
615 .endianness
= DEVICE_NATIVE_ENDIAN
,
618 /* If fd is zero, it means that the parallel device uses the console */
619 bool parallel_mm_init(MemoryRegion
*address_space
,
620 hwaddr base
, int it_shift
, qemu_irq irq
,
625 s
= g_new0(ParallelState
, 1);
627 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
628 s
->it_shift
= it_shift
;
629 qemu_register_reset(parallel_reset
, s
);
631 memory_region_init_io(&s
->iomem
, NULL
, ¶llel_mm_ops
, s
,
632 "parallel", 8 << it_shift
);
633 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
637 static Property parallel_isa_properties
[] = {
638 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
639 DEFINE_PROP_UINT32("iobase", ISAParallelState
, iobase
, -1),
640 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
641 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
642 DEFINE_PROP_END_OF_LIST(),
645 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
647 DeviceClass
*dc
= DEVICE_CLASS(klass
);
648 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
650 dc
->realize
= parallel_isa_realizefn
;
651 dc
->vmsd
= &vmstate_parallel_isa
;
652 adevc
->build_dev_aml
= parallel_isa_build_aml
;
653 device_class_set_props(dc
, parallel_isa_properties
);
654 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
657 static const TypeInfo parallel_isa_info
= {
658 .name
= TYPE_ISA_PARALLEL
,
659 .parent
= TYPE_ISA_DEVICE
,
660 .instance_size
= sizeof(ISAParallelState
),
661 .class_init
= parallel_isa_class_initfn
,
662 .interfaces
= (InterfaceInfo
[]) {
663 { TYPE_ACPI_DEV_AML_IF
},
668 static void parallel_register_types(void)
670 type_register_static(¶llel_isa_info
);
673 type_init(parallel_register_types
)