adb.c 12 KB

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