Add __DragonFly__
[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.9 2004/02/13 02:44:47 joerg 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 #if defined(__DragonFly__) || defined(__FreeBSD__)
37 #   if defined(__FreeBSD__) && __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(__DragonFly__) || !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 defined(__DragonFly__) || __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 #if defined(__DragonFly__) || defined(__FreeBSD__)
139 #if defined(__DragonFly__) || __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 defined(__DragonFly__) || __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_dunit];
343
344                         /* Leave the previous master queue. */
345                         if (c->master != c->ifp) {
346                                 cx_chan_t *p = cxchan[c->master->if_dunit];
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                                 strlcpy(o->master, sizeof(o->master),
413                                         c->master->if_xname);
414                         else
415                                 *o->master = 0;
416                         break;
417                 }
418                 return (0);
419         }
420
421         c = cxchan[unit];
422         tp = c->ttyp;
423         if (! tp)
424                 return (EINVAL);
425 #if defined(__DragonFly__) || __FreeBSD__ >= 2
426         error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag, td);
427 #else
428         error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag);
429 #endif
430         if (error != ENOIOCTL)
431                 return (error);
432         error = ttioctl (tp, cmd, data, flag);
433         if (error != ENOIOCTL)
434                 return (error);
435
436         s = spltty ();
437         switch (cmd) {
438         default:
439                 splx (s);
440                 return (ENOTTY);
441         case TIOCSBRK:          /* Start sending line break */
442                 c->brk = BRK_SEND;
443                 if (! (tp->t_state & TS_BUSY))
444                         cxoproc (tp);
445                 break;
446         case TIOCCBRK:          /* Stop sending line break */
447                 c->brk = BRK_STOP;
448                 if (! (tp->t_state & TS_BUSY))
449                         cxoproc (tp);
450                 break;
451         case TIOCSDTR:          /* Set DTR */
452                 cx_chan_dtr (c, 1);
453                 break;
454         case TIOCCDTR:          /* Clear DTR */
455                 cx_chan_dtr (c, 0);
456                 break;
457         case TIOCMSET:          /* Set DTR/RTS */
458                 cx_chan_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
459                 cx_chan_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
460                 break;
461         case TIOCMBIS:          /* Add DTR/RTS */
462                 if (*(int*)data & TIOCM_DTR) cx_chan_dtr (c, 1);
463                 if (*(int*)data & TIOCM_RTS) cx_chan_rts (c, 1);
464                 break;
465         case TIOCMBIC:          /* Clear DTR/RTS */
466                 if (*(int*)data & TIOCM_DTR) cx_chan_dtr (c, 0);
467                 if (*(int*)data & TIOCM_RTS) cx_chan_rts (c, 0);
468                 break;
469         case TIOCMGET:          /* Get modem status */
470                 msv = inb (MSVR(c->chip->port));
471                 *(int*)data = TIOCM_LE; /* always enabled while open */
472                 if (msv & MSV_DSR) *(int*)data |= TIOCM_DSR;
473                 if (msv & MSV_CTS) *(int*)data |= TIOCM_CTS;
474                 if (msv & MSV_CD)  *(int*)data |= TIOCM_CD;
475                 if (c->dtr)        *(int*)data |= TIOCM_DTR;
476                 if (c->rts)        *(int*)data |= TIOCM_RTS;
477                 break;
478         }
479         splx (s);
480         return (0);
481 }
482
483 /*
484  * Fill transmitter buffer with data.
485  */
486 static void
487 cxout (cx_chan_t *c, char b)
488 {
489         unsigned char *buf, *p, sym;
490         unsigned short port = c->chip->port, len = 0, cnt_port, sts_port;
491         struct tty *tp = c->ttyp;
492
493         if (! tp)
494                 return;
495
496         /* Choose the buffer. */
497         if (b == 'A') {
498                 buf      = c->atbuf;
499                 cnt_port = ATBCNT(port);
500                 sts_port = ATBSTS(port);
501         } else {
502                 buf      = c->btbuf;
503                 cnt_port = BTBCNT(port);
504                 sts_port = BTBSTS(port);
505         }
506
507         /* Is it busy? */
508         if (inb (sts_port) & BSTS_OWN24) {
509                 tp->t_state |= TS_BUSY;
510                 return;
511         }
512
513         switch (c->brk) {
514         case BRK_SEND:
515                 *buf++ = 0;     /* extended transmit command */
516                 *buf++ = 0x81;  /* send break */
517                 *buf++ = 0;     /* extended transmit command */
518                 *buf++ = 0x82;  /* insert delay */
519                 *buf++ = 250;   /* 1/4 of second */
520                 *buf++ = 0;     /* extended transmit command */
521                 *buf++ = 0x82;  /* insert delay */
522                 *buf++ = 250;   /* + 1/4 of second */
523                 len = 8;
524                 c->brk = BRK_IDLE;
525                 break;
526         case BRK_STOP:
527                 *buf++ = 0;     /* extended transmit command */
528                 *buf++ = 0x83;  /* stop break */
529                 len = 2;
530                 c->brk = BRK_IDLE;
531                 break;
532         case BRK_IDLE:
533                 p = buf;
534                 if (tp->t_iflag & IXOFF)
535                         while (RB_LEN (tp->t_out) && p<buf+DMABUFSZ-1) {
536                                 sym = RB_GETC (tp->t_out);
537                                 /* Send XON/XOFF out of band. */
538                                 if (sym == tp->t_cc[VSTOP]) {
539                                         outb (STCR(port), STC_SNDSPC|STC_SSPC_2);
540                                         continue;
541                                 }
542                                 if (sym == tp->t_cc[VSTART]) {
543                                         outb (STCR(port), STC_SNDSPC|STC_SSPC_1);
544                                         continue;
545                                 }
546                                 /* Duplicate NULLs in ETC mode. */
547                                 if (! sym)
548                                         *p++ = 0;
549                                 *p++ = sym;
550                         }
551                 else
552                         while (RB_LEN (tp->t_out) && p<buf+DMABUFSZ-1) {
553                                 sym = RB_GETC (tp->t_out);
554                                 /* Duplicate NULLs in ETC mode. */
555                                 if (! sym)
556                                         *p++ = 0;
557                                 *p++ = sym;
558                         }
559                 len = p - buf;
560                 break;
561         }
562
563         /* Start transmitter. */
564         if (len) {
565                 outw (cnt_port, len);
566                 outb (sts_port, BSTS_INTR | BSTS_OWN24);
567                 c->stat->obytes += len;
568                 tp->t_state |= TS_BUSY;
569                 print (("cx%d.%d: out %d bytes to %c\n",
570                         c->board->num, c->num, len, b));
571         }
572 }
573
574 void cxoproc (struct tty *tp)
575 {
576         int unit = UNIT (tp->t_dev);
577         cx_chan_t *c = cxchan[unit];
578         unsigned short port = c->chip->port;
579         int s = spltty ();
580
581         /* Set current channel number */
582         outb (CAR(port), c->num & 3);
583
584         if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
585                 /* Start transmitter. */
586                 if (! (inb (CSR(port)) & CSRA_TXEN))
587                         cx_cmd (port, CCR_ENTX);
588
589                 /* Determine the buffer order. */
590                 if (inb (DMABSTS(port)) & DMABSTS_NTBUF) {
591                         cxout (c, 'B');
592                         cxout (c, 'A');
593                 } else {
594                         cxout (c, 'A');
595                         cxout (c, 'B');
596                 }
597         }
598 #ifndef TS_ASLEEP /* FreeBSD some time after 2.0.5 */
599         ttwwakeup(tp);
600 #else
601         if (RB_LEN (tp->t_out) <= tp->t_lowat) {
602                 if (tp->t_state & TS_ASLEEP) {
603                         tp->t_state &= ~TS_ASLEEP;
604                         wakeup(TSA_OLOWAT(tp));
605                 }
606                 selwakeup(&tp->t_wsel);
607         }
608 #endif
609         splx (s);
610 }
611
612 static int
613 cxparam (struct tty *tp, struct termios *t)
614 {
615         int unit = UNIT (tp->t_dev);
616         cx_chan_t *c = cxchan[unit];
617         unsigned short port = c->chip->port;
618         int clock, period, s;
619         cx_cor1_async_t cor1;
620
621         if (t->c_ospeed == 0) {
622                 /* Clear DTR and RTS. */
623                 s = spltty ();
624                 cx_chan_dtr (c, 0);
625                 cx_chan_rts (c, 0);
626                 splx (s);
627                 print (("cx%d.%d: cxparam (hangup)\n", c->board->num, c->num));
628                 return (0);
629         }
630         print (("cx%d.%d: cxparam\n", c->board->num, c->num));
631
632         /* Check requested parameters. */
633         if (t->c_ospeed < 300 || t->c_ospeed > 256*1024)
634                 return(EINVAL);
635         if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024))
636                 return(EINVAL);
637
638 #ifdef __bsdi__
639         /* CLOCAL flag set -- wakeup everybody who waits for CD. */
640         /* FreeBSD does this themselves. */
641         if (! (tp->t_cflag & CLOCAL) && (t->c_cflag & CLOCAL))
642                 wakeup ((caddr_t) &tp->t_rawq);
643 #endif
644         /* And copy them to tty and channel structures. */
645         c->rxbaud = tp->t_ispeed = t->c_ispeed;
646         c->txbaud = tp->t_ospeed = t->c_ospeed;
647         tp->t_cflag = t->c_cflag;
648
649         /* Set character length and parity mode. */
650         BYTE cor1 = 0;
651         switch (t->c_cflag & CSIZE) {
652         default:
653         case CS8: cor1.charlen = 7; break;
654         case CS7: cor1.charlen = 6; break;
655         case CS6: cor1.charlen = 5; break;
656         case CS5: cor1.charlen = 4; break;
657         }
658         if (t->c_cflag & PARENB) {
659                 cor1.parmode = PARM_NORMAL;
660                 cor1.ignpar = 0;
661                 cor1.parity = (t->c_cflag & PARODD) ? PAR_ODD : PAR_EVEN;
662         } else {
663                 cor1.parmode = PARM_NOPAR;
664                 cor1.ignpar = 1;
665         }
666
667         /* Enable/disable hardware CTS. */
668         c->aopt.cor2.ctsae = (t->c_cflag & CRTSCTS) ? 1 : 0;
669         /* Handle DSR as CTS. */
670         c->aopt.cor2.dsrae = (t->c_cflag & CRTSCTS) ? 1 : 0;
671         /* Enable extended transmit command mode.
672          * Unfortunately, there is no other method for sending break. */
673         c->aopt.cor2.etc = 1;
674         /* Enable/disable hardware XON/XOFF. */
675         c->aopt.cor2.ixon = (t->c_iflag & IXON) ? 1 : 0;
676         c->aopt.cor2.ixany = (t->c_iflag & IXANY) ? 1 : 0;
677
678         /* Set the number of stop bits. */
679         if (t->c_cflag & CSTOPB)
680                 c->aopt.cor3.stopb = STOPB_2;
681         else
682                 c->aopt.cor3.stopb = STOPB_1;
683         /* Disable/enable passing XON/XOFF chars to the host. */
684         c->aopt.cor3.scde = (t->c_iflag & IXON) ? 1 : 0;
685         c->aopt.cor3.flowct = (t->c_iflag & IXON) ? FLOWCC_NOTPASS : FLOWCC_PASS;
686
687         c->aopt.schr1 = t->c_cc[VSTART];        /* XON */
688         c->aopt.schr2 = t->c_cc[VSTOP];         /* XOFF */
689
690         /* Set current channel number. */
691         s = spltty ();
692         outb (CAR(port), c->num & 3);
693
694         /* Set up receiver clock values. */
695         cx_clock (c->chip->oscfreq, c->rxbaud, &clock, &period);
696         c->opt.rcor.clk = clock;
697         outb (RCOR(port), BYTE c->opt.rcor);
698         outb (RBPR(port), period);
699
700         /* Set up transmitter clock values. */
701         cx_clock (c->chip->oscfreq, c->txbaud, &clock, &period);
702         c->opt.tcor.clk = clock;
703         c->opt.tcor.ext1x = 0;
704         outb (TCOR(port), BYTE c->opt.tcor);
705         outb (TBPR(port), period);
706
707         outb (COR2(port), BYTE c->aopt.cor2);
708         outb (COR3(port), BYTE c->aopt.cor3);
709         outb (SCHR1(port), c->aopt.schr1);
710         outb (SCHR2(port), c->aopt.schr2);
711
712         if (BYTE c->aopt.cor1 != BYTE cor1) {
713                 BYTE c->aopt.cor1 = BYTE cor1;
714                 outb (COR1(port), BYTE c->aopt.cor1);
715                 /* Any change to COR1 require reinitialization. */
716                 /* Unfortunately, it may cause transmitter glitches... */
717                 cx_cmd (port, CCR_INITCH);
718         }
719         splx (s);
720         return (0);
721 }
722
723 /*
724  * Stop output on a line
725  */
726 void cxstop (struct tty *tp, int flag)
727 {
728         cx_chan_t *c = cxchan[UNIT(tp->t_dev)];
729         unsigned short port = c->chip->port;
730         int s = spltty ();
731
732         if (tp->t_state & TS_BUSY) {
733                 print (("cx%d.%d: cxstop\n", c->board->num, c->num));
734
735                 /* Set current channel number */
736                 outb (CAR(port), c->num & 3);
737
738                 /* Stop transmitter */
739                 cx_cmd (port, CCR_DISTX);
740         }
741         splx (s);
742 }
743
744 /*
745  * Handle receive interrupts, including receive errors and
746  * receive timeout interrupt.
747  */
748 int cxrinta (cx_chan_t *c)
749 {
750         unsigned short port = c->chip->port;
751         unsigned short len = 0, risr = inw (RISR(port)), reoir = 0;
752         struct tty *tp = c->ttyp;
753
754         /* Compute optimal receiver buffer length. */
755         int rbsz = (c->rxbaud + 800 - 1) / 800 * 2;
756         if (rbsz < 4)
757                 rbsz = 4;
758         else if (rbsz > DMABUFSZ)
759                 rbsz = DMABUFSZ;
760
761         if (risr & RISA_TIMEOUT) {
762                 unsigned long rcbadr = (unsigned short) inw (RCBADRL(port)) |
763                         (long) inw (RCBADRU(port)) << 16;
764                 unsigned char *buf = 0;
765                 unsigned short cnt_port = 0, sts_port = 0;
766                 if (rcbadr >= c->brphys && rcbadr < c->brphys+DMABUFSZ) {
767                         buf = c->brbuf;
768                         len = rcbadr - c->brphys;
769                         cnt_port = BRBCNT(port);
770                         sts_port = BRBSTS(port);
771                 } else if (rcbadr >= c->arphys && rcbadr < c->arphys+DMABUFSZ) {
772                         buf = c->arbuf;
773                         len = rcbadr - c->arphys;
774                         cnt_port = ARBCNT(port);
775                         sts_port = ARBSTS(port);
776                 } else
777                         printf ("cx%d.%d: timeout: invalid buffer address\n",
778                                 c->board->num, c->num);
779
780                 if (len) {
781                         print (("cx%d.%d: async receive timeout (%d bytes), risr=%b, arbsts=%b, brbsts=%b\n",
782                                 c->board->num, c->num, len, risr, RISA_BITS,
783                                 inb (ARBSTS(port)), BSTS_BITS, inb (BRBSTS(port)), BSTS_BITS));
784                         c->stat->ibytes += len;
785                         if (tp && (tp->t_state & TS_ISOPEN)) {
786                                 int i;
787                                 int (*rint)(int, struct tty *) =
788                                         linesw[tp->t_line].l_rint;
789
790                                 for (i=0; i<len; ++i)
791                                         (*rint) (buf[i], tp);
792                         }
793
794                         /* Restart receiver. */
795                         outw (cnt_port, rbsz);
796                         outb (sts_port, BSTS_OWN24);
797                 }
798                 return (REOI_TERMBUFF);
799         }
800
801         print (("cx%d.%d: async receive interrupt, risr=%b, arbsts=%b, brbsts=%b\n",
802                 c->board->num, c->num, risr, RISA_BITS,
803                 inb (ARBSTS(port)), BSTS_BITS, inb (BRBSTS(port)), BSTS_BITS));
804
805         if (risr & RIS_BUSERR) {
806                 printf ("cx%d.%d: receive bus error\n", c->board->num, c->num);
807                 ++c->stat->ierrs;
808         }
809         if (risr & (RIS_OVERRUN | RISA_PARERR | RISA_FRERR | RISA_BREAK)) {
810                 int err = 0;
811
812                 if (risr & RISA_PARERR)
813                         err |= TTY_PE;
814                 if (risr & RISA_FRERR)
815                         err |= TTY_FE;
816 #ifdef TTY_OE
817                 if (risr & RIS_OVERRUN)
818                         err |= TTY_OE;
819 #endif
820 #ifdef TTY_BI
821                 if (risr & RISA_BREAK)
822                         err |= TTY_BI;
823 #endif
824                 print (("cx%d.%d: receive error %x\n", c->board->num, c->num, err));
825                 if (tp && (tp->t_state & TS_ISOPEN))
826                         (*linesw[tp->t_line].l_rint) (err, tp);
827                 ++c->stat->ierrs;
828         }
829
830         /* Discard exception characters. */
831         if ((risr & RISA_SCMASK) && tp && (tp->t_iflag & IXON))
832                 reoir |= REOI_DISCEXC;
833
834         /* Handle received data. */
835         if ((risr & RIS_EOBUF) && tp && (tp->t_state & TS_ISOPEN)) {
836                 int (*rint)(int, struct tty *) = linesw[tp->t_line].l_rint;
837                 unsigned char *buf;
838                 int i;
839
840                 len = (risr & RIS_BB) ? inw(BRBCNT(port)) : inw(ARBCNT(port));
841
842                 print (("cx%d.%d: async: %d bytes received\n",
843                         c->board->num, c->num, len));
844                 c->stat->ibytes += len;
845
846                 buf = (risr & RIS_BB) ? c->brbuf : c->arbuf;
847                 for (i=0; i<len; ++i)
848                         (*rint) (buf[i], tp);
849         }
850
851         /* Restart receiver. */
852         if (! (inb (ARBSTS(port)) & BSTS_OWN24)) {
853                 outw (ARBCNT(port), rbsz);
854                 outb (ARBSTS(port), BSTS_OWN24);
855         }
856         if (! (inb (BRBSTS(port)) & BSTS_OWN24)) {
857                 outw (BRBCNT(port), rbsz);
858                 outb (BRBSTS(port), BSTS_OWN24);
859         }
860         return (reoir);
861 }
862
863 /*
864  * Handle transmit interrupt.
865  */
866 void cxtinta (cx_chan_t *c)
867 {
868         struct tty *tp = c->ttyp;
869         unsigned short port = c->chip->port;
870         unsigned char tisr = inb (TISR(port));
871
872         print (("cx%d.%d: async transmit interrupt, tisr=%b, atbsts=%b, btbsts=%b\n",
873                 c->board->num, c->num, tisr, TIS_BITS,
874                 inb (ATBSTS(port)), BSTS_BITS, inb (BTBSTS(port)), BSTS_BITS));
875
876         if (tisr & TIS_BUSERR) {
877                 printf ("cx%d.%d: transmit bus error\n",
878                         c->board->num, c->num);
879                 ++c->stat->oerrs;
880         } else if (tisr & TIS_UNDERRUN) {
881                 printf ("cx%d.%d: transmit underrun error\n",
882                         c->board->num, c->num);
883                 ++c->stat->oerrs;
884         }
885         if (tp) {
886                 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
887                 if (tp->t_line)
888                         (*linesw[tp->t_line].l_start) (tp);
889                 else
890                         cxoproc (tp);
891         }
892 }
893
894 /*
895  * Handle modem interrupt.
896  */
897 void cxmint (cx_chan_t *c)
898 {
899         unsigned short port = c->chip->port;
900         unsigned char misr = inb (MISR(port));
901         unsigned char msvr = inb (MSVR(port));
902         struct tty *tp = c->ttyp;
903
904         if (c->mode != M_ASYNC) {
905                 printf ("cx%d.%d: unexpected modem interrupt, misr=%b, msvr=%b\n",
906                         c->board->num, c->num, misr, MIS_BITS, msvr, MSV_BITS);
907                 return;
908         }
909         print (("cx%d.%d: modem interrupt, misr=%b, msvr=%b\n",
910                 c->board->num, c->num, misr, MIS_BITS, msvr, MSV_BITS));
911
912         /* Ignore DSR events. */
913         /* Ignore RTC/CTS events, handled by hardware. */
914         /* Handle carrier detect/loss. */
915         if (tp && (misr & MIS_CCD))
916                 (*linesw[tp->t_line].l_modem) (tp, (msvr & MSV_CD) != 0);
917 }
918
919 /*
920  * Recover after lost transmit interrupts.
921  */
922 void cxtimeout (void *a)
923 {
924         cx_board_t *b;
925         cx_chan_t *c;
926         struct tty *tp;
927         int s;
928
929         for (b=cxboard; b<cxboard+NCX; ++b)
930                 for (c=b->chan; c<b->chan+NCHAN; ++c) {
931                         tp = c->ttyp;
932                         if (c->type==T_NONE || c->mode!=M_ASYNC || !tp)
933                                 continue;
934                         s = spltty ();
935                         if (tp->t_state & TS_BUSY) {
936                                 tp->t_state &= ~TS_BUSY;
937                                 if (tp->t_line)
938                                         (*linesw[tp->t_line].l_start) (tp);
939                                 else
940                                         cxoproc (tp);
941                         }
942                         splx (s);
943                 }
944         timeout (cxtimeout, 0, hz*5);
945 }
946
947
948 #if defined(__DragonFly__) || (defined(__FreeBSD__) && (__FreeBSD__ > 1 ))
949 static void     cx_drvinit(void *unused)
950 {
951
952         cdevsw_add(&cx_cdevsw);
953 }
954
955 SYSINIT(cxdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cx_drvinit,NULL)
956
957
958 #endif