xref: /haiku/src/add-ons/kernel/drivers/dvb/cx23882/util.c (revision b028e77473189065f2baefc6f5e10d451cf591e2)
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, const char *name)
41 {
42 	uint32 offset;
43 	void *phyadr;
44 	void *mapadr;
45 	area_id area;
46 
47 	TRACE("mapping physical address %p with %ld bytes for %s\n", phy, size, name);
48 
49 	offset = (uint32)phy & (B_PAGE_SIZE - 1);
50 	phyadr = (char *)phy - offset;
51 	size = ROUNDUP(size + offset, B_PAGE_SIZE);
52 	area = map_physical_memory(name, phyadr, size, B_ANY_KERNEL_BLOCK_ADDRESS, protection, &mapadr);
53 	if (area < B_OK) {
54 		TRACE("mapping '%s' failed, error 0x%lx (%s)\n", name, area, strerror(area));
55 		return area;
56 	}
57 
58 	*virt = (char *)mapadr + offset;
59 
60 	TRACE("physical = %p, virtual = %p, offset = %ld, phyadr = %p, mapadr = %p, size = %ld, area = 0x%08lx\n",
61 		phy, *virt, offset, phyadr, mapadr, size, area);
62 
63 	return area;
64 }
65 
66 
67 area_id
68 alloc_mem(void **virt, void **phy, size_t size, uint32 protection, const char *name)
69 {
70 	physical_entry pe;
71 	void * virtadr;
72 	area_id areaid;
73 	status_t rv;
74 
75 	TRACE("allocating %ld bytes for %s\n", size, name);
76 
77 	size = ROUNDUP(size, B_PAGE_SIZE);
78 	areaid = create_area(name, &virtadr, B_ANY_KERNEL_ADDRESS, size, B_FULL_LOCK | B_CONTIGUOUS, protection);
79 	if (areaid < B_OK) {
80 		TRACE("couldn't allocate area %s\n", name);
81 		return B_ERROR;
82 	}
83 	rv = get_memory_map(virtadr, size, &pe, 1);
84 	if (rv < B_OK) {
85 		delete_area(areaid);
86 		TRACE("couldn't map %s\n", name);
87 		return B_ERROR;
88 	}
89 	if (virt)
90 		*virt = virtadr;
91 	if (phy)
92 		*phy = pe.address;
93 	TRACE("area = %ld, size = %ld, virt = %p, phy = %p\n", areaid, size, virtadr, pe.address);
94 	return areaid;
95 }
96