bktr_mem.c revision 140883
1130561Sobrien/*
2130561Sobrien * This is part of the Driver for Video Capture Cards (Frame grabbers)
3130561Sobrien * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
4130561Sobrien * chipset.
5130561Sobrien * Copyright Roger Hardiman.
6130561Sobrien *
7130561Sobrien * bktr_mem : This kernel module allows us to keep our allocated
8130561Sobrien *            contiguous memory for the video buffer, DMA programs and VBI data
9130561Sobrien *            while the main bktr driver is unloaded and reloaded.
10130561Sobrien *            This avoids the problem of trying to allocate contiguous each
11130561Sobrien *            time the bktr driver is loaded.
12130561Sobrien */
13130561Sobrien
14130561Sobrien/*-
15130561Sobrien * 1. Redistributions of source code must retain the
16130561Sobrien * Copyright (c) 2000 Roger Hardiman
17130561Sobrien * All rights reserved.
18218822Sdim *
19130561Sobrien * Redistribution and use in source and binary forms, with or without
20130561Sobrien * modification, are permitted provided that the following conditions
21130561Sobrien * are met:
22130561Sobrien * 1. Redistributions of source code must retain the above copyright
23130561Sobrien *    notice, this list of conditions and the following disclaimer.
24130561Sobrien * 2. Redistributions in binary form must reproduce the above copyright
25130561Sobrien *    notice, this list of conditions and the following disclaimer in the
26130561Sobrien *    documentation and/or other materials provided with the distribution.
27130561Sobrien * 3. All advertising materials mentioning features or use of this software
28130561Sobrien *    must display the following acknowledgement:
29130561Sobrien *      This product includes software developed by Roger Hardiman
30130561Sobrien * 4. The name of the author may not be used to endorse or promote products
31130561Sobrien *    derived from this software without specific prior written permission.
32130561Sobrien *
33130561Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34130561Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35218822Sdim * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36130561Sobrien * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
37130561Sobrien * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38130561Sobrien * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39130561Sobrien * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40130561Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41130561Sobrien * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
42130561Sobrien * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43130561Sobrien * POSSIBILITY OF SUCH DAMAGE.
44130561Sobrien */
45130561Sobrien
46130561Sobrien#include <sys/cdefs.h>
47130561Sobrien__FBSDID("$FreeBSD: head/sys/dev/bktr/bktr_mem.c 140883 2005-01-27 01:40:12Z imp $");
48130561Sobrien
49130561Sobrien#include <sys/param.h>
50130561Sobrien#include <sys/kernel.h>
51130561Sobrien#include <sys/module.h>
52130561Sobrien#include <sys/systm.h>
53130561Sobrien#include <dev/bktr/bktr_mem.h>
54130561Sobrien
55130561Sobrienstruct memory_pointers {
56130561Sobrien	int		addresses_stored;
57130561Sobrien	vm_offset_t	dma_prog;
58130561Sobrien	vm_offset_t	odd_dma_prog;
59130561Sobrien	vm_offset_t	vbidata;
60130561Sobrien	vm_offset_t	vbibuffer;
61130561Sobrien	vm_offset_t	buf;
62130561Sobrien} memory_pointers;
63
64static struct memory_pointers memory_list[BKTR_MEM_MAX_DEVICES];
65
66/*************************************************************/
67
68static int
69bktr_mem_modevent(module_t mod, int type, void *unused){
70
71	switch (type) {
72	case MOD_LOAD:
73		printf("bktr_mem: memory holder loaded\n");
74		/*
75		 * bzero((caddr_t)memory_list, sizeof(memory_list));
76		 * causes a panic. So use a simple for loop for now.
77		 */
78		{
79			int x;
80			unsigned char *d;
81
82			d = (unsigned char *)memory_list;
83			for (x = 0; x < sizeof(memory_list); x++)
84				d[x] = 0;
85		}
86		return 0;
87	case MOD_UNLOAD:
88		printf("bktr_mem: memory holder cannot be unloaded\n");
89		return EBUSY;
90	default:
91		return EOPNOTSUPP;
92		break;
93	}
94	return (0);
95}
96
97/*************************************************************/
98
99int
100bktr_has_stored_addresses(int unit)
101{
102
103	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
104		printf("bktr_mem: Unit number %d invalid\n", unit);
105		return 0;
106	}
107
108	return memory_list[unit].addresses_stored;
109}
110
111/*************************************************************/
112
113void
114bktr_store_address(int unit, int type, vm_offset_t addr)
115{
116
117	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
118		printf("bktr_mem: Unit number %d invalid for memory type %d, address %p\n",
119		       unit, type, (void *) addr);
120		return;
121	}
122
123	switch (type) {
124	case BKTR_MEM_DMA_PROG:
125		memory_list[unit].dma_prog = addr;
126		memory_list[unit].addresses_stored = 1;
127		break;
128	case BKTR_MEM_ODD_DMA_PROG:
129		memory_list[unit].odd_dma_prog = addr;
130		memory_list[unit].addresses_stored = 1;
131		break;
132	case BKTR_MEM_VBIDATA:
133		memory_list[unit].vbidata = addr;
134		memory_list[unit].addresses_stored = 1;
135		break;
136	case BKTR_MEM_VBIBUFFER:
137		memory_list[unit].vbibuffer = addr;
138		memory_list[unit].addresses_stored = 1;
139		break;
140	case BKTR_MEM_BUF:
141		memory_list[unit].buf = addr;
142		memory_list[unit].addresses_stored = 1;
143		break;
144	default:
145		printf("bktr_mem: Invalid memory type %d for bktr%d, address %p\n",
146			type, unit, (void *)addr);
147		break;
148	}
149}
150
151/*************************************************************/
152
153vm_offset_t
154bktr_retrieve_address(int unit, int type)
155{
156
157	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
158		printf("bktr_mem: Unit number %d too large for memory type %d\n",
159			unit, type);
160		return (0);
161	}
162	switch (type) {
163	case BKTR_MEM_DMA_PROG:
164		return memory_list[unit].dma_prog;
165	case BKTR_MEM_ODD_DMA_PROG:
166		return memory_list[unit].odd_dma_prog;
167	case BKTR_MEM_VBIDATA:
168		return memory_list[unit].vbidata;
169	case BKTR_MEM_VBIBUFFER:
170		return memory_list[unit].vbibuffer;
171	case BKTR_MEM_BUF:
172		return memory_list[unit].buf;
173	default:
174		printf("bktr_mem: Invalid memory type %d for bktr%d",
175		       type, unit);
176		return (0);
177	}
178}
179
180/*************************************************************/
181
182static moduledata_t bktr_mem_mod = {
183	"bktr_mem",
184	bktr_mem_modevent,
185	0
186};
187
188/*
189 * The load order is First and module type is Driver to make sure bktr_mem
190 * loads (and initialises) before bktr when both are loaded together.
191 */
192DECLARE_MODULE(bktr_mem, bktr_mem_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
193MODULE_VERSION(bktr_mem, 1);
194