kernel - Tear out device polling
[dragonfly.git] / sys / net / i4b / driver / i4b_trace.c
1 /*
2  * Copyright (c) 1997, 2001 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  *      i4btrc - device driver for trace data read device
28  *      ---------------------------------------------------
29  *
30  *      last edit-date: [Sat Aug 11 18:07:15 2001]
31  *
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.17 2006/12/22 23:44:55 swildner Exp $
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include "use_i4btrc.h"
38
39 #if NI4BTRC > 0
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/uio.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <net/if.h>
49 #include <sys/tty.h>
50 #include <sys/thread2.h>
51
52 #include <net/i4b/include/machine/i4b_trace.h>
53 #include <net/i4b/include/machine/i4b_ioctl.h>
54
55 #include "../include/i4b_mbuf.h"
56 #include "../include/i4b_global.h"
57 #include "../include/i4b_l3l4.h"
58
59 static struct ifqueue trace_queue[NI4BTRC];
60 static int device_state[NI4BTRC];
61 #define ST_IDLE         0x00
62 #define ST_ISOPEN       0x01
63 #define ST_WAITDATA     0x02
64
65 static int analyzemode = 0;
66 static int rxunit = -1;
67 static int txunit = -1;
68 static int outunit = -1;
69
70 #define PDEVSTATIC static
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
76 #define CDEV_MAJOR 59
77
78 static struct dev_ops i4btrc_ops = {
79         { "i4btrc", CDEV_MAJOR, 0 },
80         .d_open =       i4btrcopen,
81         .d_close =      i4btrcclose,
82         .d_read =       i4btrcread,
83         .d_ioctl =      i4btrcioctl,
84 };
85
86 /*---------------------------------------------------------------------------*
87  *      interface init routine
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 PDEVSTATIC void
98 i4btrcattach(void *dummy)
99 {
100         int i;
101
102         kprintf("i4btrc: %d ISDN trace device(s) attached\n", NI4BTRC);
103         
104         for(i=0; i < NI4BTRC; i++)
105         {
106
107                 make_dev(&i4btrc_ops, i,
108                                      UID_ROOT, GID_WHEEL, 0600, "i4btrc%d", i);
109                 trace_queue[i].ifq_maxlen = IFQ_MAXLEN;
110
111                 device_state[i] = ST_IDLE;
112         }
113 }
114
115 /*---------------------------------------------------------------------------*
116  *      get_trace_data_from_l1()
117  *      ------------------------
118  *      is called from layer 1, adds timestamp to trace data and puts
119  *      it into a queue, from which it can be read from the i4btrc
120  *      device. The unit number in the trace header selects the minor
121  *      device's queue the data is put into.
122  *---------------------------------------------------------------------------*/
123 int
124 get_trace_data_from_l1(i4b_trace_hdr_t *hdr, int len, char *buf)
125 {
126         struct mbuf *m;
127         int unit;
128         int trunc = 0;
129         int totlen = len + sizeof(i4b_trace_hdr_t);
130
131         /*
132          * for telephony (or better non-HDLC HSCX mode) we get 
133          * (MCLBYTE + sizeof(i4b_trace_hdr_t)) length packets
134          * to put into the queue to userland. because of this
135          * we detect this situation, strip the length to MCLBYTES
136          * max size, and infor the userland program of this fact
137          * by putting the no of truncated bytes into hdr->trunc.
138          */
139          
140         if(totlen > MCLBYTES)
141         {
142                 trunc = 1;
143                 hdr->trunc = totlen - MCLBYTES;
144                 totlen = MCLBYTES;
145         }
146         else
147         {
148                 hdr->trunc = 0;
149         }
150
151         /* set length of trace record */
152         
153         hdr->length = totlen;
154         
155         /* check valid unit no */
156         
157         if((unit = hdr->unit) > NI4BTRC)
158         {
159                 kprintf("i4b_trace: get_trace_data_from_l1 - unit > NI4BTRC!\n"); 
160                 return(0);
161         }
162
163         /* get mbuf */
164         
165         if(!(m = i4b_Bgetmbuf(totlen)))
166         {
167                 kprintf("i4b_trace: get_trace_data_from_l1 - i4b_getmbuf() failed!\n");
168                 return(0);
169         }
170
171         /* check if we are in analyzemode */
172         
173         if(analyzemode && (unit == rxunit || unit == txunit))
174         {
175                 if(unit == rxunit)
176                         hdr->dir = FROM_NT;
177                 else
178                         hdr->dir = FROM_TE;
179                 unit = outunit;                 
180         }
181
182         if(IF_QFULL(&trace_queue[unit]))
183         {
184                 struct mbuf *m1;
185
186                 crit_enter();
187                 IF_DEQUEUE(&trace_queue[unit], m1);
188                 crit_exit();
189
190                 i4b_Bfreembuf(m1);
191         }
192         
193         /* copy trace header */
194         memcpy(m->m_data, hdr, sizeof(i4b_trace_hdr_t));
195
196         /* copy trace data */
197         if(trunc)
198                 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, totlen-sizeof(i4b_trace_hdr_t));
199         else
200                 memcpy(&m->m_data[sizeof(i4b_trace_hdr_t)], buf, len);
201
202         crit_enter();
203         
204         IF_ENQUEUE(&trace_queue[unit], m);
205         
206         if(device_state[unit] & ST_WAITDATA)
207         {
208                 device_state[unit] &= ~ST_WAITDATA;
209                 wakeup((caddr_t) &trace_queue[unit]);
210         }
211
212         crit_exit();
213         
214         return(1);
215 }
216
217 /*---------------------------------------------------------------------------*
218  *      open trace device
219  *---------------------------------------------------------------------------*/
220 PDEVSTATIC int
221 i4btrcopen(struct dev_open_args *ap)
222 {
223         cdev_t dev = ap->a_head.a_dev;
224         int unit = minor(dev);
225
226         if(unit >= NI4BTRC)
227                 return(ENXIO);
228
229         if(device_state[unit] & ST_ISOPEN)
230                 return(EBUSY);
231
232         if(analyzemode && (unit == outunit || unit == rxunit || unit == txunit))
233                 return(EBUSY);
234
235         crit_enter();
236         
237         device_state[unit] = ST_ISOPEN;         
238
239         crit_exit();
240         
241         return(0);
242 }
243
244 /*---------------------------------------------------------------------------*
245  *      close trace device
246  *---------------------------------------------------------------------------*/
247 PDEVSTATIC int
248 i4btrcclose(struct dev_close_args *ap)
249 {
250         cdev_t dev = ap->a_head.a_dev;
251         int unit = minor(dev);
252         int i;
253         int cno = -1;
254
255         for(i=0; i < nctrl; i++)
256         {
257                 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
258                         (ctrl_desc[i].unit == unit))
259                 {
260                         cno = i;
261                         break;
262                 }
263         }
264
265         if(analyzemode && (unit == outunit))
266         {
267                 analyzemode = 0;                
268                 outunit = -1;
269                 
270                 if(cno >= 0)
271                 {
272                         (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, TRACE_OFF);
273                         (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, TRACE_OFF);
274                 }
275                 rxunit = -1;
276                 txunit = -1;
277         }
278         
279         if(cno >= 0)
280         {
281                         (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, TRACE_OFF);
282         }
283
284         crit_enter();
285         device_state[unit] = ST_IDLE;
286         crit_exit();
287         
288         return(0);
289 }
290
291 /*---------------------------------------------------------------------------*
292  *      read from trace device
293  *---------------------------------------------------------------------------*/
294 PDEVSTATIC int
295 i4btrcread(struct dev_read_args *ap)
296 {
297         cdev_t dev = ap->a_head.a_dev;
298         struct uio *uio = ap->a_uio;
299         struct mbuf *m;
300         int error = 0;
301         int unit = minor(dev);
302         
303         if(!(device_state[unit] & ST_ISOPEN))
304                 return(EIO);
305
306         crit_enter();
307         
308         while(IF_QEMPTY(&trace_queue[unit]) && (device_state[unit] & ST_ISOPEN))
309         {
310                 device_state[unit] |= ST_WAITDATA;
311                 
312                 if((error = tsleep((caddr_t) &trace_queue[unit],
313                                         PCATCH, "bitrc", 0 )) != 0)
314                 {
315                         device_state[unit] &= ~ST_WAITDATA;
316                         crit_exit();
317                         return(error);
318                 }
319         }
320
321         IF_DEQUEUE(&trace_queue[unit], m);
322
323         if(m && m->m_len)
324                 error = uiomove(m->m_data, m->m_len, uio);
325         else
326                 error = EIO;
327                 
328         if(m)
329                 i4b_Bfreembuf(m);
330
331         crit_exit();
332         
333         return(error);
334 }
335
336 /*---------------------------------------------------------------------------*
337  *      device driver ioctl routine
338  *---------------------------------------------------------------------------*/
339 PDEVSTATIC int
340 i4btrcioctl(struct dev_ioctl_args *ap)
341 {
342         cdev_t dev = ap->a_head.a_dev;
343         int error = 0;
344         int unit = minor(dev);
345         i4b_trace_setupa_t *tsa;
346         int i;
347         int cno = -1;
348
349         /* find the first passive controller matching our unit no */
350
351         for(i=0; i < nctrl; i++)
352         {
353                 if((ctrl_desc[i].ctrl_type == CTRL_PASSIVE) &&
354                         (ctrl_desc[i].unit == unit))
355                 {
356                         cno = i;
357                         break;
358                 }
359         }
360         
361         switch(ap->a_cmd)
362         {
363                 case I4B_TRC_SET:
364                         if(cno < 0)
365                                 return ENOTTY;
366                         (*ctrl_desc[cno].N_MGMT_COMMAND)(ctrl_desc[cno].unit, CMR_SETTRACE, (void *)*(unsigned int *)ap->a_data);
367                         break;
368
369                 case I4B_TRC_SETA:
370                         tsa = (i4b_trace_setupa_t *)ap->a_data;
371
372                         if(tsa->rxunit >= 0 && tsa->rxunit < NI4BTRC)
373                                 rxunit = tsa->rxunit;
374                         else
375                                 error = EINVAL;
376
377                         if(tsa->txunit >= 0 && tsa->txunit < NI4BTRC)
378                                 txunit = tsa->txunit;
379                         else
380                                 error = EINVAL;
381
382                         if(error)
383                         {
384                                 outunit = -1;
385                                 rxunit = -1;
386                                 txunit = -1;
387                         }
388                         else
389                         {
390                                 if(cno < 0)
391                                         return ENOTTY;
392                                         
393                                 outunit = unit;
394                                 analyzemode = 1;
395                                 (*ctrl_desc[cno].N_MGMT_COMMAND)(rxunit, CMR_SETTRACE, (int *)(tsa->rxflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
396                                 (*ctrl_desc[cno].N_MGMT_COMMAND)(txunit, CMR_SETTRACE, (int *)(tsa->txflags & (TRACE_I | TRACE_D_RX | TRACE_B_RX)));
397                         }
398                         break;
399
400                 case I4B_TRC_RESETA:
401                         analyzemode = 0;                
402                         outunit = -1;
403                         rxunit = -1;
404                         txunit = -1;
405                         break;
406                         
407                 default:
408                         error = ENOTTY;
409                         break;
410         }
411         return(error);
412 }
413
414 #endif /* NI4BTRC > 0 */