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