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 "hw/acpi/acpi_aml_interface.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/qdev-properties-system.h"
33 #include "migration/vmstate.h"
34 #include "hw/char/parallel-isa.h"
35 #include "hw/char/parallel.h"
36 #include "sysemu/reset.h"
37 #include "sysemu/sysemu.h"
39 #include "qom/object.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 static void parallel_update_irq(ParallelState
*s
)
80 qemu_irq_raise(s
->irq
);
82 qemu_irq_lower(s
->irq
);
86 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
88 ParallelState
*s
= opaque
;
91 trace_parallel_ioport_write("SW", addr
, val
);
95 parallel_update_irq(s
);
99 if ((val
& PARA_CTR_INIT
) == 0 ) {
100 s
->status
= PARA_STS_BUSY
;
101 s
->status
|= PARA_STS_ACK
;
102 s
->status
|= PARA_STS_ONLINE
;
103 s
->status
|= PARA_STS_ERROR
;
105 else if (val
& PARA_CTR_SELECT
) {
106 if (val
& PARA_CTR_STROBE
) {
107 s
->status
&= ~PARA_STS_BUSY
;
108 if ((s
->control
& PARA_CTR_STROBE
) == 0)
109 /* XXX this blocks entire thread. Rewrite to use
110 * qemu_chr_fe_write and background I/O callbacks */
111 qemu_chr_fe_write_all(&s
->chr
, &s
->dataw
, 1);
113 if (s
->control
& PARA_CTR_INTEN
) {
118 parallel_update_irq(s
);
124 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
126 ParallelState
*s
= opaque
;
130 /* Sometimes programs do several writes for timing purposes on old
131 HW. Take care not to waste time on writes that do nothing. */
133 s
->last_read_offset
= ~0U;
136 trace_parallel_ioport_write("HW", addr
, val
);
141 pdebug("wd%02x\n", val
);
142 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
146 pdebug("ws%02x\n", val
);
147 if (val
& PARA_STS_TMOUT
)
152 if (s
->control
== val
)
154 pdebug("wc%02x\n", val
);
156 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
157 if (val
& PARA_CTR_DIR
) {
162 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
163 parm
&= ~PARA_CTR_DIR
;
166 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
169 case PARA_REG_EPP_ADDR
:
170 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
171 /* Controls not correct for EPP address cycle, so do nothing */
172 pdebug("wa%02x s\n", val
);
174 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
175 if (qemu_chr_fe_ioctl(&s
->chr
,
176 CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
178 pdebug("wa%02x t\n", val
);
181 pdebug("wa%02x\n", val
);
184 case PARA_REG_EPP_DATA
:
185 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
186 /* Controls not correct for EPP data cycle, so do nothing */
187 pdebug("we%02x s\n", val
);
189 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
190 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
192 pdebug("we%02x t\n", val
);
195 pdebug("we%02x\n", val
);
202 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
204 ParallelState
*s
= opaque
;
205 uint16_t eppdata
= cpu_to_le16(val
);
207 struct ParallelIOArg ioarg
= {
208 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
211 trace_parallel_ioport_write("EPP", addr
, val
);
212 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
213 /* Controls not correct for EPP data cycle, so do nothing */
214 pdebug("we%04x s\n", val
);
217 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
220 pdebug("we%04x t\n", val
);
223 pdebug("we%04x\n", val
);
227 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
229 ParallelState
*s
= opaque
;
230 uint32_t eppdata
= cpu_to_le32(val
);
232 struct ParallelIOArg ioarg
= {
233 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
236 trace_parallel_ioport_write("EPP", addr
, val
);
237 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
238 /* Controls not correct for EPP data cycle, so do nothing */
239 pdebug("we%08x s\n", val
);
242 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
245 pdebug("we%08x t\n", val
);
248 pdebug("we%08x\n", val
);
251 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
253 ParallelState
*s
= opaque
;
259 if (s
->control
& PARA_CTR_DIR
)
267 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
268 /* XXX Fixme: wait 5 microseconds */
269 if (s
->status
& PARA_STS_ACK
)
270 s
->status
&= ~PARA_STS_ACK
;
272 /* XXX Fixme: wait 5 microseconds */
273 s
->status
|= PARA_STS_ACK
;
274 s
->status
|= PARA_STS_BUSY
;
277 parallel_update_irq(s
);
283 trace_parallel_ioport_read("SW", addr
, ret
);
287 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
289 ParallelState
*s
= opaque
;
294 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
295 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
296 pdebug("rd%02x\n", ret
);
300 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
301 ret
&= ~PARA_STS_TMOUT
;
303 ret
|= PARA_STS_TMOUT
;
304 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
305 pdebug("rs%02x\n", ret
);
309 /* s->control has some bits fixed to 1. It is zero only when
310 it has not been yet written to. */
311 if (s
->control
== 0) {
312 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
313 if (s
->last_read_offset
!= addr
)
314 pdebug("rc%02x\n", ret
);
319 if (s
->last_read_offset
!= addr
)
320 pdebug("rc%02x\n", ret
);
323 case PARA_REG_EPP_ADDR
:
324 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
325 (PARA_CTR_DIR
| PARA_CTR_INIT
))
326 /* Controls not correct for EPP addr cycle, so do nothing */
327 pdebug("ra%02x s\n", ret
);
329 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
330 if (qemu_chr_fe_ioctl(&s
->chr
,
331 CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
333 pdebug("ra%02x t\n", ret
);
336 pdebug("ra%02x\n", ret
);
339 case PARA_REG_EPP_DATA
:
340 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
341 (PARA_CTR_DIR
| PARA_CTR_INIT
))
342 /* Controls not correct for EPP data cycle, so do nothing */
343 pdebug("re%02x s\n", ret
);
345 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
346 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
348 pdebug("re%02x t\n", ret
);
351 pdebug("re%02x\n", ret
);
355 trace_parallel_ioport_read("HW", addr
, ret
);
356 s
->last_read_offset
= addr
;
361 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
363 ParallelState
*s
= opaque
;
365 uint16_t eppdata
= ~0;
367 struct ParallelIOArg ioarg
= {
368 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
370 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
371 /* Controls not correct for EPP data cycle, so do nothing */
372 pdebug("re%04x s\n", eppdata
);
375 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
376 ret
= le16_to_cpu(eppdata
);
380 pdebug("re%04x t\n", ret
);
383 pdebug("re%04x\n", ret
);
384 trace_parallel_ioport_read("EPP", addr
, ret
);
389 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
391 ParallelState
*s
= opaque
;
393 uint32_t eppdata
= ~0U;
395 struct ParallelIOArg ioarg
= {
396 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
398 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
399 /* Controls not correct for EPP data cycle, so do nothing */
400 pdebug("re%08x s\n", eppdata
);
403 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
404 ret
= le32_to_cpu(eppdata
);
408 pdebug("re%08x t\n", ret
);
411 pdebug("re%08x\n", ret
);
412 trace_parallel_ioport_read("EPP", addr
, ret
);
416 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
418 trace_parallel_ioport_write("ECP", addr
& 7, val
);
419 pdebug("wecp%d=%02x\n", addr
& 7, val
);
422 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
426 trace_parallel_ioport_read("ECP", addr
& 7, ret
);
427 pdebug("recp%d:%02x\n", addr
& 7, ret
);
431 static void parallel_reset(void *opaque
)
433 ParallelState
*s
= opaque
;
437 s
->status
= PARA_STS_BUSY
;
438 s
->status
|= PARA_STS_ACK
;
439 s
->status
|= PARA_STS_ONLINE
;
440 s
->status
|= PARA_STS_ERROR
;
441 s
->status
|= PARA_STS_TMOUT
;
442 s
->control
= PARA_CTR_SELECT
;
443 s
->control
|= PARA_CTR_INIT
;
448 s
->last_read_offset
= ~0U;
451 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
453 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
455 .read
= parallel_ioport_read_hw
,
456 .write
= parallel_ioport_write_hw
},
458 .read
= parallel_ioport_eppdata_read_hw2
,
459 .write
= parallel_ioport_eppdata_write_hw2
},
461 .read
= parallel_ioport_eppdata_read_hw4
,
462 .write
= parallel_ioport_eppdata_write_hw4
},
464 .read
= parallel_ioport_ecp_read
,
465 .write
= parallel_ioport_ecp_write
},
466 PORTIO_END_OF_LIST(),
469 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
471 .read
= parallel_ioport_read_sw
,
472 .write
= parallel_ioport_write_sw
},
473 PORTIO_END_OF_LIST(),
477 static const VMStateDescription vmstate_parallel_isa
= {
478 .name
= "parallel_isa",
480 .minimum_version_id
= 1,
481 .fields
= (const VMStateField
[]) {
482 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
483 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
484 VMSTATE_UINT8(state
.status
, ISAParallelState
),
485 VMSTATE_UINT8(state
.control
, ISAParallelState
),
486 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
487 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
488 VMSTATE_END_OF_LIST()
492 static int parallel_can_receive(void *opaque
)
497 static void parallel_isa_realizefn(DeviceState
*dev
, Error
**errp
)
500 ISADevice
*isadev
= ISA_DEVICE(dev
);
501 ISAParallelState
*isa
= ISA_PARALLEL(dev
);
502 ParallelState
*s
= &isa
->state
;
506 if (!qemu_chr_fe_backend_connected(&s
->chr
)) {
507 error_setg(errp
, "Can't create parallel device, empty char device");
511 if (isa
->index
== -1) {
514 if (isa
->index
>= MAX_PARALLEL_PORTS
) {
515 error_setg(errp
, "Max. supported number of parallel ports is %d.",
519 if (isa
->iobase
== -1) {
520 isa
->iobase
= isa_parallel_io
[isa
->index
];
525 s
->irq
= isa_get_irq(isadev
, isa
->isairq
);
526 qemu_register_reset(parallel_reset
, s
);
528 qemu_chr_fe_set_handlers(&s
->chr
, parallel_can_receive
, NULL
,
529 NULL
, NULL
, s
, NULL
, true);
530 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
535 isa_register_portio_list(isadev
, &s
->portio_list
, base
,
537 ? &isa_parallel_portio_hw_list
[0]
538 : &isa_parallel_portio_sw_list
[0]),
542 static void parallel_isa_build_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
544 ISAParallelState
*isa
= ISA_PARALLEL(adev
);
548 crs
= aml_resource_template();
549 aml_append(crs
, aml_io(AML_DECODE16
, isa
->iobase
, isa
->iobase
, 0x08, 0x08));
550 aml_append(crs
, aml_irq_no_flags(isa
->isairq
));
552 dev
= aml_device("LPT%d", isa
->index
+ 1);
553 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0400")));
554 aml_append(dev
, aml_name_decl("_UID", aml_int(isa
->index
+ 1)));
555 aml_append(dev
, aml_name_decl("_STA", aml_int(0xf)));
556 aml_append(dev
, aml_name_decl("_CRS", crs
));
558 aml_append(scope
, dev
);
561 /* Memory mapped interface */
562 static uint64_t parallel_mm_readfn(void *opaque
, hwaddr addr
, unsigned size
)
564 ParallelState
*s
= opaque
;
566 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) &
567 MAKE_64BIT_MASK(0, size
* 8);
570 static void parallel_mm_writefn(void *opaque
, hwaddr addr
,
571 uint64_t value
, unsigned size
)
573 ParallelState
*s
= opaque
;
575 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
,
576 value
& MAKE_64BIT_MASK(0, size
* 8));
579 static const MemoryRegionOps parallel_mm_ops
= {
580 .read
= parallel_mm_readfn
,
581 .write
= parallel_mm_writefn
,
582 .valid
.min_access_size
= 1,
583 .valid
.max_access_size
= 4,
584 .endianness
= DEVICE_NATIVE_ENDIAN
,
587 /* If fd is zero, it means that the parallel device uses the console */
588 bool parallel_mm_init(MemoryRegion
*address_space
,
589 hwaddr base
, int it_shift
, qemu_irq irq
,
594 s
= g_new0(ParallelState
, 1);
596 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
597 s
->it_shift
= it_shift
;
598 qemu_register_reset(parallel_reset
, s
);
600 memory_region_init_io(&s
->iomem
, NULL
, ¶llel_mm_ops
, s
,
601 "parallel", 8 << it_shift
);
602 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
606 static Property parallel_isa_properties
[] = {
607 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
608 DEFINE_PROP_UINT32("iobase", ISAParallelState
, iobase
, -1),
609 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
610 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
611 DEFINE_PROP_END_OF_LIST(),
614 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
616 DeviceClass
*dc
= DEVICE_CLASS(klass
);
617 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
619 dc
->realize
= parallel_isa_realizefn
;
620 dc
->vmsd
= &vmstate_parallel_isa
;
621 adevc
->build_dev_aml
= parallel_isa_build_aml
;
622 device_class_set_props(dc
, parallel_isa_properties
);
623 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
626 static const TypeInfo parallel_isa_info
= {
627 .name
= TYPE_ISA_PARALLEL
,
628 .parent
= TYPE_ISA_DEVICE
,
629 .instance_size
= sizeof(ISAParallelState
),
630 .class_init
= parallel_isa_class_initfn
,
631 .interfaces
= (InterfaceInfo
[]) {
632 { TYPE_ACPI_DEV_AML_IF
},
637 static void parallel_register_types(void)
639 type_register_static(¶llel_isa_info
);
642 type_init(parallel_register_types
)