2 * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 *---------------------------------------------------------------------------
27 * i4btrc - device driver for trace data read device
28 * ---------------------------------------------------
30 * last edit-date: [Sat Aug 11 18:07:15 2001]
32 * $FreeBSD: src/sys/i4b/driver/i4b_trace.c,v 1.9.2.3 2001/08/12 16:22:48 hm Exp $
33 * $DragonFly: src/sys/net/i4b/driver/i4b_trace.c,v 1.13 2005/06/14 21:19:18 joerg Exp $
35 *---------------------------------------------------------------------------*/
37 #include "use_i4btrc.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioccom.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
51 #include <sys/thread2.h>
53 #include <net/i4b/include/machine/i4b_trace.h>
54 #include <net/i4b/include/machine/i4b_ioctl.h>
56 #include "../include/i4b_mbuf.h"
57 #include "../include/i4b_global.h"
58 #include "../include/i4b_l3l4.h"
60 static struct ifqueue trace_queue[NI4BTRC];
61 static int device_state[NI4BTRC];
63 #define ST_ISOPEN 0x01
64 #define ST_WAITDATA 0x02
66 static int analyzemode = 0;
67 static int rxunit = -1;
68 static int txunit = -1;
69 static int outunit = -1;
71 #define PDEVSTATIC static
72 static d_open_t i4btrcopen;
73 static d_close_t i4btrcclose;
74 static d_read_t i4btrcread;
75 static d_ioctl_t i4btrcioctl;
77 static d_poll_t i4btrcpoll;
78 #define POLLFIELD i4btrcpoll
82 static struct cdevsw i4btrc_cdevsw = {
89 /* open */ i4btrcopen,
90 /* close */ i4btrcclose,
91 /* read */ i4btrcread,
93 /* ioctl */ i4btrcioctl,
96 /* strategy */ nostrategy,
101 /*---------------------------------------------------------------------------*
102 * interface init routine
103 *---------------------------------------------------------------------------*/
105 void i4btrcinit(void *unused)
107 cdevsw_add(&i4btrc_cdevsw, 0, 0);
110 SYSINIT(i4btrcdev, SI_SUB_DRIVERS,
111 SI_ORDER_MIDDLE+CDEV_MAJOR, &i4btrcinit, NULL);
113 static void i4btrcattach(void *);
114 PSEUDO_SET(i4btrcattach, i4b_trace);
116 int get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf);
118 /*---------------------------------------------------------------------------*
119 * interface attach routine
120 *---------------------------------------------------------------------------*/
122 i4btrcattach(void *dummy)
126 printf("i4btrc: %d ISDN trace device(s) attached\n", NI4BTRC);
128 for(i=0; i < NI4BTRC; i++)
131 make_dev(&i4btrc_cdevsw, i,
132 UID_ROOT, GID_WHEEL, 0600, "i4btrc%d", i);
133 trace_queue[i].ifq_maxlen = IFQ_MAXLEN;
135 device_state[i] = ST_IDLE;
139 /*---------------------------------------------------------------------------*
140 * get_trace_data_from_l1()
141 * ------------------------
142 * is called from layer 1, adds timestamp to trace data and puts
143 * it into a queue, from which it can be read from the i4btrc
144 * device. The unit number in the trace header selects the minor
145 * device's queue the data is put into.
146 *---------------------------------------------------------------------------*/
148 get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf)
153 int totlen = len + sizeof(i4b_trace_hdr_t);
156 * for telephony (or better non-HDLC HSCX mode) we get
157 * (MCLBYTE + sizeof(i4b_trace_hdr_t)) length packets
158 * to put into the queue to userland. because of this
159 * we detect this situation, strip the length to MCLBYTES
160 * max size, and infor the userland program of this fact
161 * by putting the no of truncated bytes into hdr->trunc.
164 if(totlen > MCLBYTES)
167 hdr->trunc = totlen - MCLBYTES;
175 /* set length of trace record */
177 hdr->length = totlen;
179 /* check valid unit no */
181 if((unit = hdr->unit) > NI4BTRC)
183 printf("i4b_trace: get_trace_data_from_l1 - unit > NI4BTRC!\n");
189 if(!(m = i4b_Bgetmbuf(totlen)))
191 printf("i4b_trace: get_trace_data_from_l1 - i4b_getmbuf() failed!\n");
195 /* check if we are in analyzemode */
197 if(analyzemode && (unit == rxunit || unit == txunit))
206 if(IF_QFULL(&trace_queue[unit]))
211 IF_DEQUEUE(&trace_queue[unit], m1);
217 /* copy trace header */
218 memcpy(m->m_data, hdr, sizeof(i4b_trace_hdr_t));
220 /* copy trace data */
222 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, totlen-sizeof(i4b_trace_hdr_t));
224 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, len);
228 IF_ENQUEUE(&trace_queue[unit], m);
230 if(device_state[unit] & ST_WAITDATA)
232 device_state[unit] &= ~ST_WAITDATA;
233 wakeup((caddr_t) &trace_queue[unit]);
241 /*---------------------------------------------------------------------------*
243 *---------------------------------------------------------------------------*/
245 i4btrcopen(dev_t dev, int flag, int fmt, struct thread *td)
247 int unit = minor(dev);
252 if(device_state[unit] & ST_ISOPEN)
255 if(analyzemode && (unit == outunit || unit == rxunit || unit == txunit))
260 device_state[unit] = ST_ISOPEN;
267 /*---------------------------------------------------------------------------*
269 *---------------------------------------------------------------------------*/
271 i4btrcclose(dev_t dev, int flag, int fmt, struct thread *td)
273 int unit = minor(dev);
277 for(i=0; i < nctrl; i++)
279 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
280 (ctrl_desc[i].unit == unit))
287 if(analyzemode && (unit == outunit))
294 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, TRACE_OFF);
295 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, TRACE_OFF);
303 (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, TRACE_OFF);
307 device_state[unit] = ST_IDLE;
313 /*---------------------------------------------------------------------------*
314 * read from trace device
315 *---------------------------------------------------------------------------*/
317 i4btrcread(dev_t dev, struct uio * uio, int ioflag)
321 int unit = minor(dev);
323 if(!(device_state[unit] & ST_ISOPEN))
328 while(IF_QEMPTY(&trace_queue[unit]) && (device_state[unit] & ST_ISOPEN))
330 device_state[unit] |= ST_WAITDATA;
332 if((error = tsleep((caddr_t) &trace_queue[unit],
333 PCATCH, "bitrc", 0 )) != 0)
335 device_state[unit] &= ~ST_WAITDATA;
341 IF_DEQUEUE(&trace_queue[unit], m);
344 error = uiomove(m->m_data, m->m_len, uio);
356 /*---------------------------------------------------------------------------*
358 *---------------------------------------------------------------------------*/
360 i4btrcpoll(dev_t dev, int events, struct thread *td)
365 /*---------------------------------------------------------------------------*
366 * device driver ioctl routine
367 *---------------------------------------------------------------------------*/
369 i4btrcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
372 int unit = minor(dev);
373 i4b_trace_setupa_t *tsa;
377 /* find the first passive controller matching our unit no */
379 for(i=0; i < nctrl; i++)
381 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
382 (ctrl_desc[i].unit == unit))
394 (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, (void *)*(unsigned int *)data);
398 tsa = (i4b_trace_setupa_t *)data;
400 if(tsa->rxunit >= 0 && tsa->rxunit < NI4BTRC)
401 rxunit = tsa->rxunit;
405 if(tsa->txunit >= 0 && tsa->txunit < NI4BTRC)
406 txunit = tsa->txunit;
423 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, (int *)(tsa->rxflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
424 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, (int *)(tsa->txflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
442 #endif /* NI4BTRC > 0 */