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 "sysemu/char.h"
27 #include "hw/isa/isa.h"
28 #include "hw/i386/pc.h"
29 #include "sysemu/sysemu.h"
31 //#define DEBUG_PARALLEL
34 #define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
36 #define pdebug(fmt, ...) ((void)0)
39 #define PARA_REG_DATA 0
40 #define PARA_REG_STS 1
41 #define PARA_REG_CTR 2
42 #define PARA_REG_EPP_ADDR 3
43 #define PARA_REG_EPP_DATA 4
46 * These are the definitions for the Printer Status Register
48 #define PARA_STS_BUSY 0x80 /* Busy complement */
49 #define PARA_STS_ACK 0x40 /* Acknowledge */
50 #define PARA_STS_PAPER 0x20 /* Out of paper */
51 #define PARA_STS_ONLINE 0x10 /* Online */
52 #define PARA_STS_ERROR 0x08 /* Error complement */
53 #define PARA_STS_TMOUT 0x01 /* EPP timeout */
56 * These are the definitions for the Printer Control Register
58 #define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
59 #define PARA_CTR_INTEN 0x10 /* IRQ Enable */
60 #define PARA_CTR_SELECT 0x08 /* Select In complement */
61 #define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
62 #define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
63 #define PARA_CTR_STROBE 0x01 /* Strobe complement */
65 #define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
67 typedef struct ParallelState
{
78 uint32_t last_read_offset
; /* For debugging */
79 /* Memory-mapped interface */
83 #define TYPE_ISA_PARALLEL "isa-parallel"
84 #define ISA_PARALLEL(obj) \
85 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
87 typedef struct ISAParallelState
{
96 static void parallel_update_irq(ParallelState
*s
)
99 qemu_irq_raise(s
->irq
);
101 qemu_irq_lower(s
->irq
);
105 parallel_ioport_write_sw(void *opaque
, uint32_t addr
, uint32_t val
)
107 ParallelState
*s
= opaque
;
109 pdebug("write addr=0x%02x val=0x%02x\n", addr
, val
);
115 parallel_update_irq(s
);
119 if ((val
& PARA_CTR_INIT
) == 0 ) {
120 s
->status
= PARA_STS_BUSY
;
121 s
->status
|= PARA_STS_ACK
;
122 s
->status
|= PARA_STS_ONLINE
;
123 s
->status
|= PARA_STS_ERROR
;
125 else if (val
& PARA_CTR_SELECT
) {
126 if (val
& PARA_CTR_STROBE
) {
127 s
->status
&= ~PARA_STS_BUSY
;
128 if ((s
->control
& PARA_CTR_STROBE
) == 0)
129 qemu_chr_fe_write(s
->chr
, &s
->dataw
, 1);
131 if (s
->control
& PARA_CTR_INTEN
) {
136 parallel_update_irq(s
);
142 static void parallel_ioport_write_hw(void *opaque
, uint32_t addr
, uint32_t val
)
144 ParallelState
*s
= opaque
;
148 /* Sometimes programs do several writes for timing purposes on old
149 HW. Take care not to waste time on writes that do nothing. */
151 s
->last_read_offset
= ~0U;
158 pdebug("wd%02x\n", val
);
159 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_DATA
, &parm
);
163 pdebug("ws%02x\n", val
);
164 if (val
& PARA_STS_TMOUT
)
169 if (s
->control
== val
)
171 pdebug("wc%02x\n", val
);
173 if ((val
& PARA_CTR_DIR
) != (s
->control
& PARA_CTR_DIR
)) {
174 if (val
& PARA_CTR_DIR
) {
179 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_DATA_DIR
, &dir
);
180 parm
&= ~PARA_CTR_DIR
;
183 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_WRITE_CONTROL
, &parm
);
186 case PARA_REG_EPP_ADDR
:
187 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
188 /* Controls not correct for EPP address cycle, so do nothing */
189 pdebug("wa%02x s\n", val
);
191 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
192 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE_ADDR
, &ioarg
)) {
194 pdebug("wa%02x t\n", val
);
197 pdebug("wa%02x\n", val
);
200 case PARA_REG_EPP_DATA
:
201 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
)
202 /* Controls not correct for EPP data cycle, so do nothing */
203 pdebug("we%02x s\n", val
);
205 struct ParallelIOArg ioarg
= { .buffer
= &parm
, .count
= 1 };
206 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
)) {
208 pdebug("we%02x t\n", val
);
211 pdebug("we%02x\n", val
);
218 parallel_ioport_eppdata_write_hw2(void *opaque
, uint32_t addr
, uint32_t val
)
220 ParallelState
*s
= opaque
;
221 uint16_t eppdata
= cpu_to_le16(val
);
223 struct ParallelIOArg ioarg
= {
224 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
226 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
227 /* Controls not correct for EPP data cycle, so do nothing */
228 pdebug("we%04x s\n", val
);
231 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
234 pdebug("we%04x t\n", val
);
237 pdebug("we%04x\n", val
);
241 parallel_ioport_eppdata_write_hw4(void *opaque
, uint32_t addr
, uint32_t val
)
243 ParallelState
*s
= opaque
;
244 uint32_t eppdata
= cpu_to_le32(val
);
246 struct ParallelIOArg ioarg
= {
247 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
249 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != PARA_CTR_INIT
) {
250 /* Controls not correct for EPP data cycle, so do nothing */
251 pdebug("we%08x s\n", val
);
254 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_WRITE
, &ioarg
);
257 pdebug("we%08x t\n", val
);
260 pdebug("we%08x\n", val
);
263 static uint32_t parallel_ioport_read_sw(void *opaque
, uint32_t addr
)
265 ParallelState
*s
= opaque
;
271 if (s
->control
& PARA_CTR_DIR
)
279 if ((s
->status
& PARA_STS_BUSY
) == 0 && (s
->control
& PARA_CTR_STROBE
) == 0) {
280 /* XXX Fixme: wait 5 microseconds */
281 if (s
->status
& PARA_STS_ACK
)
282 s
->status
&= ~PARA_STS_ACK
;
284 /* XXX Fixme: wait 5 microseconds */
285 s
->status
|= PARA_STS_ACK
;
286 s
->status
|= PARA_STS_BUSY
;
289 parallel_update_irq(s
);
295 pdebug("read addr=0x%02x val=0x%02x\n", addr
, ret
);
299 static uint32_t parallel_ioport_read_hw(void *opaque
, uint32_t addr
)
301 ParallelState
*s
= opaque
;
306 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_DATA
, &ret
);
307 if (s
->last_read_offset
!= addr
|| s
->datar
!= ret
)
308 pdebug("rd%02x\n", ret
);
312 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &ret
);
313 ret
&= ~PARA_STS_TMOUT
;
315 ret
|= PARA_STS_TMOUT
;
316 if (s
->last_read_offset
!= addr
|| s
->status
!= ret
)
317 pdebug("rs%02x\n", ret
);
321 /* s->control has some bits fixed to 1. It is zero only when
322 it has not been yet written to. */
323 if (s
->control
== 0) {
324 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_CONTROL
, &ret
);
325 if (s
->last_read_offset
!= addr
)
326 pdebug("rc%02x\n", ret
);
331 if (s
->last_read_offset
!= addr
)
332 pdebug("rc%02x\n", ret
);
335 case PARA_REG_EPP_ADDR
:
336 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
337 /* Controls not correct for EPP addr cycle, so do nothing */
338 pdebug("ra%02x s\n", ret
);
340 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
341 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ_ADDR
, &ioarg
)) {
343 pdebug("ra%02x t\n", ret
);
346 pdebug("ra%02x\n", ret
);
349 case PARA_REG_EPP_DATA
:
350 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
))
351 /* Controls not correct for EPP data cycle, so do nothing */
352 pdebug("re%02x s\n", ret
);
354 struct ParallelIOArg ioarg
= { .buffer
= &ret
, .count
= 1 };
355 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
)) {
357 pdebug("re%02x t\n", ret
);
360 pdebug("re%02x\n", ret
);
364 s
->last_read_offset
= addr
;
369 parallel_ioport_eppdata_read_hw2(void *opaque
, uint32_t addr
)
371 ParallelState
*s
= opaque
;
373 uint16_t eppdata
= ~0;
375 struct ParallelIOArg ioarg
= {
376 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
378 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
379 /* Controls not correct for EPP data cycle, so do nothing */
380 pdebug("re%04x s\n", eppdata
);
383 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
384 ret
= le16_to_cpu(eppdata
);
388 pdebug("re%04x t\n", ret
);
391 pdebug("re%04x\n", ret
);
396 parallel_ioport_eppdata_read_hw4(void *opaque
, uint32_t addr
)
398 ParallelState
*s
= opaque
;
400 uint32_t eppdata
= ~0U;
402 struct ParallelIOArg ioarg
= {
403 .buffer
= &eppdata
, .count
= sizeof(eppdata
)
405 if ((s
->control
& (PARA_CTR_DIR
|PARA_CTR_SIGNAL
)) != (PARA_CTR_DIR
|PARA_CTR_INIT
)) {
406 /* Controls not correct for EPP data cycle, so do nothing */
407 pdebug("re%08x s\n", eppdata
);
410 err
= qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_EPP_READ
, &ioarg
);
411 ret
= le32_to_cpu(eppdata
);
415 pdebug("re%08x t\n", ret
);
418 pdebug("re%08x\n", ret
);
422 static void parallel_ioport_ecp_write(void *opaque
, uint32_t addr
, uint32_t val
)
424 pdebug("wecp%d=%02x\n", addr
& 7, val
);
427 static uint32_t parallel_ioport_ecp_read(void *opaque
, uint32_t addr
)
431 pdebug("recp%d:%02x\n", addr
& 7, ret
);
435 static void parallel_reset(void *opaque
)
437 ParallelState
*s
= opaque
;
441 s
->status
= PARA_STS_BUSY
;
442 s
->status
|= PARA_STS_ACK
;
443 s
->status
|= PARA_STS_ONLINE
;
444 s
->status
|= PARA_STS_ERROR
;
445 s
->status
|= PARA_STS_TMOUT
;
446 s
->control
= PARA_CTR_SELECT
;
447 s
->control
|= PARA_CTR_INIT
;
452 s
->last_read_offset
= ~0U;
455 static const int isa_parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
457 static const MemoryRegionPortio isa_parallel_portio_hw_list
[] = {
459 .read
= parallel_ioport_read_hw
,
460 .write
= parallel_ioport_write_hw
},
462 .read
= parallel_ioport_eppdata_read_hw2
,
463 .write
= parallel_ioport_eppdata_write_hw2
},
465 .read
= parallel_ioport_eppdata_read_hw4
,
466 .write
= parallel_ioport_eppdata_write_hw4
},
468 .read
= parallel_ioport_ecp_read
,
469 .write
= parallel_ioport_ecp_write
},
470 PORTIO_END_OF_LIST(),
473 static const MemoryRegionPortio isa_parallel_portio_sw_list
[] = {
475 .read
= parallel_ioport_read_sw
,
476 .write
= parallel_ioport_write_sw
},
477 PORTIO_END_OF_LIST(),
481 static const VMStateDescription vmstate_parallel_isa
= {
482 .name
= "parallel_isa",
484 .minimum_version_id
= 1,
485 .fields
= (VMStateField
[]) {
486 VMSTATE_UINT8(state
.dataw
, ISAParallelState
),
487 VMSTATE_UINT8(state
.datar
, ISAParallelState
),
488 VMSTATE_UINT8(state
.status
, ISAParallelState
),
489 VMSTATE_UINT8(state
.control
, ISAParallelState
),
490 VMSTATE_INT32(state
.irq_pending
, ISAParallelState
),
491 VMSTATE_INT32(state
.epp_timeout
, ISAParallelState
),
492 VMSTATE_END_OF_LIST()
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
;
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 isa_init_irq(isadev
, &s
->irq
, isa
->isairq
);
526 qemu_register_reset(parallel_reset
, s
);
528 if (qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_PP_READ_STATUS
, &dummy
) == 0) {
533 isa_register_portio_list(isadev
, base
,
535 ? &isa_parallel_portio_hw_list
[0]
536 : &isa_parallel_portio_sw_list
[0]),
540 /* Memory mapped interface */
541 static uint32_t parallel_mm_readb (void *opaque
, hwaddr addr
)
543 ParallelState
*s
= opaque
;
545 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFF;
548 static void parallel_mm_writeb (void *opaque
,
549 hwaddr addr
, uint32_t value
)
551 ParallelState
*s
= opaque
;
553 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFF);
556 static uint32_t parallel_mm_readw (void *opaque
, hwaddr addr
)
558 ParallelState
*s
= opaque
;
560 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
) & 0xFFFF;
563 static void parallel_mm_writew (void *opaque
,
564 hwaddr addr
, uint32_t value
)
566 ParallelState
*s
= opaque
;
568 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
571 static uint32_t parallel_mm_readl (void *opaque
, hwaddr addr
)
573 ParallelState
*s
= opaque
;
575 return parallel_ioport_read_sw(s
, addr
>> s
->it_shift
);
578 static void parallel_mm_writel (void *opaque
,
579 hwaddr addr
, uint32_t value
)
581 ParallelState
*s
= opaque
;
583 parallel_ioport_write_sw(s
, addr
>> s
->it_shift
, value
);
586 static const MemoryRegionOps parallel_mm_ops
= {
588 .read
= { parallel_mm_readb
, parallel_mm_readw
, parallel_mm_readl
},
589 .write
= { parallel_mm_writeb
, parallel_mm_writew
, parallel_mm_writel
},
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
,
597 CharDriverState
*chr
)
601 s
= g_malloc0(sizeof(ParallelState
));
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
)