xref: /haiku/src/add-ons/kernel/drivers/dvb/cx23882/util.c (revision b06a48ab8f30b45916a9c157b992827779182163)
1 /*
2  * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify,
8  * merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <KernelExport.h>
26 #include <Errors.h>
27 #include <OS.h>
28 #include <string.h>
29 #include "util.h"
30 
31 #define TRACE_UTIL
32 #ifdef TRACE_UTIL
33   #define TRACE dprintf
34 #else
35   #define TRACE(a...)
36 #endif
37 
38 
39 area_id
40 map_mem(void **virt, void *phy, size_t size, uint32 protection,
41 	const char *name)
42 {
43 	uint32 offset;
44 	void *phyadr;
45 	void *mapadr;
46 	area_id area;
47 
48 	TRACE("mapping physical address %p with %ld bytes for %s\n", phy, size,
49 		name);
50 
51 	offset = (uint32)phy & (B_PAGE_SIZE - 1);
52 	phyadr = (char *)phy - offset;
53 	size = ROUNDUP(size + offset, B_PAGE_SIZE);
54 	area = map_physical_memory(name, phyadr, size, B_ANY_KERNEL_BLOCK_ADDRESS,
55 		protection, &mapadr);
56 	if (area < B_OK) {
57 		TRACE("mapping '%s' failed, error 0x%lx (%s)\n", name, area, strerror(area));
58 		return area;
59 	}
60 
61 	*virt = (char *)mapadr + offset;
62 
63 	TRACE("physical = %p, virtual = %p, offset = %ld, phyadr = %p, mapadr = %p, size = %ld, area = 0x%08lx\n",
64 		phy, *virt, offset, phyadr, mapadr, size, area);
65 
66 	return area;
67 }
68 
69 
70 area_id
71 alloc_mem(void **virt, void **phy, size_t size, uint32 protection,
72 	const char *name)
73 {
74 	physical_entry pe;
75 	void * virtadr;
76 	area_id areaid;
77 	status_t rv;
78 
79 	TRACE("allocating %ld bytes for %s\n", size, name);
80 
81 	size = ROUNDUP(size, B_PAGE_SIZE);
82 	areaid = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size,
83 		B_CONTIGUOUS, protection);
84 	if (areaid < B_OK) {
85 		TRACE("couldn't allocate area %s\n", name);
86 		return B_ERROR;
87 	}
88 	rv = get_memory_map(virtadr, size, &pe, 1);
89 	if (rv < B_OK) {
90 		delete_area(areaid);
91 		TRACE("couldn't map %s\n", name);
92 		return B_ERROR;
93 	}
94 	if (virt)
95 		*virt = virtadr;
96 	if (phy)
97 		*phy = pe.address;
98 	TRACE("area = %ld, size = %ld, virt = %p, phy = %p\n", areaid, size, virtadr, pe.address);
99 	return areaid;
100 }
101