]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/bus_space/C/lang.c
fd: refactor closefp in preparation for close_range rework
[FreeBSD/FreeBSD.git] / tools / bus_space / C / lang.c
1 /*-
2  * Copyright (c) 2014 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <errno.h>
32
33 #include "bus.h"
34 #include "busdma.h"
35 #include "libbus.h"
36
37 int16_t
38 bus_read_1(int rid, long ofs)
39 {
40         uint8_t val;
41
42         return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
43 }
44
45 int32_t
46 bus_read_2(int rid, long ofs)
47 {
48         uint16_t val;
49
50         return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int)val);
51 }
52
53 int64_t
54 bus_read_4(int rid, long ofs)
55 {
56         uint32_t val;
57
58         return ((!bs_read(rid, ofs, &val, sizeof(val))) ? -1 : (int64_t)val);
59 }
60
61 int
62 bus_write_1(int rid, long ofs, uint8_t val)
63 {
64
65         return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
66 }
67
68 int
69 bus_write_2(int rid, long ofs, uint16_t val)
70 {
71
72         return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
73 }
74
75 int
76 bus_write_4(int rid, long ofs, uint32_t val)
77 {
78
79         return ((!bs_write(rid, ofs, &val, sizeof(val))) ? errno : 0);
80 }
81
82 int
83 bus_map(const char *dev, const char *resource)
84 {
85
86         return (bs_map(dev, resource));
87 }
88
89 int
90 bus_unmap(int rid)
91 {
92
93         return ((!bs_unmap(rid)) ? errno : 0);
94 }
95
96 int
97 bus_subregion(int rid, long ofs, long sz)
98 {
99
100         return (bs_subregion(rid, ofs, sz));
101 }
102
103 int
104 busdma_tag_create(const char *dev, bus_addr_t align, bus_addr_t bndry,
105     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz,
106     u_int datarate, u_int flags, busdma_tag_t *out_p)
107 {
108         int res;
109
110         res = bd_tag_create(dev, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
111             datarate, flags);
112         if (res == -1)
113                 return (errno);
114         *out_p = res;
115         return (0);
116 }
117
118 int
119 busdma_tag_derive(busdma_tag_t tag, bus_addr_t align, bus_addr_t bndry,
120     bus_addr_t maxaddr, bus_size_t maxsz, u_int nsegs, bus_size_t maxsegsz, 
121     u_int datarate, u_int flags, busdma_tag_t *out_p)
122 {
123         int res;
124
125         res = bd_tag_derive(tag, align, bndry, maxaddr, maxsz, nsegs, maxsegsz,
126             datarate, flags);
127         if (res == -1)
128                 return (errno);
129         *out_p = res;
130         return (0);
131 }
132
133 int
134 busdma_tag_destroy(busdma_tag_t tag)
135 {
136
137         return (bd_tag_destroy(tag));
138 }
139
140 int
141 busdma_mem_alloc(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
142 {
143         int res;
144
145         res = bd_mem_alloc(tag, flags);
146         if (res == -1)
147                 return (errno);
148         *out_p = res;
149         return (0);
150 }
151
152 int
153 busdma_mem_free(busdma_md_t md)
154 {
155
156         return (bd_mem_free(md));
157 }
158
159 int
160 busdma_md_create(busdma_tag_t tag, u_int flags, busdma_md_t *out_p)
161 {
162         int res;
163
164         res = bd_md_create(tag, flags);
165         if (res == -1)
166                 return (errno);
167         *out_p = res;
168         return (0);
169 }
170
171 int
172 busdma_md_destroy(busdma_md_t md)
173 {
174
175         return (bd_md_destroy(md));
176 }
177
178 int
179 busdma_md_load(busdma_md_t md, void *buf, size_t len, u_int flags)
180 {
181
182         return (bd_md_load(md, buf, len, flags));
183 }
184
185 int
186 busdma_md_unload(busdma_md_t md)
187 {
188
189         return (bd_md_unload(md));
190 }
191
192 busdma_seg_t
193 busdma_md_first_seg(busdma_md_t md, int space)
194 {
195         busdma_seg_t seg;
196
197         seg = bd_md_first_seg(md, space);
198         return (seg);
199 }
200
201 busdma_seg_t
202 busdma_md_next_seg(busdma_md_t md, busdma_seg_t seg)
203 {
204  
205         seg = bd_md_next_seg(md, seg);
206         return (seg);
207 }
208
209 bus_addr_t
210 busdma_seg_get_addr(busdma_seg_t seg)
211 {
212         u_long addr;
213         int error;
214
215         error = bd_seg_get_addr(seg, &addr);
216         return ((error) ? ~0UL : addr);
217 }
218
219 bus_size_t
220 busdma_seg_get_size(busdma_seg_t seg)
221 {
222         u_long size;
223         int error;
224
225         error = bd_seg_get_size(seg, &size);
226         return ((error) ? ~0UL : size);
227 }
228
229 int
230 busdma_sync(busdma_md_t md, int op)
231 {
232
233         return (bd_sync(md, op, 0UL, ~0UL));
234 }
235
236 int
237 busdma_sync_range(busdma_md_t md, int op, bus_size_t ofs, bus_size_t len)
238 {
239
240         return (bd_sync(md, op, ofs, len));
241 }