ixp12x0_com.c revision 1.16 1 /* $NetBSD: ixp12x0_com.c,v 1.16 2003/03/25 06:12:46 igy Exp $ */
2 /*
3 * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA and Naoto Shimazaki.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by IWAMOTO Toshihiro.
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Charles M. Hannum.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * Copyright (c) 1991 The Regents of the University of California.
46 * All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * @(#)com.c 7.5 (Berkeley) 5/16/91
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_com.c,v 1.16 2003/03/25 06:12:46 igy Exp $");
81
82 #include "opt_ddb.h"
83 #include "opt_kgdb.h"
84
85 #include "rnd.h"
86 #if NRND > 0 && defined(RND_COM)
87 #include <sys/rnd.h>
88 #endif
89
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/types.h>
93 #include <sys/conf.h>
94 #include <sys/file.h>
95 #include <sys/device.h>
96 #include <sys/kernel.h>
97 #include <sys/malloc.h>
98 #include <sys/tty.h>
99 #include <sys/uio.h>
100 #include <sys/vnode.h>
101
102 #include <machine/intr.h>
103 #include <machine/bus.h>
104
105 #include <arm/ixp12x0/ixp12x0_comreg.h>
106 #include <arm/ixp12x0/ixp12x0_comvar.h>
107 #include <arm/ixp12x0/ixp12x0reg.h>
108 #include <arm/ixp12x0/ixp12x0var.h>
109
110 #include <arm/ixp12x0/ixpsipvar.h>
111
112 #include <dev/cons.h>
113 #include "ixpcom.h"
114
115 static int ixpcomparam(struct tty *, struct termios *);
116 static void ixpcomstart(struct tty *);
117 static int ixpcomhwiflow(struct tty *, int);
118
119 static u_int cflag2cr(tcflag_t);
120 static void ixpcom_iflush(struct ixpcom_softc *);
121 static void ixpcom_set_cr(struct ixpcom_softc *);
122
123 int ixpcomcngetc(dev_t);
124 void ixpcomcnputc(dev_t, int);
125 void ixpcomcnpollc(dev_t, int);
126
127 static void ixpcomsoft(void* arg);
128 inline static void ixpcom_txsoft(struct ixpcom_softc *, struct tty *);
129 inline static void ixpcom_rxsoft(struct ixpcom_softc *, struct tty *);
130
131 void ixpcomcnprobe(struct consdev *);
132 void ixpcomcninit(struct consdev *);
133
134 u_int32_t ixpcom_cr = 0; /* tell cr to *_intr.c */
135 u_int32_t ixpcom_imask = 0; /* intrrupt mask from *_intr.c */
136
137
138 static struct ixpcom_cons_softc {
139 bus_space_tag_t sc_iot;
140 bus_space_handle_t sc_ioh;
141 bus_addr_t sc_baseaddr;
142 int sc_ospeed;
143 tcflag_t sc_cflag;
144 int sc_attached;
145 } ixpcomcn_sc;
146
147 static struct cnm_state ixpcom_cnm_state;
148
149 struct ixpcom_softc* ixpcom_sc = NULL;
150
151 extern struct cfdriver ixpcom_cd;
152
153 dev_type_open(ixpcomopen);
154 dev_type_close(ixpcomclose);
155 dev_type_read(ixpcomread);
156 dev_type_write(ixpcomwrite);
157 dev_type_ioctl(ixpcomioctl);
158 dev_type_stop(ixpcomstop);
159 dev_type_tty(ixpcomtty);
160 dev_type_poll(ixpcompoll);
161
162 const struct cdevsw ixpcom_cdevsw = {
163 ixpcomopen, ixpcomclose, ixpcomread, ixpcomwrite, ixpcomioctl,
164 ixpcomstop, ixpcomtty, ixpcompoll, nommap, ttykqfilter, D_TTY
165 };
166
167 struct consdev ixpcomcons = {
168 NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL,
169 NULL, NULL, NODEV, CN_NORMAL
170 };
171
172 #ifndef DEFAULT_COMSPEED
173 #define DEFAULT_COMSPEED 38400
174 #endif
175
176 #define COMUNIT_MASK 0x7ffff
177 #define COMDIALOUT_MASK 0x80000
178
179 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
180 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
181
182 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
183 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
184
185 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
186
187 #define COM_LOCK(sc);
188 #define COM_UNLOCK(sc);
189
190 #define SET(t, f) (t) |= (f)
191 #define CLR(t, f) (t) &= ~(f)
192 #define ISSET(t, f) ((t) & (f))
193
194 #define CFLAGS2CR_MASK (CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD)
195
196 void
197 ixpcom_attach_subr(sc)
198 struct ixpcom_softc *sc;
199 {
200 struct tty *tp;
201
202 ixpcom_sc = sc;
203
204 /* force to use ixpcom0 for console */
205 if (sc->sc_iot == ixpcomcn_sc.sc_iot
206 && sc->sc_baseaddr == ixpcomcn_sc.sc_baseaddr) {
207 ixpcomcn_sc.sc_attached = 1;
208 sc->sc_speed = IXPCOMSPEED2BRD(ixpcomcn_sc.sc_ospeed);
209
210 /* Make sure the console is always "hardwired". */
211 /* XXX IXPCOM_SR should be checked */
212 delay(10000); /* wait for output to finish */
213 SET(sc->sc_hwflags, COM_HW_CONSOLE);
214 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
215 }
216
217 tp = ttymalloc();
218 tp->t_oproc = ixpcomstart;
219 tp->t_param = ixpcomparam;
220 tp->t_hwiflow = ixpcomhwiflow;
221
222 sc->sc_tty = tp;
223 sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
224 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
225 sc->sc_rbavail = IXPCOM_RING_SIZE;
226 if (sc->sc_rbuf == NULL) {
227 printf("%s: unable to allocate ring buffer\n",
228 sc->sc_dev.dv_xname);
229 return;
230 }
231 sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
232 sc->sc_tbc = 0;
233
234 sc->sc_rie = sc->sc_xie = 0;
235 ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
236 | CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT;
237
238 tty_attach(tp);
239
240 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
241 int maj;
242
243 /* locate the major number */
244 maj = cdevsw_lookup_major(&ixpcom_cdevsw);
245
246 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
247
248 aprint_normal("%s: console\n", sc->sc_dev.dv_xname);
249 }
250
251 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc);
252
253 #if NRND > 0 && defined(RND_COM)
254 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
255 RND_TYPE_TTY, 0);
256 #endif
257
258 /* if there are no enable/disable functions, assume the device
259 is always enabled */
260 if (!sc->enable)
261 sc->enabled = 1;
262
263 /* XXX configure register */
264 /* xxx_config(sc) */
265
266 SET(sc->sc_hwflags, COM_HW_DEV_OK);
267 }
268
269 static int
270 ixpcomparam(tp, t)
271 struct tty *tp;
272 struct termios *t;
273 {
274 struct ixpcom_softc *sc
275 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
276 u_int32_t cr;
277 int s;
278
279 if (COM_ISALIVE(sc) == 0)
280 return (EIO);
281
282 cr = IXPCOMSPEED2BRD(t->c_ospeed);
283
284 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
285 return (EINVAL);
286
287 /*
288 * For the console, always force CLOCAL and !HUPCL, so that the port
289 * is always active.
290 */
291 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
292 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
293 SET(t->c_cflag, CLOCAL);
294 CLR(t->c_cflag, HUPCL);
295 }
296
297 /*
298 * If there were no changes, don't do anything. This avoids dropping
299 * input and improves performance when all we did was frob things like
300 * VMIN and VTIME.
301 */
302 if (tp->t_ospeed == t->c_ospeed &&
303 tp->t_cflag == t->c_cflag)
304 return (0);
305
306 cr |= cflag2cr(t->c_cflag);
307
308 s = splserial();
309 COM_LOCK(sc);
310
311 ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
312
313 /*
314 * ixpcom don't have any hardware flow control.
315 * we skip it.
316 */
317
318 /* And copy to tty. */
319 tp->t_ispeed = 0;
320 tp->t_ospeed = t->c_ospeed;
321 tp->t_cflag = t->c_cflag;
322
323 if (!sc->sc_heldchange) {
324 if (sc->sc_tx_busy) {
325 sc->sc_heldtbc = sc->sc_tbc;
326 sc->sc_tbc = 0;
327 sc->sc_heldchange = 1;
328 } else
329 ixpcom_set_cr(sc);
330 }
331
332 COM_UNLOCK(sc);
333 splx(s);
334
335 /*
336 * Update the tty layer's idea of the carrier bit.
337 * We tell tty the carrier is always on.
338 */
339 (void) (*tp->t_linesw->l_modem)(tp, 1);
340
341 #ifdef COM_DEBUG
342 if (com_debug)
343 comstatus(sc, "comparam ");
344 #endif
345
346 if (!ISSET(t->c_cflag, CHWFLOW)) {
347 if (sc->sc_tx_stopped) {
348 sc->sc_tx_stopped = 0;
349 ixpcomstart(tp);
350 }
351 }
352
353 return (0);
354 }
355
356 static int
357 ixpcomhwiflow(tp, block)
358 struct tty *tp;
359 int block;
360 {
361 return (0);
362 }
363
364 static void
365 ixpcom_filltx(struct ixpcom_softc *sc)
366 {
367 bus_space_tag_t iot = sc->sc_iot;
368 bus_space_handle_t ioh = sc->sc_ioh;
369 int n;
370
371 n = 0;
372 while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
373 if (n >= sc->sc_tbc)
374 break;
375 bus_space_write_4(iot, ioh, IXPCOM_DR,
376 0xff & *(sc->sc_tba + n));
377 n++;
378 }
379 sc->sc_tbc -= n;
380 sc->sc_tba += n;
381 }
382
383 static void
384 ixpcomstart(tp)
385 struct tty *tp;
386 {
387 struct ixpcom_softc *sc
388 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
389 int s;
390
391 if (COM_ISALIVE(sc) == 0)
392 return;
393
394 s = spltty();
395 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
396 goto out;
397 if (sc->sc_tx_stopped)
398 goto out;
399
400 if (tp->t_outq.c_cc <= tp->t_lowat) {
401 if (ISSET(tp->t_state, TS_ASLEEP)) {
402 CLR(tp->t_state, TS_ASLEEP);
403 wakeup(&tp->t_outq);
404 }
405 selwakeup(&tp->t_wsel);
406 if (tp->t_outq.c_cc == 0)
407 goto out;
408 }
409
410 /* Grab the first contiguous region of buffer space. */
411 {
412 u_char *tba;
413 int tbc;
414
415 tba = tp->t_outq.c_cf;
416 tbc = ndqb(&tp->t_outq, 0);
417
418 (void)splserial();
419 COM_LOCK(sc);
420
421 sc->sc_tba = tba;
422 sc->sc_tbc = tbc;
423 }
424
425 SET(tp->t_state, TS_BUSY);
426 sc->sc_tx_busy = 1;
427
428 /* Enable transmit completion interrupts if necessary. */
429 if (!ISSET(sc->sc_xie, CR_XIE)) {
430 SET(sc->sc_xie, CR_XIE);
431 ixpcom_set_cr(sc);
432 }
433
434 /* Output the first chunk of the contiguous buffer. */
435 ixpcom_filltx(sc);
436
437 COM_UNLOCK(sc);
438 out:
439 splx(s);
440 return;
441 }
442
443 static void
444 ixpcom_break(struct ixpcom_softc *sc, int onoff)
445 {
446 if (onoff)
447 SET(ixpcom_cr, CR_BRK);
448 else
449 CLR(ixpcom_cr, CR_BRK);
450 if (!sc->sc_heldchange) {
451 if (sc->sc_tx_busy) {
452 sc->sc_heldtbc = sc->sc_tbc;
453 sc->sc_tbc = 0;
454 sc->sc_heldchange = 1;
455 } else
456 ixpcom_set_cr(sc);
457 }
458 }
459
460 static void
461 ixpcom_shutdown(struct ixpcom_softc *sc)
462 {
463 int s;
464
465 s = splserial();
466 COM_LOCK(sc);
467
468 /* Clear any break condition set with TIOCSBRK. */
469 ixpcom_break(sc, 0);
470
471 /* Turn off interrupts. */
472 sc->sc_rie = sc->sc_xie = 0;
473 ixpcom_set_cr(sc);
474
475 if (sc->disable) {
476 #ifdef DIAGNOSTIC
477 if (!sc->enabled)
478 panic("ixpcom_shutdown: not enabled?");
479 #endif
480 (*sc->disable)(sc);
481 sc->enabled = 0;
482 }
483 COM_UNLOCK(sc);
484 splx(s);
485 }
486
487 int
488 ixpcomopen(dev, flag, mode, p)
489 dev_t dev;
490 int flag, mode;
491 struct proc *p;
492 {
493 struct ixpcom_softc *sc;
494 struct tty *tp;
495 int s, s2;
496 int error;
497
498 sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
499 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
500 sc->sc_rbuf == NULL)
501 return (ENXIO);
502
503 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
504 return (ENXIO);
505
506 #ifdef KGDB
507 /*
508 * If this is the kgdb port, no other use is permitted.
509 */
510 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
511 return (EBUSY);
512 #endif
513
514 tp = sc->sc_tty;
515
516 if (ISSET(tp->t_state, TS_ISOPEN) &&
517 ISSET(tp->t_state, TS_XCLUDE) &&
518 p->p_ucred->cr_uid != 0)
519 return (EBUSY);
520
521 s = spltty();
522
523 /*
524 * Do the following iff this is a first open.
525 */
526 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
527 struct termios t;
528
529 tp->t_dev = dev;
530
531 s2 = splserial();
532 COM_LOCK(sc);
533
534 if (sc->enable) {
535 if ((*sc->enable)(sc)) {
536 COM_UNLOCK(sc);
537 splx(s2);
538 splx(s);
539 printf("%s: device enable failed\n",
540 sc->sc_dev.dv_xname);
541 return (EIO);
542 }
543 sc->enabled = 1;
544 #if 0
545 /* XXXXXXXXXXXXXXX */
546 com_config(sc);
547 #endif
548 }
549
550 /* Turn on interrupts. */
551 SET(sc->sc_rie, CR_RIE);
552 ixpcom_set_cr(sc);
553
554 #if 0
555 /* Fetch the current modem control status, needed later. */
556 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
557
558 /* Clear PPS capture state on first open. */
559 sc->sc_ppsmask = 0;
560 sc->ppsparam.mode = 0;
561 #endif
562
563 COM_UNLOCK(sc);
564 splx(s2);
565
566 /*
567 * Initialize the termios status to the defaults. Add in the
568 * sticky bits from TIOCSFLAGS.
569 */
570 t.c_ispeed = 0;
571 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
572 t.c_ospeed = ixpcomcn_sc.sc_ospeed;
573 t.c_cflag = ixpcomcn_sc.sc_cflag;
574 } else {
575 t.c_ospeed = TTYDEF_SPEED;
576 t.c_cflag = TTYDEF_CFLAG;
577 }
578 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
579 SET(t.c_cflag, CLOCAL);
580 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
581 SET(t.c_cflag, CRTSCTS);
582 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
583 SET(t.c_cflag, MDMBUF);
584 /* Make sure ixpcomparam() will do something. */
585 tp->t_ospeed = 0;
586 (void) ixpcomparam(tp, &t);
587 tp->t_iflag = TTYDEF_IFLAG;
588 tp->t_oflag = TTYDEF_OFLAG;
589 tp->t_lflag = TTYDEF_LFLAG;
590 ttychars(tp);
591 ttsetwater(tp);
592
593 s2 = splserial();
594 COM_LOCK(sc);
595
596 /* Clear the input ring, and unblock. */
597 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
598 sc->sc_rbavail = IXPCOM_RING_SIZE;
599 ixpcom_iflush(sc);
600 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
601
602 #ifdef COM_DEBUG
603 if (ixpcom_debug)
604 comstatus(sc, "ixpcomopen ");
605 #endif
606
607 COM_UNLOCK(sc);
608 splx(s2);
609 }
610
611 splx(s);
612
613 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
614 if (error)
615 goto bad;
616
617 error = (*tp->t_linesw->l_open)(dev, tp);
618 if (error)
619 goto bad;
620
621 return (0);
622
623 bad:
624 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
625 /*
626 * We failed to open the device, and nobody else had it opened.
627 * Clean up the state as appropriate.
628 */
629 ixpcom_shutdown(sc);
630 }
631
632 return (error);
633 }
634
635 int
636 ixpcomclose(dev, flag, mode, p)
637 dev_t dev;
638 int flag, mode;
639 struct proc *p;
640 {
641 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
642 struct tty *tp = sc->sc_tty;
643
644 /* XXX This is for cons.c. */
645 if (!ISSET(tp->t_state, TS_ISOPEN))
646 return (0);
647
648 (*tp->t_linesw->l_close)(tp, flag);
649 ttyclose(tp);
650
651 if (COM_ISALIVE(sc) == 0)
652 return (0);
653
654 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
655 /*
656 * Although we got a last close, the device may still be in
657 * use; e.g. if this was the dialout node, and there are still
658 * processes waiting for carrier on the non-dialout node.
659 */
660 ixpcom_shutdown(sc);
661 }
662
663 return (0);
664 }
665
666 int
667 ixpcomread(dev, uio, flag)
668 dev_t dev;
669 struct uio *uio;
670 int flag;
671 {
672 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
673 struct tty *tp = sc->sc_tty;
674
675 if (COM_ISALIVE(sc) == 0)
676 return (EIO);
677
678 return ((*tp->t_linesw->l_read)(tp, uio, flag));
679 }
680
681 int
682 ixpcomwrite(dev, uio, flag)
683 dev_t dev;
684 struct uio *uio;
685 int flag;
686 {
687 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
688 struct tty *tp = sc->sc_tty;
689
690 if (COM_ISALIVE(sc) == 0)
691 return (EIO);
692
693 return ((*tp->t_linesw->l_write)(tp, uio, flag));
694 }
695
696 int
697 ixpcompoll(dev, events, p)
698 dev_t dev;
699 int events;
700 struct proc *p;
701 {
702 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
703 struct tty *tp = sc->sc_tty;
704
705 if (COM_ISALIVE(sc) == 0)
706 return (EIO);
707
708 return ((*tp->t_linesw->l_poll)(tp, events, p));
709 }
710
711 struct tty *
712 ixpcomtty(dev)
713 dev_t dev;
714 {
715 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
716 struct tty *tp = sc->sc_tty;
717
718 return (tp);
719 }
720
721 int
722 ixpcomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
723 {
724 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
725 struct tty *tp = sc->sc_tty;
726 int error;
727 int s;
728
729 if (COM_ISALIVE(sc) == 0)
730 return (EIO);
731
732 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
733 if (error != EPASSTHROUGH)
734 return (error);
735
736 error = ttioctl(tp, cmd, data, flag, p);
737 if (error != EPASSTHROUGH)
738 return (error);
739
740 error = 0;
741
742 s = splserial();
743 COM_LOCK(sc);
744
745 switch (cmd) {
746 case TIOCSBRK:
747 ixpcom_break(sc, 1);
748 break;
749
750 case TIOCCBRK:
751 ixpcom_break(sc, 0);
752 break;
753
754 case TIOCGFLAGS:
755 *(int *)data = sc->sc_swflags;
756 break;
757
758 case TIOCSFLAGS:
759 error = suser(p->p_ucred, &p->p_acflag);
760 if (error)
761 break;
762 sc->sc_swflags = *(int *)data;
763 break;
764
765 default:
766 error = EPASSTHROUGH;
767 break;
768 }
769
770 COM_UNLOCK(sc);
771 splx(s);
772
773 return (error);
774 }
775
776 /*
777 * Stop output on a line.
778 */
779 void
780 ixpcomstop(tp, flag)
781 struct tty *tp;
782 int flag;
783 {
784 struct ixpcom_softc *sc
785 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
786 int s;
787
788 s = splserial();
789 COM_LOCK(sc);
790 if (ISSET(tp->t_state, TS_BUSY)) {
791 /* Stop transmitting at the next chunk. */
792 sc->sc_tbc = 0;
793 sc->sc_heldtbc = 0;
794 if (!ISSET(tp->t_state, TS_TTSTOP))
795 SET(tp->t_state, TS_FLUSH);
796 }
797 COM_UNLOCK(sc);
798 splx(s);
799 }
800
801 static u_int
802 cflag2cr(cflag)
803 tcflag_t cflag;
804 {
805 u_int cr;
806
807 cr = (cflag & PARENB) ? CR_PE : 0;
808 cr |= (cflag & PARODD) ? CR_OES : 0;
809 cr |= (cflag & CSTOPB) ? CR_SBS : 0;
810 cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
811
812 return (cr);
813 }
814
815 static void
816 ixpcom_iflush(sc)
817 struct ixpcom_softc *sc;
818 {
819 bus_space_tag_t iot = sc->sc_iot;
820 bus_space_handle_t ioh = sc->sc_ioh;
821 #ifdef DIAGNOSTIC
822 int reg;
823 #endif
824 int timo;
825
826 #ifdef DIAGNOSTIC
827 reg = 0xffff;
828 #endif
829 timo = 50000;
830 /* flush any pending I/O */
831 while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
832 && --timo)
833 #ifdef DIAGNOSTIC
834 reg =
835 #else
836 (void)
837 #endif
838 bus_space_read_4(iot, ioh, IXPCOM_DR);
839 #ifdef DIAGNOSTIC
840 if (!timo)
841 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
842 reg);
843 #endif
844 }
845
846 static void
847 ixpcom_set_cr(struct ixpcom_softc *sc)
848 {
849 /* XXX */
850 ixpcom_cr &= ~(CR_RIE | CR_XIE);
851 ixpcom_cr |= sc->sc_rie | sc->sc_xie;
852 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
853 ixpcom_cr & ~ixpcom_imask);
854 }
855
856 int
857 ixpcomcnattach(iot, iobase, ioh, ospeed, cflag)
858 bus_space_tag_t iot;
859 bus_addr_t iobase;
860 bus_space_handle_t ioh;
861 int ospeed;
862 tcflag_t cflag;
863 {
864 int cr;
865
866 cn_tab = &ixpcomcons;
867 cn_init_magic(&ixpcom_cnm_state);
868 /*
869 * XXX
870 *
871 * ixpcom cannot detect a break. It can only detect framing
872 * errors. And, a sequence of "framing error, framing error"
873 * meens a break. So I made this hack:
874 *
875 * 1. Set default magic is a sequence of "BREAK, BREAK".
876 * 2. Tell cn_check_magic() a "framing error" as BREAK.
877 *
878 * see ixpcom_intr() too.
879 *
880 */
881 /* default magic is a couple of BREAK. */
882 cn_set_magic("\047\001\047\001");
883
884 ixpcomcn_sc.sc_iot = iot;
885 ixpcomcn_sc.sc_ioh = ioh;
886 ixpcomcn_sc.sc_baseaddr = iobase;
887 ixpcomcn_sc.sc_ospeed = ospeed;
888 ixpcomcn_sc.sc_cflag = cflag;
889
890 cr = cflag2cr(cflag);
891 cr |= IXPCOMSPEED2BRD(ospeed);
892 cr |= CR_UE;
893 ixpcom_cr = cr;
894
895 /* enable the UART */
896 bus_space_write_4(iot, iobase, IXPCOM_CR, cr);
897
898 return (0);
899 }
900
901 void
902 ixpcomcnprobe(cp)
903 struct consdev *cp;
904 {
905 cp->cn_pri = CN_REMOTE;
906 }
907
908 void
909 ixpcomcnpollc(dev, on)
910 dev_t dev;
911 int on;
912 {
913 }
914
915 void
916 ixpcomcnputc(dev, c)
917 dev_t dev;
918 int c;
919 {
920 int s;
921 bus_space_tag_t iot = ixpcomcn_sc.sc_iot;
922 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh;
923
924 s = splserial();
925
926 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR))
927 ;
928
929 bus_space_write_4(iot, ioh, IXPCOM_DR, c);
930
931 #ifdef DEBUG
932 if (c == '\r') {
933 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXE))
934 ;
935 }
936 #endif
937
938 splx(s);
939 }
940
941 int
942 ixpcomcngetc(dev)
943 dev_t dev;
944 {
945 int c;
946 int s;
947 bus_space_tag_t iot = ixpcomcn_sc.sc_iot;
948 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh;
949
950 s = splserial();
951
952 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_RXR))
953 ;
954
955 c = bus_space_read_4(iot, ioh, IXPCOM_DR);
956 c &= 0xff;
957 splx(s);
958
959 return (c);
960 }
961
962 inline static void
963 ixpcom_txsoft(sc, tp)
964 struct ixpcom_softc *sc;
965 struct tty *tp;
966 {
967 CLR(tp->t_state, TS_BUSY);
968 if (ISSET(tp->t_state, TS_FLUSH))
969 CLR(tp->t_state, TS_FLUSH);
970 else
971 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
972 (*tp->t_linesw->l_start)(tp);
973 }
974
975 inline static void
976 ixpcom_rxsoft(sc, tp)
977 struct ixpcom_softc *sc;
978 struct tty *tp;
979 {
980 int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint;
981 u_char *get, *end;
982 u_int cc, scc;
983 u_char lsr;
984 int code;
985 int s;
986
987 end = sc->sc_ebuf;
988 get = sc->sc_rbget;
989 scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
990 #if 0
991 if (cc == IXPCOM_RING_SIZE) {
992 sc->sc_floods++;
993 if (sc->sc_errors++ == 0)
994 callout_reset(&sc->sc_diag_callout, 60 * hz,
995 comdiag, sc);
996 }
997 #endif
998 while (cc) {
999 code = get[0];
1000 lsr = get[1];
1001 if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) {
1002 #if 0
1003 if (ISSET(lsr, DR_ROR)) {
1004 sc->sc_overflows++;
1005 if (sc->sc_errors++ == 0)
1006 callout_reset(&sc->sc_diag_callout,
1007 60 * hz, comdiag, sc);
1008 }
1009 #endif
1010 if (ISSET(lsr, DR_FRE))
1011 SET(code, TTY_FE);
1012 if (ISSET(lsr, DR_PRE))
1013 SET(code, TTY_PE);
1014 }
1015 if ((*rint)(code, tp) == -1) {
1016 /*
1017 * The line discipline's buffer is out of space.
1018 */
1019 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1020 /*
1021 * We're either not using flow control, or the
1022 * line discipline didn't tell us to block for
1023 * some reason. Either way, we have no way to
1024 * know when there's more space available, so
1025 * just drop the rest of the data.
1026 */
1027 get += cc << 1;
1028 if (get >= end)
1029 get -= IXPCOM_RING_SIZE << 1;
1030 cc = 0;
1031 } else {
1032 /*
1033 * Don't schedule any more receive processing
1034 * until the line discipline tells us there's
1035 * space available (through comhwiflow()).
1036 * Leave the rest of the data in the input
1037 * buffer.
1038 */
1039 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1040 }
1041 break;
1042 }
1043 get += 2;
1044 if (get >= end)
1045 get = sc->sc_rbuf;
1046 cc--;
1047 }
1048
1049 if (cc != scc) {
1050 sc->sc_rbget = get;
1051 s = splserial();
1052 COM_LOCK(sc);
1053
1054 cc = sc->sc_rbavail += scc - cc;
1055 /* Buffers should be ok again, release possible block. */
1056 if (cc >= 1) {
1057 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1058 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1059 SET(sc->sc_rie, CR_RIE);
1060 ixpcom_set_cr(sc);
1061 }
1062 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1063 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1064 #if 0
1065 com_hwiflow(sc);
1066 #endif
1067 }
1068 }
1069 COM_UNLOCK(sc);
1070 splx(s);
1071 }
1072 }
1073
1074 static void
1075 ixpcomsoft(void* arg)
1076 {
1077 struct ixpcom_softc *sc = arg;
1078
1079 if (COM_ISALIVE(sc) == 0)
1080 return;
1081
1082 if (sc->sc_rx_ready) {
1083 sc->sc_rx_ready = 0;
1084 ixpcom_rxsoft(sc, sc->sc_tty);
1085 }
1086 if (sc->sc_tx_done) {
1087 sc->sc_tx_done = 0;
1088 ixpcom_txsoft(sc, sc->sc_tty);
1089 }
1090 }
1091
1092 int
1093 ixpcomintr(void* arg)
1094 {
1095 struct ixpcom_softc *sc = arg;
1096 bus_space_tag_t iot = sc->sc_iot;
1097 bus_space_handle_t ioh = sc->sc_ioh;
1098 u_char *put, *end;
1099 u_int cc;
1100 u_int cr;
1101 u_int sr;
1102 u_int32_t c;
1103
1104 if (COM_ISALIVE(sc) == 0)
1105 return (0);
1106
1107 COM_LOCK(sc);
1108 cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE;
1109
1110 if (!cr) {
1111 COM_UNLOCK(sc);
1112 return (0);
1113 }
1114
1115 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1116 if (!ISSET(sr, SR_TXR | SR_RXR))
1117 return (0);
1118
1119 /*
1120 * XXX
1121 *
1122 * ixpcom cannot detect a break. It can only detect framing
1123 * errors. And, a sequence of "framing error, framing error"
1124 * meens a break. So I made this hack:
1125 *
1126 * 1. Set default magic is a sequence of "BREAK, BREAK".
1127 * 2. Tell cn_check_magic() a "framing error" as BREAK.
1128 *
1129 * see ixpcomcnattach() too.
1130 *
1131 */
1132 if (ISSET(sr, SR_FRE)) {
1133 cn_check_magic(sc->sc_tty->t_dev,
1134 CNC_BREAK, ixpcom_cnm_state);
1135 }
1136
1137 end = sc->sc_ebuf;
1138 put = sc->sc_rbput;
1139 cc = sc->sc_rbavail;
1140
1141 if (ISSET(sr, SR_RXR)) {
1142 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1143 while (cc > 0) {
1144 if (!ISSET(sr, SR_RXR))
1145 break;
1146 c = bus_space_read_4(iot, ioh, IXPCOM_DR);
1147 if (ISSET(c, DR_FRE)) {
1148 cn_check_magic(sc->sc_tty->t_dev,
1149 CNC_BREAK,
1150 ixpcom_cnm_state);
1151 }
1152 put[0] = c & 0xff;
1153 put[1] = (c >> 8) & 0xff;
1154 cn_check_magic(sc->sc_tty->t_dev,
1155 put[0], ixpcom_cnm_state);
1156 put += 2;
1157 if (put >= end)
1158 put = sc->sc_rbuf;
1159 cc--;
1160
1161 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1162 }
1163
1164 /*
1165 * Current string of incoming characters ended because
1166 * no more data was available or we ran out of space.
1167 * Schedule a receive event if any data was received.
1168 * If we're out of space, turn off receive interrupts.
1169 */
1170 sc->sc_rbput = put;
1171 sc->sc_rbavail = cc;
1172 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1173 sc->sc_rx_ready = 1;
1174
1175 /*
1176 * See if we are in danger of overflowing a buffer. If
1177 * so, use hardware flow control to ease the pressure.
1178 */
1179
1180 /* but ixpcom cannot. X-( */
1181
1182 /*
1183 * If we're out of space, disable receive interrupts
1184 * until the queue has drained a bit.
1185 */
1186 if (!cc) {
1187 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1188 CLR(sc->sc_rie, CR_RIE);
1189 ixpcom_set_cr(sc);
1190 }
1191 } else {
1192 #ifdef DIAGNOSTIC
1193 panic("ixpcomintr: we shouldn't reach here");
1194 #endif
1195 CLR(sc->sc_rie, CR_RIE);
1196 ixpcom_set_cr(sc);
1197 }
1198 }
1199
1200 /*
1201 * Done handling any receive interrupts. See if data can be
1202 * transmitted as well. Schedule tx done event if no data left
1203 * and tty was marked busy.
1204 */
1205 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1206 if (ISSET(sr, SR_TXR)) {
1207 /*
1208 * If we've delayed a parameter change, do it now, and restart
1209 * output.
1210 */
1211 if (sc->sc_heldchange) {
1212 ixpcom_set_cr(sc);
1213 sc->sc_heldchange = 0;
1214 sc->sc_tbc = sc->sc_heldtbc;
1215 sc->sc_heldtbc = 0;
1216 }
1217
1218 /* Output the next chunk of the contiguous buffer, if any. */
1219 if (sc->sc_tbc > 0) {
1220 ixpcom_filltx(sc);
1221 } else {
1222 /* Disable transmit completion interrupts if necessary. */
1223 if (ISSET(sc->sc_xie, CR_XIE)) {
1224 CLR(sc->sc_xie, CR_XIE);
1225 ixpcom_set_cr(sc);
1226 }
1227 if (sc->sc_tx_busy) {
1228 sc->sc_tx_busy = 0;
1229 sc->sc_tx_done = 1;
1230 }
1231 }
1232 }
1233 COM_UNLOCK(sc);
1234
1235 /* Wake up the poller. */
1236 softintr_schedule(sc->sc_si);
1237
1238 #if NRND > 0 && defined(RND_COM)
1239 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1240 #endif
1241 return (1);
1242 }
1243