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