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"
48 * Serial driver, based on 386BSD-0.1 com driver.
49 * Mostly rewritten to use pseudo-DMA.
50 * Works for National Semiconductor NS8250-NS16550AF UARTs.
51 * COM driver, based on HP dca driver.
53 * Changes for PC-Card integration:
54 * - Added PC-Card driver table and handlers
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/reboot.h>
59 #include <sys/malloc.h>
63 #include <sys/module.h>
65 #include <sys/dkstat.h>
66 #include <sys/fcntl.h>
67 #include <sys/interrupt.h>
68 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/sysctl.h>
73 #include <sys/timepps.h>
74 #include <sys/thread2.h>
75 #include <sys/devfs.h>
76 #include <sys/consio.h>
78 #include <machine/limits.h>
80 #include <bus/isa/isareg.h>
81 #include <bus/isa/isavar.h>
83 #include <bus/pci/pcireg.h>
84 #include <bus/pci/pcivar.h>
87 #include <dev/misc/puc/pucvar.h>
89 #include <machine/lock.h>
91 #include <machine/clock.h>
94 #include "sio_private.h"
97 #include "../ic_layer/esp.h"
100 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
102 #define CALLOUT_MASK 0x80
103 #define CONTROL_MASK 0x60
104 #define CONTROL_INIT_STATE 0x20
105 #define CONTROL_LOCK_STATE 0x40
106 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
107 #define MINOR_TO_UNIT(mynor) ((((mynor) & ~0xffffU) >> (8 + 3)) \
109 #define UNIT_TO_MINOR(unit) ((((unit) & ~0x1fU) << (8 + 3)) \
112 #define com_scr 7 /* scratch register for 16450-16550 (R/W) */
114 #define sio_getreg(com, off) \
115 (bus_space_read_1((com)->bst, (com)->bsh, (off)))
116 #define sio_setreg(com, off, value) \
117 (bus_space_write_1((com)->bst, (com)->bsh, (off), (value)))
121 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
122 * than the other bits so that they can be tested as a group without masking
125 * The following com and tty flags correspond closely:
126 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
128 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
129 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
130 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
131 * TS_FLUSH is not used.
132 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
133 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
135 #define CS_BUSY 0x80 /* output in progress */
136 #define CS_TTGO 0x40 /* output not stopped by XOFF */
137 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
138 #define CS_CHECKMSR 1 /* check of MSR scheduled */
139 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
140 #define CS_DTR_OFF 0x10 /* DTR held off */
141 #define CS_ODONE 4 /* output completed */
142 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
143 #define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */
145 static char const * const error_desc[] = {
148 #define CE_INTERRUPT_BUF_OVERFLOW 1
149 "interrupt-level buffer overflow",
150 #define CE_TTY_BUF_OVERFLOW 2
151 "tty-level buffer overflow",
155 static int espattach (struct com_s *com, Port_t esp_port);
157 static int sio_isa_attach (device_t dev);
159 static timeout_t siobusycheck;
160 static u_int siodivisor (u_long rclk, speed_t speed);
161 static timeout_t siodtrwakeup;
162 static void comhardclose (struct com_s *com);
163 static void sioinput (struct com_s *com);
164 static void siointr1 (struct com_s *com);
165 static void siointr (void *arg);
166 static int commctl (struct com_s *com, int bits, int how);
167 static int comparam (struct tty *tp, struct termios *t);
168 static inthand2_t siopoll;
169 static int sio_isa_probe (device_t dev);
170 static void siosettimeout (void);
171 static int siosetwater (struct com_s *com, speed_t speed);
172 static void comstart (struct tty *tp);
173 static void comstop (struct tty *tp, int rw);
174 static timeout_t comwakeup;
175 static void disc_optim (struct tty *tp, struct termios *t,
179 static int sio_pci_attach (device_t dev);
180 static void sio_pci_kludge_unit (device_t dev);
181 static int sio_pci_probe (device_t dev);
182 #endif /* NPCI > 0 */
185 static int sio_puc_attach (device_t dev);
186 static int sio_puc_probe (device_t dev);
187 #endif /* NPUC > 0 */
189 static char driver_name[] = "sio";
191 /* table and macro for fast conversion from a unit number to its com struct */
192 devclass_t sio_devclass;
193 #define com_addr(unit) ((struct com_s *) \
194 devclass_get_softc(sio_devclass, unit))
196 static device_method_t sio_isa_methods[] = {
197 /* Device interface */
198 DEVMETHOD(device_probe, sio_isa_probe),
199 DEVMETHOD(device_attach, sio_isa_attach),
204 static driver_t sio_isa_driver = {
207 sizeof(struct com_s),
211 static device_method_t sio_pci_methods[] = {
212 /* Device interface */
213 DEVMETHOD(device_probe, sio_pci_probe),
214 DEVMETHOD(device_attach, sio_pci_attach),
219 static driver_t sio_pci_driver = {
222 sizeof(struct com_s),
224 #endif /* NPCI > 0 */
227 static device_method_t sio_puc_methods[] = {
228 /* Device interface */
229 DEVMETHOD(device_probe, sio_puc_probe),
230 DEVMETHOD(device_attach, sio_puc_attach),
235 static driver_t sio_puc_driver = {
238 sizeof(struct com_s),
240 #endif /* NPUC > 0 */
242 static d_open_t sioopen;
243 static d_close_t sioclose;
244 static d_read_t sioread;
245 static d_write_t siowrite;
246 static d_ioctl_t sioioctl;
248 static struct dev_ops sio_ops = {
249 { driver_name, 0, D_TTY },
255 .d_kqfilter = ttykqfilter,
256 .d_revoke = ttyrevoke
260 static volatile speed_t comdefaultrate = CONSPEED;
261 static u_long comdefaultrclk = DEFAULT_RCLK;
262 SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, "");
263 static u_int com_events; /* input chars + weighted output completions */
264 static Port_t siocniobase;
265 static int siocnunit;
266 static Port_t siogdbiobase;
267 static int siogdbunit = -1;
268 static bool_t sio_registered;
269 static int sio_timeout;
270 static int sio_timeouts_until_log;
271 static struct callout sio_timeout_handle;
272 static int sio_numunits;
275 /* XXX configure this properly. */
276 static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, };
277 static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 };
281 * handle sysctl read/write requests for console speed
283 * In addition to setting comdefaultrate for I/O through /dev/console,
284 * also set the initial and lock values for the /dev/ttyXX device
285 * if there is one associated with the console. Finally, if the /dev/tty
286 * device has already been open, change the speed on the open running port
291 sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS)
298 lwkt_gettoken(&tty_token);
299 newspeed = comdefaultrate;
301 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req);
302 if (error || !req->newptr) {
303 lwkt_reltoken(&tty_token);
307 comdefaultrate = newspeed;
309 if (comconsole < 0) { /* serial console not selected? */
310 lwkt_reltoken(&tty_token);
314 com = com_addr(comconsole);
316 lwkt_reltoken(&tty_token);
321 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX
322 * (note, the lock rates really are boolean -- if non-zero, disallow
325 com->it_in.c_ispeed = com->it_in.c_ospeed =
326 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
327 com->it_out.c_ispeed = com->it_out.c_ospeed =
328 com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate;
331 * if we're open, change the running rate too
334 if (tp && (tp->t_state & TS_ISOPEN)) {
335 tp->t_termios.c_ispeed =
336 tp->t_termios.c_ospeed = comdefaultrate;
338 error = comparam(tp, &tp->t_termios);
341 lwkt_reltoken(&tty_token);
345 SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW,
346 0, 0, sysctl_machdep_comdefaultrate, "I", "");
355 static struct pci_ids pci_ids[] = {
356 { 0x100812b9, "3COM PCI FaxModem", 0x10 },
357 { 0x2000131f, "CyberSerial (1-port) 16550", 0x10 },
358 { 0x01101407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 },
359 { 0x01111407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 },
360 { 0x048011c1, "Lucent kermit based PCI Modem", 0x14 },
361 { 0x95211415, "Oxford Semiconductor PCI Dual Port Serial", 0x10 },
362 { 0x7101135e, "SeaLevel Ultra 530.PCI Single Port Serial", 0x18 },
363 { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 },
364 { 0x98459710, "Netmos Nm9845 PCI Bridge with Dual UART", 0x10 },
365 { 0x8c3d8086, "Intel Lynx Point KT Controller", 0x10 },
366 { 0x9c3d8086, "Intel Lynx Point-LP HECI KT", 0x10 },
367 { 0x8cbd8086, "Intel Wildcat Point KT Controller", 0x10 },
368 { 0x9cbd8086, "Intel Wildcat Point-LP KT Controller", 0x10 },
369 { 0x00000000, NULL, 0 }
373 sio_pci_attach(device_t dev)
378 type = pci_get_devid(dev);
380 while (id->type && id->type != type)
382 if (id->desc == NULL)
384 sio_pci_kludge_unit(dev);
385 return (sioattach(dev, id->rid, 0UL));
389 * Don't cut and paste this to other drivers. It is a horrible kludge
390 * which will fail to work and also be unnecessary in future versions.
393 sio_pci_kludge_unit(device_t dev)
402 while (resource_int_value("sio", unit, "port", &start) == 0 &&
405 if (device_get_unit(dev) < unit) {
406 dc = device_get_devclass(dev);
407 while (devclass_get_device(dc, unit))
409 device_printf(dev, "moving to sio%d\n", unit);
410 err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */
412 device_printf(dev, "error moving device %d\n", err);
417 sio_pci_probe(device_t dev)
422 type = pci_get_devid(dev);
424 while (id->type && id->type != type)
426 if (id->desc == NULL)
428 device_set_desc(dev, id->desc);
429 return (sioprobe(dev, id->rid, 0UL));
431 #endif /* NPCI > 0 */
435 sio_puc_attach(device_t dev)
439 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ,
442 return (sioattach(dev, 0, rclk));
446 sio_puc_probe(device_t dev)
450 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ,
453 return (sioprobe(dev, 0, rclk));
457 static struct isa_pnp_id sio_ids[] = {
458 {0x0005d041, "Standard PC COM port"}, /* PNP0500 */
459 {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */
460 {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */
461 {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */
462 {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */
463 /* Devices that do not have a compatid */
464 {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */
465 {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */
466 {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */
467 {0x21107905, NULL}, /* AKY1021 - 56K Plug&Play Modem */
468 {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */
469 {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */
470 {0x56159008, NULL}, /* BDP1556 - B.D. Smart One 56SPS,Voice Modem*/
471 {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */
472 {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */
473 {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */
474 {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */
475 {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */
476 {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */
477 {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */
478 {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */
479 {0x01308c0e, NULL}, /* CTL3001 - Creative Labs Phoneblaster */
480 {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */
481 {0x01009416, NULL}, /* ETT0001 - E-Tech Bullet 33k6 PnP */
482 {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */
483 {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */
484 {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */
485 {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */
486 {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */
487 {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */
488 {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */
489 {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */
490 {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */
491 {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/
492 {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */
493 {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */
494 {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */
495 {0x6045f435, NULL}, /* MOT4560 - Motorola ? */
496 {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */
497 {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */
498 {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */
499 {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */
500 {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */
501 {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */
502 {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */
503 {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */
504 {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */
505 {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */
506 {0x1010104d, NULL}, /* SHP1010 - Rockwell 33600bps Modem */
507 {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */
508 {0x9012b04e, NULL}, /* SUP1290 - Supra ? */
509 {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */
510 {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */
511 {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */
512 {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */
513 {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */
514 {0x7420b04e, NULL}, /* SUP2070 - Supra ? */
515 {0x8020b04e, NULL}, /* SUP2080 - Supra ? */
516 {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */
517 {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */
518 {0x8024b04e, NULL}, /* SUP2480 - Supra ? */
519 {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */
520 {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */
521 {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */
522 {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */
523 {0x11007256, NULL}, /* USR0011 - USR ? */
524 {0x01017256, NULL}, /* USR0101 - USR ? */
525 {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */
526 {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */
527 {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */
528 {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */
529 {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */
530 {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */
531 {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */
532 {0x90307256, NULL}, /* USR3090 - USR ? */
533 {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */
534 {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */
535 {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */
536 {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */
537 {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */
544 sio_isa_probe(device_t dev)
546 /* Check isapnp ids */
547 if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO)
549 return (sioprobe(dev, 0, 0UL));
553 sioprobe(device_t dev, int xrid, u_long rclk)
556 static bool_t already_init;
565 intrmask_t irqmap[4];
570 u_int flags = device_get_flags(dev);
572 struct resource *port;
575 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
576 0, ~0, IO_COMSIZE, RF_ACTIVE);
580 lwkt_gettoken(&tty_token);
581 com = device_get_softc(dev);
582 com->bst = rman_get_bustag(port);
583 com->bsh = rman_get_bushandle(port);
590 * XXX this is broken - when we are first called, there are no
591 * previously configured IO ports. We could hard code
592 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse.
593 * This code has been doing nothing since the conversion since
594 * "count" is zero the first time around.
598 * Turn off MCR_IENABLE for all likely serial ports. An unused
599 * port with its MCR_IENABLE gate open will inhibit interrupts
600 * from any used port that shares the interrupt vector.
601 * XXX the gate enable is elsewhere for some multiports.
604 int count, i, xioport;
606 devclass_get_devices(sio_devclass, &devs, &count);
607 for (i = 0; i < count; i++) {
609 if (device_is_enabled(xdev) &&
610 bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport,
612 outb(xioport + com_mcr, 0);
619 if (COM_LLCONSOLE(flags)) {
620 kprintf("sio%d: reserved for low-level i/o\n",
621 device_get_unit(dev));
622 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
623 lwkt_reltoken(&tty_token);
628 * If the device is on a multiport card and has an AST/4
629 * compatible interrupt control register, initialize this
630 * register and prepare to leave MCR_IENABLE clear in the mcr.
631 * Otherwise, prepare to set MCR_IENABLE in the mcr.
632 * Point idev to the device struct giving the correct id_irq.
633 * This is the struct for the master device if there is one.
636 mcr_image = MCR_IENABLE;
638 if (COM_ISMULTIPORT(flags)) {
642 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags));
644 kprintf("sio%d: master device %d not configured\n",
645 device_get_unit(dev), COM_MPMASTER(flags));
648 if (!COM_NOTAST4(flags)) {
649 if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io,
652 if (bus_get_resource(idev, SYS_RES_IRQ, 0,
654 outb(xiobase + com_scr, 0x80);
656 outb(xiobase + com_scr, 0);
661 #endif /* COM_MULTIPORT */
662 if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0)
665 bzero(failures, sizeof failures);
666 iobase = rman_get_start(port);
669 * We don't want to get actual interrupts, just masked ones.
670 * Interrupts from this line should already be masked in the ICU,
671 * but mask them in the processor as well in case there are some
672 * (misconfigured) shared interrupts.
678 * For the TI16754 chips, set prescaler to 1 (4 is often the
679 * default after-reset value) as otherwise it's impossible to
680 * get highest baudrates.
682 if (COM_TI16754(flags)) {
685 cfcr = sio_getreg(com, com_cfcr);
686 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
687 efr = sio_getreg(com, com_efr);
688 /* Unlock extended features to turn off prescaler. */
689 sio_setreg(com, com_efr, efr | EFR_EFE);
691 sio_setreg(com, com_cfcr, (cfcr != CFCR_EFR_ENABLE) ? cfcr : 0);
692 /* Turn off prescaler. */
693 sio_setreg(com, com_mcr,
694 sio_getreg(com, com_mcr) & ~MCR_PRESCALE);
695 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE);
696 sio_setreg(com, com_efr, efr);
697 sio_setreg(com, com_cfcr, cfcr);
701 * Initialize the speed and the word size and wait long enough to
702 * drain the maximum of 16 bytes of junk in device output queues.
703 * The speed is undefined after a master reset and must be set
704 * before relying on anything related to output. There may be
705 * junk after a (very fast) soft reboot and (apparently) after
707 * XXX what about the UART bug avoided by waiting in comparam()?
708 * We don't want to to wait long enough to drain at 2 bps.
710 if (iobase == siocniobase) {
711 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10));
713 sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS);
714 divisor = siodivisor(rclk, SIO_TEST_SPEED);
715 sio_setreg(com, com_dlbl, divisor & 0xff);
716 sio_setreg(com, com_dlbh, divisor >> 8);
717 sio_setreg(com, com_cfcr, CFCR_8BITS);
718 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10));
722 * Make sure we can drain the receiver. If we can't, the serial
723 * port may not exist.
725 for (fn = 0; fn < 256; ++fn) {
726 if ((sio_getreg(com, com_lsr) & LSR_RXRDY) == 0)
728 (void)sio_getreg(com, com_data);
732 lwkt_reltoken(&tty_token);
733 kprintf("sio%d: can't drain, serial port might "
734 "not exist, disabling\n", device_get_unit(dev));
739 * Enable the interrupt gate and disable device interupts. This
740 * should leave the device driving the interrupt line low and
741 * guarantee an edge trigger if an interrupt can be generated.
744 sio_setreg(com, com_mcr, mcr_image);
745 sio_setreg(com, com_ier, 0);
746 DELAY(1000); /* XXX */
747 irqmap[0] = isa_irq_pending();
750 * Attempt to set loopback mode so that we can send a null byte
751 * without annoying any external device.
754 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK);
757 * Attempt to generate an output interrupt. On 8250's, setting
758 * IER_ETXRDY generates an interrupt independent of the current
759 * setting and independent of whether the THR is empty. On 16450's,
760 * setting IER_ETXRDY generates an interrupt independent of the
761 * current setting. On 16550A's, setting IER_ETXRDY only
762 * generates an interrupt when IER_ETXRDY is not already set.
764 sio_setreg(com, com_ier, IER_ETXRDY);
767 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate
768 * an interrupt. They'd better generate one for actually doing
769 * output. Loopback may be broken on the same incompatibles but
770 * it's unlikely to do more than allow the null byte out.
772 sio_setreg(com, com_data, 0);
773 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10));
776 * Turn off loopback mode so that the interrupt gate works again
777 * (MCR_IENABLE was hidden). This should leave the device driving
778 * an interrupt line high. It doesn't matter if the interrupt
779 * line oscillates while we are not looking at it, since interrupts
783 sio_setreg(com, com_mcr, mcr_image);
786 * Some pcmcia cards have the "TXRDY bug", so we check everyone
787 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... )
789 if (COM_NOPROBE(flags)) {
790 /* Reading IIR register twice */
791 for (fn = 0; fn < 2; fn ++) {
793 failures[6] = sio_getreg(com, com_iir);
795 /* Check IIR_TXRDY clear ? */
797 if (failures[6] & IIR_TXRDY) {
798 /* Nop, Double check with clearing IER */
799 sio_setreg(com, com_ier, 0);
800 if (sio_getreg(com, com_iir) & IIR_NOPEND) {
801 /* Ok. we're familia this gang */
802 SET_FLAG(dev, COM_C_IIR_TXRDYBUG);
804 /* Unknown, Just omit this chip.. XXX */
806 sio_setreg(com, com_mcr, 0);
809 /* OK. this is well-known guys */
810 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG);
812 sio_setreg(com, com_ier, 0);
813 sio_setreg(com, com_cfcr, CFCR_8BITS);
815 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
816 lwkt_reltoken(&tty_token);
817 return (iobase == siocniobase ? 0 : result);
822 * o the CFCR, IER and MCR in UART hold the values written to them
823 * (the values happen to be all distinct - this is good for
824 * avoiding false positive tests from bus echoes).
825 * o an output interrupt is generated and its vector is correct.
826 * o the interrupt goes away when the IIR in the UART is read.
829 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS;
830 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY;
831 failures[2] = sio_getreg(com, com_mcr) - mcr_image;
832 DELAY(10000); /* Some internal modems need this time */
833 irqmap[1] = isa_irq_pending();
834 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY;
835 DELAY(1000); /* XXX */
836 irqmap[2] = isa_irq_pending();
837 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
840 * Turn off all device interrupts and check that they go off properly.
841 * Leave MCR_IENABLE alone. For ports without a master port, it gates
842 * the OUT2 output of the UART to
843 * the ICU input. Closing the gate would give a floating ICU input
844 * (unless there is another device driving it) and spurious interrupts.
845 * (On the system that this was first tested on, the input floats high
846 * and gives a (masked) interrupt as soon as the gate is closed.)
848 sio_setreg(com, com_ier, 0);
849 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */
850 failures[7] = sio_getreg(com, com_ier);
851 DELAY(1000); /* XXX */
852 irqmap[3] = isa_irq_pending();
853 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND;
857 irqs = irqmap[1] & ~irqmap[0];
858 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 &&
859 ((1 << xirq) & irqs) == 0)
861 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n",
862 device_get_unit(dev), xirq, irqs);
864 kprintf("sio%d: irq maps: %#x %#x %#x %#x\n",
865 device_get_unit(dev),
866 irqmap[0], irqmap[1], irqmap[2], irqmap[3]);
869 for (fn = 0; fn < sizeof failures; ++fn)
871 sio_setreg(com, com_mcr, 0);
874 kprintf("sio%d: probe failed test(s):",
875 device_get_unit(dev));
876 for (fn = 0; fn < sizeof failures; ++fn)
883 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
884 lwkt_reltoken(&tty_token);
885 return (iobase == siocniobase ? 0 : result);
890 espattach(struct com_s *com, Port_t esp_port)
896 * Check the ESP-specific I/O port to see if we're an ESP
897 * card. If not, return failure immediately.
899 if ((inb(esp_port) & 0xf3) == 0) {
900 kprintf(" port 0x%x is not an ESP board?\n", esp_port);
904 lwkt_gettoken(&tty_token);
906 * We've got something that claims to be a Hayes ESP card.
910 /* Get the dip-switch configuration */
911 outb(esp_port + ESP_CMD1, ESP_GETDIPS);
912 dips = inb(esp_port + ESP_STATUS1);
915 * Bits 0,1 of dips say which COM port we are.
917 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03])
920 kprintf(" esp_port has com %d\n", dips & 0x03);
921 lwkt_reltoken(&tty_token);
926 * Check for ESP version 2.0 or later: bits 4,5,6 = 010.
928 outb(esp_port + ESP_CMD1, ESP_GETTEST);
929 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */
930 val = inb(esp_port + ESP_STATUS2);
931 if ((val & 0x70) < 0x20) {
932 kprintf("-old (%o)", val & 0x70);
933 lwkt_reltoken(&tty_token);
938 * Check for ability to emulate 16550: bit 7 == 1
940 if ((dips & 0x80) == 0) {
942 lwkt_reltoken(&tty_token);
947 * Okay, we seem to be a Hayes ESP card. Whee.
950 com->esp_port = esp_port;
951 lwkt_reltoken(&tty_token);
957 sio_isa_attach(device_t dev)
959 return (sioattach(dev, 0, 0UL));
963 sioattach(device_t dev, int xrid, u_long rclk)
974 struct resource *port;
978 lwkt_gettoken(&tty_token);
981 callout_init_mp(&sio_timeout_handle);
985 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
986 0, ~0, IO_COMSIZE, RF_ACTIVE);
988 lwkt_reltoken(&tty_token);
992 iobase = rman_get_start(port);
993 unit = device_get_unit(dev);
994 com = device_get_softc(dev);
995 flags = device_get_flags(dev);
997 if (unit >= sio_numunits)
998 sio_numunits = unit + 1;
1000 * sioprobe() has initialized the device registers as follows:
1001 * o cfcr = CFCR_8BITS.
1002 * It is most important that CFCR_DLAB is off, so that the
1003 * data port is not hidden when we enable interrupts.
1005 * Interrupts are only enabled when the line is open.
1006 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible
1007 * interrupt control register or the config specifies no irq.
1008 * Keeping MCR_DTR and MCR_RTS off might stop the external
1009 * device from sending before we are ready.
1011 bzero(com, sizeof *com);
1013 com->ioportres = port;
1014 com->bst = rman_get_bustag(port);
1015 com->bsh = rman_get_bushandle(port);
1016 com->cfcr_image = CFCR_8BITS;
1017 com->dtr_wait = 3 * hz;
1018 callout_init_mp(&com->dtr_ch);
1019 callout_init_mp(&com->busy_ch);
1020 com->loses_outints = COM_LOSESOUTINTS(flags) != 0;
1021 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0;
1022 com->tx_fifo_size = 1;
1023 com->obufs[0].l_head = com->obuf1;
1024 com->obufs[1].l_head = com->obuf2;
1026 com->data_port = iobase + com_data;
1027 com->int_id_port = iobase + com_iir;
1028 com->modem_ctl_port = iobase + com_mcr;
1029 com->mcr_image = inb(com->modem_ctl_port);
1030 com->line_status_port = iobase + com_lsr;
1031 com->modem_status_port = iobase + com_msr;
1032 com->intr_ctl_port = iobase + com_ier;
1035 rclk = DEFAULT_RCLK;
1039 * We don't use all the flags from <sys/ttydefaults.h> since they
1040 * are only relevant for logins. It's important to have echo off
1041 * initially so that the line doesn't start blathering before the
1042 * echo flag can be turned off.
1044 com->it_in.c_iflag = 0;
1045 com->it_in.c_oflag = 0;
1046 com->it_in.c_cflag = TTYDEF_CFLAG;
1047 com->it_in.c_lflag = 0;
1048 if (unit == comconsole) {
1049 com->it_in.c_iflag = TTYDEF_IFLAG;
1050 com->it_in.c_oflag = TTYDEF_OFLAG;
1051 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
1052 com->it_in.c_lflag = TTYDEF_LFLAG;
1053 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
1054 com->lt_out.c_ispeed = com->lt_out.c_ospeed =
1055 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
1056 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
1058 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED;
1059 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
1061 * Leave i/o resources allocated if this is a `cn'-level
1062 * console, so that other devices can't snarf them.
1064 if (iobase != siocniobase)
1065 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
1066 lwkt_reltoken(&tty_token);
1069 termioschars(&com->it_in);
1070 com->it_out = com->it_in;
1072 /* attempt to determine UART type */
1073 kprintf("sio%d: type", unit);
1076 #ifdef COM_MULTIPORT
1077 if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags))
1079 if (!COM_IIR_TXRDYBUG(flags))
1086 scr = sio_getreg(com, com_scr);
1087 sio_setreg(com, com_scr, 0xa5);
1088 scr1 = sio_getreg(com, com_scr);
1089 sio_setreg(com, com_scr, 0x5a);
1090 scr2 = sio_getreg(com, com_scr);
1091 sio_setreg(com, com_scr, scr);
1092 if (scr1 != 0xa5 || scr2 != 0x5a) {
1094 goto determined_type;
1097 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH);
1100 switch (inb(com->int_id_port) & IIR_FIFO_MASK) {
1111 if (COM_NOFIFO(flags)) {
1112 kprintf(" 16550A fifo disabled");
1114 com->hasfifo = TRUE;
1115 if (COM_ST16650A(flags)) {
1117 com->tx_fifo_size = 32;
1118 kprintf(" ST16650A");
1119 } else if (COM_TI16754(flags)) {
1120 com->tx_fifo_size = 64;
1121 kprintf(" TI16754");
1123 com->tx_fifo_size = COM_FIFOSIZE(flags);
1128 for (espp = likely_esp_ports; *espp != 0; espp++)
1129 if (espattach(com, *espp)) {
1130 com->tx_fifo_size = 1024;
1134 if (!com->st16650a && !COM_TI16754(flags)) {
1135 if (!com->tx_fifo_size)
1136 com->tx_fifo_size = 16;
1138 kprintf(" lookalike with %d bytes FIFO",
1148 * Set 16550 compatibility mode.
1149 * We don't use the ESP_MODE_SCALE bit to increase the
1150 * fifo trigger levels because we can't handle large
1152 * XXX flow control should be set in comparam(), not here.
1154 outb(com->esp_port + ESP_CMD1, ESP_SETMODE);
1155 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1157 /* Set RTS/CTS flow control. */
1158 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE);
1159 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS);
1160 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS);
1162 /* Set flow-control levels. */
1163 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW);
1164 outb(com->esp_port + ESP_CMD2, HIBYTE(768));
1165 outb(com->esp_port + ESP_CMD2, LOBYTE(768));
1166 outb(com->esp_port + ESP_CMD2, HIBYTE(512));
1167 outb(com->esp_port + ESP_CMD2, LOBYTE(512));
1169 #endif /* COM_ESP */
1170 sio_setreg(com, com_fifo, 0);
1173 #ifdef COM_MULTIPORT
1174 if (COM_ISMULTIPORT(flags)) {
1177 com->multiport = TRUE;
1178 kprintf(" (multiport");
1179 if (unit == COM_MPMASTER(flags))
1182 masterdev = devclass_get_device(sio_devclass,
1183 COM_MPMASTER(flags));
1184 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev,
1185 SYS_RES_IRQ, 0, NULL, NULL) != 0);
1187 #endif /* COM_MULTIPORT */
1188 if (unit == comconsole)
1189 kprintf(", console");
1190 if (COM_IIR_TXRDYBUG(flags))
1191 kprintf(" with a bogus IIR_TXRDY register");
1194 if (!sio_registered) {
1195 register_swi(SWI_TTY, siopoll, NULL ,"swi_siopoll", NULL, -1);
1196 sio_registered = TRUE;
1198 minorbase = UNIT_TO_MINOR(unit);
1199 make_dev(&sio_ops, minorbase,
1200 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit);
1201 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE,
1202 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit);
1203 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE,
1204 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit);
1205 make_dev(&sio_ops, minorbase | CALLOUT_MASK,
1206 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit);
1207 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE,
1208 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit);
1209 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE,
1210 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit);
1212 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
1213 pps_init(&com->pps);
1216 com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1,
1219 ret = BUS_SETUP_INTR(device_get_parent(dev), dev,
1220 com->irqres, INTR_MPSAFE, siointr, com,
1221 &com->cookie, NULL, NULL);
1223 device_printf(dev, "could not activate interrupt\n");
1226 * Enable interrupts for early break-to-debugger support
1229 if (ret == 0 && unit == comconsole &&
1230 (break_to_debugger || alt_break_to_debugger)) {
1231 inb(siocniobase + com_data);
1232 outb(siocniobase + com_ier,
1233 IER_ERXRDY | IER_ERLS | IER_EMSC);
1238 lwkt_reltoken(&tty_token);
1243 sioopen(struct dev_open_args *ap)
1245 cdev_t dev = ap->a_head.a_dev;
1253 unit = MINOR_TO_UNIT(mynor);
1254 com = com_addr(unit);
1259 if (mynor & CONTROL_MASK)
1261 lwkt_gettoken(&tty_token);
1262 tp = dev->si_tty = com->tp = ttymalloc(com->tp);
1265 * We jump to this label after all non-interrupted sleeps to pick
1266 * up any changes of the device state.
1269 while (com->state & CS_DTR_OFF) {
1270 error = tsleep(&com->dtr_wait, PCATCH, "siodtr", 0);
1271 if (com_addr(unit) == NULL) {
1273 lwkt_reltoken(&tty_token);
1276 if (error != 0 || com->gone)
1279 if (tp->t_state & TS_ISOPEN) {
1281 * The device is open, so everything has been initialized.
1284 if (mynor & CALLOUT_MASK) {
1285 if (!com->active_out) {
1290 if (com->active_out) {
1291 if (ap->a_oflags & O_NONBLOCK) {
1295 error = tsleep(&com->active_out,
1296 PCATCH, "siobi", 0);
1297 if (com_addr(unit) == NULL) {
1299 lwkt_reltoken(&tty_token);
1302 if (error != 0 || com->gone)
1307 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
1313 * The device isn't open, so there are no conflicts.
1314 * Initialize it. Initialization is done twice in many
1315 * cases: to preempt sleeping callin opens if we are
1316 * callout, and to complete a callin open after DCD rises.
1318 tp->t_oproc = comstart;
1319 tp->t_param = comparam;
1320 tp->t_stop = comstop;
1322 tp->t_termios = mynor & CALLOUT_MASK
1323 ? com->it_out : com->it_in;
1324 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
1325 com->poll = com->no_irq;
1326 com->poll_output = com->loses_outints;
1328 error = comparam(tp, &tp->t_termios);
1333 * XXX we should goto open_top if comparam() slept.
1337 * (Re)enable and drain fifos.
1339 * Certain SMC chips cause problems if the fifos
1340 * are enabled while input is ready. Turn off the
1341 * fifo if necessary to clear the input. We test
1342 * the input ready bit after enabling the fifos
1343 * since we've already enabled them in comparam()
1344 * and to handle races between enabling and fresh
1348 sio_setreg(com, com_fifo,
1349 FIFO_RCV_RST | FIFO_XMT_RST
1352 * XXX the delays are for superstitious
1353 * historical reasons. It must be less than
1354 * the character time at the maximum
1355 * supported speed (87 usec at 115200 bps
1356 * 8N1). Otherwise we might loop endlessly
1357 * if data is streaming in. We used to use
1358 * delays of 100. That usually worked
1359 * because DELAY(100) used to usually delay
1360 * for about 85 usec instead of 100.
1363 if (!(inb(com->line_status_port) & LSR_RXRDY))
1365 sio_setreg(com, com_fifo, 0);
1367 (void) inb(com->data_port);
1372 (void) inb(com->line_status_port);
1373 (void) inb(com->data_port);
1374 com->prev_modem_status = com->last_modem_status
1375 = inb(com->modem_status_port);
1376 if (COM_IIR_TXRDYBUG(com->flags)) {
1377 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS
1380 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY
1381 | IER_ERLS | IER_EMSC);
1385 * Handle initial DCD. Callout devices get a fake initial
1386 * DCD (trapdoor DCD). If we are callout, then any sleeping
1387 * callin opens get woken up and resume sleeping on "siobi"
1388 * instead of "siodcd".
1391 * XXX `mynor & CALLOUT_MASK' should be
1392 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
1393 * TRAPDOOR_CARRIER is the default initial state for callout
1394 * devices and SOFT_CARRIER is like CLOCAL except it hides
1397 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
1398 (*linesw[tp->t_line].l_modem)(tp, 1);
1401 * Wait for DCD if necessary.
1403 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
1404 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) {
1406 error = tsleep(TSA_CARR_ON(tp), PCATCH, "siodcd", 0);
1407 if (com_addr(unit) == NULL) {
1409 lwkt_reltoken(&tty_token);
1413 if (error != 0 || com->gone)
1417 error = (*linesw[tp->t_line].l_open)(dev, tp);
1418 disc_optim(tp, &tp->t_termios, com);
1419 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
1420 com->active_out = TRUE;
1424 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
1426 lwkt_reltoken(&tty_token);
1431 sioclose(struct dev_close_args *ap)
1433 cdev_t dev = ap->a_head.a_dev;
1439 if (mynor & CONTROL_MASK)
1441 lwkt_gettoken(&tty_token);
1442 com = com_addr(MINOR_TO_UNIT(mynor));
1444 lwkt_reltoken(&tty_token);
1449 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
1450 disc_optim(tp, &tp->t_termios, com);
1451 comstop(tp, FREAD | FWRITE);
1457 kprintf("sio%d: gone\n", com->unit);
1459 if (com->ibuf != NULL)
1460 kfree(com->ibuf, M_DEVBUF);
1461 bzero(tp, sizeof *tp);
1464 lwkt_reltoken(&tty_token);
1469 comhardclose(struct com_s *com)
1473 lwkt_gettoken(&tty_token);
1475 com->poll_output = FALSE;
1476 com->do_timestamp = FALSE;
1477 com->do_dcd_timestamp = FALSE;
1478 com->pps.ppsparam.mode = 0;
1479 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
1484 * Leave interrupts enabled and don't clear DTR if this is the
1485 * console. This allows us to detect break-to-debugger events
1486 * while the console device is closed.
1488 if (com->unit != comconsole ||
1489 (break_to_debugger == 0 && alt_break_to_debugger == 0))
1492 sio_setreg(com, com_ier, 0);
1493 if (tp->t_cflag & HUPCL
1495 * XXX we will miss any carrier drop between here and the
1496 * next open. Perhaps we should watch DCD even when the
1497 * port is closed; it is not sufficient to check it at
1498 * the next open because it might go up and down while
1499 * we're not watching.
1501 || (!com->active_out
1502 && !(com->prev_modem_status & MSR_DCD)
1503 && !(com->it_in.c_cflag & CLOCAL))
1504 || !(tp->t_state & TS_ISOPEN)) {
1505 (void)commctl(com, TIOCM_DTR, DMBIC);
1506 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
1507 callout_reset(&com->dtr_ch, com->dtr_wait,
1509 com->state |= CS_DTR_OFF;
1515 * Disable fifos so that they are off after controlled
1516 * reboots. Some BIOSes fail to detect 16550s when the
1517 * fifos are enabled.
1519 sio_setreg(com, com_fifo, 0);
1521 com->active_out = FALSE;
1522 wakeup(&com->active_out);
1523 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
1524 lwkt_reltoken(&tty_token);
1529 sioread(struct dev_read_args *ap)
1531 cdev_t dev = ap->a_head.a_dev;
1535 lwkt_gettoken(&tty_token);
1537 if (mynor & CONTROL_MASK) {
1538 lwkt_reltoken(&tty_token);
1541 com = com_addr(MINOR_TO_UNIT(mynor));
1542 if (com == NULL || com->gone) {
1543 lwkt_reltoken(&tty_token);
1546 ret = ((*linesw[com->tp->t_line].l_read)(com->tp, ap->a_uio, ap->a_ioflag));
1547 lwkt_reltoken(&tty_token);
1552 siowrite(struct dev_write_args *ap)
1554 cdev_t dev = ap->a_head.a_dev;
1559 lwkt_gettoken(&tty_token);
1561 if (mynor & CONTROL_MASK) {
1562 lwkt_reltoken(&tty_token);
1566 unit = MINOR_TO_UNIT(mynor);
1567 com = com_addr(unit);
1568 if (com == NULL || com->gone) {
1569 lwkt_reltoken(&tty_token);
1573 * (XXX) We disallow virtual consoles if the physical console is
1574 * a serial port. This is in case there is a display attached that
1575 * is not the console. In that situation we don't need/want the X
1576 * server taking over the console.
1578 if (constty != NULL && unit == comconsole)
1580 ret = ((*linesw[com->tp->t_line].l_write)(com->tp, ap->a_uio, ap->a_ioflag));
1581 lwkt_reltoken(&tty_token);
1586 siobusycheck(void *chan)
1590 lwkt_gettoken(&tty_token);
1591 com = (struct com_s *)chan;
1594 * Clear TS_BUSY if low-level output is complete.
1595 * spl locking is sufficient because siointr1() does not set CS_BUSY.
1596 * If siointr1() clears CS_BUSY after we look at it, then we'll get
1597 * called again. Reading the line status port outside of siointr1()
1598 * is safe because CS_BUSY is clear so there are no output interrupts
1602 if (com->state & CS_BUSY)
1603 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */
1604 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
1605 == (LSR_TSRE | LSR_TXRDY)) {
1606 com->tp->t_state &= ~TS_BUSY;
1608 com->extra_state &= ~CSE_BUSYCHECK;
1610 callout_reset(&com->busy_ch, hz / 100, siobusycheck, com);
1613 lwkt_reltoken(&tty_token);
1617 siodivisor(u_long rclk, speed_t speed)
1623 if (speed == 0 || speed > ((speed_t)-1 - 1) / 8)
1625 divisor = (rclk / (8UL * speed) + 1) / 2;
1626 if (divisor == 0 || divisor >= 65536)
1628 actual_speed = rclk / (16UL * divisor);
1630 /* 10 times error in percent: */
1631 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2;
1633 /* 3.0% maximum error tolerance: */
1634 if (error < -30 || error > 30)
1641 siodtrwakeup(void *chan)
1645 lwkt_gettoken(&tty_token);
1646 com = (struct com_s *)chan;
1647 com->state &= ~CS_DTR_OFF;
1648 wakeup(&com->dtr_wait);
1649 lwkt_reltoken(&tty_token);
1653 * NOTE: Normally called with tty_token held but might not be when
1654 * operating as the console.
1656 * Must be called with com_lock
1659 sioinput(struct com_s *com)
1669 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) {
1670 com_events -= (com->iptr - com->ibuf);
1671 com->iptr = com->ibuf;
1674 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1676 * Avoid the grotesquely inefficient lineswitch routine
1677 * (ttyinput) in "raw" mode. It usually takes about 450
1678 * instructions (that's without canonical processing or echo!).
1679 * slinput is reasonably fast (usually 40 instructions plus
1684 incc = com->iptr - buf;
1685 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
1686 && (com->state & CS_RTS_IFLOW
1687 || tp->t_iflag & IXOFF)
1688 && !(tp->t_state & TS_TBLOCK))
1690 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
1691 += b_to_q((char *)buf, incc, &tp->t_rawq);
1695 tp->t_rawcc += incc;
1697 if (tp->t_state & TS_TTSTOP
1698 && (tp->t_iflag & IXANY
1699 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
1700 tp->t_state &= ~TS_TTSTOP;
1701 tp->t_lflag &= ~FLUSHO;
1705 } while (buf < com->iptr);
1709 line_status = buf[com->ierroff];
1712 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
1713 if (line_status & LSR_BI)
1714 recv_data |= TTY_BI;
1715 if (line_status & LSR_FE)
1716 recv_data |= TTY_FE;
1717 if (line_status & LSR_OE)
1718 recv_data |= TTY_OE;
1719 if (line_status & LSR_PE)
1720 recv_data |= TTY_PE;
1722 (*linesw[tp->t_line].l_rint)(recv_data, tp);
1724 } while (buf < com->iptr);
1726 com_events -= (com->iptr - com->ibuf);
1727 com->iptr = com->ibuf;
1730 * There is now room for another low-level buffer full of input,
1731 * so enable RTS if it is now disabled and there is room in the
1732 * high-level buffer.
1734 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) &&
1735 !(tp->t_state & TS_TBLOCK))
1736 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
1742 lwkt_gettoken(&tty_token);
1743 #ifndef COM_MULTIPORT
1745 siointr1((struct com_s *) arg);
1747 #else /* COM_MULTIPORT */
1748 bool_t possibly_more_intrs;
1753 * Loop until there is no activity on any port. This is necessary
1754 * to get an interrupt edge more than to avoid another interrupt.
1755 * If the IRQ signal is just an OR of the IRQ signals from several
1756 * devices, then the edge from one may be lost because another is
1761 possibly_more_intrs = FALSE;
1762 for (unit = 0; unit < sio_numunits; ++unit) {
1763 com = com_addr(unit);
1766 * would it work here, or be counter-productive?
1770 && (inb(com->int_id_port) & IIR_IMASK)
1773 possibly_more_intrs = TRUE;
1775 /* XXX com_unlock(); */
1777 } while (possibly_more_intrs);
1779 #endif /* COM_MULTIPORT */
1780 lwkt_reltoken(&tty_token);
1784 * Called with tty_token held and com_lock held.
1787 siointr1(struct com_s *com)
1790 u_char modem_status;
1797 int_ctl = inb(com->intr_ctl_port);
1798 int_ctl_new = int_ctl;
1800 while (!com->gone) {
1801 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) {
1802 modem_status = inb(com->modem_status_port);
1803 if ((modem_status ^ com->last_modem_status) & MSR_DCD) {
1804 count = sys_cputimer->count();
1805 pps_event(&com->pps, count,
1806 (modem_status & MSR_DCD) ?
1807 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
1810 line_status = inb(com->line_status_port);
1812 /* input event? (check first to help avoid overruns) */
1813 while (line_status & LSR_RCV_MASK) {
1814 /* break/unnattached error bits or real input? */
1815 if (!(line_status & LSR_RXRDY))
1818 recv_data = inb(com->data_port);
1821 * Solaris implements a new BREAK which is initiated
1822 * by a character sequence CR ~ ^b which is similar
1823 * to a familiar pattern used on Sun servers by the
1826 #define KEY_CRTLB 2 /* ^B */
1827 #define KEY_CR 13 /* CR '\r' */
1828 #define KEY_TILDE 126 /* ~ */
1830 if (com->unit == comconsole && alt_break_to_debugger) {
1831 static int brk_state1 = 0, brk_state2 = 0;
1832 if (recv_data == KEY_CR) {
1833 brk_state1 = recv_data;
1835 } else if (brk_state1 == KEY_CR && (recv_data == KEY_TILDE || recv_data == KEY_CRTLB)) {
1836 if (recv_data == KEY_TILDE)
1837 brk_state2 = recv_data;
1838 else if (brk_state2 == KEY_TILDE && recv_data == KEY_CRTLB) {
1842 brk_state1 = brk_state2 = 0;
1850 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) {
1852 * Don't store BI if IGNBRK or FE/PE if IGNPAR.
1853 * Otherwise, push the work to a higher level
1854 * (to handle PARMRK) if we're bypassing.
1855 * Otherwise, convert BI/FE and PE+INPCK to 0.
1857 * This makes bypassing work right in the
1858 * usual "raw" case (IGNBRK set, and IGNPAR
1861 * Note: BI together with FE/PE means just BI.
1863 if (line_status & LSR_BI) {
1865 if (com->unit == comconsole &&
1866 break_to_debugger) {
1874 || com->tp->t_iflag & IGNBRK)
1878 || com->tp->t_iflag & IGNPAR)
1881 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT
1882 && (line_status & (LSR_BI | LSR_FE)
1883 || com->tp->t_iflag & INPCK))
1887 if (com->hotchar != 0 && recv_data == com->hotchar)
1890 if (ioptr >= com->ibufend)
1891 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
1893 if (com->do_timestamp)
1894 microtime(&com->timestamp);
1897 #if 0 /* for testing input latency vs efficiency */
1898 if (com->iptr - com->ibuf == 8)
1901 ioptr[0] = recv_data;
1902 ioptr[com->ierroff] = line_status;
1903 com->iptr = ++ioptr;
1904 if (ioptr == com->ihighwater
1905 && com->state & CS_RTS_IFLOW)
1906 outb(com->modem_ctl_port,
1907 com->mcr_image &= ~MCR_RTS);
1908 if (line_status & LSR_OE)
1909 CE_RECORD(com, CE_OVERRUN);
1913 * "& 0x7F" is to avoid the gcc-1.40 generating a slow
1914 * jump from the top of the loop to here
1916 line_status = inb(com->line_status_port) & 0x7F;
1919 /* modem status change? (always check before doing output) */
1920 modem_status = inb(com->modem_status_port);
1921 if (modem_status != com->last_modem_status) {
1922 if (com->do_dcd_timestamp
1923 && !(com->last_modem_status & MSR_DCD)
1924 && modem_status & MSR_DCD)
1925 microtime(&com->dcd_timestamp);
1928 * Schedule high level to handle DCD changes. Note
1929 * that we don't use the delta bits anywhere. Some
1930 * UARTs mess them up, and it's easy to remember the
1931 * previous bits and calculate the delta.
1933 com->last_modem_status = modem_status;
1934 if (!(com->state & CS_CHECKMSR)) {
1935 com_events += LOTS_OF_EVENTS;
1936 com->state |= CS_CHECKMSR;
1940 /* handle CTS change immediately for crisp flow ctl */
1941 if (com->state & CS_CTS_OFLOW) {
1942 if (modem_status & MSR_CTS)
1943 com->state |= CS_ODEVREADY;
1945 com->state &= ~CS_ODEVREADY;
1949 /* output queued and everything ready? */
1950 if ((line_status & LSR_TXRDY)
1951 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
1952 ioptr = com->obufq.l_head;
1953 if (com->tx_fifo_size > 1) {
1956 ocount = com->obufq.l_tail - ioptr;
1957 if (ocount > com->tx_fifo_size)
1958 ocount = com->tx_fifo_size;
1959 com->bytes_out += ocount;
1961 outb(com->data_port, *ioptr++);
1962 while (--ocount != 0);
1964 outb(com->data_port, *ioptr++);
1967 com->obufq.l_head = ioptr;
1968 if (COM_IIR_TXRDYBUG(com->flags)) {
1969 int_ctl_new = int_ctl | IER_ETXRDY;
1971 if (ioptr >= com->obufq.l_tail) {
1974 qp = com->obufq.l_next;
1975 qp->l_queued = FALSE;
1978 com->obufq.l_head = qp->l_head;
1979 com->obufq.l_tail = qp->l_tail;
1980 com->obufq.l_next = qp;
1982 /* output just completed */
1983 if (COM_IIR_TXRDYBUG(com->flags)) {
1984 int_ctl_new = int_ctl & ~IER_ETXRDY;
1986 com->state &= ~CS_BUSY;
1988 if (!(com->state & CS_ODONE)) {
1989 com_events += LOTS_OF_EVENTS;
1990 com->state |= CS_ODONE;
1991 setsofttty(); /* handle at high level ASAP */
1994 if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) {
1995 outb(com->intr_ctl_port, int_ctl_new);
2000 #ifdef COM_MULTIPORT
2003 if (inb(com->int_id_port) & IIR_NOPEND)
2010 sioioctl(struct dev_ioctl_args *ap)
2012 cdev_t dev = ap->a_head.a_dev;
2013 caddr_t data = ap->a_data;
2018 #if defined(COMPAT_43)
2020 struct termios term;
2022 lwkt_gettoken(&tty_token);
2025 com = com_addr(MINOR_TO_UNIT(mynor));
2026 if (com == NULL || com->gone) {
2027 lwkt_reltoken(&tty_token);
2030 if (mynor & CONTROL_MASK) {
2033 switch (mynor & CONTROL_MASK) {
2034 case CONTROL_INIT_STATE:
2035 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
2037 case CONTROL_LOCK_STATE:
2038 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
2041 lwkt_reltoken(&tty_token);
2042 return (ENODEV); /* /dev/nodev */
2044 switch (ap->a_cmd) {
2046 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
2048 lwkt_reltoken(&tty_token);
2051 *ct = *(struct termios *)data;
2052 lwkt_reltoken(&tty_token);
2055 *(struct termios *)data = *ct;
2056 lwkt_reltoken(&tty_token);
2059 *(int *)data = TTYDISC;
2060 lwkt_reltoken(&tty_token);
2063 bzero(data, sizeof(struct winsize));
2064 lwkt_reltoken(&tty_token);
2067 lwkt_reltoken(&tty_token);
2072 #if defined(COMPAT_43)
2073 term = tp->t_termios;
2075 error = ttsetcompat(tp, &ap->a_cmd, data, &term);
2077 lwkt_reltoken(&tty_token);
2080 if (ap->a_cmd != oldcmd)
2081 data = (caddr_t)&term;
2083 if (ap->a_cmd == TIOCSETA || ap->a_cmd == TIOCSETAW ||
2084 ap->a_cmd == TIOCSETAF) {
2086 struct termios *dt = (struct termios *)data;
2087 struct termios *lt = mynor & CALLOUT_MASK
2088 ? &com->lt_out : &com->lt_in;
2090 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
2091 | (dt->c_iflag & ~lt->c_iflag);
2092 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
2093 | (dt->c_oflag & ~lt->c_oflag);
2094 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
2095 | (dt->c_cflag & ~lt->c_cflag);
2096 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
2097 | (dt->c_lflag & ~lt->c_lflag);
2098 for (cc = 0; cc < NCCS; ++cc)
2099 if (lt->c_cc[cc] != 0)
2100 dt->c_cc[cc] = tp->t_cc[cc];
2101 if (lt->c_ispeed != 0)
2102 dt->c_ispeed = tp->t_ispeed;
2103 if (lt->c_ospeed != 0)
2104 dt->c_ospeed = tp->t_ospeed;
2106 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, data, ap->a_fflag, ap->a_cred);
2107 if (error != ENOIOCTL) {
2108 lwkt_reltoken(&tty_token);
2112 error = ttioctl(tp, ap->a_cmd, data, ap->a_fflag);
2113 disc_optim(tp, &tp->t_termios, com);
2114 if (error != ENOIOCTL) {
2116 lwkt_reltoken(&tty_token);
2119 switch (ap->a_cmd) {
2121 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK);
2124 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2127 (void)commctl(com, TIOCM_DTR, DMBIS);
2130 (void)commctl(com, TIOCM_DTR, DMBIC);
2133 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
2134 * changes get undone on the next call to comparam().
2137 (void)commctl(com, *(int *)data, DMSET);
2140 (void)commctl(com, *(int *)data, DMBIS);
2143 (void)commctl(com, *(int *)data, DMBIC);
2146 *(int *)data = commctl(com, 0, DMGET);
2149 /* must be root since the wait applies to following logins */
2150 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
2153 lwkt_reltoken(&tty_token);
2156 com->dtr_wait = *(int *)data * hz / 100;
2159 *(int *)data = com->dtr_wait * 100 / hz;
2162 com->do_timestamp = TRUE;
2163 *(struct timeval *)data = com->timestamp;
2165 case TIOCDCDTIMESTAMP:
2166 com->do_dcd_timestamp = TRUE;
2167 *(struct timeval *)data = com->dcd_timestamp;
2171 error = pps_ioctl(ap->a_cmd, data, &com->pps);
2172 if (error == ENODEV)
2174 lwkt_reltoken(&tty_token);
2178 lwkt_reltoken(&tty_token);
2183 siopoll(void *dummy, void *frame)
2187 lwkt_gettoken(&tty_token);
2188 if (com_events == 0) {
2189 lwkt_reltoken(&tty_token);
2194 for (unit = 0; unit < sio_numunits; ++unit) {
2199 com = com_addr(unit);
2203 if (tp == NULL || com->gone) {
2205 * Discard any events related to never-opened or
2206 * going-away devices.
2209 incc = com->iptr - com->ibuf;
2210 com->iptr = com->ibuf;
2211 if (com->state & CS_CHECKMSR) {
2212 incc += LOTS_OF_EVENTS;
2213 com->state &= ~CS_CHECKMSR;
2219 if (com->iptr != com->ibuf) {
2224 if (com->state & CS_CHECKMSR) {
2225 u_char delta_modem_status;
2228 delta_modem_status = com->last_modem_status
2229 ^ com->prev_modem_status;
2230 com->prev_modem_status = com->last_modem_status;
2231 com_events -= LOTS_OF_EVENTS;
2232 com->state &= ~CS_CHECKMSR;
2234 if (delta_modem_status & MSR_DCD)
2235 (*linesw[tp->t_line].l_modem)
2236 (tp, com->prev_modem_status & MSR_DCD);
2238 if (com->state & CS_ODONE) {
2240 com_events -= LOTS_OF_EVENTS;
2241 com->state &= ~CS_ODONE;
2243 if (!(com->state & CS_BUSY)
2244 && !(com->extra_state & CSE_BUSYCHECK)) {
2245 callout_reset(&com->busy_ch, hz / 100,
2247 com->extra_state |= CSE_BUSYCHECK;
2249 (*linesw[tp->t_line].l_start)(tp);
2251 if (com_events == 0)
2254 if (com_events >= LOTS_OF_EVENTS)
2256 lwkt_reltoken(&tty_token);
2260 * Called with tty_token held but no com_lock
2263 comparam(struct tty *tp, struct termios *t)
2273 unit = DEV_TO_UNIT(tp->t_dev);
2274 com = com_addr(unit);
2279 /* do historical conversions */
2280 if (t->c_ispeed == 0)
2281 t->c_ispeed = t->c_ospeed;
2283 /* check requested parameters */
2284 if (t->c_ospeed == 0)
2287 if (t->c_ispeed != t->c_ospeed)
2289 divisor = siodivisor(com->rclk, t->c_ispeed);
2294 /* parameters are OK, convert them to the com struct and the device */
2297 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
2299 (void)commctl(com, TIOCM_DTR, DMBIS);
2301 switch (cflag & CSIZE) {
2315 if (cflag & PARENB) {
2317 if (!(cflag & PARODD))
2323 if (com->hasfifo && divisor != 0) {
2325 * Use a fifo trigger level low enough so that the input
2326 * latency from the fifo is less than about 16 msec and
2327 * the total latency is less than about 30 msec. These
2328 * latencies are reasonable for humans. Serial comms
2329 * protocols shouldn't expect anything better since modem
2330 * latencies are larger.
2332 * Interrupts can be held up for long periods of time
2333 * due to inefficiencies in other parts of the kernel,
2334 * certain video cards, etc. Setting the FIFO trigger
2335 * point to MEDH instead of HIGH gives us 694uS of slop
2336 * (8 character times) instead of 173uS (2 character times)
2339 com->fifo_image = t->c_ospeed <= 4800
2340 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH;
2343 * The Hayes ESP card needs the fifo DMA mode bit set
2344 * in compatibility mode. If not, it will interrupt
2345 * for each character received.
2348 com->fifo_image |= FIFO_DMA_MODE;
2350 sio_setreg(com, com_fifo, com->fifo_image);
2354 * This returns with interrupts disabled so that we can complete
2355 * the speed change atomically. Keeping interrupts disabled is
2356 * especially important while com_data is hidden.
2358 (void) siosetwater(com, t->c_ispeed);
2361 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB);
2363 * Only set the divisor registers if they would change,
2364 * since on some 16550 incompatibles (UMC8669F), setting
2365 * them while input is arriving them loses sync until
2366 * data stops arriving.
2368 dlbl = divisor & 0xFF;
2369 if (sio_getreg(com, com_dlbl) != dlbl)
2370 sio_setreg(com, com_dlbl, dlbl);
2371 dlbh = divisor >> 8;
2372 if (sio_getreg(com, com_dlbh) != dlbh)
2373 sio_setreg(com, com_dlbh, dlbh);
2376 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr);
2378 if (!(tp->t_state & TS_TTSTOP))
2379 com->state |= CS_TTGO;
2381 if (cflag & CRTS_IFLOW) {
2382 if (com->st16650a) {
2383 sio_setreg(com, com_cfcr, 0xbf);
2384 sio_setreg(com, com_fifo,
2385 sio_getreg(com, com_fifo) | 0x40);
2387 com->state |= CS_RTS_IFLOW;
2389 * If CS_RTS_IFLOW just changed from off to on, the change
2390 * needs to be propagated to MCR_RTS. This isn't urgent,
2391 * so do it later by calling comstart() instead of repeating
2392 * a lot of code from comstart() here.
2394 } else if (com->state & CS_RTS_IFLOW) {
2395 com->state &= ~CS_RTS_IFLOW;
2397 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
2398 * on here, since comstart() won't do it later.
2400 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2401 if (com->st16650a) {
2402 sio_setreg(com, com_cfcr, 0xbf);
2403 sio_setreg(com, com_fifo,
2404 sio_getreg(com, com_fifo) & ~0x40);
2410 * Set up state to handle output flow control.
2411 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
2412 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
2414 com->state |= CS_ODEVREADY;
2415 com->state &= ~CS_CTS_OFLOW;
2416 if (cflag & CCTS_OFLOW) {
2417 com->state |= CS_CTS_OFLOW;
2418 if (!(com->last_modem_status & MSR_CTS))
2419 com->state &= ~CS_ODEVREADY;
2420 if (com->st16650a) {
2421 sio_setreg(com, com_cfcr, 0xbf);
2422 sio_setreg(com, com_fifo,
2423 sio_getreg(com, com_fifo) | 0x80);
2426 if (com->st16650a) {
2427 sio_setreg(com, com_cfcr, 0xbf);
2428 sio_setreg(com, com_fifo,
2429 sio_getreg(com, com_fifo) & ~0x80);
2433 sio_setreg(com, com_cfcr, com->cfcr_image);
2435 /* XXX shouldn't call functions while intrs are disabled. */
2436 disc_optim(tp, t, com);
2438 * Recover from fiddling with CS_TTGO. We used to call siointr1()
2439 * unconditionally, but that defeated the careful discarding of
2440 * stale input in sioopen().
2442 if (com->state >= (CS_BUSY | CS_TTGO)) {
2449 if (com->ibufold != NULL) {
2450 kfree(com->ibufold, M_DEVBUF);
2451 com->ibufold = NULL;
2457 * called with tty_token held
2460 siosetwater(struct com_s *com, speed_t speed)
2468 * Make the buffer size large enough to handle a softtty interrupt
2469 * latency of about 2 ticks without loss of throughput or data
2470 * (about 3 ticks if input flow control is not used or not honoured,
2471 * but a bit less for CS5-CS7 modes).
2473 cp4ticks = speed / 10 / hz * 4;
2474 for (ibufsize = 128; ibufsize < cp4ticks;)
2476 if (ibufsize == com->ibufsize)
2480 * Allocate input buffer. The extra factor of 2 in the size is
2481 * to allow for an error byte for each input byte.
2483 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK | M_ZERO);
2485 /* Initialize non-critical variables. */
2486 com->ibufold = com->ibuf;
2487 com->ibufsize = ibufsize;
2490 tp->t_ififosize = 2 * ibufsize;
2491 tp->t_ispeedwat = (speed_t)-1;
2492 tp->t_ospeedwat = (speed_t)-1;
2496 * Read current input buffer.
2499 if (com->iptr != com->ibuf)
2503 * Initialize critical variables, including input buffer watermarks.
2504 * The external device is asked to stop sending when the buffer
2505 * exactly reaches high water, or when the high level requests it.
2506 * The high level is notified immediately (rather than at a later
2507 * clock tick) when this watermark is reached.
2508 * The buffer size is chosen so the watermark should almost never
2510 * The low watermark is invisibly 0 since the buffer is always
2511 * emptied all at once.
2513 com->iptr = com->ibuf = ibuf;
2514 com->ibufend = ibuf + ibufsize;
2515 com->ierroff = ibufsize;
2516 com->ihighwater = ibuf + 3 * ibufsize / 4;
2522 comstart(struct tty *tp)
2527 lwkt_gettoken(&tty_token);
2528 unit = DEV_TO_UNIT(tp->t_dev);
2529 com = com_addr(unit);
2531 lwkt_reltoken(&tty_token);
2536 if (tp->t_state & TS_TTSTOP)
2537 com->state &= ~CS_TTGO;
2539 com->state |= CS_TTGO;
2540 if (tp->t_state & TS_TBLOCK) {
2541 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW)
2542 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
2544 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater
2545 && com->state & CS_RTS_IFLOW)
2546 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2549 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
2552 lwkt_reltoken(&tty_token);
2555 if (tp->t_outq.c_cc != 0) {
2559 if (!com->obufs[0].l_queued) {
2560 com->obufs[0].l_tail
2561 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
2563 com->obufs[0].l_next = NULL;
2564 com->obufs[0].l_queued = TRUE;
2566 if (com->state & CS_BUSY) {
2567 qp = com->obufq.l_next;
2568 while ((next = qp->l_next) != NULL)
2570 qp->l_next = &com->obufs[0];
2572 com->obufq.l_head = com->obufs[0].l_head;
2573 com->obufq.l_tail = com->obufs[0].l_tail;
2574 com->obufq.l_next = &com->obufs[0];
2575 com->state |= CS_BUSY;
2579 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
2580 com->obufs[1].l_tail
2581 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
2583 com->obufs[1].l_next = NULL;
2584 com->obufs[1].l_queued = TRUE;
2586 if (com->state & CS_BUSY) {
2587 qp = com->obufq.l_next;
2588 while ((next = qp->l_next) != NULL)
2590 qp->l_next = &com->obufs[1];
2592 com->obufq.l_head = com->obufs[1].l_head;
2593 com->obufq.l_tail = com->obufs[1].l_tail;
2594 com->obufq.l_next = &com->obufs[1];
2595 com->state |= CS_BUSY;
2599 tp->t_state |= TS_BUSY;
2602 if (com->state >= (CS_BUSY | CS_TTGO))
2603 siointr1(com); /* fake interrupt to start output */
2607 lwkt_reltoken(&tty_token);
2611 comstop(struct tty *tp, int rw)
2615 lwkt_gettoken(&tty_token);
2616 com = com_addr(DEV_TO_UNIT(tp->t_dev));
2617 if (com == NULL || com->gone) {
2618 lwkt_reltoken(&tty_token);
2625 /* XXX avoid h/w bug. */
2628 sio_setreg(com, com_fifo,
2629 FIFO_XMT_RST | com->fifo_image);
2630 com->obufs[0].l_queued = FALSE;
2631 com->obufs[1].l_queued = FALSE;
2632 if (com->state & CS_ODONE)
2633 com_events -= LOTS_OF_EVENTS;
2634 com->state &= ~(CS_ODONE | CS_BUSY);
2635 com->tp->t_state &= ~TS_BUSY;
2640 /* XXX avoid h/w bug. */
2643 sio_setreg(com, com_fifo,
2644 FIFO_RCV_RST | com->fifo_image);
2645 com_events -= (com->iptr - com->ibuf);
2646 com->iptr = com->ibuf;
2650 lwkt_reltoken(&tty_token);
2654 commctl(struct com_s *com, int bits, int how)
2659 lwkt_gettoken(&tty_token);
2661 bits = TIOCM_LE; /* XXX - always enabled while open */
2662 mcr = com->mcr_image;
2667 msr = com->prev_modem_status;
2675 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI
2676 * more volatile by reading the modem status a lot. Perhaps
2677 * we should latch both bits until the status is read here.
2679 if (msr & (MSR_RI | MSR_TERI))
2681 lwkt_reltoken(&tty_token);
2685 if (bits & TIOCM_DTR)
2687 if (bits & TIOCM_RTS)
2690 lwkt_reltoken(&tty_token);
2696 outb(com->modem_ctl_port,
2697 com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE));
2700 outb(com->modem_ctl_port, com->mcr_image |= mcr);
2703 outb(com->modem_ctl_port, com->mcr_image &= ~mcr);
2707 lwkt_reltoken(&tty_token);
2712 * NOTE: Must be called with tty_token held
2721 ASSERT_LWKT_TOKEN_HELD(&tty_token);
2723 * Set our timeout period to 1 second if no polled devices are open.
2724 * Otherwise set it to max(1/200, 1/hz).
2725 * Enable timeouts iff some device is open.
2727 callout_stop(&sio_timeout_handle);
2730 for (unit = 0; unit < sio_numunits; ++unit) {
2731 com = com_addr(unit);
2732 if (com != NULL && com->tp != NULL
2733 && com->tp->t_state & TS_ISOPEN && !com->gone) {
2735 if (com->poll || com->poll_output) {
2736 sio_timeout = hz > 200 ? hz / 200 : 1;
2742 sio_timeouts_until_log = hz / sio_timeout;
2743 callout_reset(&sio_timeout_handle, sio_timeout,
2746 /* Flush error messages, if any. */
2747 sio_timeouts_until_log = 1;
2749 callout_stop(&sio_timeout_handle);
2754 * NOTE: Must be called with tty_token held
2757 comwakeup(void *chan)
2763 * Can be called from a callout too so just get the token
2765 lwkt_gettoken(&tty_token);
2766 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL);
2769 * Recover from lost output interrupts.
2770 * Poll any lines that don't use interrupts.
2772 for (unit = 0; unit < sio_numunits; ++unit) {
2773 com = com_addr(unit);
2774 if (com != NULL && !com->gone
2775 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
2783 * Check for and log errors, but not too often.
2785 if (--sio_timeouts_until_log > 0) {
2786 lwkt_reltoken(&tty_token);
2789 sio_timeouts_until_log = hz / sio_timeout;
2790 for (unit = 0; unit < sio_numunits; ++unit) {
2793 com = com_addr(unit);
2798 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
2803 delta = com->delta_error_counts[errnum];
2804 com->delta_error_counts[errnum] = 0;
2808 total = com->error_counts[errnum] += delta;
2809 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n",
2810 unit, delta, error_desc[errnum],
2811 delta == 1 ? "" : "s", total);
2814 lwkt_reltoken(&tty_token);
2818 disc_optim(struct tty *tp, struct termios *t, struct com_s *com)
2820 lwkt_gettoken(&tty_token);
2821 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
2822 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
2823 && (!(t->c_iflag & PARMRK)
2824 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
2825 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
2826 && linesw[tp->t_line].l_rint == ttyinput)
2827 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2829 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2830 com->hotchar = linesw[tp->t_line].l_hotchar;
2831 lwkt_reltoken(&tty_token);
2835 * Following are all routines needed for SIO to act as console
2837 #include <sys/cons.h>
2847 static speed_t siocngetspeed (Port_t, u_long rclk);
2849 static void siocnclose (struct siocnstate *sp, Port_t iobase);
2851 static void siocnopen (struct siocnstate *sp, Port_t iobase, int speed);
2852 static void siocntxwait (Port_t iobase);
2854 static cn_probe_t siocnprobe;
2855 static cn_init_t siocninit;
2856 static cn_init_fini_t siocninit_fini;
2857 static cn_checkc_t siocncheckc;
2858 static cn_getc_t siocngetc;
2859 static cn_putc_t siocnputc;
2861 #if defined(__i386__) || defined(__x86_64__)
2862 CONS_DRIVER(sio, siocnprobe, siocninit, siocninit_fini,
2863 NULL, siocngetc, siocncheckc, siocnputc, NULL, NULL);
2866 /* To get the GDB related variables */
2868 #include <ddb/ddb.h>
2872 siocntxwait(Port_t iobase)
2877 * Wait for any pending transmission to finish. Required to avoid
2878 * the UART lockup bug when the speed is changed, and for normal
2882 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY))
2883 != (LSR_TSRE | LSR_TXRDY) && --timo != 0)
2888 * Read the serial port specified and try to figure out what speed
2889 * it's currently running at. We're assuming the serial port has
2890 * been initialized and is basicly idle. This routine is only intended
2891 * to be run at system startup.
2893 * If the value read from the serial port doesn't make sense, return 0.
2896 * NOTE: Must be called with tty_token held
2899 siocngetspeed(Port_t iobase, u_long rclk)
2906 cfcr = inb(iobase + com_cfcr);
2907 outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
2909 dlbl = inb(iobase + com_dlbl);
2910 dlbh = inb(iobase + com_dlbh);
2912 outb(iobase + com_cfcr, cfcr);
2914 divisor = dlbh << 8 | dlbl;
2916 /* XXX there should be more sanity checking. */
2919 return (rclk / (16UL * divisor));
2923 siocnopen(struct siocnstate *sp, Port_t iobase, int speed)
2930 * Save all the device control registers except the fifo register
2931 * and set our default ones (cs8 -parenb speed=comdefaultrate).
2932 * We can't save the fifo register since it is read-only.
2934 sp->ier = inb(iobase + com_ier);
2935 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */
2936 siocntxwait(iobase);
2937 sp->cfcr = inb(iobase + com_cfcr);
2938 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2939 sp->dlbl = inb(iobase + com_dlbl);
2940 sp->dlbh = inb(iobase + com_dlbh);
2942 * Only set the divisor registers if they would change, since on
2943 * some 16550 incompatibles (Startech), setting them clears the
2944 * data input register. This also reduces the effects of the
2947 divisor = siodivisor(comdefaultrclk, speed);
2948 dlbl = divisor & 0xFF;
2949 if (sp->dlbl != dlbl)
2950 outb(iobase + com_dlbl, dlbl);
2951 dlbh = divisor >> 8;
2952 if (sp->dlbh != dlbh)
2953 outb(iobase + com_dlbh, dlbh);
2954 outb(iobase + com_cfcr, CFCR_8BITS);
2955 sp->mcr = inb(iobase + com_mcr);
2957 * We don't want interrupts, but must be careful not to "disable"
2958 * them by clearing the MCR_IENABLE bit, since that might cause
2959 * an interrupt by floating the IRQ line.
2961 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS);
2966 siocnclose(struct siocnstate *sp, Port_t iobase)
2969 * Restore the device control registers.
2971 siocntxwait(iobase);
2972 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
2973 if (sp->dlbl != inb(iobase + com_dlbl))
2974 outb(iobase + com_dlbl, sp->dlbl);
2975 if (sp->dlbh != inb(iobase + com_dlbh))
2976 outb(iobase + com_dlbh, sp->dlbh);
2977 outb(iobase + com_cfcr, sp->cfcr);
2979 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them.
2981 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS);
2982 outb(iobase + com_ier, sp->ier);
2987 siocnprobe(struct consdev *cp)
2992 struct siocnstate sp;
2995 * Find our first enabled console, if any. If it is a high-level
2996 * console device, then initialize it and return successfully.
2997 * If it is a low-level console device, then initialize it and
2998 * return unsuccessfully. It must be initialized in both cases
2999 * for early use by console drivers and debuggers. Initializing
3000 * the hardware is not necessary in all cases, since the i/o
3001 * routines initialize it on the fly, but it is necessary if
3002 * input might arrive while the hardware is switched back to an
3003 * uninitialized state. We can't handle multiple console devices
3004 * yet because our low-level routines don't take a device arg.
3005 * We trust the user to set the console flags properly so that we
3006 * don't need to probe.
3008 cp->cn_pri = CN_DEAD;
3010 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */
3013 if (resource_int_value("sio", unit, "disabled", &disabled) == 0) {
3017 if (resource_int_value("sio", unit, "flags", &flags))
3019 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) {
3025 if (resource_int_value("sio", unit, "port", &port))
3027 if (resource_int_value("sio", unit, "baud", &baud) == 0)
3033 if (boothowto & RB_SERIAL) {
3034 if (boot_speed == 0) {
3035 boot_speed = siocngetspeed(iobase,
3039 comdefaultrate = boot_speed;
3043 * Initialize the divisor latch. We can't rely on
3044 * siocnopen() to do this the first time, since it
3045 * avoids writing to the latch if the latch appears
3046 * to have the correct value. Also, if we didn't
3047 * just read the speed from the hardware, then we
3048 * need to set the speed in hardware so that
3049 * switching it later is null.
3052 cfcr = inb(iobase + com_cfcr);
3053 outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
3054 divisor = siodivisor(comdefaultrclk, comdefaultrate);
3055 outb(iobase + com_dlbl, divisor & 0xff);
3056 outb(iobase + com_dlbh, divisor >> 8);
3057 outb(iobase + com_cfcr, cfcr);
3059 siocnopen(&sp, iobase, comdefaultrate);
3063 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) {
3064 cp->cn_probegood = 1;
3065 cp->cn_private = (void *)(intptr_t)unit;
3066 cp->cn_pri = COM_FORCECONSOLE(flags)
3067 || boothowto & RB_SERIAL
3068 ? CN_REMOTE : CN_NORMAL;
3069 siocniobase = iobase;
3072 if (COM_DEBUGGER(flags) && gdb_tab == NULL) {
3073 kprintf("sio%d: gdb debugging port\n", unit);
3074 siogdbiobase = iobase;
3077 cp->cn_gdbprivate = (void *)(intptr_t)unit;
3083 #if defined(__i386__) || defined(__x86_64__)
3086 * XXX Ugly Compatability.
3087 * If no gdb port has been specified, set it to be the console
3088 * as some configuration files don't specify the gdb port.
3090 if (gdb_tab == NULL && (boothowto & RB_GDB)) {
3091 kprintf("Warning: no GDB port specified. Defaulting to sio%d.\n",
3093 kprintf("Set flag 0x80 on desired GDB port in your\n");
3094 kprintf("configuration file (currently sio only).\n");
3095 siogdbiobase = siocniobase;
3096 siogdbunit = siocnunit;
3097 cp->cn_gdbprivate = (void *)(intptr_t)siocnunit;
3105 siocninit(struct consdev *cp)
3107 comconsole = (int)(intptr_t)cp->cn_private;
3111 siocninit_fini(struct consdev *cp)
3116 if (cp->cn_probegood) {
3117 unit = (int)(intptr_t)cp->cn_private;
3119 * Call devfs_find_device_by_name on ttydX to find the correct device,
3120 * as it should have been created already at this point by the
3122 * If it isn't found, the serial port was not attached at all and we
3123 * shouldn't be here, so assert this case.
3125 dev = devfs_find_device_by_name("ttyd%r", unit);
3127 KKASSERT(dev != NULL);
3133 siocncheckc(void *private)
3136 int unit = (int)(intptr_t)private;
3139 struct siocnstate sp;
3142 if (unit == siogdbunit)
3143 iobase = siogdbiobase;
3145 iobase = siocniobase;
3149 siocnopen(&sp, iobase, comdefaultrate);
3151 if (inb(iobase + com_lsr) & LSR_RXRDY)
3152 c = inb(iobase + com_data);
3156 siocnclose(&sp, iobase);
3165 siocngetc(void *private)
3168 int unit = (int)(intptr_t)private;
3171 struct siocnstate sp;
3174 if (unit == siogdbunit)
3175 iobase = siogdbiobase;
3177 iobase = siocniobase;
3181 siocnopen(&sp, iobase, comdefaultrate);
3183 while (!(inb(iobase + com_lsr) & LSR_RXRDY))
3185 c = inb(iobase + com_data);
3187 siocnclose(&sp, iobase);
3195 siocnputc(void *private, int c)
3197 int unit = (int)(intptr_t)private;
3199 struct siocnstate sp;
3203 if (unit == siogdbunit)
3204 iobase = siogdbiobase;
3206 iobase = siocniobase;
3210 siocnopen(&sp, iobase, comdefaultrate);
3212 siocntxwait(iobase);
3213 outb(iobase + com_data, c);
3215 siocnclose(&sp, iobase);
3221 DRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, NULL, NULL);
3222 DRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, NULL, NULL);
3224 DRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, NULL, NULL);
3227 DRIVER_MODULE(sio, puc, sio_puc_driver, sio_devclass, NULL, NULL);