ofw_bus_subr.c revision 152683
1152683Smarius/*- 2152683Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org> 3152683Smarius * All rights reserved. 4152683Smarius * 5152683Smarius * Redistribution and use in source and binary forms, with or without 6152683Smarius * modification, are permitted provided that the following conditions 7152683Smarius * are met: 8152683Smarius * 1. Redistributions of source code must retain the above copyright 9152683Smarius * notice, this list of conditions, and the following disclaimer, 10152683Smarius * without modification, immediately at the beginning of the file. 11152683Smarius * 2. Redistributions in binary form must reproduce the above copyright 12152683Smarius * notice, this list of conditions and the following disclaimer in 13152683Smarius * the documentation and/or other materials provided with the 14152683Smarius * distribution. 15152683Smarius * 16152683Smarius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17152683Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18152683Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19152683Smarius * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20152683Smarius * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21152683Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22152683Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23152683Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24152683Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25152683Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26152683Smarius * SUCH DAMAGE. 27152683Smarius */ 28152683Smarius 29152683Smarius#include <sys/cdefs.h> 30152683Smarius__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_bus_subr.c 152683 2005-11-22 16:37:45Z marius $"); 31152683Smarius 32152683Smarius#include <sys/param.h> 33152683Smarius#include <sys/systm.h> 34152683Smarius#include <sys/bus.h> 35152683Smarius#include <sys/errno.h> 36152683Smarius 37152683Smarius#include <dev/ofw/ofw_bus_subr.h> 38152683Smarius#include <dev/ofw/openfirm.h> 39152683Smarius 40152683Smarius#include "ofw_bus_if.h" 41152683Smarius 42152683Smariusint 43152683Smariusofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node) 44152683Smarius{ 45152683Smarius 46152683Smarius if (obd == NULL) 47152683Smarius return (ENOMEM); 48152683Smarius /* The 'name' property is considered mandatory. */ 49152683Smarius if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1) 50152683Smarius return (EINVAL); 51152683Smarius OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat); 52152683Smarius OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type); 53152683Smarius OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model); 54152683Smarius obd->obd_node = node; 55152683Smarius return (0); 56152683Smarius} 57152683Smarius 58152683Smariusvoid 59152683Smariusofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd) 60152683Smarius{ 61152683Smarius 62152683Smarius if (obd == NULL) 63152683Smarius return; 64152683Smarius if (obd->obd_compat != NULL) 65152683Smarius free(obd->obd_compat, M_OFWPROP); 66152683Smarius if (obd->obd_model != NULL) 67152683Smarius free(obd->obd_model, M_OFWPROP); 68152683Smarius if (obd->obd_name != NULL) 69152683Smarius free(obd->obd_name, M_OFWPROP); 70152683Smarius if (obd->obd_type != NULL) 71152683Smarius free(obd->obd_type, M_OFWPROP); 72152683Smarius} 73152683Smarius 74152683Smarius 75152683Smariusconst char * 76152683Smariusofw_bus_gen_get_compat(device_t bus, device_t dev) 77152683Smarius{ 78152683Smarius const struct ofw_bus_devinfo *obd; 79152683Smarius 80152683Smarius obd = OFW_BUS_GET_DEVINFO(bus, dev); 81152683Smarius if (obd == NULL) 82152683Smarius return (NULL); 83152683Smarius return (obd->obd_compat); 84152683Smarius} 85152683Smarius 86152683Smariusconst char * 87152683Smariusofw_bus_gen_get_model(device_t bus, device_t dev) 88152683Smarius{ 89152683Smarius const struct ofw_bus_devinfo *obd; 90152683Smarius 91152683Smarius obd = OFW_BUS_GET_DEVINFO(bus, dev); 92152683Smarius if (obd == NULL) 93152683Smarius return (NULL); 94152683Smarius return (obd->obd_model); 95152683Smarius} 96152683Smarius 97152683Smariusconst char * 98152683Smariusofw_bus_gen_get_name(device_t bus, device_t dev) 99152683Smarius{ 100152683Smarius const struct ofw_bus_devinfo *obd; 101152683Smarius 102152683Smarius obd = OFW_BUS_GET_DEVINFO(bus, dev); 103152683Smarius if (obd == NULL) 104152683Smarius return (NULL); 105152683Smarius return (obd->obd_name); 106152683Smarius} 107152683Smarius 108152683Smariusphandle_t 109152683Smariusofw_bus_gen_get_node(device_t bus, device_t dev) 110152683Smarius{ 111152683Smarius const struct ofw_bus_devinfo *obd; 112152683Smarius 113152683Smarius obd = OFW_BUS_GET_DEVINFO(bus, dev); 114152683Smarius if (obd == NULL) 115152683Smarius return (0); 116152683Smarius return (obd->obd_node); 117152683Smarius} 118152683Smarius 119152683Smariusconst char * 120152683Smariusofw_bus_gen_get_type(device_t bus, device_t dev) 121152683Smarius{ 122152683Smarius const struct ofw_bus_devinfo *obd; 123152683Smarius 124152683Smarius obd = OFW_BUS_GET_DEVINFO(bus, dev); 125152683Smarius if (obd == NULL) 126152683Smarius return (NULL); 127152683Smarius return (obd->obd_type); 128152683Smarius} 129