Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / video / bktr / bktr_mem.c
1 /* $FreeBSD: src/sys/dev/bktr/bktr_mem.c,v 1.2.2.4 2001/10/09 04:08:41 jlemon Exp $ */
2
3 /*
4  * This is prt of the Driver for Video Capture Cards (Frame grabbers)
5  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
6  * chipset.
7  * Copyright Roger Hardiman.
8  *
9  * bktr_mem : This kernel module allows us to keep our allocated
10  *            contiguous memory for the video buffer, DMA programs and VBI data
11  *            while the main bktr driver is unloaded and reloaded.
12  *            This avoids the problem of trying to allocate contiguous each
13  *            time the bktr driver is loaded.
14  */
15
16 /*
17  * 1. Redistributions of source code must retain the
18  * Copyright (c) 2000 Roger Hardiman
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  * 3. All advertising materials mentioning features or use of this software
30  *    must display the following acknowledgement:
31  *      This product includes software developed by Roger Hardiman
32  * 4. The name of the author may not be used to endorse or promote products
33  *    derived from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
39  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  */
47
48
49 #include <sys/param.h>
50 #include <sys/kernel.h>
51 #include <dev/bktr/bktr_mem.h>
52
53 struct memory_pointers {
54         int         addresses_stored;
55         vm_offset_t dma_prog;
56         vm_offset_t odd_dma_prog;
57         vm_offset_t vbidata;
58         vm_offset_t vbibuffer;
59         vm_offset_t buf;
60 } memory_pointers;
61
62 static struct memory_pointers memory_list[BKTR_MEM_MAX_DEVICES];
63
64 /*************************************************************/
65
66 static int
67 bktr_mem_modevent(module_t mod, int type, void *unused){
68
69         switch (type) {
70         case MOD_LOAD:
71                 {
72                 printf("bktr_mem: memory holder loaded\n");
73 /*
74  * bzero causes a panic.
75                 bzero((caddr_t)memory_list, sizeof(memory_list));
76  * So use a simple for loop for now.
77 */
78                 {int x;
79                  unsigned char *d = (unsigned char *)memory_list;
80                  for (x=0; x< sizeof(memory_list); x++) {
81                   d[x]=0;
82                  }
83                 }
84                 return 0;
85                 }
86         case MOD_UNLOAD:
87                 {
88                 printf("bktr_mem: memory holder cannot be unloaded\n");
89                 return EBUSY;
90                 }
91         default:
92                 break;
93         }
94         return 0;
95 };
96
97 /*************************************************************/
98
99 int
100 bktr_has_stored_addresses(int unit) {
101
102         if ((unit < 0) || (unit >= BKTR_MEM_MAX_DEVICES)) {
103                 printf("bktr_mem: Unit number %d invalid\n",unit);
104                 return 0;
105         }
106
107         return memory_list[unit].addresses_stored;
108 }
109
110 /*************************************************************/
111
112 void
113 bktr_store_address(int unit, int type, vm_offset_t addr) {
114
115         if ((unit < 0) || (unit >= BKTR_MEM_MAX_DEVICES)) {
116                 printf("bktr_mem: Unit number %d invalid for memory type %d, address 0x%x\n"
117                        ,unit,type,addr);
118                 return;
119         }
120
121         switch (type) {
122                 case BKTR_MEM_DMA_PROG:     memory_list[unit].dma_prog = addr;
123                                             memory_list[unit].addresses_stored = 1;
124                                             break;
125                 case BKTR_MEM_ODD_DMA_PROG: memory_list[unit].odd_dma_prog = addr;
126                                             memory_list[unit].addresses_stored = 1;
127                                             break;
128                 case BKTR_MEM_VBIDATA:      memory_list[unit].vbidata = addr;
129                                             memory_list[unit].addresses_stored = 1;
130                                             break;
131                 case BKTR_MEM_VBIBUFFER:    memory_list[unit].vbibuffer = addr;
132                                             memory_list[unit].addresses_stored = 1;
133                                             break;
134                 case BKTR_MEM_BUF:          memory_list[unit].buf = addr;
135                                             memory_list[unit].addresses_stored = 1;
136                                             break;
137                 default:                    printf("bktr_mem: Invalid memory type %d for bktr%d, address 0x%xn",
138                                                    type,unit,addr);
139                                             break;
140         }
141 }
142
143 /*************************************************************/
144
145 vm_offset_t
146 bktr_retrieve_address(int unit, int type) {
147
148         if ((unit < 0) || (unit >= BKTR_MEM_MAX_DEVICES)) {
149                 printf("bktr_mem: Unit number %d too large for memory type %d\n",unit,type);
150                 return NULL;
151         }
152         switch (type) {
153                 case BKTR_MEM_DMA_PROG:     return memory_list[unit].dma_prog;
154                 case BKTR_MEM_ODD_DMA_PROG: return memory_list[unit].odd_dma_prog;
155                 case BKTR_MEM_VBIDATA:      return memory_list[unit].vbidata;
156                 case BKTR_MEM_VBIBUFFER:    return memory_list[unit].vbibuffer;
157                 case BKTR_MEM_BUF:          return memory_list[unit].buf;
158                 default:                    printf("bktr_mem: Invalid memory type %d for bktr%d",type,unit);
159                                             return NULL;
160         }
161 }
162
163 /*************************************************************/
164
165 static moduledata_t bktr_mem_mod = {
166         "bktr_mem",
167         bktr_mem_modevent,
168         0
169 };
170 /* The load order is First and module type is Driver to make sure bktr_mem
171    loads (and initialises) before bktr when both are loaded together */
172 DECLARE_MODULE(bktr_mem, bktr_mem_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
173 MODULE_VERSION(bktr_mem, 1);