2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__)
32 #include <sys/ioctl.h>
51 #include "throughput.h"
57 #include "descriptor.h"
68 #if defined(__mac68k__) || defined(__macppc__)
71 #define CRTS_IFLOW CDTRCTS
72 #define CCTS_OFLOW CDTRCTS
75 #define Online(dev) ((dev)->mbits & TIOCM_CD)
78 struct device dev; /* What struct physical knows about */
79 struct pppTimer Timer; /* CD checks */
80 int mbits; /* Current DCD status */
81 int carrier_seconds; /* seconds before CD is *required* */
82 struct termios ios; /* To be able to reset from raw mode */
85 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
90 return sizeof(struct ttydevice);
94 * tty_Timeout() watches the DCD signal and mentions it if it's status
98 tty_Timeout(void *data)
100 struct physical *p = data;
101 struct ttydevice *dev = device2tty(p->handler);
104 timer_Stop(&dev->Timer);
105 dev->Timer.load = SECTICKS; /* Once a second please */
106 timer_Start(&dev->Timer);
110 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
111 /* we must be a pty ? */
112 if (p->cfg.cd.necessity != CD_DEFAULT)
113 log_Printf(LogWARN, "%s: Carrier ioctl not supported, "
114 "using ``set cd off''\n", p->link.name);
115 timer_Stop(&dev->Timer);
116 dev->mbits = TIOCM_CD;
123 /* First time looking for carrier */
125 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
126 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
127 if (dev->dev.cd.necessity == CD_REQUIRED)
128 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
129 p->link.name, p->name.full);
131 log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
132 p->link.name, p->name.full);
133 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */
135 timer_Stop(&dev->Timer);
136 /* tty_AwaitCarrier() will notice */
139 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n",
140 p->link.name, p->name.full, dev->carrier_seconds,
145 change = ombits ^ dev->mbits;
146 if (change & TIOCM_CD) {
147 if (dev->mbits & TIOCM_CD)
148 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name);
150 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name);
151 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name);
152 datalink_Down(p->dl, CLOSE_NORMAL);
153 timer_Stop(&dev->Timer);
156 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
157 Online(dev) ? "on" : "off");
162 tty_StartTimer(struct physical *p)
164 struct ttydevice *dev = device2tty(p->handler);
166 timer_Stop(&dev->Timer);
167 dev->Timer.load = SECTICKS;
168 dev->Timer.func = tty_Timeout;
169 dev->Timer.name = "tty CD";
171 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
172 p->link.name, tty_Timeout);
173 timer_Start(&dev->Timer);
177 tty_AwaitCarrier(struct physical *p)
179 struct ttydevice *dev = device2tty(p->handler);
181 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p))
184 if (dev->mbits == -1) {
185 if (dev->Timer.state == TIMER_STOPPED) {
186 dev->carrier_seconds = 0;
189 return CARRIER_PENDING; /* Not yet ! */
192 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
196 tty_Raw(struct physical *p)
198 struct ttydevice *dev = device2tty(p->handler);
202 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name);
204 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
205 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
206 p->link.name, p->fd, dev->mbits);
208 if (!physical_IsSync(p)) {
209 tcgetattr(p->fd, &ios);
212 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
214 ios.c_cflag |= CLOCAL;
216 if (p->type != PHYS_DEDICATED)
217 ios.c_cflag |= HUPCL;
219 tcsetattr(p->fd, TCSANOW, &ios);
222 oldflag = fcntl(p->fd, F_GETFL, 0);
225 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
231 tty_Offline(struct physical *p)
233 struct ttydevice *dev = device2tty(p->handler);
236 timer_Stop(&dev->Timer);
237 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */
241 tcgetattr(p->fd, &tio);
242 if (cfsetspeed(&tio, B0) == -1)
243 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n",
246 tcsetattr(p->fd, TCSANOW, &tio);
252 tty_Cooked(struct physical *p)
254 struct ttydevice *dev = device2tty(p->handler);
257 tty_Offline(p); /* In case of emergency close()s */
259 tcflush(p->fd, TCIOFLUSH);
261 if (!physical_IsSync(p))
262 tcsetattr(p->fd, TCSAFLUSH, &dev->ios);
264 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
265 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
269 tty_StopTimer(struct physical *p)
271 struct ttydevice *dev = device2tty(p->handler);
273 timer_Stop(&dev->Timer);
277 tty_Free(struct physical *p)
279 struct ttydevice *dev = device2tty(p->handler);
281 tty_Offline(p); /* In case of emergency close()s */
286 tty_Speed(struct physical *p)
290 if (tcgetattr(p->fd, &ios) == -1)
293 return SpeedToInt(cfgetispeed(&ios));
297 tty_OpenInfo(struct physical *p)
299 struct ttydevice *dev = device2tty(p->handler);
306 strcat(buf, " carrier");
312 tty_device2iov(struct device *d, struct iovec *iov, int *niov,
313 int maxiov, int *auxfd, int *nauxfd)
315 struct ttydevice *dev = device2tty(d);
316 int sz = physical_MaxDeviceSize();
318 iov[*niov].iov_base = realloc(d, sz);
319 if (iov[*niov].iov_base == NULL) {
320 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
321 AbortProgram(EX_OSERR);
323 iov[*niov].iov_len = sz;
326 if (dev->Timer.state != TIMER_STOPPED) {
327 timer_Stop(&dev->Timer);
328 dev->Timer.state = TIMER_RUNNING;
332 static struct device basettydevice = {
335 { CD_VARIABLE, DEF_TTYCDDELAY },
351 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
352 int maxiov, int *auxfd, int *nauxfd)
354 if (type == TTY_DEVICE) {
355 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base;
357 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
359 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
361 AbortProgram(EX_OSERR);
364 /* Refresh function pointers etc */
365 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
367 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
368 if (dev->Timer.state != TIMER_STOPPED) {
369 dev->Timer.state = TIMER_STOPPED;
370 p->handler = &dev->dev; /* For the benefit of StartTimer */
380 tty_Create(struct physical *p)
382 struct ttydevice *dev;
386 if (p->fd < 0 || !isatty(p->fd))
387 /* Don't want this */
390 if (*p->name.full == '\0') {
391 physical_SetDevice(p, ttyname(p->fd));
392 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n",
393 p->link.name, p->name.full);
395 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
397 /* We're gonna return a ttydevice (unless something goes horribly wrong) */
399 if ((dev = malloc(sizeof *dev)) == NULL) {
400 /* Complete failure - parent doesn't continue trying to ``create'' */
406 memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
407 memset(&dev->Timer, '\0', sizeof dev->Timer);
409 tcgetattr(p->fd, &ios);
412 if (p->cfg.cd.necessity != CD_DEFAULT)
413 /* Any override is ok for the tty device */
414 dev->dev.cd = p->cfg.cd;
416 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d,"
417 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
418 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
422 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
424 ios.c_cflag |= CLOCAL;
425 ios.c_iflag |= IXOFF;
428 if (p->type != PHYS_DEDICATED)
429 ios.c_cflag |= HUPCL;
431 if (p->type != PHYS_DIRECT) {
432 /* Change tty speed when we're not in -direct mode */
433 ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
434 ios.c_cflag |= p->cfg.parity;
435 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
436 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
437 p->link.name, p->name.full, p->cfg.speed);
439 tcsetattr(p->fd, TCSADRAIN, &ios);
440 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
441 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
442 (u_long)ios.c_oflag, (u_long)ios.c_cflag);
444 oldflag = fcntl(p->fd, F_GETFL, 0);
446 /* Complete failure - parent doesn't continue trying to ``create'' */
448 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
449 p->link.name, strerror(errno));
456 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
458 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);