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"
30 #include "chardev/char-parallel.h"
31 #include "chardev/char-fe.h"
32 #include "hw/isa/isa.h"
33 #include "hw/char/parallel.h"
34 #include "sysemu/sysemu.h"
37 //#define DEBUG_PARALLEL
40 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
42 #define pdebug(fmt, ...) ((void)0)
45 #define PARA_REG_DATA 0
46 #define PARA_REG_STS 1
47 #define PARA_REG_CTR 2
48 #define PARA_REG_EPP_ADDR 3
49 #define PARA_REG_EPP_DATA 4
52 * These are the definitions for the Printer Status Register
54 #define PARA_STS_BUSY 0x80 /* Busy complement */
55 #define PARA_STS_ACK 0x40 /* Acknowledge */
56 #define PARA_STS_PAPER 0x20 /* Out of paper */
57 #define PARA_STS_ONLINE 0x10 /* Online */
58 #define PARA_STS_ERROR 0x08 /* Error complement */
59 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
62 * These are the definitions for the Printer Control Register
64 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
65 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
66 #define PARA_CTR_SELECT 0x08 /* Select In complement */
67 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
68 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
69 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
71 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
73 typedef struct ParallelState
{
84 uint32_t last_read_offset
; /* For debugging */
85 /* Memory-mapped interface */
87 PortioList portio_list
;
90 #define TYPE_ISA_PARALLEL "isa-parallel"
91 #define ISA_PARALLEL(obj) \
92 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
94 typedef struct ISAParallelState
{
103 static void parallel_update_irq(ParallelState
*s
)
106 qemu_irq_raise(s
->irq
);
108 qemu_irq_lower(s
->irq
);
112 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
114 ParallelState
*s
= opaque
;
117 trace_parallel_ioport_write("SW", addr
, val
);
121 parallel_update_irq(s
);
125 if ((val
& PARA_CTR_INIT
) == 0 ) {
126 s
->status
= PARA_STS_BUSY
;
127 s
->status
|= PARA_STS_ACK
;
128 s
->status
|= PARA_STS_ONLINE
;
129 s
->status
|= PARA_STS_ERROR
;
131 else if (val
& PARA_CTR_SELECT
) {
132 if (val
& PARA_CTR_STROBE
) {
133 s
->status
&= ~PARA_STS_BUSY
;
134 if ((s
->control
& PARA_CTR_STROBE
) == 0)
135 /* XXX this blocks entire thread. Rewrite to use
136 * qemu_chr_fe_write and background I/O callbacks */
137 qemu_chr_fe_write_all(&s
->chr
, &s
->dataw
, 1);
139 if (s
->control
& PARA_CTR_INTEN
) {
144 parallel_update_irq(s
);
150 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
152 ParallelState
*s
= opaque
;
156 /* Sometimes programs do several writes for timing purposes on old
157 HW. Take care not to waste time on writes that do nothing. */
159 s
->last_read_offset
= ~0U;
162 trace_parallel_ioport_write("HW", addr
, val
);
167 pdebug("wd%02x\n", val
);
168 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
172 pdebug("ws%02x\n", val
);
173 if (val
& PARA_STS_TMOUT
)
178 if (s
->control
== val
)
180 pdebug("wc%02x\n", val
);
182 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
183 if (val
& PARA_CTR_DIR
) {
188 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
189 parm
&= ~PARA_CTR_DIR
;
192 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
195 case PARA_REG_EPP_ADDR
:
196 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
197 /* Controls not correct for EPP address cycle, so do nothing */
198 pdebug("wa%02x s\n", val
);
200 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
201 if (qemu_chr_fe_ioctl(&s
->chr
,
202 CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
204 pdebug("wa%02x t\n", val
);
207 pdebug("wa%02x\n", val
);
210 case PARA_REG_EPP_DATA
:
211 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
212 /* Controls not correct for EPP data cycle, so do nothing */
213 pdebug("we%02x s\n", val
);
215 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
216 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
218 pdebug("we%02x t\n", val
);
221 pdebug("we%02x\n", val
);
228 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
230 ParallelState
*s
= opaque
;
231 uint16_t eppdata
= cpu_to_le16(val
);
233 struct ParallelIOArg ioarg
= {
234 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
237 trace_parallel_ioport_write("EPP", addr
, val
);
238 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
239 /* Controls not correct for EPP data cycle, so do nothing */
240 pdebug("we%04x s\n", val
);
243 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
246 pdebug("we%04x t\n", val
);
249 pdebug("we%04x\n", val
);
253 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
255 ParallelState
*s
= opaque
;
256 uint32_t eppdata
= cpu_to_le32(val
);
258 struct ParallelIOArg ioarg
= {
259 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
262 trace_parallel_ioport_write("EPP", addr
, val
);
263 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
264 /* Controls not correct for EPP data cycle, so do nothing */
265 pdebug("we%08x s\n", val
);
268 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
271 pdebug("we%08x t\n", val
);
274 pdebug("we%08x\n", val
);
277 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
279 ParallelState
*s
= opaque
;
285 if (s
->control
& PARA_CTR_DIR
)
293 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
294 /* XXX Fixme: wait 5 microseconds */
295 if (s
->status
& PARA_STS_ACK
)
296 s
->status
&= ~PARA_STS_ACK
;
298 /* XXX Fixme: wait 5 microseconds */
299 s
->status
|= PARA_STS_ACK
;
300 s
->status
|= PARA_STS_BUSY
;
303 parallel_update_irq(s
);
309 trace_parallel_ioport_read("SW", addr
, ret
);
313 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
315 ParallelState
*s
= opaque
;
320 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
321 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
322 pdebug("rd%02x\n", ret
);
326 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
327 ret
&= ~PARA_STS_TMOUT
;
329 ret
|= PARA_STS_TMOUT
;
330 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
331 pdebug("rs%02x\n", ret
);
335 /* s->control has some bits fixed to 1. It is zero only when
336 it has not been yet written to. */
337 if (s
->control
== 0) {
338 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
339 if (s
->last_read_offset
!= addr
)
340 pdebug("rc%02x\n", ret
);
345 if (s
->last_read_offset
!= addr
)
346 pdebug("rc%02x\n", ret
);
349 case PARA_REG_EPP_ADDR
:
350 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
351 (PARA_CTR_DIR
| PARA_CTR_INIT
))
352 /* Controls not correct for EPP addr cycle, so do nothing */
353 pdebug("ra%02x s\n", ret
);
355 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
356 if (qemu_chr_fe_ioctl(&s
->chr
,
357 CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
359 pdebug("ra%02x t\n", ret
);
362 pdebug("ra%02x\n", ret
);
365 case PARA_REG_EPP_DATA
:
366 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
367 (PARA_CTR_DIR
| PARA_CTR_INIT
))
368 /* Controls not correct for EPP data cycle, so do nothing */
369 pdebug("re%02x s\n", ret
);
371 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
372 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
374 pdebug("re%02x t\n", ret
);
377 pdebug("re%02x\n", ret
);
381 trace_parallel_ioport_read("HW", addr
, ret
);
382 s
->last_read_offset
= addr
;
387 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
389 ParallelState
*s
= opaque
;
391 uint16_t eppdata
= ~0;
393 struct ParallelIOArg ioarg
= {
394 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
396 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
397 /* Controls not correct for EPP data cycle, so do nothing */
398 pdebug("re%04x s\n", eppdata
);
401 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
402 ret
= le16_to_cpu(eppdata
);
406 pdebug("re%04x t\n", ret
);
409 pdebug("re%04x\n", ret
);
410 trace_parallel_ioport_read("EPP", addr
, ret
);
415 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
417 ParallelState
*s
= opaque
;
419 uint32_t eppdata
= ~0U;
421 struct ParallelIOArg ioarg
= {
422 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
424 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
425 /* Controls not correct for EPP data cycle, so do nothing */
426 pdebug("re%08x s\n", eppdata
);
429 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
430 ret
= le32_to_cpu(eppdata
);
434 pdebug("re%08x t\n", ret
);
437 pdebug("re%08x\n", ret
);
438 trace_parallel_ioport_read("EPP", addr
, ret
);
442 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
444 trace_parallel_ioport_write("ECP", addr
& 7, val
);
445 pdebug("wecp%d=%02x\n", addr
& 7, val
);
448 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
452 trace_parallel_ioport_read("ECP", addr
& 7, ret
);
453 pdebug("recp%d:%02x\n", addr
& 7, ret
);
457 static void parallel_reset(void *opaque
)
459 ParallelState
*s
= opaque
;
463 s
->status
= PARA_STS_BUSY
;
464 s
->status
|= PARA_STS_ACK
;
465 s
->status
|= PARA_STS_ONLINE
;
466 s
->status
|= PARA_STS_ERROR
;
467 s
->status
|= PARA_STS_TMOUT
;
468 s
->control
= PARA_CTR_SELECT
;
469 s
->control
|= PARA_CTR_INIT
;
474 s
->last_read_offset
= ~0U;
477 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
479 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
481 .read
= parallel_ioport_read_hw
,
482 .write
= parallel_ioport_write_hw
},
484 .read
= parallel_ioport_eppdata_read_hw2
,
485 .write
= parallel_ioport_eppdata_write_hw2
},
487 .read
= parallel_ioport_eppdata_read_hw4
,
488 .write
= parallel_ioport_eppdata_write_hw4
},
490 .read
= parallel_ioport_ecp_read
,
491 .write
= parallel_ioport_ecp_write
},
492 PORTIO_END_OF_LIST(),
495 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
497 .read
= parallel_ioport_read_sw
,
498 .write
= parallel_ioport_write_sw
},
499 PORTIO_END_OF_LIST(),
503 static const VMStateDescription vmstate_parallel_isa
= {
504 .name
= "parallel_isa",
506 .minimum_version_id
= 1,
507 .fields
= (VMStateField
[]) {
508 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
509 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
510 VMSTATE_UINT8(state
.status
, ISAParallelState
),
511 VMSTATE_UINT8(state
.control
, ISAParallelState
),
512 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
513 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
514 VMSTATE_END_OF_LIST()
518 static int parallel_can_receive(void *opaque
)
523 static void parallel_isa_realizefn(DeviceState
*dev
, Error
**errp
)
526 ISADevice
*isadev
= ISA_DEVICE(dev
);
527 ISAParallelState
*isa
= ISA_PARALLEL(dev
);
528 ParallelState
*s
= &isa
->state
;
532 if (!qemu_chr_fe_backend_connected(&s
->chr
)) {
533 error_setg(errp
, "Can't create parallel device, empty char device");
537 if (isa
->index
== -1) {
540 if (isa
->index
>= MAX_PARALLEL_PORTS
) {
541 error_setg(errp
, "Max. supported number of parallel ports is %d.",
545 if (isa
->iobase
== -1) {
546 isa
->iobase
= isa_parallel_io
[isa
->index
];
551 isa_init_irq(isadev
, &s
->irq
, isa
->isairq
);
552 qemu_register_reset(parallel_reset
, s
);
554 qemu_chr_fe_set_handlers(&s
->chr
, parallel_can_receive
, NULL
,
555 NULL
, NULL
, s
, NULL
, true);
556 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
561 isa_register_portio_list(isadev
, &s
->portio_list
, base
,
563 ? &isa_parallel_portio_hw_list
[0]
564 : &isa_parallel_portio_sw_list
[0]),
568 /* Memory mapped interface */
569 static uint64_t parallel_mm_readfn(void *opaque
, hwaddr addr
, unsigned size
)
571 ParallelState
*s
= opaque
;
573 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) &
574 MAKE_64BIT_MASK(0, size
* 8);
577 static void parallel_mm_writefn(void *opaque
, hwaddr addr
,
578 uint64_t value
, unsigned size
)
580 ParallelState
*s
= opaque
;
582 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
,
583 value
& MAKE_64BIT_MASK(0, size
* 8));
586 static const MemoryRegionOps parallel_mm_ops
= {
587 .read
= parallel_mm_readfn
,
588 .write
= parallel_mm_writefn
,
589 .valid
.min_access_size
= 1,
590 .valid
.max_access_size
= 4,
591 .endianness
= DEVICE_NATIVE_ENDIAN
,
594 /* If fd is zero, it means that the parallel device uses the console */
595 bool parallel_mm_init(MemoryRegion
*address_space
,
596 hwaddr base
, int it_shift
, qemu_irq irq
,
601 s
= g_malloc0(sizeof(ParallelState
));
603 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
604 s
->it_shift
= it_shift
;
605 qemu_register_reset(parallel_reset
, s
);
607 memory_region_init_io(&s
->iomem
, NULL
, ¶llel_mm_ops
, s
,
608 "parallel", 8 << it_shift
);
609 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
613 static Property parallel_isa_properties
[] = {
614 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
615 DEFINE_PROP_UINT32("iobase", ISAParallelState
, iobase
, -1),
616 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
617 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
618 DEFINE_PROP_END_OF_LIST(),
621 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
623 DeviceClass
*dc
= DEVICE_CLASS(klass
);
625 dc
->realize
= parallel_isa_realizefn
;
626 dc
->vmsd
= &vmstate_parallel_isa
;
627 dc
->props
= parallel_isa_properties
;
628 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
631 static const TypeInfo parallel_isa_info
= {
632 .name
= TYPE_ISA_PARALLEL
,
633 .parent
= TYPE_ISA_DEVICE
,
634 .instance_size
= sizeof(ISAParallelState
),
635 .class_init
= parallel_isa_class_initfn
,
638 static void parallel_register_types(void)
640 type_register_static(¶llel_isa_info
);
643 type_init(parallel_register_types
)