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