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