low level host parallel port access

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1611 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
bellard 2005-11-10 23:58:52 +00:00
parent 2122c51a9c
commit e57a8c0eef
4 changed files with 171 additions and 65 deletions

View File

@ -8,6 +8,7 @@ version 0.7.3:
- ES1370 PCI audio device (malc) - ES1370 PCI audio device (malc)
- Initial USB support - Initial USB support
- Linux host serial port access - Linux host serial port access
- Linux host low level parallel port access
version 0.7.2: version 0.7.2:

View File

@ -1,7 +1,7 @@
/* /*
* QEMU Parallel PORT emulation * QEMU Parallel PORT emulation
* *
* Copyright (c) 2003-2004 Fabrice Bellard * Copyright (c) 2003-2005 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -50,6 +50,7 @@ struct ParallelState {
int irq; int irq;
int irq_pending; int irq_pending;
CharDriverState *chr; CharDriverState *chr;
int hw_driver;
}; };
static void parallel_update_irq(ParallelState *s) static void parallel_update_irq(ParallelState *s)
@ -70,29 +71,39 @@ static void parallel_ioport_write(void *opaque, uint32_t addr, uint32_t val)
#endif #endif
switch(addr) { switch(addr) {
case 0: case 0:
s->data = val; if (s->hw_driver) {
parallel_update_irq(s); s->data = val;
qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_DATA, &s->data);
} else {
s->data = val;
parallel_update_irq(s);
}
break; break;
case 2: case 2:
if ((val & PARA_CTR_INIT) == 0 ) { if (s->hw_driver) {
s->status = PARA_STS_BUSY; s->control = val;
s->status |= PARA_STS_ACK; qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_WRITE_CONTROL, &s->control);
s->status |= PARA_STS_ONLINE; } else {
s->status |= PARA_STS_ERROR; if ((val & PARA_CTR_INIT) == 0 ) {
} s->status = PARA_STS_BUSY;
else if (val & PARA_CTR_SELECT) { s->status |= PARA_STS_ACK;
if (val & PARA_CTR_STROBE) { s->status |= PARA_STS_ONLINE;
s->status &= ~PARA_STS_BUSY; s->status |= PARA_STS_ERROR;
if ((s->control & PARA_CTR_STROBE) == 0) }
qemu_chr_write(s->chr, &s->data, 1); else if (val & PARA_CTR_SELECT) {
} else { if (val & PARA_CTR_STROBE) {
if (s->control & PARA_CTR_INTEN) { s->status &= ~PARA_STS_BUSY;
s->irq_pending = 1; if ((s->control & PARA_CTR_STROBE) == 0)
qemu_chr_write(s->chr, &s->data, 1);
} else {
if (s->control & PARA_CTR_INTEN) {
s->irq_pending = 1;
}
} }
} }
parallel_update_irq(s);
s->control = val;
} }
parallel_update_irq(s);
s->control = val;
break; break;
} }
} }
@ -105,24 +116,35 @@ static uint32_t parallel_ioport_read(void *opaque, uint32_t addr)
addr &= 7; addr &= 7;
switch(addr) { switch(addr) {
case 0: case 0:
if (s->hw_driver) {
qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_DATA, &s->data);
}
ret = s->data; ret = s->data;
break; break;
case 1: case 1:
ret = s->status; if (s->hw_driver) {
s->irq_pending = 0; qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_STATUS, &s->status);
if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) { ret = s->status;
/* XXX Fixme: wait 5 microseconds */ } else {
if (s->status & PARA_STS_ACK) ret = s->status;
s->status &= ~PARA_STS_ACK; s->irq_pending = 0;
else { if ((s->status & PARA_STS_BUSY) == 0 && (s->control & PARA_CTR_STROBE) == 0) {
/* XXX Fixme: wait 5 microseconds */ /* XXX Fixme: wait 5 microseconds */
s->status |= PARA_STS_ACK; if (s->status & PARA_STS_ACK)
s->status |= PARA_STS_BUSY; s->status &= ~PARA_STS_ACK;
else {
/* XXX Fixme: wait 5 microseconds */
s->status |= PARA_STS_ACK;
s->status |= PARA_STS_BUSY;
}
} }
parallel_update_irq(s);
} }
parallel_update_irq(s);
break; break;
case 2: case 2:
if (s->hw_driver) {
qemu_chr_ioctl(s->chr, CHR_IOCTL_PP_READ_CONTROL, &s->control);
}
ret = s->control; ret = s->control;
break; break;
} }
@ -153,18 +175,20 @@ static void parallel_receive1(void *opaque, const uint8_t *buf, int size)
parallel_receive_byte(s, buf[0]); parallel_receive_byte(s, buf[0]);
} }
static void parallel_event(void *opaque, int event)
{
}
/* If fd is zero, it means that the parallel device uses the console */ /* If fd is zero, it means that the parallel device uses the console */
ParallelState *parallel_init(int base, int irq, CharDriverState *chr) ParallelState *parallel_init(int base, int irq, CharDriverState *chr)
{ {
ParallelState *s; ParallelState *s;
uint8_t dummy;
s = qemu_mallocz(sizeof(ParallelState)); s = qemu_mallocz(sizeof(ParallelState));
if (!s) if (!s)
return NULL; return NULL;
s->chr = chr;
s->hw_driver = 0;
if (qemu_chr_ioctl(chr, CHR_IOCTL_PP_READ_STATUS, &dummy) == 0)
s->hw_driver = 1;
s->irq = irq; s->irq = irq;
s->data = 0; s->data = 0;
s->status = PARA_STS_BUSY; s->status = PARA_STS_BUSY;
@ -176,8 +200,6 @@ ParallelState *parallel_init(int base, int irq, CharDriverState *chr)
register_ioport_write(base, 8, 1, parallel_ioport_write, s); register_ioport_write(base, 8, 1, parallel_ioport_write, s);
register_ioport_read(base, 8, 1, parallel_ioport_read, s); register_ioport_read(base, 8, 1, parallel_ioport_read, s);
s->chr = chr;
qemu_chr_add_read_handler(chr, parallel_can_receive1, parallel_receive1, s); qemu_chr_add_read_handler(chr, parallel_can_receive1, parallel_receive1, s);
qemu_chr_add_event_handler(chr, parallel_event);
return s; return s;
} }

