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"
26 #include "qapi/error.h"
28 #include "chardev/char-parallel.h"
29 #include "chardev/char-fe.h"
30 #include "hw/isa/isa.h"
31 #include "hw/i386/pc.h"
32 #include "sysemu/sysemu.h"
34 //#define DEBUG_PARALLEL
37 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
39 #define pdebug(fmt, ...) ((void)0)
42 #define PARA_REG_DATA 0
43 #define PARA_REG_STS 1
44 #define PARA_REG_CTR 2
45 #define PARA_REG_EPP_ADDR 3
46 #define PARA_REG_EPP_DATA 4
49 * These are the definitions for the Printer Status Register
51 #define PARA_STS_BUSY 0x80 /* Busy complement */
52 #define PARA_STS_ACK 0x40 /* Acknowledge */
53 #define PARA_STS_PAPER 0x20 /* Out of paper */
54 #define PARA_STS_ONLINE 0x10 /* Online */
55 #define PARA_STS_ERROR 0x08 /* Error complement */
56 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
59 * These are the definitions for the Printer Control Register
61 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
62 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
63 #define PARA_CTR_SELECT 0x08 /* Select In complement */
64 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
65 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
66 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
68 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
70 typedef struct ParallelState
{
81 uint32_t last_read_offset
; /* For debugging */
82 /* Memory-mapped interface */
84 PortioList portio_list
;
87 #define TYPE_ISA_PARALLEL "isa-parallel"
88 #define ISA_PARALLEL(obj) \
89 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
91 typedef struct ISAParallelState
{
100 static void parallel_update_irq(ParallelState
*s
)
103 qemu_irq_raise(s
->irq
);
105 qemu_irq_lower(s
->irq
);
109 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
111 ParallelState
*s
= opaque
;
113 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
119 parallel_update_irq(s
);
123 if ((val
& PARA_CTR_INIT
) == 0 ) {
124 s
->status
= PARA_STS_BUSY
;
125 s
->status
|= PARA_STS_ACK
;
126 s
->status
|= PARA_STS_ONLINE
;
127 s
->status
|= PARA_STS_ERROR
;
129 else if (val
& PARA_CTR_SELECT
) {
130 if (val
& PARA_CTR_STROBE
) {
131 s
->status
&= ~PARA_STS_BUSY
;
132 if ((s
->control
& PARA_CTR_STROBE
) == 0)
133 /* XXX this blocks entire thread. Rewrite to use
134 * qemu_chr_fe_write and background I/O callbacks */
135 qemu_chr_fe_write_all(&s
->chr
, &s
->dataw
, 1);
137 if (s
->control
& PARA_CTR_INTEN
) {
142 parallel_update_irq(s
);
148 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
150 ParallelState
*s
= opaque
;
154 /* Sometimes programs do several writes for timing purposes on old
155 HW. Take care not to waste time on writes that do nothing. */
157 s
->last_read_offset
= ~0U;
164 pdebug("wd%02x\n", val
);
165 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
169 pdebug("ws%02x\n", val
);
170 if (val
& PARA_STS_TMOUT
)
175 if (s
->control
== val
)
177 pdebug("wc%02x\n", val
);
179 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
180 if (val
& PARA_CTR_DIR
) {
185 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
186 parm
&= ~PARA_CTR_DIR
;
189 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
192 case PARA_REG_EPP_ADDR
:
193 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
194 /* Controls not correct for EPP address cycle, so do nothing */
195 pdebug("wa%02x s\n", val
);
197 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
198 if (qemu_chr_fe_ioctl(&s
->chr
,
199 CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
201 pdebug("wa%02x t\n", val
);
204 pdebug("wa%02x\n", val
);
207 case PARA_REG_EPP_DATA
:
208 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
209 /* Controls not correct for EPP data cycle, so do nothing */
210 pdebug("we%02x s\n", val
);
212 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
213 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
215 pdebug("we%02x t\n", val
);
218 pdebug("we%02x\n", val
);
225 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
227 ParallelState
*s
= opaque
;
228 uint16_t eppdata
= cpu_to_le16(val
);
230 struct ParallelIOArg ioarg
= {
231 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
233 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
234 /* Controls not correct for EPP data cycle, so do nothing */
235 pdebug("we%04x s\n", val
);
238 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
241 pdebug("we%04x t\n", val
);
244 pdebug("we%04x\n", val
);
248 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
250 ParallelState
*s
= opaque
;
251 uint32_t eppdata
= cpu_to_le32(val
);
253 struct ParallelIOArg ioarg
= {
254 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
256 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
257 /* Controls not correct for EPP data cycle, so do nothing */
258 pdebug("we%08x s\n", val
);
261 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
264 pdebug("we%08x t\n", val
);
267 pdebug("we%08x\n", val
);
270 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
272 ParallelState
*s
= opaque
;
278 if (s
->control
& PARA_CTR_DIR
)
286 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
287 /* XXX Fixme: wait 5 microseconds */
288 if (s
->status
& PARA_STS_ACK
)
289 s
->status
&= ~PARA_STS_ACK
;
291 /* XXX Fixme: wait 5 microseconds */
292 s
->status
|= PARA_STS_ACK
;
293 s
->status
|= PARA_STS_BUSY
;
296 parallel_update_irq(s
);
302 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
306 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
308 ParallelState
*s
= opaque
;
313 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
314 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
315 pdebug("rd%02x\n", ret
);
319 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
320 ret
&= ~PARA_STS_TMOUT
;
322 ret
|= PARA_STS_TMOUT
;
323 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
324 pdebug("rs%02x\n", ret
);
328 /* s->control has some bits fixed to 1. It is zero only when
329 it has not been yet written to. */
330 if (s
->control
== 0) {
331 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
332 if (s
->last_read_offset
!= addr
)
333 pdebug("rc%02x\n", ret
);
338 if (s
->last_read_offset
!= addr
)
339 pdebug("rc%02x\n", ret
);
342 case PARA_REG_EPP_ADDR
:
343 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
344 (PARA_CTR_DIR
| PARA_CTR_INIT
))
345 /* Controls not correct for EPP addr cycle, so do nothing */
346 pdebug("ra%02x s\n", ret
);
348 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
349 if (qemu_chr_fe_ioctl(&s
->chr
,
350 CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
352 pdebug("ra%02x t\n", ret
);
355 pdebug("ra%02x\n", ret
);
358 case PARA_REG_EPP_DATA
:
359 if ((s
->control
& (PARA_CTR_DIR
| PARA_CTR_SIGNAL
)) !=
360 (PARA_CTR_DIR
| PARA_CTR_INIT
))
361 /* Controls not correct for EPP data cycle, so do nothing */
362 pdebug("re%02x s\n", ret
);
364 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
365 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
367 pdebug("re%02x t\n", ret
);
370 pdebug("re%02x\n", ret
);
374 s
->last_read_offset
= addr
;
379 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
381 ParallelState
*s
= opaque
;
383 uint16_t eppdata
= ~0;
385 struct ParallelIOArg ioarg
= {
386 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
388 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
389 /* Controls not correct for EPP data cycle, so do nothing */
390 pdebug("re%04x s\n", eppdata
);
393 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
394 ret
= le16_to_cpu(eppdata
);
398 pdebug("re%04x t\n", ret
);
401 pdebug("re%04x\n", ret
);
406 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
408 ParallelState
*s
= opaque
;
410 uint32_t eppdata
= ~0U;
412 struct ParallelIOArg ioarg
= {
413 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
415 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
416 /* Controls not correct for EPP data cycle, so do nothing */
417 pdebug("re%08x s\n", eppdata
);
420 err
= qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
421 ret
= le32_to_cpu(eppdata
);
425 pdebug("re%08x t\n", ret
);
428 pdebug("re%08x\n", ret
);
432 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
434 pdebug("wecp%d=%02x\n", addr
& 7, val
);
437 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
441 pdebug("recp%d:%02x\n", addr
& 7, ret
);
445 static void parallel_reset(void *opaque
)
447 ParallelState
*s
= opaque
;
451 s
->status
= PARA_STS_BUSY
;
452 s
->status
|= PARA_STS_ACK
;
453 s
->status
|= PARA_STS_ONLINE
;
454 s
->status
|= PARA_STS_ERROR
;
455 s
->status
|= PARA_STS_TMOUT
;
456 s
->control
= PARA_CTR_SELECT
;
457 s
->control
|= PARA_CTR_INIT
;
462 s
->last_read_offset
= ~0U;
465 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
467 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
469 .read
= parallel_ioport_read_hw
,
470 .write
= parallel_ioport_write_hw
},
472 .read
= parallel_ioport_eppdata_read_hw2
,
473 .write
= parallel_ioport_eppdata_write_hw2
},
475 .read
= parallel_ioport_eppdata_read_hw4
,
476 .write
= parallel_ioport_eppdata_write_hw4
},
478 .read
= parallel_ioport_ecp_read
,
479 .write
= parallel_ioport_ecp_write
},
480 PORTIO_END_OF_LIST(),
483 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
485 .read
= parallel_ioport_read_sw
,
486 .write
= parallel_ioport_write_sw
},
487 PORTIO_END_OF_LIST(),
491 static const VMStateDescription vmstate_parallel_isa
= {
492 .name
= "parallel_isa",
494 .minimum_version_id
= 1,
495 .fields
= (VMStateField
[]) {
496 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
497 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
498 VMSTATE_UINT8(state
.status
, ISAParallelState
),
499 VMSTATE_UINT8(state
.control
, ISAParallelState
),
500 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
501 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
502 VMSTATE_END_OF_LIST()
506 static int parallel_can_receive(void *opaque
)
511 static void parallel_isa_realizefn(DeviceState
*dev
, Error
**errp
)
514 ISADevice
*isadev
= ISA_DEVICE(dev
);
515 ISAParallelState
*isa
= ISA_PARALLEL(dev
);
516 ParallelState
*s
= &isa
->state
;
520 if (!qemu_chr_fe_backend_connected(&s
->chr
)) {
521 error_setg(errp
, "Can't create parallel device, empty char device");
525 if (isa
->index
== -1) {
528 if (isa
->index
>= MAX_PARALLEL_PORTS
) {
529 error_setg(errp
, "Max. supported number of parallel ports is %d.",
533 if (isa
->iobase
== -1) {
534 isa
->iobase
= isa_parallel_io
[isa
->index
];
539 isa_init_irq(isadev
, &s
->irq
, isa
->isairq
);
540 qemu_register_reset(parallel_reset
, s
);
542 qemu_chr_fe_set_handlers(&s
->chr
, parallel_can_receive
, NULL
,
543 NULL
, NULL
, s
, NULL
, true);
544 if (qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
549 isa_register_portio_list(isadev
, &s
->portio_list
, base
,
551 ? &isa_parallel_portio_hw_list
[0]
552 : &isa_parallel_portio_sw_list
[0]),
556 /* Memory mapped interface */
557 static uint32_t parallel_mm_readb (void *opaque
, hwaddr addr
)
559 ParallelState
*s
= opaque
;
561 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFF;
564 static void parallel_mm_writeb (void *opaque
,
565 hwaddr addr
, uint32_t value
)
567 ParallelState
*s
= opaque
;
569 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFF);
572 static uint32_t parallel_mm_readw (void *opaque
, hwaddr addr
)
574 ParallelState
*s
= opaque
;
576 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFFFF;
579 static void parallel_mm_writew (void *opaque
,
580 hwaddr addr
, uint32_t value
)
582 ParallelState
*s
= opaque
;
584 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
587 static uint32_t parallel_mm_readl (void *opaque
, hwaddr addr
)
589 ParallelState
*s
= opaque
;
591 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
);
594 static void parallel_mm_writel (void *opaque
,
595 hwaddr addr
, uint32_t value
)
597 ParallelState
*s
= opaque
;
599 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
);
602 static const MemoryRegionOps parallel_mm_ops
= {
604 .read
= { parallel_mm_readb
, parallel_mm_readw
, parallel_mm_readl
},
605 .write
= { parallel_mm_writeb
, parallel_mm_writew
, parallel_mm_writel
},
607 .endianness
= DEVICE_NATIVE_ENDIAN
,
610 /* If fd is zero, it means that the parallel device uses the console */
611 bool parallel_mm_init(MemoryRegion
*address_space
,
612 hwaddr base
, int it_shift
, qemu_irq irq
,
617 s
= g_malloc0(sizeof(ParallelState
));
619 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
620 s
->it_shift
= it_shift
;
621 qemu_register_reset(parallel_reset
, s
);
623 memory_region_init_io(&s
->iomem
, NULL
, ¶llel_mm_ops
, s
,
624 "parallel", 8 << it_shift
);
625 memory_region_add_subregion(address_space
, base
, &s
->iomem
);
629 static Property parallel_isa_properties
[] = {
630 DEFINE_PROP_UINT32("index", ISAParallelState
, index
, -1),
631 DEFINE_PROP_UINT32("iobase", ISAParallelState
, iobase
, -1),
632 DEFINE_PROP_UINT32("irq", ISAParallelState
, isairq
, 7),
633 DEFINE_PROP_CHR("chardev", ISAParallelState
, state
.chr
),
634 DEFINE_PROP_END_OF_LIST(),
637 static void parallel_isa_class_initfn(ObjectClass
*klass
, void *data
)
639 DeviceClass
*dc
= DEVICE_CLASS(klass
);
641 dc
->realize
= parallel_isa_realizefn
;
642 dc
->vmsd
= &vmstate_parallel_isa
;
643 dc
->props
= parallel_isa_properties
;
644 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
647 static const TypeInfo parallel_isa_info
= {
648 .name
= TYPE_ISA_PARALLEL
,
649 .parent
= TYPE_ISA_DEVICE
,
650 .instance_size
= sizeof(ISAParallelState
),
651 .class_init
= parallel_isa_class_initfn
,
654 static void parallel_register_types(void)
656 type_register_static(¶llel_isa_info
);
659 type_init(parallel_register_types
)