165692Sroger/*
2130356Sschweikh * This is part of the Driver for Video Capture Cards (Frame grabbers)
365692Sroger * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
465692Sroger * chipset.
565692Sroger * Copyright Roger Hardiman.
665692Sroger *
765692Sroger * bktr_mem : This kernel module allows us to keep our allocated
865692Sroger *            contiguous memory for the video buffer, DMA programs and VBI data
965692Sroger *            while the main bktr driver is unloaded and reloaded.
1065692Sroger *            This avoids the problem of trying to allocate contiguous each
1165692Sroger *            time the bktr driver is loaded.
1265692Sroger */
1365692Sroger
14139749Simp/*-
1565692Sroger * 1. Redistributions of source code must retain the
1665692Sroger * Copyright (c) 2000 Roger Hardiman
1765692Sroger * All rights reserved.
1865692Sroger *
1965692Sroger * Redistribution and use in source and binary forms, with or without
2065692Sroger * modification, are permitted provided that the following conditions
2165692Sroger * are met:
2265692Sroger * 1. Redistributions of source code must retain the above copyright
2365692Sroger *    notice, this list of conditions and the following disclaimer.
2465692Sroger * 2. Redistributions in binary form must reproduce the above copyright
2565692Sroger *    notice, this list of conditions and the following disclaimer in the
2665692Sroger *    documentation and/or other materials provided with the distribution.
2765692Sroger * 3. All advertising materials mentioning features or use of this software
2865692Sroger *    must display the following acknowledgement:
2965692Sroger *      This product includes software developed by Roger Hardiman
3065692Sroger * 4. The name of the author may not be used to endorse or promote products
3165692Sroger *    derived from this software without specific prior written permission.
3265692Sroger *
3365692Sroger * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
3465692Sroger * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3565692Sroger * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3665692Sroger * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
3765692Sroger * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3865692Sroger * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3965692Sroger * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4065692Sroger * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4165692Sroger * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
4265692Sroger * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4365692Sroger * POSSIBILITY OF SUCH DAMAGE.
4465692Sroger */
4565692Sroger
46130359Sschweikh#include <sys/cdefs.h>
47130359Sschweikh__FBSDID("$FreeBSD$");
4865692Sroger
4965692Sroger#include <sys/param.h>
5065692Sroger#include <sys/kernel.h>
51129879Sphk#include <sys/module.h>
5286903Srwatson#include <sys/systm.h>
5365692Sroger#include <dev/bktr/bktr_mem.h>
5465692Sroger
5565692Srogerstruct memory_pointers {
56130359Sschweikh	int		addresses_stored;
57130359Sschweikh	vm_offset_t	dma_prog;
58130359Sschweikh	vm_offset_t	odd_dma_prog;
59130359Sschweikh	vm_offset_t	vbidata;
60130359Sschweikh	vm_offset_t	vbibuffer;
61130359Sschweikh	vm_offset_t	buf;
6265692Sroger} memory_pointers;
6365692Sroger
6465692Srogerstatic struct memory_pointers memory_list[BKTR_MEM_MAX_DEVICES];
6565692Sroger
6665692Sroger/*************************************************************/
6765692Sroger
6865692Srogerstatic int
6965692Srogerbktr_mem_modevent(module_t mod, int type, void *unused){
7065692Sroger
7165692Sroger	switch (type) {
7265692Sroger	case MOD_LOAD:
73130359Sschweikh		printf("bktr_mem: memory holder loaded\n");
74130359Sschweikh		/*
75130359Sschweikh		 * bzero((caddr_t)memory_list, sizeof(memory_list));
76130359Sschweikh		 * causes a panic. So use a simple for loop for now.
77130359Sschweikh		 */
7865692Sroger		{
79130359Sschweikh			int x;
80130359Sschweikh			unsigned char *d;
81130359Sschweikh
82130359Sschweikh			d = (unsigned char *)memory_list;
83130359Sschweikh			for (x = 0; x < sizeof(memory_list); x++)
84130359Sschweikh				d[x] = 0;
8565692Sroger		}
8665692Sroger		return 0;
8765692Sroger	case MOD_UNLOAD:
8865743Sroger		printf("bktr_mem: memory holder cannot be unloaded\n");
8965743Sroger		return EBUSY;
9065692Sroger	default:
91132199Sphk		return EOPNOTSUPP;
9265692Sroger		break;
9365692Sroger	}
94130359Sschweikh	return (0);
95130356Sschweikh}
9665692Sroger
9765692Sroger/*************************************************************/
9865692Sroger
9965692Srogerint
100130359Sschweikhbktr_has_stored_addresses(int unit)
101130359Sschweikh{
10265692Sroger
103130359Sschweikh	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
104130359Sschweikh		printf("bktr_mem: Unit number %d invalid\n", unit);
10565692Sroger		return 0;
10665692Sroger	}
10765692Sroger
10865692Sroger	return memory_list[unit].addresses_stored;
10965692Sroger}
11065692Sroger
11165692Sroger/*************************************************************/
11265692Sroger
11365692Srogervoid
114130359Sschweikhbktr_store_address(int unit, int type, vm_offset_t addr)
115130359Sschweikh{
11665692Sroger
117130359Sschweikh	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
118140883Simp		printf("bktr_mem: Unit number %d invalid for memory type %d, address %p\n",
119140883Simp		       unit, type, (void *) addr);
12065692Sroger		return;
12165692Sroger	}
12265692Sroger
12365692Sroger	switch (type) {
124130359Sschweikh	case BKTR_MEM_DMA_PROG:
125130359Sschweikh		memory_list[unit].dma_prog = addr;
126130359Sschweikh		memory_list[unit].addresses_stored = 1;
127130359Sschweikh		break;
128130359Sschweikh	case BKTR_MEM_ODD_DMA_PROG:
129130359Sschweikh		memory_list[unit].odd_dma_prog = addr;
130130359Sschweikh		memory_list[unit].addresses_stored = 1;
131130359Sschweikh		break;
132130359Sschweikh	case BKTR_MEM_VBIDATA:
133130359Sschweikh		memory_list[unit].vbidata = addr;
134130359Sschweikh		memory_list[unit].addresses_stored = 1;
135130359Sschweikh		break;
136130359Sschweikh	case BKTR_MEM_VBIBUFFER:
137130359Sschweikh		memory_list[unit].vbibuffer = addr;
138130359Sschweikh		memory_list[unit].addresses_stored = 1;
139130359Sschweikh		break;
140130359Sschweikh	case BKTR_MEM_BUF:
141130359Sschweikh		memory_list[unit].buf = addr;
142130359Sschweikh		memory_list[unit].addresses_stored = 1;
143130359Sschweikh		break;
144130359Sschweikh	default:
145140883Simp		printf("bktr_mem: Invalid memory type %d for bktr%d, address %p\n",
146140883Simp			type, unit, (void *)addr);
147130359Sschweikh		break;
14865692Sroger	}
14965692Sroger}
15065692Sroger
15165692Sroger/*************************************************************/
15265692Sroger
15365692Srogervm_offset_t
154130359Sschweikhbktr_retrieve_address(int unit, int type)
155130359Sschweikh{
15665692Sroger
157130359Sschweikh	if (unit < 0 || unit >= BKTR_MEM_MAX_DEVICES) {
158130359Sschweikh		printf("bktr_mem: Unit number %d too large for memory type %d\n",
159130359Sschweikh			unit, type);
160130359Sschweikh		return (0);
16165692Sroger	}
16265692Sroger	switch (type) {
163130359Sschweikh	case BKTR_MEM_DMA_PROG:
164130359Sschweikh		return memory_list[unit].dma_prog;
165130359Sschweikh	case BKTR_MEM_ODD_DMA_PROG:
166130359Sschweikh		return memory_list[unit].odd_dma_prog;
167130359Sschweikh	case BKTR_MEM_VBIDATA:
168130359Sschweikh		return memory_list[unit].vbidata;
169130359Sschweikh	case BKTR_MEM_VBIBUFFER:
170130359Sschweikh		return memory_list[unit].vbibuffer;
171130359Sschweikh	case BKTR_MEM_BUF:
172130359Sschweikh		return memory_list[unit].buf;
173130359Sschweikh	default:
174130359Sschweikh		printf("bktr_mem: Invalid memory type %d for bktr%d",
175130359Sschweikh		       type, unit);
176130359Sschweikh		return (0);
17765692Sroger	}
17865692Sroger}
17965692Sroger
18065692Sroger/*************************************************************/
18165692Sroger
18265692Srogerstatic moduledata_t bktr_mem_mod = {
183130359Sschweikh	"bktr_mem",
184130359Sschweikh	bktr_mem_modevent,
185241394Skevlo	0
18665692Sroger};
187130359Sschweikh
188130359Sschweikh/*
189130359Sschweikh * The load order is First and module type is Driver to make sure bktr_mem
190130359Sschweikh * loads (and initialises) before bktr when both are loaded together.
191130359Sschweikh */
19265743SrogerDECLARE_MODULE(bktr_mem, bktr_mem_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
19365728SrogerMODULE_VERSION(bktr_mem, 1);
194