bus_if.m revision 112588
136973Sdfr#
236973Sdfr# Copyright (c) 1998 Doug Rabson
336973Sdfr# All rights reserved.
436973Sdfr#
536973Sdfr# Redistribution and use in source and binary forms, with or without
636973Sdfr# modification, are permitted provided that the following conditions
736973Sdfr# are met:
836973Sdfr# 1. Redistributions of source code must retain the above copyright
936973Sdfr#    notice, this list of conditions and the following disclaimer.
1036973Sdfr# 2. Redistributions in binary form must reproduce the above copyright
1136973Sdfr#    notice, this list of conditions and the following disclaimer in the
1236973Sdfr#    documentation and/or other materials provided with the distribution.
1336973Sdfr#
1436973Sdfr# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1536973Sdfr# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1636973Sdfr# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1736973Sdfr# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1836973Sdfr# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1936973Sdfr# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2036973Sdfr# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2136973Sdfr# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2236973Sdfr# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2336973Sdfr# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2436973Sdfr# SUCH DAMAGE.
2536973Sdfr#
2650477Speter# $FreeBSD: head/sys/kern/bus_if.m 112588 2003-03-25 04:32:52Z mdodd $
2736973Sdfr#
2836973Sdfr
2959093Sdfr#include <sys/bus.h>
3059093Sdfr
3141017SnsouchINTERFACE bus;
3236973Sdfr
3336973Sdfr#
3446913Sdfr# Default implementations of some methods.
3546913Sdfr#
3646913SdfrCODE {
3746913Sdfr	static struct resource *
3846913Sdfr	null_alloc_resource(device_t dev, device_t child,
39100421Simp	    int type, int *rid, u_long start, u_long end,
40100421Simp	    u_long count, u_int flags)
4146913Sdfr	{
42100421Simp	    return (0);
4346913Sdfr	}
4446913Sdfr};
4546913Sdfr
4646913Sdfr#
4736973Sdfr# This is called from system code which prints out a description of a
4836973Sdfr# device.  It should describe the attachment that the child has with
4936973Sdfr# the parent.  For instance the TurboLaser bus prints which node the
5049195Smdodd# device is attached to.  See bus_generic_print_child.9 for more 
5149195Smdodd# information.
5249195Smdodd# This method returns the number of characters output.
5336973Sdfr#
5449195SmdoddMETHOD int print_child {
5536973Sdfr	device_t dev;
5636973Sdfr	device_t child;
57112588Smdodd} DEFAULT bus_generic_print_child;
5836973Sdfr
5948754Sdfr# 
6048754Sdfr# Called for each child device that 
6148754Sdfr# did not succeed in probing for a
6248754Sdfr# driver.
6348754Sdfr#    
6448754SdfrMETHOD void probe_nomatch {
6548754Sdfr        device_t dev;
6648754Sdfr        device_t child;
6748754Sdfr};
6848754Sdfr
6936973Sdfr#
7036973Sdfr# These two methods manage a bus specific set of instance variables of
7136973Sdfr# a child device.  The intention is that each different type of bus
7236973Sdfr# defines a set of appropriate instance variables (such as ports and
7336973Sdfr# irqs for ISA bus etc.)
7436973Sdfr#
7536973Sdfr# This information could be given to the child device as a struct but
7636973Sdfr# that makes it hard for a bus to add or remove variables without
7736973Sdfr# forcing an edit and recompile for all drivers which may not be
7836973Sdfr# possible for vendor supplied binary drivers.
7936973Sdfr
8036973Sdfr#
8136973Sdfr# Read an instance variable.  Return 0 on success.
8236973Sdfr#
8336973SdfrMETHOD int read_ivar {
8495201Smarkm	device_t _dev;
8595201Smarkm	device_t _child;
8695201Smarkm	int _indx;
8795201Smarkm	uintptr_t *_result;
8836973Sdfr};
8936973Sdfr
9036973Sdfr#
9136973Sdfr# Write an instance variable.  Return 0 on success.
9236973Sdfr#
9336973SdfrMETHOD int write_ivar {
9495201Smarkm	device_t _dev;
9595201Smarkm	device_t _child;
9695201Smarkm	int _indx;
9795201Smarkm	uintptr_t _value;
9836973Sdfr};
9936973Sdfr
10036973Sdfr#
10145107Sdfr# Called after the child's DEVICE_DETACH method to allow the parent
10245107Sdfr# to reclaim any resources allocated on behalf of the child.
10345107Sdfr#
10445107SdfrMETHOD void child_detached {
10595201Smarkm	device_t _dev;
10695201Smarkm	device_t _child;
10745107Sdfr};
10845107Sdfr
10945107Sdfr#
11045720Speter# Called when a new driver is added to the devclass which owns this
11145720Speter# bus. The generic implementation of this method attempts to probe and
11245720Speter# attach any un-matched children of the bus.
11345720Speter#
11445720SpeterMETHOD void driver_added {
11595201Smarkm	device_t _dev;
11695201Smarkm	driver_t *_driver;
11752045Simp} DEFAULT bus_generic_driver_added;
11845720Speter
11945720Speter#
12047178Sdfr# For busses which use use drivers supporting DEVICE_IDENTIFY to
12147178Sdfr# enumerate their devices, these methods are used to create new
12247178Sdfr# device instances. If place is non-NULL, the new device will be
12347578Sdfr# added after the last existing child with the same order.
12447178Sdfr#
12547178SdfrMETHOD device_t add_child {
12695201Smarkm	device_t _dev;
12795201Smarkm	int _order;
12895201Smarkm	const char *_name;
12995201Smarkm	int _unit;
13047178Sdfr};
13147178Sdfr
13247178Sdfr#
13341153Swollman# Allocate a system resource attached to `dev' on behalf of `child'.
13441153Swollman# The types are defined in <machine/resource.h>; the meaning of the
13541153Swollman# resource-ID field varies from bus to bus (but *rid == 0 is always
13641153Swollman# valid if the resource type is).  start and end reflect the allowable
13741153Swollman# range, and should be passed as `0UL' and `~0UL', respectively, if
13841153Swollman# the client has no range restriction.  count is the number of consecutive
13941153Swollman# indices in the resource required.  flags is a set of sharing flags
14041153Swollman# as defined in <sys/rman.h>.
14136973Sdfr#
14241153Swollman# Returns a resource or a null pointer on failure.  The caller is
14341153Swollman# responsible for calling rman_activate_resource() when it actually
14441153Swollman# uses the resource.
14541153Swollman#
14641153SwollmanMETHOD struct resource * alloc_resource {
14795201Smarkm	device_t	_dev;
14895201Smarkm	device_t	_child;
14995201Smarkm	int		_type;
15095201Smarkm	int	       *_rid;
15195201Smarkm	u_long		_start;
15295201Smarkm	u_long		_end;
15395201Smarkm	u_long		_count;
15495201Smarkm	u_int		_flags;
15546913Sdfr} DEFAULT null_alloc_resource;
15637592Sdfr
15741153SwollmanMETHOD int activate_resource {
15895201Smarkm	device_t	_dev;
15995201Smarkm	device_t	_child;
16095201Smarkm	int		_type;
16195201Smarkm	int		_rid;
16295201Smarkm	struct resource *_r;
16341153Swollman};
16441153Swollman
16541153SwollmanMETHOD int deactivate_resource {
16695201Smarkm	device_t	_dev;
16795201Smarkm	device_t	_child;
16895201Smarkm	int		_type;
16995201Smarkm	int		_rid;
17095201Smarkm	struct resource *_r;
17141153Swollman};
17241153Swollman
17339344Sdfr#
17441153Swollman# Free a resource allocated by the preceding method.  The `rid' value
17541153Swollman# must be the same as the one returned by BUS_ALLOC_RESOURCE (which
17641153Swollman# is not necessarily the same as the one the client passed).
17739344Sdfr#
17841153SwollmanMETHOD int release_resource {
17995201Smarkm	device_t	_dev;
18095201Smarkm	device_t	_child;
18195201Smarkm	int		_type;
18295201Smarkm	int		_rid;
18395201Smarkm	struct resource *_res;
18437592Sdfr};
18541153Swollman
18641153SwollmanMETHOD int setup_intr {
18795201Smarkm	device_t	_dev;
18895201Smarkm	device_t	_child;
18995201Smarkm	struct resource *_irq;
19095201Smarkm	int		_flags;
19195201Smarkm	driver_intr_t	*_intr;
19295201Smarkm	void		*_arg;
19395201Smarkm	void		**_cookiep;
19441153Swollman};
19541153Swollman
19641153SwollmanMETHOD int teardown_intr {
19795201Smarkm	device_t	_dev;
19895201Smarkm	device_t	_child;
19995201Smarkm	struct resource	*_irq;
20095201Smarkm	void		*_cookie;
20141153Swollman};
20252174Sdfr
20352174Sdfr#
20452174Sdfr# Set the range used for a particular resource. Return EINVAL if
20552174Sdfr# the type or rid are out of range.
20652174Sdfr#
20752174SdfrMETHOD int set_resource {
20895201Smarkm	device_t	_dev;
20995201Smarkm	device_t	_child;
21095201Smarkm	int		_type;
21195201Smarkm	int		_rid;
21295201Smarkm	u_long		_start;
21395201Smarkm	u_long		_count;
21452174Sdfr};
21552174Sdfr
21652174Sdfr#
21752174Sdfr# Get the range for a resource. Return ENOENT if the type or rid are
21852174Sdfr# out of range or have not been set.
21952174Sdfr#
22052174SdfrMETHOD int get_resource {
22195201Smarkm	device_t	_dev;
22295201Smarkm	device_t	_child;
22395201Smarkm	int		_type;
22495201Smarkm	int		_rid;
22595201Smarkm	u_long		*_startp;
22695201Smarkm	u_long		*_countp;
22752174Sdfr};
22852174Sdfr
22952174Sdfr#
23052174Sdfr# Delete a resource.
23152174Sdfr#
23252174SdfrMETHOD void delete_resource {
23395201Smarkm	device_t	_dev;
23495201Smarkm	device_t	_child;
23595201Smarkm	int		_type;
23695201Smarkm	int		_rid;
23752174Sdfr};
23867278Smdodd
23967278Smdodd#
24067278Smdodd# Return a struct resource_list.
24167278Smdodd#
24269294SmdoddMETHOD struct resource_list * get_resource_list {
24395201Smarkm	device_t	_dev;
24495201Smarkm	device_t	_child;
24567278Smdodd} DEFAULT bus_generic_get_resource_list;
246100421Simp
247100421Simp#
248100421Simp# Is the hardware described by _child still attached to the system?
249100421Simp#
250103209Simp# This method should return 0 if the device is not present.  It should
251103209Simp# return -1 if it is present.  Any errors in determining should be
252103209Simp# returned as a normal errno value.  Client drivers are to assume that
253103209Simp# the device is present, even if there is an error determining if it is
254103209Simp# there.  Busses are to try to avoid returning errors, but newcard will return
255103209Simp# an error if the device fails to implement this method.
256103209Simp#
257100421SimpMETHOD int child_present {
258100421Simp	device_t	_dev;
259100421Simp	device_t	_child;
260100421Simp} DEFAULT bus_generic_child_present;
261104597Simp
262104597Simp#
263104597Simp# Returns the pnp info for this device.  Return it as a string.  If the
264104597Simp# string is insufficient for the storage, then return EOVERFLOW.
265104597Simp#
266104597SimpMETHOD int child_pnpinfo_str {
267104597Simp	device_t	_dev;
268104597Simp	device_t	_child;
269104597Simp	char		*_buf;
270104597Simp	size_t		_buflen;
271104597Simp};
272104597Simp
273104597Simp#
274104597Simp# Returns the location for this device.  Return it as a string.  If the
275104597Simp# string is insufficient for the storage, then return EOVERFLOW.
276104597Simp#
277104597SimpMETHOD int child_location_str {
278104597Simp	device_t	_dev;
279104597Simp	device_t	_child;
280104597Simp	char		*_buf;
281104597Simp	size_t		_buflen;
282104597Simp};
283