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