PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/netbsd/src/sys/arch/pmax/tc/dt.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 485 lines | 273 code | 64 blank | 148 comment | 70 complexity | 36646416b3a78f319902297d495b140c MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* $NetBSD: dt.c,v 1.11 2011/06/04 01:37:36 tsutsui Exp $ */
  2. /*-
  3. * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Andrew Doran.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*-
  31. * Copyright (c) 1992, 1993
  32. * The Regents of the University of California. All rights reserved.
  33. *
  34. * This code is derived from software contributed to Berkeley by
  35. * Ralph Campbell and Rick Macklem.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. * 1. Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * 2. Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in the
  44. * documentation and/or other materials provided with the distribution.
  45. * 3. Neither the name of the University nor the names of its contributors
  46. * may be used to endorse or promote products derived from this software
  47. * without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. * SUCH DAMAGE.
  60. *
  61. * @(#)dtop.c 8.2 (Berkeley) 11/30/93
  62. */
  63. /*
  64. * Mach Operating System
  65. * Copyright (c) 1991,1990,1989 Carnegie Mellon University
  66. * All Rights Reserved.
  67. *
  68. * Permission to use, copy, modify and distribute this software and its
  69. * documentation is hereby granted, provided that both the copyright
  70. * notice and this permission notice appear in all copies of the
  71. * software, derivative works or modified versions, and any portions
  72. * thereof, and that both notices appear in supporting documentation.
  73. *
  74. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  75. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  76. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  77. *
  78. * Carnegie Mellon requests users of this software to return to
  79. *
  80. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  81. * School of Computer Science
  82. * Carnegie Mellon University
  83. * Pittsburgh PA 15213-3890
  84. *
  85. * any improvements or extensions that they make and grant Carnegie the
  86. * rights to redistribute these changes.
  87. */
  88. /*
  89. * Author: Alessandro Forin, Carnegie Mellon University
  90. *
  91. * Hardware-level operations for the Desktop serial line
  92. * bus (i2c aka ACCESS).
  93. */
  94. /************************************************************
  95. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  96. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  97. All Rights Reserved
  98. Permission to use, copy, modify, and distribute this software and its
  99. documentation for any purpose and without fee is hereby granted,
  100. provided that the above copyright notice appear in all copies and that
  101. both that copyright notice and this permission notice appear in
  102. supporting documentation, and that the names of Digital or MIT not be
  103. used in advertising or publicity pertaining to distribution of the
  104. software without specific, written prior permission.
  105. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  106. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  107. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  108. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  109. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  110. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  111. SOFTWARE.
  112. ********************************************************/
  113. /*
  114. * ACCESS.bus device support for the Personal DECstation. This code handles
  115. * only the keyboard and mouse, and will likely not work if other ACCESS.bus
  116. * devices are physically attached to the system.
  117. *
  118. * Since we do not know how to drive the hardware (the only reference being
  119. * Mach), we can't identify which devices are connected to the system by
  120. * sending idenfication requests. With only a mouse and keyboard attached
  121. * to the system, we do know which two slave addresses will be in use.
  122. * However, we don't know which is the mouse, and which is the keyboard.
  123. * So, we resort to inspecting device reports and making an educated guess
  124. * as to which is which.
  125. */
  126. #include <sys/cdefs.h>
  127. __KERNEL_RCSID(0, "$NetBSD: dt.c,v 1.11 2011/06/04 01:37:36 tsutsui Exp $");
  128. #include <sys/param.h>
  129. #include <sys/systm.h>
  130. #include <sys/tty.h>
  131. #include <sys/proc.h>
  132. #include <sys/conf.h>
  133. #include <sys/file.h>
  134. #include <sys/kernel.h>
  135. #include <sys/device.h>
  136. #include <sys/malloc.h>
  137. #include <sys/intr.h>
  138. #include <dev/dec/lk201.h>
  139. #include <dev/tc/tcvar.h>
  140. #include <dev/tc/ioasicreg.h>
  141. #include <dev/tc/ioasicvar.h>
  142. #include <pmax/pmax/maxine.h>
  143. #include <pmax/tc/dtreg.h>
  144. #include <pmax/tc/dtvar.h>
  145. #define DT_BUF_CNT 16
  146. #define DT_ESC_CHAR 0xf8
  147. #define DT_XMT_OK 0xfb
  148. #define DT_MAX_POLL 0x70000 /* about half a sec */
  149. #define DT_GET_BYTE(data) (((*(data)) >> 8) & 0xff)
  150. #define DT_PUT_BYTE(data,c) { *(data) = (c) << 8; wbflush(); }
  151. #define DT_RX_AVAIL(poll) ((*(poll) & 1) != 0)
  152. #define DT_TX_AVAIL(poll) ((*(poll) & 2) != 0)
  153. int dt_match(device_t, cfdata_t, void *);
  154. void dt_attach(device_t, device_t, void *);
  155. int dt_intr(void *);
  156. int dt_print(void *, const char *);
  157. void dt_strvis(uint8_t *, char *, int);
  158. void dt_dispatch(void *);
  159. int dt_kbd_addr = DT_ADDR_KBD;
  160. struct dt_device dt_kbd_dv;
  161. int dt_ms_addr = DT_ADDR_MOUSE;
  162. struct dt_device dt_ms_dv;
  163. struct dt_state dt_state;
  164. CFATTACH_DECL_NEW(dt, sizeof(struct dt_softc),
  165. dt_match, dt_attach, NULL, NULL);
  166. int
  167. dt_match(device_t parent, cfdata_t cf, void *aux)
  168. {
  169. struct ioasicdev_attach_args *d;
  170. d = aux;
  171. if (strcmp(d->iada_modname, "dtop") != 0)
  172. return (0);
  173. if (badaddr((void *)(d->iada_addr), 2))
  174. return (0);
  175. return (1);
  176. }
  177. void
  178. dt_attach(device_t parent, device_t self, void *aux)
  179. {
  180. struct ioasicdev_attach_args *d;
  181. struct dt_attach_args dta;
  182. struct dt_softc *sc;
  183. struct dt_msg *msg;
  184. int i;
  185. d = aux;
  186. sc = device_private(self);
  187. sc->sc_dev = self;
  188. dt_cninit();
  189. msg = malloc(sizeof(*msg) * DT_BUF_CNT, M_DEVBUF, M_NOWAIT);
  190. if (msg == NULL) {
  191. printf("%s: memory exhausted\n", device_xname(self));
  192. return;
  193. }
  194. sc->sc_sih = softint_establish(SOFTINT_SERIAL, dt_dispatch, sc);
  195. if (sc->sc_sih == NULL) {
  196. printf("%s: memory exhausted\n", device_xname(self));
  197. free(msg, M_DEVBUF);
  198. }
  199. SIMPLEQ_INIT(&sc->sc_queue);
  200. SLIST_INIT(&sc->sc_free);
  201. for (i = 0; i < DT_BUF_CNT; i++, msg++)
  202. SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
  203. ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY, dt_intr, sc);
  204. printf("\n");
  205. dta.dta_addr = DT_ADDR_KBD;
  206. config_found(self, &dta, dt_print);
  207. dta.dta_addr = DT_ADDR_MOUSE;
  208. config_found(self, &dta, dt_print);
  209. }
  210. void
  211. dt_cninit(void)
  212. {
  213. dt_state.ds_poll = (volatile u_int *)
  214. MIPS_PHYS_TO_KSEG1(XINE_REG_INTR);
  215. dt_state.ds_data = (volatile u_int *)
  216. MIPS_PHYS_TO_KSEG1(XINE_PHYS_TC_3_START + 0x280000);
  217. }
  218. int
  219. dt_print(void *aux, const char *pnp)
  220. {
  221. return (QUIET);
  222. }
  223. int
  224. dt_establish_handler(struct dt_softc *sc, struct dt_device *dtdv,
  225. void *arg, void (*hdlr)(void *, struct dt_msg *))
  226. {
  227. dtdv->dtdv_arg = arg;
  228. dtdv->dtdv_handler = hdlr;
  229. return (0);
  230. }
  231. int
  232. dt_intr(void *cookie)
  233. {
  234. struct dt_softc *sc;
  235. struct dt_msg *msg, *pend;
  236. sc = cookie;
  237. switch (dt_msg_get(&sc->sc_msg, 1)) {
  238. case DT_GET_ERROR:
  239. /*
  240. * Ugh! The most common occurrence of a data overrun is upon
  241. * a key press and the result is a software generated "stuck
  242. * key". All I can think to do is fake an "all keys up"
  243. * whenever a data overrun occurs.
  244. */
  245. sc->sc_msg.src = dt_kbd_addr;
  246. sc->sc_msg.ctl = DT_CTL(1, 0, 0);
  247. sc->sc_msg.body[0] = DT_KBD_EMPTY;
  248. #ifdef DIAGNOSTIC
  249. printf("%s: data overrun or stray interrupt\n",
  250. device_xname(sc->sc_dev));
  251. #endif
  252. break;
  253. case DT_GET_DONE:
  254. break;
  255. case DT_GET_NOTYET:
  256. return (1);
  257. }
  258. if ((msg = SLIST_FIRST(&sc->sc_free)) == NULL) {
  259. printf("%s: input overflow\n", device_xname(sc->sc_dev));
  260. return (1);
  261. }
  262. SLIST_REMOVE_HEAD(&sc->sc_free, chain.slist);
  263. memcpy(msg, &sc->sc_msg, sizeof(*msg));
  264. pend = SIMPLEQ_FIRST(&sc->sc_queue);
  265. SIMPLEQ_INSERT_TAIL(&sc->sc_queue, msg, chain.simpleq);
  266. if (pend == NULL)
  267. softint_schedule(sc->sc_sih);
  268. return (1);
  269. }
  270. void
  271. dt_dispatch(void *cookie)
  272. {
  273. struct dt_softc *sc;
  274. struct dt_msg *msg;
  275. int s;
  276. struct dt_device *dtdv;
  277. sc = cookie;
  278. msg = NULL;
  279. for (;;) {
  280. s = spltty();
  281. if (msg != NULL)
  282. SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
  283. msg = SIMPLEQ_FIRST(&sc->sc_queue);
  284. if (msg != NULL)
  285. SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, chain.simpleq);
  286. splx(s);
  287. if (msg == NULL)
  288. break;
  289. if (msg->src != DT_ADDR_MOUSE && msg->src != DT_ADDR_KBD) {
  290. printf("%s: message from unknown dev 0x%x\n",
  291. device_xname(sc->sc_dev), sc->sc_msg.src);
  292. dt_msg_dump(msg);
  293. continue;
  294. }
  295. if (DT_CTL_P(msg->ctl) != 0) {
  296. printf("%s: received control message\n",
  297. device_xname(sc->sc_dev));
  298. dt_msg_dump(msg);
  299. continue;
  300. }
  301. /*
  302. * 1. Mouse should have no more than eight buttons, so first
  303. * 8 bits of body will be zero.
  304. * 2. Mouse should always send full locator report.
  305. * Note: my mouse does not send 'z' data, so the size
  306. * did not match the size of struct dt_locator_msg - mhitch
  307. * 3. Keyboard should never report all-up (0x00) in
  308. * a packet with size > 1.
  309. */
  310. if (DT_CTL_LEN(msg->ctl) >= 6 &&
  311. msg->body[0] == 0 && msg->src != dt_ms_addr) {
  312. dt_kbd_addr = dt_ms_addr;
  313. dt_ms_addr = msg->src;
  314. } else if (DT_CTL_LEN(msg->ctl) < 6 && msg->body[0] != 0 &&
  315. msg->src != dt_kbd_addr) {
  316. dt_ms_addr = dt_kbd_addr;
  317. dt_kbd_addr = msg->src;
  318. }
  319. if (msg->src == dt_kbd_addr)
  320. dtdv = &dt_kbd_dv;
  321. else
  322. dtdv = &dt_ms_dv;
  323. if (dtdv->dtdv_handler != NULL)
  324. (*dtdv->dtdv_handler)(dtdv->dtdv_arg, msg);
  325. }
  326. }
  327. int
  328. dt_msg_get(struct dt_msg *msg, int intr)
  329. {
  330. volatile u_int *poll, *data;
  331. uint8_t c;
  332. int max_polls;
  333. poll = dt_state.ds_poll;
  334. data = dt_state.ds_data;
  335. /*
  336. * The interface does not hand us the first byte, which is our
  337. * address and cannot ever be anything else but 0x50.
  338. */
  339. if (dt_state.ds_state == 0) {
  340. dt_state.ds_escaped = 0;
  341. dt_state.ds_ptr = 0;
  342. }
  343. for (;;) {
  344. max_polls = DT_MAX_POLL;
  345. while (!DT_RX_AVAIL(poll)) {
  346. if (intr)
  347. return (DT_GET_NOTYET);
  348. if (max_polls-- <= 0)
  349. break;
  350. DELAY(1);
  351. }
  352. if (max_polls <= 0) {
  353. if (dt_state.ds_state != 0) {
  354. dt_state.ds_bad_pkts++;
  355. dt_state.ds_state = 0;
  356. }
  357. return (DT_GET_ERROR);
  358. }
  359. c = DT_GET_BYTE(data);
  360. if (dt_state.ds_escaped) {
  361. switch (c) {
  362. case 0xe8:
  363. case 0xe9:
  364. case 0xea:
  365. case 0xeb:
  366. c += 0x10;
  367. break;
  368. }
  369. if (c == 'O') {
  370. dt_state.ds_bad_pkts++;
  371. dt_state.ds_state = 0;
  372. return (DT_GET_ERROR);
  373. }
  374. dt_state.ds_escaped = 0;
  375. } else if (c == DT_ESC_CHAR) {
  376. dt_state.ds_escaped = 1;
  377. continue;
  378. }
  379. if (dt_state.ds_state == 0) {
  380. msg->src = c;
  381. dt_state.ds_state = 1;
  382. } else if (dt_state.ds_state == 1) {
  383. msg->ctl = c;
  384. dt_state.ds_state = 2;
  385. dt_state.ds_len = DT_CTL_LEN(msg->ctl) + 1;
  386. if (dt_state.ds_len > sizeof(msg->body))
  387. printf("dt_msg_get: msg truncated: %d\n",
  388. dt_state.ds_len);
  389. } else /* if (dt_state.ds_state == 2) */ {
  390. if (dt_state.ds_ptr < sizeof(msg->body))
  391. msg->body[dt_state.ds_ptr++] = c;
  392. if (dt_state.ds_ptr >= dt_state.ds_len)
  393. break;
  394. }
  395. }
  396. msg->dst = DT_ADDR_HOST;
  397. dt_state.ds_state = 0;
  398. return (DT_GET_DONE);
  399. }
  400. void
  401. dt_msg_dump(struct dt_msg *msg)
  402. {
  403. int i, l;
  404. l = DT_CTL_LEN(msg->ctl);
  405. printf("hdr: dst=%02x src=%02x p=%02x sub=%02x len=%02x\n",
  406. msg->dst, msg->src, DT_CTL_P(msg->ctl), DT_CTL_SUBADDR(msg->ctl),
  407. l);
  408. printf("body: ");
  409. for (i = 0; i < l && i < 20; i++)
  410. printf("%02x ", msg->body[i]);
  411. if (i < l) {
  412. printf("\n");
  413. for (; i < l; i++)
  414. printf("%02x ", msg->body[i]);
  415. }
  416. printf("\n");
  417. }