Add a spinlock(9) manual page (based on a writeup by Matt).
[dragonfly.git] / share / examples / meteor / test-n.c
1 /* A simple program to test the communication between the matrox meteor
2  * driver and an user application in the continous sync capture mode.
3  * 
4  * First the driver clears the mask and decrements the counter like it
5  * would in a normal application. Then it purpose does not handle these
6  * responsibilities to simulate an application falling behind. I use
7  * the HUP signal to work as if the some of the  buffers were removed
8  * (the second couter is used to cleared the correct bits.
9  *
10  *      build kernel with at least:
11  *
12  *              options "METEOR_ALLOC_PAGES=301"  
13  *
14  */
15 /* Copyright (c) 1995 Mark Tinguely and Jim Lowe 
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *      This product includes software developed by Mark Tinguely and Jim Lowe
29  * 4. The name of the author may not be used to endorse or promote products
30  *    derived from this software without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGE.
43  */
44
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <sys/fcntl.h>
48 /*#include <machine/ioctl_meteor.h> */
49 #include "/sys/i386/include/ioctl_meteor.h"
50
51 typedef unsigned char uint8;
52 typedef signed char int8;
53
54 int i;
55 static uint8 *y;
56 extern int errno;
57 struct meteor_mem *common_mem;
58 int sig_cnt;
59 int sig_cnt2;
60
61 void
62 hup_catcher()
63 {
64         /* clear 4 oldest active bits */
65         common_mem->active &= ~(1 << (sig_cnt2++ % 32));
66         common_mem->active &= ~(1 << (sig_cnt2++ % 32));
67         common_mem->active &= ~(1 << (sig_cnt2++ % 32));
68         common_mem->active &= ~(1 << (sig_cnt2++ % 32));
69         /* lowat is low enough that I will need 2 hups to start saving again */
70         common_mem->num_active_bufs -= 4;
71         puts("hup caught");
72 }
73
74 void
75 usr2_catcher()
76 {
77         int j;
78         struct meteor_capframe capframe;
79
80         printf("active %3d = %08x ", sig_cnt,common_mem->active);
81         printf("# act  %3d = %d\n", sig_cnt, common_mem->num_active_bufs);
82
83         if (sig_cnt < 80) {
84                 common_mem->active &= ~(1 << (sig_cnt % 32));
85                 common_mem->num_active_bufs--; 
86                 sig_cnt2++;
87         }
88
89         printf("data %08x\n", *((u_long *) (y + (sig_cnt % 32) *
90                                                 common_mem->frame_size)));
91         if (++sig_cnt >= 200) {
92                 capframe.command=METEOR_CAP_STOP_FRAMES;
93
94                 if (ioctl(i, METEORCAPFRM, &capframe) < 0) {
95                         printf("METEORCAPFRM failed %d\n", errno);
96                         exit(1);
97                 }
98                 exit (0);
99         }
100 }
101
102 main()
103 {
104         struct meteor_geomet geo;
105         int height, width, depth, frames, size;
106         struct meteor_capframe capframe;
107
108         if ((i = open("/dev/meteor", O_RDONLY)) < 0) {
109                 printf("open failed\n");
110                 exit(1);
111         }
112         printf("test %d %d\n", errno, i);
113
114         height = geo.rows = 120;
115         width= geo.columns = 160;
116         frames = geo.frames = 32;
117         depth = 2;                      /* 2 bytes per pixel */
118
119         printf("ioctl %d %d\n", errno, i);
120
121         geo.oformat = METEOR_GEO_RGB16;
122
123         if (ioctl(i, METEORSETGEO, &geo) < 0) {
124                 printf("METEORSETGEO failed %d\n", errno);
125                 exit(1);
126         }
127
128         printf("mmap %d %d\n", errno, i);
129         size = ((width*height*depth*frames+4095)/4096)*4096;
130         y=(uint8 *) mmap((caddr_t)0, size + 4096, PROT_READ |PROT_WRITE,MAP_SHARED, i, (off_t)0);
131
132         if (y == (uint8 *) MAP_FAILED) return (0);
133
134         common_mem = (struct meteor_mem *) (y + size);
135
136         signal(1, hup_catcher);
137         signal(31, usr2_catcher);
138
139         capframe.command=METEOR_CAP_N_FRAMES;
140         capframe.signal=31;
141         capframe.lowat=25;
142         capframe.hiwat=30;
143
144         printf("ioctl %d %d\n", errno, i);
145         if (ioctl(i, METEORCAPFRM, &capframe) < 0) {
146                 printf("METEORCAPFRM failed %d\n", errno);
147                 exit(1);
148         }
149
150         printf("signal = %d\n", common_mem->signal);
151         printf("frame size = %d\n", common_mem->frame_size);
152         printf("buffers = %d\n", common_mem->num_bufs);
153         printf("hiwater = %d\n", common_mem->hiwat);
154         printf("lowater = %d\n", common_mem->lowat);
155         printf("active = %08x\n", common_mem->active);
156         printf("# active = %d\n", common_mem->num_active_bufs);
157   
158         printf("sleep loop\n", errno, i);
159         while (1) {
160                 sleep (60);
161         }
162 }