char-parallel.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "chardev/char.h"
  26. #include "qapi/error.h"
  27. #include "qemu/module.h"
  28. #include "qemu/option.h"
  29. #include <sys/ioctl.h>
  30. #ifdef CONFIG_BSD
  31. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  32. #include <dev/ppbus/ppi.h>
  33. #include <dev/ppbus/ppbconf.h>
  34. #elif defined(__DragonFly__)
  35. #include <dev/misc/ppi/ppi.h>
  36. #include <bus/ppbus/ppbconf.h>
  37. #endif
  38. #else
  39. #ifdef __linux__
  40. #include <linux/ppdev.h>
  41. #include <linux/parport.h>
  42. #endif
  43. #endif
  44. #include "chardev/char-fd.h"
  45. #include "chardev/char-parallel.h"
  46. #if defined(__linux__)
  47. typedef struct {
  48. Chardev parent;
  49. int fd;
  50. int mode;
  51. } ParallelChardev;
  52. #define PARALLEL_CHARDEV(obj) \
  53. OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
  54. static int pp_hw_mode(ParallelChardev *s, uint16_t mode)
  55. {
  56. if (s->mode != mode) {
  57. int m = mode;
  58. if (ioctl(s->fd, PPSETMODE, &m) < 0) {
  59. return 0;
  60. }
  61. s->mode = mode;
  62. }
  63. return 1;
  64. }
  65. static int pp_ioctl(Chardev *chr, int cmd, void *arg)
  66. {
  67. ParallelChardev *drv = PARALLEL_CHARDEV(chr);
  68. int fd = drv->fd;
  69. uint8_t b;
  70. switch (cmd) {
  71. case CHR_IOCTL_PP_READ_DATA:
  72. if (ioctl(fd, PPRDATA, &b) < 0) {
  73. return -ENOTSUP;
  74. }
  75. *(uint8_t *)arg = b;
  76. break;
  77. case CHR_IOCTL_PP_WRITE_DATA:
  78. b = *(uint8_t *)arg;
  79. if (ioctl(fd, PPWDATA, &b) < 0) {
  80. return -ENOTSUP;
  81. }
  82. break;
  83. case CHR_IOCTL_PP_READ_CONTROL:
  84. if (ioctl(fd, PPRCONTROL, &b) < 0) {
  85. return -ENOTSUP;
  86. }
  87. /* Linux gives only the lowest bits, and no way to know data
  88. direction! For better compatibility set the fixed upper
  89. bits. */
  90. *(uint8_t *)arg = b | 0xc0;
  91. break;
  92. case CHR_IOCTL_PP_WRITE_CONTROL:
  93. b = *(uint8_t *)arg;
  94. if (ioctl(fd, PPWCONTROL, &b) < 0) {
  95. return -ENOTSUP;
  96. }
  97. break;
  98. case CHR_IOCTL_PP_READ_STATUS:
  99. if (ioctl(fd, PPRSTATUS, &b) < 0) {
  100. return -ENOTSUP;
  101. }
  102. *(uint8_t *)arg = b;
  103. break;
  104. case CHR_IOCTL_PP_DATA_DIR:
  105. if (ioctl(fd, PPDATADIR, (int *)arg) < 0) {
  106. return -ENOTSUP;
  107. }
  108. break;
  109. case CHR_IOCTL_PP_EPP_READ_ADDR:
  110. if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) {
  111. struct ParallelIOArg *parg = arg;
  112. int n = read(fd, parg->buffer, parg->count);
  113. if (n != parg->count) {
  114. return -EIO;
  115. }
  116. }
  117. break;
  118. case CHR_IOCTL_PP_EPP_READ:
  119. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  120. struct ParallelIOArg *parg = arg;
  121. int n = read(fd, parg->buffer, parg->count);
  122. if (n != parg->count) {
  123. return -EIO;
  124. }
  125. }
  126. break;
  127. case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  128. if (pp_hw_mode(drv, IEEE1284_MODE_EPP | IEEE1284_ADDR)) {
  129. struct ParallelIOArg *parg = arg;
  130. int n = write(fd, parg->buffer, parg->count);
  131. if (n != parg->count) {
  132. return -EIO;
  133. }
  134. }
  135. break;
  136. case CHR_IOCTL_PP_EPP_WRITE:
  137. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  138. struct ParallelIOArg *parg = arg;
  139. int n = write(fd, parg->buffer, parg->count);
  140. if (n != parg->count) {
  141. return -EIO;
  142. }
  143. }
  144. break;
  145. default:
  146. return -ENOTSUP;
  147. }
  148. return 0;
  149. }
  150. static void qemu_chr_open_pp_fd(Chardev *chr,
  151. int fd,
  152. bool *be_opened,
  153. Error **errp)
  154. {
  155. ParallelChardev *drv = PARALLEL_CHARDEV(chr);
  156. if (ioctl(fd, PPCLAIM) < 0) {
  157. error_setg_errno(errp, errno, "not a parallel port");
  158. close(fd);
  159. return;
  160. }
  161. drv->fd = fd;
  162. drv->mode = IEEE1284_MODE_COMPAT;
  163. }
  164. #endif /* __linux__ */
  165. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  166. typedef struct {
  167. Chardev parent;
  168. int fd;
  169. } ParallelChardev;
  170. #define PARALLEL_CHARDEV(obj) \
  171. OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL)
  172. static int pp_ioctl(Chardev *chr, int cmd, void *arg)
  173. {
  174. ParallelChardev *drv = PARALLEL_CHARDEV(chr);
  175. uint8_t b;
  176. switch (cmd) {
  177. case CHR_IOCTL_PP_READ_DATA:
  178. if (ioctl(drv->fd, PPIGDATA, &b) < 0) {
  179. return -ENOTSUP;
  180. }
  181. *(uint8_t *)arg = b;
  182. break;
  183. case CHR_IOCTL_PP_WRITE_DATA:
  184. b = *(uint8_t *)arg;
  185. if (ioctl(drv->fd, PPISDATA, &b) < 0) {
  186. return -ENOTSUP;
  187. }
  188. break;
  189. case CHR_IOCTL_PP_READ_CONTROL:
  190. if (ioctl(drv->fd, PPIGCTRL, &b) < 0) {
  191. return -ENOTSUP;
  192. }
  193. *(uint8_t *)arg = b;
  194. break;
  195. case CHR_IOCTL_PP_WRITE_CONTROL:
  196. b = *(uint8_t *)arg;
  197. if (ioctl(drv->fd, PPISCTRL, &b) < 0) {
  198. return -ENOTSUP;
  199. }
  200. break;
  201. case CHR_IOCTL_PP_READ_STATUS:
  202. if (ioctl(drv->fd, PPIGSTATUS, &b) < 0) {
  203. return -ENOTSUP;
  204. }
  205. *(uint8_t *)arg = b;
  206. break;
  207. default:
  208. return -ENOTSUP;
  209. }
  210. return 0;
  211. }
  212. static void qemu_chr_open_pp_fd(Chardev *chr,
  213. int fd,
  214. bool *be_opened,
  215. Error **errp)
  216. {
  217. ParallelChardev *drv = PARALLEL_CHARDEV(chr);
  218. drv->fd = fd;
  219. *be_opened = false;
  220. }
  221. #endif
  222. static void qmp_chardev_open_parallel(Chardev *chr,
  223. ChardevBackend *backend,
  224. bool *be_opened,
  225. Error **errp)
  226. {
  227. ChardevHostdev *parallel = backend->u.parallel.data;
  228. int fd;
  229. fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
  230. if (fd < 0) {
  231. return;
  232. }
  233. qemu_chr_open_pp_fd(chr, fd, be_opened, errp);
  234. }
  235. static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
  236. Error **errp)
  237. {
  238. const char *device = qemu_opt_get(opts, "path");
  239. ChardevHostdev *parallel;
  240. if (device == NULL) {
  241. error_setg(errp, "chardev: parallel: no device path given");
  242. return;
  243. }
  244. backend->type = CHARDEV_BACKEND_KIND_PARALLEL;
  245. parallel = backend->u.parallel.data = g_new0(ChardevHostdev, 1);
  246. qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(parallel));
  247. parallel->device = g_strdup(device);
  248. }
  249. static void char_parallel_class_init(ObjectClass *oc, void *data)
  250. {
  251. ChardevClass *cc = CHARDEV_CLASS(oc);
  252. cc->parse = qemu_chr_parse_parallel;
  253. cc->open = qmp_chardev_open_parallel;
  254. cc->chr_ioctl = pp_ioctl;
  255. }
  256. static void char_parallel_finalize(Object *obj)
  257. {
  258. Chardev *chr = CHARDEV(obj);
  259. ParallelChardev *drv = PARALLEL_CHARDEV(chr);
  260. int fd = drv->fd;
  261. #if defined(__linux__)
  262. pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  263. ioctl(fd, PPRELEASE);
  264. #endif
  265. close(fd);
  266. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  267. }
  268. static const TypeInfo char_parallel_type_info = {
  269. .name = TYPE_CHARDEV_PARALLEL,
  270. .parent = TYPE_CHARDEV,
  271. .instance_size = sizeof(ParallelChardev),
  272. .instance_finalize = char_parallel_finalize,
  273. .class_init = char_parallel_class_init,
  274. };
  275. static void register_types(void)
  276. {
  277. type_register_static(&char_parallel_type_info);
  278. }
  279. type_init(register_types);