2
0

adb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "adb-internal.h"
  30. /* error codes */
  31. #define ADB_RET_NOTPRESENT (-2)
  32. static void adb_device_reset(ADBDevice *d)
  33. {
  34. qdev_reset_all(DEVICE(d));
  35. }
  36. int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
  37. {
  38. ADBDevice *d;
  39. int devaddr, cmd, i;
  40. cmd = buf[0] & 0xf;
  41. if (cmd == ADB_BUSRESET) {
  42. for(i = 0; i < s->nb_devices; i++) {
  43. d = s->devices[i];
  44. adb_device_reset(d);
  45. }
  46. return 0;
  47. }
  48. devaddr = buf[0] >> 4;
  49. for(i = 0; i < s->nb_devices; i++) {
  50. d = s->devices[i];
  51. if (d->devaddr == devaddr) {
  52. ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d);
  53. return adc->devreq(d, obuf, buf, len);
  54. }
  55. }
  56. return ADB_RET_NOTPRESENT;
  57. }
  58. /* XXX: move that to cuda ? */
  59. int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
  60. {
  61. ADBDevice *d;
  62. int olen, i;
  63. uint8_t buf[1];
  64. olen = 0;
  65. for(i = 0; i < s->nb_devices; i++) {
  66. if (s->poll_index >= s->nb_devices)
  67. s->poll_index = 0;
  68. d = s->devices[s->poll_index];
  69. if ((1 << d->devaddr) & poll_mask) {
  70. buf[0] = ADB_READREG | (d->devaddr << 4);
  71. olen = adb_request(s, obuf + 1, buf, 1);
  72. /* if there is data, we poll again the same device */
  73. if (olen > 0) {
  74. obuf[0] = buf[0];
  75. olen++;
  76. break;
  77. }
  78. }
  79. s->poll_index++;
  80. }
  81. return olen;
  82. }
  83. static const TypeInfo adb_bus_type_info = {
  84. .name = TYPE_ADB_BUS,
  85. .parent = TYPE_BUS,
  86. .instance_size = sizeof(ADBBusState),
  87. };
  88. const VMStateDescription vmstate_adb_device = {
  89. .name = "adb_device",
  90. .version_id = 0,
  91. .minimum_version_id = 0,
  92. .fields = (VMStateField[]) {
  93. VMSTATE_INT32(devaddr, ADBDevice),
  94. VMSTATE_INT32(handler, ADBDevice),
  95. VMSTATE_END_OF_LIST()
  96. }
  97. };
  98. static void adb_device_realizefn(DeviceState *dev, Error **errp)
  99. {
  100. ADBDevice *d = ADB_DEVICE(dev);
  101. ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
  102. if (bus->nb_devices >= MAX_ADB_DEVICES) {
  103. return;
  104. }
  105. bus->devices[bus->nb_devices++] = d;
  106. }
  107. static Property adb_device_properties[] = {
  108. DEFINE_PROP_BOOL("disable-direct-reg3-writes", ADBDevice,
  109. disable_direct_reg3_writes, false),
  110. DEFINE_PROP_END_OF_LIST(),
  111. };
  112. static void adb_device_class_init(ObjectClass *oc, void *data)
  113. {
  114. DeviceClass *dc = DEVICE_CLASS(oc);
  115. dc->realize = adb_device_realizefn;
  116. dc->props = adb_device_properties;
  117. dc->bus_type = TYPE_ADB_BUS;
  118. }
  119. static const TypeInfo adb_device_type_info = {
  120. .name = TYPE_ADB_DEVICE,
  121. .parent = TYPE_DEVICE,
  122. .instance_size = sizeof(ADBDevice),
  123. .abstract = true,
  124. .class_init = adb_device_class_init,
  125. };
  126. static void adb_register_types(void)
  127. {
  128. type_register_static(&adb_bus_type_info);
  129. type_register_static(&adb_device_type_info);
  130. }
  131. type_init(adb_register_types)