adb.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * QEMU ADB support
  3. *
  4. * Copyright (c) 2004 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 "hw/input/adb.h"
  26. #include "hw/qdev-properties.h"
  27. #include "migration/vmstate.h"
  28. #include "qemu/module.h"
  29. #include "qemu/timer.h"
  30. #include "adb-internal.h"
  31. #include "trace.h"
  32. /* error codes */
  33. #define ADB_RET_NOTPRESENT (-2)
  34. static const char *adb_commands[] = {
  35. "RESET", "FLUSH", "(Reserved 0x2)", "(Reserved 0x3)",
  36. "Reserved (0x4)", "(Reserved 0x5)", "(Reserved 0x6)", "(Reserved 0x7)",
  37. "LISTEN r0", "LISTEN r1", "LISTEN r2", "LISTEN r3",
  38. "TALK r0", "TALK r1", "TALK r2", "TALK r3",
  39. };
  40. static void adb_device_reset(ADBDevice *d)
  41. {
  42. qdev_reset_all(DEVICE(d));
  43. }
  44. static int do_adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf,
  45. int len)
  46. {
  47. ADBDevice *d;
  48. ADBDeviceClass *adc;
  49. int devaddr, cmd, olen, i;
  50. cmd = buf[0] & 0xf;
  51. if (cmd == ADB_BUSRESET) {
  52. for (i = 0; i < s->nb_devices; i++) {
  53. d = s->devices[i];
  54. adb_device_reset(d);
  55. }
  56. s->status = 0;
  57. return 0;
  58. }
  59. s->pending = 0;
  60. for (i = 0; i < s->nb_devices; i++) {
  61. d = s->devices[i];
  62. adc = ADB_DEVICE_GET_CLASS(d);
  63. if (adc->devhasdata(d)) {
  64. s->pending |= (1 << d->devaddr);
  65. }
  66. }
  67. s->status = 0;
  68. devaddr = buf[0] >> 4;
  69. for (i = 0; i < s->nb_devices; i++) {
  70. d = s->devices[i];
  71. adc = ADB_DEVICE_GET_CLASS(d);
  72. if (d->devaddr == devaddr) {
  73. olen = adc->devreq(d, obuf, buf, len);
  74. if (!olen) {
  75. s->status |= ADB_STATUS_BUSTIMEOUT;
  76. }
  77. return olen;
  78. }
  79. }
  80. s->status |= ADB_STATUS_BUSTIMEOUT;
  81. return ADB_RET_NOTPRESENT;
  82. }
  83. int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
  84. {
  85. int ret;
  86. trace_adb_bus_request(buf[0] >> 4, adb_commands[buf[0] & 0xf], len);
  87. assert(s->autopoll_blocked);
  88. ret = do_adb_request(s, obuf, buf, len);
  89. trace_adb_bus_request_done(buf[0] >> 4, adb_commands[buf[0] & 0xf], ret);
  90. return ret;
  91. }
  92. int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
  93. {
  94. ADBDevice *d;
  95. int olen, i;
  96. uint8_t buf[1];
  97. olen = 0;
  98. for (i = 0; i < s->nb_devices; i++) {
  99. if (s->poll_index >= s->nb_devices) {
  100. s->poll_index = 0;
  101. }
  102. d = s->devices[s->poll_index];
  103. if ((1 << d->devaddr) & poll_mask) {
  104. buf[0] = ADB_READREG | (d->devaddr << 4);
  105. olen = do_adb_request(s, obuf + 1, buf, 1);
  106. /* if there is data, we poll again the same device */
  107. if (olen > 0) {
  108. s->status |= ADB_STATUS_POLLREPLY;
  109. obuf[0] = buf[0];
  110. olen++;
  111. return olen;
  112. }
  113. }
  114. s->poll_index++;
  115. }
  116. return olen;
  117. }
  118. void adb_set_autopoll_enabled(ADBBusState *s, bool enabled)
  119. {
  120. if (s->autopoll_enabled != enabled) {
  121. s->autopoll_enabled = enabled;
  122. if (s->autopoll_enabled) {
  123. timer_mod(s->autopoll_timer,
  124. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  125. s->autopoll_rate_ms);
  126. } else {
  127. timer_del(s->autopoll_timer);
  128. }
  129. }
  130. }
  131. void adb_set_autopoll_rate_ms(ADBBusState *s, int rate_ms)
  132. {
  133. s->autopoll_rate_ms = rate_ms;
  134. if (s->autopoll_enabled) {
  135. timer_mod(s->autopoll_timer,
  136. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  137. s->autopoll_rate_ms);
  138. }
  139. }
  140. void adb_set_autopoll_mask(ADBBusState *s, uint16_t mask)
  141. {
  142. if (s->autopoll_mask != mask) {
  143. s->autopoll_mask = mask;
  144. if (s->autopoll_enabled && s->autopoll_mask) {
  145. timer_mod(s->autopoll_timer,
  146. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  147. s->autopoll_rate_ms);
  148. } else {
  149. timer_del(s->autopoll_timer);
  150. }
  151. }
  152. }
  153. void adb_autopoll_block(ADBBusState *s)
  154. {
  155. s->autopoll_blocked = true;
  156. trace_adb_bus_autopoll_block(s->autopoll_blocked);
  157. if (s->autopoll_enabled) {
  158. timer_del(s->autopoll_timer);
  159. }
  160. }
  161. void adb_autopoll_unblock(ADBBusState *s)
  162. {
  163. s->autopoll_blocked = false;
  164. trace_adb_bus_autopoll_block(s->autopoll_blocked);
  165. if (s->autopoll_enabled) {
  166. timer_mod(s->autopoll_timer,
  167. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  168. s->autopoll_rate_ms);
  169. }
  170. }
  171. static void adb_autopoll(void *opaque)
  172. {
  173. ADBBusState *s = opaque;
  174. if (!s->autopoll_blocked) {
  175. trace_adb_bus_autopoll_cb(s->autopoll_mask);
  176. s->autopoll_cb(s->autopoll_cb_opaque);
  177. trace_adb_bus_autopoll_cb_done(s->autopoll_mask);
  178. }
  179. timer_mod(s->autopoll_timer,
  180. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  181. s->autopoll_rate_ms);
  182. }
  183. void adb_register_autopoll_callback(ADBBusState *s, void (*cb)(void *opaque),
  184. void *opaque)
  185. {
  186. s->autopoll_cb = cb;
  187. s->autopoll_cb_opaque = opaque;
  188. }
  189. static const VMStateDescription vmstate_adb_bus = {
  190. .name = "adb_bus",
  191. .version_id = 0,
  192. .minimum_version_id = 0,
  193. .fields = (VMStateField[]) {
  194. VMSTATE_TIMER_PTR(autopoll_timer, ADBBusState),
  195. VMSTATE_BOOL(autopoll_enabled, ADBBusState),
  196. VMSTATE_UINT8(autopoll_rate_ms, ADBBusState),
  197. VMSTATE_UINT16(autopoll_mask, ADBBusState),
  198. VMSTATE_BOOL(autopoll_blocked, ADBBusState),
  199. VMSTATE_END_OF_LIST()
  200. }
  201. };
  202. static void adb_bus_reset(BusState *qbus)
  203. {
  204. ADBBusState *adb_bus = ADB_BUS(qbus);
  205. adb_bus->autopoll_enabled = false;
  206. adb_bus->autopoll_mask = 0xffff;
  207. adb_bus->autopoll_rate_ms = 20;
  208. }
  209. static void adb_bus_realize(BusState *qbus, Error **errp)
  210. {
  211. ADBBusState *adb_bus = ADB_BUS(qbus);
  212. adb_bus->autopoll_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, adb_autopoll,
  213. adb_bus);
  214. vmstate_register(NULL, -1, &vmstate_adb_bus, adb_bus);
  215. }
  216. static void adb_bus_unrealize(BusState *qbus)
  217. {
  218. ADBBusState *adb_bus = ADB_BUS(qbus);
  219. timer_del(adb_bus->autopoll_timer);
  220. vmstate_unregister(NULL, &vmstate_adb_bus, adb_bus);
  221. }
  222. static void adb_bus_class_init(ObjectClass *klass, void *data)
  223. {
  224. BusClass *k = BUS_CLASS(klass);
  225. k->realize = adb_bus_realize;
  226. k->unrealize = adb_bus_unrealize;
  227. k->reset = adb_bus_reset;
  228. }
  229. static const TypeInfo adb_bus_type_info = {
  230. .name = TYPE_ADB_BUS,
  231. .parent = TYPE_BUS,
  232. .instance_size = sizeof(ADBBusState),
  233. .class_init = adb_bus_class_init,
  234. };
  235. const VMStateDescription vmstate_adb_device = {
  236. .name = "adb_device",
  237. .version_id = 0,
  238. .minimum_version_id = 0,
  239. .fields = (VMStateField[]) {
  240. VMSTATE_INT32(devaddr, ADBDevice),
  241. VMSTATE_INT32(handler, ADBDevice),
  242. VMSTATE_END_OF_LIST()
  243. }
  244. };
  245. static void adb_device_realizefn(DeviceState *dev, Error **errp)
  246. {
  247. ADBDevice *d = ADB_DEVICE(dev);
  248. ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
  249. if (bus->nb_devices >= MAX_ADB_DEVICES) {
  250. return;
  251. }
  252. bus->devices[bus->nb_devices++] = d;
  253. }
  254. static void adb_device_class_init(ObjectClass *oc, void *data)
  255. {
  256. DeviceClass *dc = DEVICE_CLASS(oc);
  257. dc->realize = adb_device_realizefn;
  258. dc->bus_type = TYPE_ADB_BUS;
  259. }
  260. static const TypeInfo adb_device_type_info = {
  261. .name = TYPE_ADB_DEVICE,
  262. .parent = TYPE_DEVICE,
  263. .class_size = sizeof(ADBDeviceClass),
  264. .instance_size = sizeof(ADBDevice),
  265. .abstract = true,
  266. .class_init = adb_device_class_init,
  267. };
  268. static void adb_register_types(void)
  269. {
  270. type_register_static(&adb_bus_type_info);
  271. type_register_static(&adb_device_type_info);
  272. }
  273. type_init(adb_register_types)