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