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>
32 #if defined(__OpenBSD__) || defined(__NetBSD__)
33 #include <sys/ioctl.h>
40 #include <machine/i4b_ioctl.h>
41 #include <machine/i4b_rbch_ioctl.h>
44 #include <netisdn/i4b_ioctl.h>
45 #include <netisdn/i4b_rbch_ioctl.h>
47 #include <i4b/i4b_ioctl.h>
48 #include <i4b/i4b_rbch_ioctl.h>
66 #include "throughput.h"
72 #include "descriptor.h"
83 #define Online(dev) ((dev)->mbits & TIOCM_CD)
86 struct device dev; /* What struct physical knows about */
87 struct pppTimer Timer; /* CD checks */
88 int mbits; /* Current DCD status */
89 int carrier_seconds; /* seconds before CD is *required* */
92 #define device2i4b(d) ((d)->type == I4B_DEVICE ? (struct i4bdevice *)d : NULL)
97 return sizeof(struct i4bdevice);
101 * i4b_Timeout() watches the DCD signal and mentions it if it's status
105 i4b_Timeout(void *data)
107 struct physical *p = data;
108 struct i4bdevice *dev = device2i4b(p->handler);
111 timer_Stop(&dev->Timer);
112 dev->Timer.load = SECTICKS; /* Once a second please */
113 timer_Start(&dev->Timer);
117 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
118 log_Printf(LogPHASE, "%s: ioctl error (%s)!\n", p->link.name,
120 datalink_Down(p->dl, CLOSE_NORMAL);
121 timer_Stop(&dev->Timer);
128 /* First time looking for carrier */
130 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full);
131 else if (++dev->carrier_seconds >= dev->dev.cd.delay) {
132 log_Printf(LogPHASE, "%s: %s: No carrier"
133 " (increase ``set cd'' from %d ?)\n",
134 p->link.name, p->name.full, dev->dev.cd.delay);
135 timer_Stop(&dev->Timer);
136 /* i4b_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 i4b_StartTimer(struct physical *p)
164 struct i4bdevice *dev = device2i4b(p->handler);
166 timer_Stop(&dev->Timer);
167 dev->Timer.load = SECTICKS;
168 dev->Timer.func = i4b_Timeout;
169 dev->Timer.name = "i4b CD";
171 log_Printf(LogDEBUG, "%s: Using i4b_Timeout [%p]\n",
172 p->link.name, i4b_Timeout);
173 timer_Start(&dev->Timer);
177 i4b_AwaitCarrier(struct physical *p)
179 struct i4bdevice *dev = device2i4b(p->handler);
181 if (dev->mbits == -1) {
182 if (dev->Timer.state == TIMER_STOPPED) {
183 dev->carrier_seconds = 0;
186 return CARRIER_PENDING; /* Not yet ! */
189 return Online(dev) ? CARRIER_OK : CARRIER_LOST;
193 i4b_Raw(struct physical *p)
197 log_Printf(LogDEBUG, "%s: Entering i4b_Raw\n", p->link.name);
199 oldflag = fcntl(p->fd, F_GETFL, 0);
202 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
208 i4b_Offline(struct physical *p)
210 struct i4bdevice *dev = device2i4b(p->handler);
213 timer_Stop(&dev->Timer);
218 ioctl(p->fd, TIOCCDTR, &dummy);
224 i4b_Cooked(struct physical *p)
228 i4b_Offline(p); /* In case of emergency close()s */
230 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1)
231 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
235 i4b_StopTimer(struct physical *p)
237 struct i4bdevice *dev = device2i4b(p->handler);
239 timer_Stop(&dev->Timer);
243 i4b_Free(struct physical *p)
245 struct i4bdevice *dev = device2i4b(p->handler);
247 i4b_Offline(p); /* In case of emergency close()s */
252 i4b_Speed(struct physical *p)
257 if (tcgetattr(p->fd, &ios) == -1 ||
258 (ret = SpeedToUnsigned(cfgetispeed(&ios))) == 0)
265 i4b_OpenInfo(struct physical *p)
267 struct i4bdevice *dev = device2i4b(p->handler);
271 snprintf(buf, sizeof buf, "carrier took %ds", dev->carrier_seconds);
279 i4b_Slot(struct physical *p)
283 if (fstat(p->fd, &st) == 0)
284 return minor(st.st_rdev);
290 i4b_device2iov(struct device *d, struct iovec *iov, int *niov,
291 int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
293 struct i4bdevice *dev = device2i4b(d);
294 int sz = physical_MaxDeviceSize();
296 iov[*niov].iov_base = realloc(d, sz);
297 if (iov[*niov].iov_base == NULL) {
298 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
299 AbortProgram(EX_OSERR);
301 iov[*niov].iov_len = sz;
304 if (dev->Timer.state != TIMER_STOPPED) {
305 timer_Stop(&dev->Timer);
306 dev->Timer.state = TIMER_RUNNING;
310 static struct device basei4bdevice = {
314 { CD_REQUIRED, DEF_I4BCDDELAY },
332 i4b_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
333 int maxiov __unused, int *auxfd __unused, int *nauxfd __unused)
335 if (type == I4B_DEVICE) {
336 struct i4bdevice *dev = (struct i4bdevice *)iov[(*niov)++].iov_base;
338 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
340 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
342 AbortProgram(EX_OSERR);
345 /* Refresh function pointers etc */
346 memcpy(&dev->dev, &basei4bdevice, sizeof dev->dev);
348 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
349 if (dev->Timer.state != TIMER_STOPPED) {
350 dev->Timer.state = TIMER_STOPPED;
351 p->handler = &dev->dev; /* For the benefit of StartTimer */
361 i4b_Create(struct physical *p)
363 struct i4bdevice *dev;
368 if (p->fd < 0 || ioctl(p->fd, I4B_RBCH_VR_REQ, &req))
369 /* Don't want this */
373 * We don't bother validating the version.... all versions of i4b that
374 * support I4B_RBCH_VR_REQ are fair game :-)
377 if (*p->name.full == '\0') {
378 physical_SetDevice(p, ttyname(p->fd));
379 log_Printf(LogDEBUG, "%s: Input is an i4b version %d.%d.%d isdn "
380 "device (%s)\n", p->link.name, req.version, req.release,
381 req.step, p->name.full);
384 log_Printf(LogDEBUG, "%s: Opened %s (i4b version %d.%d.%d)\n",
385 p->link.name, p->name.full, req.version, req.release, req.step);
389 /* We're gonna return an i4bdevice (unless something goes horribly wrong) */
391 if ((dev = malloc(sizeof *dev)) == NULL) {
392 /* Complete failure - parent doesn't continue trying to ``create'' */
398 memcpy(&dev->dev, &basei4bdevice, sizeof dev->dev);
399 memset(&dev->Timer, '\0', sizeof dev->Timer);
402 switch (p->cfg.cd.necessity) {
404 dev->dev.cd.delay = p->cfg.cd.delay;
407 dev->dev.cd = p->cfg.cd;
410 log_Printf(LogWARN, "%s: Carrier must be set, using ``set cd %d!''\n",
411 p->link.name, dev->dev.cd.delay);
416 oldflag = fcntl(p->fd, F_GETFL, 0);
418 /* Complete failure - parent doesn't continue trying to ``create'' */
420 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
421 p->link.name, strerror(errno));
428 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
431 strncpy(number, datalink_ChoosePhoneNumber(p->dl), sizeof number - 1);
432 number[sizeof number - 1] = '\0';
433 if (number[0] == '\0')
436 if (dial && ioctl(p->fd, I4B_RBCH_DIALOUT, number) == -1) {
437 /* Complete failure - parent doesn't continue trying to ``create'' */
439 log_Printf(LogWARN, "%s: ioctl(I4B_RBCH_DIALOUT): %s\n",
440 p->link.name, strerror(errno));
448 physical_SetupStack(p, dev->dev.name, PHYSICAL_FORCE_SYNC);