1 /*- 2 * (MPSAFE) 3 * 4 * Copyright (c) 1991 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 22 * 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 33 * SUCH DAMAGE. 34 * 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 38 */ 39 40 #include "opt_comconsole.h" 41 #include "opt_compat.h" 42 #include "opt_ddb.h" 43 #include "opt_sio.h" 44 #include "use_pci.h" 45 #include "use_puc.h" 46 47 /* 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. 52 * 53 * Changes for PC-Card integration: 54 * - Added PC-Card driver table and handlers 55 */ 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/reboot.h> 59 #include <sys/malloc.h> 60 #include <sys/tty.h> 61 #include <sys/proc.h> 62 #include <sys/priv.h> 63 #include <sys/module.h> 64 #include <sys/conf.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> 71 #include <sys/bus.h> 72 #include <sys/rman.h> 73 #include <sys/timepps.h> 74 #include <sys/thread2.h> 75 #include <sys/devfs.h> 76 77 #include <machine/limits.h> 78 79 #include <bus/isa/isareg.h> 80 #include <bus/isa/isavar.h> 81 #if NPCI > 0 82 #include <bus/pci/pcireg.h> 83 #include <bus/pci/pcivar.h> 84 #endif 85 #if NPUC > 0 86 #include <dev/misc/puc/pucvar.h> 87 #endif 88 #include <machine/lock.h> 89 90 #include <machine/clock.h> 91 #ifndef SMP 92 #include <machine/lock.h> 93 #endif 94 95 #include "sioreg.h" 96 #include "sio_private.h" 97 98 #ifdef COM_ESP 99 #include "../ic_layer/esp.h" 100 #endif 101 102 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 103 104 #define CALLOUT_MASK 0x80 105 #define CONTROL_MASK 0x60 106 #define CONTROL_INIT_STATE 0x20 107 #define CONTROL_LOCK_STATE 0x40 108 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev))) 109 #define MINOR_TO_UNIT(mynor) ((((mynor) & ~0xffffU) >> (8 + 3)) \ 110 | ((mynor) & 0x1f)) 111 #define UNIT_TO_MINOR(unit) ((((unit) & ~0x1fU) << (8 + 3)) \ 112 | ((unit) & 0x1f)) 113 114 #define com_scr 7 /* scratch register for 16450-16550 (R/W) */ 115 116 #define sio_getreg(com, off) \ 117 (bus_space_read_1((com)->bst, (com)->bsh, (off))) 118 #define sio_setreg(com, off, value) \ 119 (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 120 121 /* 122 * com state bits. 123 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 124 * than the other bits so that they can be tested as a group without masking 125 * off the low bits. 126 * 127 * The following com and tty flags correspond closely: 128 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 129 * comstop()) 130 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 131 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 132 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 133 * TS_FLUSH is not used. 134 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 135 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 136 */ 137 #define CS_BUSY 0x80 /* output in progress */ 138 #define CS_TTGO 0x40 /* output not stopped by XOFF */ 139 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 140 #define CS_CHECKMSR 1 /* check of MSR scheduled */ 141 #define CS_CTS_OFLOW 2 /* use CTS output flow control */ 142 #define CS_DTR_OFF 0x10 /* DTR held off */ 143 #define CS_ODONE 4 /* output completed */ 144 #define CS_RTS_IFLOW 8 /* use RTS input flow control */ 145 #define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 146 147 static char const * const error_desc[] = { 148 #define CE_OVERRUN 0 149 "silo overflow", 150 #define CE_INTERRUPT_BUF_OVERFLOW 1 151 "interrupt-level buffer overflow", 152 #define CE_TTY_BUF_OVERFLOW 2 153 "tty-level buffer overflow", 154 }; 155 156 #ifdef COM_ESP 157 static int espattach (struct com_s *com, Port_t esp_port); 158 #endif 159 static int sio_isa_attach (device_t dev); 160 161 static timeout_t siobusycheck; 162 static u_int siodivisor (u_long rclk, speed_t speed); 163 static timeout_t siodtrwakeup; 164 static void comhardclose (struct com_s *com); 165 static void sioinput (struct com_s *com); 166 static void siointr1 (struct com_s *com); 167 static void siointr (void *arg); 168 static int commctl (struct com_s *com, int bits, int how); 169 static int comparam (struct tty *tp, struct termios *t); 170 static inthand2_t siopoll; 171 static int sio_isa_probe (device_t dev); 172 static void siosettimeout (void); 173 static int siosetwater (struct com_s *com, speed_t speed); 174 static void comstart (struct tty *tp); 175 static void comstop (struct tty *tp, int rw); 176 static timeout_t comwakeup; 177 static void disc_optim (struct tty *tp, struct termios *t, 178 struct com_s *com); 179 180 #if NPCI > 0 181 static int sio_pci_attach (device_t dev); 182 static void sio_pci_kludge_unit (device_t dev); 183 static int sio_pci_probe (device_t dev); 184 #endif /* NPCI > 0 */ 185 186 #if NPUC > 0 187 static int sio_puc_attach (device_t dev); 188 static int sio_puc_probe (device_t dev); 189 #endif /* NPUC > 0 */ 190 191 static char driver_name[] = "sio"; 192 193 /* table and macro for fast conversion from a unit number to its com struct */ 194 devclass_t sio_devclass; 195 #define com_addr(unit) ((struct com_s *) \ 196 devclass_get_softc(sio_devclass, unit)) 197 198 static device_method_t sio_isa_methods[] = { 199 /* Device interface */ 200 DEVMETHOD(device_probe, sio_isa_probe), 201 DEVMETHOD(device_attach, sio_isa_attach), 202 203 { 0, 0 } 204 }; 205 206 static driver_t sio_isa_driver = { 207 driver_name, 208 sio_isa_methods, 209 sizeof(struct com_s), 210 }; 211 212 #if NPCI > 0 213 static device_method_t sio_pci_methods[] = { 214 /* Device interface */ 215 DEVMETHOD(device_probe, sio_pci_probe), 216 DEVMETHOD(device_attach, sio_pci_attach), 217 218 { 0, 0 } 219 }; 220 221 static driver_t sio_pci_driver = { 222 driver_name, 223 sio_pci_methods, 224 sizeof(struct com_s), 225 }; 226 #endif /* NPCI > 0 */ 227 228 #if NPUC > 0 229 static device_method_t sio_puc_methods[] = { 230 /* Device interface */ 231 DEVMETHOD(device_probe, sio_puc_probe), 232 DEVMETHOD(device_attach, sio_puc_attach), 233 234 { 0, 0 } 235 }; 236 237 static driver_t sio_puc_driver = { 238 driver_name, 239 sio_puc_methods, 240 sizeof(struct com_s), 241 }; 242 #endif /* NPUC > 0 */ 243 244 static d_open_t sioopen; 245 static d_close_t sioclose; 246 static d_read_t sioread; 247 static d_write_t siowrite; 248 static d_ioctl_t sioioctl; 249 250 static struct dev_ops sio_ops = { 251 { driver_name, 0, D_TTY }, 252 .d_open = sioopen, 253 .d_close = sioclose, 254 .d_read = sioread, 255 .d_write = siowrite, 256 .d_ioctl = sioioctl, 257 .d_kqfilter = ttykqfilter, 258 .d_revoke = ttyrevoke 259 }; 260 261 int comconsole = -1; 262 static volatile speed_t comdefaultrate = CONSPEED; 263 static u_long comdefaultrclk = DEFAULT_RCLK; 264 SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, ""); 265 static u_int com_events; /* input chars + weighted output completions */ 266 static Port_t siocniobase; 267 static int siocnunit; 268 static Port_t siogdbiobase; 269 static int siogdbunit = -1; 270 static bool_t sio_registered; 271 static int sio_timeout; 272 static int sio_timeouts_until_log; 273 static struct callout sio_timeout_handle; 274 static int sio_numunits; 275 276 #ifdef COM_ESP 277 /* XXX configure this properly. */ 278 static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 279 static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 280 #endif 281 282 /* 283 * handle sysctl read/write requests for console speed 284 * 285 * In addition to setting comdefaultrate for I/O through /dev/console, 286 * also set the initial and lock values for the /dev/ttyXX device 287 * if there is one associated with the console. Finally, if the /dev/tty 288 * device has already been open, change the speed on the open running port 289 * itself. 290 */ 291 292 static int 293 sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 294 { 295 int error; 296 speed_t newspeed; 297 struct com_s *com; 298 struct tty *tp; 299 300 lwkt_gettoken(&tty_token); 301 newspeed = comdefaultrate; 302 303 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 304 if (error || !req->newptr) { 305 lwkt_reltoken(&tty_token); 306 return (error); 307 } 308 309 comdefaultrate = newspeed; 310 311 if (comconsole < 0) { /* serial console not selected? */ 312 lwkt_reltoken(&tty_token); 313 return (0); 314 } 315 316 com = com_addr(comconsole); 317 if (com == NULL) { 318 lwkt_reltoken(&tty_token); 319 return (ENXIO); 320 } 321 322 /* 323 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 324 * (note, the lock rates really are boolean -- if non-zero, disallow 325 * speed changes) 326 */ 327 com->it_in.c_ispeed = com->it_in.c_ospeed = 328 com->lt_in.c_ispeed = com->lt_in.c_ospeed = 329 com->it_out.c_ispeed = com->it_out.c_ospeed = 330 com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate; 331 332 /* 333 * if we're open, change the running rate too 334 */ 335 tp = com->tp; 336 if (tp && (tp->t_state & TS_ISOPEN)) { 337 tp->t_termios.c_ispeed = 338 tp->t_termios.c_ospeed = comdefaultrate; 339 crit_enter(); 340 error = comparam(tp, &tp->t_termios); 341 crit_exit(); 342 } 343 lwkt_reltoken(&tty_token); 344 return error; 345 } 346 347 SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 348 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 349 350 #if NPCI > 0 351 struct pci_ids { 352 u_int32_t type; 353 const char *desc; 354 int rid; 355 }; 356 357 static struct pci_ids pci_ids[] = { 358 { 0x100812b9, "3COM PCI FaxModem", 0x10 }, 359 { 0x2000131f, "CyberSerial (1-port) 16550", 0x10 }, 360 { 0x01101407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 361 { 0x01111407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 362 { 0x048011c1, "Lucent kermit based PCI Modem", 0x14 }, 363 { 0x95211415, "Oxford Semiconductor PCI Dual Port Serial", 0x10 }, 364 { 0x7101135e, "SeaLevel Ultra 530.PCI Single Port Serial", 0x18 }, 365 { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 }, 366 { 0x98459710, "Netmos Nm9845 PCI Bridge with Dual UART", 0x10 }, 367 { 0x00000000, NULL, 0 } 368 }; 369 370 static int 371 sio_pci_attach(device_t dev) 372 { 373 u_int32_t type; 374 struct pci_ids *id; 375 376 type = pci_get_devid(dev); 377 id = pci_ids; 378 while (id->type && id->type != type) 379 id++; 380 if (id->desc == NULL) 381 return (ENXIO); 382 sio_pci_kludge_unit(dev); 383 return (sioattach(dev, id->rid, 0UL)); 384 } 385 386 /* 387 * Don't cut and paste this to other drivers. It is a horrible kludge 388 * which will fail to work and also be unnecessary in future versions. 389 */ 390 static void 391 sio_pci_kludge_unit(device_t dev) 392 { 393 devclass_t dc; 394 int err; 395 int start; 396 int unit; 397 398 unit = 0; 399 start = 0; 400 while (resource_int_value("sio", unit, "port", &start) == 0 && 401 start > 0) 402 unit++; 403 if (device_get_unit(dev) < unit) { 404 dc = device_get_devclass(dev); 405 while (devclass_get_device(dc, unit)) 406 unit++; 407 device_printf(dev, "moving to sio%d\n", unit); 408 err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ 409 if (err) 410 device_printf(dev, "error moving device %d\n", err); 411 } 412 } 413 414 static int 415 sio_pci_probe(device_t dev) 416 { 417 u_int32_t type; 418 struct pci_ids *id; 419 420 type = pci_get_devid(dev); 421 id = pci_ids; 422 while (id->type && id->type != type) 423 id++; 424 if (id->desc == NULL) 425 return (ENXIO); 426 device_set_desc(dev, id->desc); 427 return (sioprobe(dev, id->rid, 0UL)); 428 } 429 #endif /* NPCI > 0 */ 430 431 #if NPUC > 0 432 static int 433 sio_puc_attach(device_t dev) 434 { 435 uintptr_t rclk; 436 437 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ, 438 &rclk) != 0) 439 rclk = DEFAULT_RCLK; 440 return (sioattach(dev, 0, rclk)); 441 } 442 443 static int 444 sio_puc_probe(device_t dev) 445 { 446 uintptr_t rclk; 447 448 if (BUS_READ_IVAR(device_get_parent(dev), dev, PUC_IVAR_FREQ, 449 &rclk) != 0) 450 rclk = DEFAULT_RCLK; 451 return (sioprobe(dev, 0, rclk)); 452 } 453 #endif /* NPUC */ 454 455 static struct isa_pnp_id sio_ids[] = { 456 {0x0005d041, "Standard PC COM port"}, /* PNP0500 */ 457 {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */ 458 {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */ 459 {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */ 460 {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */ 461 /* Devices that do not have a compatid */ 462 {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */ 463 {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */ 464 {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */ 465 {0x21107905, NULL}, /* AKY1021 - 56K Plug&Play Modem */ 466 {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */ 467 {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */ 468 {0x56159008, NULL}, /* BDP1556 - B.D. Smart One 56SPS,Voice Modem*/ 469 {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 470 {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 471 {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 472 {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 473 {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 474 {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 475 {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 476 {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 477 {0x01308c0e, NULL}, /* CTL3001 - Creative Labs Phoneblaster */ 478 {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 479 {0x01009416, NULL}, /* ETT0001 - E-Tech Bullet 33k6 PnP */ 480 {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 481 {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 482 {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 483 {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 484 {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */ 485 {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 486 {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 487 {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 488 {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */ 489 {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/ 490 {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */ 491 {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */ 492 {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */ 493 {0x6045f435, NULL}, /* MOT4560 - Motorola ? */ 494 {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */ 495 {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */ 496 {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 497 {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 498 {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */ 499 {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 500 {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 501 {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */ 502 {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 503 {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 504 {0x1010104d, NULL}, /* SHP1010 - Rockwell 33600bps Modem */ 505 {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 506 {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 507 {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 508 {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 509 {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 510 {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 511 {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 512 {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 513 {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 514 {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 515 {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 516 {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 517 {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 518 {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 519 {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 520 {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 521 {0x11007256, NULL}, /* USR0011 - USR ? */ 522 {0x01017256, NULL}, /* USR0101 - USR ? */ 523 {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 524 {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 525 {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 526 {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 527 {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 528 {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 529 {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 530 {0x90307256, NULL}, /* USR3090 - USR ? */ 531 {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 532 {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 533 {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 534 {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 535 {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 536 {0} 537 }; 538 539 540 541 static int 542 sio_isa_probe(device_t dev) 543 { 544 /* Check isapnp ids */ 545 if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 546 return (ENXIO); 547 return (sioprobe(dev, 0, 0UL)); 548 } 549 550 int 551 sioprobe(device_t dev, int xrid, u_long rclk) 552 { 553 #if 0 554 static bool_t already_init; 555 device_t xdev; 556 #endif 557 struct com_s *com; 558 u_int divisor; 559 bool_t failures[10]; 560 int fn; 561 device_t idev; 562 Port_t iobase; 563 intrmask_t irqmap[4]; 564 intrmask_t irqs; 565 u_char mcr_image; 566 int result; 567 u_long xirq; 568 u_int flags = device_get_flags(dev); 569 int rid; 570 struct resource *port; 571 572 rid = xrid; 573 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 574 0, ~0, IO_COMSIZE, RF_ACTIVE); 575 if (!port) 576 return (ENXIO); 577 578 lwkt_gettoken(&tty_token); 579 com = device_get_softc(dev); 580 com->bst = rman_get_bustag(port); 581 com->bsh = rman_get_bushandle(port); 582 if (rclk == 0) 583 rclk = DEFAULT_RCLK; 584 com->rclk = rclk; 585 586 #if 0 587 /* 588 * XXX this is broken - when we are first called, there are no 589 * previously configured IO ports. We could hard code 590 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 591 * This code has been doing nothing since the conversion since 592 * "count" is zero the first time around. 593 */ 594 if (!already_init) { 595 /* 596 * Turn off MCR_IENABLE for all likely serial ports. An unused 597 * port with its MCR_IENABLE gate open will inhibit interrupts 598 * from any used port that shares the interrupt vector. 599 * XXX the gate enable is elsewhere for some multiports. 600 */ 601 device_t *devs; 602 int count, i, xioport; 603 604 devclass_get_devices(sio_devclass, &devs, &count); 605 for (i = 0; i < count; i++) { 606 xdev = devs[i]; 607 if (device_is_enabled(xdev) && 608 bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 609 NULL) == 0) 610 outb(xioport + com_mcr, 0); 611 } 612 kfree(devs, M_TEMP); 613 already_init = TRUE; 614 } 615 #endif 616 617 if (COM_LLCONSOLE(flags)) { 618 kprintf("sio%d: reserved for low-level i/o\n", 619 device_get_unit(dev)); 620 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 621 lwkt_reltoken(&tty_token); 622 return (ENXIO); 623 } 624 625 /* 626 * If the device is on a multiport card and has an AST/4 627 * compatible interrupt control register, initialize this 628 * register and prepare to leave MCR_IENABLE clear in the mcr. 629 * Otherwise, prepare to set MCR_IENABLE in the mcr. 630 * Point idev to the device struct giving the correct id_irq. 631 * This is the struct for the master device if there is one. 632 */ 633 idev = dev; 634 mcr_image = MCR_IENABLE; 635 #ifdef COM_MULTIPORT 636 if (COM_ISMULTIPORT(flags)) { 637 Port_t xiobase; 638 u_long io; 639 640 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 641 if (idev == NULL) { 642 kprintf("sio%d: master device %d not configured\n", 643 device_get_unit(dev), COM_MPMASTER(flags)); 644 idev = dev; 645 } 646 if (!COM_NOTAST4(flags)) { 647 if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 648 NULL) == 0) { 649 xiobase = io; 650 if (bus_get_resource(idev, SYS_RES_IRQ, 0, 651 NULL, NULL) == 0) 652 outb(xiobase + com_scr, 0x80); 653 else 654 outb(xiobase + com_scr, 0); 655 } 656 mcr_image = 0; 657 } 658 } 659 #endif /* COM_MULTIPORT */ 660 if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 661 mcr_image = 0; 662 663 bzero(failures, sizeof failures); 664 iobase = rman_get_start(port); 665 666 /* 667 * We don't want to get actual interrupts, just masked ones. 668 * Interrupts from this line should already be masked in the ICU, 669 * but mask them in the processor as well in case there are some 670 * (misconfigured) shared interrupts. 671 */ 672 com_lock(); 673 /* EXTRA DELAY? */ 674 675 /* 676 * For the TI16754 chips, set prescaler to 1 (4 is often the 677 * default after-reset value) as otherwise it's impossible to 678 * get highest baudrates. 679 */ 680 if (COM_TI16754(flags)) { 681 u_char cfcr, efr; 682 683 cfcr = sio_getreg(com, com_cfcr); 684 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE); 685 efr = sio_getreg(com, com_efr); 686 /* Unlock extended features to turn off prescaler. */ 687 sio_setreg(com, com_efr, efr | EFR_EFE); 688 /* Disable EFR. */ 689 sio_setreg(com, com_cfcr, (cfcr != CFCR_EFR_ENABLE) ? cfcr : 0); 690 /* Turn off prescaler. */ 691 sio_setreg(com, com_mcr, 692 sio_getreg(com, com_mcr) & ~MCR_PRESCALE); 693 sio_setreg(com, com_cfcr, CFCR_EFR_ENABLE); 694 sio_setreg(com, com_efr, efr); 695 sio_setreg(com, com_cfcr, cfcr); 696 } 697 698 /* 699 * Initialize the speed and the word size and wait long enough to 700 * drain the maximum of 16 bytes of junk in device output queues. 701 * The speed is undefined after a master reset and must be set 702 * before relying on anything related to output. There may be 703 * junk after a (very fast) soft reboot and (apparently) after 704 * master reset. 705 * XXX what about the UART bug avoided by waiting in comparam()? 706 * We don't want to to wait long enough to drain at 2 bps. 707 */ 708 if (iobase == siocniobase) { 709 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 710 } else { 711 sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 712 divisor = siodivisor(rclk, SIO_TEST_SPEED); 713 sio_setreg(com, com_dlbl, divisor & 0xff); 714 sio_setreg(com, com_dlbh, divisor >> 8); 715 sio_setreg(com, com_cfcr, CFCR_8BITS); 716 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 717 } 718 719 /* 720 * Make sure we can drain the receiver. If we can't, the serial 721 * port may not exist. 722 */ 723 for (fn = 0; fn < 256; ++fn) { 724 if ((sio_getreg(com, com_lsr) & LSR_RXRDY) == 0) 725 break; 726 (void)sio_getreg(com, com_data); 727 } 728 if (fn == 256) { 729 com_unlock(); 730 lwkt_reltoken(&tty_token); 731 kprintf("sio%d: can't drain, serial port might " 732 "not exist, disabling\n", device_get_unit(dev)); 733 return (ENXIO); 734 } 735 736 /* 737 * Enable the interrupt gate and disable device interupts. This 738 * should leave the device driving the interrupt line low and 739 * guarantee an edge trigger if an interrupt can be generated. 740 */ 741 /* EXTRA DELAY? */ 742 sio_setreg(com, com_mcr, mcr_image); 743 sio_setreg(com, com_ier, 0); 744 DELAY(1000); /* XXX */ 745 irqmap[0] = isa_irq_pending(); 746 747 /* 748 * Attempt to set loopback mode so that we can send a null byte 749 * without annoying any external device. 750 */ 751 /* EXTRA DELAY? */ 752 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 753 754 /* 755 * Attempt to generate an output interrupt. On 8250's, setting 756 * IER_ETXRDY generates an interrupt independent of the current 757 * setting and independent of whether the THR is empty. On 16450's, 758 * setting IER_ETXRDY generates an interrupt independent of the 759 * current setting. On 16550A's, setting IER_ETXRDY only 760 * generates an interrupt when IER_ETXRDY is not already set. 761 */ 762 sio_setreg(com, com_ier, IER_ETXRDY); 763 764 /* 765 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 766 * an interrupt. They'd better generate one for actually doing 767 * output. Loopback may be broken on the same incompatibles but 768 * it's unlikely to do more than allow the null byte out. 769 */ 770 sio_setreg(com, com_data, 0); 771 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 772 773 /* 774 * Turn off loopback mode so that the interrupt gate works again 775 * (MCR_IENABLE was hidden). This should leave the device driving 776 * an interrupt line high. It doesn't matter if the interrupt 777 * line oscillates while we are not looking at it, since interrupts 778 * are disabled. 779 */ 780 /* EXTRA DELAY? */ 781 sio_setreg(com, com_mcr, mcr_image); 782 783 /* 784 * Some pcmcia cards have the "TXRDY bug", so we check everyone 785 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 786 */ 787 if (COM_NOPROBE(flags)) { 788 /* Reading IIR register twice */ 789 for (fn = 0; fn < 2; fn ++) { 790 DELAY(10000); 791 failures[6] = sio_getreg(com, com_iir); 792 } 793 /* Check IIR_TXRDY clear ? */ 794 result = 0; 795 if (failures[6] & IIR_TXRDY) { 796 /* Nop, Double check with clearing IER */ 797 sio_setreg(com, com_ier, 0); 798 if (sio_getreg(com, com_iir) & IIR_NOPEND) { 799 /* Ok. we're familia this gang */ 800 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 801 } else { 802 /* Unknown, Just omit this chip.. XXX */ 803 result = ENXIO; 804 sio_setreg(com, com_mcr, 0); 805 } 806 } else { 807 /* OK. this is well-known guys */ 808 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 809 } 810 sio_setreg(com, com_ier, 0); 811 sio_setreg(com, com_cfcr, CFCR_8BITS); 812 com_unlock(); 813 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 814 lwkt_reltoken(&tty_token); 815 return (iobase == siocniobase ? 0 : result); 816 } 817 818 /* 819 * Check that 820 * o the CFCR, IER and MCR in UART hold the values written to them 821 * (the values happen to be all distinct - this is good for 822 * avoiding false positive tests from bus echoes). 823 * o an output interrupt is generated and its vector is correct. 824 * o the interrupt goes away when the IIR in the UART is read. 825 */ 826 /* EXTRA DELAY? */ 827 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 828 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 829 failures[2] = sio_getreg(com, com_mcr) - mcr_image; 830 DELAY(10000); /* Some internal modems need this time */ 831 irqmap[1] = isa_irq_pending(); 832 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 833 DELAY(1000); /* XXX */ 834 irqmap[2] = isa_irq_pending(); 835 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 836 837 /* 838 * Turn off all device interrupts and check that they go off properly. 839 * Leave MCR_IENABLE alone. For ports without a master port, it gates 840 * the OUT2 output of the UART to 841 * the ICU input. Closing the gate would give a floating ICU input 842 * (unless there is another device driving it) and spurious interrupts. 843 * (On the system that this was first tested on, the input floats high 844 * and gives a (masked) interrupt as soon as the gate is closed.) 845 */ 846 sio_setreg(com, com_ier, 0); 847 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 848 failures[7] = sio_getreg(com, com_ier); 849 DELAY(1000); /* XXX */ 850 irqmap[3] = isa_irq_pending(); 851 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 852 853 com_unlock(); 854 855 irqs = irqmap[1] & ~irqmap[0]; 856 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 857 ((1 << xirq) & irqs) == 0) 858 kprintf( 859 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 860 device_get_unit(dev), xirq, irqs); 861 if (bootverbose) 862 kprintf("sio%d: irq maps: %#x %#x %#x %#x\n", 863 device_get_unit(dev), 864 irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 865 866 result = 0; 867 for (fn = 0; fn < sizeof failures; ++fn) 868 if (failures[fn]) { 869 sio_setreg(com, com_mcr, 0); 870 result = ENXIO; 871 if (bootverbose) { 872 kprintf("sio%d: probe failed test(s):", 873 device_get_unit(dev)); 874 for (fn = 0; fn < sizeof failures; ++fn) 875 if (failures[fn]) 876 kprintf(" %d", fn); 877 kprintf("\n"); 878 } 879 break; 880 } 881 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 882 lwkt_reltoken(&tty_token); 883 return (iobase == siocniobase ? 0 : result); 884 } 885 886 #ifdef COM_ESP 887 static int 888 espattach(struct com_s *com, Port_t esp_port) 889 { 890 u_char dips; 891 u_char val; 892 893 /* 894 * Check the ESP-specific I/O port to see if we're an ESP 895 * card. If not, return failure immediately. 896 */ 897 if ((inb(esp_port) & 0xf3) == 0) { 898 kprintf(" port 0x%x is not an ESP board?\n", esp_port); 899 return (0); 900 } 901 902 lwkt_gettoken(&tty_token); 903 /* 904 * We've got something that claims to be a Hayes ESP card. 905 * Let's hope so. 906 */ 907 908 /* Get the dip-switch configuration */ 909 outb(esp_port + ESP_CMD1, ESP_GETDIPS); 910 dips = inb(esp_port + ESP_STATUS1); 911 912 /* 913 * Bits 0,1 of dips say which COM port we are. 914 */ 915 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 916 kprintf(" : ESP"); 917 else { 918 kprintf(" esp_port has com %d\n", dips & 0x03); 919 lwkt_reltoken(&tty_token); 920 return (0); 921 } 922 923 /* 924 * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 925 */ 926 outb(esp_port + ESP_CMD1, ESP_GETTEST); 927 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 928 val = inb(esp_port + ESP_STATUS2); 929 if ((val & 0x70) < 0x20) { 930 kprintf("-old (%o)", val & 0x70); 931 lwkt_reltoken(&tty_token); 932 return (0); 933 } 934 935 /* 936 * Check for ability to emulate 16550: bit 7 == 1 937 */ 938 if ((dips & 0x80) == 0) { 939 kprintf(" slave"); 940 lwkt_reltoken(&tty_token); 941 return (0); 942 } 943 944 /* 945 * Okay, we seem to be a Hayes ESP card. Whee. 946 */ 947 com->esp = TRUE; 948 com->esp_port = esp_port; 949 lwkt_reltoken(&tty_token); 950 return (1); 951 } 952 #endif /* COM_ESP */ 953 954 static int 955 sio_isa_attach(device_t dev) 956 { 957 return (sioattach(dev, 0, 0UL)); 958 } 959 960 int 961 sioattach(device_t dev, int xrid, u_long rclk) 962 { 963 struct com_s *com; 964 #ifdef COM_ESP 965 Port_t *espp; 966 #endif 967 Port_t iobase; 968 int minorbase; 969 int unit; 970 u_int flags; 971 int rid; 972 struct resource *port; 973 int ret; 974 static int did_init; 975 976 lwkt_gettoken(&tty_token); 977 if (did_init == 0) { 978 did_init = 1; 979 callout_init_mp(&sio_timeout_handle); 980 } 981 982 rid = xrid; 983 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 984 0, ~0, IO_COMSIZE, RF_ACTIVE); 985 if (!port) { 986 lwkt_reltoken(&tty_token); 987 return (ENXIO); 988 } 989 990 iobase = rman_get_start(port); 991 unit = device_get_unit(dev); 992 com = device_get_softc(dev); 993 flags = device_get_flags(dev); 994 995 if (unit >= sio_numunits) 996 sio_numunits = unit + 1; 997 /* 998 * sioprobe() has initialized the device registers as follows: 999 * o cfcr = CFCR_8BITS. 1000 * It is most important that CFCR_DLAB is off, so that the 1001 * data port is not hidden when we enable interrupts. 1002 * o ier = 0. 1003 * Interrupts are only enabled when the line is open. 1004 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 1005 * interrupt control register or the config specifies no irq. 1006 * Keeping MCR_DTR and MCR_RTS off might stop the external 1007 * device from sending before we are ready. 1008 */ 1009 bzero(com, sizeof *com); 1010 com->unit = unit; 1011 com->ioportres = port; 1012 com->bst = rman_get_bustag(port); 1013 com->bsh = rman_get_bushandle(port); 1014 com->cfcr_image = CFCR_8BITS; 1015 com->dtr_wait = 3 * hz; 1016 callout_init_mp(&com->dtr_ch); 1017 callout_init_mp(&com->busy_ch); 1018 com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 1019 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 1020 com->tx_fifo_size = 1; 1021 com->obufs[0].l_head = com->obuf1; 1022 com->obufs[1].l_head = com->obuf2; 1023 1024 com->data_port = iobase + com_data; 1025 com->int_id_port = iobase + com_iir; 1026 com->modem_ctl_port = iobase + com_mcr; 1027 com->mcr_image = inb(com->modem_ctl_port); 1028 com->line_status_port = iobase + com_lsr; 1029 com->modem_status_port = iobase + com_msr; 1030 com->intr_ctl_port = iobase + com_ier; 1031 1032 if (rclk == 0) 1033 rclk = DEFAULT_RCLK; 1034 com->rclk = rclk; 1035 1036 /* 1037 * We don't use all the flags from <sys/ttydefaults.h> since they 1038 * are only relevant for logins. It's important to have echo off 1039 * initially so that the line doesn't start blathering before the 1040 * echo flag can be turned off. 1041 */ 1042 com->it_in.c_iflag = 0; 1043 com->it_in.c_oflag = 0; 1044 com->it_in.c_cflag = TTYDEF_CFLAG; 1045 com->it_in.c_lflag = 0; 1046 if (unit == comconsole) { 1047 com->it_in.c_iflag = TTYDEF_IFLAG; 1048 com->it_in.c_oflag = TTYDEF_OFLAG; 1049 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 1050 com->it_in.c_lflag = TTYDEF_LFLAG; 1051 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 1052 com->lt_out.c_ispeed = com->lt_out.c_ospeed = 1053 com->lt_in.c_ispeed = com->lt_in.c_ospeed = 1054 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 1055 } else 1056 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 1057 if (siosetwater(com, com->it_in.c_ispeed) != 0) { 1058 /* 1059 * Leave i/o resources allocated if this is a `cn'-level 1060 * console, so that other devices can't snarf them. 1061 */ 1062 if (iobase != siocniobase) 1063 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1064 lwkt_reltoken(&tty_token); 1065 return (ENOMEM); 1066 } 1067 termioschars(&com->it_in); 1068 com->it_out = com->it_in; 1069 1070 /* attempt to determine UART type */ 1071 kprintf("sio%d: type", unit); 1072 1073 1074 #ifdef COM_MULTIPORT 1075 if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 1076 #else 1077 if (!COM_IIR_TXRDYBUG(flags)) 1078 #endif 1079 { 1080 u_char scr; 1081 u_char scr1; 1082 u_char scr2; 1083 1084 scr = sio_getreg(com, com_scr); 1085 sio_setreg(com, com_scr, 0xa5); 1086 scr1 = sio_getreg(com, com_scr); 1087 sio_setreg(com, com_scr, 0x5a); 1088 scr2 = sio_getreg(com, com_scr); 1089 sio_setreg(com, com_scr, scr); 1090 if (scr1 != 0xa5 || scr2 != 0x5a) { 1091 kprintf(" 8250"); 1092 goto determined_type; 1093 } 1094 } 1095 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 1096 DELAY(100); 1097 com->st16650a = 0; 1098 switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 1099 case FIFO_RX_LOW: 1100 kprintf(" 16450"); 1101 break; 1102 case FIFO_RX_MEDL: 1103 kprintf(" 16450?"); 1104 break; 1105 case FIFO_RX_MEDH: 1106 kprintf(" 16550?"); 1107 break; 1108 case FIFO_RX_HIGH: 1109 if (COM_NOFIFO(flags)) { 1110 kprintf(" 16550A fifo disabled"); 1111 } else { 1112 com->hasfifo = TRUE; 1113 if (COM_ST16650A(flags)) { 1114 com->st16650a = 1; 1115 com->tx_fifo_size = 32; 1116 kprintf(" ST16650A"); 1117 } else if (COM_TI16754(flags)) { 1118 com->tx_fifo_size = 64; 1119 kprintf(" TI16754"); 1120 } else { 1121 com->tx_fifo_size = COM_FIFOSIZE(flags); 1122 kprintf(" 16550A"); 1123 } 1124 } 1125 #ifdef COM_ESP 1126 for (espp = likely_esp_ports; *espp != 0; espp++) 1127 if (espattach(com, *espp)) { 1128 com->tx_fifo_size = 1024; 1129 break; 1130 } 1131 #endif 1132 if (!com->st16650a && !COM_TI16754(flags)) { 1133 if (!com->tx_fifo_size) 1134 com->tx_fifo_size = 16; 1135 else 1136 kprintf(" lookalike with %d bytes FIFO", 1137 com->tx_fifo_size); 1138 } 1139 1140 break; 1141 } 1142 1143 #ifdef COM_ESP 1144 if (com->esp) { 1145 /* 1146 * Set 16550 compatibility mode. 1147 * We don't use the ESP_MODE_SCALE bit to increase the 1148 * fifo trigger levels because we can't handle large 1149 * bursts of input. 1150 * XXX flow control should be set in comparam(), not here. 1151 */ 1152 outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 1153 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 1154 1155 /* Set RTS/CTS flow control. */ 1156 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 1157 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 1158 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 1159 1160 /* Set flow-control levels. */ 1161 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 1162 outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 1163 outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 1164 outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 1165 outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 1166 } 1167 #endif /* COM_ESP */ 1168 sio_setreg(com, com_fifo, 0); 1169 determined_type: ; 1170 1171 #ifdef COM_MULTIPORT 1172 if (COM_ISMULTIPORT(flags)) { 1173 device_t masterdev; 1174 1175 com->multiport = TRUE; 1176 kprintf(" (multiport"); 1177 if (unit == COM_MPMASTER(flags)) 1178 kprintf(" master"); 1179 kprintf(")"); 1180 masterdev = devclass_get_device(sio_devclass, 1181 COM_MPMASTER(flags)); 1182 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 1183 SYS_RES_IRQ, 0, NULL, NULL) != 0); 1184 } 1185 #endif /* COM_MULTIPORT */ 1186 if (unit == comconsole) 1187 kprintf(", console"); 1188 if (COM_IIR_TXRDYBUG(flags)) 1189 kprintf(" with a bogus IIR_TXRDY register"); 1190 kprintf("\n"); 1191 1192 if (!sio_registered) { 1193 register_swi(SWI_TTY, siopoll, NULL ,"swi_siopoll", NULL, -1); 1194 sio_registered = TRUE; 1195 } 1196 minorbase = UNIT_TO_MINOR(unit); 1197 make_dev(&sio_ops, minorbase, 1198 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 1199 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE, 1200 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 1201 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE, 1202 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 1203 make_dev(&sio_ops, minorbase | CALLOUT_MASK, 1204 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 1205 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE, 1206 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 1207 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE, 1208 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 1209 com->flags = flags; 1210 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 1211 pps_init(&com->pps); 1212 1213 rid = 0; 1214 com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 1215 RF_ACTIVE); 1216 if (com->irqres) { 1217 ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 1218 com->irqres, INTR_MPSAFE, siointr, com, 1219 &com->cookie, NULL, NULL); 1220 if (ret) 1221 device_printf(dev, "could not activate interrupt\n"); 1222 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1223 defined(ALT_BREAK_TO_DEBUGGER)) 1224 /* 1225 * Enable interrupts for early break-to-debugger support 1226 * on the console. 1227 */ 1228 if (ret == 0 && unit == comconsole) 1229 outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 1230 IER_EMSC); 1231 #endif 1232 } 1233 1234 lwkt_reltoken(&tty_token); 1235 return (0); 1236 } 1237 1238 static int 1239 sioopen(struct dev_open_args *ap) 1240 { 1241 cdev_t dev = ap->a_head.a_dev; 1242 struct com_s *com; 1243 int error; 1244 int mynor; 1245 struct tty *tp; 1246 int unit; 1247 1248 mynor = minor(dev); 1249 unit = MINOR_TO_UNIT(mynor); 1250 com = com_addr(unit); 1251 if (com == NULL) 1252 return (ENXIO); 1253 if (com->gone) 1254 return (ENXIO); 1255 if (mynor & CONTROL_MASK) 1256 return (0); 1257 lwkt_gettoken(&tty_token); 1258 tp = dev->si_tty = com->tp = ttymalloc(com->tp); 1259 crit_enter(); 1260 /* 1261 * We jump to this label after all non-interrupted sleeps to pick 1262 * up any changes of the device state. 1263 */ 1264 open_top: 1265 while (com->state & CS_DTR_OFF) { 1266 error = tsleep(&com->dtr_wait, PCATCH, "siodtr", 0); 1267 if (com_addr(unit) == NULL) { 1268 crit_exit(); 1269 lwkt_reltoken(&tty_token); 1270 return (ENXIO); 1271 } 1272 if (error != 0 || com->gone) 1273 goto out; 1274 } 1275 if (tp->t_state & TS_ISOPEN) { 1276 /* 1277 * The device is open, so everything has been initialized. 1278 * Handle conflicts. 1279 */ 1280 if (mynor & CALLOUT_MASK) { 1281 if (!com->active_out) { 1282 error = EBUSY; 1283 goto out; 1284 } 1285 } else { 1286 if (com->active_out) { 1287 if (ap->a_oflags & O_NONBLOCK) { 1288 error = EBUSY; 1289 goto out; 1290 } 1291 error = tsleep(&com->active_out, 1292 PCATCH, "siobi", 0); 1293 if (com_addr(unit) == NULL) { 1294 crit_exit(); 1295 lwkt_reltoken(&tty_token); 1296 return (ENXIO); 1297 } 1298 if (error != 0 || com->gone) 1299 goto out; 1300 goto open_top; 1301 } 1302 } 1303 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) { 1304 error = EBUSY; 1305 goto out; 1306 } 1307 } else { 1308 /* 1309 * The device isn't open, so there are no conflicts. 1310 * Initialize it. Initialization is done twice in many 1311 * cases: to preempt sleeping callin opens if we are 1312 * callout, and to complete a callin open after DCD rises. 1313 */ 1314 tp->t_oproc = comstart; 1315 tp->t_param = comparam; 1316 tp->t_stop = comstop; 1317 tp->t_dev = dev; 1318 tp->t_termios = mynor & CALLOUT_MASK 1319 ? com->it_out : com->it_in; 1320 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 1321 com->poll = com->no_irq; 1322 com->poll_output = com->loses_outints; 1323 ++com->wopeners; 1324 error = comparam(tp, &tp->t_termios); 1325 --com->wopeners; 1326 if (error != 0) 1327 goto out; 1328 /* 1329 * XXX we should goto open_top if comparam() slept. 1330 */ 1331 if (com->hasfifo) { 1332 /* 1333 * (Re)enable and drain fifos. 1334 * 1335 * Certain SMC chips cause problems if the fifos 1336 * are enabled while input is ready. Turn off the 1337 * fifo if necessary to clear the input. We test 1338 * the input ready bit after enabling the fifos 1339 * since we've already enabled them in comparam() 1340 * and to handle races between enabling and fresh 1341 * input. 1342 */ 1343 while (TRUE) { 1344 sio_setreg(com, com_fifo, 1345 FIFO_RCV_RST | FIFO_XMT_RST 1346 | com->fifo_image); 1347 /* 1348 * XXX the delays are for superstitious 1349 * historical reasons. It must be less than 1350 * the character time at the maximum 1351 * supported speed (87 usec at 115200 bps 1352 * 8N1). Otherwise we might loop endlessly 1353 * if data is streaming in. We used to use 1354 * delays of 100. That usually worked 1355 * because DELAY(100) used to usually delay 1356 * for about 85 usec instead of 100. 1357 */ 1358 DELAY(50); 1359 if (!(inb(com->line_status_port) & LSR_RXRDY)) 1360 break; 1361 sio_setreg(com, com_fifo, 0); 1362 DELAY(50); 1363 (void) inb(com->data_port); 1364 } 1365 } 1366 1367 com_lock(); 1368 (void) inb(com->line_status_port); 1369 (void) inb(com->data_port); 1370 com->prev_modem_status = com->last_modem_status 1371 = inb(com->modem_status_port); 1372 if (COM_IIR_TXRDYBUG(com->flags)) { 1373 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 1374 | IER_EMSC); 1375 } else { 1376 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 1377 | IER_ERLS | IER_EMSC); 1378 } 1379 com_unlock(); 1380 /* 1381 * Handle initial DCD. Callout devices get a fake initial 1382 * DCD (trapdoor DCD). If we are callout, then any sleeping 1383 * callin opens get woken up and resume sleeping on "siobi" 1384 * instead of "siodcd". 1385 */ 1386 /* 1387 * XXX `mynor & CALLOUT_MASK' should be 1388 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 1389 * TRAPDOOR_CARRIER is the default initial state for callout 1390 * devices and SOFT_CARRIER is like CLOCAL except it hides 1391 * the true carrier. 1392 */ 1393 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 1394 (*linesw[tp->t_line].l_modem)(tp, 1); 1395 } 1396 /* 1397 * Wait for DCD if necessary. 1398 */ 1399 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 1400 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) { 1401 ++com->wopeners; 1402 error = tsleep(TSA_CARR_ON(tp), PCATCH, "siodcd", 0); 1403 if (com_addr(unit) == NULL) { 1404 crit_exit(); 1405 lwkt_reltoken(&tty_token); 1406 return (ENXIO); 1407 } 1408 --com->wopeners; 1409 if (error != 0 || com->gone) 1410 goto out; 1411 goto open_top; 1412 } 1413 error = (*linesw[tp->t_line].l_open)(dev, tp); 1414 disc_optim(tp, &tp->t_termios, com); 1415 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 1416 com->active_out = TRUE; 1417 siosettimeout(); 1418 out: 1419 crit_exit(); 1420 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 1421 comhardclose(com); 1422 lwkt_reltoken(&tty_token); 1423 return (error); 1424 } 1425 1426 static int 1427 sioclose(struct dev_close_args *ap) 1428 { 1429 cdev_t dev = ap->a_head.a_dev; 1430 struct com_s *com; 1431 int mynor; 1432 struct tty *tp; 1433 1434 mynor = minor(dev); 1435 if (mynor & CONTROL_MASK) 1436 return (0); 1437 lwkt_gettoken(&tty_token); 1438 com = com_addr(MINOR_TO_UNIT(mynor)); 1439 if (com == NULL) { 1440 lwkt_reltoken(&tty_token); 1441 return (ENODEV); 1442 } 1443 tp = com->tp; 1444 crit_enter(); 1445 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); 1446 disc_optim(tp, &tp->t_termios, com); 1447 comstop(tp, FREAD | FWRITE); 1448 comhardclose(com); 1449 ttyclose(tp); 1450 siosettimeout(); 1451 crit_exit(); 1452 if (com->gone) { 1453 kprintf("sio%d: gone\n", com->unit); 1454 crit_enter(); 1455 if (com->ibuf != NULL) 1456 kfree(com->ibuf, M_DEVBUF); 1457 bzero(tp, sizeof *tp); 1458 crit_exit(); 1459 } 1460 lwkt_reltoken(&tty_token); 1461 return (0); 1462 } 1463 1464 static void 1465 comhardclose(struct com_s *com) 1466 { 1467 struct tty *tp; 1468 int unit; 1469 1470 unit = com->unit; 1471 crit_enter(); 1472 lwkt_gettoken(&tty_token); 1473 com->poll = FALSE; 1474 com->poll_output = FALSE; 1475 com->do_timestamp = FALSE; 1476 com->do_dcd_timestamp = FALSE; 1477 com->pps.ppsparam.mode = 0; 1478 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 1479 tp = com->tp; 1480 1481 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1482 defined(ALT_BREAK_TO_DEBUGGER)) 1483 /* 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. 1487 */ 1488 if (com->unit != comconsole) 1489 #endif 1490 { 1491 sio_setreg(com, com_ier, 0); 1492 if (tp->t_cflag & HUPCL 1493 /* 1494 * XXX we will miss any carrier drop between here and the 1495 * next open. Perhaps we should watch DCD even when the 1496 * port is closed; it is not sufficient to check it at 1497 * the next open because it might go up and down while 1498 * we're not watching. 1499 */ 1500 || (!com->active_out 1501 && !(com->prev_modem_status & MSR_DCD) 1502 && !(com->it_in.c_cflag & CLOCAL)) 1503 || !(tp->t_state & TS_ISOPEN)) { 1504 (void)commctl(com, TIOCM_DTR, DMBIC); 1505 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 1506 callout_reset(&com->dtr_ch, com->dtr_wait, 1507 siodtrwakeup, com); 1508 com->state |= CS_DTR_OFF; 1509 } 1510 } 1511 } 1512 if (com->hasfifo) { 1513 /* 1514 * Disable fifos so that they are off after controlled 1515 * reboots. Some BIOSes fail to detect 16550s when the 1516 * fifos are enabled. 1517 */ 1518 sio_setreg(com, com_fifo, 0); 1519 } 1520 com->active_out = FALSE; 1521 wakeup(&com->active_out); 1522 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 1523 lwkt_reltoken(&tty_token); 1524 crit_exit(); 1525 } 1526 1527 static int 1528 sioread(struct dev_read_args *ap) 1529 { 1530 cdev_t dev = ap->a_head.a_dev; 1531 int mynor, ret; 1532 struct com_s *com; 1533 1534 lwkt_gettoken(&tty_token); 1535 mynor = minor(dev); 1536 if (mynor & CONTROL_MASK) { 1537 lwkt_reltoken(&tty_token); 1538 return (ENODEV); 1539 } 1540 com = com_addr(MINOR_TO_UNIT(mynor)); 1541 if (com == NULL || com->gone) { 1542 lwkt_reltoken(&tty_token); 1543 return (ENODEV); 1544 } 1545 ret = ((*linesw[com->tp->t_line].l_read)(com->tp, ap->a_uio, ap->a_ioflag)); 1546 lwkt_reltoken(&tty_token); 1547 return ret; 1548 } 1549 1550 static int 1551 siowrite(struct dev_write_args *ap) 1552 { 1553 cdev_t dev = ap->a_head.a_dev; 1554 int mynor; 1555 struct com_s *com; 1556 int unit, ret; 1557 1558 lwkt_gettoken(&tty_token); 1559 mynor = minor(dev); 1560 if (mynor & CONTROL_MASK) { 1561 lwkt_reltoken(&tty_token); 1562 return (ENODEV); 1563 } 1564 1565 unit = MINOR_TO_UNIT(mynor); 1566 com = com_addr(unit); 1567 if (com == NULL || com->gone) { 1568 lwkt_reltoken(&tty_token); 1569 return (ENODEV); 1570 } 1571 /* 1572 * (XXX) We disallow virtual consoles if the physical console is 1573 * a serial port. This is in case there is a display attached that 1574 * is not the console. In that situation we don't need/want the X 1575 * server taking over the console. 1576 */ 1577 if (constty != NULL && unit == comconsole) 1578 constty = NULL; 1579 ret = ((*linesw[com->tp->t_line].l_write)(com->tp, ap->a_uio, ap->a_ioflag)); 1580 lwkt_reltoken(&tty_token); 1581 return ret; 1582 } 1583 1584 static void 1585 siobusycheck(void *chan) 1586 { 1587 struct com_s *com; 1588 1589 lwkt_gettoken(&tty_token); 1590 com = (struct com_s *)chan; 1591 1592 /* 1593 * Clear TS_BUSY if low-level output is complete. 1594 * spl locking is sufficient because siointr1() does not set CS_BUSY. 1595 * If siointr1() clears CS_BUSY after we look at it, then we'll get 1596 * called again. Reading the line status port outside of siointr1() 1597 * is safe because CS_BUSY is clear so there are no output interrupts 1598 * to lose. 1599 */ 1600 crit_enter(); 1601 if (com->state & CS_BUSY) 1602 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 1603 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 1604 == (LSR_TSRE | LSR_TXRDY)) { 1605 com->tp->t_state &= ~TS_BUSY; 1606 ttwwakeup(com->tp); 1607 com->extra_state &= ~CSE_BUSYCHECK; 1608 } else { 1609 callout_reset(&com->busy_ch, hz / 100, siobusycheck, com); 1610 } 1611 crit_exit(); 1612 lwkt_reltoken(&tty_token); 1613 } 1614 1615 static u_int 1616 siodivisor(u_long rclk, speed_t speed) 1617 { 1618 long actual_speed; 1619 u_int divisor; 1620 int error; 1621 1622 if (speed == 0 || speed > ((speed_t)-1 - 1) / 8) 1623 return (0); 1624 divisor = (rclk / (8UL * speed) + 1) / 2; 1625 if (divisor == 0 || divisor >= 65536) 1626 return (0); 1627 actual_speed = rclk / (16UL * divisor); 1628 1629 /* 10 times error in percent: */ 1630 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2; 1631 1632 /* 3.0% maximum error tolerance: */ 1633 if (error < -30 || error > 30) 1634 return (0); 1635 1636 return (divisor); 1637 } 1638 1639 static void 1640 siodtrwakeup(void *chan) 1641 { 1642 struct com_s *com; 1643 1644 lwkt_gettoken(&tty_token); 1645 com = (struct com_s *)chan; 1646 com->state &= ~CS_DTR_OFF; 1647 wakeup(&com->dtr_wait); 1648 lwkt_reltoken(&tty_token); 1649 } 1650 1651 /* 1652 * NOTE: Normally called with tty_token held but might not be when 1653 * operating as the console. 1654 * 1655 * Must be called with com_lock 1656 */ 1657 static void 1658 sioinput(struct com_s *com) 1659 { 1660 u_char *buf; 1661 int incc; 1662 u_char line_status; 1663 int recv_data; 1664 struct tty *tp; 1665 1666 buf = com->ibuf; 1667 tp = com->tp; 1668 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 1669 com_events -= (com->iptr - com->ibuf); 1670 com->iptr = com->ibuf; 1671 return; 1672 } 1673 if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 1674 /* 1675 * Avoid the grotesquely inefficient lineswitch routine 1676 * (ttyinput) in "raw" mode. It usually takes about 450 1677 * instructions (that's without canonical processing or echo!). 1678 * slinput is reasonably fast (usually 40 instructions plus 1679 * call overhead). 1680 */ 1681 do { 1682 com_unlock(); 1683 incc = com->iptr - buf; 1684 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 1685 && (com->state & CS_RTS_IFLOW 1686 || tp->t_iflag & IXOFF) 1687 && !(tp->t_state & TS_TBLOCK)) 1688 ttyblock(tp); 1689 com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 1690 += b_to_q((char *)buf, incc, &tp->t_rawq); 1691 buf += incc; 1692 tk_nin += incc; 1693 tk_rawcc += incc; 1694 tp->t_rawcc += incc; 1695 ttwakeup(tp); 1696 if (tp->t_state & TS_TTSTOP 1697 && (tp->t_iflag & IXANY 1698 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 1699 tp->t_state &= ~TS_TTSTOP; 1700 tp->t_lflag &= ~FLUSHO; 1701 comstart(tp); 1702 } 1703 com_lock(); 1704 } while (buf < com->iptr); 1705 } else { 1706 do { 1707 com_unlock(); 1708 line_status = buf[com->ierroff]; 1709 recv_data = *buf++; 1710 if (line_status 1711 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 1712 if (line_status & LSR_BI) 1713 recv_data |= TTY_BI; 1714 if (line_status & LSR_FE) 1715 recv_data |= TTY_FE; 1716 if (line_status & LSR_OE) 1717 recv_data |= TTY_OE; 1718 if (line_status & LSR_PE) 1719 recv_data |= TTY_PE; 1720 } 1721 (*linesw[tp->t_line].l_rint)(recv_data, tp); 1722 com_lock(); 1723 } while (buf < com->iptr); 1724 } 1725 com_events -= (com->iptr - com->ibuf); 1726 com->iptr = com->ibuf; 1727 1728 /* 1729 * There is now room for another low-level buffer full of input, 1730 * so enable RTS if it is now disabled and there is room in the 1731 * high-level buffer. 1732 */ 1733 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 1734 !(tp->t_state & TS_TBLOCK)) 1735 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 1736 } 1737 1738 void 1739 siointr(void *arg) 1740 { 1741 lwkt_gettoken(&tty_token); 1742 #ifndef COM_MULTIPORT 1743 com_lock(); 1744 siointr1((struct com_s *) arg); 1745 com_unlock(); 1746 #else /* COM_MULTIPORT */ 1747 bool_t possibly_more_intrs; 1748 int unit; 1749 struct com_s *com; 1750 1751 /* 1752 * Loop until there is no activity on any port. This is necessary 1753 * to get an interrupt edge more than to avoid another interrupt. 1754 * If the IRQ signal is just an OR of the IRQ signals from several 1755 * devices, then the edge from one may be lost because another is 1756 * on. 1757 */ 1758 com_lock(); 1759 do { 1760 possibly_more_intrs = FALSE; 1761 for (unit = 0; unit < sio_numunits; ++unit) { 1762 com = com_addr(unit); 1763 /* 1764 * XXX com_lock(); 1765 * would it work here, or be counter-productive? 1766 */ 1767 if (com != NULL 1768 && !com->gone 1769 && (inb(com->int_id_port) & IIR_IMASK) 1770 != IIR_NOPEND) { 1771 siointr1(com); 1772 possibly_more_intrs = TRUE; 1773 } 1774 /* XXX com_unlock(); */ 1775 } 1776 } while (possibly_more_intrs); 1777 com_unlock(); 1778 #endif /* COM_MULTIPORT */ 1779 lwkt_reltoken(&tty_token); 1780 } 1781 1782 /* 1783 * Called with tty_token held and com_lock held. 1784 */ 1785 static void 1786 siointr1(struct com_s *com) 1787 { 1788 u_char line_status; 1789 u_char modem_status; 1790 u_char *ioptr; 1791 u_char recv_data; 1792 u_char int_ctl; 1793 u_char int_ctl_new; 1794 u_int count; 1795 1796 int_ctl = inb(com->intr_ctl_port); 1797 int_ctl_new = int_ctl; 1798 1799 while (!com->gone) { 1800 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 1801 modem_status = inb(com->modem_status_port); 1802 if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 1803 count = sys_cputimer->count(); 1804 pps_event(&com->pps, count, 1805 (modem_status & MSR_DCD) ? 1806 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 1807 } 1808 } 1809 line_status = inb(com->line_status_port); 1810 1811 /* input event? (check first to help avoid overruns) */ 1812 while (line_status & LSR_RCV_MASK) { 1813 /* break/unnattached error bits or real input? */ 1814 if (!(line_status & LSR_RXRDY)) 1815 recv_data = 0; 1816 else 1817 recv_data = inb(com->data_port); 1818 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 1819 /* 1820 * Solaris implements a new BREAK which is initiated 1821 * by a character sequence CR ~ ^b which is similar 1822 * to a familiar pattern used on Sun servers by the 1823 * Remote Console. 1824 */ 1825 #define KEY_CRTLB 2 /* ^B */ 1826 #define KEY_CR 13 /* CR '\r' */ 1827 #define KEY_TILDE 126 /* ~ */ 1828 1829 if (com->unit == comconsole) { 1830 static int brk_state1 = 0, brk_state2 = 0; 1831 if (recv_data == KEY_CR) { 1832 brk_state1 = recv_data; 1833 brk_state2 = 0; 1834 } else if (brk_state1 == KEY_CR && (recv_data == KEY_TILDE || recv_data == KEY_CRTLB)) { 1835 if (recv_data == KEY_TILDE) 1836 brk_state2 = recv_data; 1837 else if (brk_state2 == KEY_TILDE && recv_data == KEY_CRTLB) { 1838 com_unlock(); 1839 breakpoint(); 1840 com_lock(); 1841 brk_state1 = brk_state2 = 0; 1842 goto cont; 1843 } else 1844 brk_state2 = 0; 1845 } else 1846 brk_state1 = 0; 1847 } 1848 #endif 1849 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 1850 /* 1851 * Don't store BI if IGNBRK or FE/PE if IGNPAR. 1852 * Otherwise, push the work to a higher level 1853 * (to handle PARMRK) if we're bypassing. 1854 * Otherwise, convert BI/FE and PE+INPCK to 0. 1855 * 1856 * This makes bypassing work right in the 1857 * usual "raw" case (IGNBRK set, and IGNPAR 1858 * and INPCK clear). 1859 * 1860 * Note: BI together with FE/PE means just BI. 1861 */ 1862 if (line_status & LSR_BI) { 1863 #if defined(DDB) && defined(BREAK_TO_DEBUGGER) 1864 if (com->unit == comconsole) { 1865 com_unlock(); 1866 breakpoint(); 1867 com_lock(); 1868 goto cont; 1869 } 1870 #endif 1871 if (com->tp == NULL 1872 || com->tp->t_iflag & IGNBRK) 1873 goto cont; 1874 } else { 1875 if (com->tp == NULL 1876 || com->tp->t_iflag & IGNPAR) 1877 goto cont; 1878 } 1879 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 1880 && (line_status & (LSR_BI | LSR_FE) 1881 || com->tp->t_iflag & INPCK)) 1882 recv_data = 0; 1883 } 1884 ++com->bytes_in; 1885 if (com->hotchar != 0 && recv_data == com->hotchar) 1886 setsofttty(); 1887 ioptr = com->iptr; 1888 if (ioptr >= com->ibufend) 1889 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 1890 else { 1891 if (com->do_timestamp) 1892 microtime(&com->timestamp); 1893 ++com_events; 1894 schedsofttty(); 1895 #if 0 /* for testing input latency vs efficiency */ 1896 if (com->iptr - com->ibuf == 8) 1897 setsofttty(); 1898 #endif 1899 ioptr[0] = recv_data; 1900 ioptr[com->ierroff] = line_status; 1901 com->iptr = ++ioptr; 1902 if (ioptr == com->ihighwater 1903 && com->state & CS_RTS_IFLOW) 1904 outb(com->modem_ctl_port, 1905 com->mcr_image &= ~MCR_RTS); 1906 if (line_status & LSR_OE) 1907 CE_RECORD(com, CE_OVERRUN); 1908 } 1909 cont: 1910 /* 1911 * "& 0x7F" is to avoid the gcc-1.40 generating a slow 1912 * jump from the top of the loop to here 1913 */ 1914 line_status = inb(com->line_status_port) & 0x7F; 1915 } 1916 1917 /* modem status change? (always check before doing output) */ 1918 modem_status = inb(com->modem_status_port); 1919 if (modem_status != com->last_modem_status) { 1920 if (com->do_dcd_timestamp 1921 && !(com->last_modem_status & MSR_DCD) 1922 && modem_status & MSR_DCD) 1923 microtime(&com->dcd_timestamp); 1924 1925 /* 1926 * Schedule high level to handle DCD changes. Note 1927 * that we don't use the delta bits anywhere. Some 1928 * UARTs mess them up, and it's easy to remember the 1929 * previous bits and calculate the delta. 1930 */ 1931 com->last_modem_status = modem_status; 1932 if (!(com->state & CS_CHECKMSR)) { 1933 com_events += LOTS_OF_EVENTS; 1934 com->state |= CS_CHECKMSR; 1935 setsofttty(); 1936 } 1937 1938 /* handle CTS change immediately for crisp flow ctl */ 1939 if (com->state & CS_CTS_OFLOW) { 1940 if (modem_status & MSR_CTS) 1941 com->state |= CS_ODEVREADY; 1942 else 1943 com->state &= ~CS_ODEVREADY; 1944 } 1945 } 1946 1947 /* output queued and everything ready? */ 1948 if (line_status & LSR_TXRDY 1949 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 1950 ioptr = com->obufq.l_head; 1951 if (com->tx_fifo_size > 1) { 1952 u_int ocount; 1953 1954 ocount = com->obufq.l_tail - ioptr; 1955 if (ocount > com->tx_fifo_size) 1956 ocount = com->tx_fifo_size; 1957 com->bytes_out += ocount; 1958 do 1959 outb(com->data_port, *ioptr++); 1960 while (--ocount != 0); 1961 } else { 1962 outb(com->data_port, *ioptr++); 1963 ++com->bytes_out; 1964 } 1965 com->obufq.l_head = ioptr; 1966 if (COM_IIR_TXRDYBUG(com->flags)) { 1967 int_ctl_new = int_ctl | IER_ETXRDY; 1968 } 1969 if (ioptr >= com->obufq.l_tail) { 1970 struct lbq *qp; 1971 1972 qp = com->obufq.l_next; 1973 qp->l_queued = FALSE; 1974 qp = qp->l_next; 1975 if (qp != NULL) { 1976 com->obufq.l_head = qp->l_head; 1977 com->obufq.l_tail = qp->l_tail; 1978 com->obufq.l_next = qp; 1979 } else { 1980 /* output just completed */ 1981 if (COM_IIR_TXRDYBUG(com->flags)) { 1982 int_ctl_new = int_ctl & ~IER_ETXRDY; 1983 } 1984 com->state &= ~CS_BUSY; 1985 } 1986 if (!(com->state & CS_ODONE)) { 1987 com_events += LOTS_OF_EVENTS; 1988 com->state |= CS_ODONE; 1989 setsofttty(); /* handle at high level ASAP */ 1990 } 1991 } 1992 if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 1993 outb(com->intr_ctl_port, int_ctl_new); 1994 } 1995 } 1996 1997 /* finished? */ 1998 #ifndef COM_MULTIPORT 1999 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 2000 #endif /* COM_MULTIPORT */ 2001 { 2002 return; 2003 } 2004 } 2005 } 2006 2007 static int 2008 sioioctl(struct dev_ioctl_args *ap) 2009 { 2010 cdev_t dev = ap->a_head.a_dev; 2011 caddr_t data = ap->a_data; 2012 struct com_s *com; 2013 int error; 2014 int mynor; 2015 struct tty *tp; 2016 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2017 u_long oldcmd; 2018 struct termios term; 2019 #endif 2020 lwkt_gettoken(&tty_token); 2021 mynor = minor(dev); 2022 2023 com = com_addr(MINOR_TO_UNIT(mynor)); 2024 if (com == NULL || com->gone) { 2025 lwkt_reltoken(&tty_token); 2026 return (ENODEV); 2027 } 2028 if (mynor & CONTROL_MASK) { 2029 struct termios *ct; 2030 2031 switch (mynor & CONTROL_MASK) { 2032 case CONTROL_INIT_STATE: 2033 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 2034 break; 2035 case CONTROL_LOCK_STATE: 2036 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 2037 break; 2038 default: 2039 lwkt_reltoken(&tty_token); 2040 return (ENODEV); /* /dev/nodev */ 2041 } 2042 switch (ap->a_cmd) { 2043 case TIOCSETA: 2044 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 2045 if (error != 0) 2046 return (error); 2047 *ct = *(struct termios *)data; 2048 lwkt_reltoken(&tty_token); 2049 return (0); 2050 case TIOCGETA: 2051 *(struct termios *)data = *ct; 2052 lwkt_reltoken(&tty_token); 2053 return (0); 2054 case TIOCGETD: 2055 *(int *)data = TTYDISC; 2056 lwkt_reltoken(&tty_token); 2057 return (0); 2058 case TIOCGWINSZ: 2059 bzero(data, sizeof(struct winsize)); 2060 lwkt_reltoken(&tty_token); 2061 return (0); 2062 default: 2063 lwkt_reltoken(&tty_token); 2064 return (ENOTTY); 2065 } 2066 } 2067 tp = com->tp; 2068 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2069 term = tp->t_termios; 2070 oldcmd = ap->a_cmd; 2071 error = ttsetcompat(tp, &ap->a_cmd, data, &term); 2072 if (error != 0) { 2073 lwkt_reltoken(&tty_token); 2074 return (error); 2075 } 2076 if (ap->a_cmd != oldcmd) 2077 data = (caddr_t)&term; 2078 #endif 2079 if (ap->a_cmd == TIOCSETA || ap->a_cmd == TIOCSETAW || 2080 ap->a_cmd == TIOCSETAF) { 2081 int cc; 2082 struct termios *dt = (struct termios *)data; 2083 struct termios *lt = mynor & CALLOUT_MASK 2084 ? &com->lt_out : &com->lt_in; 2085 2086 dt->c_iflag = (tp->t_iflag & lt->c_iflag) 2087 | (dt->c_iflag & ~lt->c_iflag); 2088 dt->c_oflag = (tp->t_oflag & lt->c_oflag) 2089 | (dt->c_oflag & ~lt->c_oflag); 2090 dt->c_cflag = (tp->t_cflag & lt->c_cflag) 2091 | (dt->c_cflag & ~lt->c_cflag); 2092 dt->c_lflag = (tp->t_lflag & lt->c_lflag) 2093 | (dt->c_lflag & ~lt->c_lflag); 2094 for (cc = 0; cc < NCCS; ++cc) 2095 if (lt->c_cc[cc] != 0) 2096 dt->c_cc[cc] = tp->t_cc[cc]; 2097 if (lt->c_ispeed != 0) 2098 dt->c_ispeed = tp->t_ispeed; 2099 if (lt->c_ospeed != 0) 2100 dt->c_ospeed = tp->t_ospeed; 2101 } 2102 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, data, ap->a_fflag, ap->a_cred); 2103 if (error != ENOIOCTL) { 2104 lwkt_reltoken(&tty_token); 2105 return (error); 2106 } 2107 crit_enter(); 2108 error = ttioctl(tp, ap->a_cmd, data, ap->a_fflag); 2109 disc_optim(tp, &tp->t_termios, com); 2110 if (error != ENOIOCTL) { 2111 crit_exit(); 2112 lwkt_reltoken(&tty_token); 2113 return (error); 2114 } 2115 switch (ap->a_cmd) { 2116 case TIOCSBRK: 2117 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 2118 break; 2119 case TIOCCBRK: 2120 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 2121 break; 2122 case TIOCSDTR: 2123 (void)commctl(com, TIOCM_DTR, DMBIS); 2124 break; 2125 case TIOCCDTR: 2126 (void)commctl(com, TIOCM_DTR, DMBIC); 2127 break; 2128 /* 2129 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 2130 * changes get undone on the next call to comparam(). 2131 */ 2132 case TIOCMSET: 2133 (void)commctl(com, *(int *)data, DMSET); 2134 break; 2135 case TIOCMBIS: 2136 (void)commctl(com, *(int *)data, DMBIS); 2137 break; 2138 case TIOCMBIC: 2139 (void)commctl(com, *(int *)data, DMBIC); 2140 break; 2141 case TIOCMGET: 2142 *(int *)data = commctl(com, 0, DMGET); 2143 break; 2144 case TIOCMSDTRWAIT: 2145 /* must be root since the wait applies to following logins */ 2146 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 2147 if (error != 0) { 2148 crit_exit(); 2149 lwkt_reltoken(&tty_token); 2150 return (error); 2151 } 2152 com->dtr_wait = *(int *)data * hz / 100; 2153 break; 2154 case TIOCMGDTRWAIT: 2155 *(int *)data = com->dtr_wait * 100 / hz; 2156 break; 2157 case TIOCTIMESTAMP: 2158 com->do_timestamp = TRUE; 2159 *(struct timeval *)data = com->timestamp; 2160 break; 2161 case TIOCDCDTIMESTAMP: 2162 com->do_dcd_timestamp = TRUE; 2163 *(struct timeval *)data = com->dcd_timestamp; 2164 break; 2165 default: 2166 crit_exit(); 2167 error = pps_ioctl(ap->a_cmd, data, &com->pps); 2168 if (error == ENODEV) 2169 error = ENOTTY; 2170 lwkt_reltoken(&tty_token); 2171 return (error); 2172 } 2173 crit_exit(); 2174 lwkt_reltoken(&tty_token); 2175 return (0); 2176 } 2177 2178 static void 2179 siopoll(void *dummy, void *frame) 2180 { 2181 int unit; 2182 2183 lwkt_gettoken(&tty_token); 2184 if (com_events == 0) { 2185 lwkt_reltoken(&tty_token); 2186 return; 2187 } 2188 2189 repeat: 2190 for (unit = 0; unit < sio_numunits; ++unit) { 2191 struct com_s *com; 2192 int incc; 2193 struct tty *tp; 2194 2195 com = com_addr(unit); 2196 if (com == NULL) 2197 continue; 2198 tp = com->tp; 2199 if (tp == NULL || com->gone) { 2200 /* 2201 * Discard any events related to never-opened or 2202 * going-away devices. 2203 */ 2204 com_lock(); 2205 incc = com->iptr - com->ibuf; 2206 com->iptr = com->ibuf; 2207 if (com->state & CS_CHECKMSR) { 2208 incc += LOTS_OF_EVENTS; 2209 com->state &= ~CS_CHECKMSR; 2210 } 2211 com_events -= incc; 2212 com_unlock(); 2213 continue; 2214 } 2215 if (com->iptr != com->ibuf) { 2216 com_lock(); 2217 sioinput(com); 2218 com_unlock(); 2219 } 2220 if (com->state & CS_CHECKMSR) { 2221 u_char delta_modem_status; 2222 2223 com_lock(); 2224 delta_modem_status = com->last_modem_status 2225 ^ com->prev_modem_status; 2226 com->prev_modem_status = com->last_modem_status; 2227 com_events -= LOTS_OF_EVENTS; 2228 com->state &= ~CS_CHECKMSR; 2229 com_unlock(); 2230 if (delta_modem_status & MSR_DCD) 2231 (*linesw[tp->t_line].l_modem) 2232 (tp, com->prev_modem_status & MSR_DCD); 2233 } 2234 if (com->state & CS_ODONE) { 2235 com_lock(); 2236 com_events -= LOTS_OF_EVENTS; 2237 com->state &= ~CS_ODONE; 2238 com_unlock(); 2239 if (!(com->state & CS_BUSY) 2240 && !(com->extra_state & CSE_BUSYCHECK)) { 2241 callout_reset(&com->busy_ch, hz / 100, 2242 siobusycheck, com); 2243 com->extra_state |= CSE_BUSYCHECK; 2244 } 2245 (*linesw[tp->t_line].l_start)(tp); 2246 } 2247 if (com_events == 0) 2248 break; 2249 } 2250 if (com_events >= LOTS_OF_EVENTS) 2251 goto repeat; 2252 lwkt_reltoken(&tty_token); 2253 } 2254 2255 /* 2256 * Called with tty_token held but no com_lock 2257 */ 2258 static int 2259 comparam(struct tty *tp, struct termios *t) 2260 { 2261 u_int cfcr; 2262 int cflag; 2263 struct com_s *com; 2264 u_int divisor; 2265 u_char dlbh; 2266 u_char dlbl; 2267 int unit; 2268 2269 unit = DEV_TO_UNIT(tp->t_dev); 2270 com = com_addr(unit); 2271 if (com == NULL) { 2272 return (ENODEV); 2273 } 2274 2275 /* do historical conversions */ 2276 if (t->c_ispeed == 0) 2277 t->c_ispeed = t->c_ospeed; 2278 2279 /* check requested parameters */ 2280 if (t->c_ospeed == 0) 2281 divisor = 0; 2282 else { 2283 if (t->c_ispeed != t->c_ospeed) 2284 return (EINVAL); 2285 divisor = siodivisor(com->rclk, t->c_ispeed); 2286 if (divisor == 0) 2287 return (EINVAL); 2288 } 2289 2290 /* parameters are OK, convert them to the com struct and the device */ 2291 crit_enter(); 2292 if (divisor == 0) 2293 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 2294 else 2295 (void)commctl(com, TIOCM_DTR, DMBIS); 2296 cflag = t->c_cflag; 2297 switch (cflag & CSIZE) { 2298 case CS5: 2299 cfcr = CFCR_5BITS; 2300 break; 2301 case CS6: 2302 cfcr = CFCR_6BITS; 2303 break; 2304 case CS7: 2305 cfcr = CFCR_7BITS; 2306 break; 2307 default: 2308 cfcr = CFCR_8BITS; 2309 break; 2310 } 2311 if (cflag & PARENB) { 2312 cfcr |= CFCR_PENAB; 2313 if (!(cflag & PARODD)) 2314 cfcr |= CFCR_PEVEN; 2315 } 2316 if (cflag & CSTOPB) 2317 cfcr |= CFCR_STOPB; 2318 2319 if (com->hasfifo && divisor != 0) { 2320 /* 2321 * Use a fifo trigger level low enough so that the input 2322 * latency from the fifo is less than about 16 msec and 2323 * the total latency is less than about 30 msec. These 2324 * latencies are reasonable for humans. Serial comms 2325 * protocols shouldn't expect anything better since modem 2326 * latencies are larger. 2327 * 2328 * Interrupts can be held up for long periods of time 2329 * due to inefficiencies in other parts of the kernel, 2330 * certain video cards, etc. Setting the FIFO trigger 2331 * point to MEDH instead of HIGH gives us 694uS of slop 2332 * (8 character times) instead of 173uS (2 character times) 2333 * @ 115200 bps. 2334 */ 2335 com->fifo_image = t->c_ospeed <= 4800 2336 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH; 2337 #ifdef COM_ESP 2338 /* 2339 * The Hayes ESP card needs the fifo DMA mode bit set 2340 * in compatibility mode. If not, it will interrupt 2341 * for each character received. 2342 */ 2343 if (com->esp) 2344 com->fifo_image |= FIFO_DMA_MODE; 2345 #endif 2346 sio_setreg(com, com_fifo, com->fifo_image); 2347 } 2348 2349 /* 2350 * This returns with interrupts disabled so that we can complete 2351 * the speed change atomically. Keeping interrupts disabled is 2352 * especially important while com_data is hidden. 2353 */ 2354 (void) siosetwater(com, t->c_ispeed); 2355 2356 if (divisor != 0) { 2357 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 2358 /* 2359 * Only set the divisor registers if they would change, 2360 * since on some 16550 incompatibles (UMC8669F), setting 2361 * them while input is arriving them loses sync until 2362 * data stops arriving. 2363 */ 2364 dlbl = divisor & 0xFF; 2365 if (sio_getreg(com, com_dlbl) != dlbl) 2366 sio_setreg(com, com_dlbl, dlbl); 2367 dlbh = divisor >> 8; 2368 if (sio_getreg(com, com_dlbh) != dlbh) 2369 sio_setreg(com, com_dlbh, dlbh); 2370 } 2371 2372 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 2373 2374 if (!(tp->t_state & TS_TTSTOP)) 2375 com->state |= CS_TTGO; 2376 2377 if (cflag & CRTS_IFLOW) { 2378 if (com->st16650a) { 2379 sio_setreg(com, com_cfcr, 0xbf); 2380 sio_setreg(com, com_fifo, 2381 sio_getreg(com, com_fifo) | 0x40); 2382 } 2383 com->state |= CS_RTS_IFLOW; 2384 /* 2385 * If CS_RTS_IFLOW just changed from off to on, the change 2386 * needs to be propagated to MCR_RTS. This isn't urgent, 2387 * so do it later by calling comstart() instead of repeating 2388 * a lot of code from comstart() here. 2389 */ 2390 } else if (com->state & CS_RTS_IFLOW) { 2391 com->state &= ~CS_RTS_IFLOW; 2392 /* 2393 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 2394 * on here, since comstart() won't do it later. 2395 */ 2396 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2397 if (com->st16650a) { 2398 sio_setreg(com, com_cfcr, 0xbf); 2399 sio_setreg(com, com_fifo, 2400 sio_getreg(com, com_fifo) & ~0x40); 2401 } 2402 } 2403 2404 2405 /* 2406 * Set up state to handle output flow control. 2407 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 2408 * Now has 10+ msec latency, while CTS flow has 50- usec latency. 2409 */ 2410 com->state |= CS_ODEVREADY; 2411 com->state &= ~CS_CTS_OFLOW; 2412 if (cflag & CCTS_OFLOW) { 2413 com->state |= CS_CTS_OFLOW; 2414 if (!(com->last_modem_status & MSR_CTS)) 2415 com->state &= ~CS_ODEVREADY; 2416 if (com->st16650a) { 2417 sio_setreg(com, com_cfcr, 0xbf); 2418 sio_setreg(com, com_fifo, 2419 sio_getreg(com, com_fifo) | 0x80); 2420 } 2421 } else { 2422 if (com->st16650a) { 2423 sio_setreg(com, com_cfcr, 0xbf); 2424 sio_setreg(com, com_fifo, 2425 sio_getreg(com, com_fifo) & ~0x80); 2426 } 2427 } 2428 2429 sio_setreg(com, com_cfcr, com->cfcr_image); 2430 2431 /* XXX shouldn't call functions while intrs are disabled. */ 2432 disc_optim(tp, t, com); 2433 /* 2434 * Recover from fiddling with CS_TTGO. We used to call siointr1() 2435 * unconditionally, but that defeated the careful discarding of 2436 * stale input in sioopen(). 2437 */ 2438 if (com->state >= (CS_BUSY | CS_TTGO)) { 2439 com_lock(); 2440 siointr1(com); 2441 com_unlock(); 2442 } 2443 crit_exit(); 2444 comstart(tp); 2445 if (com->ibufold != NULL) { 2446 kfree(com->ibufold, M_DEVBUF); 2447 com->ibufold = NULL; 2448 } 2449 return (0); 2450 } 2451 2452 /* 2453 * called with tty_token held 2454 */ 2455 static int 2456 siosetwater(struct com_s *com, speed_t speed) 2457 { 2458 int cp4ticks; 2459 u_char *ibuf; 2460 int ibufsize; 2461 struct tty *tp; 2462 2463 /* 2464 * Make the buffer size large enough to handle a softtty interrupt 2465 * latency of about 2 ticks without loss of throughput or data 2466 * (about 3 ticks if input flow control is not used or not honoured, 2467 * but a bit less for CS5-CS7 modes). 2468 */ 2469 cp4ticks = speed / 10 / hz * 4; 2470 for (ibufsize = 128; ibufsize < cp4ticks;) 2471 ibufsize <<= 1; 2472 if (ibufsize == com->ibufsize) 2473 return (0); 2474 2475 /* 2476 * Allocate input buffer. The extra factor of 2 in the size is 2477 * to allow for an error byte for each input byte. 2478 */ 2479 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK | M_ZERO); 2480 2481 /* Initialize non-critical variables. */ 2482 com->ibufold = com->ibuf; 2483 com->ibufsize = ibufsize; 2484 tp = com->tp; 2485 if (tp != NULL) { 2486 tp->t_ififosize = 2 * ibufsize; 2487 tp->t_ispeedwat = (speed_t)-1; 2488 tp->t_ospeedwat = (speed_t)-1; 2489 } 2490 2491 /* 2492 * Read current input buffer. 2493 */ 2494 com_lock(); 2495 if (com->iptr != com->ibuf) 2496 sioinput(com); 2497 2498 /*- 2499 * Initialize critical variables, including input buffer watermarks. 2500 * The external device is asked to stop sending when the buffer 2501 * exactly reaches high water, or when the high level requests it. 2502 * The high level is notified immediately (rather than at a later 2503 * clock tick) when this watermark is reached. 2504 * The buffer size is chosen so the watermark should almost never 2505 * be reached. 2506 * The low watermark is invisibly 0 since the buffer is always 2507 * emptied all at once. 2508 */ 2509 com->iptr = com->ibuf = ibuf; 2510 com->ibufend = ibuf + ibufsize; 2511 com->ierroff = ibufsize; 2512 com->ihighwater = ibuf + 3 * ibufsize / 4; 2513 com_unlock(); 2514 return (0); 2515 } 2516 2517 static void 2518 comstart(struct tty *tp) 2519 { 2520 struct com_s *com; 2521 int unit; 2522 2523 lwkt_gettoken(&tty_token); 2524 unit = DEV_TO_UNIT(tp->t_dev); 2525 com = com_addr(unit); 2526 if (com == NULL) { 2527 lwkt_reltoken(&tty_token); 2528 return; 2529 } 2530 crit_enter(); 2531 com_lock(); 2532 if (tp->t_state & TS_TTSTOP) 2533 com->state &= ~CS_TTGO; 2534 else 2535 com->state |= CS_TTGO; 2536 if (tp->t_state & TS_TBLOCK) { 2537 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 2538 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 2539 } else { 2540 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 2541 && com->state & CS_RTS_IFLOW) 2542 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2543 } 2544 com_unlock(); 2545 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 2546 ttwwakeup(tp); 2547 crit_exit(); 2548 lwkt_reltoken(&tty_token); 2549 return; 2550 } 2551 if (tp->t_outq.c_cc != 0) { 2552 struct lbq *qp; 2553 struct lbq *next; 2554 2555 if (!com->obufs[0].l_queued) { 2556 com->obufs[0].l_tail 2557 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 2558 sizeof com->obuf1); 2559 com->obufs[0].l_next = NULL; 2560 com->obufs[0].l_queued = TRUE; 2561 com_lock(); 2562 if (com->state & CS_BUSY) { 2563 qp = com->obufq.l_next; 2564 while ((next = qp->l_next) != NULL) 2565 qp = next; 2566 qp->l_next = &com->obufs[0]; 2567 } else { 2568 com->obufq.l_head = com->obufs[0].l_head; 2569 com->obufq.l_tail = com->obufs[0].l_tail; 2570 com->obufq.l_next = &com->obufs[0]; 2571 com->state |= CS_BUSY; 2572 } 2573 com_unlock(); 2574 } 2575 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 2576 com->obufs[1].l_tail 2577 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 2578 sizeof com->obuf2); 2579 com->obufs[1].l_next = NULL; 2580 com->obufs[1].l_queued = TRUE; 2581 com_lock(); 2582 if (com->state & CS_BUSY) { 2583 qp = com->obufq.l_next; 2584 while ((next = qp->l_next) != NULL) 2585 qp = next; 2586 qp->l_next = &com->obufs[1]; 2587 } else { 2588 com->obufq.l_head = com->obufs[1].l_head; 2589 com->obufq.l_tail = com->obufs[1].l_tail; 2590 com->obufq.l_next = &com->obufs[1]; 2591 com->state |= CS_BUSY; 2592 } 2593 com_unlock(); 2594 } 2595 tp->t_state |= TS_BUSY; 2596 } 2597 com_lock(); 2598 if (com->state >= (CS_BUSY | CS_TTGO)) 2599 siointr1(com); /* fake interrupt to start output */ 2600 com_unlock(); 2601 ttwwakeup(tp); 2602 crit_exit(); 2603 lwkt_reltoken(&tty_token); 2604 } 2605 2606 static void 2607 comstop(struct tty *tp, int rw) 2608 { 2609 struct com_s *com; 2610 2611 lwkt_gettoken(&tty_token); 2612 com = com_addr(DEV_TO_UNIT(tp->t_dev)); 2613 if (com == NULL || com->gone) { 2614 lwkt_reltoken(&tty_token); 2615 return; 2616 } 2617 com_lock(); 2618 if (rw & FWRITE) { 2619 if (com->hasfifo) 2620 #ifdef COM_ESP 2621 /* XXX avoid h/w bug. */ 2622 if (!com->esp) 2623 #endif 2624 sio_setreg(com, com_fifo, 2625 FIFO_XMT_RST | com->fifo_image); 2626 com->obufs[0].l_queued = FALSE; 2627 com->obufs[1].l_queued = FALSE; 2628 if (com->state & CS_ODONE) 2629 com_events -= LOTS_OF_EVENTS; 2630 com->state &= ~(CS_ODONE | CS_BUSY); 2631 com->tp->t_state &= ~TS_BUSY; 2632 } 2633 if (rw & FREAD) { 2634 if (com->hasfifo) 2635 #ifdef COM_ESP 2636 /* XXX avoid h/w bug. */ 2637 if (!com->esp) 2638 #endif 2639 sio_setreg(com, com_fifo, 2640 FIFO_RCV_RST | com->fifo_image); 2641 com_events -= (com->iptr - com->ibuf); 2642 com->iptr = com->ibuf; 2643 } 2644 com_unlock(); 2645 comstart(tp); 2646 lwkt_reltoken(&tty_token); 2647 } 2648 2649 static int 2650 commctl(struct com_s *com, int bits, int how) 2651 { 2652 int mcr; 2653 int msr; 2654 2655 lwkt_gettoken(&tty_token); 2656 if (how == DMGET) { 2657 bits = TIOCM_LE; /* XXX - always enabled while open */ 2658 mcr = com->mcr_image; 2659 if (mcr & MCR_DTR) 2660 bits |= TIOCM_DTR; 2661 if (mcr & MCR_RTS) 2662 bits |= TIOCM_RTS; 2663 msr = com->prev_modem_status; 2664 if (msr & MSR_CTS) 2665 bits |= TIOCM_CTS; 2666 if (msr & MSR_DCD) 2667 bits |= TIOCM_CD; 2668 if (msr & MSR_DSR) 2669 bits |= TIOCM_DSR; 2670 /* 2671 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 2672 * more volatile by reading the modem status a lot. Perhaps 2673 * we should latch both bits until the status is read here. 2674 */ 2675 if (msr & (MSR_RI | MSR_TERI)) 2676 bits |= TIOCM_RI; 2677 lwkt_reltoken(&tty_token); 2678 return (bits); 2679 } 2680 mcr = 0; 2681 if (bits & TIOCM_DTR) 2682 mcr |= MCR_DTR; 2683 if (bits & TIOCM_RTS) 2684 mcr |= MCR_RTS; 2685 if (com->gone) { 2686 lwkt_reltoken(&tty_token); 2687 return(0); 2688 } 2689 com_lock(); 2690 switch (how) { 2691 case DMSET: 2692 outb(com->modem_ctl_port, 2693 com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 2694 break; 2695 case DMBIS: 2696 outb(com->modem_ctl_port, com->mcr_image |= mcr); 2697 break; 2698 case DMBIC: 2699 outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 2700 break; 2701 } 2702 com_unlock(); 2703 lwkt_reltoken(&tty_token); 2704 return (0); 2705 } 2706 2707 /* 2708 * NOTE: Must be called with tty_token held 2709 */ 2710 static void 2711 siosettimeout(void) 2712 { 2713 struct com_s *com; 2714 bool_t someopen; 2715 int unit; 2716 2717 ASSERT_LWKT_TOKEN_HELD(&tty_token); 2718 /* 2719 * Set our timeout period to 1 second if no polled devices are open. 2720 * Otherwise set it to max(1/200, 1/hz). 2721 * Enable timeouts iff some device is open. 2722 */ 2723 callout_stop(&sio_timeout_handle); 2724 sio_timeout = hz; 2725 someopen = FALSE; 2726 for (unit = 0; unit < sio_numunits; ++unit) { 2727 com = com_addr(unit); 2728 if (com != NULL && com->tp != NULL 2729 && com->tp->t_state & TS_ISOPEN && !com->gone) { 2730 someopen = TRUE; 2731 if (com->poll || com->poll_output) { 2732 sio_timeout = hz > 200 ? hz / 200 : 1; 2733 break; 2734 } 2735 } 2736 } 2737 if (someopen) { 2738 sio_timeouts_until_log = hz / sio_timeout; 2739 callout_reset(&sio_timeout_handle, sio_timeout, 2740 comwakeup, NULL); 2741 } else { 2742 /* Flush error messages, if any. */ 2743 sio_timeouts_until_log = 1; 2744 comwakeup(NULL); 2745 callout_stop(&sio_timeout_handle); 2746 } 2747 } 2748 2749 /* 2750 * NOTE: Must be called with tty_token held 2751 */ 2752 static void 2753 comwakeup(void *chan) 2754 { 2755 struct com_s *com; 2756 int unit; 2757 2758 /* 2759 * Can be called from a callout too so just get the token 2760 */ 2761 lwkt_gettoken(&tty_token); 2762 callout_reset(&sio_timeout_handle, sio_timeout, comwakeup, NULL); 2763 2764 /* 2765 * Recover from lost output interrupts. 2766 * Poll any lines that don't use interrupts. 2767 */ 2768 for (unit = 0; unit < sio_numunits; ++unit) { 2769 com = com_addr(unit); 2770 if (com != NULL && !com->gone 2771 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 2772 com_lock(); 2773 siointr1(com); 2774 com_unlock(); 2775 } 2776 } 2777 2778 /* 2779 * Check for and log errors, but not too often. 2780 */ 2781 if (--sio_timeouts_until_log > 0) { 2782 lwkt_reltoken(&tty_token); 2783 return; 2784 } 2785 sio_timeouts_until_log = hz / sio_timeout; 2786 for (unit = 0; unit < sio_numunits; ++unit) { 2787 int errnum; 2788 2789 com = com_addr(unit); 2790 if (com == NULL) 2791 continue; 2792 if (com->gone) 2793 continue; 2794 for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 2795 u_int delta; 2796 u_long total; 2797 2798 com_lock(); 2799 delta = com->delta_error_counts[errnum]; 2800 com->delta_error_counts[errnum] = 0; 2801 com_unlock(); 2802 if (delta == 0) 2803 continue; 2804 total = com->error_counts[errnum] += delta; 2805 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 2806 unit, delta, error_desc[errnum], 2807 delta == 1 ? "" : "s", total); 2808 } 2809 } 2810 lwkt_reltoken(&tty_token); 2811 } 2812 2813 static void 2814 disc_optim(struct tty *tp, struct termios *t, struct com_s *com) 2815 { 2816 lwkt_gettoken(&tty_token); 2817 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 2818 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 2819 && (!(t->c_iflag & PARMRK) 2820 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 2821 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 2822 && linesw[tp->t_line].l_rint == ttyinput) 2823 tp->t_state |= TS_CAN_BYPASS_L_RINT; 2824 else 2825 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 2826 com->hotchar = linesw[tp->t_line].l_hotchar; 2827 lwkt_reltoken(&tty_token); 2828 } 2829 2830 /* 2831 * Following are all routines needed for SIO to act as console 2832 */ 2833 #include <sys/cons.h> 2834 2835 struct siocnstate { 2836 u_char dlbl; 2837 u_char dlbh; 2838 u_char ier; 2839 u_char cfcr; 2840 u_char mcr; 2841 }; 2842 2843 static speed_t siocngetspeed (Port_t, u_long rclk); 2844 static void siocnclose (struct siocnstate *sp, Port_t iobase); 2845 static void siocnopen (struct siocnstate *sp, Port_t iobase, int speed); 2846 static void siocntxwait (Port_t iobase); 2847 2848 static cn_probe_t siocnprobe; 2849 static cn_init_t siocninit; 2850 static cn_init_fini_t siocninit_fini; 2851 static cn_checkc_t siocncheckc; 2852 static cn_getc_t siocngetc; 2853 static cn_putc_t siocnputc; 2854 2855 #if defined(__i386__) || defined(__x86_64__) 2856 CONS_DRIVER(sio, siocnprobe, siocninit, siocninit_fini, 2857 NULL, siocngetc, siocncheckc, siocnputc, NULL); 2858 #endif 2859 2860 /* To get the GDB related variables */ 2861 #if DDB > 0 2862 #include <ddb/ddb.h> 2863 #endif 2864 2865 static void 2866 siocntxwait(Port_t iobase) 2867 { 2868 int timo; 2869 2870 /* 2871 * Wait for any pending transmission to finish. Required to avoid 2872 * the UART lockup bug when the speed is changed, and for normal 2873 * transmits. 2874 */ 2875 timo = 100000; 2876 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 2877 != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 2878 ; 2879 } 2880 2881 /* 2882 * Read the serial port specified and try to figure out what speed 2883 * it's currently running at. We're assuming the serial port has 2884 * been initialized and is basicly idle. This routine is only intended 2885 * to be run at system startup. 2886 * 2887 * If the value read from the serial port doesn't make sense, return 0. 2888 */ 2889 /* 2890 * NOTE: Must be called with tty_token held 2891 */ 2892 static speed_t 2893 siocngetspeed(Port_t iobase, u_long rclk) 2894 { 2895 u_int divisor; 2896 u_char dlbh; 2897 u_char dlbl; 2898 u_char cfcr; 2899 2900 cfcr = inb(iobase + com_cfcr); 2901 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 2902 2903 dlbl = inb(iobase + com_dlbl); 2904 dlbh = inb(iobase + com_dlbh); 2905 2906 outb(iobase + com_cfcr, cfcr); 2907 2908 divisor = dlbh << 8 | dlbl; 2909 2910 /* XXX there should be more sanity checking. */ 2911 if (divisor == 0) 2912 return (CONSPEED); 2913 return (rclk / (16UL * divisor)); 2914 } 2915 2916 static void 2917 siocnopen(struct siocnstate *sp, Port_t iobase, int speed) 2918 { 2919 u_int divisor; 2920 u_char dlbh; 2921 u_char dlbl; 2922 2923 /* 2924 * Save all the device control registers except the fifo register 2925 * and set our default ones (cs8 -parenb speed=comdefaultrate). 2926 * We can't save the fifo register since it is read-only. 2927 */ 2928 sp->ier = inb(iobase + com_ier); 2929 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 2930 siocntxwait(iobase); 2931 sp->cfcr = inb(iobase + com_cfcr); 2932 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2933 sp->dlbl = inb(iobase + com_dlbl); 2934 sp->dlbh = inb(iobase + com_dlbh); 2935 /* 2936 * Only set the divisor registers if they would change, since on 2937 * some 16550 incompatibles (Startech), setting them clears the 2938 * data input register. This also reduces the effects of the 2939 * UMC8669F bug. 2940 */ 2941 divisor = siodivisor(comdefaultrclk, speed); 2942 dlbl = divisor & 0xFF; 2943 if (sp->dlbl != dlbl) 2944 outb(iobase + com_dlbl, dlbl); 2945 dlbh = divisor >> 8; 2946 if (sp->dlbh != dlbh) 2947 outb(iobase + com_dlbh, dlbh); 2948 outb(iobase + com_cfcr, CFCR_8BITS); 2949 sp->mcr = inb(iobase + com_mcr); 2950 /* 2951 * We don't want interrupts, but must be careful not to "disable" 2952 * them by clearing the MCR_IENABLE bit, since that might cause 2953 * an interrupt by floating the IRQ line. 2954 */ 2955 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 2956 } 2957 2958 static void 2959 siocnclose(struct siocnstate *sp, Port_t iobase) 2960 { 2961 /* 2962 * Restore the device control registers. 2963 */ 2964 siocntxwait(iobase); 2965 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2966 if (sp->dlbl != inb(iobase + com_dlbl)) 2967 outb(iobase + com_dlbl, sp->dlbl); 2968 if (sp->dlbh != inb(iobase + com_dlbh)) 2969 outb(iobase + com_dlbh, sp->dlbh); 2970 outb(iobase + com_cfcr, sp->cfcr); 2971 /* 2972 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 2973 */ 2974 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 2975 outb(iobase + com_ier, sp->ier); 2976 } 2977 2978 static void 2979 siocnprobe(struct consdev *cp) 2980 { 2981 speed_t boot_speed; 2982 u_char cfcr; 2983 u_int divisor; 2984 int unit; 2985 struct siocnstate sp; 2986 2987 /* 2988 * Find our first enabled console, if any. If it is a high-level 2989 * console device, then initialize it and return successfully. 2990 * If it is a low-level console device, then initialize it and 2991 * return unsuccessfully. It must be initialized in both cases 2992 * for early use by console drivers and debuggers. Initializing 2993 * the hardware is not necessary in all cases, since the i/o 2994 * routines initialize it on the fly, but it is necessary if 2995 * input might arrive while the hardware is switched back to an 2996 * uninitialized state. We can't handle multiple console devices 2997 * yet because our low-level routines don't take a device arg. 2998 * We trust the user to set the console flags properly so that we 2999 * don't need to probe. 3000 */ 3001 cp->cn_pri = CN_DEAD; 3002 3003 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 3004 int flags; 3005 int disabled; 3006 if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 3007 if (disabled) 3008 continue; 3009 } 3010 if (resource_int_value("sio", unit, "flags", &flags)) 3011 continue; 3012 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 3013 int port; 3014 Port_t iobase; 3015 3016 if (resource_int_value("sio", unit, "port", &port)) 3017 continue; 3018 iobase = port; 3019 crit_enter(); 3020 if (boothowto & RB_SERIAL) { 3021 boot_speed = 3022 siocngetspeed(iobase, comdefaultrclk); 3023 if (boot_speed) 3024 comdefaultrate = boot_speed; 3025 } 3026 3027 /* 3028 * Initialize the divisor latch. We can't rely on 3029 * siocnopen() to do this the first time, since it 3030 * avoids writing to the latch if the latch appears 3031 * to have the correct value. Also, if we didn't 3032 * just read the speed from the hardware, then we 3033 * need to set the speed in hardware so that 3034 * switching it later is null. 3035 */ 3036 com_lock(); 3037 cfcr = inb(iobase + com_cfcr); 3038 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 3039 divisor = siodivisor(comdefaultrclk, comdefaultrate); 3040 outb(iobase + com_dlbl, divisor & 0xff); 3041 outb(iobase + com_dlbh, divisor >> 8); 3042 outb(iobase + com_cfcr, cfcr); 3043 3044 siocnopen(&sp, iobase, comdefaultrate); 3045 com_unlock(); 3046 3047 crit_exit(); 3048 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 3049 cp->cn_probegood = 1; 3050 cp->cn_private = (void *)(intptr_t)unit; 3051 cp->cn_pri = COM_FORCECONSOLE(flags) 3052 || boothowto & RB_SERIAL 3053 ? CN_REMOTE : CN_NORMAL; 3054 siocniobase = iobase; 3055 siocnunit = unit; 3056 } 3057 if (COM_DEBUGGER(flags) && gdb_tab == NULL) { 3058 kprintf("sio%d: gdb debugging port\n", unit); 3059 siogdbiobase = iobase; 3060 siogdbunit = unit; 3061 #if DDB > 0 3062 cp->cn_gdbprivate = (void *)(intptr_t)unit; 3063 gdb_tab = cp; 3064 #endif 3065 } 3066 } 3067 } 3068 #if defined(__i386__) || defined(__x86_64__) 3069 #if DDB > 0 3070 /* 3071 * XXX Ugly Compatability. 3072 * If no gdb port has been specified, set it to be the console 3073 * as some configuration files don't specify the gdb port. 3074 */ 3075 if (gdb_tab == NULL && (boothowto & RB_GDB)) { 3076 kprintf("Warning: no GDB port specified. Defaulting to sio%d.\n", 3077 siocnunit); 3078 kprintf("Set flag 0x80 on desired GDB port in your\n"); 3079 kprintf("configuration file (currently sio only).\n"); 3080 siogdbiobase = siocniobase; 3081 siogdbunit = siocnunit; 3082 cp->cn_gdbprivate = (void *)(intptr_t)siocnunit; 3083 gdb_tab = cp; 3084 } 3085 #endif 3086 #endif 3087 } 3088 3089 static void 3090 siocninit(struct consdev *cp) 3091 { 3092 comconsole = (int)(intptr_t)cp->cn_private; 3093 } 3094 3095 static void 3096 siocninit_fini(struct consdev *cp) 3097 { 3098 cdev_t dev; 3099 int unit; 3100 3101 if (cp->cn_probegood) { 3102 unit = (int)(intptr_t)cp->cn_private; 3103 /* 3104 * Call devfs_find_device_by_name on ttydX to find the correct device, 3105 * as it should have been created already at this point by the 3106 * attach routine. 3107 * If it isn't found, the serial port was not attached at all and we 3108 * shouldn't be here, so assert this case. 3109 */ 3110 dev = devfs_find_device_by_name("ttyd%r", unit); 3111 3112 KKASSERT(dev != NULL); 3113 cp->cn_dev = dev; 3114 } 3115 } 3116 3117 static int 3118 siocncheckc(void *private) 3119 { 3120 int c; 3121 int unit = (int)(intptr_t)private; 3122 Port_t iobase; 3123 struct siocnstate sp; 3124 3125 if (unit == siogdbunit) 3126 iobase = siogdbiobase; 3127 else 3128 iobase = siocniobase; 3129 com_lock(); 3130 crit_enter(); 3131 siocnopen(&sp, iobase, comdefaultrate); 3132 if (inb(iobase + com_lsr) & LSR_RXRDY) 3133 c = inb(iobase + com_data); 3134 else 3135 c = -1; 3136 siocnclose(&sp, iobase); 3137 crit_exit(); 3138 com_unlock(); 3139 return (c); 3140 } 3141 3142 3143 int 3144 siocngetc(void *private) 3145 { 3146 int c; 3147 int unit = (int)(intptr_t)private; 3148 Port_t iobase; 3149 struct siocnstate sp; 3150 3151 if (unit == siogdbunit) 3152 iobase = siogdbiobase; 3153 else 3154 iobase = siocniobase; 3155 com_lock(); 3156 crit_enter(); 3157 siocnopen(&sp, iobase, comdefaultrate); 3158 while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 3159 ; 3160 c = inb(iobase + com_data); 3161 siocnclose(&sp, iobase); 3162 crit_exit(); 3163 com_unlock(); 3164 return (c); 3165 } 3166 3167 void 3168 siocnputc(void *private, int c) 3169 { 3170 int unit = (int)(intptr_t)private; 3171 struct siocnstate sp; 3172 Port_t iobase; 3173 3174 if (unit == siogdbunit) 3175 iobase = siogdbiobase; 3176 else 3177 iobase = siocniobase; 3178 com_lock(); 3179 crit_enter(); 3180 siocnopen(&sp, iobase, comdefaultrate); 3181 siocntxwait(iobase); 3182 outb(iobase + com_data, c); 3183 siocnclose(&sp, iobase); 3184 crit_exit(); 3185 com_unlock(); 3186 } 3187 3188 DRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, NULL, NULL); 3189 DRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, NULL, NULL); 3190 #if NPCI > 0 3191 DRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, NULL, NULL); 3192 #endif 3193 #if NPUC > 0 3194 DRIVER_MODULE(sio, puc, sio_puc_driver, sio_devclass, NULL, NULL); 3195 #endif 3196