4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "chardev/char.h"
26 #include "qapi/error.h"
27 #include <sys/ioctl.h>
30 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
31 #include <dev/ppbus/ppi.h>
32 #include <dev/ppbus/ppbconf.h>
33 #elif defined(__DragonFly__)
34 #include <dev/misc/ppi/ppi.h>
35 #include <bus/ppbus/ppbconf.h>
39 #include <linux/ppdev.h>
40 #include <linux/parport.h>
44 #include "chardev/char-fd.h"
45 #include "chardev/char-parallel.h"
47 #if defined(__linux__)
55 #define PARALLEL_CHARDEV(obj) \
56 OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
58 static int pp_hw_mode(ParallelChardev *s, uint16_t mode)
60 if (s->mode != mode) {
62 if (ioctl(s->fd, PPSETMODE, &m) < 0) {
70 static int pp_ioctl(Chardev *chr, int cmd, void *arg)
72 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
77 case CHR_IOCTL_PP_READ_DATA:
78 if (ioctl(fd, PPRDATA, &b) < 0) {
83 case CHR_IOCTL_PP_WRITE_DATA:
85 if (ioctl(fd, PPWDATA, &b) < 0) {
89 case CHR_IOCTL_PP_READ_CONTROL:
90 if (ioctl(fd, PPRCONTROL, &b) < 0) {
93 /* Linux gives only the lowest bits, and no way to know data
94 direction! For better compatibility set the fixed upper
96 *(uint8_t *)arg = b | 0xc0;
98 case CHR_IOCTL_PP_WRITE_CONTROL:
100 if (ioctl(fd, PPWCONTROL, &b) < 0) {
104 case CHR_IOCTL_PP_READ_STATUS:
105 if (ioctl(fd, PPRSTATUS, &b) < 0) {
110 case CHR_IOCTL_PP_DATA_DIR:
111 if (ioctl(fd, PPDATADIR, (int *)arg) < 0) {
115 case CHR_IOCTL_PP_EPP_READ_ADDR:
116 if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) {
117 struct ParallelIOArg *parg = arg;
118 int n = read(fd, parg->buffer, parg->count);
119 if (n != parg->count) {
124 case CHR_IOCTL_PP_EPP_READ:
125 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
126 struct ParallelIOArg *parg = arg;
127 int n = read(fd, parg->buffer, parg->count);
128 if (n != parg->count) {
133 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
134 if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) {
135 struct ParallelIOArg *parg = arg;
136 int n = write(fd, parg->buffer, parg->count);
137 if (n != parg->count) {
142 case CHR_IOCTL_PP_EPP_WRITE:
143 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
144 struct ParallelIOArg *parg = arg;
145 int n = write(fd, parg->buffer, parg->count);
146 if (n != parg->count) {
157 static void qemu_chr_open_pp_fd(Chardev *chr,
162 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
164 if (ioctl(fd, PPCLAIM) < 0) {
165 error_setg_errno(errp, errno, "not a parallel port");
171 drv->mode = IEEE1284_MODE_COMPAT;
173 #endif /* __linux__ */
175 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
182 #define PARALLEL_CHARDEV(obj) \
183 OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
185 static int pp_ioctl(Chardev *chr, int cmd, void *arg)
187 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
191 case CHR_IOCTL_PP_READ_DATA:
192 if (ioctl(drv->fd, PPIGDATA, &b) < 0) {
197 case CHR_IOCTL_PP_WRITE_DATA:
199 if (ioctl(drv->fd, PPISDATA, &b) < 0) {
203 case CHR_IOCTL_PP_READ_CONTROL:
204 if (ioctl(drv->fd, PPIGCTRL, &b) < 0) {
209 case CHR_IOCTL_PP_WRITE_CONTROL:
211 if (ioctl(drv->fd, PPISCTRL, &b) < 0) {
215 case CHR_IOCTL_PP_READ_STATUS:
216 if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) {
227 static void qemu_chr_open_pp_fd(Chardev *chr,
232 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
238 #ifdef HAVE_CHARDEV_PARPORT
239 static void qmp_chardev_open_parallel(Chardev *chr,
240 ChardevBackend *backend,
244 ChardevHostdev *parallel = backend->u.parallel.data;
247 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
251 qemu_chr_open_pp_fd(chr, fd, be_opened, errp);
254 static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
257 const char *device = qemu_opt_get(opts, "path");
258 ChardevHostdev *parallel;
260 if (device == NULL) {
261 error_setg(errp, "chardev: parallel: no device path given");
264 backend->type = CHARDEV_BACKEND_KIND_PARALLEL;
265 parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1);
266 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel));
267 parallel->device = g_strdup(device);
270 static void char_parallel_class_init(ObjectClass *oc, void *data)
272 ChardevClass *cc = CHARDEV_CLASS(oc);
274 cc->parse = qemu_chr_parse_parallel;
275 cc->open = qmp_chardev_open_parallel;
276 #if defined(__linux__)
277 cc->chr_ioctl = pp_ioctl;
278 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
279 defined(__DragonFly__)
280 cc->chr_ioctl = pp_ioctl;
284 static void char_parallel_finalize(Object *obj)
286 #if defined(__linux__)
287 Chardev *chr = CHARDEV(obj);
288 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
291 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
292 ioctl(fd, PPRELEASE);
294 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
295 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
296 defined(__DragonFly__)
297 /* FIXME: close fd? */
301 static const TypeInfo char_parallel_type_info = {
302 .name = TYPE_CHARDEV_PARALLEL,
303 .parent = TYPE_CHARDEV,
304 .instance_size = sizeof(ParallelChardev),
305 .instance_finalize = char_parallel_finalize,
306 .class_init = char_parallel_class_init,
309 static void register_types(void)
311 type_register_static(&char_parallel_type_info);
314 type_init(register_types);