PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

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