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 $
34 *---------------------------------------------------------------------------*/
36 #include "use_i4btrc.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/socket.h>
49 #include <sys/thread2.h>
51 #include <net/i4b/include/machine/i4b_trace.h>
52 #include <net/i4b/include/machine/i4b_ioctl.h>
54 #include "../include/i4b_mbuf.h"
55 #include "../include/i4b_global.h"
56 #include "../include/i4b_l3l4.h"
58 static struct ifqueue trace_queue[NI4BTRC];
59 static int device_state[NI4BTRC];
61 #define ST_ISOPEN 0x01
62 #define ST_WAITDATA 0x02
64 static int analyzemode = 0;
65 static int rxunit = -1;
66 static int txunit = -1;
67 static int outunit = -1;
69 #define PDEVSTATIC static
70 static d_open_t i4btrcopen;
71 static d_close_t i4btrcclose;
72 static d_read_t i4btrcread;
73 static d_ioctl_t i4btrcioctl;
75 static struct dev_ops i4btrc_ops = {
78 .d_close = i4btrcclose,
80 .d_ioctl = i4btrcioctl,
83 /*---------------------------------------------------------------------------*
84 * interface init routine
85 *---------------------------------------------------------------------------*/
86 static void i4btrcattach(void *);
87 PSEUDO_SET(i4btrcattach, i4b_trace);
89 int get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf);
91 /*---------------------------------------------------------------------------*
92 * interface attach routine
93 *---------------------------------------------------------------------------*/
95 i4btrcattach(void *dummy)
99 kprintf("i4btrc: %d ISDN trace device(s) attached\n", NI4BTRC);
101 for(i=0; i < NI4BTRC; i++)
104 make_dev(&i4btrc_ops, i,
105 UID_ROOT, GID_WHEEL, 0600, "i4btrc%d", i);
106 trace_queue[i].ifq_maxlen = IFQ_MAXLEN;
108 device_state[i] = ST_IDLE;
112 /*---------------------------------------------------------------------------*
113 * get_trace_data_from_l1()
114 * ------------------------
115 * is called from layer 1, adds timestamp to trace data and puts
116 * it into a queue, from which it can be read from the i4btrc
117 * device. The unit number in the trace header selects the minor
118 * device's queue the data is put into.
119 *---------------------------------------------------------------------------*/
121 get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf)
126 int totlen = len + sizeof(i4b_trace_hdr_t);
129 * for telephony (or better non-HDLC HSCX mode) we get
130 * (MCLBYTE + sizeof(i4b_trace_hdr_t)) length packets
131 * to put into the queue to userland. because of this
132 * we detect this situation, strip the length to MCLBYTES
133 * max size, and infor the userland program of this fact
134 * by putting the no of truncated bytes into hdr->trunc.
137 if(totlen > MCLBYTES)
140 hdr->trunc = totlen - MCLBYTES;
148 /* set length of trace record */
150 hdr->length = totlen;
152 /* check valid unit no */
154 if((unit = hdr->unit) > NI4BTRC)
156 kprintf("i4b_trace: get_trace_data_from_l1 - unit > NI4BTRC!\n");
162 if(!(m = i4b_Bgetmbuf(totlen)))
164 kprintf("i4b_trace: get_trace_data_from_l1 - i4b_getmbuf() failed!\n");
168 /* check if we are in analyzemode */
170 if(analyzemode && (unit == rxunit || unit == txunit))
179 if(IF_QFULL(&trace_queue[unit]))
184 IF_DEQUEUE(&trace_queue[unit], m1);
190 /* copy trace header */
191 memcpy(m->m_data, hdr, sizeof(i4b_trace_hdr_t));
193 /* copy trace data */
195 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, totlen-sizeof(i4b_trace_hdr_t));
197 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, len);
201 IF_ENQUEUE(&trace_queue[unit], m);
203 if(device_state[unit] & ST_WAITDATA)
205 device_state[unit] &= ~ST_WAITDATA;
206 wakeup((caddr_t) &trace_queue[unit]);
214 /*---------------------------------------------------------------------------*
216 *---------------------------------------------------------------------------*/
218 i4btrcopen(struct dev_open_args *ap)
220 cdev_t dev = ap->a_head.a_dev;
221 int unit = minor(dev);
226 if(device_state[unit] & ST_ISOPEN)
229 if(analyzemode && (unit == outunit || unit == rxunit || unit == txunit))
234 device_state[unit] = ST_ISOPEN;
241 /*---------------------------------------------------------------------------*
243 *---------------------------------------------------------------------------*/
245 i4btrcclose(struct dev_close_args *ap)
247 cdev_t dev = ap->a_head.a_dev;
248 int unit = minor(dev);
252 for(i=0; i < nctrl; i++)
254 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
255 (ctrl_desc[i].unit == unit))
262 if(analyzemode && (unit == outunit))
269 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, TRACE_OFF);
270 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, TRACE_OFF);
278 (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, TRACE_OFF);
282 device_state[unit] = ST_IDLE;
288 /*---------------------------------------------------------------------------*
289 * read from trace device
290 *---------------------------------------------------------------------------*/
292 i4btrcread(struct dev_read_args *ap)
294 cdev_t dev = ap->a_head.a_dev;
295 struct uio *uio = ap->a_uio;
298 int unit = minor(dev);
300 if(!(device_state[unit] & ST_ISOPEN))
305 while(IF_QEMPTY(&trace_queue[unit]) && (device_state[unit] & ST_ISOPEN))
307 device_state[unit] |= ST_WAITDATA;
309 if((error = tsleep((caddr_t) &trace_queue[unit],
310 PCATCH, "bitrc", 0 )) != 0)
312 device_state[unit] &= ~ST_WAITDATA;
318 IF_DEQUEUE(&trace_queue[unit], m);
321 error = uiomove(m->m_data, m->m_len, uio);
333 /*---------------------------------------------------------------------------*
334 * device driver ioctl routine
335 *---------------------------------------------------------------------------*/
337 i4btrcioctl(struct dev_ioctl_args *ap)
339 cdev_t dev = ap->a_head.a_dev;
341 int unit = minor(dev);
342 i4b_trace_setupa_t *tsa;
346 /* find the first passive controller matching our unit no */
348 for(i=0; i < nctrl; i++)
350 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
351 (ctrl_desc[i].unit == unit))
363 (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, (void *)*(unsigned int *)ap->a_data);
367 tsa = (i4b_trace_setupa_t *)ap->a_data;
369 if(tsa->rxunit >= 0 && tsa->rxunit < NI4BTRC)
370 rxunit = tsa->rxunit;
374 if(tsa->txunit >= 0 && tsa->txunit < NI4BTRC)
375 txunit = tsa->txunit;
392 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, (int *)(tsa->rxflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
393 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, (int *)(tsa->txflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
411 #endif /* NI4BTRC > 0 */