kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / cx / cx.c
1 /*
2  * Cronyx-Sigma adapter driver for FreeBSD.
3  * Supports PPP/HDLC protocol in synchronous mode,
4  * and asyncronous channels with full modem control.
5  *
6  * Copyright (C) 1994 Cronyx Ltd.
7  * Author: Serge Vakulenko, <vak@zebub.msk.su>
8  *
9  * This software is distributed with NO WARRANTIES, not even the implied
10  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Authors grant any other persons or organisations permission to use
13  * or modify this software as long as this message is kept with the software,
14  * all derivative works or modified versions.
15  *
16  * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
17  *
18  * $FreeBSD: src/sys/i386/isa/cx.c,v 1.45.2.1 2001/02/26 04:23:09 jlemon Exp $
19  * $DragonFly: src/sys/dev/netif/cx/cx.c,v 1.7 2003/08/07 21:17:00 dillon Exp $
20  *
21  */
22 #undef DEBUG
23
24 #include "use_cx.h"
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/fcntl.h>
30 #include <sys/conf.h>
31 #include <sys/proc.h>
32 #include <sys/tty.h>
33 #include <sys/socket.h>
34 #include <net/if.h>
35
36 #ifdef __FreeBSD__
37 #   if __FreeBSD__ < 2
38 #      include <machine/pio.h>
39 #      define RB_GETC(q) getc(q)
40 #   endif
41 #endif
42 #ifdef __bsdi__
43 #   include <sys/ttystats.h>
44 #   include <machine/inline.h>
45 #   define tsleep(tp,pri,msg,x) ((tp)->t_state |= TS_WOPEN,\
46                 ttysleep (tp, (caddr_t)&tp->t_rawq, pri, msg, x))
47 #endif
48 #if !defined (__FreeBSD__) || __FreeBSD__ >= 2
49 #      define t_out t_outq
50 #      define RB_LEN(q) ((q).c_cc)
51 #      define RB_GETC(q) getc(&q)
52 #ifndef TSA_CARR_ON /* FreeBSD 2.x before not long after 2.0.5 */
53 #      define TSA_CARR_ON(tp) tp
54 #      define TSA_OLOWAT(q) ((caddr_t)&(q)->t_out)
55 #endif
56 #endif
57
58 #include <machine/cronyx.h>
59 #include "cxreg.h"
60
61 /* XXX imported from if_cx.c. */
62 void cxswitch (cx_chan_t *c, cx_soft_opt_t new);
63
64 /* XXX exported. */
65 void cxmint (cx_chan_t *c);
66 int cxrinta (cx_chan_t *c);
67 void cxtinta (cx_chan_t *c);
68 timeout_t cxtimeout;
69
70 #ifdef DEBUG
71 #   define print(s)     printf s
72 #else
73 #   define print(s)     {/*void*/}
74 #endif
75
76 #define DMABUFSZ        (6*256)         /* buffer size */
77 #define BYTE            *(unsigned char*)&
78 #define UNIT(u)         (minor(u) & 077)
79 #define UNIT_CTL        077
80
81 extern cx_board_t cxboard [NCX];        /* adapter state structures */
82 extern cx_chan_t *cxchan [NCX*NCHAN];   /* unit to channel struct pointer */
83 #if __FreeBSD__ >= 2
84 static struct tty cx_tty [NCX*NCHAN];          /* tty data */
85
86 static  d_open_t        cxopen;
87 static  d_close_t       cxclose;
88 static  d_ioctl_t       cxioctl;
89
90 #define CDEV_MAJOR      42
91 /* Don't make this static, since if_cx.c uses it. */
92 struct cdevsw cx_cdevsw = {
93         /* name */      "cx",
94         /* maj */       CDEV_MAJOR,
95         /* flags */     D_TTY | D_KQFILTER,
96         /* port */      NULL,
97         /* autoq */     0,
98
99         /* open */      cxopen,
100         /* close */     cxclose,
101         /* read */      ttyread,
102         /* write */     ttywrite,
103         /* ioctl */     cxioctl,
104         /* poll */      ttypoll,
105         /* mmap */      nommap,
106         /* strategy */  nostrategy,
107         /* dump */      nodump,
108         /* psize */     nopsize,
109         /* kqfilter */  ttykqfilter
110 };
111 #else
112 struct tty *cx_tty [NCX*NCHAN];         /* tty data */
113 #endif
114
115 static void cxoproc (struct tty *tp);
116 static void cxstop (struct tty *tp, int flag);
117 static int cxparam (struct tty *tp, struct termios *t);
118
119 int cxopen (dev_t dev, int flag, int mode, struct thread *td)
120 {
121         int unit = UNIT (dev);
122         cx_chan_t *c = cxchan[unit];
123         unsigned short port;
124         struct tty *tp;
125         int error = 0;
126
127         if (unit == UNIT_CTL) {
128                 print (("cx: cxopen /dev/cronyx\n"));
129                 return (0);
130         }
131         if (unit >= NCX*NCHAN || !c || c->type==T_NONE)
132                 return (ENXIO);
133         port = c->chip->port;
134         print (("cx%d.%d: cxopen unit=%d\n", c->board->num, c->num, unit));
135         if (c->mode != M_ASYNC)
136                 return (EBUSY);
137         if (! c->ttyp) {
138 #ifdef __FreeBSD__
139 #if __FreeBSD__ >= 2
140                 c->ttyp = &cx_tty[unit];
141 #else
142                 c->ttyp = cx_tty[unit] = ttymalloc (cx_tty[unit]);
143 #endif
144 #else
145                 MALLOC (cx_tty[unit], struct tty*, sizeof (struct tty), M_DEVBUF, M_WAITOK);
146                 bzero (cx_tty[unit], sizeof (*cx_tty[unit]));
147                 c->ttyp = cx_tty[unit];
148 #endif
149                 c->ttyp->t_oproc = cxoproc;
150                 c->ttyp->t_stop = cxstop;
151                 c->ttyp->t_param = cxparam;
152         }
153         dev->si_tty = c->ttyp;
154 #ifdef __bsdi__
155         if (! c->ttydev) {
156                 MALLOC (c->ttydev, struct ttydevice_tmp*,
157                         sizeof (struct ttydevice_tmp), M_DEVBUF, M_WAITOK);
158                 bzero (c->ttydev, sizeof (*c->ttydev));
159                 strcpy (c->ttydev->tty_name, "cx");
160                 c->ttydev->tty_unit = unit;
161                 c->ttydev->tty_base = unit;
162                 c->ttydev->tty_count = 1;
163                 c->ttydev->tty_ttys = c->ttyp;
164                 tty_attach (c->ttydev);
165         }
166 #endif
167         tp = c->ttyp;
168         tp->t_dev = dev;
169         if ((tp->t_state & TS_ISOPEN) && (tp->t_state & TS_XCLUDE) &&
170             suser(td))
171                 return (EBUSY);
172         if (! (tp->t_state & TS_ISOPEN)) {
173                 ttychars (tp);
174                 if (tp->t_ispeed == 0) {
175 #ifdef __bsdi__
176                         tp->t_termios = deftermios;
177 #else
178                         tp->t_iflag = 0;
179                         tp->t_oflag = 0;
180                         tp->t_lflag = 0;
181                         tp->t_cflag = CREAD | CS8 | HUPCL;
182                         tp->t_ispeed = c->rxbaud;
183                         tp->t_ospeed = c->txbaud;
184 #endif
185                 }
186                 cxparam (tp, &tp->t_termios);
187                 ttsetwater (tp);
188         }
189
190         spltty ();
191         if (! (tp->t_state & TS_ISOPEN)) {
192                 /*
193                  * Compute optimal receiver buffer length.
194                  * The best choice is rxbaud/400.
195                  * Make it even, to avoid byte-wide DMA transfers.
196                  * --------------------------
197                  * Baud rate    Buffer length
198                  * --------------------------
199                  *      300     4
200                  *     1200     4
201                  *     9600     24
202                  *    19200     48
203                  *    38400     96
204                  *    57600     192
205                  *   115200     288
206                  * --------------------------
207                  */
208                 int rbsz = (c->rxbaud + 800 - 1) / 800 * 2;
209                 if (rbsz < 4)
210                         rbsz = 4;
211                 else if (rbsz > DMABUFSZ)
212                         rbsz = DMABUFSZ;
213
214                 /* Initialize channel, enable receiver. */
215                 cx_cmd (port, CCR_INITCH | CCR_ENRX);
216                 cx_cmd (port, CCR_INITCH | CCR_ENRX);
217
218                 /* Start receiver. */
219                 outw (ARBCNT(port), rbsz);
220                 outw (BRBCNT(port), rbsz);
221                 outw (ARBSTS(port), BSTS_OWN24);
222                 outw (BRBSTS(port), BSTS_OWN24);
223
224                 /* Enable interrupts. */
225                 outb (IER(port), IER_RXD | IER_RET | IER_TXD | IER_MDM);
226
227                 cx_chan_dtr (c, 1);
228                 cx_chan_rts (c, 1);
229         }
230         if (cx_chan_cd (c))
231                 (*linesw[tp->t_line].l_modem)(tp, 1);
232         if (! (flag & O_NONBLOCK)) {
233                 /* Lock the channel against cxconfig while we are
234                  * waiting for carrier. */
235                 c->sopt.lock = 1;
236                 while (!(tp->t_cflag & CLOCAL) && !(tp->t_state & TS_CARR_ON))
237                         if ((error = tsleep (TSA_CARR_ON(tp), PCATCH,
238                             "cxdcd", 0)))
239                                 break;
240                 c->sopt.lock = 0;       /* Unlock the channel. */
241         }
242         print (("cx%d.%d: cxopen done csr=%b\n", c->board->num, c->num,
243                 inb(CSR(c->chip->port)), CSRA_BITS));
244         spl0 ();
245         if (error)
246                 return (error);
247 #if __FreeBSD__ >= 2
248         error = (*linesw[tp->t_line].l_open) (dev, tp);
249 #else
250         error = (*linesw[tp->t_line].l_open) (dev, tp, 0);
251 #endif
252         return (error);
253 }
254
255 int cxclose (dev_t dev, int flag, int mode, struct thread *td)
256 {
257         int unit = UNIT (dev);
258         cx_chan_t *c = cxchan[unit];
259         struct tty *tp;
260         int s;
261
262         if (unit == UNIT_CTL)
263                 return (0);
264         tp = c->ttyp;
265         (*linesw[tp->t_line].l_close) (tp, flag);
266
267         /* Disable receiver.
268          * Transmitter continues sending the queued data. */
269         s = spltty ();
270         outb (CAR(c->chip->port), c->num & 3);
271         outb (IER(c->chip->port), IER_TXD | IER_MDM);
272         cx_cmd (c->chip->port, CCR_DISRX);
273
274         /* Clear DTR and RTS. */
275         if ((tp->t_cflag & HUPCL) || ! (tp->t_state & TS_ISOPEN)) {
276                 cx_chan_dtr (c, 0);
277                 cx_chan_rts (c, 0);
278         }
279
280         /* Stop sending break. */
281         if (c->brk == BRK_SEND) {
282                 c->brk = BRK_STOP;
283                 if (! (tp->t_state & TS_BUSY))
284                         cxoproc (tp);
285         }
286         splx (s);
287         ttyclose (tp);
288         return (0);
289 }
290
291 int cxioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
292 {
293         int unit = UNIT (dev);
294         cx_chan_t *c, *m;
295         cx_stat_t *st;
296         struct tty *tp;
297         int error, s;
298         unsigned char msv;
299         struct ifnet *master;
300
301         if (unit == UNIT_CTL) {
302                 /* Process an ioctl request on /dev/cronyx */
303                 cx_options_t *o = (cx_options_t*) data;
304
305                 if (o->board >= NCX || o->channel >= NCHAN)
306                         return (EINVAL);
307                 c = &cxboard[o->board].chan[o->channel];
308                 if (c->type == T_NONE)
309                         return (ENXIO);
310                 switch (cmd) {
311                 default:
312                         return (EINVAL);
313
314                 case CXIOCSETMODE:
315                         print (("cx%d.%d: CXIOCSETMODE\n", o->board, o->channel));
316                         if (c->type == T_NONE)
317                                 return (EINVAL);
318                         if (c->type == T_ASYNC && o->mode != M_ASYNC)
319                                 return (EINVAL);
320                         if (o->mode == M_ASYNC)
321                                 switch (c->type) {
322                                 case T_SYNC_RS232:
323                                 case T_SYNC_V35:
324                                 case T_SYNC_RS449:
325                                         return (EINVAL);
326                                 }
327                         /* Somebody is waiting for carrier? */
328                         if (c->sopt.lock)
329                                 return (EBUSY);
330                         /* /dev/ttyXX is already opened by someone? */
331                         if (c->mode == M_ASYNC && c->ttyp &&
332                             (c->ttyp->t_state & TS_ISOPEN))
333                                 return (EBUSY);
334                         /* Network interface is up? */
335                         if (c->mode != M_ASYNC && (c->ifp->if_flags & IFF_UP))
336                                 return (EBUSY);
337
338                         /* Find the master interface. */
339                         master = *o->master ? ifunit (o->master) : c->ifp;
340                         if (! master)
341                                 return (EINVAL);
342                         m = cxchan[master->if_unit];
343
344                         /* Leave the previous master queue. */
345                         if (c->master != c->ifp) {
346                                 cx_chan_t *p = cxchan[c->master->if_unit];
347
348                                 for (; p; p=p->slaveq)
349                                         if (p->slaveq == c)
350                                                 p->slaveq = c->slaveq;
351                         }
352
353                         /* Set up new master. */
354                         c->master = master;
355                         c->slaveq = 0;
356
357                         /* Join the new master queue. */
358                         if (c->master != c->ifp) {
359                                 c->slaveq = m->slaveq;
360                                 m->slaveq = c;
361                         }
362
363                         c->mode   = o->mode;
364                         c->rxbaud = o->rxbaud;
365                         c->txbaud = o->txbaud;
366                         c->opt    = o->opt;
367                         c->aopt   = o->aopt;
368                         c->hopt   = o->hopt;
369                         c->bopt   = o->bopt;
370                         c->xopt   = o->xopt;
371                         switch (c->num) {
372                         case 0: c->board->if0type = o->iftype; break;
373                         case 8: c->board->if8type = o->iftype; break;
374                         }
375                         s = spltty ();
376                         cxswitch (c, o->sopt);
377                         cx_setup_chan (c);
378                         outb (IER(c->chip->port), 0);
379                         splx (s);
380                         break;
381
382                 case CXIOCGETSTAT:
383                         st = (cx_stat_t*) data;
384                         st->rintr  = c->stat->rintr;
385                         st->tintr  = c->stat->tintr;
386                         st->mintr  = c->stat->mintr;
387                         st->ibytes = c->stat->ibytes;
388                         st->ipkts  = c->stat->ipkts;
389                         st->ierrs  = c->stat->ierrs;
390                         st->obytes = c->stat->obytes;
391                         st->opkts  = c->stat->opkts;
392                         st->oerrs  = c->stat->oerrs;
393                         break;
394
395                 case CXIOCGETMODE:
396                         print (("cx%d.%d: CXIOCGETMODE\n", o->board, o->channel));
397                         o->type   = c->type;
398                         o->mode   = c->mode;
399                         o->rxbaud = c->rxbaud;
400                         o->txbaud = c->txbaud;
401                         o->opt    = c->opt;
402                         o->aopt   = c->aopt;
403                         o->hopt   = c->hopt;
404                         o->bopt   = c->bopt;
405                         o->xopt   = c->xopt;
406                         o->sopt   = c->sopt;
407                         switch (c->num) {
408                         case 0: o->iftype = c->board->if0type; break;
409                         case 8: o->iftype = c->board->if8type; break;
410                         }
411                         if (c->master != c->ifp)
412                                 snprintf (o->master, sizeof(o->master),
413                                     "%s%d", c->master->if_name,
414                                         c->master->if_unit);
415                         else
416                                 *o->master = 0;
417                         break;
418                 }
419                 return (0);
420         }
421
422         c = cxchan[unit];
423         tp = c->ttyp;
424         if (! tp)
425                 return (EINVAL);
426 #if __FreeBSD__ >= 2
427         error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag, td);
428 #else
429         error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag);
430 #endif
431         if (error != ENOIOCTL)
432                 return (error);
433         error = ttioctl (tp, cmd, data, flag);
434         if (error != ENOIOCTL)
435                 return (error);
436
437         s = spltty ();
438         switch (cmd) {
439         default:
440                 splx (s);
441                 return (ENOTTY);
442         case TIOCSBRK:          /* Start sending line break */
443                 c->brk = BRK_SEND;
444                 if (! (tp->t_state & TS_BUSY))
445                         cxoproc (tp);
446                 break;
447         case TIOCCBRK:          /* Stop sending line break */
448                 c->brk = BRK_STOP;
449                 if (! (tp->t_state & TS_BUSY))
450                         cxoproc (tp);
451                 break;
452         case TIOCSDTR:          /* Set DTR */
453                 cx_chan_dtr (c, 1);
454                 break;
455         case TIOCCDTR:          /* Clear DTR */
456                 cx_chan_dtr (c, 0);
457                 break;
458         case TIOCMSET:          /* Set DTR/RTS */
459                 cx_chan_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
460                 cx_chan_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
461                 break;
462         case TIOCMBIS:          /* Add DTR/RTS */
463                 if (*(int*)data & TIOCM_DTR) cx_chan_dtr (c, 1);
464                 if (*(int*)data & TIOCM_RTS) cx_chan_rts (c, 1);
465                 break;
466         case TIOCMBIC:          /* Clear DTR/RTS */
467                 if (*(int*)data & TIOCM_DTR) cx_chan_dtr (c, 0);
468                 if (*(int*)data & TIOCM_RTS) cx_chan_rts (c, 0);
469                 break;
470         case TIOCMGET:          /* Get modem status */
471                 msv = inb (MSVR(c->chip->port));
472                 *(int*)data = TIOCM_LE; /* always enabled while open */
473                 if (msv & MSV_DSR) *(int*)data |= TIOCM_DSR;
474                 if (msv & MSV_CTS) *(int*)data |= TIOCM_CTS;
475                 if (msv & MSV_CD)  *(int*)data |= TIOCM_CD;
476                 if (c->dtr)        *(int*)data |= TIOCM_DTR;
477                 if (c->rts)        *(int*)data |= TIOCM_RTS;
478                 break;
479         }
480         splx (s);
481         return (0);
482 }
483
484 /*
485  * Fill transmitter buffer with data.
486  */
487 static void
488 cxout (cx_chan_t *c, char b)
489 {
490         unsigned char *buf, *p, sym;
491         unsigned short port = c->chip->port, len = 0, cnt_port, sts_port;
492         struct tty *tp = c->ttyp;
493
494         if (! tp)
495                 return;
496
497         /* Choose the buffer. */
498         if (b == 'A') {
499                 buf      = c->atbuf;
500                 cnt_port = ATBCNT(port);
501                 sts_port = ATBSTS(port);
502         } else {
503                 buf      = c->btbuf;
504                 cnt_port = BTBCNT(port);
505                 sts_port = BTBSTS(port);
506         }
507
508         /* Is it busy? */
509         if (inb (sts_port) & BSTS_OWN24) {
510                 tp->t_state |= TS_BUSY;
511                 return;
512         }
513
514         switch (c->brk) {
515         case BRK_SEND:
516                 *buf++ = 0;     /* extended transmit command */
517                 *buf++ = 0x81;  /* send break */
518                 *buf++ = 0;     /* extended transmit command */
519                 *buf++ = 0x82;  /* insert delay */
520                 *buf++ = 250;   /* 1/4 of second */
521                 *buf++ = 0;     /* extended transmit command */
522                 *buf++ = 0x82;  /* insert delay */
523                 *buf++ = 250;   /* + 1/4 of second */
524                 len = 8;
525                 c->brk = BRK_IDLE;
526                 break;
527         case BRK_STOP:
528                 *buf++ = 0;     /* extended transmit command */
529                 *buf++ = 0x83;  /* stop break */
530                 len = 2;
531                 c->brk = BRK_IDLE;
532                 break;
533         case BRK_IDLE:
534                 p = buf;
535                 if (tp->t_iflag & IXOFF)
536                         while (RB_LEN (tp->t_out) && p<buf+DMABUFSZ-1) {
537                                 sym = RB_GETC (tp->t_out);
538                                 /* Send XON/XOFF out of band. */
539                                 if (sym == tp->t_cc[VSTOP]) {
540                                         outb (STCR(port), STC_SNDSPC|STC_SSPC_2);
541                                         continue;
542                                 }
543                                 if (sym == tp->t_cc[VSTART]) {
544                                         outb (STCR(port), STC_SNDSPC|STC_SSPC_1);
545                                         continue;
546                                 }
547                                 /* Duplicate NULLs in ETC mode. */
548                                 if (! sym)
549                                         *p++ = 0;
550                                 *p++ = sym;
551                         }
552                 else
553                         while (RB_LEN (tp->t_out) && p<buf+DMABUFSZ-1) {
554                                 sym = RB_GETC (tp->t_out);
555                                 /* Duplicate NULLs in ETC mode. */
556                                 if (! sym)
557                                         *p++ = 0;
558                                 *p++ = sym;
559                         }
560                 len = p - buf;
561                 break;
562         }
563
564         /* Start transmitter. */
565         if (len) {
566                 outw (cnt_port, len);
567                 outb (sts_port, BSTS_INTR | BSTS_OWN24);
568                 c->stat->obytes += len;
569                 tp->t_state |= TS_BUSY;
570                 print (("cx%d.%d: out %d bytes to %c\n",
571                         c->board->num, c->num, len, b));
572         }
573 }
574
575 void cxoproc (struct tty *tp)
576 {
577         int unit = UNIT (tp->t_dev);
578         cx_chan_t *c = cxchan[unit];
579         unsigned short port = c->chip->port;
580         int s = spltty ();
581
582         /* Set current channel number */
583         outb (CAR(port), c->num & 3);
584
585         if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
586                 /* Start transmitter. */
587                 if (! (inb (CSR(port)) & CSRA_TXEN))
588                         cx_cmd (port, CCR_ENTX);
589
590                 /* Determine the buffer order. */
591                 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
592                         cxout (c, 'B');
593                         cxout (c, 'A');
594                 } else {
595                         cxout (c, 'A');
596                         cxout (c, 'B');
597                 }
598         }
599 #ifndef TS_ASLEEP /* FreeBSD some time after 2.0.5 */
600         ttwwakeup(tp);
601 #else
602         if (RB_LEN (tp->t_out) <= tp->t_lowat) {
603                 if (tp->t_state & TS_ASLEEP) {
604                         tp->t_state &= ~TS_ASLEEP;
605                         wakeup(TSA_OLOWAT(tp));
606                 }
607                 selwakeup(&tp->t_wsel);
608         }
609 #endif
610         splx (s);
611 }
612
613 static int
614 cxparam (struct tty *tp, struct termios *t)
615 {
616         int unit = UNIT (tp->t_dev);
617         cx_chan_t *c = cxchan[unit];
618         unsigned short port = c->chip->port;
619         int clock, period, s;
620         cx_cor1_async_t cor1;
621
622         if (t->c_ospeed == 0) {
623                 /* Clear DTR and RTS. */
624                 s = spltty ();
625                 cx_chan_dtr (c, 0);
626                 cx_chan_rts (c, 0);
627                 splx (s);
628                 print (("cx%d.%d: cxparam (hangup)\n", c->board->num, c->num));
629                 return (0);
630         }
631         print (("cx%d.%d: cxparam\n", c->board->num, c->num));
632
633         /* Check requested parameters. */
634         if (t->c_ospeed < 300 || t->c_ospeed > 256*1024)
635                 return(EINVAL);
636         if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024))
637                 return(EINVAL);
638
639 #ifdef __bsdi__
640         /* CLOCAL flag set -- wakeup everybody who waits for CD. */
641         /* FreeBSD does this themselves. */
642         if (! (tp->t_cflag & CLOCAL) && (t->c_cflag & CLOCAL))
643                 wakeup ((caddr_t) &tp->t_rawq);
644 #endif
645         /* And copy them to tty and channel structures. */
646         c->rxbaud = tp->t_ispeed = t->c_ispeed;
647         c->txbaud = tp->t_ospeed = t->c_ospeed;
648         tp->t_cflag = t->c_cflag;
649
650         /* Set character length and parity mode. */
651         BYTE cor1 = 0;
652         switch (t->c_cflag & CSIZE) {
653         default:
654         case CS8: cor1.charlen = 7; break;
655         case CS7: cor1.charlen = 6; break;
656         case CS6: cor1.charlen = 5; break;
657         case CS5: cor1.charlen = 4; break;
658         }
659         if (t->c_cflag & PARENB) {
660                 cor1.parmode = PARM_NORMAL;
661                 cor1.ignpar = 0;
662                 cor1.parity = (t->c_cflag & PARODD) ? PAR_ODD : PAR_EVEN;
663         } else {
664                 cor1.parmode = PARM_NOPAR;
665                 cor1.ignpar = 1;
666         }
667
668         /* Enable/disable hardware CTS. */
669         c->aopt.cor2.ctsae = (t->c_cflag & CRTSCTS) ? 1 : 0;
670         /* Handle DSR as CTS. */
671         c->aopt.cor2.dsrae = (t->c_cflag & CRTSCTS) ? 1 : 0;
672         /* Enable extended transmit command mode.
673          * Unfortunately, there is no other method for sending break. */
674         c->aopt.cor2.etc = 1;
675         /* Enable/disable hardware XON/XOFF. */
676         c->aopt.cor2.ixon = (t->c_iflag & IXON) ? 1 : 0;
677         c->aopt.cor2.ixany = (t->c_iflag & IXANY) ? 1 : 0;
678
679         /* Set the number of stop bits. */
680         if (t->c_cflag & CSTOPB)
681                 c->aopt.cor3.stopb = STOPB_2;
682         else
683                 c->aopt.cor3.stopb = STOPB_1;
684         /* Disable/enable passing XON/XOFF chars to the host. */
685         c->aopt.cor3.scde = (t->c_iflag & IXON) ? 1 : 0;
686         c->aopt.cor3.flowct = (t->c_iflag & IXON) ? FLOWCC_NOTPASS : FLOWCC_PASS;
687
688         c->aopt.schr1 = t->c_cc[VSTART];        /* XON */
689         c->aopt.schr2 = t->c_cc[VSTOP];         /* XOFF */
690
691         /* Set current channel number. */
692         s = spltty ();
693         outb (CAR(port), c->num & 3);
694
695         /* Set up receiver clock values. */
696         cx_clock (c->chip->oscfreq, c->rxbaud, &clock, &period);
697         c->opt.rcor.clk = clock;
698         outb (RCOR(port), BYTE c->opt.rcor);
699         outb (RBPR(port), period);
700
701         /* Set up transmitter clock values. */
702         cx_clock (c->chip->oscfreq, c->txbaud, &clock, &period);
703         c->opt.tcor.clk = clock;
704         c->opt.tcor.ext1x = 0;
705         outb (TCOR(port), BYTE c->opt.tcor);
706         outb (TBPR(port), period);
707
708         outb (COR2(port), BYTE c->aopt.cor2);
709         outb (COR3(port), BYTE c->aopt.cor3);
710         outb (SCHR1(port), c->aopt.schr1);
711         outb (SCHR2(port), c->aopt.schr2);
712
713         if (BYTE c->aopt.cor1 != BYTE cor1) {
714                 BYTE c->aopt.cor1 = BYTE cor1;
715                 outb (COR1(port), BYTE c->aopt.cor1);
716                 /* Any change to COR1 require reinitialization. */
717                 /* Unfortunately, it may cause transmitter glitches... */
718                 cx_cmd (port, CCR_INITCH);
719         }
720         splx (s);
721         return (0);
722 }
723
724 /*
725  * Stop output on a line
726  */
727 void cxstop (struct tty *tp, int flag)
728 {
729         cx_chan_t *c = cxchan[UNIT(tp->t_dev)];
730         unsigned short port = c->chip->port;
731         int s = spltty ();
732
733         if (tp->t_state & TS_BUSY) {
734                 print (("cx%d.%d: cxstop\n", c->board->num, c->num));
735
736                 /* Set current channel number */
737                 outb (CAR(port), c->num & 3);
738
739                 /* Stop transmitter */
740                 cx_cmd (port, CCR_DISTX);
741         }
742         splx (s);
743 }
744
745 /*
746  * Handle receive interrupts, including receive errors and
747  * receive timeout interrupt.
748  */
749 int cxrinta (cx_chan_t *c)
750 {
751         unsigned short port = c->chip->port;
752         unsigned short len = 0, risr = inw (RISR(port)), reoir = 0;
753         struct tty *tp = c->ttyp;
754
755         /* Compute optimal receiver buffer length. */
756         int rbsz = (c->rxbaud + 800 - 1) / 800 * 2;
757         if (rbsz < 4)
758                 rbsz = 4;
759         else if (rbsz > DMABUFSZ)
760                 rbsz = DMABUFSZ;
761
762         if (risr & RISA_TIMEOUT) {
763                 unsigned long rcbadr = (unsigned short) inw (RCBADRL(port)) |
764                         (long) inw (RCBADRU(port)) << 16;
765                 unsigned char *buf = 0;
766                 unsigned short cnt_port = 0, sts_port = 0;
767                 if (rcbadr >= c->brphys && rcbadr < c->brphys+DMABUFSZ) {
768                         buf = c->brbuf;
769                         len = rcbadr - c->brphys;
770                         cnt_port = BRBCNT(port);
771                         sts_port = BRBSTS(port);
772                 } else if (rcbadr >= c->arphys && rcbadr < c->arphys+DMABUFSZ) {
773                         buf = c->arbuf;
774                         len = rcbadr - c->arphys;
775                         cnt_port = ARBCNT(port);
776                         sts_port = ARBSTS(port);
777                 } else
778                         printf ("cx%d.%d: timeout: invalid buffer address\n",
779                                 c->board->num, c->num);
780
781                 if (len) {
782                         print (("cx%d.%d: async receive timeout (%d bytes), risr=%b, arbsts=%b, brbsts=%b\n",
783                                 c->board->num, c->num, len, risr, RISA_BITS,
784                                 inb (ARBSTS(port)), BSTS_BITS, inb (BRBSTS(port)), BSTS_BITS));
785                         c->stat->ibytes += len;
786                         if (tp && (tp->t_state & TS_ISOPEN)) {
787                                 int i;
788                                 int (*rint)(int, struct tty *) =
789                                         linesw[tp->t_line].l_rint;
790
791                                 for (i=0; i<len; ++i)
792                                         (*rint) (buf[i], tp);
793                         }
794
795                         /* Restart receiver. */
796                         outw (cnt_port, rbsz);
797                         outb (sts_port, BSTS_OWN24);
798                 }
799                 return (REOI_TERMBUFF);
800         }
801
802         print (("cx%d.%d: async receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
803                 c->board->num, c->num, risr, RISA_BITS,
804                 inb (ARBSTS(port)), BSTS_BITS, inb (BRBSTS(port)), BSTS_BITS));
805
806         if (risr & RIS_BUSERR) {
807                 printf ("cx%d.%d: receive bus error\n", c->board->num, c->num);
808                 ++c->stat->ierrs;
809         }
810         if (risr & (RIS_OVERRUN | RISA_PARERR | RISA_FRERR | RISA_BREAK)) {
811                 int err = 0;
812
813                 if (risr & RISA_PARERR)
814                         err |= TTY_PE;
815                 if (risr & RISA_FRERR)
816                         err |= TTY_FE;
817 #ifdef TTY_OE
818                 if (risr & RIS_OVERRUN)
819                         err |= TTY_OE;
820 #endif
821 #ifdef TTY_BI
822                 if (risr & RISA_BREAK)
823                         err |= TTY_BI;
824 #endif
825                 print (("cx%d.%d: receive error %x\n", c->board->num, c->num, err));
826                 if (tp && (tp->t_state & TS_ISOPEN))
827                         (*linesw[tp->t_line].l_rint) (err, tp);
828                 ++c->stat->ierrs;
829         }
830
831         /* Discard exception characters. */
832         if ((risr & RISA_SCMASK) && tp && (tp->t_iflag & IXON))
833                 reoir |= REOI_DISCEXC;
834
835         /* Handle received data. */
836         if ((risr & RIS_EOBUF) && tp && (tp->t_state & TS_ISOPEN)) {
837                 int (*rint)(int, struct tty *) = linesw[tp->t_line].l_rint;
838                 unsigned char *buf;
839                 int i;
840
841                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
842
843                 print (("cx%d.%d: async: %d bytes received\n",
844                         c->board->num, c->num, len));
845                 c->stat->ibytes += len;
846
847                 buf = (risr & RIS_BB) ? c->brbuf : c->arbuf;
848                 for (i=0; i<len; ++i)
849                         (*rint) (buf[i], tp);
850         }
851
852         /* Restart receiver. */
853         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
854                 outw (ARBCNT(port), rbsz);
855                 outb (ARBSTS(port), BSTS_OWN24);
856         }
857         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
858                 outw (BRBCNT(port), rbsz);
859                 outb (BRBSTS(port), BSTS_OWN24);
860         }
861         return (reoir);
862 }
863
864 /*
865  * Handle transmit interrupt.
866  */
867 void cxtinta (cx_chan_t *c)
868 {
869         struct tty *tp = c->ttyp;
870         unsigned short port = c->chip->port;
871         unsigned char tisr = inb (TISR(port));
872
873         print (("cx%d.%d: async transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
874                 c->board->num, c->num, tisr, TIS_BITS,
875                 inb (ATBSTS(port)), BSTS_BITS, inb (BTBSTS(port)), BSTS_BITS));
876
877         if (tisr & TIS_BUSERR) {
878                 printf ("cx%d.%d: transmit bus error\n",
879                         c->board->num, c->num);
880                 ++c->stat->oerrs;
881         } else if (tisr & TIS_UNDERRUN) {
882                 printf ("cx%d.%d: transmit underrun error\n",
883                         c->board->num, c->num);
884                 ++c->stat->oerrs;
885         }
886         if (tp) {
887                 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
888                 if (tp->t_line)
889                         (*linesw[tp->t_line].l_start) (tp);
890                 else
891                         cxoproc (tp);
892         }
893 }
894
895 /*
896  * Handle modem interrupt.
897  */
898 void cxmint (cx_chan_t *c)
899 {
900         unsigned short port = c->chip->port;
901         unsigned char misr = inb (MISR(port));
902         unsigned char msvr = inb (MSVR(port));
903         struct tty *tp = c->ttyp;
904
905         if (c->mode != M_ASYNC) {
906                 printf ("cx%d.%d: unexpected modem interrupt, misr=%b, msvr=%b\n",
907                         c->board->num, c->num, misr, MIS_BITS, msvr, MSV_BITS);
908                 return;
909         }
910         print (("cx%d.%d: modem interrupt, misr=%b, msvr=%b\n",
911                 c->board->num, c->num, misr, MIS_BITS, msvr, MSV_BITS));
912
913         /* Ignore DSR events. */
914         /* Ignore RTC/CTS events, handled by hardware. */
915         /* Handle carrier detect/loss. */
916         if (tp && (misr & MIS_CCD))
917                 (*linesw[tp->t_line].l_modem) (tp, (msvr & MSV_CD) != 0);
918 }
919
920 /*
921  * Recover after lost transmit interrupts.
922  */
923 void cxtimeout (void *a)
924 {
925         cx_board_t *b;
926         cx_chan_t *c;
927         struct tty *tp;
928         int s;
929
930         for (b=cxboard; b<cxboard+NCX; ++b)
931                 for (c=b->chan; c<b->chan+NCHAN; ++c) {
932                         tp = c->ttyp;
933                         if (c->type==T_NONE || c->mode!=M_ASYNC || !tp)
934                                 continue;
935                         s = spltty ();
936                         if (tp->t_state & TS_BUSY) {
937                                 tp->t_state &= ~TS_BUSY;
938                                 if (tp->t_line)
939                                         (*linesw[tp->t_line].l_start) (tp);
940                                 else
941                                         cxoproc (tp);
942                         }
943                         splx (s);
944                 }
945         timeout (cxtimeout, 0, hz*5);
946 }
947
948
949 #if defined(__FreeBSD__) && (__FreeBSD__ > 1 )
950 static void     cx_drvinit(void *unused)
951 {
952
953         cdevsw_add(&cx_cdevsw);
954 }
955
956 SYSINIT(cxdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cx_drvinit,NULL)
957
958
959 #endif