View File

@ -364,8 +364,11 @@ Virtual console
@item null @item null
void device void device
@item /dev/XXX @item /dev/XXX
[Linux only]Use host tty, e.g. @file{/dev/ttyS0}. The host serial port [Linux only] Use host tty, e.g. @file{/dev/ttyS0}. The host serial port
parameters are set according to the emulated ones. parameters are set according to the emulated ones.
@item /dev/parportN
[Linux only, parallel port only] Use host parallel port
@var{N}. Currently only SPP parallel port features can be used.
@item file:filename @item file:filename
Write output to filename. No character can be read. Write output to filename. No character can be read.
@item stdio @item stdio
@ -379,6 +382,15 @@ non graphical mode.
This option can be used several times to simulate up to 4 serials This option can be used several times to simulate up to 4 serials
ports. ports.
@item -parallel dev
Redirect the virtual parallel port to host device @var{dev} (same
devices as the serial port). On Linux hosts, @file{/dev/parportN} can
be used to use hardware devices connected on the corresponding host
parallel port.
This option can be used several times to simulate up to 3 parallel
ports.
@item -monitor dev @item -monitor dev
Redirect the monitor to host device @var{dev} (same devices as the Redirect the monitor to host device @var{dev} (same devices as the
serial port). serial port).

127
vl.c
View File

