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