]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/parallel.c
qdev: set properties with device_class_set_props()
[mirror_qemu.git] / hw / char / parallel.c
CommitLineData
6508fe59
FB
1/*
2 * QEMU Parallel PORT emulation
5fafdf24 3 *
e57a8c0e 4 * Copyright (c) 2003-2005 Fabrice Bellard
5867c88a 5 * Copyright (c) 2007 Marko Kohtala
5fafdf24 6 *
6508fe59
FB
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
0b8fa32f 25
b6a0aa05 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
0b8fa32f 28#include "qemu/module.h"
7566c6ef 29#include "chardev/char-parallel.h"
4d43a603 30#include "chardev/char-fe.h"
64552b6b 31#include "hw/irq.h"
0d09e41a 32#include "hw/isa/isa.h"
a27bd6c7 33#include "hw/qdev-properties.h"
d6454270 34#include "migration/vmstate.h"
bb3d5ea8 35#include "hw/char/parallel.h"
71e8a915 36#include "sysemu/reset.h"
9c17d615 37#include "sysemu/sysemu.h"
cb2d721c 38#include "trace.h"
6508fe59
FB
39
40//#define DEBUG_PARALLEL
41
5867c88a 42#ifdef DEBUG_PARALLEL
001faf32 43#define pdebug(fmt, ...) printf("pp: " fmt, ## __VA_ARGS__)
5867c88a 44#else
001faf32 45#define pdebug(fmt, ...) ((void)0)
5867c88a
TS
46#endif
47
48#define PARA_REG_DATA 0
49#define PARA_REG_STS 1
50#define PARA_REG_CTR 2
51#define PARA_REG_EPP_ADDR 3
52#define PARA_REG_EPP_DATA 4
53
6508fe59
FB
54/*
55 * These are the definitions for the Printer Status Register
56 */
57#define PARA_STS_BUSY 0x80 /* Busy complement */
58#define PARA_STS_ACK 0x40 /* Acknowledge */
59#define PARA_STS_PAPER 0x20 /* Out of paper */
60#define PARA_STS_ONLINE 0x10 /* Online */
61#define PARA_STS_ERROR 0x08 /* Error complement */
5867c88a 62#define PARA_STS_TMOUT 0x01 /* EPP timeout */
6508fe59
FB
63
64/*
65 * These are the definitions for the Printer Control Register
66 */
5867c88a 67#define PARA_CTR_DIR 0x20 /* Direction (1=read, 0=write) */
6508fe59
FB
68#define PARA_CTR_INTEN 0x10 /* IRQ Enable */
69#define PARA_CTR_SELECT 0x08 /* Select In complement */
70#define PARA_CTR_INIT 0x04 /* Initialize Printer complement */
71#define PARA_CTR_AUTOLF 0x02 /* Auto linefeed complement */
72#define PARA_CTR_STROBE 0x01 /* Strobe complement */
73
5867c88a
TS
74#define PARA_CTR_SIGNAL (PARA_CTR_SELECT|PARA_CTR_INIT|PARA_CTR_AUTOLF|PARA_CTR_STROBE)
75
defdb20e 76typedef struct ParallelState {
63858cd9 77 MemoryRegion iomem;
5867c88a
TS
78 uint8_t dataw;
79 uint8_t datar;
80 uint8_t status;
6508fe59 81 uint8_t control;
d537cf6c 82 qemu_irq irq;
6508fe59 83 int irq_pending;
becdfa00 84 CharBackend chr;
e57a8c0e 85 int hw_driver;
5867c88a
TS
86 int epp_timeout;
87 uint32_t last_read_offset; /* For debugging */
d60532ca 88 /* Memory-mapped interface */
d60532ca 89 int it_shift;
e305a165 90 PortioList portio_list;
defdb20e 91} ParallelState;
6508fe59 92
b0dc5ee6
AF
93#define TYPE_ISA_PARALLEL "isa-parallel"
94#define ISA_PARALLEL(obj) \
95 OBJECT_CHECK(ISAParallelState, (obj), TYPE_ISA_PARALLEL)
96
021f0674 97typedef struct ISAParallelState {
b0dc5ee6
AF
98 ISADevice parent_obj;
99
e8ee28fb 100 uint32_t index;
021f0674
GH
101 uint32_t iobase;
102 uint32_t isairq;
103 ParallelState state;
104} ISAParallelState;
105
6508fe59
FB
106static void parallel_update_irq(ParallelState *s)
107{
108 if (s->irq_pending)
d537cf6c 109 qemu_irq_raise(s->irq);
6508fe59 110 else
d537cf6c 111 qemu_irq_lower(s->irq);
6508fe59
FB
112}
113
5867c88a
TS
114static void
115parallel_ioport_write_sw(void *opaque, uint32_t addr, uint32_t val)
6508fe59
FB
116{
117 ParallelState *s = opaque;
3b46e624 118
5867c88a 119 addr &= 7;
cb2d721c 120 trace_parallel_ioport_write("SW", addr, val);
5867c88a
TS
121 switch(addr) {
122 case PARA_REG_DATA:
0fa7f157
TS
123 s->dataw = val;
124 parallel_update_irq(s);
5867c88a
TS
125 break;
126 case PARA_REG_CTR:
52ccc5e0 127 val |= 0xc0;
0fa7f157
TS
128 if ((val & PARA_CTR_INIT) == 0 ) {
129 s->status = PARA_STS_BUSY;
130 s->status |= PARA_STS_ACK;
131 s->status |= PARA_STS_ONLINE;
132 s->status |= PARA_STS_ERROR;
133 }
134 else if (val & PARA_CTR_SELECT) {
135 if (val & PARA_CTR_STROBE) {
136 s->status &= ~PARA_STS_BUSY;
137 if ((s->control & PARA_CTR_STROBE) == 0)
6ab3fc32
DB
138 /* XXX this blocks entire thread. Rewrite to use
139 * qemu_chr_fe_write and background I/O callbacks */
5345fdb4 140 qemu_chr_fe_write_all(&s->chr, &s->dataw, 1);
0fa7f157
TS
141 } else {
142 if (s->control & PARA_CTR_INTEN) {
143 s->irq_pending = 1;
144 }
145 }
146 }
147 parallel_update_irq(s);
148 s->control = val;
5867c88a
TS
149 break;
150 }
151}
152
153static void parallel_ioport_write_hw(void *opaque, uint32_t addr, uint32_t val)
154{
155 ParallelState *s = opaque;
156 uint8_t parm = val;
563e3c6e 157 int dir;
5867c88a
TS
158
159 /* Sometimes programs do several writes for timing purposes on old
160 HW. Take care not to waste time on writes that do nothing. */
161
162 s->last_read_offset = ~0U;
163
6508fe59 164 addr &= 7;
cb2d721c 165 trace_parallel_ioport_write("HW", addr, val);
6508fe59 166 switch(addr) {
5867c88a
TS
167 case PARA_REG_DATA:
168 if (s->dataw == val)
0fa7f157
TS
169 return;
170 pdebug("wd%02x\n", val);
5345fdb4 171 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_DATA, &parm);
0fa7f157 172 s->dataw = val;
6508fe59 173 break;
5867c88a 174 case PARA_REG_STS:
0fa7f157
TS
175 pdebug("ws%02x\n", val);
176 if (val & PARA_STS_TMOUT)
177 s->epp_timeout = 0;
178 break;
5867c88a
TS
179 case PARA_REG_CTR:
180 val |= 0xc0;
181 if (s->control == val)
0fa7f157
TS
182 return;
183 pdebug("wc%02x\n", val);
563e3c6e
AJ
184
185 if ((val & PARA_CTR_DIR) != (s->control & PARA_CTR_DIR)) {
186 if (val & PARA_CTR_DIR) {
187 dir = 1;
188 } else {
189 dir = 0;
190 }
5345fdb4 191 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_DATA_DIR, &dir);
563e3c6e
AJ
192 parm &= ~PARA_CTR_DIR;
193 }
194
5345fdb4 195 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &parm);
0fa7f157 196 s->control = val;
6508fe59 197 break;
5867c88a 198 case PARA_REG_EPP_ADDR:
0fa7f157
TS
199 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
200 /* Controls not correct for EPP address cycle, so do nothing */
201 pdebug("wa%02x s\n", val);
202 else {
203 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
5345fdb4 204 if (qemu_chr_fe_ioctl(&s->chr,
becdfa00 205 CHR_IOCTL_PP_EPP_WRITE_ADDR, &ioarg)) {
0fa7f157
TS
206 s->epp_timeout = 1;
207 pdebug("wa%02x t\n", val);
208 }
209 else
210 pdebug("wa%02x\n", val);
211 }
212 break;
5867c88a 213 case PARA_REG_EPP_DATA:
0fa7f157
TS
214 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT)
215 /* Controls not correct for EPP data cycle, so do nothing */
216 pdebug("we%02x s\n", val);
217 else {
218 struct ParallelIOArg ioarg = { .buffer = &parm, .count = 1 };
5345fdb4 219 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg)) {
0fa7f157
TS
220 s->epp_timeout = 1;
221 pdebug("we%02x t\n", val);
222 }
223 else
224 pdebug("we%02x\n", val);
225 }
226 break;
5867c88a
TS
227 }
228}
229
230static void
231parallel_ioport_eppdata_write_hw2(void *opaque, uint32_t addr, uint32_t val)
232{
233 ParallelState *s = opaque;
234 uint16_t eppdata = cpu_to_le16(val);
235 int err;
236 struct ParallelIOArg ioarg = {
0fa7f157 237 .buffer = &eppdata, .count = sizeof(eppdata)
5867c88a 238 };
cb2d721c
PMD
239
240 trace_parallel_ioport_write("EPP", addr, val);
5867c88a 241 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
0fa7f157
TS
242 /* Controls not correct for EPP data cycle, so do nothing */
243 pdebug("we%04x s\n", val);
244 return;
5867c88a 245 }
5345fdb4 246 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
5867c88a 247 if (err) {
0fa7f157
TS
248 s->epp_timeout = 1;
249 pdebug("we%04x t\n", val);
5867c88a
TS
250 }
251 else
0fa7f157 252 pdebug("we%04x\n", val);
5867c88a
TS
253}
254
255static void
256parallel_ioport_eppdata_write_hw4(void *opaque, uint32_t addr, uint32_t val)
257{
258 ParallelState *s = opaque;
259 uint32_t eppdata = cpu_to_le32(val);
260 int err;
261 struct ParallelIOArg ioarg = {
0fa7f157 262 .buffer = &eppdata, .count = sizeof(eppdata)
5867c88a 263 };
cb2d721c
PMD
264
265 trace_parallel_ioport_write("EPP", addr, val);
5867c88a 266 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != PARA_CTR_INIT) {
0fa7f157
TS
267 /* Controls not correct for EPP data cycle, so do nothing */
268 pdebug("we%08x s\n", val);
269 return;
5867c88a 270 }
5345fdb4 271 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_WRITE, &ioarg);
5867c88a 272 if (err) {
0fa7f157
TS
273 s->epp_timeout = 1;
274 pdebug("we%08x t\n", val);
6508fe59 275 }
5867c88a 276 else
0fa7f157 277 pdebug("we%08x\n", val);
6508fe59
FB
278}
279
5867c88a 280static uint32_t parallel_ioport_read_sw(void *opaque, uint32_t addr)
6508fe59
FB
281{
282 ParallelState *s = opaque;
283 uint32_t ret = 0xff;
284
285 addr &= 7;
286 switch(addr) {
5867c88a 287 case PARA_REG_DATA:
0fa7f157
TS
288 if (s->control & PARA_CTR_DIR)
289 ret = s->datar;
290 else
291 ret = s->dataw;
6508fe59 292 break;
5867c88a 293 case PARA_REG_STS:
0fa7f157
TS
294 ret = s->status;
295 s->irq_pending = 0;
296 if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
297 /* XXX Fixme: wait 5 microseconds */
298 if (s->status & PARA_STS_ACK)
299 s->status &= ~PARA_STS_ACK;
300 else {
301 /* XXX Fixme: wait 5 microseconds */
302 s->status |= PARA_STS_ACK;
303 s->status |= PARA_STS_BUSY;
304 }
305 }
306 parallel_update_irq(s);
6508fe59 307 break;
5867c88a 308 case PARA_REG_CTR:
6508fe59
FB
309 ret = s->control;
310 break;
311 }
cb2d721c 312 trace_parallel_ioport_read("SW", addr, ret);
5867c88a
TS
313 return ret;
314}
315
316static uint32_t parallel_ioport_read_hw(void *opaque, uint32_t addr)
317{
318 ParallelState *s = opaque;
319 uint8_t ret = 0xff;
320 addr &= 7;
321 switch(addr) {
322 case PARA_REG_DATA:
5345fdb4 323 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_DATA, &ret);
0fa7f157
TS
324 if (s->last_read_offset != addr || s->datar != ret)
325 pdebug("rd%02x\n", ret);
5867c88a
TS
326 s->datar = ret;
327 break;
328 case PARA_REG_STS:
5345fdb4 329 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &ret);
0fa7f157
TS
330 ret &= ~PARA_STS_TMOUT;
331 if (s->epp_timeout)
332 ret |= PARA_STS_TMOUT;
333 if (s->last_read_offset != addr || s->status != ret)
334 pdebug("rs%02x\n", ret);
335 s->status = ret;
5867c88a
TS
336 break;
337 case PARA_REG_CTR:
338 /* s->control has some bits fixed to 1. It is zero only when
0fa7f157
TS
339 it has not been yet written to. */
340 if (s->control == 0) {
5345fdb4 341 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_CONTROL, &ret);
0fa7f157
TS
342 if (s->last_read_offset != addr)
343 pdebug("rc%02x\n", ret);
344 s->control = ret;
345 }
346 else {
347 ret = s->control;
348 if (s->last_read_offset != addr)
349 pdebug("rc%02x\n", ret);
350 }
5867c88a
TS
351 break;
352 case PARA_REG_EPP_ADDR:
becdfa00
MAL
353 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
354 (PARA_CTR_DIR | PARA_CTR_INIT))
0fa7f157
TS
355 /* Controls not correct for EPP addr cycle, so do nothing */
356 pdebug("ra%02x s\n", ret);
357 else {
358 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
5345fdb4 359 if (qemu_chr_fe_ioctl(&s->chr,
becdfa00 360 CHR_IOCTL_PP_EPP_READ_ADDR, &ioarg)) {
0fa7f157
TS
361 s->epp_timeout = 1;
362 pdebug("ra%02x t\n", ret);
363 }
364 else
365 pdebug("ra%02x\n", ret);
366 }
367 break;
5867c88a 368 case PARA_REG_EPP_DATA:
becdfa00
MAL
369 if ((s->control & (PARA_CTR_DIR | PARA_CTR_SIGNAL)) !=
370 (PARA_CTR_DIR | PARA_CTR_INIT))
0fa7f157
TS
371 /* Controls not correct for EPP data cycle, so do nothing */
372 pdebug("re%02x s\n", ret);
373 else {
374 struct ParallelIOArg ioarg = { .buffer = &ret, .count = 1 };
5345fdb4 375 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg)) {
0fa7f157
TS
376 s->epp_timeout = 1;
377 pdebug("re%02x t\n", ret);
378 }
379 else
380 pdebug("re%02x\n", ret);
381 }
382 break;
5867c88a 383 }
cb2d721c 384 trace_parallel_ioport_read("HW", addr, ret);
5867c88a
TS
385 s->last_read_offset = addr;
386 return ret;
387}
388
389static uint32_t
390parallel_ioport_eppdata_read_hw2(void *opaque, uint32_t addr)
391{
392 ParallelState *s = opaque;
393 uint32_t ret;
394 uint16_t eppdata = ~0;
395 int err;
396 struct ParallelIOArg ioarg = {
0fa7f157 397 .buffer = &eppdata, .count = sizeof(eppdata)
5867c88a
TS
398 };
399 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
0fa7f157
TS
400 /* Controls not correct for EPP data cycle, so do nothing */
401 pdebug("re%04x s\n", eppdata);
402 return eppdata;
5867c88a 403 }
5345fdb4 404 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
5867c88a
TS
405 ret = le16_to_cpu(eppdata);
406
407 if (err) {
0fa7f157
TS
408 s->epp_timeout = 1;
409 pdebug("re%04x t\n", ret);
5867c88a
TS
410 }
411 else
0fa7f157 412 pdebug("re%04x\n", ret);
cb2d721c 413 trace_parallel_ioport_read("EPP", addr, ret);
5867c88a
TS
414 return ret;
415}
416
417static uint32_t
418parallel_ioport_eppdata_read_hw4(void *opaque, uint32_t addr)
419{
420 ParallelState *s = opaque;
421 uint32_t ret;
422 uint32_t eppdata = ~0U;
423 int err;
424 struct ParallelIOArg ioarg = {
0fa7f157 425 .buffer = &eppdata, .count = sizeof(eppdata)
5867c88a
TS
426 };
427 if ((s->control & (PARA_CTR_DIR|PARA_CTR_SIGNAL)) != (PARA_CTR_DIR|PARA_CTR_INIT)) {
0fa7f157
TS
428 /* Controls not correct for EPP data cycle, so do nothing */
429 pdebug("re%08x s\n", eppdata);
430 return eppdata;
5867c88a 431 }
5345fdb4 432 err = qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_EPP_READ, &ioarg);
5867c88a
TS
433 ret = le32_to_cpu(eppdata);
434
435 if (err) {
0fa7f157
TS
436 s->epp_timeout = 1;
437 pdebug("re%08x t\n", ret);
5867c88a
TS
438 }
439 else
0fa7f157 440 pdebug("re%08x\n", ret);
cb2d721c 441 trace_parallel_ioport_read("EPP", addr, ret);
5867c88a
TS
442 return ret;
443}
444
445static void parallel_ioport_ecp_write(void *opaque, uint32_t addr, uint32_t val)
446{
cb2d721c 447 trace_parallel_ioport_write("ECP", addr & 7, val);
7f5b7d3e 448 pdebug("wecp%d=%02x\n", addr & 7, val);
5867c88a
TS
449}
450
451static uint32_t parallel_ioport_ecp_read(void *opaque, uint32_t addr)
452{
453 uint8_t ret = 0xff;
7f5b7d3e 454
cb2d721c 455 trace_parallel_ioport_read("ECP", addr & 7, ret);
7f5b7d3e 456 pdebug("recp%d:%02x\n", addr & 7, ret);
6508fe59
FB
457 return ret;
458}
459
33093a0a 460static void parallel_reset(void *opaque)
6508fe59 461{
33093a0a
AJ
462 ParallelState *s = opaque;
463
5867c88a
TS
464 s->datar = ~0;
465 s->dataw = ~0;
6508fe59
FB
466 s->status = PARA_STS_BUSY;
467 s->status |= PARA_STS_ACK;
468 s->status |= PARA_STS_ONLINE;
469 s->status |= PARA_STS_ERROR;
52ccc5e0 470 s->status |= PARA_STS_TMOUT;
6508fe59
FB
471 s->control = PARA_CTR_SELECT;
472 s->control |= PARA_CTR_INIT;
52ccc5e0 473 s->control |= 0xc0;
5867c88a 474 s->irq_pending = 0;
5867c88a
TS
475 s->hw_driver = 0;
476 s->epp_timeout = 0;
477 s->last_read_offset = ~0U;
d60532ca
TS
478}
479
e8ee28fb
GH
480static const int isa_parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
481
1922abd0
RH
482static const MemoryRegionPortio isa_parallel_portio_hw_list[] = {
483 { 0, 8, 1,
484 .read = parallel_ioport_read_hw,
485 .write = parallel_ioport_write_hw },
486 { 4, 1, 2,
487 .read = parallel_ioport_eppdata_read_hw2,
488 .write = parallel_ioport_eppdata_write_hw2 },
489 { 4, 1, 4,
490 .read = parallel_ioport_eppdata_read_hw4,
491 .write = parallel_ioport_eppdata_write_hw4 },
492 { 0x400, 8, 1,
493 .read = parallel_ioport_ecp_read,
494 .write = parallel_ioport_ecp_write },
495 PORTIO_END_OF_LIST(),
496};
497
498static const MemoryRegionPortio isa_parallel_portio_sw_list[] = {
499 { 0, 8, 1,
500 .read = parallel_ioport_read_sw,
501 .write = parallel_ioport_write_sw },
502 PORTIO_END_OF_LIST(),
503};
504
461a2753
PD
505
506static const VMStateDescription vmstate_parallel_isa = {
507 .name = "parallel_isa",
508 .version_id = 1,
509 .minimum_version_id = 1,
510 .fields = (VMStateField[]) {
511 VMSTATE_UINT8(state.dataw, ISAParallelState),
512 VMSTATE_UINT8(state.datar, ISAParallelState),
513 VMSTATE_UINT8(state.status, ISAParallelState),
514 VMSTATE_UINT8(state.control, ISAParallelState),
515 VMSTATE_INT32(state.irq_pending, ISAParallelState),
516 VMSTATE_INT32(state.epp_timeout, ISAParallelState),
517 VMSTATE_END_OF_LIST()
518 }
519};
520
98fab4c1
PH
521static int parallel_can_receive(void *opaque)
522{
523 return 1;
524}
461a2753 525
db895a1e 526static void parallel_isa_realizefn(DeviceState *dev, Error **errp)
d60532ca 527{
e8ee28fb 528 static int index;
db895a1e 529 ISADevice *isadev = ISA_DEVICE(dev);
b0dc5ee6 530 ISAParallelState *isa = ISA_PARALLEL(dev);
021f0674 531 ParallelState *s = &isa->state;
e8ee28fb 532 int base;
d60532ca
TS
533 uint8_t dummy;
534
30650701 535 if (!qemu_chr_fe_backend_connected(&s->chr)) {
db895a1e
AF
536 error_setg(errp, "Can't create parallel device, empty char device");
537 return;
021f0674
GH
538 }
539
db895a1e 540 if (isa->index == -1) {
e8ee28fb 541 isa->index = index;
db895a1e
AF
542 }
543 if (isa->index >= MAX_PARALLEL_PORTS) {
544 error_setg(errp, "Max. supported number of parallel ports is %d.",
545 MAX_PARALLEL_PORTS);
546 return;
547 }
548 if (isa->iobase == -1) {
e8ee28fb 549 isa->iobase = isa_parallel_io[isa->index];
db895a1e 550 }
e8ee28fb
GH
551 index++;
552
553 base = isa->iobase;
db895a1e 554 isa_init_irq(isadev, &s->irq, isa->isairq);
a08d4367 555 qemu_register_reset(parallel_reset, s);
6508fe59 556
98fab4c1
PH
557 qemu_chr_fe_set_handlers(&s->chr, parallel_can_receive, NULL,
558 NULL, NULL, s, NULL, true);
5345fdb4 559 if (qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0) {
5867c88a 560 s->hw_driver = 1;
0fa7f157 561 s->status = dummy;
5867c88a
TS
562 }
563
e305a165 564 isa_register_portio_list(isadev, &s->portio_list, base,
1922abd0
RH
565 (s->hw_driver
566 ? &isa_parallel_portio_hw_list[0]
567 : &isa_parallel_portio_sw_list[0]),
568 s, "parallel");
021f0674
GH
569}
570
d60532ca 571/* Memory mapped interface */
05b4940b 572static uint64_t parallel_mm_readfn(void *opaque, hwaddr addr, unsigned size)
d60532ca
TS
573{
574 ParallelState *s = opaque;
575
05b4940b
PM
576 return parallel_ioport_read_sw(s, addr >> s->it_shift) &
577 MAKE_64BIT_MASK(0, size * 8);
d60532ca
TS
578}
579
05b4940b
PM
580static void parallel_mm_writefn(void *opaque, hwaddr addr,
581 uint64_t value, unsigned size)
d60532ca
TS
582{
583 ParallelState *s = opaque;
584
05b4940b
PM
585 parallel_ioport_write_sw(s, addr >> s->it_shift,
586 value & MAKE_64BIT_MASK(0, size * 8));
d60532ca
TS
587}
588
63858cd9 589static const MemoryRegionOps parallel_mm_ops = {
05b4940b
PM
590 .read = parallel_mm_readfn,
591 .write = parallel_mm_writefn,
592 .valid.min_access_size = 1,
593 .valid.max_access_size = 4,
63858cd9 594 .endianness = DEVICE_NATIVE_ENDIAN,
d60532ca
TS
595};
596
597/* If fd is zero, it means that the parallel device uses the console */
63858cd9 598bool parallel_mm_init(MemoryRegion *address_space,
a8170e5e 599 hwaddr base, int it_shift, qemu_irq irq,
0ec7b3e7 600 Chardev *chr)
d60532ca
TS
601{
602 ParallelState *s;
d60532ca 603
7267c094 604 s = g_malloc0(sizeof(ParallelState));
33093a0a 605 s->irq = irq;
becdfa00 606 qemu_chr_fe_init(&s->chr, chr, &error_abort);
d60532ca 607 s->it_shift = it_shift;
a08d4367 608 qemu_register_reset(parallel_reset, s);
d60532ca 609
2c9b15ca 610 memory_region_init_io(&s->iomem, NULL, &parallel_mm_ops, s,
63858cd9
AK
611 "parallel", 8 << it_shift);
612 memory_region_add_subregion(address_space, base, &s->iomem);
defdb20e 613 return true;
d60532ca 614}
021f0674 615
39bffca2
AL
616static Property parallel_isa_properties[] = {
617 DEFINE_PROP_UINT32("index", ISAParallelState, index, -1),
c7bcc85d 618 DEFINE_PROP_UINT32("iobase", ISAParallelState, iobase, -1),
39bffca2
AL
619 DEFINE_PROP_UINT32("irq", ISAParallelState, isairq, 7),
620 DEFINE_PROP_CHR("chardev", ISAParallelState, state.chr),
621 DEFINE_PROP_END_OF_LIST(),
622};
623
8f04ee08
AL
624static void parallel_isa_class_initfn(ObjectClass *klass, void *data)
625{
39bffca2 626 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e
AF
627
628 dc->realize = parallel_isa_realizefn;
461a2753 629 dc->vmsd = &vmstate_parallel_isa;
4f67d30b 630 device_class_set_props(dc, parallel_isa_properties);
125ee0ed 631 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
8f04ee08
AL
632}
633
8c43a6f0 634static const TypeInfo parallel_isa_info = {
b0dc5ee6 635 .name = TYPE_ISA_PARALLEL,
39bffca2
AL
636 .parent = TYPE_ISA_DEVICE,
637 .instance_size = sizeof(ISAParallelState),
638 .class_init = parallel_isa_class_initfn,
021f0674
GH
639};
640
83f7d43a 641static void parallel_register_types(void)
021f0674 642{
39bffca2 643 type_register_static(&parallel_isa_info);
021f0674
GH
644}
645
83f7d43a 646type_init(parallel_register_types)