parallel.c 19 KB

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