]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i4b/driver/i4b_trace.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / i4b / driver / i4b_trace.c
1 /*-
2  * Copyright (c) 1997, 2002 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 /*---------------------------------------------------------------------------
27  *
28  *      i4btrc - device driver for trace data read device
29  *      ---------------------------------------------------
30  *      last edit-date: [Sun Mar 17 09:52:51 2002]
31  *
32  *      NOTE: the code assumes that SPLI4B >= splimp !
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_i4b.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioccom.h>
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <net/if.h>
50 #include <sys/tty.h>
51
52 #include <i4b/include/i4b_trace.h>
53 #include <i4b/include/i4b_ioctl.h>
54
55 #include <i4b/include/i4b_mbuf.h>
56 #include <i4b/include/i4b_global.h>
57 #include <i4b/include/i4b_l3l4.h>
58
59 static struct ifqueue trace_queue[NI4BTRC];
60
61 static int device_state[NI4BTRC];
62 #define ST_IDLE         0x00
63 #define ST_ISOPEN       0x01
64 #define ST_WAITDATA     0x02
65
66 static int analyzemode = 0;
67 static int rxunit = -1;
68 static int txunit = -1;
69 static int outunit = -1;
70
71 static d_open_t i4btrcopen;
72 static d_close_t i4btrcclose;
73 static d_read_t i4btrcread;
74 static d_ioctl_t i4btrcioctl;
75 static d_poll_t i4btrcpoll;
76
77
78 static struct cdevsw i4btrc_cdevsw = {
79         .d_version =    D_VERSION,
80         .d_flags =      D_NEEDGIANT,
81         .d_open =       i4btrcopen,
82         .d_close =      i4btrcclose,
83         .d_read =       i4btrcread,
84         .d_ioctl =      i4btrcioctl,
85         .d_poll =       i4btrcpoll,
86         .d_name =       "i4btrc",
87 };
88
89 static void i4btrcattach(void *);
90 PSEUDO_SET(i4btrcattach, i4b_trace);
91
92 int get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf);
93
94 /*---------------------------------------------------------------------------*
95  *      interface attach routine
96  *---------------------------------------------------------------------------*/
97 static void
98 i4btrcattach(void *dummy)
99 {
100         int i;
101
102         printf("i4btrc: %d ISDN trace device(s) attached\n", NI4BTRC);
103         
104         for(i=0; i < NI4BTRC; i++)
105         {
106                 make_dev(&i4btrc_cdevsw, i,
107                                      UID_ROOT, GID_WHEEL, 0600, "i4btrc%d", i);
108                 trace_queue[i].ifq_maxlen = IFQ_MAXLEN;
109
110                 if(!mtx_initialized(&trace_queue[i].ifq_mtx))
111                         mtx_init(&trace_queue[i].ifq_mtx, "i4b_trace", NULL, MTX_DEF);
112
113                 device_state[i] = ST_IDLE;
114         }
115 }
116
117 /*---------------------------------------------------------------------------*
118  *      get_trace_data_from_l1()
119  *      ------------------------
120  *      is called from layer 1, adds timestamp to trace data and puts
121  *      it into a queue, from which it can be read from the i4btrc
122  *      device. The unit number in the trace header selects the minor
123  *      device's queue the data is put into.
124  *---------------------------------------------------------------------------*/
125 int
126 get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf)
127 {
128         struct mbuf *m;
129         int x;
130         int unit;
131         int trunc = 0;
132         int totlen = len + sizeof(i4b_trace_hdr_t);
133
134         /*
135          * for telephony (or better non-HDLC HSCX mode) we get 
136          * (MCLBYTE + sizeof(i4b_trace_hdr_t)) length packets
137          * to put into the queue to userland. because of this
138          * we detect this situation, strip the length to MCLBYTES
139          * max size, and infor the userland program of this fact
140          * by putting the no of truncated bytes into hdr->trunc.
141          */
142          
143         if(totlen > MCLBYTES)
144         {
145                 trunc = 1;
146                 hdr->trunc = totlen - MCLBYTES;
147                 totlen = MCLBYTES;
148         }
149         else
150         {
151                 hdr->trunc = 0;
152         }
153
154         /* set length of trace record */
155         
156         hdr->length = totlen;
157         
158         /* check valid unit no */
159         
160         if((unit = hdr->unit) >= NI4BTRC)
161         {
162                 printf("i4b_trace: get_trace_data_from_l1 - unit > NI4BTRC!\n"); 
163                 return(0);
164         }
165
166         /* get mbuf */
167         
168         if(!(m = i4b_Bgetmbuf(totlen)))
169         {
170                 printf("i4b_trace: get_trace_data_from_l1 - i4b_getmbuf() failed!\n");
171                 return(0);
172         }
173
174         /* check if we are in analyzemode */
175         
176         if(analyzemode && (unit == rxunit || unit == txunit))
177         {
178                 if(unit == rxunit)
179                         hdr->dir = FROM_NT;
180                 else
181                         hdr->dir = FROM_TE;
182                 unit = outunit;                 
183         }
184
185         IF_LOCK(&trace_queue[unit]);
186
187         if(_IF_QFULL(&trace_queue[unit]))
188         {
189                 struct mbuf *m1;
190
191                 x = SPLI4B();
192                 _IF_DEQUEUE(&trace_queue[unit], m1);
193                 splx(x);                
194
195                 i4b_Bfreembuf(m1);
196         }
197         
198         /* copy trace header */
199         memcpy(m->m_data, hdr, sizeof(i4b_trace_hdr_t));
200
201         /* copy trace data */
202         if(trunc)
203                 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, totlen-sizeof(i4b_trace_hdr_t));
204         else
205                 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, len);
206
207         x = SPLI4B();
208         
209         _IF_ENQUEUE(&trace_queue[unit], m);
210         IF_UNLOCK(&trace_queue[unit]);
211         
212         if(device_state[unit] & ST_WAITDATA)
213         {
214                 device_state[unit] &= ~ST_WAITDATA;
215                 wakeup( &trace_queue[unit]);
216         }
217
218         splx(x);
219         
220         return(1);
221 }
222
223 /*---------------------------------------------------------------------------*
224  *      open trace device
225  *---------------------------------------------------------------------------*/
226 static int
227 i4btrcopen(struct cdev *dev, int flag, int fmt, struct thread *td)
228 {
229         int x;
230         int unit = minor(dev);
231
232         if(unit >= NI4BTRC)
233                 return(ENXIO);
234
235         if(device_state[unit] & ST_ISOPEN)
236                 return(EBUSY);
237
238         if(analyzemode && (unit == outunit || unit == rxunit || unit == txunit))
239                 return(EBUSY);
240
241         x = SPLI4B();
242         
243         device_state[unit] = ST_ISOPEN;         
244
245         splx(x);
246         
247         return(0);
248 }
249
250 /*---------------------------------------------------------------------------*
251  *      close trace device
252  *---------------------------------------------------------------------------*/
253 static int
254 i4btrcclose(struct cdev *dev, int flag, int fmt, struct thread *td)
255 {
256         int unit = minor(dev);
257         int i, x;
258         int cno = -1;
259
260         for(i=0; i < nctrl; i++)
261         {
262                 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
263                         (ctrl_desc[i].unit == unit))
264                 {
265                         cno = i;
266                         break;
267                 }
268         }
269
270         if(analyzemode && (unit == outunit))
271         {
272                 analyzemode = 0;                
273                 outunit = -1;
274                 
275                 if(cno >= 0)
276                 {
277                         (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, TRACE_OFF);
278                         (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, TRACE_OFF);
279                 }
280                 rxunit = -1;
281                 txunit = -1;
282         }
283         
284         if(cno >= 0)
285         {
286                         (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, TRACE_OFF);
287         }
288
289         x = SPLI4B();
290         device_state[unit] = ST_IDLE;
291         splx(x);
292         
293         return(0);
294 }
295
296 /*---------------------------------------------------------------------------*
297  *      read from trace device
298  *---------------------------------------------------------------------------*/
299 static int
300 i4btrcread(struct cdev *dev, struct uio * uio, int ioflag)
301 {
302         struct mbuf *m;
303         int x;
304         int error = 0;
305         int unit = minor(dev);
306         
307         if(!(device_state[unit] & ST_ISOPEN))
308                 return(EIO);
309
310         x = SPLI4B();
311         
312         IF_LOCK(&trace_queue[unit]);
313
314         while(IF_QEMPTY(&trace_queue[unit]) && (device_state[unit] & ST_ISOPEN))
315         {
316                 device_state[unit] |= ST_WAITDATA;
317                 
318                 if((error = msleep( &trace_queue[unit],
319                                         &trace_queue[unit].ifq_mtx,
320                                         I4BPRI | PCATCH,
321                                         "bitrc", 0 )) != 0)
322                 {
323                         device_state[unit] &= ~ST_WAITDATA;
324                         IF_UNLOCK(&trace_queue[unit]);
325                         splx(x);
326                         return(error);
327                 }
328         }
329
330         _IF_DEQUEUE(&trace_queue[unit], m);
331         IF_UNLOCK(&trace_queue[unit]);
332
333         if(m && m->m_len)
334                 error = uiomove(m->m_data, m->m_len, uio);
335         else
336                 error = EIO;
337                 
338         if(m)
339                 i4b_Bfreembuf(m);
340
341         splx(x);
342         
343         return(error);
344 }
345
346 /*---------------------------------------------------------------------------*
347  *      poll device
348  *---------------------------------------------------------------------------*/
349 static int
350 i4btrcpoll(struct cdev *dev, int events, struct thread *td)
351 {
352         return(ENODEV);
353 }
354
355 /*---------------------------------------------------------------------------*
356  *      device driver ioctl routine
357  *---------------------------------------------------------------------------*/
358 static int
359 i4btrcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
360 {
361         int error = 0;
362         int unit = minor(dev);
363         i4b_trace_setupa_t *tsa;
364         int i;
365         int cno = -1;
366
367         /* find the first passive controller matching our unit no */
368
369         for(i=0; i < nctrl; i++)
370         {
371                 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
372                         (ctrl_desc[i].unit == unit))
373                 {
374                         cno = i;
375                         break;
376                 }
377         }
378         
379         switch(cmd)
380         {
381                 case I4B_TRC_SET:
382                         if(cno < 0)
383                                 return ENOTTY;
384                         (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, (void *)*(unsigned int *)data);
385                         break;
386
387                 case I4B_TRC_SETA:
388                         tsa = (i4b_trace_setupa_t *)data;
389
390                         if(tsa->rxunit >= 0 && tsa->rxunit < NI4BTRC)
391                                 rxunit = tsa->rxunit;
392                         else
393                                 error = EINVAL;
394
395                         if(tsa->txunit >= 0 && tsa->txunit < NI4BTRC)
396                                 txunit = tsa->txunit;
397                         else
398                                 error = EINVAL;
399
400                         if(error)
401                         {
402                                 outunit = -1;
403                                 rxunit = -1;
404                                 txunit = -1;
405                         }
406                         else
407                         {
408                                 if(cno < 0)
409                                         return ENOTTY;
410                                         
411                                 outunit = unit;
412                                 analyzemode = 1;
413                                 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, (int *)(tsa->rxflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
414                                 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, (int *)(tsa->txflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
415                         }
416                         break;
417
418                 case I4B_TRC_RESETA:
419                         analyzemode = 0;                
420                         outunit = -1;
421                         rxunit = -1;
422                         txunit = -1;
423                         break;
424                         
425                 default:
426                         error = ENOTTY;
427                         break;
428         }
429         return(error);
430 }