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