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 com_unlock(); 735 lwkt_reltoken(&tty_token); 736 kprintf("sio%d: can't drain, serial port might " 737 "not exist, disabling\n", device_get_unit(dev)); 738 return (ENXIO); 739 } 740 741 /* 742 * Enable the interrupt gate and disable device interupts. This 743 * should leave the device driving the interrupt line low and 744 * guarantee an edge trigger if an interrupt can be generated. 745 */ 746 /* EXTRA DELAY? */ 747 sio_setreg(com, com_mcr, mcr_image); 748 sio_setreg(com, com_ier, 0); 749 DELAY(1000); /* XXX */ 750 irqmap[0] = isa_irq_pending(); 751 752 /* 753 * Attempt to set loopback mode so that we can send a null byte 754 * without annoying any external device. 755 */ 756 /* EXTRA DELAY? */ 757 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 758 759 /* 760 * Attempt to generate an output interrupt. On 8250's, setting 761 * IER_ETXRDY generates an interrupt independent of the current 762 * setting and independent of whether the THR is empty. On 16450's, 763 * setting IER_ETXRDY generates an interrupt independent of the 764 * current setting. On 16550A's, setting IER_ETXRDY only 765 * generates an interrupt when IER_ETXRDY is not already set. 766 */ 767 sio_setreg(com, com_ier, IER_ETXRDY); 768 769 /* 770 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 771 * an interrupt. They'd better generate one for actually doing 772 * output. Loopback may be broken on the same incompatibles but 773 * it's unlikely to do more than allow the null byte out. 774 */ 775 sio_setreg(com, com_data, 0); 776 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 777 778 /* 779 * Turn off loopback mode so that the interrupt gate works again 780 * (MCR_IENABLE was hidden). This should leave the device driving 781 * an interrupt line high. It doesn't matter if the interrupt 782 * line oscillates while we are not looking at it, since interrupts 783 * are disabled. 784 */ 785 /* EXTRA DELAY? */ 786 sio_setreg(com, com_mcr, mcr_image); 787 788 /* 789 * Some pcmcia cards have the "TXRDY bug", so we check everyone 790 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 791 */ 792 if (COM_NOPROBE(flags)) { 793 /* Reading IIR register twice */ 794 for (fn = 0; fn < 2; fn ++) { 795 DELAY(10000); 796 failures[6] = sio_getreg(com, com_iir); 797 } 798 /* Check IIR_TXRDY clear ? */ 799 result = 0; 800 if (failures[6] & IIR_TXRDY) { 801 /* Nop, Double check with clearing IER */ 802 sio_setreg(com, com_ier, 0); 803 if (sio_getreg(com, com_iir) & IIR_NOPEND) { 804 /* Ok. we're familia this gang */ 805 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 806 } else { 807 /* Unknown, Just omit this chip.. XXX */ 808 result = ENXIO; 809 sio_setreg(com, com_mcr, 0); 810 } 811 } else { 812 /* OK. this is well-known guys */ 813 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 814 } 815 sio_setreg(com, com_ier, 0); 816 sio_setreg(com, com_cfcr, CFCR_8BITS); 817 com_unlock(); 818 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 819 lwkt_reltoken(&tty_token); 820 return (iobase == siocniobase ? 0 : result); 821 } 822 823 /* 824 * Check that 825 * o the CFCR, IER and MCR in UART hold the values written to them 826 * (the values happen to be all distinct - this is good for 827 * avoiding false positive tests from bus echoes). 828 * o an output interrupt is generated and its vector is correct. 829 * o the interrupt goes away when the IIR in the UART is read. 830 */ 831 /* EXTRA DELAY? */ 832 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 833 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 834 failures[2] = sio_getreg(com, com_mcr) - mcr_image; 835 DELAY(10000); /* Some internal modems need this time */ 836 irqmap[1] = isa_irq_pending(); 837 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 838 DELAY(1000); /* XXX */ 839 irqmap[2] = isa_irq_pending(); 840 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 841 842 /* 843 * Turn off all device interrupts and check that they go off properly. 844 * Leave MCR_IENABLE alone. For ports without a master port, it gates 845 * the OUT2 output of the UART to 846 * the ICU input. Closing the gate would give a floating ICU input 847 * (unless there is another device driving it) and spurious interrupts. 848 * (On the system that this was first tested on, the input floats high 849 * and gives a (masked) interrupt as soon as the gate is closed.) 850 */ 851 sio_setreg(com, com_ier, 0); 852 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 853 failures[7] = sio_getreg(com, com_ier); 854 DELAY(1000); /* XXX */ 855 irqmap[3] = isa_irq_pending(); 856 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 857 858 com_unlock(); 859 860 irqs = irqmap[1] & ~irqmap[0]; 861 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 862 ((1 << xirq) & irqs) == 0) 863 kprintf( 864 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 865 device_get_unit(dev), xirq, irqs); 866 if (bootverbose) 867 kprintf("sio%d: irq maps: %#x %#x %#x %#x\n", 868 device_get_unit(dev), 869 irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 870 871 result = 0; 872 for (fn = 0; fn < sizeof failures; ++fn) 873 if (failures[fn]) { 874 sio_setreg(com, com_mcr, 0); 875 result = ENXIO; 876 if (bootverbose) { 877 kprintf("sio%d: probe failed test(s):", 878 device_get_unit(dev)); 879 for (fn = 0; fn < sizeof failures; ++fn) 880 if (failures[fn]) 881 kprintf(" %d", fn); 882 kprintf("\n"); 883 } 884 break; 885 } 886 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 887 lwkt_reltoken(&tty_token); 888 return (iobase == siocniobase ? 0 : result); 889 } 890 891 #ifdef COM_ESP 892 static int 893 espattach(struct com_s *com, Port_t esp_port) 894 { 895 u_char dips; 896 u_char val; 897 898 /* 899 * Check the ESP-specific I/O port to see if we're an ESP 900 * card. If not, return failure immediately. 901 */ 902 if ((inb(esp_port) & 0xf3) == 0) { 903 kprintf(" port 0x%x is not an ESP board?\n", esp_port); 904 return (0); 905 } 906 907 lwkt_gettoken(&tty_token); 908 /* 909 * We've got something that claims to be a Hayes ESP card. 910 * Let's hope so. 911 */ 912 913 /* Get the dip-switch configuration */ 914 outb(esp_port + ESP_CMD1, ESP_GETDIPS); 915 dips = inb(esp_port + ESP_STATUS1); 916 917 /* 918 * Bits 0,1 of dips say which COM port we are. 919 */ 920 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 921 kprintf(" : ESP"); 922 else { 923 kprintf(" esp_port has com %d\n", dips & 0x03); 924 lwkt_reltoken(&tty_token); 925 return (0); 926 } 927 928 /* 929 * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 930 */ 931 outb(esp_port + ESP_CMD1, ESP_GETTEST); 932 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 933 val = inb(esp_port + ESP_STATUS2); 934 if ((val & 0x70) < 0x20) { 935 kprintf("-old (%o)", val & 0x70); 936 lwkt_reltoken(&tty_token); 937 return (0); 938 } 939 940 /* 941 * Check for ability to emulate 16550: bit 7 == 1 942 */ 943 if ((dips & 0x80) == 0) { 944 kprintf(" slave"); 945 lwkt_reltoken(&tty_token); 946 return (0); 947 } 948 949 /* 950 * Okay, we seem to be a Hayes ESP card. Whee. 951 */ 952 com->esp = TRUE; 953 com->esp_port = esp_port; 954 lwkt_reltoken(&tty_token); 955 return (1); 956 } 957 #endif /* COM_ESP */ 958 959 static int 960 sio_isa_attach(device_t dev) 961 { 962 return (sioattach(dev, 0, 0UL)); 963 } 964 965 int 966 sioattach(device_t dev, int xrid, u_long rclk) 967 { 968 struct com_s *com; 969 #ifdef COM_ESP 970 Port_t *espp; 971 #endif 972 Port_t iobase; 973 int minorbase; 974 int unit; 975 u_int flags; 976 int rid; 977 struct resource *port; 978 int ret; 979 static int did_init; 980 981 lwkt_gettoken(&tty_token); 982 if (did_init == 0) { 983 did_init = 1; 984 callout_init_mp(&sio_timeout_handle); 985 } 986 987 rid = xrid; 988 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 989 0, ~0, IO_COMSIZE, RF_ACTIVE); 990 if (!port) { 991 lwkt_reltoken(&tty_token); 992 return (ENXIO); 993 } 994 995 iobase = rman_get_start(port); 996 unit = device_get_unit(dev); 997 com = device_get_softc(dev); 998 flags = device_get_flags(dev); 999 1000 if (unit >= sio_numunits) 1001 sio_numunits = unit + 1; 1002 /* 1003 * sioprobe() has initialized the device registers as follows: 1004 * o cfcr = CFCR_8BITS. 1005 * It is most important that CFCR_DLAB is off, so that the 1006 * data port is not hidden when we enable interrupts. 1007 * o ier = 0. 1008 * Interrupts are only enabled when the line is open. 1009 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 1010 * interrupt control register or the config specifies no irq. 1011 * Keeping MCR_DTR and MCR_RTS off might stop the external 1012 * device from sending before we are ready. 1013 */ 1014 bzero(com, sizeof *com); 1015 com->unit = unit; 1016 com->ioportres = port; 1017 com->bst = rman_get_bustag(port); 1018 com->bsh = rman_get_bushandle(port); 1019 com->cfcr_image = CFCR_8BITS; 1020 com->dtr_wait = 3 * hz; 1021 callout_init_mp(&com->dtr_ch); 1022 callout_init_mp(&com->busy_ch); 1023 com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 1024 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 1025 com->tx_fifo_size = 1; 1026 com->obufs[0].l_head = com->obuf1; 1027 com->obufs[1].l_head = com->obuf2; 1028 1029 com->data_port = iobase + com_data; 1030 com->int_id_port = iobase + com_iir; 1031 com->modem_ctl_port = iobase + com_mcr; 1032 com->mcr_image = inb(com->modem_ctl_port); 1033 com->line_status_port = iobase + com_lsr; 1034 com->modem_status_port = iobase + com_msr; 1035 com->intr_ctl_port = iobase + com_ier; 1036 1037 if (rclk == 0) 1038 rclk = DEFAULT_RCLK; 1039 com->rclk = rclk; 1040 1041 /* 1042 * We don't use all the flags from <sys/ttydefaults.h> since they 1043 * are only relevant for logins. It's important to have echo off 1044 * initially so that the line doesn't start blathering before the 1045 * echo flag can be turned off. 1046 */ 1047 com->it_in.c_iflag = 0; 1048 com->it_in.c_oflag = 0; 1049 com->it_in.c_cflag = TTYDEF_CFLAG; 1050 com->it_in.c_lflag = 0; 1051 if (unit == comconsole) { 1052 com->it_in.c_iflag = TTYDEF_IFLAG; 1053 com->it_in.c_oflag = TTYDEF_OFLAG; 1054 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 1055 com->it_in.c_lflag = TTYDEF_LFLAG; 1056 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 1057 com->lt_out.c_ispeed = com->lt_out.c_ospeed = 1058 com->lt_in.c_ispeed = com->lt_in.c_ospeed = 1059 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 1060 } else 1061 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 1062 if (siosetwater(com, com->it_in.c_ispeed) != 0) { 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 termioschars(&com->it_in); 1073 com->it_out = com->it_in; 1074 1075 /* attempt to determine UART type */ 1076 kprintf("sio%d: type", unit); 1077 1078 1079 #ifdef COM_MULTIPORT 1080 if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 1081 #else 1082 if (!COM_IIR_TXRDYBUG(flags)) 1083 #endif 1084 { 1085 u_char scr; 1086 u_char scr1; 1087 u_char scr2; 1088 1089 scr = sio_getreg(com, com_scr); 1090 sio_setreg(com, com_scr, 0xa5); 1091 scr1 = sio_getreg(com, com_scr); 1092 sio_setreg(com, com_scr, 0x5a); 1093 scr2 = sio_getreg(com, com_scr); 1094 sio_setreg(com, com_scr, scr); 1095 if (scr1 != 0xa5 || scr2 != 0x5a) { 1096 kprintf(" 8250"); 1097 goto determined_type; 1098 } 1099 } 1100 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 1101 DELAY(100); 1102 com->st16650a = 0; 1103 switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 1104 case FIFO_RX_LOW: 1105 kprintf(" 16450"); 1106 break; 1107 case FIFO_RX_MEDL: 1108 kprintf(" 16450?"); 1109 break; 1110 case FIFO_RX_MEDH: 1111 kprintf(" 16550?"); 1112 break; 1113 case FIFO_RX_HIGH: 1114 if (COM_NOFIFO(flags)) { 1115 kprintf(" 16550A fifo disabled"); 1116 } else { 1117 com->hasfifo = TRUE; 1118 if (COM_ST16650A(flags)) { 1119 com->st16650a = 1; 1120 com->tx_fifo_size = 32; 1121 kprintf(" ST16650A"); 1122 } else if (COM_TI16754(flags)) { 1123 com->tx_fifo_size = 64; 1124 kprintf(" TI16754"); 1125 } else { 1126 com->tx_fifo_size = COM_FIFOSIZE(flags); 1127 kprintf(" 16550A"); 1128 } 1129 } 1130 #ifdef COM_ESP 1131 for (espp = likely_esp_ports; *espp != 0; espp++) 1132 if (espattach(com, *espp)) { 1133 com->tx_fifo_size = 1024; 1134 break; 1135 } 1136 #endif 1137 if (!com->st16650a && !COM_TI16754(flags)) { 1138 if (!com->tx_fifo_size) 1139 com->tx_fifo_size = 16; 1140 else 1141 kprintf(" lookalike with %d bytes FIFO", 1142 com->tx_fifo_size); 1143 } 1144 1145 break; 1146 } 1147 1148 #ifdef COM_ESP 1149 if (com->esp) { 1150 /* 1151 * Set 16550 compatibility mode. 1152 * We don't use the ESP_MODE_SCALE bit to increase the 1153 * fifo trigger levels because we can't handle large 1154 * bursts of input. 1155 * XXX flow control should be set in comparam(), not here. 1156 */ 1157 outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 1158 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 1159 1160 /* Set RTS/CTS flow control. */ 1161 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 1162 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 1163 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 1164 1165 /* Set flow-control levels. */ 1166 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 1167 outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 1168 outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 1169 outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 1170 outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 1171 } 1172 #endif /* COM_ESP */ 1173 sio_setreg(com, com_fifo, 0); 1174 determined_type: ; 1175 1176 #ifdef COM_MULTIPORT 1177 if (COM_ISMULTIPORT(flags)) { 1178 device_t masterdev; 1179 1180 com->multiport = TRUE; 1181 kprintf(" (multiport"); 1182 if (unit == COM_MPMASTER(flags)) 1183 kprintf(" master"); 1184 kprintf(")"); 1185 masterdev = devclass_get_device(sio_devclass, 1186 COM_MPMASTER(flags)); 1187 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 1188 SYS_RES_IRQ, 0, NULL, NULL) != 0); 1189 } 1190 #endif /* COM_MULTIPORT */ 1191 if (unit == comconsole) 1192 kprintf(", console"); 1193 if (COM_IIR_TXRDYBUG(flags)) 1194 kprintf(" with a bogus IIR_TXRDY register"); 1195 kprintf("\n"); 1196 1197 if (!sio_registered) { 1198 register_swi(SWI_TTY, siopoll, NULL ,"swi_siopoll", NULL); 1199 sio_registered = TRUE; 1200 } 1201 minorbase = UNIT_TO_MINOR(unit); 1202 make_dev(&sio_ops, minorbase, 1203 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 1204 make_dev(&sio_ops, minorbase | CONTROL_INIT_STATE, 1205 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 1206 make_dev(&sio_ops, minorbase | CONTROL_LOCK_STATE, 1207 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 1208 make_dev(&sio_ops, minorbase | CALLOUT_MASK, 1209 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 1210 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_INIT_STATE, 1211 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 1212 make_dev(&sio_ops, minorbase | CALLOUT_MASK | CONTROL_LOCK_STATE, 1213 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 1214 com->flags = flags; 1215 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 1216 pps_init(&com->pps); 1217 1218 rid = 0; 1219 com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 1220 RF_ACTIVE); 1221 if (com->irqres) { 1222 ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 1223 com->irqres, INTR_MPSAFE, siointr, com, 1224 &com->cookie, NULL); 1225 if (ret) 1226 device_printf(dev, "could not activate interrupt\n"); 1227 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1228 defined(ALT_BREAK_TO_DEBUGGER)) 1229 /* 1230 * Enable interrupts for early break-to-debugger support 1231 * on the console. 1232 */ 1233 if (ret == 0 && unit == comconsole) 1234 outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 1235 IER_EMSC); 1236 #endif 1237 } 1238 1239 lwkt_reltoken(&tty_token); 1240 return (0); 1241 } 1242 1243 static int 1244 sioopen(struct dev_open_args *ap) 1245 { 1246 cdev_t dev = ap->a_head.a_dev; 1247 struct com_s *com; 1248 int error; 1249 int mynor; 1250 struct tty *tp; 1251 int unit; 1252 1253 mynor = minor(dev); 1254 unit = MINOR_TO_UNIT(mynor); 1255 com = com_addr(unit); 1256 if (com == NULL) 1257 return (ENXIO); 1258 if (com->gone) 1259 return (ENXIO); 1260 if (mynor & CONTROL_MASK) 1261 return (0); 1262 lwkt_gettoken(&tty_token); 1263 tp = dev->si_tty = com->tp = ttymalloc(com->tp); 1264 crit_enter(); 1265 /* 1266 * We jump to this label after all non-interrupted sleeps to pick 1267 * up any changes of the device state. 1268 */ 1269 open_top: 1270 while (com->state & CS_DTR_OFF) { 1271 error = tsleep(&com->dtr_wait, PCATCH, "siodtr", 0); 1272 if (com_addr(unit) == NULL) { 1273 crit_exit(); 1274 lwkt_reltoken(&tty_token); 1275 return (ENXIO); 1276 } 1277 if (error != 0 || com->gone) 1278 goto out; 1279 } 1280 if (tp->t_state & TS_ISOPEN) { 1281 /* 1282 * The device is open, so everything has been initialized. 1283 * Handle conflicts. 1284 */ 1285 if (mynor & CALLOUT_MASK) { 1286 if (!com->active_out) { 1287 error = EBUSY; 1288 goto out; 1289 } 1290 } else { 1291 if (com->active_out) { 1292 if (ap->a_oflags & O_NONBLOCK) { 1293 error = EBUSY; 1294 goto out; 1295 } 1296 error = tsleep(&com->active_out, 1297 PCATCH, "siobi", 0); 1298 if (com_addr(unit) == NULL) { 1299 crit_exit(); 1300 lwkt_reltoken(&tty_token); 1301 return (ENXIO); 1302 } 1303 if (error != 0 || com->gone) 1304 goto out; 1305 goto open_top; 1306 } 1307 } 1308 if (tp->t_state & TS_XCLUDE && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) { 1309 error = EBUSY; 1310 goto out; 1311 } 1312 } else { 1313 /* 1314 * The device isn't open, so there are no conflicts. 1315 * Initialize it. Initialization is done twice in many 1316 * cases: to preempt sleeping callin opens if we are 1317 * callout, and to complete a callin open after DCD rises. 1318 */ 1319 tp->t_oproc = comstart; 1320 tp->t_param = comparam; 1321 tp->t_stop = comstop; 1322 tp->t_dev = dev; 1323 tp->t_termios = mynor & CALLOUT_MASK 1324 ? com->it_out : com->it_in; 1325 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 1326 com->poll = com->no_irq; 1327 com->poll_output = com->loses_outints; 1328 ++com->wopeners; 1329 error = comparam(tp, &tp->t_termios); 1330 --com->wopeners; 1331 if (error != 0) 1332 goto out; 1333 /* 1334 * XXX we should goto open_top if comparam() slept. 1335 */ 1336 if (com->hasfifo) { 1337 /* 1338 * (Re)enable and drain fifos. 1339 * 1340 * Certain SMC chips cause problems if the fifos 1341 * are enabled while input is ready. Turn off the 1342 * fifo if necessary to clear the input. We test 1343 * the input ready bit after enabling the fifos 1344 * since we've already enabled them in comparam() 1345 * and to handle races between enabling and fresh 1346 * input. 1347 */ 1348 while (TRUE) { 1349 sio_setreg(com, com_fifo, 1350 FIFO_RCV_RST | FIFO_XMT_RST 1351 | com->fifo_image); 1352 /* 1353 * XXX the delays are for superstitious 1354 * historical reasons. It must be less than 1355 * the character time at the maximum 1356 * supported speed (87 usec at 115200 bps 1357 * 8N1). Otherwise we might loop endlessly 1358 * if data is streaming in. We used to use 1359 * delays of 100. That usually worked 1360 * because DELAY(100) used to usually delay 1361 * for about 85 usec instead of 100. 1362 */ 1363 DELAY(50); 1364 if (!(inb(com->line_status_port) & LSR_RXRDY)) 1365 break; 1366 sio_setreg(com, com_fifo, 0); 1367 DELAY(50); 1368 (void) inb(com->data_port); 1369 } 1370 } 1371 1372 com_lock(); 1373 (void) inb(com->line_status_port); 1374 (void) inb(com->data_port); 1375 com->prev_modem_status = com->last_modem_status 1376 = inb(com->modem_status_port); 1377 if (COM_IIR_TXRDYBUG(com->flags)) { 1378 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 1379 | IER_EMSC); 1380 } else { 1381 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 1382 | IER_ERLS | IER_EMSC); 1383 } 1384 com_unlock(); 1385 /* 1386 * Handle initial DCD. Callout devices get a fake initial 1387 * DCD (trapdoor DCD). If we are callout, then any sleeping 1388 * callin opens get woken up and resume sleeping on "siobi" 1389 * instead of "siodcd". 1390 */ 1391 /* 1392 * XXX `mynor & CALLOUT_MASK' should be 1393 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 1394 * TRAPDOOR_CARRIER is the default initial state for callout 1395 * devices and SOFT_CARRIER is like CLOCAL except it hides 1396 * the true carrier. 1397 */ 1398 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 1399 (*linesw[tp->t_line].l_modem)(tp, 1); 1400 } 1401 /* 1402 * Wait for DCD if necessary. 1403 */ 1404 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 1405 && !(tp->t_cflag & CLOCAL) && !(ap->a_oflags & O_NONBLOCK)) { 1406 ++com->wopeners; 1407 error = tsleep(TSA_CARR_ON(tp), PCATCH, "siodcd", 0); 1408 if (com_addr(unit) == NULL) { 1409 crit_exit(); 1410 lwkt_reltoken(&tty_token); 1411 return (ENXIO); 1412 } 1413 --com->wopeners; 1414 if (error != 0 || com->gone) 1415 goto out; 1416 goto open_top; 1417 } 1418 error = (*linesw[tp->t_line].l_open)(dev, tp); 1419 disc_optim(tp, &tp->t_termios, com); 1420 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 1421 com->active_out = TRUE; 1422 siosettimeout(); 1423 out: 1424 crit_exit(); 1425 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 1426 comhardclose(com); 1427 lwkt_reltoken(&tty_token); 1428 return (error); 1429 } 1430 1431 static int 1432 sioclose(struct dev_close_args *ap) 1433 { 1434 cdev_t dev = ap->a_head.a_dev; 1435 struct com_s *com; 1436 int mynor; 1437 struct tty *tp; 1438 1439 mynor = minor(dev); 1440 if (mynor & CONTROL_MASK) 1441 return (0); 1442 lwkt_gettoken(&tty_token); 1443 com = com_addr(MINOR_TO_UNIT(mynor)); 1444 if (com == NULL) { 1445 lwkt_reltoken(&tty_token); 1446 return (ENODEV); 1447 } 1448 tp = com->tp; 1449 crit_enter(); 1450 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); 1451 disc_optim(tp, &tp->t_termios, com); 1452 comstop(tp, FREAD | FWRITE); 1453 comhardclose(com); 1454 ttyclose(tp); 1455 siosettimeout(); 1456 crit_exit(); 1457 if (com->gone) { 1458 kprintf("sio%d: gone\n", com->unit); 1459 crit_enter(); 1460 if (com->ibuf != NULL) 1461 kfree(com->ibuf, M_DEVBUF); 1462 bzero(tp, sizeof *tp); 1463 crit_exit(); 1464 } 1465 lwkt_reltoken(&tty_token); 1466 return (0); 1467 } 1468 1469 static void 1470 comhardclose(struct com_s *com) 1471 { 1472 struct tty *tp; 1473 int unit; 1474 1475 unit = com->unit; 1476 crit_enter(); 1477 lwkt_gettoken(&tty_token); 1478 com->poll = FALSE; 1479 com->poll_output = FALSE; 1480 com->do_timestamp = FALSE; 1481 com->do_dcd_timestamp = FALSE; 1482 com->pps.ppsparam.mode = 0; 1483 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 1484 tp = com->tp; 1485 1486 #if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 1487 defined(ALT_BREAK_TO_DEBUGGER)) 1488 /* 1489 * Leave interrupts enabled and don't clear DTR if this is the 1490 * console. This allows us to detect break-to-debugger events 1491 * while the console device is closed. 1492 */ 1493 if (com->unit != comconsole) 1494 #endif 1495 { 1496 sio_setreg(com, com_ier, 0); 1497 if (tp->t_cflag & HUPCL 1498 /* 1499 * XXX we will miss any carrier drop between here and the 1500 * next open. Perhaps we should watch DCD even when the 1501 * port is closed; it is not sufficient to check it at 1502 * the next open because it might go up and down while 1503 * we're not watching. 1504 */ 1505 || (!com->active_out 1506 && !(com->prev_modem_status & MSR_DCD) 1507 && !(com->it_in.c_cflag & CLOCAL)) 1508 || !(tp->t_state & TS_ISOPEN)) { 1509 (void)commctl(com, TIOCM_DTR, DMBIC); 1510 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 1511 callout_reset(&com->dtr_ch, com->dtr_wait, 1512 siodtrwakeup, com); 1513 com->state |= CS_DTR_OFF; 1514 } 1515 } 1516 } 1517 if (com->hasfifo) { 1518 /* 1519 * Disable fifos so that they are off after controlled 1520 * reboots. Some BIOSes fail to detect 16550s when the 1521 * fifos are enabled. 1522 */ 1523 sio_setreg(com, com_fifo, 0); 1524 } 1525 com->active_out = FALSE; 1526 wakeup(&com->active_out); 1527 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 1528 lwkt_reltoken(&tty_token); 1529 crit_exit(); 1530 } 1531 1532 static int 1533 sioread(struct dev_read_args *ap) 1534 { 1535 cdev_t dev = ap->a_head.a_dev; 1536 int mynor, ret; 1537 struct com_s *com; 1538 1539 lwkt_gettoken(&tty_token); 1540 mynor = minor(dev); 1541 if (mynor & CONTROL_MASK) { 1542 lwkt_reltoken(&tty_token); 1543 return (ENODEV); 1544 } 1545 com = com_addr(MINOR_TO_UNIT(mynor)); 1546 if (com == NULL || com->gone) { 1547 lwkt_reltoken(&tty_token); 1548 return (ENODEV); 1549 } 1550 ret = ((*linesw[com->tp->t_line].l_read)(com->tp, ap->a_uio, ap->a_ioflag)); 1551 lwkt_reltoken(&tty_token); 1552 return ret; 1553 } 1554 1555 static int 1556 siowrite(struct dev_write_args *ap) 1557 { 1558 cdev_t dev = ap->a_head.a_dev; 1559 int mynor; 1560 struct com_s *com; 1561 int unit, ret; 1562 1563 lwkt_gettoken(&tty_token); 1564 mynor = minor(dev); 1565 if (mynor & CONTROL_MASK) { 1566 lwkt_reltoken(&tty_token); 1567 return (ENODEV); 1568 } 1569 1570 unit = MINOR_TO_UNIT(mynor); 1571 com = com_addr(unit); 1572 if (com == NULL || com->gone) { 1573 lwkt_reltoken(&tty_token); 1574 return (ENODEV); 1575 } 1576 /* 1577 * (XXX) We disallow virtual consoles if the physical console is 1578 * a serial port. This is in case there is a display attached that 1579 * is not the console. In that situation we don't need/want the X 1580 * server taking over the console. 1581 */ 1582 if (constty != NULL && unit == comconsole) 1583 constty = NULL; 1584 ret = ((*linesw[com->tp->t_line].l_write)(com->tp, ap->a_uio, ap->a_ioflag)); 1585 lwkt_reltoken(&tty_token); 1586 return ret; 1587 } 1588 1589 static void 1590 siobusycheck(void *chan) 1591 { 1592 struct com_s *com; 1593 1594 lwkt_gettoken(&tty_token); 1595 com = (struct com_s *)chan; 1596 1597 /* 1598 * Clear TS_BUSY if low-level output is complete. 1599 * spl locking is sufficient because siointr1() does not set CS_BUSY. 1600 * If siointr1() clears CS_BUSY after we look at it, then we'll get 1601 * called again. Reading the line status port outside of siointr1() 1602 * is safe because CS_BUSY is clear so there are no output interrupts 1603 * to lose. 1604 */ 1605 crit_enter(); 1606 if (com->state & CS_BUSY) 1607 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 1608 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 1609 == (LSR_TSRE | LSR_TXRDY)) { 1610 com->tp->t_state &= ~TS_BUSY; 1611 ttwwakeup(com->tp); 1612 com->extra_state &= ~CSE_BUSYCHECK; 1613 } else { 1614 callout_reset(&com->busy_ch, hz / 100, siobusycheck, com); 1615 } 1616 crit_exit(); 1617 lwkt_reltoken(&tty_token); 1618 } 1619 1620 static u_int 1621 siodivisor(u_long rclk, speed_t speed) 1622 { 1623 long actual_speed; 1624 u_int divisor; 1625 int error; 1626 1627 if (speed == 0 || speed > ((speed_t)-1 - 1) / 8) 1628 return (0); 1629 divisor = (rclk / (8UL * speed) + 1) / 2; 1630 if (divisor == 0 || divisor >= 65536) 1631 return (0); 1632 actual_speed = rclk / (16UL * divisor); 1633 1634 /* 10 times error in percent: */ 1635 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2; 1636 1637 /* 3.0% maximum error tolerance: */ 1638 if (error < -30 || error > 30) 1639 return (0); 1640 1641 return (divisor); 1642 } 1643 1644 static void 1645 siodtrwakeup(void *chan) 1646 { 1647 struct com_s *com; 1648 1649 lwkt_gettoken(&tty_token); 1650 com = (struct com_s *)chan; 1651 com->state &= ~CS_DTR_OFF; 1652 wakeup(&com->dtr_wait); 1653 lwkt_reltoken(&tty_token); 1654 } 1655 1656 /* 1657 * NOTE: Normally called with tty_token held but might not be when 1658 * operating as the console. 1659 * 1660 * Must be called with com_lock 1661 */ 1662 static void 1663 sioinput(struct com_s *com) 1664 { 1665 u_char *buf; 1666 int incc; 1667 u_char line_status; 1668 int recv_data; 1669 struct tty *tp; 1670 1671 buf = com->ibuf; 1672 tp = com->tp; 1673 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 1674 com_events -= (com->iptr - com->ibuf); 1675 com->iptr = com->ibuf; 1676 return; 1677 } 1678 if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 1679 /* 1680 * Avoid the grotesquely inefficient lineswitch routine 1681 * (ttyinput) in "raw" mode. It usually takes about 450 1682 * instructions (that's without canonical processing or echo!). 1683 * slinput is reasonably fast (usually 40 instructions plus 1684 * call overhead). 1685 */ 1686 do { 1687 com_unlock(); 1688 incc = com->iptr - buf; 1689 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 1690 && (com->state & CS_RTS_IFLOW 1691 || tp->t_iflag & IXOFF) 1692 && !(tp->t_state & TS_TBLOCK)) 1693 ttyblock(tp); 1694 com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 1695 += b_to_q((char *)buf, incc, &tp->t_rawq); 1696 buf += incc; 1697 tk_nin += incc; 1698 tk_rawcc += incc; 1699 tp->t_rawcc += incc; 1700 ttwakeup(tp); 1701 if (tp->t_state & TS_TTSTOP 1702 && (tp->t_iflag & IXANY 1703 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 1704 tp->t_state &= ~TS_TTSTOP; 1705 tp->t_lflag &= ~FLUSHO; 1706 comstart(tp); 1707 } 1708 com_lock(); 1709 } while (buf < com->iptr); 1710 } else { 1711 do { 1712 com_unlock(); 1713 line_status = buf[com->ierroff]; 1714 recv_data = *buf++; 1715 if (line_status 1716 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 1717 if (line_status & LSR_BI) 1718 recv_data |= TTY_BI; 1719 if (line_status & LSR_FE) 1720 recv_data |= TTY_FE; 1721 if (line_status & LSR_OE) 1722 recv_data |= TTY_OE; 1723 if (line_status & LSR_PE) 1724 recv_data |= TTY_PE; 1725 } 1726 (*linesw[tp->t_line].l_rint)(recv_data, tp); 1727 com_lock(); 1728 } while (buf < com->iptr); 1729 } 1730 com_events -= (com->iptr - com->ibuf); 1731 com->iptr = com->ibuf; 1732 1733 /* 1734 * There is now room for another low-level buffer full of input, 1735 * so enable RTS if it is now disabled and there is room in the 1736 * high-level buffer. 1737 */ 1738 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 1739 !(tp->t_state & TS_TBLOCK)) 1740 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 1741 } 1742 1743 void 1744 siointr(void *arg) 1745 { 1746 lwkt_gettoken(&tty_token); 1747 #ifndef COM_MULTIPORT 1748 com_lock(); 1749 siointr1((struct com_s *) arg); 1750 com_unlock(); 1751 #else /* COM_MULTIPORT */ 1752 bool_t possibly_more_intrs; 1753 int unit; 1754 struct com_s *com; 1755 1756 /* 1757 * Loop until there is no activity on any port. This is necessary 1758 * to get an interrupt edge more than to avoid another interrupt. 1759 * If the IRQ signal is just an OR of the IRQ signals from several 1760 * devices, then the edge from one may be lost because another is 1761 * on. 1762 */ 1763 com_lock(); 1764 do { 1765 possibly_more_intrs = FALSE; 1766 for (unit = 0; unit < sio_numunits; ++unit) { 1767 com = com_addr(unit); 1768 /* 1769 * XXX com_lock(); 1770 * would it work here, or be counter-productive? 1771 */ 1772 if (com != NULL 1773 && !com->gone 1774 && (inb(com->int_id_port) & IIR_IMASK) 1775 != IIR_NOPEND) { 1776 siointr1(com); 1777 possibly_more_intrs = TRUE; 1778 } 1779 /* XXX com_unlock(); */ 1780 } 1781 } while (possibly_more_intrs); 1782 com_unlock(); 1783 #endif /* COM_MULTIPORT */ 1784 lwkt_reltoken(&tty_token); 1785 } 1786 1787 /* 1788 * Called with tty_token held and com_lock held. 1789 */ 1790 static void 1791 siointr1(struct com_s *com) 1792 { 1793 u_char line_status; 1794 u_char modem_status; 1795 u_char *ioptr; 1796 u_char recv_data; 1797 u_char int_ctl; 1798 u_char int_ctl_new; 1799 u_int count; 1800 1801 int_ctl = inb(com->intr_ctl_port); 1802 int_ctl_new = int_ctl; 1803 1804 while (!com->gone) { 1805 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 1806 modem_status = inb(com->modem_status_port); 1807 if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 1808 count = sys_cputimer->count(); 1809 pps_event(&com->pps, count, 1810 (modem_status & MSR_DCD) ? 1811 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 1812 } 1813 } 1814 line_status = inb(com->line_status_port); 1815 1816 /* input event? (check first to help avoid overruns) */ 1817 while (line_status & LSR_RCV_MASK) { 1818 /* break/unnattached error bits or real input? */ 1819 if (!(line_status & LSR_RXRDY)) 1820 recv_data = 0; 1821 else 1822 recv_data = inb(com->data_port); 1823 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 1824 /* 1825 * Solaris implements a new BREAK which is initiated 1826 * by a character sequence CR ~ ^b which is similar 1827 * to a familiar pattern used on Sun servers by the 1828 * Remote Console. 1829 */ 1830 #define KEY_CRTLB 2 /* ^B */ 1831 #define KEY_CR 13 /* CR '\r' */ 1832 #define KEY_TILDE 126 /* ~ */ 1833 1834 if (com->unit == comconsole) { 1835 static int brk_state1 = 0, brk_state2 = 0; 1836 if (recv_data == KEY_CR) { 1837 brk_state1 = recv_data; 1838 brk_state2 = 0; 1839 } else if (brk_state1 == KEY_CR && (recv_data == KEY_TILDE || recv_data == KEY_CRTLB)) { 1840 if (recv_data == KEY_TILDE) 1841 brk_state2 = recv_data; 1842 else if (brk_state2 == KEY_TILDE && recv_data == KEY_CRTLB) { 1843 com_unlock(); 1844 breakpoint(); 1845 com_lock(); 1846 brk_state1 = brk_state2 = 0; 1847 goto cont; 1848 } else 1849 brk_state2 = 0; 1850 } else 1851 brk_state1 = 0; 1852 } 1853 #endif 1854 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 1855 /* 1856 * Don't store BI if IGNBRK or FE/PE if IGNPAR. 1857 * Otherwise, push the work to a higher level 1858 * (to handle PARMRK) if we're bypassing. 1859 * Otherwise, convert BI/FE and PE+INPCK to 0. 1860 * 1861 * This makes bypassing work right in the 1862 * usual "raw" case (IGNBRK set, and IGNPAR 1863 * and INPCK clear). 1864 * 1865 * Note: BI together with FE/PE means just BI. 1866 */ 1867 if (line_status & LSR_BI) { 1868 #if defined(DDB) && defined(BREAK_TO_DEBUGGER) 1869 if (com->unit == comconsole) { 1870 com_unlock(); 1871 breakpoint(); 1872 com_lock(); 1873 goto cont; 1874 } 1875 #endif 1876 if (com->tp == NULL 1877 || com->tp->t_iflag & IGNBRK) 1878 goto cont; 1879 } else { 1880 if (com->tp == NULL 1881 || com->tp->t_iflag & IGNPAR) 1882 goto cont; 1883 } 1884 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 1885 && (line_status & (LSR_BI | LSR_FE) 1886 || com->tp->t_iflag & INPCK)) 1887 recv_data = 0; 1888 } 1889 ++com->bytes_in; 1890 if (com->hotchar != 0 && recv_data == com->hotchar) 1891 setsofttty(); 1892 ioptr = com->iptr; 1893 if (ioptr >= com->ibufend) 1894 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 1895 else { 1896 if (com->do_timestamp) 1897 microtime(&com->timestamp); 1898 ++com_events; 1899 schedsofttty(); 1900 #if 0 /* for testing input latency vs efficiency */ 1901 if (com->iptr - com->ibuf == 8) 1902 setsofttty(); 1903 #endif 1904 ioptr[0] = recv_data; 1905 ioptr[com->ierroff] = line_status; 1906 com->iptr = ++ioptr; 1907 if (ioptr == com->ihighwater 1908 && com->state & CS_RTS_IFLOW) 1909 outb(com->modem_ctl_port, 1910 com->mcr_image &= ~MCR_RTS); 1911 if (line_status & LSR_OE) 1912 CE_RECORD(com, CE_OVERRUN); 1913 } 1914 cont: 1915 /* 1916 * "& 0x7F" is to avoid the gcc-1.40 generating a slow 1917 * jump from the top of the loop to here 1918 */ 1919 line_status = inb(com->line_status_port) & 0x7F; 1920 } 1921 1922 /* modem status change? (always check before doing output) */ 1923 modem_status = inb(com->modem_status_port); 1924 if (modem_status != com->last_modem_status) { 1925 if (com->do_dcd_timestamp 1926 && !(com->last_modem_status & MSR_DCD) 1927 && modem_status & MSR_DCD) 1928 microtime(&com->dcd_timestamp); 1929 1930 /* 1931 * Schedule high level to handle DCD changes. Note 1932 * that we don't use the delta bits anywhere. Some 1933 * UARTs mess them up, and it's easy to remember the 1934 * previous bits and calculate the delta. 1935 */ 1936 com->last_modem_status = modem_status; 1937 if (!(com->state & CS_CHECKMSR)) { 1938 com_events += LOTS_OF_EVENTS; 1939 com->state |= CS_CHECKMSR; 1940 setsofttty(); 1941 } 1942 1943 /* handle CTS change immediately for crisp flow ctl */ 1944 if (com->state & CS_CTS_OFLOW) { 1945 if (modem_status & MSR_CTS) 1946 com->state |= CS_ODEVREADY; 1947 else 1948 com->state &= ~CS_ODEVREADY; 1949 } 1950 } 1951 1952 /* output queued and everything ready? */ 1953 if (line_status & LSR_TXRDY 1954 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 1955 ioptr = com->obufq.l_head; 1956 if (com->tx_fifo_size > 1) { 1957 u_int ocount; 1958 1959 ocount = com->obufq.l_tail - ioptr; 1960 if (ocount > com->tx_fifo_size) 1961 ocount = com->tx_fifo_size; 1962 com->bytes_out += ocount; 1963 do 1964 outb(com->data_port, *ioptr++); 1965 while (--ocount != 0); 1966 } else { 1967 outb(com->data_port, *ioptr++); 1968 ++com->bytes_out; 1969 } 1970 com->obufq.l_head = ioptr; 1971 if (COM_IIR_TXRDYBUG(com->flags)) { 1972 int_ctl_new = int_ctl | IER_ETXRDY; 1973 } 1974 if (ioptr >= com->obufq.l_tail) { 1975 struct lbq *qp; 1976 1977 qp = com->obufq.l_next; 1978 qp->l_queued = FALSE; 1979 qp = qp->l_next; 1980 if (qp != NULL) { 1981 com->obufq.l_head = qp->l_head; 1982 com->obufq.l_tail = qp->l_tail; 1983 com->obufq.l_next = qp; 1984 } else { 1985 /* output just completed */ 1986 if (COM_IIR_TXRDYBUG(com->flags)) { 1987 int_ctl_new = int_ctl & ~IER_ETXRDY; 1988 } 1989 com->state &= ~CS_BUSY; 1990 } 1991 if (!(com->state & CS_ODONE)) { 1992 com_events += LOTS_OF_EVENTS; 1993 com->state |= CS_ODONE; 1994 setsofttty(); /* handle at high level ASAP */ 1995 } 1996 } 1997 if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 1998 outb(com->intr_ctl_port, int_ctl_new); 1999 } 2000 } 2001 2002 /* finished? */ 2003 #ifndef COM_MULTIPORT 2004 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 2005 #endif /* COM_MULTIPORT */ 2006 { 2007 return; 2008 } 2009 } 2010 } 2011 2012 static int 2013 sioioctl(struct dev_ioctl_args *ap) 2014 { 2015 cdev_t dev = ap->a_head.a_dev; 2016 caddr_t data = ap->a_data; 2017 struct com_s *com; 2018 int error; 2019 int mynor; 2020 struct tty *tp; 2021 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2022 u_long oldcmd; 2023 struct termios term; 2024 #endif 2025 lwkt_gettoken(&tty_token); 2026 mynor = minor(dev); 2027 2028 com = com_addr(MINOR_TO_UNIT(mynor)); 2029 if (com == NULL || com->gone) { 2030 lwkt_reltoken(&tty_token); 2031 return (ENODEV); 2032 } 2033 if (mynor & CONTROL_MASK) { 2034 struct termios *ct; 2035 2036 switch (mynor & CONTROL_MASK) { 2037 case CONTROL_INIT_STATE: 2038 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 2039 break; 2040 case CONTROL_LOCK_STATE: 2041 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 2042 break; 2043 default: 2044 lwkt_reltoken(&tty_token); 2045 return (ENODEV); /* /dev/nodev */ 2046 } 2047 switch (ap->a_cmd) { 2048 case TIOCSETA: 2049 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 2050 if (error != 0) 2051 return (error); 2052 *ct = *(struct termios *)data; 2053 lwkt_reltoken(&tty_token); 2054 return (0); 2055 case TIOCGETA: 2056 *(struct termios *)data = *ct; 2057 lwkt_reltoken(&tty_token); 2058 return (0); 2059 case TIOCGETD: 2060 *(int *)data = TTYDISC; 2061 lwkt_reltoken(&tty_token); 2062 return (0); 2063 case TIOCGWINSZ: 2064 bzero(data, sizeof(struct winsize)); 2065 lwkt_reltoken(&tty_token); 2066 return (0); 2067 default: 2068 lwkt_reltoken(&tty_token); 2069 return (ENOTTY); 2070 } 2071 } 2072 tp = com->tp; 2073 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 2074 term = tp->t_termios; 2075 oldcmd = ap->a_cmd; 2076 error = ttsetcompat(tp, &ap->a_cmd, data, &term); 2077 if (error != 0) { 2078 lwkt_reltoken(&tty_token); 2079 return (error); 2080 } 2081 if (ap->a_cmd != oldcmd) 2082 data = (caddr_t)&term; 2083 #endif 2084 if (ap->a_cmd == TIOCSETA || ap->a_cmd == TIOCSETAW || 2085 ap->a_cmd == TIOCSETAF) { 2086 int cc; 2087 struct termios *dt = (struct termios *)data; 2088 struct termios *lt = mynor & CALLOUT_MASK 2089 ? &com->lt_out : &com->lt_in; 2090 2091 dt->c_iflag = (tp->t_iflag & lt->c_iflag) 2092 | (dt->c_iflag & ~lt->c_iflag); 2093 dt->c_oflag = (tp->t_oflag & lt->c_oflag) 2094 | (dt->c_oflag & ~lt->c_oflag); 2095 dt->c_cflag = (tp->t_cflag & lt->c_cflag) 2096 | (dt->c_cflag & ~lt->c_cflag); 2097 dt->c_lflag = (tp->t_lflag & lt->c_lflag) 2098 | (dt->c_lflag & ~lt->c_lflag); 2099 for (cc = 0; cc < NCCS; ++cc) 2100 if (lt->c_cc[cc] != 0) 2101 dt->c_cc[cc] = tp->t_cc[cc]; 2102 if (lt->c_ispeed != 0) 2103 dt->c_ispeed = tp->t_ispeed; 2104 if (lt->c_ospeed != 0) 2105 dt->c_ospeed = tp->t_ospeed; 2106 } 2107 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, data, ap->a_fflag, ap->a_cred); 2108 if (error != ENOIOCTL) { 2109 lwkt_reltoken(&tty_token); 2110 return (error); 2111 } 2112 crit_enter(); 2113 error = ttioctl(tp, ap->a_cmd, data, ap->a_fflag); 2114 disc_optim(tp, &tp->t_termios, com); 2115 if (error != ENOIOCTL) { 2116 crit_exit(); 2117 lwkt_reltoken(&tty_token); 2118 return (error); 2119 } 2120 switch (ap->a_cmd) { 2121 case TIOCSBRK: 2122 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 2123 break; 2124 case TIOCCBRK: 2125 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 2126 break; 2127 case TIOCSDTR: 2128 (void)commctl(com, TIOCM_DTR, DMBIS); 2129 break; 2130 case TIOCCDTR: 2131 (void)commctl(com, TIOCM_DTR, DMBIC); 2132 break; 2133 /* 2134 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 2135 * changes get undone on the next call to comparam(). 2136 */ 2137 case TIOCMSET: 2138 (void)commctl(com, *(int *)data, DMSET); 2139 break; 2140 case TIOCMBIS: 2141 (void)commctl(com, *(int *)data, DMBIS); 2142 break; 2143 case TIOCMBIC: 2144 (void)commctl(com, *(int *)data, DMBIC); 2145 break; 2146 case TIOCMGET: 2147 *(int *)data = commctl(com, 0, DMGET); 2148 break; 2149 case TIOCMSDTRWAIT: 2150 /* must be root since the wait applies to following logins */ 2151 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); 2152 if (error != 0) { 2153 crit_exit(); 2154 lwkt_reltoken(&tty_token); 2155 return (error); 2156 } 2157 com->dtr_wait = *(int *)data * hz / 100; 2158 break; 2159 case TIOCMGDTRWAIT: 2160 *(int *)data = com->dtr_wait * 100 / hz; 2161 break; 2162 case TIOCTIMESTAMP: 2163 com->do_timestamp = TRUE; 2164 *(struct timeval *)data = com->timestamp; 2165 break; 2166 case TIOCDCDTIMESTAMP: 2167 com->do_dcd_timestamp = TRUE; 2168 *(struct timeval *)data = com->dcd_timestamp; 2169 break; 2170 default: 2171 crit_exit(); 2172 error = pps_ioctl(ap->a_cmd, data, &com->pps); 2173 if (error == ENODEV) 2174 error = ENOTTY; 2175 lwkt_reltoken(&tty_token); 2176 return (error); 2177 } 2178 crit_exit(); 2179 lwkt_reltoken(&tty_token); 2180 return (0); 2181 } 2182 2183 static void 2184 siopoll(void *dummy, void *frame) 2185 { 2186 int unit; 2187 2188 lwkt_gettoken(&tty_token); 2189 if (com_events == 0) { 2190 lwkt_reltoken(&tty_token); 2191 return; 2192 } 2193 2194 repeat: 2195 for (unit = 0; unit < sio_numunits; ++unit) { 2196 struct com_s *com; 2197 int incc; 2198 struct tty *tp; 2199 2200 com = com_addr(unit); 2201 if (com == NULL) 2202 continue; 2203 tp = com->tp; 2204 if (tp == NULL || com->gone) { 2205 /* 2206 * Discard any events related to never-opened or 2207 * going-away devices. 2208 */ 2209 com_lock(); 2210 incc = com->iptr - com->ibuf; 2211 com->iptr = com->ibuf; 2212 if (com->state & CS_CHECKMSR) { 2213 incc += LOTS_OF_EVENTS; 2214 com->state &= ~CS_CHECKMSR; 2215 } 2216 com_events -= incc; 2217 com_unlock(); 2218 continue; 2219 } 2220 if (com->iptr != com->ibuf) { 2221 com_lock(); 2222 sioinput(com); 2223 com_unlock(); 2224 } 2225 if (com->state & CS_CHECKMSR) { 2226 u_char delta_modem_status; 2227 2228 com_lock(); 2229 delta_modem_status = com->last_modem_status 2230 ^ com->prev_modem_status; 2231 com->prev_modem_status = com->last_modem_status; 2232 com_events -= LOTS_OF_EVENTS; 2233 com->state &= ~CS_CHECKMSR; 2234 com_unlock(); 2235 if (delta_modem_status & MSR_DCD) 2236 (*linesw[tp->t_line].l_modem) 2237 (tp, com->prev_modem_status & MSR_DCD); 2238 } 2239 if (com->state & CS_ODONE) { 2240 com_lock(); 2241 com_events -= LOTS_OF_EVENTS; 2242 com->state &= ~CS_ODONE; 2243 com_unlock(); 2244 if (!(com->state & CS_BUSY) 2245 && !(com->extra_state & CSE_BUSYCHECK)) { 2246 callout_reset(&com->busy_ch, hz / 100, 2247 siobusycheck, com); 2248 com->extra_state |= CSE_BUSYCHECK; 2249 } 2250 (*linesw[tp->t_line].l_start)(tp); 2251 } 2252 if (com_events == 0) 2253 break; 2254 } 2255 if (com_events >= LOTS_OF_EVENTS) 2256 goto repeat; 2257 lwkt_reltoken(&tty_token); 2258 } 2259 2260 /* 2261 * Called with tty_token held but no com_lock 2262 */ 2263 static int 2264 comparam(struct tty *tp, struct termios *t) 2265 { 2266 u_int cfcr; 2267 int cflag; 2268 struct com_s *com; 2269 u_int divisor; 2270 u_char dlbh; 2271 u_char dlbl; 2272 int unit; 2273 2274 unit = DEV_TO_UNIT(tp->t_dev); 2275 com = com_addr(unit); 2276 if (com == NULL) { 2277 return (ENODEV); 2278 } 2279 2280 /* do historical conversions */ 2281 if (t->c_ispeed == 0) 2282 t->c_ispeed = t->c_ospeed; 2283 2284 /* check requested parameters */ 2285 if (t->c_ospeed == 0) 2286 divisor = 0; 2287 else { 2288 if (t->c_ispeed != t->c_ospeed) 2289 return (EINVAL); 2290 divisor = siodivisor(com->rclk, t->c_ispeed); 2291 if (divisor == 0) 2292 return (EINVAL); 2293 } 2294 2295 /* parameters are OK, convert them to the com struct and the device */ 2296 crit_enter(); 2297 if (divisor == 0) 2298 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 2299 else 2300 (void)commctl(com, TIOCM_DTR, DMBIS); 2301 cflag = t->c_cflag; 2302 switch (cflag & CSIZE) { 2303 case CS5: 2304 cfcr = CFCR_5BITS; 2305 break; 2306 case CS6: 2307 cfcr = CFCR_6BITS; 2308 break; 2309 case CS7: 2310 cfcr = CFCR_7BITS; 2311 break; 2312 default: 2313 cfcr = CFCR_8BITS; 2314 break; 2315 } 2316 if (cflag & PARENB) { 2317 cfcr |= CFCR_PENAB; 2318 if (!(cflag & PARODD)) 2319 cfcr |= CFCR_PEVEN; 2320 } 2321 if (cflag & CSTOPB) 2322 cfcr |= CFCR_STOPB; 2323 2324 if (com->hasfifo && divisor != 0) { 2325 /* 2326 * Use a fifo trigger level low enough so that the input 2327 * latency from the fifo is less than about 16 msec and 2328 * the total latency is less than about 30 msec. These 2329 * latencies are reasonable for humans. Serial comms 2330 * protocols shouldn't expect anything better since modem 2331 * latencies are larger. 2332 * 2333 * Interrupts can be held up for long periods of time 2334 * due to inefficiencies in other parts of the kernel, 2335 * certain video cards, etc. Setting the FIFO trigger 2336 * point to MEDH instead of HIGH gives us 694uS of slop 2337 * (8 character times) instead of 173uS (2 character times) 2338 * @ 115200 bps. 2339 */ 2340 com->fifo_image = t->c_ospeed <= 4800 2341 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH; 2342 #ifdef COM_ESP 2343 /* 2344 * The Hayes ESP card needs the fifo DMA mode bit set 2345 * in compatibility mode. If not, it will interrupt 2346 * for each character received. 2347 */ 2348 if (com->esp) 2349 com->fifo_image |= FIFO_DMA_MODE; 2350 #endif 2351 sio_setreg(com, com_fifo, com->fifo_image); 2352 } 2353 2354 /* 2355 * This returns with interrupts disabled so that we can complete 2356 * the speed change atomically. Keeping interrupts disabled is 2357 * especially important while com_data is hidden. 2358 */ 2359 (void) siosetwater(com, t->c_ispeed); 2360 2361 if (divisor != 0) { 2362 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 2363 /* 2364 * Only set the divisor registers if they would change, 2365 * since on some 16550 incompatibles (UMC8669F), setting 2366 * them while input is arriving them loses sync until 2367 * data stops arriving. 2368 */ 2369 dlbl = divisor & 0xFF; 2370 if (sio_getreg(com, com_dlbl) != dlbl) 2371 sio_setreg(com, com_dlbl, dlbl); 2372 dlbh = divisor >> 8; 2373 if (sio_getreg(com, com_dlbh) != dlbh) 2374 sio_setreg(com, com_dlbh, dlbh); 2375 } 2376 2377 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 2378 2379 if (!(tp->t_state & TS_TTSTOP)) 2380 com->state |= CS_TTGO; 2381 2382 if (cflag & CRTS_IFLOW) { 2383 if (com->st16650a) { 2384 sio_setreg(com, com_cfcr, 0xbf); 2385 sio_setreg(com, com_fifo, 2386 sio_getreg(com, com_fifo) | 0x40); 2387 } 2388 com->state |= CS_RTS_IFLOW; 2389 /* 2390 * If CS_RTS_IFLOW just changed from off to on, the change 2391 * needs to be propagated to MCR_RTS. This isn't urgent, 2392 * so do it later by calling comstart() instead of repeating 2393 * a lot of code from comstart() here. 2394 */ 2395 } else if (com->state & CS_RTS_IFLOW) { 2396 com->state &= ~CS_RTS_IFLOW; 2397 /* 2398 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 2399 * on here, since comstart() won't do it later. 2400 */ 2401 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2402 if (com->st16650a) { 2403 sio_setreg(com, com_cfcr, 0xbf); 2404 sio_setreg(com, com_fifo, 2405 sio_getreg(com, com_fifo) & ~0x40); 2406 } 2407 } 2408 2409 2410 /* 2411 * Set up state to handle output flow control. 2412 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 2413 * Now has 10+ msec latency, while CTS flow has 50- usec latency. 2414 */ 2415 com->state |= CS_ODEVREADY; 2416 com->state &= ~CS_CTS_OFLOW; 2417 if (cflag & CCTS_OFLOW) { 2418 com->state |= CS_CTS_OFLOW; 2419 if (!(com->last_modem_status & MSR_CTS)) 2420 com->state &= ~CS_ODEVREADY; 2421 if (com->st16650a) { 2422 sio_setreg(com, com_cfcr, 0xbf); 2423 sio_setreg(com, com_fifo, 2424 sio_getreg(com, com_fifo) | 0x80); 2425 } 2426 } else { 2427 if (com->st16650a) { 2428 sio_setreg(com, com_cfcr, 0xbf); 2429 sio_setreg(com, com_fifo, 2430 sio_getreg(com, com_fifo) & ~0x80); 2431 } 2432 } 2433 2434 sio_setreg(com, com_cfcr, com->cfcr_image); 2435 2436 /* XXX shouldn't call functions while intrs are disabled. */ 2437 disc_optim(tp, t, com); 2438 /* 2439 * Recover from fiddling with CS_TTGO. We used to call siointr1() 2440 * unconditionally, but that defeated the careful discarding of 2441 * stale input in sioopen(). 2442 */ 2443 if (com->state >= (CS_BUSY | CS_TTGO)) { 2444 com_lock(); 2445 siointr1(com); 2446 com_unlock(); 2447 } 2448 crit_exit(); 2449 comstart(tp); 2450 if (com->ibufold != NULL) { 2451 kfree(com->ibufold, M_DEVBUF); 2452 com->ibufold = NULL; 2453 } 2454 return (0); 2455 } 2456 2457 /* 2458 * called with tty_token held 2459 */ 2460 static int 2461 siosetwater(struct com_s *com, speed_t speed) 2462 { 2463 int cp4ticks; 2464 u_char *ibuf; 2465 int ibufsize; 2466 struct tty *tp; 2467 2468 /* 2469 * Make the buffer size large enough to handle a softtty interrupt 2470 * latency of about 2 ticks without loss of throughput or data 2471 * (about 3 ticks if input flow control is not used or not honoured, 2472 * but a bit less for CS5-CS7 modes). 2473 */ 2474 cp4ticks = speed / 10 / hz * 4; 2475 for (ibufsize = 128; ibufsize < cp4ticks;) 2476 ibufsize <<= 1; 2477 if (ibufsize == com->ibufsize) 2478 return (0); 2479 2480 /* 2481 * Allocate input buffer. The extra factor of 2 in the size is 2482 * to allow for an error byte for each input byte. 2483 */ 2484 ibuf = kmalloc(2 * ibufsize, M_DEVBUF, M_WAITOK | M_ZERO); 2485 2486 /* Initialize non-critical variables. */ 2487 com->ibufold = com->ibuf; 2488 com->ibufsize = ibufsize; 2489 tp = com->tp; 2490 if (tp != NULL) { 2491 tp->t_ififosize = 2 * ibufsize; 2492 tp->t_ispeedwat = (speed_t)-1; 2493 tp->t_ospeedwat = (speed_t)-1; 2494 } 2495 2496 /* 2497 * Read current input buffer. 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 com_unlock(); 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_reltoken(&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 static void 2871 siocntxwait(Port_t iobase) 2872 { 2873 int timo; 2874 2875 /* 2876 * Wait for any pending transmission to finish. Required to avoid 2877 * the UART lockup bug when the speed is changed, and for normal 2878 * transmits. 2879 */ 2880 timo = 100000; 2881 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 2882 != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 2883 ; 2884 } 2885 2886 /* 2887 * Read the serial port specified and try to figure out what speed 2888 * it's currently running at. We're assuming the serial port has 2889 * been initialized and is basicly idle. This routine is only intended 2890 * to be run at system startup. 2891 * 2892 * If the value read from the serial port doesn't make sense, return 0. 2893 */ 2894 /* 2895 * NOTE: Must be called with tty_token held 2896 */ 2897 static speed_t 2898 siocngetspeed(Port_t iobase, u_long rclk) 2899 { 2900 u_int divisor; 2901 u_char dlbh; 2902 u_char dlbl; 2903 u_char cfcr; 2904 2905 cfcr = inb(iobase + com_cfcr); 2906 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 2907 2908 dlbl = inb(iobase + com_dlbl); 2909 dlbh = inb(iobase + com_dlbh); 2910 2911 outb(iobase + com_cfcr, cfcr); 2912 2913 divisor = dlbh << 8 | dlbl; 2914 2915 /* XXX there should be more sanity checking. */ 2916 if (divisor == 0) 2917 return (CONSPEED); 2918 return (rclk / (16UL * divisor)); 2919 } 2920 2921 static void 2922 siocnopen(struct siocnstate *sp, Port_t iobase, int speed) 2923 { 2924 u_int divisor; 2925 u_char dlbh; 2926 u_char dlbl; 2927 2928 /* 2929 * Save all the device control registers except the fifo register 2930 * and set our default ones (cs8 -parenb speed=comdefaultrate). 2931 * We can't save the fifo register since it is read-only. 2932 */ 2933 sp->ier = inb(iobase + com_ier); 2934 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 2935 siocntxwait(iobase); 2936 sp->cfcr = inb(iobase + com_cfcr); 2937 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2938 sp->dlbl = inb(iobase + com_dlbl); 2939 sp->dlbh = inb(iobase + com_dlbh); 2940 /* 2941 * Only set the divisor registers if they would change, since on 2942 * some 16550 incompatibles (Startech), setting them clears the 2943 * data input register. This also reduces the effects of the 2944 * UMC8669F bug. 2945 */ 2946 divisor = siodivisor(comdefaultrclk, speed); 2947 dlbl = divisor & 0xFF; 2948 if (sp->dlbl != dlbl) 2949 outb(iobase + com_dlbl, dlbl); 2950 dlbh = divisor >> 8; 2951 if (sp->dlbh != dlbh) 2952 outb(iobase + com_dlbh, dlbh); 2953 outb(iobase + com_cfcr, CFCR_8BITS); 2954 sp->mcr = inb(iobase + com_mcr); 2955 /* 2956 * We don't want interrupts, but must be careful not to "disable" 2957 * them by clearing the MCR_IENABLE bit, since that might cause 2958 * an interrupt by floating the IRQ line. 2959 */ 2960 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 2961 } 2962 2963 static void 2964 siocnclose(struct siocnstate *sp, Port_t iobase) 2965 { 2966 /* 2967 * Restore the device control registers. 2968 */ 2969 siocntxwait(iobase); 2970 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 2971 if (sp->dlbl != inb(iobase + com_dlbl)) 2972 outb(iobase + com_dlbl, sp->dlbl); 2973 if (sp->dlbh != inb(iobase + com_dlbh)) 2974 outb(iobase + com_dlbh, sp->dlbh); 2975 outb(iobase + com_cfcr, sp->cfcr); 2976 /* 2977 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 2978 */ 2979 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 2980 outb(iobase + com_ier, sp->ier); 2981 } 2982 2983 static void 2984 siocnprobe(struct consdev *cp) 2985 { 2986 speed_t boot_speed; 2987 u_char cfcr; 2988 u_int divisor; 2989 int unit; 2990 struct siocnstate sp; 2991 2992 /* 2993 * Find our first enabled console, if any. If it is a high-level 2994 * console device, then initialize it and return successfully. 2995 * If it is a low-level console device, then initialize it and 2996 * return unsuccessfully. It must be initialized in both cases 2997 * for early use by console drivers and debuggers. Initializing 2998 * the hardware is not necessary in all cases, since the i/o 2999 * routines initialize it on the fly, but it is necessary if 3000 * input might arrive while the hardware is switched back to an 3001 * uninitialized state. We can't handle multiple console devices 3002 * yet because our low-level routines don't take a device arg. 3003 * We trust the user to set the console flags properly so that we 3004 * don't need to probe. 3005 */ 3006 cp->cn_pri = CN_DEAD; 3007 3008 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 3009 int flags; 3010 int disabled; 3011 if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 3012 if (disabled) 3013 continue; 3014 } 3015 if (resource_int_value("sio", unit, "flags", &flags)) 3016 continue; 3017 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 3018 int port; 3019 Port_t iobase; 3020 3021 if (resource_int_value("sio", unit, "port", &port)) 3022 continue; 3023 iobase = port; 3024 crit_enter(); 3025 if (boothowto & RB_SERIAL) { 3026 boot_speed = 3027 siocngetspeed(iobase, comdefaultrclk); 3028 if (boot_speed) 3029 comdefaultrate = boot_speed; 3030 } 3031 3032 /* 3033 * Initialize the divisor latch. We can't rely on 3034 * siocnopen() to do this the first time, since it 3035 * avoids writing to the latch if the latch appears 3036 * to have the correct value. Also, if we didn't 3037 * just read the speed from the hardware, then we 3038 * need to set the speed in hardware so that 3039 * switching it later is null. 3040 */ 3041 com_lock(); 3042 cfcr = inb(iobase + com_cfcr); 3043 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 3044 divisor = siodivisor(comdefaultrclk, comdefaultrate); 3045 outb(iobase + com_dlbl, divisor & 0xff); 3046 outb(iobase + com_dlbh, divisor >> 8); 3047 outb(iobase + com_cfcr, cfcr); 3048 3049 siocnopen(&sp, iobase, comdefaultrate); 3050 com_unlock(); 3051 3052 crit_exit(); 3053 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 3054 cp->cn_probegood = 1; 3055 cp->cn_private = (void *)(intptr_t)unit; 3056 cp->cn_pri = COM_FORCECONSOLE(flags) 3057 || boothowto & RB_SERIAL 3058 ? CN_REMOTE : CN_NORMAL; 3059 siocniobase = iobase; 3060 siocnunit = unit; 3061 } 3062 if (COM_DEBUGGER(flags) && gdb_tab == NULL) { 3063 kprintf("sio%d: gdb debugging port\n", unit); 3064 siogdbiobase = iobase; 3065 siogdbunit = unit; 3066 #if DDB > 0 3067 cp->cn_gdbprivate = (void *)(intptr_t)unit; 3068 gdb_tab = cp; 3069 #endif 3070 } 3071 } 3072 } 3073 #if defined(__i386__) || defined(__x86_64__) 3074 #if DDB > 0 3075 /* 3076 * XXX Ugly Compatability. 3077 * If no gdb port has been specified, set it to be the console 3078 * as some configuration files don't specify the gdb port. 3079 */ 3080 if (gdb_tab == NULL && (boothowto & RB_GDB)) { 3081 kprintf("Warning: no GDB port specified. Defaulting to sio%d.\n", 3082 siocnunit); 3083 kprintf("Set flag 0x80 on desired GDB port in your\n"); 3084 kprintf("configuration file (currently sio only).\n"); 3085 siogdbiobase = siocniobase; 3086 siogdbunit = siocnunit; 3087 cp->cn_gdbprivate = (void *)(intptr_t)siocnunit; 3088 gdb_tab = cp; 3089 } 3090 #endif 3091 #endif 3092 } 3093 3094 static void 3095 siocninit(struct consdev *cp) 3096 { 3097 comconsole = (int)(intptr_t)cp->cn_private; 3098 } 3099 3100 static void 3101 siocninit_fini(struct consdev *cp) 3102 { 3103 cdev_t dev; 3104 int unit; 3105 3106 if (cp->cn_probegood) { 3107 unit = (int)(intptr_t)cp->cn_private; 3108 /* 3109 * Call devfs_find_device_by_name on ttydX to find the correct device, 3110 * as it should have been created already at this point by the 3111 * attach routine. 3112 * If it isn't found, the serial port was not attached at all and we 3113 * shouldn't be here, so assert this case. 3114 */ 3115 dev = devfs_find_device_by_name("ttyd%r", unit); 3116 3117 KKASSERT(dev != NULL); 3118 cp->cn_dev = dev; 3119 } 3120 } 3121 3122 static int 3123 siocncheckc(void *private) 3124 { 3125 int c; 3126 int unit = (int)(intptr_t)private; 3127 Port_t iobase; 3128 struct siocnstate sp; 3129 3130 if (unit == siogdbunit) 3131 iobase = siogdbiobase; 3132 else 3133 iobase = siocniobase; 3134 com_lock(); 3135 crit_enter(); 3136 siocnopen(&sp, iobase, comdefaultrate); 3137 if (inb(iobase + com_lsr) & LSR_RXRDY) 3138 c = inb(iobase + com_data); 3139 else 3140 c = -1; 3141 siocnclose(&sp, iobase); 3142 crit_exit(); 3143 com_unlock(); 3144 return (c); 3145 } 3146 3147 3148 int 3149 siocngetc(void *private) 3150 { 3151 int c; 3152 int unit = (int)(intptr_t)private; 3153 Port_t iobase; 3154 struct siocnstate sp; 3155 3156 if (unit == siogdbunit) 3157 iobase = siogdbiobase; 3158 else 3159 iobase = siocniobase; 3160 com_lock(); 3161 crit_enter(); 3162 siocnopen(&sp, iobase, comdefaultrate); 3163 while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 3164 ; 3165 c = inb(iobase + com_data); 3166 siocnclose(&sp, iobase); 3167 crit_exit(); 3168 com_unlock(); 3169 return (c); 3170 } 3171 3172 void 3173 siocnputc(void *private, int c) 3174 { 3175 int unit = (int)(intptr_t)private; 3176 struct siocnstate sp; 3177 Port_t iobase; 3178 3179 if (unit == siogdbunit) 3180 iobase = siogdbiobase; 3181 else 3182 iobase = siocniobase; 3183 com_lock(); 3184 crit_enter(); 3185 siocnopen(&sp, iobase, comdefaultrate); 3186 siocntxwait(iobase); 3187 outb(iobase + com_data, c); 3188 siocnclose(&sp, iobase); 3189 crit_exit(); 3190 com_unlock(); 3191 } 3192 3193 DRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 3194 DRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, 0, 0); 3195 #if NPCI > 0 3196 DRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 3197 #endif 3198 #if NPUC > 0 3199 DRIVER_MODULE(sio, puc, sio_puc_driver, sio_devclass, 0, 0); 3200 #endif 3201