@ -51,6 +51,7 @@
#include <pty.h> #include <pty.h>
#include <malloc.h> #include <malloc.h>
#include <linux/rtc.h> #include <linux/rtc.h>
#include <linux/ppdev.h>
#endif #endif
#endif #endif
@ -1013,21 +1014,13 @@ int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
return s->chr_write(s, buf, len); return s->chr_write(s, buf, len);
} }
void qemu_chr_set_serial_parameters(CharDriverState *s, int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
int speed, int parity,
int data_bits, int stop_bits)
{ {
if (s->chr_set_serial_parameters) if (!s->chr_ioctl)
s->chr_set_serial_parameters(s, speed, parity, data_bits, stop_bits); return -ENOTSUP;
return s->chr_ioctl(s, cmd, arg);
} }
void qemu_chr_set_serial_break(CharDriverState *s, int enable)
{
if (s->chr_set_serial_break)
s->chr_set_serial_break(s, enable);
}
void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
{ {
char buf[4096]; char buf[4096];
@ -1379,7 +1372,11 @@ static void tty_serial_init(int fd, int speed,
struct termios tty; struct termios tty;
speed_t spd; speed_t spd;
tcgetattr (0, &tty); #if 0
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
speed, parity, data_bits, stop_bits);
#endif
tcgetattr (fd, &tty);
switch(speed) { switch(speed) {
case 50: case 50:
@ -1459,20 +1456,29 @@ static void tty_serial_init(int fd, int speed,
tcsetattr (fd, TCSANOW, &tty); tcsetattr (fd, TCSANOW, &tty);
} }
static void tty_set_serial_parameters(CharDriverState *chr, static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
int speed, int parity,
int data_bits, int stop_bits)
{ {
FDCharDriver *s = chr->opaque; FDCharDriver *s = chr->opaque;
tty_serial_init(s->fd_in, speed, parity, data_bits, stop_bits);
} switch(cmd) {
case CHR_IOCTL_SERIAL_SET_PARAMS:
static void tty_set_serial_break(CharDriverState *chr, int enable) {
{ QEMUSerialSetParams *ssp = arg;
FDCharDriver *s = chr->opaque; tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
/* XXX: find a better solution */ ssp->data_bits, ssp->stop_bits);
if (enable) }
tcsendbreak(s->fd_in, 1); break;
case CHR_IOCTL_SERIAL_SET_BREAK:
{
int enable = *(int *)arg;
if (enable)
tcsendbreak(s->fd_in, 1);
}
break;
default:
return -ENOTSUP;
}
return 0;
} }
CharDriverState *qemu_chr_open_tty(const char *filename) CharDriverState *qemu_chr_open_tty(const char *filename)
@ -1480,7 +1486,7 @@ CharDriverState *qemu_chr_open_tty(const char *filename)
CharDriverState *chr; CharDriverState *chr;
int fd; int fd;
fd = open(filename, O_RDWR); fd = open(filename, O_RDWR | O_NONBLOCK);
if (fd < 0) if (fd < 0)
return NULL; return NULL;
fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFL, O_NONBLOCK);
@ -1488,8 +1494,70 @@ CharDriverState *qemu_chr_open_tty(const char *filename)
chr = qemu_chr_open_fd(fd, fd); chr = qemu_chr_open_fd(fd, fd);
if (!chr) if (!chr)
return NULL; return NULL;
chr->chr_set_serial_parameters = tty_set_serial_parameters; chr->chr_ioctl = tty_serial_ioctl;
chr->chr_set_serial_break = tty_set_serial_break; return chr;
}
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
{
int fd = (int)chr->opaque;
uint8_t b;
switch(cmd) {
case CHR_IOCTL_PP_READ_DATA:
if (ioctl(fd, PPRDATA, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_WRITE_DATA:
b = *(uint8_t *)arg;
if (ioctl(fd, PPWDATA, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_CONTROL:
if (ioctl(fd, PPRCONTROL, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
case CHR_IOCTL_PP_WRITE_CONTROL:
b = *(uint8_t *)arg;
if (ioctl(fd, PPWCONTROL, &b) < 0)
return -ENOTSUP;
break;
case CHR_IOCTL_PP_READ_STATUS:
if (ioctl(fd, PPRSTATUS, &b) < 0)
return -ENOTSUP;
*(uint8_t *)arg = b;
break;
default:
return -ENOTSUP;
}
return 0;
}
CharDriverState *qemu_chr_open_pp(const char *filename)
{
CharDriverState *chr;
int fd;
fd = open(filename, O_RDWR);
if (fd < 0)
return NULL;
if (ioctl(fd, PPCLAIM) < 0) {
close(fd);
return NULL;
}
chr = qemu_mallocz(sizeof(CharDriverState));
if (!chr) {
close(fd);
return NULL;
}
chr->opaque = (void *)fd;
chr->chr_write = null_chr_write;
chr->chr_add_read_handler = null_chr_add_read_handler;
chr->chr_ioctl = pp_ioctl;
return chr; return chr;
} }
@ -1522,6 +1590,9 @@ CharDriverState *qemu_chr_open(const char *filename)
} else } else
#endif #endif
#if defined(__linux__) #if defined(__linux__)
if (strstart(filename, "/dev/parport", NULL)) {
return qemu_chr_open_pp(filename);
} else
if (strstart(filename, "/dev/", NULL)) { if (strstart(filename, "/dev/", NULL)) {
return qemu_chr_open_tty(filename); return qemu_chr_open_tty(filename);
} else } else