ixp12x0_com.c revision 1.13 1 /* $NetBSD: ixp12x0_com.c,v 1.13 2003/02/22 05:32:00 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 aprint_normal("%s: console\n", sc->sc_dev.dv_xname);
248 }
249
250 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc);
251
252 #if NRND > 0 && defined(RND_COM)
253 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
254 RND_TYPE_TTY, 0);
255 #endif
256
257 /* if there are no enable/disable functions, assume the device
258 is always enabled */
259 if (!sc->enable)
260 sc->enabled = 1;
261
262 /* XXX configure register */
263 /* xxx_config(sc) */
264
265 SET(sc->sc_hwflags, COM_HW_DEV_OK);
266 }
267
268 static int
269 ixpcomparam(tp, t)
270 struct tty *tp;
271 struct termios *t;
272 {
273 struct ixpcom_softc *sc
274 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
275 u_int32_t cr;
276 int s;
277
278 if (COM_ISALIVE(sc) == 0)
279 return (EIO);
280
281 cr = IXPCOMSPEED2BRD(t->c_ospeed);
282
283 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
284 return (EINVAL);
285
286 /*
287 * For the console, always force CLOCAL and !HUPCL, so that the port
288 * is always active.
289 */
290 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
291 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
292 SET(t->c_cflag, CLOCAL);
293 CLR(t->c_cflag, HUPCL);
294 }
295
296 /*
297 * If there were no changes, don't do anything. This avoids dropping
298 * input and improves performance when all we did was frob things like
299 * VMIN and VTIME.
300 */
301 if (tp->t_ospeed == t->c_ospeed &&
302 tp->t_cflag == t->c_cflag)
303 return (0);
304
305 cr |= cflag2cr(t->c_cflag);
306
307 s = splserial();
308 COM_LOCK(sc);
309
310 ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
311
312 /*
313 * ixpcom don't have any hardware flow control.
314 * we skip it.
315 */
316
317 /* And copy to tty. */
318 tp->t_ispeed = 0;
319 tp->t_ospeed = t->c_ospeed;
320 tp->t_cflag = t->c_cflag;
321
322 if (!sc->sc_heldchange) {
323 if (sc->sc_tx_busy) {
324 sc->sc_heldtbc = sc->sc_tbc;
325 sc->sc_tbc = 0;
326 sc->sc_heldchange = 1;
327 } else
328 ixpcom_set_cr(sc);
329 }
330
331 COM_UNLOCK(sc);
332 splx(s);
333
334 /*
335 * Update the tty layer's idea of the carrier bit.
336 * We tell tty the carrier is always on.
337 */
338 (void) (*tp->t_linesw->l_modem)(tp, 1);
339
340 #ifdef COM_DEBUG
341 if (com_debug)
342 comstatus(sc, "comparam ");
343 #endif
344
345 if (!ISSET(t->c_cflag, CHWFLOW)) {
346 if (sc->sc_tx_stopped) {
347 sc->sc_tx_stopped = 0;
348 ixpcomstart(tp);
349 }
350 }
351
352 return (0);
353 }
354
355 static int
356 ixpcomhwiflow(tp, block)
357 struct tty *tp;
358 int block;
359 {
360 return (0);
361 }
362
363 static void
364 ixpcom_filltx(struct ixpcom_softc *sc)
365 {
366 bus_space_tag_t iot = sc->sc_iot;
367 bus_space_handle_t ioh = sc->sc_ioh;
368 int n;
369
370 n = 0;
371 while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
372 if (n >= sc->sc_tbc)
373 break;
374 bus_space_write_4(iot, ioh, IXPCOM_DR,
375 0xff & *(sc->sc_tba + n));
376 n++;
377 }
378 sc->sc_tbc -= n;
379 sc->sc_tba += n;
380 }
381
382 static void
383 ixpcomstart(tp)
384 struct tty *tp;
385 {
386 struct ixpcom_softc *sc
387 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
388 int s;
389
390 if (COM_ISALIVE(sc) == 0)
391 return;
392
393 s = spltty();
394 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
395 goto out;
396 if (sc->sc_tx_stopped)
397 goto out;
398
399 if (tp->t_outq.c_cc <= tp->t_lowat) {
400 if (ISSET(tp->t_state, TS_ASLEEP)) {
401 CLR(tp->t_state, TS_ASLEEP);
402 wakeup(&tp->t_outq);
403 }
404 selwakeup(&tp->t_wsel);
405 if (tp->t_outq.c_cc == 0)
406 goto out;
407 }
408
409 /* Grab the first contiguous region of buffer space. */
410 {
411 u_char *tba;
412 int tbc;
413
414 tba = tp->t_outq.c_cf;
415 tbc = ndqb(&tp->t_outq, 0);
416
417 (void)splserial();
418 COM_LOCK(sc);
419
420 sc->sc_tba = tba;
421 sc->sc_tbc = tbc;
422 }
423
424 SET(tp->t_state, TS_BUSY);
425 sc->sc_tx_busy = 1;
426
427 /* Enable transmit completion interrupts if necessary. */
428 if (!ISSET(sc->sc_xie, CR_XIE)) {
429 SET(sc->sc_xie, CR_XIE);
430 ixpcom_set_cr(sc);
431 }
432
433 /* Output the first chunk of the contiguous buffer. */
434 ixpcom_filltx(sc);
435
436 COM_UNLOCK(sc);
437 out:
438 splx(s);
439 return;
440 }
441
442 static void
443 ixpcom_break(struct ixpcom_softc *sc, int onoff)
444 {
445 if (onoff)
446 SET(ixpcom_cr, CR_BRK);
447 else
448 CLR(ixpcom_cr, CR_BRK);
449 if (!sc->sc_heldchange) {
450 if (sc->sc_tx_busy) {
451 sc->sc_heldtbc = sc->sc_tbc;
452 sc->sc_tbc = 0;
453 sc->sc_heldchange = 1;
454 } else
455 ixpcom_set_cr(sc);
456 }
457 }
458
459 static void
460 ixpcom_shutdown(struct ixpcom_softc *sc)
461 {
462 int s;
463
464 s = splserial();
465 COM_LOCK(sc);
466
467 /* Clear any break condition set with TIOCSBRK. */
468 ixpcom_break(sc, 0);
469
470 /* Turn off interrupts. */
471 sc->sc_rie = sc->sc_xie = 0;
472 ixpcom_set_cr(sc);
473
474 if (sc->disable) {
475 #ifdef DIAGNOSTIC
476 if (!sc->enabled)
477 panic("ixpcom_shutdown: not enabled?");
478 #endif
479 (*sc->disable)(sc);
480 sc->enabled = 0;
481 }
482 COM_UNLOCK(sc);
483 splx(s);
484 }
485
486 int
487 ixpcomopen(dev, flag, mode, p)
488 dev_t dev;
489 int flag, mode;
490 struct proc *p;
491 {
492 struct ixpcom_softc *sc;
493 struct tty *tp;
494 int s, s2;
495 int error;
496
497 sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
498 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
499 sc->sc_rbuf == NULL)
500 return (ENXIO);
501
502 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
503 return (ENXIO);
504
505 #ifdef KGDB
506 /*
507 * If this is the kgdb port, no other use is permitted.
508 */
509 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
510 return (EBUSY);
511 #endif
512
513 tp = sc->sc_tty;
514
515 if (ISSET(tp->t_state, TS_ISOPEN) &&
516 ISSET(tp->t_state, TS_XCLUDE) &&
517 p->p_ucred->cr_uid != 0)
518 return (EBUSY);
519
520 s = spltty();
521
522 /*
523 * Do the following iff this is a first open.
524 */
525 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
526 struct termios t;
527
528 tp->t_dev = dev;
529
530 s2 = splserial();
531 COM_LOCK(sc);
532
533 if (sc->enable) {
534 if ((*sc->enable)(sc)) {
535 COM_UNLOCK(sc);
536 splx(s2);
537 splx(s);
538 printf("%s: device enable failed\n",
539 sc->sc_dev.dv_xname);
540 return (EIO);
541 }
542 sc->enabled = 1;
543 #if 0
544 /* XXXXXXXXXXXXXXX */
545 com_config(sc);
546 #endif
547 }
548
549 /* Turn on interrupts. */
550 SET(sc->sc_rie, CR_RIE);
551 ixpcom_set_cr(sc);
552
553 #if 0
554 /* Fetch the current modem control status, needed later. */
555 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
556
557 /* Clear PPS capture state on first open. */
558 sc->sc_ppsmask = 0;
559 sc->ppsparam.mode = 0;
560 #endif
561
562 COM_UNLOCK(sc);
563 splx(s2);
564
565 /*
566 * Initialize the termios status to the defaults. Add in the
567 * sticky bits from TIOCSFLAGS.
568 */
569 t.c_ispeed = 0;
570 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
571 t.c_ospeed = ixpcomcn_sc.sc_ospeed;
572 t.c_cflag = ixpcomcn_sc.sc_cflag;
573 } else {
574 t.c_ospeed = TTYDEF_SPEED;
575 t.c_cflag = TTYDEF_CFLAG;
576 }
577 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
578 SET(t.c_cflag, CLOCAL);
579 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
580 SET(t.c_cflag, CRTSCTS);
581 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
582 SET(t.c_cflag, MDMBUF);
583 /* Make sure ixpcomparam() will do something. */
584 tp->t_ospeed = 0;
585 (void) ixpcomparam(tp, &t);
586 tp->t_iflag = TTYDEF_IFLAG;
587 tp->t_oflag = TTYDEF_OFLAG;
588 tp->t_lflag = TTYDEF_LFLAG;
589 ttychars(tp);
590 ttsetwater(tp);
591
592 s2 = splserial();
593 COM_LOCK(sc);
594
595 /* Clear the input ring, and unblock. */
596 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
597 sc->sc_rbavail = IXPCOM_RING_SIZE;
598 ixpcom_iflush(sc);
599 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
600
601 #ifdef COM_DEBUG
602 if (ixpcom_debug)
603 comstatus(sc, "ixpcomopen ");
604 #endif
605
606 COM_UNLOCK(sc);
607 splx(s2);
608 }
609
610 splx(s);
611
612 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
613 if (error)
614 goto bad;
615
616 error = (*tp->t_linesw->l_open)(dev, tp);
617 if (error)
618 goto bad;
619
620 return (0);
621
622 bad:
623 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
624 /*
625 * We failed to open the device, and nobody else had it opened.
626 * Clean up the state as appropriate.
627 */
628 ixpcom_shutdown(sc);
629 }
630
631 return (error);
632 }
633
634 int
635 ixpcomclose(dev, flag, mode, p)
636 dev_t dev;
637 int flag, mode;
638 struct proc *p;
639 {
640 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
641 struct tty *tp = sc->sc_tty;
642
643 /* XXX This is for cons.c. */
644 if (!ISSET(tp->t_state, TS_ISOPEN))
645 return (0);
646
647 (*tp->t_linesw->l_close)(tp, flag);
648 ttyclose(tp);
649
650 if (COM_ISALIVE(sc) == 0)
651 return (0);
652
653 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
654 /*
655 * Although we got a last close, the device may still be in
656 * use; e.g. if this was the dialout node, and there are still
657 * processes waiting for carrier on the non-dialout node.
658 */
659 ixpcom_shutdown(sc);
660 }
661
662 return (0);
663 }
664
665 int
666 ixpcomread(dev, uio, flag)
667 dev_t dev;
668 struct uio *uio;
669 int flag;
670 {
671 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
672 struct tty *tp = sc->sc_tty;
673
674 if (COM_ISALIVE(sc) == 0)
675 return (EIO);
676
677 return ((*tp->t_linesw->l_read)(tp, uio, flag));
678 }
679
680 int
681 ixpcomwrite(dev, uio, flag)
682 dev_t dev;
683 struct uio *uio;
684 int flag;
685 {
686 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
687 struct tty *tp = sc->sc_tty;
688
689 if (COM_ISALIVE(sc) == 0)
690 return (EIO);
691
692 return ((*tp->t_linesw->l_write)(tp, uio, flag));
693 }
694
695 int
696 ixpcompoll(dev, events, p)
697 dev_t dev;
698 int events;
699 struct proc *p;
700 {
701 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
702 struct tty *tp = sc->sc_tty;
703
704 if (COM_ISALIVE(sc) == 0)
705 return (EIO);
706
707 return ((*tp->t_linesw->l_poll)(tp, events, p));
708 }
709
710 struct tty *
711 ixpcomtty(dev)
712 dev_t dev;
713 {
714 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
715 struct tty *tp = sc->sc_tty;
716
717 return (tp);
718 }
719
720 int
721 ixpcomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
722 {
723 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
724 struct tty *tp = sc->sc_tty;
725 int error;
726 int s;
727
728 if (COM_ISALIVE(sc) == 0)
729 return (EIO);
730
731 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
732 if (error != EPASSTHROUGH)
733 return (error);
734
735 error = ttioctl(tp, cmd, data, flag, p);
736 if (error != EPASSTHROUGH)
737 return (error);
738
739 error = 0;
740
741 s = splserial();
742 COM_LOCK(sc);
743
744 switch (cmd) {
745 case TIOCSBRK:
746 ixpcom_break(sc, 1);
747 break;
748
749 case TIOCCBRK:
750 ixpcom_break(sc, 0);
751 break;
752
753 case TIOCGFLAGS:
754 *(int *)data = sc->sc_swflags;
755 break;
756
757 case TIOCSFLAGS:
758 error = suser(p->p_ucred, &p->p_acflag);
759 if (error)
760 break;
761 sc->sc_swflags = *(int *)data;
762 break;
763
764 default:
765 error = EPASSTHROUGH;
766 break;
767 }
768
769 COM_UNLOCK(sc);
770 splx(s);
771
772 return (error);
773 }
774
775 /*
776 * Stop output on a line.
777 */
778 void
779 ixpcomstop(tp, flag)
780 struct tty *tp;
781 int flag;
782 {
783 struct ixpcom_softc *sc
784 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
785 int s;
786
787 s = splserial();
788 COM_LOCK(sc);
789 if (ISSET(tp->t_state, TS_BUSY)) {
790 /* Stop transmitting at the next chunk. */
791 sc->sc_tbc = 0;
792 sc->sc_heldtbc = 0;
793 if (!ISSET(tp->t_state, TS_TTSTOP))
794 SET(tp->t_state, TS_FLUSH);
795 }
796 COM_UNLOCK(sc);
797 splx(s);
798 }
799
800 static u_int
801 cflag2cr(cflag)
802 tcflag_t cflag;
803 {
804 u_int cr;
805
806 cr = (cflag & PARENB) ? CR_PE : 0;
807 cr |= (cflag & PARODD) ? CR_OES : 0;
808 cr |= (cflag & CSTOPB) ? CR_SBS : 0;
809 cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
810
811 return (cr);
812 }
813
814 static void
815 ixpcom_iflush(sc)
816 struct ixpcom_softc *sc;
817 {
818 bus_space_tag_t iot = sc->sc_iot;
819 bus_space_handle_t ioh = sc->sc_ioh;
820 #ifdef DIAGNOSTIC
821 int reg;
822 #endif
823 int timo;
824
825 #ifdef DIAGNOSTIC
826 reg = 0xffff;
827 #endif
828 timo = 50000;
829 /* flush any pending I/O */
830 while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
831 && --timo)
832 #ifdef DIAGNOSTIC
833 reg =
834 #else
835 (void)
836 #endif
837 bus_space_read_4(iot, ioh, IXPCOM_DR);
838 #ifdef DIAGNOSTIC
839 if (!timo)
840 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
841 reg);
842 #endif
843 }
844
845 static void
846 ixpcom_set_cr(struct ixpcom_softc *sc)
847 {
848 /* XXX */
849 ixpcom_cr &= ~(CR_RIE | CR_XIE);
850 ixpcom_cr |= sc->sc_rie | sc->sc_xie;
851 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
852 ixpcom_cr & ~ixpcom_imask);
853 }
854
855 int
856 ixpcomcnattach(iot, iobase, ioh, ospeed, cflag)
857 bus_space_tag_t iot;
858 bus_addr_t iobase;
859 bus_space_handle_t ioh;
860 int ospeed;
861 tcflag_t cflag;
862 {
863 int cr;
864
865 cn_tab = &ixpcomcons;
866 cn_init_magic(&ixpcom_cnm_state);
867 /*
868 * XXX
869 *
870 * ixpcom cannot detect a break. It can only detect framing
871 * errors. And, a sequence of "framing error, framing error"
872 * meens a break. So I made this hack:
873 *
874 * 1. Set default magic is a sequence of "BREAK, BREAK".
875 * 2. Tell cn_check_magic() a "framing error" as BREAK.
876 *
877 * see ixpcom_intr() too.
878 *
879 */
880 /* default magic is a couple of BREAK. */
881 cn_set_magic("\047\001\047\001");
882
883 ixpcomcn_sc.sc_iot = iot;
884 ixpcomcn_sc.sc_ioh = ioh;
885 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