adb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "hw.h"
  25. #include "ppc_mac.h"
  26. #include "console.h"
  27. /* debug ADB */
  28. //#define DEBUG_ADB
  29. #ifdef DEBUG_ADB
  30. #define ADB_DPRINTF(fmt, ...) \
  31. do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0)
  32. #else
  33. #define ADB_DPRINTF(fmt, ...)
  34. #endif
  35. /* ADB commands */
  36. #define ADB_BUSRESET 0x00
  37. #define ADB_FLUSH 0x01
  38. #define ADB_WRITEREG 0x08
  39. #define ADB_READREG 0x0c
  40. /* ADB device commands */
  41. #define ADB_CMD_SELF_TEST 0xff
  42. #define ADB_CMD_CHANGE_ID 0xfe
  43. #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd
  44. #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00
  45. /* ADB default device IDs (upper 4 bits of ADB command byte) */
  46. #define ADB_DONGLE 1
  47. #define ADB_KEYBOARD 2
  48. #define ADB_MOUSE 3
  49. #define ADB_TABLET 4
  50. #define ADB_MODEM 5
  51. #define ADB_MISC 7
  52. /* error codes */
  53. #define ADB_RET_NOTPRESENT (-2)
  54. int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
  55. {
  56. ADBDevice *d;
  57. int devaddr, cmd, i;
  58. cmd = buf[0] & 0xf;
  59. if (cmd == ADB_BUSRESET) {
  60. for(i = 0; i < s->nb_devices; i++) {
  61. d = &s->devices[i];
  62. if (d->devreset) {
  63. d->devreset(d);
  64. }
  65. }
  66. return 0;
  67. }
  68. devaddr = buf[0] >> 4;
  69. for(i = 0; i < s->nb_devices; i++) {
  70. d = &s->devices[i];
  71. if (d->devaddr == devaddr) {
  72. return d->devreq(d, obuf, buf, len);
  73. }
  74. }
  75. return ADB_RET_NOTPRESENT;
  76. }
  77. /* XXX: move that to cuda ? */
  78. int adb_poll(ADBBusState *s, uint8_t *obuf)
  79. {
  80. ADBDevice *d;
  81. int olen, i;
  82. uint8_t buf[1];
  83. olen = 0;
  84. for(i = 0; i < s->nb_devices; i++) {
  85. if (s->poll_index >= s->nb_devices)
  86. s->poll_index = 0;
  87. d = &s->devices[s->poll_index];
  88. buf[0] = ADB_READREG | (d->devaddr << 4);
  89. olen = adb_request(s, obuf + 1, buf, 1);
  90. /* if there is data, we poll again the same device */
  91. if (olen > 0) {
  92. obuf[0] = buf[0];
  93. olen++;
  94. break;
  95. }
  96. s->poll_index++;
  97. }
  98. return olen;
  99. }
  100. ADBDevice *adb_register_device(ADBBusState *s, int devaddr,
  101. ADBDeviceRequest *devreq,
  102. ADBDeviceReset *devreset,
  103. void *opaque)
  104. {
  105. ADBDevice *d;
  106. if (s->nb_devices >= MAX_ADB_DEVICES)
  107. return NULL;
  108. d = &s->devices[s->nb_devices++];
  109. d->bus = s;
  110. d->devaddr = devaddr;
  111. d->devreq = devreq;
  112. d->devreset = devreset;
  113. d->opaque = opaque;
  114. qemu_register_reset((QEMUResetHandler *)devreset, d);
  115. return d;
  116. }
  117. /***************************************************************/
  118. /* Keyboard ADB device */
  119. typedef struct KBDState {
  120. uint8_t data[128];
  121. int rptr, wptr, count;
  122. } KBDState;
  123. static const uint8_t pc_to_adb_keycode[256] = {
  124. 0, 53, 18, 19, 20, 21, 23, 22, 26, 28, 25, 29, 27, 24, 51, 48,
  125. 12, 13, 14, 15, 17, 16, 32, 34, 31, 35, 33, 30, 36, 54, 0, 1,
  126. 2, 3, 5, 4, 38, 40, 37, 41, 39, 50, 56, 42, 6, 7, 8, 9,
  127. 11, 45, 46, 43, 47, 44,123, 67, 58, 49, 57,122,120, 99,118, 96,
  128. 97, 98,100,101,109, 71,107, 89, 91, 92, 78, 86, 87, 88, 69, 83,
  129. 84, 85, 82, 65, 0, 0, 10,103,111, 0, 0,110, 81, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  131. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  132. 0, 0, 0, 94, 0, 93, 0, 0, 0, 0, 0, 0,104,102, 0, 0,
  133. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76,125, 0, 0,
  134. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,105, 0, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 0, 75, 0, 0,124, 0, 0, 0, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 0, 0, 0,115, 62,116, 0, 59, 0, 60, 0,119,
  137. 61,121,114,117, 0, 0, 0, 0, 0, 0, 0, 55,126, 0,127, 0,
  138. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  139. 0, 0, 0, 0, 0, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  140. };
  141. static void adb_kbd_put_keycode(void *opaque, int keycode)
  142. {
  143. ADBDevice *d = opaque;
  144. KBDState *s = d->opaque;
  145. if (s->count < sizeof(s->data)) {
  146. s->data[s->wptr] = keycode;
  147. if (++s->wptr == sizeof(s->data))
  148. s->wptr = 0;
  149. s->count++;
  150. }
  151. }
  152. static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
  153. {
  154. static int ext_keycode;
  155. KBDState *s = d->opaque;
  156. int adb_keycode, keycode;
  157. int olen;
  158. olen = 0;
  159. for(;;) {
  160. if (s->count == 0)
  161. break;
  162. keycode = s->data[s->rptr];
  163. if (++s->rptr == sizeof(s->data))
  164. s->rptr = 0;
  165. s->count--;
  166. if (keycode == 0xe0) {
  167. ext_keycode = 1;
  168. } else {
  169. if (ext_keycode)
  170. adb_keycode = pc_to_adb_keycode[keycode | 0x80];
  171. else
  172. adb_keycode = pc_to_adb_keycode[keycode & 0x7f];
  173. obuf[0] = adb_keycode | (keycode & 0x80);
  174. /* NOTE: could put a second keycode if needed */
  175. obuf[1] = 0xff;
  176. olen = 2;
  177. ext_keycode = 0;
  178. break;
  179. }
  180. }
  181. return olen;
  182. }
  183. static int adb_kbd_request(ADBDevice *d, uint8_t *obuf,
  184. const uint8_t *buf, int len)
  185. {
  186. KBDState *s = d->opaque;
  187. int cmd, reg, olen;
  188. if ((buf[0] & 0x0f) == ADB_FLUSH) {
  189. /* flush keyboard fifo */
  190. s->wptr = s->rptr = s->count = 0;
  191. return 0;
  192. }
  193. cmd = buf[0] & 0xc;
  194. reg = buf[0] & 0x3;
  195. olen = 0;
  196. switch(cmd) {
  197. case ADB_WRITEREG:
  198. switch(reg) {
  199. case 2:
  200. /* LED status */
  201. break;
  202. case 3:
  203. switch(buf[2]) {
  204. case ADB_CMD_SELF_TEST:
  205. break;
  206. case ADB_CMD_CHANGE_ID:
  207. case ADB_CMD_CHANGE_ID_AND_ACT:
  208. case ADB_CMD_CHANGE_ID_AND_ENABLE:
  209. d->devaddr = buf[1] & 0xf;
  210. break;
  211. default:
  212. /* XXX: check this */
  213. d->devaddr = buf[1] & 0xf;
  214. d->handler = buf[2];
  215. break;
  216. }
  217. }
  218. break;
  219. case ADB_READREG:
  220. switch(reg) {
  221. case 0:
  222. olen = adb_kbd_poll(d, obuf);
  223. break;
  224. case 1:
  225. break;
  226. case 2:
  227. obuf[0] = 0x00; /* XXX: check this */
  228. obuf[1] = 0x07; /* led status */
  229. olen = 2;
  230. break;
  231. case 3:
  232. obuf[0] = d->handler;
  233. obuf[1] = d->devaddr;
  234. olen = 2;
  235. break;
  236. }
  237. break;
  238. }
  239. return olen;
  240. }
  241. static const VMStateDescription vmstate_adb_kbd = {
  242. .name = "adb_kbd",
  243. .version_id = 1,
  244. .minimum_version_id = 1,
  245. .minimum_version_id_old = 1,
  246. .fields = (VMStateField[]) {
  247. VMSTATE_BUFFER(data, KBDState),
  248. VMSTATE_INT32(rptr, KBDState),
  249. VMSTATE_INT32(wptr, KBDState),
  250. VMSTATE_INT32(count, KBDState),
  251. VMSTATE_END_OF_LIST()
  252. }
  253. };
  254. static int adb_kbd_reset(ADBDevice *d)
  255. {
  256. KBDState *s = d->opaque;
  257. d->handler = 1;
  258. d->devaddr = ADB_KEYBOARD;
  259. memset(s, 0, sizeof(KBDState));
  260. return 0;
  261. }
  262. void adb_kbd_init(ADBBusState *bus)
  263. {
  264. ADBDevice *d;
  265. KBDState *s;
  266. s = qemu_mallocz(sizeof(KBDState));
  267. d = adb_register_device(bus, ADB_KEYBOARD, adb_kbd_request,
  268. adb_kbd_reset, s);
  269. qemu_add_kbd_event_handler(adb_kbd_put_keycode, d);
  270. vmstate_register(NULL, -1, &vmstate_adb_kbd, s);
  271. }
  272. /***************************************************************/
  273. /* Mouse ADB device */
  274. typedef struct MouseState {
  275. int buttons_state, last_buttons_state;
  276. int dx, dy, dz;
  277. } MouseState;
  278. static void adb_mouse_event(void *opaque,
  279. int dx1, int dy1, int dz1, int buttons_state)
  280. {
  281. ADBDevice *d = opaque;
  282. MouseState *s = d->opaque;
  283. s->dx += dx1;
  284. s->dy += dy1;
  285. s->dz += dz1;
  286. s->buttons_state = buttons_state;
  287. }
  288. static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf)
  289. {
  290. MouseState *s = d->opaque;
  291. int dx, dy;
  292. if (s->last_buttons_state == s->buttons_state &&
  293. s->dx == 0 && s->dy == 0)
  294. return 0;
  295. dx = s->dx;
  296. if (dx < -63)
  297. dx = -63;
  298. else if (dx > 63)
  299. dx = 63;
  300. dy = s->dy;
  301. if (dy < -63)
  302. dy = -63;
  303. else if (dy > 63)
  304. dy = 63;
  305. s->dx -= dx;
  306. s->dy -= dy;
  307. s->last_buttons_state = s->buttons_state;
  308. dx &= 0x7f;
  309. dy &= 0x7f;
  310. if (!(s->buttons_state & MOUSE_EVENT_LBUTTON))
  311. dy |= 0x80;
  312. if (!(s->buttons_state & MOUSE_EVENT_RBUTTON))
  313. dx |= 0x80;
  314. obuf[0] = dy;
  315. obuf[1] = dx;
  316. return 2;
  317. }
  318. static int adb_mouse_request(ADBDevice *d, uint8_t *obuf,
  319. const uint8_t *buf, int len)
  320. {
  321. MouseState *s = d->opaque;
  322. int cmd, reg, olen;
  323. if ((buf[0] & 0x0f) == ADB_FLUSH) {
  324. /* flush mouse fifo */
  325. s->buttons_state = s->last_buttons_state;
  326. s->dx = 0;
  327. s->dy = 0;
  328. s->dz = 0;
  329. return 0;
  330. }
  331. cmd = buf[0] & 0xc;
  332. reg = buf[0] & 0x3;
  333. olen = 0;
  334. switch(cmd) {
  335. case ADB_WRITEREG:
  336. ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]);
  337. switch(reg) {
  338. case 2:
  339. break;
  340. case 3:
  341. switch(buf[2]) {
  342. case ADB_CMD_SELF_TEST:
  343. break;
  344. case ADB_CMD_CHANGE_ID:
  345. case ADB_CMD_CHANGE_ID_AND_ACT:
  346. case ADB_CMD_CHANGE_ID_AND_ENABLE:
  347. d->devaddr = buf[1] & 0xf;
  348. break;
  349. default:
  350. /* XXX: check this */
  351. d->devaddr = buf[1] & 0xf;
  352. break;
  353. }
  354. }
  355. break;
  356. case ADB_READREG:
  357. switch(reg) {
  358. case 0:
  359. olen = adb_mouse_poll(d, obuf);
  360. break;
  361. case 1:
  362. break;
  363. case 3:
  364. obuf[0] = d->handler;
  365. obuf[1] = d->devaddr;
  366. olen = 2;
  367. break;
  368. }
  369. ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg,
  370. obuf[0], obuf[1]);
  371. break;
  372. }
  373. return olen;
  374. }
  375. static int adb_mouse_reset(ADBDevice *d)
  376. {
  377. MouseState *s = d->opaque;
  378. d->handler = 2;
  379. d->devaddr = ADB_MOUSE;
  380. memset(s, 0, sizeof(MouseState));
  381. return 0;
  382. }
  383. static const VMStateDescription vmstate_adb_mouse = {
  384. .name = "adb_mouse",
  385. .version_id = 1,
  386. .minimum_version_id = 1,
  387. .minimum_version_id_old = 1,
  388. .fields = (VMStateField[]) {
  389. VMSTATE_INT32(buttons_state, MouseState),
  390. VMSTATE_INT32(last_buttons_state, MouseState),
  391. VMSTATE_INT32(dx, MouseState),
  392. VMSTATE_INT32(dy, MouseState),
  393. VMSTATE_INT32(dz, MouseState),
  394. VMSTATE_END_OF_LIST()
  395. }
  396. };
  397. void adb_mouse_init(ADBBusState *bus)
  398. {
  399. ADBDevice *d;
  400. MouseState *s;
  401. s = qemu_mallocz(sizeof(MouseState));
  402. d = adb_register_device(bus, ADB_MOUSE, adb_mouse_request,
  403. adb_mouse_reset, s);
  404. qemu_add_mouse_event_handler(adb_mouse_event, d, 0, "QEMU ADB Mouse");
  405. vmstate_register(NULL, -1, &vmstate_adb_mouse, s);
  406. }