qed: introduce qed_aio_start_io and qed_aio_next_io_cb
[qemu/ar7.git] / chardev / char-parallel.c
blob3fa22ce29d4520c8afb2391d028c3189e9287f17
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "sysemu/char.h"
26 #include "qapi/error.h"
27 #include <sys/ioctl.h>
29 #ifdef CONFIG_BSD
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>
36 #endif
37 #else
38 #ifdef __linux__
39 #include <linux/ppdev.h>
40 #include <linux/parport.h>
41 #endif
42 #endif
44 #include "char-fd.h"
45 #include "char-parallel.h"
47 #if defined(__linux__)
49 typedef struct {
50 Chardev parent;
51 int fd;
52 int mode;
53 } ParallelChardev;
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) {
61 int m = mode;
62 if (ioctl(s->fd, PPSETMODE, &m) < 0) {
63 return 0;
65 s->mode = mode;
67 return 1;
70 static int pp_ioctl(Chardev *chr, int cmd, void *arg)
72 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
73 int fd = drv->fd;
74 uint8_t b;
76 switch (cmd) {
77 case CHR_IOCTL_PP_READ_DATA:
78 if (ioctl(fd, PPRDATA, &b) < 0) {
79 return -ENOTSUP;
81 *(uint8_t *)arg = b;
82 break;
83 case CHR_IOCTL_PP_WRITE_DATA:
84 b = *(uint8_t *)arg;
85 if (ioctl(fd, PPWDATA, &b) < 0) {
86 return -ENOTSUP;
88 break;
89 case CHR_IOCTL_PP_READ_CONTROL:
90 if (ioctl(fd, PPRCONTROL, &b) < 0) {
91 return -ENOTSUP;
93 /* Linux gives only the lowest bits, and no way to know data
94 direction! For better compatibility set the fixed upper
95 bits. */
96 *(uint8_t *)arg = b | 0xc0;
97 break;
98 case CHR_IOCTL_PP_WRITE_CONTROL:
99 b = *(uint8_t *)arg;
100 if (ioctl(fd, PPWCONTROL, &b) < 0) {
101 return -ENOTSUP;
103 break;
104 case CHR_IOCTL_PP_READ_STATUS:
105 if (ioctl(fd, PPRSTATUS, &b) < 0) {
106 return -ENOTSUP;
108 *(uint8_t *)arg = b;
109 break;
110 case CHR_IOCTL_PP_DATA_DIR:
111 if (ioctl(fd, PPDATADIR, (int *)arg) < 0) {
112 return -ENOTSUP;
114 break;
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) {
120 return -EIO;
123 break;
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) {
129 return -EIO;
132 break;
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) {
138 return -EIO;
141 break;
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) {
147 return -EIO;
150 break;
151 default:
152 return -ENOTSUP;
154 return 0;
157 static void qemu_chr_open_pp_fd(Chardev *chr,
158 int fd,
159 bool *be_opened,
160 Error **errp)
162 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
164 if (ioctl(fd, PPCLAIM) < 0) {
165 error_setg_errno(errp, errno, "not a parallel port");
166 close(fd);
167 return;
170 drv->fd = fd;
171 drv->mode = IEEE1284_MODE_COMPAT;
173 #endif /* __linux__ */
175 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
177 typedef struct {
178 Chardev parent;
179 int fd;
180 } ParallelChardev;
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);
188 uint8_t b;
190 switch (cmd) {
191 case CHR_IOCTL_PP_READ_DATA:
192 if (ioctl(drv->fd, PPIGDATA, &b) < 0) {
193 return -ENOTSUP;
195 *(uint8_t *)arg = b;
196 break;
197 case CHR_IOCTL_PP_WRITE_DATA:
198 b = *(uint8_t *)arg;
199 if (ioctl(drv->fd, PPISDATA, &b) < 0) {
200 return -ENOTSUP;
202 break;
203 case CHR_IOCTL_PP_READ_CONTROL:
204 if (ioctl(drv->fd, PPIGCTRL, &b) < 0) {
205 return -ENOTSUP;
207 *(uint8_t *)arg = b;
208 break;
209 case CHR_IOCTL_PP_WRITE_CONTROL:
210 b = *(uint8_t *)arg;
211 if (ioctl(drv->fd, PPISCTRL, &b) < 0) {
212 return -ENOTSUP;
214 break;
215 case CHR_IOCTL_PP_READ_STATUS:
216 if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) {
217 return -ENOTSUP;
219 *(uint8_t *)arg = b;
220 break;
221 default:
222 return -ENOTSUP;
224 return 0;
227 static void qemu_chr_open_pp_fd(Chardev *chr,
228 int fd,
229 bool *be_opened,
230 Error **errp)
232 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
233 drv->fd = fd;
234 *be_opened = false;
236 #endif
238 #ifdef HAVE_CHARDEV_PARPORT
239 static void qmp_chardev_open_parallel(Chardev *chr,
240 ChardevBackend *backend,
241 bool *be_opened,
242 Error **errp)
244 ChardevHostdev *parallel = backend->u.parallel.data;
245 int fd;
247 fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
248 if (fd < 0) {
249 return;
251 qemu_chr_open_pp_fd(chr, fd, be_opened, errp);
254 static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
255 Error **errp)
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");
262 return;
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;
281 #endif
284 static void char_parallel_finalize(Object *obj)
286 #if defined(__linux__)
287 Chardev *chr = CHARDEV(obj);
288 ParallelChardev *drv = PARALLEL_CHARDEV(chr);
289 int fd = drv->fd;
291 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
292 ioctl(fd, PPRELEASE);
293 close(fd);
294 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
295 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
296 defined(__DragonFly__)
297 /* FIXME: close fd? */
298 #endif
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);
316 #endif