4 * Copyright (c) 1991 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/sys/isa/sio.c,v 1.291.2.35 2003/05/18 08:51:15 murray Exp $
36 * from: @(#)com.c 7.5 (Berkeley) 5/16/91
37 * from: i386/isa sio.c,v 1.234
40 #include "opt_comconsole.h"
41 #include "opt_compat.h"
51 * Serial driver, based on 386BSD-0.1 com driver.
52 * Mostly rewritten to use pseudo-DMA.
53 * Works for National Semiconductor NS8250-NS16550AF UARTs.
54 * COM driver, based on HP dca driver.
56 * Changes for PC-Card integration:
57 * - Added PC-Card driver table and handlers
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/reboot.h>
62 #include <sys/malloc.h>
66 #include <sys/module.h>
68 #include <sys/dkstat.h>
69 #include <sys/fcntl.h>
70 #include <sys/interrupt.h>
71 #include <sys/kernel.h>
72 #include <sys/syslog.h>
73 #include <sys/sysctl.h>
76 #include <sys/timepps.h>
77 #include <sys/thread2.h>
78 #include <sys/devfs.h>
80 #include <machine/limits.h>
82 #include <bus/isa/isareg.h>
83 #include <bus/isa/isavar.h>
85 #include <bus/pci/pcireg.h>
86 #include <bus/pci/pcivar.h>
89 #include <dev/misc/puc/pucvar.h>
91 #include <machine/lock.h>
93 #include <machine/clock.h>
95 #include <machine/lock.h>
99 #include "sio_private.h"
102 #include "../ic_layer/esp.h"
105 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
107 #define CALLOUT_MASK 0x80
108 #define CONTROL_MASK 0x60
109 #define CONTROL_INIT_STATE 0x20
110 #define CONTROL_LOCK_STATE 0x40
111 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
112 #define MINOR_TO_UNIT(mynor) ((((mynor) & ~0xffffU) >> (8 + 3)) \
114 #define UNIT_TO_MINOR(unit) ((((unit) & ~0x1fU) << (8 + 3)) \
117 #define com_scr 7 /* scratch register for 16450-16550 (R/W) */
119 #define sio_getreg(com, off) \
120 (bus_space_read_1((com)->bst, (com)->bsh, (off)))
121 #define sio_setreg(com, off, value) \
122 (bus_space_write_1((com)->bst, (com)->bsh, (off), (value)))
126 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
127 * than the other bits so that they can be tested as a group without masking
130 * The following com and tty flags correspond closely:
131 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
133 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
134 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
135 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
136 * TS_FLUSH is not used.
137 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
138 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
140 #define CS_BUSY 0x80 /* output in progress */
141 #define CS_TTGO 0x40 /* output not stopped by XOFF */
142 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
143 #define CS_CHECKMSR 1 /* check of MSR scheduled */
144 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
145 #define CS_DTR_OFF 0x10 /* DTR held off */
146 #define CS_ODONE 4 /* output completed */
147 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
148 #define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */
150 static char const * const error_desc[] = {
153 #define CE_INTERRUPT_BUF_OVERFLOW 1
154 "interrupt-level buffer overflow",
155 #define CE_TTY_BUF_OVERFLOW 2
156 "tty-level buffer overflow",
160 static int espattach (struct com_s *com, Port_t esp_port);
162 static int sio_isa_attach (device_t dev);
164 static timeout_t siobusycheck;
165 static u_int siodivisor (u_long rclk, speed_t speed);
166 static timeout_t siodtrwakeup;
167 static void comhardclose (struct com_s *com);
168 static void sioinput (struct com_s *com);
169 static void siointr1 (struct com_s *com);
170 static void siointr (void *arg);
171 static int commctl (struct com_s *com, int bits, int how);
172 static int comparam (struct tty *tp, struct termios *t);
173 static inthand2_t siopoll;
174 static int sio_isa_probe (device_t dev);
175 static void siosettimeout (void);
176 static int siosetwater (struct com_s *com, speed_t speed);
177 static void comstart (struct tty *tp);
178 static void comstop (struct tty *tp, int rw);
179 static timeout_t comwakeup;
180 static void disc_optim (struct tty *tp, struct termios *t,
184 static int sio_pci_attach (device_t dev);
185 static void sio_pci_kludge_unit (device_t dev);
186 static int sio_pci_probe (device_t dev);
187 #endif /* NPCI > 0 */
190 static int sio_puc_attach (device_t dev);
191 static int sio_puc_probe (device_t dev);
192 #endif /* NPUC > 0 */
194 static char driver_name[] = "sio";
196 /* table and macro for fast conversion from a unit number to its com struct */
197 devclass_t sio_devclass;
198 #define com_addr(unit) ((struct com_s *) \
199 devclass_get_softc(sio_devclass, unit))
201 static device_method_t sio_isa_methods[] = {
202 /* Device interface */
203 DEVMETHOD(device_probe, sio_isa_probe),
204 DEVMETHOD(device_attach, sio_isa_attach),
209 static driver_t sio_isa_driver = {
212 sizeof(struct com_s),
216 static device_method_t sio_pci_methods[] = {
217 /* Device interface */
218 DEVMETHOD(device_probe, sio_pci_probe),
219 DEVMETHOD(device_attach, sio_pci_attach),
224 static driver_t sio_pci_driver = {
227 sizeof(struct com_s),
229 #endif /* NPCI > 0 */
232 static device_method_t sio_puc_methods[] = {
233 /* Device interface */
234 DEVMETHOD(device_probe, sio_puc_probe),
235 DEVMETHOD(device_attach, sio_puc_attach),
240 static driver_t sio_puc_driver = {
243 sizeof(struct com_s),
245 #endif /* NPUC > 0 */
247 static d_open_t sioopen;
248 static d_close_t sioclose;
249 static d_read_t sioread;
250 static d_write_t siowrite;
251 static d_ioctl_t sioioctl;
253 static struct dev_ops sio_ops = {
254 { driver_name, 0, D_TTY },
260 .d_kqfilter = ttykqfilter,
261 .d_revoke = ttyrevoke
265 static volatile speed_t comdefaultrate = CONSPEED;
266 static u_long comdefaultrclk = DEFAULT_RCLK;
267 SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, "");
268 static u_int com_events; /* input chars + weighted output completions */
269 static Port_t siocniobase;
270 static int siocnunit;
271 static Port_t siogdbiobase;
272 static int siogdbunit = -1;
273 static bool_t sio_registered;
274 static int sio_timeout;
275 static int sio_timeouts_until_log;
276 static struct callout sio_timeout_handle;
277 static int sio_numunits;
280 /* XXX configure this properly. */
281 static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, };
282 static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 };
286 * handle sysctl read/write requests for console speed
288 * In addition to setting comdefaultrate for I/O through /dev/console,
289 * also set the initial and lock values for the /dev/ttyXX device
290 * if there is one associated with the console. Finally, if the /dev/tty
291 * device has already been open, change the speed on the open running port
296 sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS)
303 lwkt_gettoken(&tty_token);
304 newspeed = comdefaultrate;
306 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req);
307 if (error || !req->newptr) {
308 lwkt_reltoken(&tty_token);
312 comdefaultrate = newspeed;
314 if (comconsole < 0) { /* serial console not selected? */
315 lwkt_reltoken(&tty_token);
319 com = com_addr(comconsole);
321 lwkt_reltoken(&tty_token);
326 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX
327 * (note, the lock rates really are boolean -- if non-zero, disallow
330 com->it_in.c_ispeed = com->it_in.c_ospeed =
331 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
332 com->it_out.c_ispeed = com->it_out.c_ospeed =
333 com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate;
336 * if we're open, change the running rate too
339 if (tp && (tp->t_state & TS_ISOPEN)) {
340 tp->t_termios.c_ispeed =
341 tp->t_termios.c_ospeed = comdefaultrate;
343 error = comparam(tp, &tp->t_termios);
346 lwkt_reltoken(&tty_token);
350 SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW,
351 0, 0, sysctl_machdep_comdefaultrate, "I", "");
360 static struct pci_ids pci_ids[] = {
361 { 0x100812b9, "3COM PCI FaxModem", 0x10 },
362 { 0x2000131f, "CyberSerial (1-port) 16550", 0x10 },
363 { 0x01101407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 },
364 { 0x01111407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 },
365 { 0x048011c1, "Lucent kermit based PCI Modem", 0x14 },
366 { 0x95211415, "Oxford Semiconductor PCI Dual Port Serial", 0x10 },
367 { 0x7101135e, "SeaLevel Ultra 530.PCI Single Port Serial", 0x18 },
368 { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 },
369 { 0x98459710, "Netmos Nm9845 PCI Bridge with Dual UART", 0x10 },
370 { 0x00000000, NULL, 0 }
374 sio_pci_attach(device_t dev)
379 type = pci_get_devid(dev);
381 while (id->type && id->type != type)
383 if (id->desc == NULL)
385 sio_pci_kludge_unit(dev);
386 return (sioattach(dev, id->rid, 0UL));
390 * Don't cut and paste this to other drivers. It is a horrible kludge
391 * which will fail to work and also be unnecessary in future versions.
394 sio_pci_kludge_unit(device_t dev)
403 while (resource_int_value("sio", unit, "port", &start) == 0 &&
406 if (device_get_unit(dev) < unit) {
407 dc = device_get_devclass(dev);
408 while (devclass_get_device(dc, unit))
410 device_printf(dev, "moving to sio%d\n", unit);
411 err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */
413 device_printf(dev, "error moving device %d\n", err);
418 sio_pci_probe(device_t dev)
423 type = pci_get_devid(dev);
425 while (id->type && id->type != type)
427 if (id->desc == NULL)
429 device_set_desc(dev, id->desc);
430 return (sioprobe(dev, id->rid, 0UL));
432 #endif /* NPCI > 0 */
436 sio_puc_attach(device_t dev)
440 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ,
443 return (sioattach(dev, 0, rclk));
447 sio_puc_probe(device_t dev)
451 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ,
454 return (sioprobe(dev, 0, rclk));
458 static struct isa_pnp_id sio_ids[] = {
459 {0x0005d041, "Standard PC COM port"}, /* PNP0500 */
460 {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */
461 {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */
462 {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */
463 {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */
464 /* Devices that do not have a compatid */
465 {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */
466 {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */
467 {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */
468 {0x21107905, NULL}, /* AKY1021 - 56K Plug&Play Modem */
469 {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */
470 {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */
471 {0x56159008, NULL}, /* BDP1556 - B.D. Smart One 56SPS,Voice Modem*/
472 {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */
473 {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */
474 {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */
475 {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */
476 {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */
477 {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */
478 {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */
479 {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */
480 {0x01308c0e, NULL}, /* CTL3001 - Creative Labs Phoneblaster */
481 {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */
482 {0x01009416, NULL}, /* ETT0001 - E-Tech Bullet 33k6 PnP */
483 {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */
484 {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */
485 {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */
486 {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */
487 {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */
488 {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */
489 {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */
490 {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */
491 {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */
492 {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/
493 {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */
494 {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */
495 {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */
496 {0x6045f435, NULL}, /* MOT4560 - Motorola ? */
497 {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */
498 {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */
499 {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */
500 {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */
501 {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */
502 {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */
503 {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */
504 {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */
505 {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */
506 {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */
507 {0x1010104d, NULL}, /* SHP1010 - Rockwell 33600bps Modem */
508 {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */
509 {0x9012b04e, NULL}, /* SUP1290 - Supra ? */
510 {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */
511 {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */
512 {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */
513 {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */
514 {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */
515 {0x7420b04e, NULL}, /* SUP2070 - Supra ? */
516 {0x8020b04e, NULL}, /* SUP2080 - Supra ? */
517 {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */
518 {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */
519 {0x8024b04e, NULL}, /* SUP2480 - Supra ? */
520 {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */
521 {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */
522 {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */
523 {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */
524 {0x11007256, NULL}, /* USR0011 - USR ? */
525 {0x01017256, NULL}, /* USR0101 - USR ? */
526 {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */
527 {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */
528 {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */
529 {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */
530 {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */
531 {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */
532 {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */
533 {0x90307256, NULL}, /* USR3090 - USR ? */
534 {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */
535 {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */
536 {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */
537 {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */
538 {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */
545 sio_isa_probe(device_t dev)
547 /* Check isapnp ids */
548 if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO)
550 return (sioprobe(dev, 0, 0UL));
554 sioprobe(device_t dev, int xrid, u_long rclk)
557 static bool_t already_init;
566 intrmask_t irqmap[4];
571 u_int flags = device_get_flags(dev);
573 struct resource *port;
576 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
577 0, ~0, IO_COMSIZE, RF_ACTIVE);
581 lwkt_gettoken(&tty_token);
582 com = device_get_softc(dev);
583 com->bst = rman_get_bustag(port);
584 com->bsh = rman_get_bushandle(port);
591 * XXX this is broken - when we are first called, there are no
592 * previously configured IO ports. We could hard code
593 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse.
594 * This code has been doing nothing since the conversion since
595 * "count" is zero the first time around.
599 * Turn off MCR_IENABLE for all likely serial ports. An unused
600 * port with its MCR_IENABLE gate open will inhibit interrupts
601 * from any used port that shares the interrupt vector.
602 * XXX the gate enable is elsewhere for some multiports.
605 int count, i, xioport;
607 devclass_get_devices(sio_devclass, &devs, &count);
608 for (i = 0; i < count; i++) {
610 if (device_is_enabled(xdev) &&
611 bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport,
613 outb(xioport + com_mcr, 0);
620 if (COM_LLCONSOLE(flags)) {
621 kprintf("sio%d: reserved for low-level i/o\n",
622 device_get_unit(dev));
623 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
624 lwkt_reltoken(&tty_token);
629 * If the device is on a multiport card and has an AST/4
630 * compatible interrupt control register, initialize this
631 * register and prepare to leave MCR_IENABLE clear in the mcr.
632 * Otherwise, prepare to set MCR_IENABLE in the mcr.
633 * Point idev to the device struct giving the correct id_irq.
634 * This is the struct for the master device if there is one.
637 mcr_image = MCR_IENABLE;
639 if (COM_ISMULTIPORT(flags)) {
643 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags));
645 kprintf("sio%d: master device %d not configured\n",
646 device_get_unit(dev), COM_MPMASTER(flags));
649 if (!COM_NOTAST4(flags)) {
650 if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io,
653 if (bus_get_resource(idev, SYS_RES_IRQ, 0,
655 outb(xiobase + com_scr, 0x80);
657 outb(xiobase + com_scr, 0);
662 #endif /* COM_MULTIPORT */
663 if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0)
666 bzero(failures, sizeof failures);
667 iobase = rman_get_start(port);
670 * We don't want to get actual interrupts, just masked ones.
671 * Interrupts from this line should already be masked in the ICU,
672 * but mask them in the processor as well in case there are some
673 * (misconfigured) shared interrupts.
679 * For the TI16754 chips, set prescaler to 1 (4 is often the
680 * default after-reset value) as otherwise it's impossible to
681 * get highest baudrates.
683 if (COM_TI16754(flags)) {
686 cfcr = sio_getreg(com, com_cfcr);
687 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
688 efr = sio_getreg(com, com_efr);
689 /* Unlock extended features to turn off prescaler. */
690 sio_setreg(com, com_efr, efr | EFR_EFE);
692 sio_setreg(com, com_cfcr, (cfcr != CFCR_EFR_ENABLE) ? cfcr : 0);
693 /* Turn off prescaler. */
694 sio_setreg(com, com_mcr,
695 sio_getreg(com, com_mcr) & ~MCR_PRESCALE);
696 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
697 sio_setreg(com, com_efr, efr);
698 sio_setreg(com, com_cfcr, cfcr);
702 * Initialize the speed and the word size and wait long enough to
703 * drain the maximum of 16 bytes of junk in device output queues.
704 * The speed is undefined after a master reset and must be set
705 * before relying on anything related to output. There may be
706 * junk after a (very fast) soft reboot and (apparently) after
708 * XXX what about the UART bug avoided by waiting in comparam()?
709 * We don't want to to wait long enough to drain at 2 bps.
711 if (iobase == siocniobase) {
712 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10));
714 sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS);
715 divisor = siodivisor(rclk, SIO_TEST_SPEED);
716 sio_setreg(com, com_dlbl, divisor & 0xff);
717 sio_setreg(com, com_dlbh, divisor >> 8);
718 sio_setreg(com, com_cfcr, CFCR_8BITS);
719 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10));
723 * Make sure we can drain the receiver. If we can't, the serial
724 * port may not exist.
726 for (fn = 0; fn < 256; ++fn) {
727 if ((sio_getreg(com, com_lsr) & LSR_RXRDY) == 0)
729 (void)sio_getreg(com, com_data);
733 lwkt_reltoken(&tty_token);
734 kprintf("sio%d: can't drain, serial port might "
735 "not exist, disabling\n", device_get_unit(dev));
740 * Enable the interrupt gate and disable device interupts. This
741 * should leave the device driving the interrupt line low and
742 * guarantee an edge trigger if an interrupt can be generated.
745 sio_setreg(com, com_mcr, mcr_image);
746 sio_setreg(com, com_ier, 0);
747 DELAY(1000); /* XXX */
748 irqmap[0] = isa_irq_pending();
751 * Attempt to set loopback mode so that we can send a null byte
752 * without annoying any external device.
755 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK);
758 * Attempt to generate an output interrupt. On 8250's, setting
759 * IER_ETXRDY generates an interrupt independent of the current
760 * setting and independent of whether the THR is empty. On 16450's,
761 * setting IER_ETXRDY generates an interrupt independent of the
762 * current setting. On 16550A's, setting IER_ETXRDY only
763 * generates an interrupt when IER_ETXRDY is not already set.
765 sio_setreg(com, com_ier, IER_ETXRDY);
768 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate
769 * an interrupt. They'd better generate one for actually doing
770 * output. Loopback may be broken on the same incompatibles but
771 * it's unlikely to do more than allow the null byte out.
773 sio_setreg(com, com_data, 0);
774 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10));
777 * Turn off loopback mode so that the interrupt gate works again
778 * (MCR_IENABLE was hidden). This should leave the device driving
779 * an interrupt line high. It doesn't matter if the interrupt
780 * line oscillates while we are not looking at it, since interrupts
784 sio_setreg(com, com_mcr, mcr_image);
787 * Some pcmcia cards have the "TXRDY bug", so we check everyone
788 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... )
790 if (COM_NOPROBE(flags)) {
791 /* Reading IIR register twice */
792 for (fn = 0; fn < 2; fn ++) {
794 failures[6] = sio_getreg(com, com_iir);
796 /* Check IIR_TXRDY clear ? */
798 if (failures[6] & IIR_TXRDY) {
799 /* Nop, Double check with clearing IER */
800 sio_setreg(com, com_ier, 0);
801 if (sio_getreg(com, com_iir) & IIR_NOPEND) {
802 /* Ok. we're familia this gang */
803 SET_FLAG(dev, COM_C_IIR_TXRDYBUG);
805 /* Unknown, Just omit this chip.. XXX */
807 sio_setreg(com, com_mcr, 0);
810 /* OK. this is well-known guys */
811 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG);
813 sio_setreg(com, com_ier, 0);
814 sio_setreg(com, com_cfcr, CFCR_8BITS);
816 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
817 lwkt_reltoken(&tty_token);
818 return (iobase == siocniobase ? 0 : result);
823 * o the CFCR, IER and MCR in UART hold the values written to them
824 * (the values happen to be all distinct - this is good for
825 * avoiding false positive tests from bus echoes).
826 * o an output interrupt is generated and its vector is correct.
827 * o the interrupt goes away when the IIR in the UART is read.
830 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS;
831 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY;
832 failures[2] = sio_getreg(com, com_mcr) - mcr_image;
833 DELAY(10000); /* Some internal modems need this time */
834 irqmap[1] = isa_irq_pending();
835 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY;
836 DELAY(1000); /* XXX */
837 irqmap[2] = isa_irq_pending();
838 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
841 * Turn off all device interrupts and check that they go off properly.
842 * Leave MCR_IENABLE alone. For ports without a master port, it gates
843 * the OUT2 output of the UART to
844 * the ICU input. Closing the gate would give a floating ICU input
845 * (unless there is another device driving it) and spurious interrupts.
846 * (On the system that this was first tested on, the input floats high
847 * and gives a (masked) interrupt as soon as the gate is closed.)
849 sio_setreg(com, com_ier, 0);
850 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */
851 failures[7] = sio_getreg(com, com_ier);
852 DELAY(1000); /* XXX */
853 irqmap[3] = isa_irq_pending();
854 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
858 irqs = irqmap[1] & ~irqmap[0];
859 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 &&
860 ((1 << xirq) & irqs) == 0)
862 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n",
863 device_get_unit(dev), xirq, irqs);
865 kprintf("sio%d: irq maps: %#x %#x %#x %#x\n",
866 device_get_unit(dev),
867 irqmap[0], irqmap[1], irqmap[2], irqmap[3]);
870 for (fn = 0; fn < sizeof failures; ++fn)
872 sio_setreg(com, com_mcr, 0);
875 kprintf("sio%d: probe failed test(s):",
876 device_get_unit(dev));
877 for (fn = 0; fn < sizeof failures; ++fn)
884 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
885 lwkt_reltoken(&tty_token);
886 return (iobase == siocniobase ? 0 : result);
891 espattach(struct com_s *com, Port_t esp_port)
897 * Check the ESP-specific I/O port to see if we're an ESP
898 * card. If not, return failure immediately.
900 if ((inb(esp_port) & 0xf3) == 0) {
901 kprintf(" port 0x%x is not an ESP board?\n", esp_port);
905 lwkt_gettoken(&tty_token);
907 * We've got something that claims to be a Hayes ESP card.
911 /* Get the dip-switch configuration */
912 outb(esp_port + ESP_CMD1, ESP_GETDIPS);
913 dips = inb(esp_port + ESP_STATUS1);
916 * Bits 0,1 of dips say which COM port we are.
918 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03])
921 kprintf(" esp_port has com %d\n", dips & 0x03);
922 lwkt_reltoken(&tty_token);
927 * Check for ESP version 2.0 or later: bits 4,5,6 = 010.
929 outb(esp_port + ESP_CMD1, ESP_GETTEST);
930 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */
931 val = inb(esp_port + ESP_STATUS2);
932 if ((val & 0x70) < 0x20) {
933 kprintf("-old (%o)", val & 0x70);
934 lwkt_reltoken(&tty_token);
939 * Check for ability to emulate 16550: bit 7 == 1
941 if ((dips & 0x80) == 0) {
943 lwkt_reltoken(&tty_token);
948 * Okay, we seem to be a Hayes ESP card. Whee.
951 com->esp_port = esp_port;
952 lwkt_reltoken(&tty_token);
958 sio_isa_attach(device_t dev)
960 return (sioattach(dev, 0, 0UL));
964 sioattach(device_t dev, int xrid, u_long rclk)
975 struct resource *port;
979 lwkt_gettoken(&tty_token);
982 callout_init_mp(&sio_timeout_handle);
986 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
987 0, ~0, IO_COMSIZE, RF_ACTIVE);
989 lwkt_reltoken(&tty_token);
993 iobase = rman_get_start(port);
994 unit = device_get_unit(dev);
995 com = device_get_softc(dev);
996 flags = device_get_flags(dev);
998 if (unit >= sio_numunits)
999 sio_numunits = unit + 1;
1001 * sioprobe() has initialized the device registers as follows:
1002 * o cfcr = CFCR_8BITS.
1003 * It is most important that CFCR_DLAB is off, so that the
1004 * data port is not hidden when we enable interrupts.
1006 * Interrupts are only enabled when the line is open.
1007 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible
1008 * interrupt control register or the config specifies no irq.
1009 * Keeping MCR_DTR and MCR_RTS off might stop the external
1010 * device from sending before we are ready.
1012 bzero(com, sizeof *com);
1014 com->ioportres = port;
1015 com->bst = rman_get_bustag(port);
1016 com->bsh = rman_get_bushandle(port);
1017 com->cfcr_image = CFCR_8BITS;
1018 com->dtr_wait = 3 * hz;
1019 callout_init_mp(&com->dtr_ch);
1020 callout_init_mp(&com->busy_ch);
1021 com->loses_outints = COM_LOSESOUTINTS(flags) != 0;
1022 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0;
1023 com->tx_fifo_size = 1;
1024 com->obufs[0].l_head = com->obuf1;
1025 com->obufs[1].l_head = com->obuf2;
1027 com->data_port = iobase + com_data;
1028 com->int_id_port = iobase + com_iir;
1029 com->modem_ctl_port = iobase + com_mcr;
1030 com->mcr_image = inb(com->modem_ctl_port);
1031 com->line_status_port = iobase + com_lsr;
1032 com->modem_status_port = iobase + com_msr;
1033 com->intr_ctl_port = iobase + com_ier;
1036 rclk = DEFAULT_RCLK;
1040 * We don't use all the flags from <sys/ttydefaults.h> since they
1041 * are only relevant for logins. It's important to have echo off
1042 * initially so that the line doesn't start blathering before the
1043 * echo flag can be turned off.
1045 com->it_in.c_iflag = 0;
1046 com->it_in.c_oflag = 0;
1047 com->it_in.c_cflag = TTYDEF_CFLAG;
1048 com->it_in.c_lflag = 0;
1049 if (unit == comconsole) {
1050 com->it_in.c_iflag = TTYDEF_IFLAG;
1051 com->it_in.c_oflag = TTYDEF_OFLAG;
1052 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
1053 com->it_in.c_lflag = TTYDEF_LFLAG;
1054 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
1055 com->lt_out.c_ispeed = com->lt_out.c_ospeed =
1056 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
1057 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
1059 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED;
1060 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
1062 * Leave i/o resources allocated if this is a `cn'-level
1063 * console, so that other devices can't snarf them.
1065 if (iobase != siocniobase)
1066 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
1067 lwkt_reltoken(&tty_token);
1070 termioschars(&com->it_in);
1071 com->it_out = com->it_in;
1073 /* attempt to determine UART type */
1074 kprintf("sio%d: type", unit);
1077 #ifdef COM_MULTIPORT
1078 if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags))
1080 if (!COM_IIR_TXRDYBUG(flags))
1087 scr = sio_getreg(com, com_scr);
1088 sio_setreg(com, com_scr, 0xa5);
1089 scr1 = sio_getreg(com, com_scr);
1090 sio_setreg(com, com_scr, 0x5a);
1091 scr2 = sio_getreg(com, com_scr);
1092 sio_setreg(com, com_scr, scr);
1093 if (scr1 != 0xa5 || scr2 != 0x5a) {
1095 goto determined_type;
1098 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH);
1101 switch (inb(com->int_id_port) & IIR_FIFO_MASK) {
1112 if (COM_NOFIFO(flags)) {
1113 kprintf(" 16550A fifo disabled");
1115 com->hasfifo = TRUE;
1116 if (COM_ST16650A(flags)) {
1118 com->tx_fifo_size = 32;
1119 kprintf(" ST16650A");
1120 } else if (COM_TI16754(flags)) {
1121 com->tx_fifo_size = 64;
1122 kprintf(" TI16754");
1124 com->tx_fifo_size = COM_FIFOSIZE(flags);
1129 for (espp = likely_esp_ports; *espp != 0; espp++)
1130 if (espattach(com, *espp)) {
1131 com->tx_fifo_size = 1024;
1135 if (!com->st16650a && !COM_TI16754(flags)) {
1136 if (!com->tx_fifo_size)
1137 com->tx_fifo_size = 16;
1139 kprintf(" lookalike with %d bytes FIFO",
1149 * Set 16550 compatibility mode.
1150 * We don't use the ESP_MODE_SCALE bit to increase the
1151 * fifo trigger levels because we can't handle large
1153 * XXX flow control should be set in comparam(), not here.
1155 outb(com->esp_port + ESP_CMD1, ESP_SETMODE);
1156 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1158 /* Set RTS/CTS flow control. */
1159 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE);
1160 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS);
1161 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS);
1163 /* Set flow-control levels. */
1164 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW);
1165 outb(com->esp_port + ESP_CMD2, HIBYTE(768));
1166 outb(com->esp_port + ESP_CMD2, LOBYTE(768));
1167 outb(com->esp_port + ESP_CMD2, HIBYTE(512));
1168 outb(com->esp_port + ESP_CMD2, LOBYTE(512));
1170 #endif /* COM_ESP */
1171 sio_setreg(com, com_fifo, 0);
1174 #ifdef COM_MULTIPORT
1175 if (COM_ISMULTIPORT(flags)) {
1178 com->multiport = TRUE;
1179 kprintf(" (multiport");
1180 if (unit == COM_MPMASTER(flags))
1183 masterdev = devclass_get_device(sio_devclass,
1184 COM_MPMASTER(flags));
1185 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev,
1186 SYS_RES_IRQ, 0, NULL, NULL) != 0);
1188 #endif /* COM_MULTIPORT */
1189 if (unit == comconsole)
1190 kprintf(", console");
1191 if (COM_IIR_TXRDYBUG(flags))
1192 kprintf(" with a bogus IIR_TXRDY register");
1195 if (!sio_registered) {
1196 register_swi(SWI_TTY, siopoll, NULL ,"swi_siopoll", NULL);
1197 sio_registered = TRUE;
1199 minorbase = UNIT_TO_MINOR(unit);
1200 make_dev(&sio_ops, minorbase,
1201 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit);
1202 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
1203 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit);
1204 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
1205 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit);
1206 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
1207 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit);
1208 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
1209 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit);
1210 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
1211 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit);
1213 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
1214 pps_init(&com->pps);
1217 com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1,
1220 ret = BUS_SETUP_INTR(device_get_parent(dev), dev,
1221 com->irqres, INTR_MPSAFE, siointr, com,
1222 &com->cookie, NULL);
1224 device_printf(dev, "could not activate interrupt\n");
1225 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \
1226 defined(ALT_BREAK_TO_DEBUGGER))
1228 * Enable interrupts for early break-to-debugger support
1231 if (ret == 0 && unit == comconsole)
1232 outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS |
1237 lwkt_reltoken(&tty_token);
1242 sioopen(struct dev_open_args *ap)
1244 cdev_t dev = ap->a_head.a_dev;
1252 unit = MINOR_TO_UNIT(mynor);
1253 com = com_addr(unit);
1258 if (mynor & CONTROL_MASK)
1260 lwkt_gettoken(&tty_token);
1261 tp = dev->si_tty = com->tp = ttymalloc(com->tp);
1264 * We jump to this label after all non-interrupted sleeps to pick
1265 * up any changes of the device state.
1268 while (com->state & CS_DTR_OFF) {
1269 error = tsleep(&com->dtr_wait, PCATCH, "siodtr", 0);
1270 if (com_addr(unit) == NULL) {
1272 lwkt_reltoken(&tty_token);
1275 if (error != 0 || com->gone)
1278 if (tp->t_state & TS_ISOPEN) {
1280 * The device is open, so everything has been initialized.
1283 if (mynor & CALLOUT_MASK) {
1284 if (!com->active_out) {
1289 if (com->active_out) {
1290 if (ap->a_oflags & O_NONBLOCK) {
1294 error = tsleep(&com->active_out,
1295 PCATCH, "siobi", 0);
1296 if (com_addr(unit) == NULL) {
1298 lwkt_reltoken(&tty_token);
1301 if (error != 0 || com->gone)
1306 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
1312 * The device isn't open, so there are no conflicts.
1313 * Initialize it. Initialization is done twice in many
1314 * cases: to preempt sleeping callin opens if we are
1315 * callout, and to complete a callin open after DCD rises.
1317 tp->t_oproc = comstart;
1318 tp->t_param = comparam;
1319 tp->t_stop = comstop;
1321 tp->t_termios = mynor & CALLOUT_MASK
1322 ? com->it_out : com->it_in;
1323 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
1324 com->poll = com->no_irq;
1325 com->poll_output = com->loses_outints;
1327 error = comparam(tp, &tp->t_termios);
1332 * XXX we should goto open_top if comparam() slept.
1336 * (Re)enable and drain fifos.
1338 * Certain SMC chips cause problems if the fifos
1339 * are enabled while input is ready. Turn off the
1340 * fifo if necessary to clear the input. We test
1341 * the input ready bit after enabling the fifos
1342 * since we've already enabled them in comparam()
1343 * and to handle races between enabling and fresh
1347 sio_setreg(com, com_fifo,
1348 FIFO_RCV_RST | FIFO_XMT_RST
1351 * XXX the delays are for superstitious
1352 * historical reasons. It must be less than
1353 * the character time at the maximum
1354 * supported speed (87 usec at 115200 bps
1355 * 8N1). Otherwise we might loop endlessly
1356 * if data is streaming in. We used to use
1357 * delays of 100. That usually worked
1358 * because DELAY(100) used to usually delay
1359 * for about 85 usec instead of 100.
1362 if (!(inb(com->line_status_port) & LSR_RXRDY))
1364 sio_setreg(com, com_fifo, 0);
1366 (void) inb(com->data_port);
1371 (void) inb(com->line_status_port);
1372 (void) inb(com->data_port);
1373 com->prev_modem_status = com->last_modem_status
1374 = inb(com->modem_status_port);
1375 if (COM_IIR_TXRDYBUG(com->flags)) {
1376 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS
1379 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY
1380 | IER_ERLS | IER_EMSC);
1384 * Handle initial DCD. Callout devices get a fake initial
1385 * DCD (trapdoor DCD). If we are callout, then any sleeping
1386 * callin opens get woken up and resume sleeping on "siobi"
1387 * instead of "siodcd".
1390 * XXX `mynor & CALLOUT_MASK' should be
1391 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
1392 * TRAPDOOR_CARRIER is the default initial state for callout
1393 * devices and SOFT_CARRIER is like CLOCAL except it hides
1396 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
1397 (*linesw[tp->t_line].l_modem)(tp, 1);
1400 * Wait for DCD if necessary.
1402 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
1403 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
1405 error = tsleep(TSA_CARR_ON(tp), PCATCH, "siodcd", 0);
1406 if (com_addr(unit) == NULL) {
1408 lwkt_reltoken(&tty_token);
1412 if (error != 0 || com->gone)
1416 error = (*linesw[tp->t_line].l_open)(dev, tp);
1417 disc_optim(tp, &tp->t_termios, com);
1418 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
1419 com->active_out = TRUE;
1423 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
1425 lwkt_reltoken(&tty_token);
1430 sioclose(struct dev_close_args *ap)
1432 cdev_t dev = ap->a_head.a_dev;
1438 if (mynor & CONTROL_MASK)
1440 lwkt_gettoken(&tty_token);
1441 com = com_addr(MINOR_TO_UNIT(mynor));
1443 lwkt_reltoken(&tty_token);
1448 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
1449 disc_optim(tp, &tp->t_termios, com);
1450 comstop(tp, FREAD | FWRITE);
1456 kprintf("sio%d: gone\n", com->unit);
1458 if (com->ibuf != NULL)
1459 kfree(com->ibuf, M_DEVBUF);
1460 bzero(tp, sizeof *tp);
1463 lwkt_reltoken(&tty_token);
1468 comhardclose(struct com_s *com)
1475 lwkt_gettoken(&tty_token);
1477 com->poll_output = FALSE;
1478 com->do_timestamp = FALSE;
1479 com->do_dcd_timestamp = FALSE;
1480 com->pps.ppsparam.mode = 0;
1481 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1484 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \
1485 defined(ALT_BREAK_TO_DEBUGGER))
1487 * Leave interrupts enabled and don't clear DTR if this is the
1488 * console. This allows us to detect break-to-debugger events
1489 * while the console device is closed.
1491 if (com->unit != comconsole)
1494 sio_setreg(com, com_ier, 0);
1495 if (tp->t_cflag & HUPCL
1497 * XXX we will miss any carrier drop between here and the
1498 * next open. Perhaps we should watch DCD even when the
1499 * port is closed; it is not sufficient to check it at
1500 * the next open because it might go up and down while
1501 * we're not watching.
1503 || (!com->active_out
1504 && !(com->prev_modem_status & MSR_DCD)
1505 && !(com->it_in.c_cflag & CLOCAL))
1506 || !(tp->t_state & TS_ISOPEN)) {
1507 (void)commctl(com, TIOCM_DTR, DMBIC);
1508 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
1509 callout_reset(&com->dtr_ch, com->dtr_wait,
1511 com->state |= CS_DTR_OFF;
1517 * Disable fifos so that they are off after controlled
1518 * reboots. Some BIOSes fail to detect 16550s when the
1519 * fifos are enabled.
1521 sio_setreg(com, com_fifo, 0);
1523 com->active_out = FALSE;
1524 wakeup(&com->active_out);
1525 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
1526 lwkt_reltoken(&tty_token);
1531 sioread(struct dev_read_args *ap)
1533 cdev_t dev = ap->a_head.a_dev;
1537 lwkt_gettoken(&tty_token);
1539 if (mynor & CONTROL_MASK) {
1540 lwkt_reltoken(&tty_token);
1543 com = com_addr(MINOR_TO_UNIT(mynor));
1544 if (com == NULL || com->gone) {
1545 lwkt_reltoken(&tty_token);
1548 ret = ((*linesw[com->tp->t_line].l_read)(com->tp, ap->a_uio, ap->a_ioflag));
1549 lwkt_reltoken(&tty_token);
1554 siowrite(struct dev_write_args *ap)
1556 cdev_t dev = ap->a_head.a_dev;
1561 lwkt_gettoken(&tty_token);
1563 if (mynor & CONTROL_MASK) {
1564 lwkt_reltoken(&tty_token);
1568 unit = MINOR_TO_UNIT(mynor);
1569 com = com_addr(unit);
1570 if (com == NULL || com->gone) {
1571 lwkt_reltoken(&tty_token);
1575 * (XXX) We disallow virtual consoles if the physical console is
1576 * a serial port. This is in case there is a display attached that
1577 * is not the console. In that situation we don't need/want the X
1578 * server taking over the console.
1580 if (constty != NULL && unit == comconsole)
1582 ret = ((*linesw[com->tp->t_line].l_write)(com->tp, ap->a_uio, ap->a_ioflag));
1583 lwkt_reltoken(&tty_token);
1588 siobusycheck(void *chan)
1592 lwkt_gettoken(&tty_token);
1593 com = (struct com_s *)chan;
1596 * Clear TS_BUSY if low-level output is complete.
1597 * spl locking is sufficient because siointr1() does not set CS_BUSY.
1598 * If siointr1() clears CS_BUSY after we look at it, then we'll get
1599 * called again. Reading the line status port outside of siointr1()
1600 * is safe because CS_BUSY is clear so there are no output interrupts
1604 if (com->state & CS_BUSY)
1605 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */
1606 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
1607 == (LSR_TSRE | LSR_TXRDY)) {
1608 com->tp->t_state &= ~TS_BUSY;
1610 com->extra_state &= ~CSE_BUSYCHECK;
1612 callout_reset(&com->busy_ch, hz / 100, siobusycheck, com);
1615 lwkt_reltoken(&tty_token);
1619 siodivisor(u_long rclk, speed_t speed)
1625 if (speed == 0 || speed > ((speed_t)-1 - 1) / 8)
1627 divisor = (rclk / (8UL * speed) + 1) / 2;
1628 if (divisor == 0 || divisor >= 65536)
1630 actual_speed = rclk / (16UL * divisor);
1632 /* 10 times error in percent: */
1633 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2;
1635 /* 3.0% maximum error tolerance: */
1636 if (error < -30 || error > 30)
1643 siodtrwakeup(void *chan)
1647 lwkt_gettoken(&tty_token);
1648 com = (struct com_s *)chan;
1649 com->state &= ~CS_DTR_OFF;
1650 wakeup(&com->dtr_wait);
1651 lwkt_reltoken(&tty_token);
1655 * NOTE: Normally called with tty_token held but might not be when
1656 * operating as the console.
1658 * Must be called with com_lock
1661 sioinput(struct com_s *com)
1671 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) {
1672 com_events -= (com->iptr - com->ibuf);
1673 com->iptr = com->ibuf;
1676 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1678 * Avoid the grotesquely inefficient lineswitch routine
1679 * (ttyinput) in "raw" mode. It usually takes about 450
1680 * instructions (that's without canonical processing or echo!).
1681 * slinput is reasonably fast (usually 40 instructions plus
1686 incc = com->iptr - buf;
1687 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1688 && (com->state & CS_RTS_IFLOW
1689 || tp->t_iflag & IXOFF)
1690 && !(tp->t_state & TS_TBLOCK))
1692 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1693 += b_to_q((char *)buf, incc, &tp->t_rawq);
1697 tp->t_rawcc += incc;
1699 if (tp->t_state & TS_TTSTOP
1700 && (tp->t_iflag & IXANY
1701 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1702 tp->t_state &= ~TS_TTSTOP;
1703 tp->t_lflag &= ~FLUSHO;
1707 } while (buf < com->iptr);
1711 line_status = buf[com->ierroff];
1714 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1715 if (line_status & LSR_BI)
1716 recv_data |= TTY_BI;
1717 if (line_status & LSR_FE)
1718 recv_data |= TTY_FE;
1719 if (line_status & LSR_OE)
1720 recv_data |= TTY_OE;
1721 if (line_status & LSR_PE)
1722 recv_data |= TTY_PE;
1724 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1726 } while (buf < com->iptr);
1728 com_events -= (com->iptr - com->ibuf);
1729 com->iptr = com->ibuf;
1732 * There is now room for another low-level buffer full of input,
1733 * so enable RTS if it is now disabled and there is room in the
1734 * high-level buffer.
1736 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) &&
1737 !(tp->t_state & TS_TBLOCK))
1738 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1744 lwkt_gettoken(&tty_token);
1745 #ifndef COM_MULTIPORT
1747 siointr1((struct com_s *) arg);
1749 #else /* COM_MULTIPORT */
1750 bool_t possibly_more_intrs;
1755 * Loop until there is no activity on any port. This is necessary
1756 * to get an interrupt edge more than to avoid another interrupt.
1757 * If the IRQ signal is just an OR of the IRQ signals from several
1758 * devices, then the edge from one may be lost because another is
1763 possibly_more_intrs = FALSE;
1764 for (unit = 0; unit < sio_numunits; ++unit) {
1765 com = com_addr(unit);
1768 * would it work here, or be counter-productive?
1772 && (inb(com->int_id_port) & IIR_IMASK)
1775 possibly_more_intrs = TRUE;
1777 /* XXX com_unlock(); */
1779 } while (possibly_more_intrs);
1781 #endif /* COM_MULTIPORT */
1782 lwkt_reltoken(&tty_token);
1786 * Called with tty_token held and com_lock held.
1789 siointr1(struct com_s *com)
1792 u_char modem_status;
1799 int_ctl = inb(com->intr_ctl_port);
1800 int_ctl_new = int_ctl;
1802 while (!com->gone) {
1803 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) {
1804 modem_status = inb(com->modem_status_port);
1805 if ((modem_status ^ com->last_modem_status) & MSR_DCD) {
1806 count = sys_cputimer->count();
1807 pps_event(&com->pps, count,
1808 (modem_status & MSR_DCD) ?
1809 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
1812 line_status = inb(com->line_status_port);
1814 /* input event? (check first to help avoid overruns) */
1815 while (line_status & LSR_RCV_MASK) {
1816 /* break/unnattached error bits or real input? */
1817 if (!(line_status & LSR_RXRDY))
1820 recv_data = inb(com->data_port);
1821 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
1823 * Solaris implements a new BREAK which is initiated
1824 * by a character sequence CR ~ ^b which is similar
1825 * to a familiar pattern used on Sun servers by the
1828 #define KEY_CRTLB 2 /* ^B */
1829 #define KEY_CR 13 /* CR '\r' */
1830 #define KEY_TILDE 126 /* ~ */
1832 if (com->unit == comconsole) {
1833 static int brk_state1 = 0, brk_state2 = 0;
1834 if (recv_data == KEY_CR) {
1835 brk_state1 = recv_data;
1837 } else if (brk_state1 == KEY_CR && (recv_data == KEY_TILDE || recv_data == KEY_CRTLB)) {
1838 if (recv_data == KEY_TILDE)
1839 brk_state2 = recv_data;
1840 else if (brk_state2 == KEY_TILDE && recv_data == KEY_CRTLB) {
1844 brk_state1 = brk_state2 = 0;
1852 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) {
1854 * Don't store BI if IGNBRK or FE/PE if IGNPAR.
1855 * Otherwise, push the work to a higher level
1856 * (to handle PARMRK) if we're bypassing.
1857 * Otherwise, convert BI/FE and PE+INPCK to 0.
1859 * This makes bypassing work right in the
1860 * usual "raw" case (IGNBRK set, and IGNPAR
1863 * Note: BI together with FE/PE means just BI.
1865 if (line_status & LSR_BI) {
1866 #if defined(DDB) && defined(BREAK_TO_DEBUGGER)
1867 if (com->unit == comconsole) {
1875 || com->tp->t_iflag & IGNBRK)
1879 || com->tp->t_iflag & IGNPAR)
1882 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT
1883 && (line_status & (LSR_BI | LSR_FE)
1884 || com->tp->t_iflag & INPCK))
1888 if (com->hotchar != 0 && recv_data == com->hotchar)
1891 if (ioptr >= com->ibufend)
1892 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1894 if (com->do_timestamp)
1895 microtime(&com->timestamp);
1898 #if 0 /* for testing input latency vs efficiency */
1899 if (com->iptr - com->ibuf == 8)
1902 ioptr[0] = recv_data;
1903 ioptr[com->ierroff] = line_status;
1904 com->iptr = ++ioptr;
1905 if (ioptr == com->ihighwater
1906 && com->state & CS_RTS_IFLOW)
1907 outb(com->modem_ctl_port,
1908 com->mcr_image &= ~MCR_RTS);
1909 if (line_status & LSR_OE)
1910 CE_RECORD(com, CE_OVERRUN);
1914 * "& 0x7F" is to avoid the gcc-1.40 generating a slow
1915 * jump from the top of the loop to here
1917 line_status = inb(com->line_status_port) & 0x7F;
1920 /* modem status change? (always check before doing output) */
1921 modem_status = inb(com->modem_status_port);
1922 if (modem_status != com->last_modem_status) {
1923 if (com->do_dcd_timestamp
1924 && !(com->last_modem_status & MSR_DCD)
1925 && modem_status & MSR_DCD)
1926 microtime(&com->dcd_timestamp);
1929 * Schedule high level to handle DCD changes. Note
1930 * that we don't use the delta bits anywhere. Some
1931 * UARTs mess them up, and it's easy to remember the
1932 * previous bits and calculate the delta.
1934 com->last_modem_status = modem_status;
1935 if (!(com->state & CS_CHECKMSR)) {
1936 com_events += LOTS_OF_EVENTS;
1937 com->state |= CS_CHECKMSR;
1941 /* handle CTS change immediately for crisp flow ctl */
1942 if (com->state & CS_CTS_OFLOW) {
1943 if (modem_status & MSR_CTS)
1944 com->state |= CS_ODEVREADY;
1946 com->state &= ~CS_ODEVREADY;
1950 /* output queued and everything ready? */
1951 if (line_status & LSR_TXRDY
1952 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1953 ioptr = com->obufq.l_head;
1954 if (com->tx_fifo_size > 1) {
1957 ocount = com->obufq.l_tail - ioptr;
1958 if (ocount > com->tx_fifo_size)
1959 ocount = com->tx_fifo_size;
1960 com->bytes_out += ocount;
1962 outb(com->data_port, *ioptr++);
1963 while (--ocount != 0);
1965 outb(com->data_port, *ioptr++);
1968 com->obufq.l_head = ioptr;
1969 if (COM_IIR_TXRDYBUG(com->flags)) {
1970 int_ctl_new = int_ctl | IER_ETXRDY;
1972 if (ioptr >= com->obufq.l_tail) {
1975 qp = com->obufq.l_next;
1976 qp->l_queued = FALSE;
1979 com->obufq.l_head = qp->l_head;
1980 com->obufq.l_tail = qp->l_tail;
1981 com->obufq.l_next = qp;
1983 /* output just completed */
1984 if (COM_IIR_TXRDYBUG(com->flags)) {
1985 int_ctl_new = int_ctl & ~IER_ETXRDY;
1987 com->state &= ~CS_BUSY;
1989 if (!(com->state & CS_ODONE)) {
1990 com_events += LOTS_OF_EVENTS;
1991 com->state |= CS_ODONE;
1992 setsofttty(); /* handle at high level ASAP */
1995 if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) {
1996 outb(com->intr_ctl_port, int_ctl_new);
2001 #ifndef COM_MULTIPORT
2002 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND)
2003 #endif /* COM_MULTIPORT */
2011 sioioctl(struct dev_ioctl_args *ap)
2013 cdev_t dev = ap->a_head.a_dev;
2014 caddr_t data = ap->a_data;
2019 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2021 struct termios term;
2023 lwkt_gettoken(&tty_token);
2026 com = com_addr(MINOR_TO_UNIT(mynor));
2027 if (com == NULL || com->gone) {
2028 lwkt_reltoken(&tty_token);
2031 if (mynor & CONTROL_MASK) {
2034 switch (mynor & CONTROL_MASK) {
2035 case CONTROL_INIT_STATE:
2036 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
2038 case CONTROL_LOCK_STATE:
2039 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
2042 lwkt_reltoken(&tty_token);
2043 return (ENODEV); /* /dev/nodev */
2045 switch (ap->a_cmd) {
2047 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
2050 *ct = *(struct termios *)data;
2051 lwkt_reltoken(&tty_token);
2054 *(struct termios *)data = *ct;
2055 lwkt_reltoken(&tty_token);
2058 *(int *)data = TTYDISC;
2059 lwkt_reltoken(&tty_token);
2062 bzero(data, sizeof(struct winsize));
2063 lwkt_reltoken(&tty_token);
2066 lwkt_reltoken(&tty_token);
2071 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2072 term = tp->t_termios;
2074 error = ttsetcompat(tp, &ap->a_cmd, data, &term);
2076 lwkt_reltoken(&tty_token);
2079 if (ap->a_cmd != oldcmd)
2080 data = (caddr_t)&term;
2082 if (ap->a_cmd == TIOCSETA || ap->a_cmd == TIOCSETAW ||
2083 ap->a_cmd == TIOCSETAF) {
2085 struct termios *dt = (struct termios *)data;
2086 struct termios *lt = mynor & CALLOUT_MASK
2087 ? &com->lt_out : &com->lt_in;
2089 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
2090 | (dt->c_iflag & ~lt->c_iflag);
2091 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
2092 | (dt->c_oflag & ~lt->c_oflag);
2093 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
2094 | (dt->c_cflag & ~lt->c_cflag);
2095 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
2096 | (dt->c_lflag & ~lt->c_lflag);
2097 for (cc = 0; cc < NCCS; ++cc)
2098 if (lt->c_cc[cc] != 0)
2099 dt->c_cc[cc] = tp->t_cc[cc];
2100 if (lt->c_ispeed != 0)
2101 dt->c_ispeed = tp->t_ispeed;
2102 if (lt->c_ospeed != 0)
2103 dt->c_ospeed = tp->t_ospeed;
2105 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, data, ap->a_fflag, ap->a_cred);
2106 if (error != ENOIOCTL) {
2107 lwkt_reltoken(&tty_token);
2111 error = ttioctl(tp, ap->a_cmd, data, ap->a_fflag);
2112 disc_optim(tp, &tp->t_termios, com);
2113 if (error != ENOIOCTL) {
2115 lwkt_reltoken(&tty_token);
2118 switch (ap->a_cmd) {
2120 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK);
2123 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2126 (void)commctl(com, TIOCM_DTR, DMBIS);
2129 (void)commctl(com, TIOCM_DTR, DMBIC);
2132 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
2133 * changes get undone on the next call to comparam().
2136 (void)commctl(com, *(int *)data, DMSET);
2139 (void)commctl(com, *(int *)data, DMBIS);
2142 (void)commctl(com, *(int *)data, DMBIC);
2145 *(int *)data = commctl(com, 0, DMGET);
2148 /* must be root since the wait applies to following logins */
2149 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
2152 lwkt_reltoken(&tty_token);
2155 com->dtr_wait = *(int *)data * hz / 100;
2158 *(int *)data = com->dtr_wait * 100 / hz;
2161 com->do_timestamp = TRUE;
2162 *(struct timeval *)data = com->timestamp;
2164 case TIOCDCDTIMESTAMP:
2165 com->do_dcd_timestamp = TRUE;
2166 *(struct timeval *)data = com->dcd_timestamp;
2170 error = pps_ioctl(ap->a_cmd, data, &com->pps);
2171 if (error == ENODEV)
2173 lwkt_reltoken(&tty_token);
2177 lwkt_reltoken(&tty_token);
2182 siopoll(void *dummy, void *frame)
2186 lwkt_gettoken(&tty_token);
2187 if (com_events == 0) {
2188 lwkt_reltoken(&tty_token);
2193 for (unit = 0; unit < sio_numunits; ++unit) {
2198 com = com_addr(unit);
2202 if (tp == NULL || com->gone) {
2204 * Discard any events related to never-opened or
2205 * going-away devices.
2208 incc = com->iptr - com->ibuf;
2209 com->iptr = com->ibuf;
2210 if (com->state & CS_CHECKMSR) {
2211 incc += LOTS_OF_EVENTS;
2212 com->state &= ~CS_CHECKMSR;
2218 if (com->iptr != com->ibuf) {
2223 if (com->state & CS_CHECKMSR) {
2224 u_char delta_modem_status;
2227 delta_modem_status = com->last_modem_status
2228 ^ com->prev_modem_status;
2229 com->prev_modem_status = com->last_modem_status;
2230 com_events -= LOTS_OF_EVENTS;
2231 com->state &= ~CS_CHECKMSR;
2233 if (delta_modem_status & MSR_DCD)
2234 (*linesw[tp->t_line].l_modem)
2235 (tp, com->prev_modem_status & MSR_DCD);
2237 if (com->state & CS_ODONE) {
2239 com_events -= LOTS_OF_EVENTS;
2240 com->state &= ~CS_ODONE;
2242 if (!(com->state & CS_BUSY)
2243 && !(com->extra_state & CSE_BUSYCHECK)) {
2244 callout_reset(&com->busy_ch, hz / 100,
2246 com->extra_state |= CSE_BUSYCHECK;
2248 (*linesw[tp->t_line].l_start)(tp);
2250 if (com_events == 0)
2253 if (com_events >= LOTS_OF_EVENTS)
2255 lwkt_reltoken(&tty_token);
2259 * Called with tty_token held but no com_lock
2262 comparam(struct tty *tp, struct termios *t)
2272 unit = DEV_TO_UNIT(tp->t_dev);
2273 com = com_addr(unit);
2278 /* do historical conversions */
2279 if (t->c_ispeed == 0)
2280 t->c_ispeed = t->c_ospeed;
2282 /* check requested parameters */
2283 if (t->c_ospeed == 0)
2286 if (t->c_ispeed != t->c_ospeed)
2288 divisor = siodivisor(com->rclk, t->c_ispeed);
2293 /* parameters are OK, convert them to the com struct and the device */
2296 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
2298 (void)commctl(com, TIOCM_DTR, DMBIS);
2300 switch (cflag & CSIZE) {
2314 if (cflag & PARENB) {
2316 if (!(cflag & PARODD))
2322 if (com->hasfifo && divisor != 0) {
2324 * Use a fifo trigger level low enough so that the input
2325 * latency from the fifo is less than about 16 msec and
2326 * the total latency is less than about 30 msec. These
2327 * latencies are reasonable for humans. Serial comms
2328 * protocols shouldn't expect anything better since modem
2329 * latencies are larger.
2331 * Interrupts can be held up for long periods of time
2332 * due to inefficiencies in other parts of the kernel,
2333 * certain video cards, etc. Setting the FIFO trigger
2334 * point to MEDH instead of HIGH gives us 694uS of slop
2335 * (8 character times) instead of 173uS (2 character times)
2338 com->fifo_image = t->c_ospeed <= 4800
2339 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH;
2342 * The Hayes ESP card needs the fifo DMA mode bit set
2343 * in compatibility mode. If not, it will interrupt
2344 * for each character received.
2347 com->fifo_image |= FIFO_DMA_MODE;
2349 sio_setreg(com, com_fifo, com->fifo_image);
2353 * This returns with interrupts disabled so that we can complete
2354 * the speed change atomically. Keeping interrupts disabled is
2355 * especially important while com_data is hidden.
2357 (void) siosetwater(com, t->c_ispeed);
2360 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB);
2362 * Only set the divisor registers if they would change,
2363 * since on some 16550 incompatibles (UMC8669F), setting
2364 * them while input is arriving them loses sync until
2365 * data stops arriving.
2367 dlbl = divisor & 0xFF;
2368 if (sio_getreg(com, com_dlbl) != dlbl)
2369 sio_setreg(com, com_dlbl, dlbl);
2370 dlbh = divisor >> 8;
2371 if (sio_getreg(com, com_dlbh) != dlbh)
2372 sio_setreg(com, com_dlbh, dlbh);
2375 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr);
2377 if (!(tp->t_state & TS_TTSTOP))
2378 com->state |= CS_TTGO;
2380 if (cflag & CRTS_IFLOW) {
2381 if (com->st16650a) {
2382 sio_setreg(com, com_cfcr, 0xbf);
2383 sio_setreg(com, com_fifo,
2384 sio_getreg(com, com_fifo) | 0x40);
2386 com->state |= CS_RTS_IFLOW;
2388 * If CS_RTS_IFLOW just changed from off to on, the change
2389 * needs to be propagated to MCR_RTS. This isn't urgent,
2390 * so do it later by calling comstart() instead of repeating
2391 * a lot of code from comstart() here.
2393 } else if (com->state & CS_RTS_IFLOW) {
2394 com->state &= ~CS_RTS_IFLOW;
2396 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2397 * on here, since comstart() won't do it later.
2399 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2400 if (com->st16650a) {
2401 sio_setreg(com, com_cfcr, 0xbf);
2402 sio_setreg(com, com_fifo,
2403 sio_getreg(com, com_fifo) & ~0x40);
2409 * Set up state to handle output flow control.
2410 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2411 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2413 com->state |= CS_ODEVREADY;
2414 com->state &= ~CS_CTS_OFLOW;
2415 if (cflag & CCTS_OFLOW) {
2416 com->state |= CS_CTS_OFLOW;
2417 if (!(com->last_modem_status & MSR_CTS))
2418 com->state &= ~CS_ODEVREADY;
2419 if (com->st16650a) {
2420 sio_setreg(com, com_cfcr, 0xbf);
2421 sio_setreg(com, com_fifo,
2422 sio_getreg(com, com_fifo) | 0x80);
2425 if (com->st16650a) {
2426 sio_setreg(com, com_cfcr, 0xbf);
2427 sio_setreg(com, com_fifo,
2428 sio_getreg(com, com_fifo) & ~0x80);
2432 sio_setreg(com, com_cfcr, com->cfcr_image);
2434 /* XXX shouldn't call functions while intrs are disabled. */
2435 disc_optim(tp, t, com);
2437 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2438 * unconditionally, but that defeated the careful discarding of
2439 * stale input in sioopen().
2441 if (com->state >= (CS_BUSY | CS_TTGO)) {
2448 if (com->ibufold != NULL) {
2449 kfree(com->ibufold, M_DEVBUF);
2450 com->ibufold = NULL;
2456 * called with tty_token held
2459 siosetwater(struct com_s *com, speed_t speed)
2467 * Make the buffer size large enough to handle a softtty interrupt
2468 * latency of about 2 ticks without loss of throughput or data
2469 * (about 3 ticks if input flow control is not used or not honoured,
2470 * but a bit less for CS5-CS7 modes).
2472 cp4ticks = speed / 10 / hz * 4;
2473 for (ibufsize = 128; ibufsize < cp4ticks;)
2475 if (ibufsize == com->ibufsize)
2479 * Allocate input buffer. The extra factor of 2 in the size is
2480 * to allow for an error byte for each input byte.
2482 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK | M_ZERO);
2484 /* Initialize non-critical variables. */
2485 com->ibufold = com->ibuf;
2486 com->ibufsize = ibufsize;
2489 tp->t_ififosize = 2 * ibufsize;
2490 tp->t_ispeedwat = (speed_t)-1;
2491 tp->t_ospeedwat = (speed_t)-1;
2495 * Read current input buffer.
2498 if (com->iptr != com->ibuf)
2502 * Initialize critical variables, including input buffer watermarks.
2503 * The external device is asked to stop sending when the buffer
2504 * exactly reaches high water, or when the high level requests it.
2505 * The high level is notified immediately (rather than at a later
2506 * clock tick) when this watermark is reached.
2507 * The buffer size is chosen so the watermark should almost never
2509 * The low watermark is invisibly 0 since the buffer is always
2510 * emptied all at once.
2512 com->iptr = com->ibuf = ibuf;
2513 com->ibufend = ibuf + ibufsize;
2514 com->ierroff = ibufsize;
2515 com->ihighwater = ibuf + 3 * ibufsize / 4;
2521 comstart(struct tty *tp)
2526 lwkt_gettoken(&tty_token);
2527 unit = DEV_TO_UNIT(tp->t_dev);
2528 com = com_addr(unit);
2530 lwkt_reltoken(&tty_token);
2535 if (tp->t_state & TS_TTSTOP)
2536 com->state &= ~CS_TTGO;
2538 com->state |= CS_TTGO;
2539 if (tp->t_state & TS_TBLOCK) {
2540 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW)
2541 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2543 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater
2544 && com->state & CS_RTS_IFLOW)
2545 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2548 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2551 lwkt_reltoken(&tty_token);
2554 if (tp->t_outq.c_cc != 0) {
2558 if (!com->obufs[0].l_queued) {
2559 com->obufs[0].l_tail
2560 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2562 com->obufs[0].l_next = NULL;
2563 com->obufs[0].l_queued = TRUE;
2565 if (com->state & CS_BUSY) {
2566 qp = com->obufq.l_next;
2567 while ((next = qp->l_next) != NULL)
2569 qp->l_next = &com->obufs[0];
2571 com->obufq.l_head = com->obufs[0].l_head;
2572 com->obufq.l_tail = com->obufs[0].l_tail;
2573 com->obufq.l_next = &com->obufs[0];
2574 com->state |= CS_BUSY;
2578 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2579 com->obufs[1].l_tail
2580 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2582 com->obufs[1].l_next = NULL;
2583 com->obufs[1].l_queued = TRUE;
2585 if (com->state & CS_BUSY) {
2586 qp = com->obufq.l_next;
2587 while ((next = qp->l_next) != NULL)
2589 qp->l_next = &com->obufs[1];
2591 com->obufq.l_head = com->obufs[1].l_head;
2592 com->obufq.l_tail = com->obufs[1].l_tail;
2593 com->obufq.l_next = &com->obufs[1];
2594 com->state |= CS_BUSY;
2598 tp->t_state |= TS_BUSY;
2601 if (com->state >= (CS_BUSY | CS_TTGO))
2602 siointr1(com); /* fake interrupt to start output */
2606 lwkt_reltoken(&tty_token);
2610 comstop(struct tty *tp, int rw)
2614 lwkt_gettoken(&tty_token);
2615 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2616 if (com == NULL || com->gone) {
2617 lwkt_reltoken(&tty_token);
2624 /* XXX avoid h/w bug. */
2627 sio_setreg(com, com_fifo,
2628 FIFO_XMT_RST | com->fifo_image);
2629 com->obufs[0].l_queued = FALSE;
2630 com->obufs[1].l_queued = FALSE;
2631 if (com->state & CS_ODONE)
2632 com_events -= LOTS_OF_EVENTS;
2633 com->state &= ~(CS_ODONE | CS_BUSY);
2634 com->tp->t_state &= ~TS_BUSY;
2639 /* XXX avoid h/w bug. */
2642 sio_setreg(com, com_fifo,
2643 FIFO_RCV_RST | com->fifo_image);
2644 com_events -= (com->iptr - com->ibuf);
2645 com->iptr = com->ibuf;
2649 lwkt_reltoken(&tty_token);
2653 commctl(struct com_s *com, int bits, int how)
2658 lwkt_gettoken(&tty_token);
2660 bits = TIOCM_LE; /* XXX - always enabled while open */
2661 mcr = com->mcr_image;
2666 msr = com->prev_modem_status;
2674 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI
2675 * more volatile by reading the modem status a lot. Perhaps
2676 * we should latch both bits until the status is read here.
2678 if (msr & (MSR_RI | MSR_TERI))
2680 lwkt_reltoken(&tty_token);
2684 if (bits & TIOCM_DTR)
2686 if (bits & TIOCM_RTS)
2689 lwkt_reltoken(&tty_token);
2695 outb(com->modem_ctl_port,
2696 com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE));
2699 outb(com->modem_ctl_port, com->mcr_image |= mcr);
2702 outb(com->modem_ctl_port, com->mcr_image &= ~mcr);
2706 lwkt_reltoken(&tty_token);
2711 * NOTE: Must be called with tty_token held
2720 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2722 * Set our timeout period to 1 second if no polled devices are open.
2723 * Otherwise set it to max(1/200, 1/hz).
2724 * Enable timeouts iff some device is open.
2726 callout_stop(&sio_timeout_handle);
2729 for (unit = 0; unit < sio_numunits; ++unit) {
2730 com = com_addr(unit);
2731 if (com != NULL && com->tp != NULL
2732 && com->tp->t_state & TS_ISOPEN && !com->gone) {
2734 if (com->poll || com->poll_output) {
2735 sio_timeout = hz > 200 ? hz / 200 : 1;
2741 sio_timeouts_until_log = hz / sio_timeout;
2742 callout_reset(&sio_timeout_handle, sio_timeout,
2745 /* Flush error messages, if any. */
2746 sio_timeouts_until_log = 1;
2748 callout_stop(&sio_timeout_handle);
2753 * NOTE: Must be called with tty_token held
2756 comwakeup(void *chan)
2762 * Can be called from a callout too so just get the token
2764 lwkt_gettoken(&tty_token);
2765 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2768 * Recover from lost output interrupts.
2769 * Poll any lines that don't use interrupts.
2771 for (unit = 0; unit < sio_numunits; ++unit) {
2772 com = com_addr(unit);
2773 if (com != NULL && !com->gone
2774 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2782 * Check for and log errors, but not too often.
2784 if (--sio_timeouts_until_log > 0) {
2785 lwkt_reltoken(&tty_token);
2788 sio_timeouts_until_log = hz / sio_timeout;
2789 for (unit = 0; unit < sio_numunits; ++unit) {
2792 com = com_addr(unit);
2797 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2802 delta = com->delta_error_counts[errnum];
2803 com->delta_error_counts[errnum] = 0;
2807 total = com->error_counts[errnum] += delta;
2808 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n",
2809 unit, delta, error_desc[errnum],
2810 delta == 1 ? "" : "s", total);
2813 lwkt_reltoken(&tty_token);
2817 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2819 lwkt_gettoken(&tty_token);
2820 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2821 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2822 && (!(t->c_iflag & PARMRK)
2823 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2824 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2825 && linesw[tp->t_line].l_rint == ttyinput)
2826 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2828 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2829 com->hotchar = linesw[tp->t_line].l_hotchar;
2830 lwkt_reltoken(&tty_token);
2834 * Following are all routines needed for SIO to act as console
2836 #include <sys/cons.h>
2846 static speed_t siocngetspeed (Port_t, u_long rclk);
2847 static void siocnclose (struct siocnstate *sp, Port_t iobase);
2848 static void siocnopen (struct siocnstate *sp, Port_t iobase, int speed);
2849 static void siocntxwait (Port_t iobase);
2851 static cn_probe_t siocnprobe;
2852 static cn_init_t siocninit;
2853 static cn_init_fini_t siocninit_fini;
2854 static cn_checkc_t siocncheckc;
2855 static cn_getc_t siocngetc;
2856 static cn_putc_t siocnputc;
2858 #if defined(__i386__) || defined(__x86_64__)
2859 CONS_DRIVER(sio, siocnprobe, siocninit, siocninit_fini,
2860 NULL, siocngetc, siocncheckc, siocnputc, NULL);
2863 /* To get the GDB related variables */
2865 #include <ddb/ddb.h>
2869 siocntxwait(Port_t iobase)
2874 * Wait for any pending transmission to finish. Required to avoid
2875 * the UART lockup bug when the speed is changed, and for normal
2879 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY))
2880 != (LSR_TSRE | LSR_TXRDY) && --timo != 0)
2885 * Read the serial port specified and try to figure out what speed
2886 * it's currently running at. We're assuming the serial port has
2887 * been initialized and is basicly idle. This routine is only intended
2888 * to be run at system startup.
2890 * If the value read from the serial port doesn't make sense, return 0.
2893 * NOTE: Must be called with tty_token held
2896 siocngetspeed(Port_t iobase, u_long rclk)
2903 cfcr = inb(iobase + com_cfcr);
2904 outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
2906 dlbl = inb(iobase + com_dlbl);
2907 dlbh = inb(iobase + com_dlbh);
2909 outb(iobase + com_cfcr, cfcr);
2911 divisor = dlbh << 8 | dlbl;
2913 /* XXX there should be more sanity checking. */
2916 return (rclk / (16UL * divisor));
2920 siocnopen(struct siocnstate *sp, Port_t iobase, int speed)
2927 * Save all the device control registers except the fifo register
2928 * and set our default ones (cs8 -parenb speed=comdefaultrate).
2929 * We can't save the fifo register since it is read-only.
2931 sp->ier = inb(iobase + com_ier);
2932 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */
2933 siocntxwait(iobase);
2934 sp->cfcr = inb(iobase + com_cfcr);
2935 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2936 sp->dlbl = inb(iobase + com_dlbl);
2937 sp->dlbh = inb(iobase + com_dlbh);
2939 * Only set the divisor registers if they would change, since on
2940 * some 16550 incompatibles (Startech), setting them clears the
2941 * data input register. This also reduces the effects of the
2944 divisor = siodivisor(comdefaultrclk, speed);
2945 dlbl = divisor & 0xFF;
2946 if (sp->dlbl != dlbl)
2947 outb(iobase + com_dlbl, dlbl);
2948 dlbh = divisor >> 8;
2949 if (sp->dlbh != dlbh)
2950 outb(iobase + com_dlbh, dlbh);
2951 outb(iobase + com_cfcr, CFCR_8BITS);
2952 sp->mcr = inb(iobase + com_mcr);
2954 * We don't want interrupts, but must be careful not to "disable"
2955 * them by clearing the MCR_IENABLE bit, since that might cause
2956 * an interrupt by floating the IRQ line.
2958 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS);
2962 siocnclose(struct siocnstate *sp, Port_t iobase)
2965 * Restore the device control registers.
2967 siocntxwait(iobase);
2968 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2969 if (sp->dlbl != inb(iobase + com_dlbl))
2970 outb(iobase + com_dlbl, sp->dlbl);
2971 if (sp->dlbh != inb(iobase + com_dlbh))
2972 outb(iobase + com_dlbh, sp->dlbh);
2973 outb(iobase + com_cfcr, sp->cfcr);
2975 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them.
2977 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS);
2978 outb(iobase + com_ier, sp->ier);
2982 siocnprobe(struct consdev *cp)
2988 struct siocnstate sp;
2991 * Find our first enabled console, if any. If it is a high-level
2992 * console device, then initialize it and return successfully.
2993 * If it is a low-level console device, then initialize it and
2994 * return unsuccessfully. It must be initialized in both cases
2995 * for early use by console drivers and debuggers. Initializing
2996 * the hardware is not necessary in all cases, since the i/o
2997 * routines initialize it on the fly, but it is necessary if
2998 * input might arrive while the hardware is switched back to an
2999 * uninitialized state. We can't handle multiple console devices
3000 * yet because our low-level routines don't take a device arg.
3001 * We trust the user to set the console flags properly so that we
3002 * don't need to probe.
3004 cp->cn_pri = CN_DEAD;
3006 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */
3009 if (resource_int_value("sio", unit, "disabled", &disabled) == 0) {
3013 if (resource_int_value("sio", unit, "flags", &flags))
3015 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) {
3019 if (resource_int_value("sio", unit, "port", &port))
3023 if (boothowto & RB_SERIAL) {
3025 siocngetspeed(iobase, comdefaultrclk);
3027 comdefaultrate = boot_speed;
3031 * Initialize the divisor latch. We can't rely on
3032 * siocnopen() to do this the first time, since it
3033 * avoids writing to the latch if the latch appears
3034 * to have the correct value. Also, if we didn't
3035 * just read the speed from the hardware, then we
3036 * need to set the speed in hardware so that
3037 * switching it later is null.
3040 cfcr = inb(iobase + com_cfcr);
3041 outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
3042 divisor = siodivisor(comdefaultrclk, comdefaultrate);
3043 outb(iobase + com_dlbl, divisor & 0xff);
3044 outb(iobase + com_dlbh, divisor >> 8);
3045 outb(iobase + com_cfcr, cfcr);
3047 siocnopen(&sp, iobase, comdefaultrate);
3051 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) {
3052 cp->cn_probegood = 1;
3053 cp->cn_private = (void *)(intptr_t)unit;
3054 cp->cn_pri = COM_FORCECONSOLE(flags)
3055 || boothowto & RB_SERIAL
3056 ? CN_REMOTE : CN_NORMAL;
3057 siocniobase = iobase;
3060 if (COM_DEBUGGER(flags) && gdb_tab == NULL) {
3061 kprintf("sio%d: gdb debugging port\n", unit);
3062 siogdbiobase = iobase;
3065 cp->cn_gdbprivate = (void *)(intptr_t)unit;
3071 #if defined(__i386__) || defined(__x86_64__)
3074 * XXX Ugly Compatability.
3075 * If no gdb port has been specified, set it to be the console
3076 * as some configuration files don't specify the gdb port.
3078 if (gdb_tab == NULL && (boothowto & RB_GDB)) {
3079 kprintf("Warning: no GDB port specified. Defaulting to sio%d.\n",
3081 kprintf("Set flag 0x80 on desired GDB port in your\n");
3082 kprintf("configuration file (currently sio only).\n");
3083 siogdbiobase = siocniobase;
3084 siogdbunit = siocnunit;
3085 cp->cn_gdbprivate = (void *)(intptr_t)siocnunit;
3093 siocninit(struct consdev *cp)
3095 comconsole = (int)(intptr_t)cp->cn_private;
3099 siocninit_fini(struct consdev *cp)
3104 if (cp->cn_probegood) {
3105 unit = (int)(intptr_t)cp->cn_private;
3107 * Call devfs_find_device_by_name on ttydX to find the correct device,
3108 * as it should have been created already at this point by the
3110 * If it isn't found, the serial port was not attached at all and we
3111 * shouldn't be here, so assert this case.
3113 dev = devfs_find_device_by_name("ttyd%r", unit);
3115 KKASSERT(dev != NULL);
3121 siocncheckc(void *private)
3124 int unit = (int)(intptr_t)private;
3126 struct siocnstate sp;
3128 if (unit == siogdbunit)
3129 iobase = siogdbiobase;
3131 iobase = siocniobase;
3134 siocnopen(&sp, iobase, comdefaultrate);
3135 if (inb(iobase + com_lsr) & LSR_RXRDY)
3136 c = inb(iobase + com_data);
3139 siocnclose(&sp, iobase);
3147 siocngetc(void *private)
3150 int unit = (int)(intptr_t)private;
3152 struct siocnstate sp;
3154 if (unit == siogdbunit)
3155 iobase = siogdbiobase;
3157 iobase = siocniobase;
3160 siocnopen(&sp, iobase, comdefaultrate);
3161 while (!(inb(iobase + com_lsr) & LSR_RXRDY))
3163 c = inb(iobase + com_data);
3164 siocnclose(&sp, iobase);
3171 siocnputc(void *private, int c)
3173 int unit = (int)(intptr_t)private;
3174 struct siocnstate sp;
3177 if (unit == siogdbunit)
3178 iobase = siogdbiobase;
3180 iobase = siocniobase;
3183 siocnopen(&sp, iobase, comdefaultrate);
3184 siocntxwait(iobase);
3185 outb(iobase + com_data, c);
3186 siocnclose(&sp, iobase);
3191 DRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0);
3192 DRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, 0, 0);
3194 DRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0);
3197 DRIVER_MODULE(sio, puc, sio_puc_driver, sio_devclass, 0, 0);