xref: /freebsd/sys/kern/bus_if.m (revision c844066969af2d2e550d8e8499883302b361e863)
1b1bf6610SDoug Rabson#
2b1bf6610SDoug Rabson# Copyright (c) 1998 Doug Rabson
3b1bf6610SDoug Rabson# All rights reserved.
4b1bf6610SDoug Rabson#
5b1bf6610SDoug Rabson# Redistribution and use in source and binary forms, with or without
6b1bf6610SDoug Rabson# modification, are permitted provided that the following conditions
7b1bf6610SDoug Rabson# are met:
8b1bf6610SDoug Rabson# 1. Redistributions of source code must retain the above copyright
9b1bf6610SDoug Rabson#    notice, this list of conditions and the following disclaimer.
10b1bf6610SDoug Rabson# 2. Redistributions in binary form must reproduce the above copyright
11b1bf6610SDoug Rabson#    notice, this list of conditions and the following disclaimer in the
12b1bf6610SDoug Rabson#    documentation and/or other materials provided with the distribution.
13b1bf6610SDoug Rabson#
14b1bf6610SDoug Rabson# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15b1bf6610SDoug Rabson# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b1bf6610SDoug Rabson# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b1bf6610SDoug Rabson# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18b1bf6610SDoug Rabson# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b1bf6610SDoug Rabson# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b1bf6610SDoug Rabson# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b1bf6610SDoug Rabson# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b1bf6610SDoug Rabson# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b1bf6610SDoug Rabson# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b1bf6610SDoug Rabson# SUCH DAMAGE.
25b1bf6610SDoug Rabson#
26c3aac50fSPeter Wemm# $FreeBSD$
27b1bf6610SDoug Rabson#
28b1bf6610SDoug Rabson
29f7b77691SDoug Rabson#include <sys/bus.h>
30f7b77691SDoug Rabson
3131a7daaeSNicolas SouchuINTERFACE bus;
32b1bf6610SDoug Rabson
33b1bf6610SDoug Rabson#
348b2970bbSDoug Rabson# Default implementations of some methods.
358b2970bbSDoug Rabson#
368b2970bbSDoug RabsonCODE {
378b2970bbSDoug Rabson	static struct resource *
388b2970bbSDoug Rabson	null_alloc_resource(device_t dev, device_t child,
395878eb3fSWarner Losh	    int type, int *rid, u_long start, u_long end,
408b2970bbSDoug Rabson	    u_long count, u_int flags)
418b2970bbSDoug Rabson	{
425878eb3fSWarner Losh	    return (0);
438b2970bbSDoug Rabson	}
448b2970bbSDoug Rabson};
458b2970bbSDoug Rabson
468b2970bbSDoug Rabson#
47b1bf6610SDoug Rabson# This is called from system code which prints out a description of a
48b1bf6610SDoug Rabson# device.  It should describe the attachment that the child has with
49b1bf6610SDoug Rabson# the parent.  For instance the TurboLaser bus prints which node the
5015317dd8SMatthew N. Dodd# device is attached to.  See bus_generic_print_child.9 for more
5115317dd8SMatthew N. Dodd# information.
5215317dd8SMatthew N. Dodd# This method returns the number of characters output.
53b1bf6610SDoug Rabson#
5415317dd8SMatthew N. DoddMETHOD int print_child {
55b1bf6610SDoug Rabson	device_t dev;
56b1bf6610SDoug Rabson	device_t child;
57c8440669SMatthew N. Dodd} DEFAULT bus_generic_print_child;
58b1bf6610SDoug Rabson
59b1bf6610SDoug Rabson#
60ca7036d8SDoug Rabson# Called for each child device that
61ca7036d8SDoug Rabson# did not succeed in probing for a
62ca7036d8SDoug Rabson# driver.
63ca7036d8SDoug Rabson#
64ca7036d8SDoug RabsonMETHOD void probe_nomatch {
65ca7036d8SDoug Rabson        device_t dev;
66ca7036d8SDoug Rabson        device_t child;
67ca7036d8SDoug Rabson};
68ca7036d8SDoug Rabson
69ca7036d8SDoug Rabson#
70b1bf6610SDoug Rabson# These two methods manage a bus specific set of instance variables of
71b1bf6610SDoug Rabson# a child device.  The intention is that each different type of bus
72b1bf6610SDoug Rabson# defines a set of appropriate instance variables (such as ports and
73b1bf6610SDoug Rabson# irqs for ISA bus etc.)
74b1bf6610SDoug Rabson#
75b1bf6610SDoug Rabson# This information could be given to the child device as a struct but
76b1bf6610SDoug Rabson# that makes it hard for a bus to add or remove variables without
77b1bf6610SDoug Rabson# forcing an edit and recompile for all drivers which may not be
78b1bf6610SDoug Rabson# possible for vendor supplied binary drivers.
79b1bf6610SDoug Rabson
80b1bf6610SDoug Rabson#
81b1bf6610SDoug Rabson# Read an instance variable.  Return 0 on success.
82b1bf6610SDoug Rabson#
83b1bf6610SDoug RabsonMETHOD int read_ivar {
84bd418641SMark Murray	device_t _dev;
85bd418641SMark Murray	device_t _child;
86bd418641SMark Murray	int _indx;
87bd418641SMark Murray	uintptr_t *_result;
88b1bf6610SDoug Rabson};
89b1bf6610SDoug Rabson
90b1bf6610SDoug Rabson#
91b1bf6610SDoug Rabson# Write an instance variable.  Return 0 on success.
92b1bf6610SDoug Rabson#
93b1bf6610SDoug RabsonMETHOD int write_ivar {
94bd418641SMark Murray	device_t _dev;
95bd418641SMark Murray	device_t _child;
96bd418641SMark Murray	int _indx;
97bd418641SMark Murray	uintptr_t _value;
98b1bf6610SDoug Rabson};
99b1bf6610SDoug Rabson
100b1bf6610SDoug Rabson#
1016350e58aSDoug Rabson# Called after the child's DEVICE_DETACH method to allow the parent
1026350e58aSDoug Rabson# to reclaim any resources allocated on behalf of the child.
1036350e58aSDoug Rabson#
1046350e58aSDoug RabsonMETHOD void child_detached {
105bd418641SMark Murray	device_t _dev;
106bd418641SMark Murray	device_t _child;
1076350e58aSDoug Rabson};
1086350e58aSDoug Rabson
1096350e58aSDoug Rabson#
1106182fdbdSPeter Wemm# Called when a new driver is added to the devclass which owns this
1116182fdbdSPeter Wemm# bus. The generic implementation of this method attempts to probe and
1126182fdbdSPeter Wemm# attach any un-matched children of the bus.
1136182fdbdSPeter Wemm#
1146182fdbdSPeter WemmMETHOD void driver_added {
115bd418641SMark Murray	device_t _dev;
116bd418641SMark Murray	driver_t *_driver;
1176d4d0ac9SWarner Losh} DEFAULT bus_generic_driver_added;
1186182fdbdSPeter Wemm
1196182fdbdSPeter Wemm#
1206c2e3ddeSDoug Rabson# For busses which use use drivers supporting DEVICE_IDENTIFY to
1216c2e3ddeSDoug Rabson# enumerate their devices, these methods are used to create new
1226c2e3ddeSDoug Rabson# device instances. If place is non-NULL, the new device will be
123bea6af4dSDoug Rabson# added after the last existing child with the same order.
1246c2e3ddeSDoug Rabson#
1256c2e3ddeSDoug RabsonMETHOD device_t add_child {
126bd418641SMark Murray	device_t _dev;
127bd418641SMark Murray	int _order;
128bd418641SMark Murray	const char *_name;
129bd418641SMark Murray	int _unit;
1306c2e3ddeSDoug Rabson};
1316c2e3ddeSDoug Rabson
1326c2e3ddeSDoug Rabson#
13314177d72SGarrett Wollman# Allocate a system resource attached to `dev' on behalf of `child'.
13414177d72SGarrett Wollman# The types are defined in <machine/resource.h>; the meaning of the
13514177d72SGarrett Wollman# resource-ID field varies from bus to bus (but *rid == 0 is always
13614177d72SGarrett Wollman# valid if the resource type is).  start and end reflect the allowable
13714177d72SGarrett Wollman# range, and should be passed as `0UL' and `~0UL', respectively, if
13814177d72SGarrett Wollman# the client has no range restriction.  count is the number of consecutive
13914177d72SGarrett Wollman# indices in the resource required.  flags is a set of sharing flags
14014177d72SGarrett Wollman# as defined in <sys/rman.h>.
141b1bf6610SDoug Rabson#
14214177d72SGarrett Wollman# Returns a resource or a null pointer on failure.  The caller is
14314177d72SGarrett Wollman# responsible for calling rman_activate_resource() when it actually
14414177d72SGarrett Wollman# uses the resource.
14514177d72SGarrett Wollman#
14614177d72SGarrett WollmanMETHOD struct resource * alloc_resource {
147bd418641SMark Murray	device_t	_dev;
148bd418641SMark Murray	device_t	_child;
149bd418641SMark Murray	int		_type;
150bd418641SMark Murray	int	       *_rid;
151bd418641SMark Murray	u_long		_start;
152bd418641SMark Murray	u_long		_end;
153bd418641SMark Murray	u_long		_count;
154bd418641SMark Murray	u_int		_flags;
1558b2970bbSDoug Rabson} DEFAULT null_alloc_resource;
15614177d72SGarrett Wollman
15714177d72SGarrett WollmanMETHOD int activate_resource {
158bd418641SMark Murray	device_t	_dev;
159bd418641SMark Murray	device_t	_child;
160bd418641SMark Murray	int		_type;
161bd418641SMark Murray	int		_rid;
162bd418641SMark Murray	struct resource *_r;
16314177d72SGarrett Wollman};
16414177d72SGarrett Wollman
16514177d72SGarrett WollmanMETHOD int deactivate_resource {
166bd418641SMark Murray	device_t	_dev;
167bd418641SMark Murray	device_t	_child;
168bd418641SMark Murray	int		_type;
169bd418641SMark Murray	int		_rid;
170bd418641SMark Murray	struct resource *_r;
171b1bf6610SDoug Rabson};
17245c95fa1SDoug Rabson
1736e4fb915SDoug Rabson#
17414177d72SGarrett Wollman# Free a resource allocated by the preceding method.  The `rid' value
17514177d72SGarrett Wollman# must be the same as the one returned by BUS_ALLOC_RESOURCE (which
17614177d72SGarrett Wollman# is not necessarily the same as the one the client passed).
1776e4fb915SDoug Rabson#
17814177d72SGarrett WollmanMETHOD int release_resource {
179bd418641SMark Murray	device_t	_dev;
180bd418641SMark Murray	device_t	_child;
181bd418641SMark Murray	int		_type;
182bd418641SMark Murray	int		_rid;
183bd418641SMark Murray	struct resource *_res;
18414177d72SGarrett Wollman};
18514177d72SGarrett Wollman
18614177d72SGarrett WollmanMETHOD int setup_intr {
187bd418641SMark Murray	device_t	_dev;
188bd418641SMark Murray	device_t	_child;
189bd418641SMark Murray	struct resource *_irq;
190bd418641SMark Murray	int		_flags;
191bd418641SMark Murray	driver_intr_t	*_intr;
192bd418641SMark Murray	void		*_arg;
193bd418641SMark Murray	void		**_cookiep;
19414177d72SGarrett Wollman};
19514177d72SGarrett Wollman
19614177d72SGarrett WollmanMETHOD int teardown_intr {
197bd418641SMark Murray	device_t	_dev;
198bd418641SMark Murray	device_t	_child;
199bd418641SMark Murray	struct resource	*_irq;
200bd418641SMark Murray	void		*_cookie;
20145c95fa1SDoug Rabson};
20225afb89bSDoug Rabson
20325afb89bSDoug Rabson#
20425afb89bSDoug Rabson# Set the range used for a particular resource. Return EINVAL if
20525afb89bSDoug Rabson# the type or rid are out of range.
20625afb89bSDoug Rabson#
20725afb89bSDoug RabsonMETHOD int set_resource {
208bd418641SMark Murray	device_t	_dev;
209bd418641SMark Murray	device_t	_child;
210bd418641SMark Murray	int		_type;
211bd418641SMark Murray	int		_rid;
212bd418641SMark Murray	u_long		_start;
213bd418641SMark Murray	u_long		_count;
21425afb89bSDoug Rabson};
21525afb89bSDoug Rabson
21625afb89bSDoug Rabson#
21725afb89bSDoug Rabson# Get the range for a resource. Return ENOENT if the type or rid are
21825afb89bSDoug Rabson# out of range or have not been set.
21925afb89bSDoug Rabson#
22025afb89bSDoug RabsonMETHOD int get_resource {
221bd418641SMark Murray	device_t	_dev;
222bd418641SMark Murray	device_t	_child;
223bd418641SMark Murray	int		_type;
224bd418641SMark Murray	int		_rid;
225bd418641SMark Murray	u_long		*_startp;
226bd418641SMark Murray	u_long		*_countp;
22725afb89bSDoug Rabson};
22825afb89bSDoug Rabson
22925afb89bSDoug Rabson#
23025afb89bSDoug Rabson# Delete a resource.
23125afb89bSDoug Rabson#
23225afb89bSDoug RabsonMETHOD void delete_resource {
233bd418641SMark Murray	device_t	_dev;
234bd418641SMark Murray	device_t	_child;
235bd418641SMark Murray	int		_type;
236bd418641SMark Murray	int		_rid;
23725afb89bSDoug Rabson};
2380cb53e24SMatthew N. Dodd
2390cb53e24SMatthew N. Dodd#
2400cb53e24SMatthew N. Dodd# Return a struct resource_list.
2410cb53e24SMatthew N. Dodd#
24246aa504eSMatthew N. DoddMETHOD struct resource_list * get_resource_list {
243bd418641SMark Murray	device_t	_dev;
244bd418641SMark Murray	device_t	_child;
2450cb53e24SMatthew N. Dodd} DEFAULT bus_generic_get_resource_list;
2465878eb3fSWarner Losh
2475878eb3fSWarner Losh#
2485878eb3fSWarner Losh# Is the hardware described by _child still attached to the system?
2495878eb3fSWarner Losh#
25074014b7fSWarner Losh# This method should return 0 if the device is not present.  It should
25174014b7fSWarner Losh# return -1 if it is present.  Any errors in determining should be
25274014b7fSWarner Losh# returned as a normal errno value.  Client drivers are to assume that
25374014b7fSWarner Losh# the device is present, even if there is an error determining if it is
25474014b7fSWarner Losh# there.  Busses are to try to avoid returning errors, but newcard will return
25574014b7fSWarner Losh# an error if the device fails to implement this method.
25674014b7fSWarner Losh#
2575878eb3fSWarner LoshMETHOD int child_present {
2585878eb3fSWarner Losh	device_t	_dev;
2595878eb3fSWarner Losh	device_t	_child;
2605878eb3fSWarner Losh} DEFAULT bus_generic_child_present;
2613d9841b4SWarner Losh
2623d9841b4SWarner Losh#
2633d9841b4SWarner Losh# Returns the pnp info for this device.  Return it as a string.  If the
2643d9841b4SWarner Losh# string is insufficient for the storage, then return EOVERFLOW.
2653d9841b4SWarner Losh#
2663d9841b4SWarner LoshMETHOD int child_pnpinfo_str {
2673d9841b4SWarner Losh	device_t	_dev;
2683d9841b4SWarner Losh	device_t	_child;
2693d9841b4SWarner Losh	char		*_buf;
2703d9841b4SWarner Losh	size_t		_buflen;
2713d9841b4SWarner Losh};
2723d9841b4SWarner Losh
2733d9841b4SWarner Losh#
2743d9841b4SWarner Losh# Returns the location for this device.  Return it as a string.  If the
2753d9841b4SWarner Losh# string is insufficient for the storage, then return EOVERFLOW.
2763d9841b4SWarner Losh#
2773d9841b4SWarner LoshMETHOD int child_location_str {
2783d9841b4SWarner Losh	device_t	_dev;
2793d9841b4SWarner Losh	device_t	_child;
2803d9841b4SWarner Losh	char		*_buf;
2813d9841b4SWarner Losh	size_t		_buflen;
2823d9841b4SWarner Losh};
283