Home | History | Annotate | Line # | Download | only in ofw
openfirmio.c revision 1.11.92.1
      1  1.11.92.1     rmind /*	$NetBSD: openfirmio.c,v 1.11.92.1 2014/05/18 17:45:39 rmind Exp $ */
      2        1.1      matt 
      3        1.1      matt /*
      4        1.1      matt  * Copyright (c) 1992, 1993
      5        1.1      matt  *	The Regents of the University of California.  All rights reserved.
      6        1.1      matt  *
      7        1.1      matt  * This software was developed by the Computer Systems Engineering group
      8        1.1      matt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9        1.1      matt  * contributed to Berkeley.
     10        1.1      matt  *
     11        1.1      matt  * All advertising materials mentioning features or use of this software
     12        1.1      matt  * must display the following acknowledgement:
     13        1.1      matt  *	This product includes software developed by the University of
     14        1.1      matt  *	California, Lawrence Berkeley Laboratory.
     15        1.1      matt  *
     16        1.1      matt  * Redistribution and use in source and binary forms, with or without
     17        1.1      matt  * modification, are permitted provided that the following conditions
     18        1.1      matt  * are met:
     19        1.1      matt  * 1. Redistributions of source code must retain the above copyright
     20        1.1      matt  *    notice, this list of conditions and the following disclaimer.
     21        1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     22        1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     23        1.1      matt  *    documentation and/or other materials provided with the distribution.
     24        1.8       agc  * 3. Neither the name of the University nor the names of its contributors
     25        1.1      matt  *    may be used to endorse or promote products derived from this software
     26        1.1      matt  *    without specific prior written permission.
     27        1.1      matt  *
     28        1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29        1.1      matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30        1.1      matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31        1.1      matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32        1.1      matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33        1.1      matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34        1.1      matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35        1.1      matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36        1.1      matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37        1.1      matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38        1.1      matt  * SUCH DAMAGE.
     39        1.1      matt  *
     40        1.1      matt  *	@(#)openfirm.c	8.1 (Berkeley) 6/11/93
     41        1.1      matt  */
     42        1.3     lukem 
     43        1.3     lukem #include <sys/cdefs.h>
     44  1.11.92.1     rmind __KERNEL_RCSID(0, "$NetBSD: openfirmio.c,v 1.11.92.1 2014/05/18 17:45:39 rmind Exp $");
     45        1.1      matt 
     46        1.1      matt #include <sys/param.h>
     47        1.1      matt #include <sys/systm.h>
     48        1.1      matt #include <sys/errno.h>
     49        1.1      matt #include <sys/fcntl.h>
     50        1.1      matt #include <sys/ioctl.h>
     51        1.1      matt #include <sys/malloc.h>
     52        1.1      matt #include <sys/conf.h>
     53        1.1      matt #include <sys/device.h>
     54        1.5  jdolecek #include <sys/event.h>
     55        1.1      matt 
     56        1.1      matt #include <dev/ofw/openfirm.h>
     57        1.1      matt #include <dev/ofw/openfirmio.h>
     58        1.1      matt 
     59        1.1      matt static	int lastnode;			/* speed hack */
     60        1.1      matt 
     61        1.1      matt static int openfirmcheckid (int, int);
     62        1.1      matt static int openfirmgetstr (int, char *, char **);
     63        1.1      matt 
     64        1.1      matt void openfirmattach (int);
     65        1.1      matt 
     66        1.4   gehenna dev_type_ioctl(openfirmioctl);
     67        1.4   gehenna 
     68        1.4   gehenna const struct cdevsw openfirm_cdevsw = {
     69  1.11.92.1     rmind 	.d_open = nullopen,
     70  1.11.92.1     rmind 	.d_close = nullclose,
     71  1.11.92.1     rmind 	.d_read = noread,
     72  1.11.92.1     rmind 	.d_write = nowrite,
     73  1.11.92.1     rmind 	.d_ioctl = openfirmioctl,
     74  1.11.92.1     rmind 	.d_stop = nostop,
     75  1.11.92.1     rmind 	.d_tty = notty,
     76  1.11.92.1     rmind 	.d_poll = nopoll,
     77  1.11.92.1     rmind 	.d_mmap = nommap,
     78  1.11.92.1     rmind 	.d_kqfilter = nokqfilter,
     79  1.11.92.1     rmind 	.d_flag = 0
     80        1.4   gehenna };
     81        1.4   gehenna 
     82        1.1      matt void
     83        1.1      matt openfirmattach(int num)
     84        1.1      matt {
     85        1.1      matt 	/* nothing */
     86        1.1      matt }
     87        1.1      matt 
     88        1.1      matt /*
     89        1.1      matt  * Verify target ID is valid (exists in the OPENPROM tree), as
     90        1.1      matt  * listed from node ID sid forward.
     91        1.1      matt  */
     92        1.1      matt static int
     93        1.1      matt openfirmcheckid(int sid, int tid)
     94        1.1      matt {
     95        1.1      matt 
     96        1.1      matt 	for (; sid != 0; sid = OF_peer(sid))
     97        1.1      matt 		if (sid == tid || openfirmcheckid(OF_child(sid), tid))
     98        1.1      matt 			return (1);
     99        1.1      matt 
    100        1.1      matt 	return (0);
    101        1.1      matt }
    102        1.1      matt 
    103        1.1      matt static int
    104        1.1      matt openfirmgetstr(int len, char *user, char **cpp)
    105        1.1      matt {
    106        1.1      matt 	int error;
    107        1.1      matt 	char *cp;
    108        1.1      matt 
    109        1.1      matt 	/* Reject obvious bogus requests */
    110        1.1      matt 	if ((u_int)len > (8 * 1024) - 1)
    111        1.1      matt 		return (ENAMETOOLONG);
    112        1.1      matt 
    113        1.1      matt 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
    114        1.1      matt 	error = copyin(user, cp, len);
    115        1.1      matt 	cp[len] = '\0';
    116        1.1      matt 	return (error);
    117        1.1      matt }
    118        1.1      matt 
    119        1.1      matt int
    120       1.11  christos openfirmioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    121        1.1      matt {
    122        1.1      matt 	struct ofiocdesc *of;
    123        1.1      matt 	int node, len, ok, error, s;
    124        1.1      matt 	char *name, *value;
    125        1.1      matt 
    126        1.2      matt 	if (cmd == OFIOCGETOPTNODE) {
    127        1.2      matt 		s = splhigh();
    128        1.2      matt 		*(int *) data = OF_finddevice("/options");
    129        1.2      matt 		splx(s);
    130        1.2      matt 		return (0);
    131        1.2      matt 	}
    132        1.2      matt 
    133        1.1      matt 	/* Verify node id */
    134        1.1      matt 	of = (struct ofiocdesc *)data;
    135        1.1      matt 	node = of->of_nodeid;
    136        1.1      matt 	if (node != 0 && node != lastnode) {
    137        1.1      matt 		/* Not an easy one, must search for it */
    138        1.1      matt 		s = splhigh();
    139        1.1      matt 		ok = openfirmcheckid(OF_peer(0), node);
    140        1.1      matt 		splx(s);
    141        1.1      matt 		if (!ok)
    142        1.1      matt 			return (EINVAL);
    143        1.1      matt 		lastnode = node;
    144        1.1      matt 	}
    145        1.1      matt 
    146        1.1      matt 	name = value = NULL;
    147        1.1      matt 	error = 0;
    148        1.1      matt 	switch (cmd) {
    149        1.1      matt 
    150        1.1      matt 	case OFIOCGET:
    151        1.1      matt 		if ((flags & FREAD) == 0)
    152        1.1      matt 			return (EBADF);
    153        1.1      matt 		if (node == 0)
    154        1.1      matt 			return (EINVAL);
    155        1.1      matt 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    156        1.1      matt 		if (error)
    157        1.1      matt 			break;
    158        1.1      matt 		s = splhigh();
    159        1.1      matt 		len = OF_getproplen(node, name);
    160        1.1      matt 		splx(s);
    161        1.1      matt 		if (len > of->of_buflen) {
    162        1.1      matt 			error = ENOMEM;
    163        1.1      matt 			break;
    164        1.1      matt 		}
    165        1.1      matt 		of->of_buflen = len;
    166        1.1      matt 		/* -1 means no entry; 0 means no value */
    167        1.1      matt 		if (len <= 0)
    168        1.1      matt 			break;
    169        1.1      matt 		value = malloc(len, M_TEMP, M_WAITOK);
    170        1.1      matt 		if (value == NULL) {
    171        1.1      matt 			error = ENOMEM;
    172        1.1      matt 			break;
    173        1.1      matt 		}
    174        1.1      matt 		s = splhigh();
    175        1.1      matt 		len = OF_getprop(node, name, (void *)value, len);
    176        1.1      matt 		splx(s);
    177        1.1      matt 		error = copyout(value, of->of_buf, len);
    178        1.1      matt 		break;
    179        1.1      matt 
    180       1.10  macallan 
    181        1.1      matt 	case OFIOCSET:
    182        1.1      matt 		if ((flags & FWRITE) == 0)
    183        1.1      matt 			return (EBADF);
    184        1.1      matt 		if (node == 0)
    185        1.1      matt 			return (EINVAL);
    186        1.1      matt 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    187        1.1      matt 		if (error)
    188        1.1      matt 			break;
    189        1.1      matt 		error = openfirmgetstr(of->of_buflen, of->of_buf, &value);
    190        1.1      matt 		if (error)
    191        1.1      matt 			break;
    192        1.1      matt 		s = splhigh();
    193        1.1      matt 		len = OF_setprop(node, name, value, of->of_buflen + 1);
    194        1.1      matt 		splx(s);
    195       1.10  macallan 
    196       1.10  macallan 		/*
    197       1.10  macallan 		 * XXX
    198       1.10  macallan 		 * some OF implementations return the buffer length including
    199       1.10  macallan 		 * the trailing zero ( like macppc ) and some without ( like
    200       1.10  macallan 		 * FirmWorks OF used in Shark )
    201       1.10  macallan 		 */
    202       1.10  macallan 		if ((len != (of->of_buflen + 1)) && (len != of->of_buflen))
    203        1.1      matt 			error = EINVAL;
    204        1.1      matt 		break;
    205        1.1      matt 
    206        1.1      matt 	case OFIOCNEXTPROP: {
    207        1.2      matt 		char newname[32];
    208        1.1      matt 		if ((flags & FREAD) == 0)
    209        1.1      matt 			return (EBADF);
    210        1.1      matt 		if (node == 0)
    211        1.1      matt 			return (EINVAL);
    212        1.2      matt 		if (of->of_namelen != 0) {
    213        1.1      matt 			error = openfirmgetstr(of->of_namelen, of->of_name,
    214        1.1      matt 			    &name);
    215        1.1      matt 			if (error)
    216        1.1      matt 				break;
    217        1.1      matt 		}
    218        1.1      matt 		s = splhigh();
    219        1.1      matt 		ok = OF_nextprop(node, name, newname);
    220        1.1      matt 		splx(s);
    221        1.2      matt 		if (ok == 0) {
    222        1.2      matt 			error = ENOENT;
    223        1.2      matt 			break;
    224        1.2      matt 		}
    225        1.1      matt 		if (ok == -1) {
    226        1.1      matt 			error = EINVAL;
    227        1.1      matt 			break;
    228        1.1      matt 		}
    229        1.1      matt 		len = strlen(newname);
    230        1.1      matt 		if (len > of->of_buflen)
    231        1.1      matt 			len = of->of_buflen;
    232        1.1      matt 		else
    233        1.1      matt 			of->of_buflen = len;
    234        1.1      matt 		error = copyout(newname, of->of_buf, len);
    235        1.1      matt 		break;
    236        1.1      matt 	}
    237        1.1      matt 
    238        1.1      matt 	case OFIOCGETNEXT:
    239        1.1      matt 		if ((flags & FREAD) == 0)
    240        1.1      matt 			return (EBADF);
    241        1.1      matt 		s = splhigh();
    242        1.1      matt 		node = OF_peer(node);
    243        1.1      matt 		splx(s);
    244        1.1      matt 		*(int *)data = lastnode = node;
    245        1.1      matt 		break;
    246        1.1      matt 
    247        1.1      matt 	case OFIOCGETCHILD:
    248        1.1      matt 		if ((flags & FREAD) == 0)
    249        1.1      matt 			return (EBADF);
    250        1.1      matt 		if (node == 0)
    251        1.1      matt 			return (EINVAL);
    252        1.1      matt 		s = splhigh();
    253        1.1      matt 		node = OF_child(node);
    254        1.1      matt 		splx(s);
    255        1.1      matt 		*(int *)data = lastnode = node;
    256        1.1      matt 		break;
    257        1.1      matt 
    258        1.1      matt 	case OFIOCFINDDEVICE:
    259        1.1      matt 		if ((flags & FREAD) == 0)
    260        1.1      matt 			return (EBADF);
    261        1.1      matt 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    262        1.1      matt 		if (error)
    263        1.1      matt 			break;
    264        1.1      matt 		node = OF_finddevice(name);
    265        1.1      matt 		if (node == 0 || node == -1) {
    266        1.1      matt 			error = ENOENT;
    267        1.1      matt 			break;
    268        1.1      matt 		}
    269        1.2      matt 		of->of_nodeid = lastnode = node;
    270        1.1      matt 		break;
    271        1.1      matt 
    272        1.1      matt 	default:
    273        1.1      matt 		return (ENOTTY);
    274        1.1      matt 	}
    275        1.1      matt 
    276        1.1      matt 	if (name)
    277        1.1      matt 		free(name, M_TEMP);
    278        1.1      matt 	if (value)
    279        1.1      matt 		free(value, M_TEMP);
    280        1.1      matt 
    281        1.1      matt 	return (error);
    282        1.1      matt }
    283