dl.c revision 1.47 1 /* $NetBSD: dl.c,v 1.47 2012/10/27 17:18:37 chs Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1997 Ben Harris. All rights reserved.
34 * Copyright (c) 1982, 1986, 1990, 1992, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * Ralph Campbell and Rick Macklem.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 /*
66 * Copyright (c) 1996 Ken C. Wellsch. All rights reserved.
67 *
68 * This code is derived from software contributed to Berkeley by
69 * Ralph Campbell and Rick Macklem.
70 *
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
73 * are met:
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. All advertising materials mentioning features or use of this software
80 * must display the following acknowledgement:
81 * This product includes software developed by the University of
82 * California, Berkeley and its contributors.
83 * 4. Neither the name of the University nor the names of its contributors
84 * may be used to endorse or promote products derived from this software
85 * without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * SUCH DAMAGE.
98 */
99
100 /*
101 * dl.c -- Device driver for the DL11 and DLV11 serial cards.
102 *
103 * OS-interface code derived from the dz and dca (hp300) drivers.
104 */
105
106 #include <sys/cdefs.h>
107 __KERNEL_RCSID(0, "$NetBSD: dl.c,v 1.47 2012/10/27 17:18:37 chs Exp $");
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/ioctl.h>
112 #include <sys/tty.h>
113 #include <sys/proc.h>
114 #include <sys/buf.h>
115 #include <sys/conf.h>
116 #include <sys/file.h>
117 #include <sys/uio.h>
118 #include <sys/kernel.h>
119 #include <sys/syslog.h>
120 #include <sys/device.h>
121 #include <sys/kauth.h>
122
123 #include <sys/bus.h>
124
125 #include <dev/qbus/ubavar.h>
126
127 #include <dev/qbus/dlreg.h>
128
129 #include "ioconf.h"
130
131 struct dl_softc {
132 device_t sc_dev;
133 struct evcnt sc_rintrcnt;
134 struct evcnt sc_tintrcnt;
135 bus_space_tag_t sc_iot;
136 bus_space_handle_t sc_ioh;
137 struct tty *sc_tty;
138 };
139
140 static int dl_match (device_t, cfdata_t, void *);
141 static void dl_attach (device_t, device_t, void *);
142 static void dlrint (void *);
143 static void dlxint (void *);
144 static void dlstart (struct tty *);
145 static int dlparam (struct tty *, struct termios *);
146 static void dlbrk (struct dl_softc *, int);
147
148 CFATTACH_DECL_NEW(dl, sizeof(struct dl_softc),
149 dl_match, dl_attach, NULL, NULL);
150
151 dev_type_open(dlopen);
152 dev_type_close(dlclose);
153 dev_type_read(dlread);
154 dev_type_write(dlwrite);
155 dev_type_ioctl(dlioctl);
156 dev_type_stop(dlstop);
157 dev_type_tty(dltty);
158 dev_type_poll(dlpoll);
159
160 const struct cdevsw dl_cdevsw = {
161 dlopen, dlclose, dlread, dlwrite, dlioctl,
162 dlstop, dltty, dlpoll, nommap, ttykqfilter, D_TTY
163 };
164
165 #define DL_READ_WORD(reg) \
166 bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
167 #define DL_WRITE_WORD(reg, val) \
168 bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
169 #define DL_WRITE_BYTE(reg, val) \
170 bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
171
172 /* Autoconfig handles: setup the controller to interrupt, */
173 /* then complete the housecleaning for full operation */
174
175 static int
176 dl_match (device_t parent, cfdata_t cf, void *aux)
177 {
178 struct uba_attach_args *ua = aux;
179
180 #ifdef DL_DEBUG
181 printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr);
182 #endif
183
184 bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE);
185 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
186 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
187 #ifdef DL_DEBUG
188 printf("failed (step 1; XCSR = %.4b)\n",
189 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
190 DL_XCSR_BITS);
191 #endif
192 return 0;
193 }
194
195 /*
196 * We have to force an interrupt so the uba driver can work
197 * out where we are. Unfortunately, the only way to make a
198 * DL11 interrupt is to get it to send or receive a
199 * character. We'll send a NUL and hope it doesn't hurt
200 * anything.
201 */
202
203 bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0');
204 #if 0 /* This test seems to fail 2/3 of the time :-( */
205 if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) {
206 #ifdef DL_DEBUG
207 printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr,
208 DL_XCSR_BITS);
209 #endif
210 return 0;
211 }
212 #endif
213 DELAY(100000); /* delay 1/10 s for character to transmit */
214 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
215 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
216 #ifdef DL_DEBUG
217 printf("failed (step 3; XCSR = %.4b)\n",
218 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
219 DL_XCSR_BITS);
220 #endif
221 return 0;
222 }
223
224
225 /* What else do I need to do? */
226
227 return 1;
228
229 }
230
231 static void
232 dl_attach (device_t parent, device_t self, void *aux)
233 {
234 struct dl_softc *sc = device_private(self);
235 struct uba_attach_args *ua = aux;
236
237 sc->sc_dev = self;
238 sc->sc_iot = ua->ua_iot;
239 sc->sc_ioh = ua->ua_ioh;
240
241 /* Tidy up the device */
242
243 DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE);
244 DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE);
245
246 /* Initialize our softc structure. Should be done in open? */
247
248 sc->sc_tty = tty_alloc();
249 tty_attach(sc->sc_tty);
250
251 /* Now register the TX & RX interrupt handlers */
252 uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
253 dlxint, sc, &sc->sc_tintrcnt);
254 uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4,
255 dlrint, sc, &sc->sc_rintrcnt);
256 evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
257 device_xname(sc->sc_dev), "rintr");
258 evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
259 device_xname(sc->sc_dev), "tintr");
260
261 printf("\n");
262 }
263
264 /* Receiver Interrupt Handler */
265
266 static void
267 dlrint(void *arg)
268 {
269 struct dl_softc *sc = arg;
270
271 if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
272 struct tty *tp = sc->sc_tty;
273 unsigned c;
274 int cc;
275
276 c = DL_READ_WORD(DL_UBA_RBUF);
277 cc = c & 0xFF;
278
279 if (!(tp->t_state & TS_ISOPEN)) {
280 cv_broadcast(&tp->t_rawcv);
281 return;
282 }
283
284 if (c & DL_RBUF_OVERRUN_ERR) {
285 /*
286 * XXX: This should really be logged somwhere
287 * else where we can afford the time.
288 */
289 log(LOG_WARNING, "%s: rx overrun\n",
290 device_xname(sc->sc_dev));
291 }
292 if (c & DL_RBUF_FRAMING_ERR)
293 cc |= TTY_FE;
294 if (c & DL_RBUF_PARITY_ERR)
295 cc |= TTY_PE;
296
297 (*tp->t_linesw->l_rint)(cc, tp);
298 #if defined(DIAGNOSTIC)
299 } else {
300 log(LOG_WARNING, "%s: stray rx interrupt\n",
301 device_xname(sc->sc_dev));
302 #endif
303 }
304 }
305
306 /* Transmitter Interrupt Handler */
307
308 static void
309 dlxint(void *arg)
310 {
311 struct dl_softc *sc = arg;
312 struct tty *tp = sc->sc_tty;
313
314 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
315 (*tp->t_linesw->l_start)(tp);
316
317 return;
318 }
319
320 int
321 dlopen(dev_t dev, int flag, int mode, struct lwp *l)
322 {
323 struct tty *tp;
324 struct dl_softc *sc;
325 int unit;
326
327 unit = minor(dev);
328
329 sc = device_lookup_private(&dl_cd, unit);
330 if (!sc)
331 return ENXIO;
332
333 tp = sc->sc_tty;
334 if (tp == NULL)
335 return ENODEV;
336 tp->t_oproc = dlstart;
337 tp->t_param = dlparam;
338 tp->t_dev = dev;
339
340 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
341 return (EBUSY);
342
343 if (!(tp->t_state & TS_ISOPEN)) {
344 ttychars(tp);
345 tp->t_iflag = TTYDEF_IFLAG;
346 tp->t_oflag = TTYDEF_OFLAG;
347 /* No modem control, so set CLOCAL. */
348 tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
349 tp->t_lflag = TTYDEF_LFLAG;
350 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
351
352 dlparam(tp, &tp->t_termios);
353 ttsetwater(tp);
354
355 }
356
357 return ((*tp->t_linesw->l_open)(dev, tp));
358 }
359
360 /*ARGSUSED*/
361 int
362 dlclose(dev_t dev, int flag, int mode, struct lwp *l)
363 {
364 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
365 struct tty *tp = sc->sc_tty;
366
367 (*tp->t_linesw->l_close)(tp, flag);
368
369 /* Make sure a BREAK state is not left enabled. */
370 dlbrk(sc, 0);
371
372 return (ttyclose(tp));
373 }
374
375 int
376 dlread(dev_t dev, struct uio *uio, int flag)
377 {
378 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
379 struct tty *tp = sc->sc_tty;
380
381 return ((*tp->t_linesw->l_read)(tp, uio, flag));
382 }
383
384 int
385 dlwrite(dev_t dev, struct uio *uio, int flag)
386 {
387 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
388 struct tty *tp = sc->sc_tty;
389
390 return ((*tp->t_linesw->l_write)(tp, uio, flag));
391 }
392
393 int
394 dlpoll(dev_t dev, int events, struct lwp *l)
395 {
396 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
397 struct tty *tp = sc->sc_tty;
398
399 return ((*tp->t_linesw->l_poll)(tp, events, l));
400 }
401
402 int
403 dlioctl(dev_t dev, unsigned long cmd, void *data, int flag, struct lwp *l)
404 {
405 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
406 struct tty *tp = sc->sc_tty;
407 int error;
408
409
410 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
411 if (error != EPASSTHROUGH)
412 return (error);
413
414 error = ttioctl(tp, cmd, data, flag, l);
415 if (error != EPASSTHROUGH)
416 return (error);
417
418 switch (cmd) {
419
420 case TIOCSBRK:
421 dlbrk(sc, 1);
422 break;
423
424 case TIOCCBRK:
425 dlbrk(sc, 0);
426 break;
427
428 case TIOCMGET:
429 /* No modem control, assume they're all low. */
430 *(int *)data = 0;
431 break;
432
433 default:
434 return (EPASSTHROUGH);
435 }
436 return (0);
437 }
438
439 struct tty *
440 dltty(dev_t dev)
441 {
442 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(dev));
443
444 return sc->sc_tty;
445 }
446
447 void
448 dlstop(struct tty *tp, int flag)
449 {
450 int s = spltty();
451
452 if ((tp->t_state & (TS_BUSY|TS_TTSTOP)) == TS_BUSY)
453 tp->t_state |= TS_FLUSH;
454 splx(s);
455 }
456
457 static void
458 dlstart(struct tty *tp)
459 {
460 struct dl_softc *sc = device_lookup_private(&dl_cd, minor(tp->t_dev));
461 int s = spltty();
462
463 if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
464 goto out;
465 if (!ttypull(tp))
466 goto out;
467 if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
468 tp->t_state |= TS_BUSY;
469 DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
470 }
471 out:
472 splx(s);
473 return;
474 }
475
476 /*ARGSUSED*/
477 static int
478 dlparam(struct tty *tp, struct termios *t)
479 {
480 /*
481 * All this kind of stuff (speed, character format, whatever)
482 * is set by jumpers on the card. Changing it is thus rather
483 * tricky for a mere device driver.
484 */
485 return 0;
486 }
487
488 static void
489 dlbrk(struct dl_softc *sc, int state)
490 {
491 int s = spltty();
492
493 if (state) {
494 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
495 DL_XCSR_TX_BREAK);
496 } else {
497 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
498 ~DL_XCSR_TX_BREAK);
499 }
500 splx(s);
501 }
502