1/* $NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */
2
3/*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
--- 29 unchanged lines hidden (view full) ---
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)openfirm.c 8.1 (Berkeley) 6/11/93
45 *
46 * $FreeBSD: head/sys/dev/ofw/openfirmio.c 111815 2003-03-03 12:15:54Z phk $
47 */
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/conf.h>
52#include <sys/errno.h>
53#include <sys/fcntl.h>
54#include <sys/ioccom.h>
55#include <sys/kernel.h>
56#include <sys/malloc.h>
57#include <sys/module.h>
58
59#include <dev/ofw/openfirmio.h>
60
61static dev_t openfirm_dev;
62
63static d_ioctl_t openfirm_ioctl;
64
65#define CDEV_MAJOR 177
66#define OPENFIRM_MINOR 0
67
68static struct cdevsw openfirm_cdevsw = {
69 .d_open = nullopen,
70 .d_close = nullclose,
71 .d_ioctl = openfirm_ioctl,
72 .d_name = "openfirm",
73 .d_maj = CDEV_MAJOR,
74};
75
76static phandle_t lastnode; /* speed hack */
77
78static int openfirm_checkid(phandle_t, phandle_t);
79static int openfirm_getstr(int, const char *, char **);
80
81/* Maximum accepted name length. */
--- 37 unchanged lines hidden (view full) ---
119 struct thread *td)
120{
121 struct ofiocdesc *of;
122 phandle_t node;
123 int len, ok, error;
124 char *name, *value;
125 char newname[32];
126
127 of = (struct ofiocdesc *)data;
128 switch (cmd) {
129 case OFIOCGETOPTNODE:
130 *(phandle_t *) data = OF_finddevice("/options");
131 return (0);
132 case OFIOCGET:
133#if 0
134 case OFIOCSET:
135#endif
136 case OFIOCNEXTPROP:
137 case OFIOCFINDDEVICE:
138 node = of->of_nodeid;
139 break;
140 case OFIOCGETNEXT:
141 case OFIOCGETCHILD:
142 node = *(phandle_t *)data;
143 break;
144 default:
145 return (ENOTTY);
146 }
147
148 if (node != 0 && node != lastnode) {
149 /* Not an easy one, must search for it */
150 ok = openfirm_checkid(OF_peer(0), node);
151 if (!ok)
152 return (EINVAL);
153 lastnode = node;
154 }
155
156 name = value = NULL;
157 error = 0;
158 switch (cmd) {
159
160 case OFIOCGET:
161 if ((flags & FREAD) == 0)
162 return (EBADF);
163 if (node == 0)
164 return (EINVAL);
165 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
166 if (error)
167 break;
168 len = OF_getproplen(node, name);
169 if (len > of->of_buflen) {
170 error = ENOMEM;
171 break;
172 }
173 of->of_buflen = len;
174 /* -1 means no entry; 0 means no value */
175 if (len <= 0)
176 break;
--- 20 unchanged lines hidden (view full) ---
197 break;
198 len = OF_setprop(node, name, value, of->of_buflen);
199 if (len != of->of_buflen)
200 error = EINVAL;
201 break;
202#endif
203
204 case OFIOCNEXTPROP:
205 if ((flags & FREAD) == 0)
206 return (EBADF);
207 if (node == 0 || of->of_buflen < 0)
208 return (EINVAL);
209 if (of->of_namelen != 0) {
210 error = openfirm_getstr(of->of_namelen, of->of_name,
211 &name);
212 if (error)
213 break;
214 }
--- 10 unchanged lines hidden (view full) ---
225 if (len > of->of_buflen)
226 len = of->of_buflen;
227 else
228 of->of_buflen = len;
229 error = copyout(newname, of->of_buf, len);
230 break;
231
232 case OFIOCGETNEXT:
233 if ((flags & FREAD) == 0)
234 return (EBADF);
235 node = OF_peer(node);
236 *(phandle_t *)data = lastnode = node;
237 break;
238
239 case OFIOCGETCHILD:
240 if ((flags & FREAD) == 0)
241 return (EBADF);
242 if (node == 0)
243 return (EINVAL);
244 node = OF_child(node);
245 *(phandle_t *)data = lastnode = node;
246 break;
247
248 case OFIOCFINDDEVICE:
249 if ((flags & FREAD) == 0)
250 return (EBADF);
251 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
252 if (error)
253 break;
254 node = OF_finddevice(name);
255 if (node == 0 || node == -1) {
256 error = ENOENT;
257 break;
258 }
--- 41 unchanged lines hidden ---
2
3/*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
--- 29 unchanged lines hidden (view full) ---
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)openfirm.c 8.1 (Berkeley) 6/11/93
45 *
46 * $FreeBSD: head/sys/dev/ofw/openfirmio.c 111815 2003-03-03 12:15:54Z phk $
47 */
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/conf.h>
52#include <sys/errno.h>
53#include <sys/fcntl.h>
54#include <sys/ioccom.h>
55#include <sys/kernel.h>
56#include <sys/malloc.h>
57#include <sys/module.h>
58
59#include <dev/ofw/openfirmio.h>
60
61static dev_t openfirm_dev;
62
63static d_ioctl_t openfirm_ioctl;
64
65#define CDEV_MAJOR 177
66#define OPENFIRM_MINOR 0
67
68static struct cdevsw openfirm_cdevsw = {
69 .d_open = nullopen,
70 .d_close = nullclose,
71 .d_ioctl = openfirm_ioctl,
72 .d_name = "openfirm",
73 .d_maj = CDEV_MAJOR,
74};
75
76static phandle_t lastnode; /* speed hack */
77
78static int openfirm_checkid(phandle_t, phandle_t);
79static int openfirm_getstr(int, const char *, char **);
80
81/* Maximum accepted name length. */
--- 37 unchanged lines hidden (view full) ---
119 struct thread *td)
120{
121 struct ofiocdesc *of;
122 phandle_t node;
123 int len, ok, error;
124 char *name, *value;
125 char newname[32];
126
127 of = (struct ofiocdesc *)data;
128 switch (cmd) {
129 case OFIOCGETOPTNODE:
130 *(phandle_t *) data = OF_finddevice("/options");
131 return (0);
132 case OFIOCGET:
133#if 0
134 case OFIOCSET:
135#endif
136 case OFIOCNEXTPROP:
137 case OFIOCFINDDEVICE:
138 node = of->of_nodeid;
139 break;
140 case OFIOCGETNEXT:
141 case OFIOCGETCHILD:
142 node = *(phandle_t *)data;
143 break;
144 default:
145 return (ENOTTY);
146 }
147
148 if (node != 0 && node != lastnode) {
149 /* Not an easy one, must search for it */
150 ok = openfirm_checkid(OF_peer(0), node);
151 if (!ok)
152 return (EINVAL);
153 lastnode = node;
154 }
155
156 name = value = NULL;
157 error = 0;
158 switch (cmd) {
159
160 case OFIOCGET:
161 if ((flags & FREAD) == 0)
162 return (EBADF);
163 if (node == 0)
164 return (EINVAL);
165 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
166 if (error)
167 break;
168 len = OF_getproplen(node, name);
169 if (len > of->of_buflen) {
170 error = ENOMEM;
171 break;
172 }
173 of->of_buflen = len;
174 /* -1 means no entry; 0 means no value */
175 if (len <= 0)
176 break;
--- 20 unchanged lines hidden (view full) ---
197 break;
198 len = OF_setprop(node, name, value, of->of_buflen);
199 if (len != of->of_buflen)
200 error = EINVAL;
201 break;
202#endif
203
204 case OFIOCNEXTPROP:
205 if ((flags & FREAD) == 0)
206 return (EBADF);
207 if (node == 0 || of->of_buflen < 0)
208 return (EINVAL);
209 if (of->of_namelen != 0) {
210 error = openfirm_getstr(of->of_namelen, of->of_name,
211 &name);
212 if (error)
213 break;
214 }
--- 10 unchanged lines hidden (view full) ---
225 if (len > of->of_buflen)
226 len = of->of_buflen;
227 else
228 of->of_buflen = len;
229 error = copyout(newname, of->of_buf, len);
230 break;
231
232 case OFIOCGETNEXT:
233 if ((flags & FREAD) == 0)
234 return (EBADF);
235 node = OF_peer(node);
236 *(phandle_t *)data = lastnode = node;
237 break;
238
239 case OFIOCGETCHILD:
240 if ((flags & FREAD) == 0)
241 return (EBADF);
242 if (node == 0)
243 return (EINVAL);
244 node = OF_child(node);
245 *(phandle_t *)data = lastnode = node;
246 break;
247
248 case OFIOCFINDDEVICE:
249 if ((flags & FREAD) == 0)
250 return (EBADF);
251 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
252 if (error)
253 break;
254 node = OF_finddevice(name);
255 if (node == 0 || node == -1) {
256 error = ENOENT;
257 break;
258 }
--- 41 unchanged lines hidden ---