xref: /freebsd/sys/kern/bus_if.m (revision 9a14aa017b21c292740c00ee098195cd46642730)
1#-
2# Copyright (c) 1998-2004 Doug Rabson
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27#
28
29#include <sys/types.h>
30#include <sys/systm.h>
31#include <sys/bus.h>
32
33/**
34 * @defgroup BUS bus - KObj methods for drivers of devices with children
35 * @brief A set of methods required device drivers that support
36 * child devices.
37 * @{
38 */
39INTERFACE bus;
40
41#
42# Default implementations of some methods.
43#
44CODE {
45	static struct resource *
46	null_alloc_resource(device_t dev, device_t child,
47	    int type, int *rid, u_long start, u_long end,
48	    u_long count, u_int flags)
49	{
50	    return (0);
51	}
52
53	static int
54	null_remap_intr(device_t bus, device_t dev, u_int irq)
55	{
56
57		if (dev != NULL)
58			return (BUS_REMAP_INTR(dev, NULL, irq));
59		return (ENXIO);
60	}
61
62	static device_t
63	null_add_child(device_t bus, int order, const char *name,
64	    int unit)
65	{
66
67		panic("bus_add_child is not implemented");
68	}
69};
70
71/**
72 * @brief Print a description of a child device
73 *
74 * This is called from system code which prints out a description of a
75 * device. It should describe the attachment that the child has with
76 * the parent. For instance the TurboLaser bus prints which node the
77 * device is attached to. See bus_generic_print_child() for more
78 * information.
79 *
80 * @param _dev		the device whose child is being printed
81 * @param _child	the child device to describe
82 *
83 * @returns		the number of characters output.
84 */
85METHOD int print_child {
86	device_t _dev;
87	device_t _child;
88} DEFAULT bus_generic_print_child;
89
90/**
91 * @brief Print a notification about an unprobed child device.
92 *
93 * Called for each child device that did not succeed in probing for a
94 * driver.
95 *
96 * @param _dev		the device whose child was being probed
97 * @param _child	the child device which failed to probe
98 */
99METHOD void probe_nomatch {
100        device_t _dev;
101        device_t _child;
102};
103
104/**
105 * @brief Read the value of a bus-specific attribute of a device
106 *
107 * This method, along with BUS_WRITE_IVAR() manages a bus-specific set
108 * of instance variables of a child device.  The intention is that
109 * each different type of bus defines a set of appropriate instance
110 * variables (such as ports and irqs for ISA bus etc.)
111 *
112 * This information could be given to the child device as a struct but
113 * that makes it hard for a bus to add or remove variables without
114 * forcing an edit and recompile for all drivers which may not be
115 * possible for vendor supplied binary drivers.
116 *
117 * This method copies the value of an instance variable to the
118 * location specified by @p *_result.
119 *
120 * @param _dev		the device whose child was being examined
121 * @param _child	the child device whose instance variable is
122 *			being read
123 * @param _index	the instance variable to read
124 * @param _result	a loction to recieve the instance variable
125 *			value
126 *
127 * @retval 0		success
128 * @retval ENOENT	no such instance variable is supported by @p
129 *			_dev
130 */
131METHOD int read_ivar {
132	device_t _dev;
133	device_t _child;
134	int _index;
135	uintptr_t *_result;
136};
137
138/**
139 * @brief Write the value of a bus-specific attribute of a device
140 *
141 * This method sets the value of an instance variable to @p _value.
142 *
143 * @param _dev		the device whose child was being updated
144 * @param _child	the child device whose instance variable is
145 *			being written
146 * @param _index	the instance variable to write
147 * @param _value	the value to write to that instance variable
148 *
149 * @retval 0		success
150 * @retval ENOENT	no such instance variable is supported by @p
151 *			_dev
152 * @retval EINVAL	the instance variable was recognised but
153 *			contains a read-only value
154 */
155METHOD int write_ivar {
156	device_t _dev;
157	device_t _child;
158	int _indx;
159	uintptr_t _value;
160};
161
162/**
163 * @brief Notify a bus that a child was detached
164 *
165 * Called after the child's DEVICE_DETACH() method to allow the parent
166 * to reclaim any resources allocated on behalf of the child.
167 *
168 * @param _dev		the device whose child changed state
169 * @param _child	the child device which changed state
170 */
171METHOD void child_detached {
172	device_t _dev;
173	device_t _child;
174};
175
176/**
177 * @brief Notify a bus that a new driver was added
178 *
179 * Called when a new driver is added to the devclass which owns this
180 * bus. The generic implementation of this method attempts to probe and
181 * attach any un-matched children of the bus.
182 *
183 * @param _dev		the device whose devclass had a new driver
184 *			added to it
185 * @param _driver	the new driver which was added
186 */
187METHOD void driver_added {
188	device_t _dev;
189	driver_t *_driver;
190} DEFAULT bus_generic_driver_added;
191
192/**
193 * @brief Create a new child device
194 *
195 * For busses which use use drivers supporting DEVICE_IDENTIFY() to
196 * enumerate their devices, this method is used to create new
197 * device instances. The new device will be added after the last
198 * existing child with the same order.
199 *
200 * @param _dev		the bus device which will be the parent of the
201 *			new child device
202 * @param _order	a value which is used to partially sort the
203 *			children of @p _dev - devices created using
204 *			lower values of @p _order appear first in @p
205 *			_dev's list of children
206 * @param _name		devclass name for new device or @c NULL if not
207 *			specified
208 * @param _unit		unit number for new device or @c -1 if not
209 *			specified
210 */
211METHOD device_t add_child {
212	device_t _dev;
213	u_int _order;
214	const char *_name;
215	int _unit;
216} DEFAULT null_add_child;
217
218/**
219 * @brief Allocate a system resource
220 *
221 * This method is called by child devices of a bus to allocate resources.
222 * The types are defined in <machine/resource.h>; the meaning of the
223 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
224 * valid if the resource type is). If a resource was allocated and the
225 * caller did not use the RF_ACTIVE to specify that it should be
226 * activated immediately, the caller is responsible for calling
227 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
228 *
229 * @param _dev		the parent device of @p _child
230 * @param _child	the device which is requesting an allocation
231 * @param _type		the type of resource to allocate
232 * @param _rid		a pointer to the resource identifier
233 * @param _start	hint at the start of the resource range - pass
234 *			@c 0UL for any start address
235 * @param _end		hint at the end of the resource range - pass
236 *			@c ~0UL for any end address
237 * @param _count	hint at the size of range required - pass @c 1
238 *			for any size
239 * @param _flags	any extra flags to control the resource
240 *			allocation - see @c RF_XXX flags in
241 *			<sys/rman.h> for details
242 *
243 * @returns		the resource which was allocated or @c NULL if no
244 *			resource could be allocated
245 */
246METHOD struct resource * alloc_resource {
247	device_t	_dev;
248	device_t	_child;
249	int		_type;
250	int	       *_rid;
251	u_long		_start;
252	u_long		_end;
253	u_long		_count;
254	u_int		_flags;
255} DEFAULT null_alloc_resource;
256
257/**
258 * @brief Activate a resource
259 *
260 * Activate a resource previously allocated with
261 * BUS_ALLOC_RESOURCE(). This may for instance map a memory region
262 * into the kernel's virtual address space.
263 *
264 * @param _dev		the parent device of @p _child
265 * @param _child	the device which allocated the resource
266 * @param _type		the type of resource
267 * @param _rid		the resource identifier
268 * @param _r		the resource to activate
269 */
270METHOD int activate_resource {
271	device_t	_dev;
272	device_t	_child;
273	int		_type;
274	int		_rid;
275	struct resource *_r;
276};
277
278/**
279 * @brief Deactivate a resource
280 *
281 * Deactivate a resource previously allocated with
282 * BUS_ALLOC_RESOURCE(). This may for instance unmap a memory region
283 * from the kernel's virtual address space.
284 *
285 * @param _dev		the parent device of @p _child
286 * @param _child	the device which allocated the resource
287 * @param _type		the type of resource
288 * @param _rid		the resource identifier
289 * @param _r		the resource to deactivate
290 */
291METHOD int deactivate_resource {
292	device_t	_dev;
293	device_t	_child;
294	int		_type;
295	int		_rid;
296	struct resource *_r;
297};
298
299/**
300 * @brief Adjust a resource
301 *
302 * Adjust the start and/or end of a resource allocated by
303 * BUS_ALLOC_RESOURCE.  At least part of the new address range must overlap
304 * with the existing address range.  If the successful, the resource's range
305 * will be adjusted to [start, end] on return.
306 *
307 * @param _dev		the parent device of @p _child
308 * @param _child	the device which allocated the resource
309 * @param _type		the type of resource
310 * @param _res		the resource to adjust
311 * @param _start	the new starting address of the resource range
312 * @param _end		the new ending address of the resource range
313 */
314METHOD int adjust_resource {
315	device_t	_dev;
316	device_t	_child;
317	int		_type;
318	struct resource *_res;
319	u_long		_start;
320	u_long		_end;
321};
322
323/**
324 * @brief Release a resource
325 *
326 * Free a resource allocated by the BUS_ALLOC_RESOURCE.  The @p _rid
327 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
328 * (which is not necessarily the same as the one the client passed).
329 *
330 * @param _dev		the parent device of @p _child
331 * @param _child	the device which allocated the resource
332 * @param _type		the type of resource
333 * @param _rid		the resource identifier
334 * @param _r		the resource to release
335 */
336METHOD int release_resource {
337	device_t	_dev;
338	device_t	_child;
339	int		_type;
340	int		_rid;
341	struct resource *_res;
342};
343
344/**
345 * @brief Install an interrupt handler
346 *
347 * This method is used to associate an interrupt handler function with
348 * an irq resource. When the interrupt triggers, the function @p _intr
349 * will be called with the value of @p _arg as its single
350 * argument. The value returned in @p *_cookiep is used to cancel the
351 * interrupt handler - the caller should save this value to use in a
352 * future call to BUS_TEARDOWN_INTR().
353 *
354 * @param _dev		the parent device of @p _child
355 * @param _child	the device which allocated the resource
356 * @param _irq		the resource representing the interrupt
357 * @param _flags	a set of bits from enum intr_type specifying
358 *			the class of interrupt
359 * @param _intr		the function to call when the interrupt
360 *			triggers
361 * @param _arg		a value to use as the single argument in calls
362 *			to @p _intr
363 * @param _cookiep	a pointer to a location to recieve a cookie
364 *			value that may be used to remove the interrupt
365 *			handler
366 */
367METHOD int setup_intr {
368	device_t	_dev;
369	device_t	_child;
370	struct resource *_irq;
371	int		_flags;
372	driver_filter_t	*_filter;
373	driver_intr_t	*_intr;
374	void		*_arg;
375	void		**_cookiep;
376};
377
378/**
379 * @brief Uninstall an interrupt handler
380 *
381 * This method is used to disassociate an interrupt handler function
382 * with an irq resource. The value of @p _cookie must be the value
383 * returned from a previous call to BUS_SETUP_INTR().
384 *
385 * @param _dev		the parent device of @p _child
386 * @param _child	the device which allocated the resource
387 * @param _irq		the resource representing the interrupt
388 * @param _cookie	the cookie value returned when the interrupt
389 *			was originally registered
390 */
391METHOD int teardown_intr {
392	device_t	_dev;
393	device_t	_child;
394	struct resource	*_irq;
395	void		*_cookie;
396};
397
398/**
399 * @brief Define a resource which can be allocated with
400 * BUS_ALLOC_RESOURCE().
401 *
402 * This method is used by some busses (typically ISA) to allow a
403 * driver to describe a resource range that it would like to
404 * allocate. The resource defined by @p _type and @p _rid is defined
405 * to start at @p _start and to include @p _count indices in its
406 * range.
407 *
408 * @param _dev		the parent device of @p _child
409 * @param _child	the device which owns the resource
410 * @param _type		the type of resource
411 * @param _rid		the resource identifier
412 * @param _start	the start of the resource range
413 * @param _count	the size of the resource range
414 */
415METHOD int set_resource {
416	device_t	_dev;
417	device_t	_child;
418	int		_type;
419	int		_rid;
420	u_long		_start;
421	u_long		_count;
422};
423
424/**
425 * @brief Describe a resource
426 *
427 * This method allows a driver to examine the range used for a given
428 * resource without actually allocating it.
429 *
430 * @param _dev		the parent device of @p _child
431 * @param _child	the device which owns the resource
432 * @param _type		the type of resource
433 * @param _rid		the resource identifier
434 * @param _start	the address of a location to recieve the start
435 *			index of the resource range
436 * @param _count	the address of a location to recieve the size
437 *			of the resource range
438 */
439METHOD int get_resource {
440	device_t	_dev;
441	device_t	_child;
442	int		_type;
443	int		_rid;
444	u_long		*_startp;
445	u_long		*_countp;
446};
447
448/**
449 * @brief Delete a resource.
450 *
451 * Use this to delete a resource (possibly one previously added with
452 * BUS_SET_RESOURCE()).
453 *
454 * @param _dev		the parent device of @p _child
455 * @param _child	the device which owns the resource
456 * @param _type		the type of resource
457 * @param _rid		the resource identifier
458 */
459METHOD void delete_resource {
460	device_t	_dev;
461	device_t	_child;
462	int		_type;
463	int		_rid;
464};
465
466/**
467 * @brief Return a struct resource_list.
468 *
469 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
470 * implement their resource handling. It should return the resource
471 * list of the given child device.
472 *
473 * @param _dev		the parent device of @p _child
474 * @param _child	the device which owns the resource list
475 */
476METHOD struct resource_list * get_resource_list {
477	device_t	_dev;
478	device_t	_child;
479} DEFAULT bus_generic_get_resource_list;
480
481/**
482 * @brief Is the hardware described by @p _child still attached to the
483 * system?
484 *
485 * This method should return 0 if the device is not present.  It
486 * should return -1 if it is present.  Any errors in determining
487 * should be returned as a normal errno value.  Client drivers are to
488 * assume that the device is present, even if there is an error
489 * determining if it is there.  Busses are to try to avoid returning
490 * errors, but newcard will return an error if the device fails to
491 * implement this method.
492 *
493 * @param _dev		the parent device of @p _child
494 * @param _child	the device which is being examined
495 */
496METHOD int child_present {
497	device_t	_dev;
498	device_t	_child;
499} DEFAULT bus_generic_child_present;
500
501/**
502 * @brief Returns the pnp info for this device.
503 *
504 * Return it as a string.  If the string is insufficient for the
505 * storage, then return EOVERFLOW.
506 *
507 * @param _dev		the parent device of @p _child
508 * @param _child	the device which is being examined
509 * @param _buf		the address of a buffer to receive the pnp
510 *			string
511 * @param _buflen	the size of the buffer pointed to by @p _buf
512 */
513METHOD int child_pnpinfo_str {
514	device_t	_dev;
515	device_t	_child;
516	char		*_buf;
517	size_t		_buflen;
518};
519
520/**
521 * @brief Returns the location for this device.
522 *
523 * Return it as a string.  If the string is insufficient for the
524 * storage, then return EOVERFLOW.
525 *
526 * @param _dev		the parent device of @p _child
527 * @param _child	the device which is being examined
528 * @param _buf		the address of a buffer to receive the location
529 *			string
530 * @param _buflen	the size of the buffer pointed to by @p _buf
531 */
532METHOD int child_location_str {
533	device_t	_dev;
534	device_t	_child;
535	char		*_buf;
536	size_t		_buflen;
537};
538
539/**
540 * @brief Allow drivers to request that an interrupt be bound to a specific
541 * CPU.
542 *
543 * @param _dev		the parent device of @p _child
544 * @param _child	the device which allocated the resource
545 * @param _irq		the resource representing the interrupt
546 * @param _cpu		the CPU to bind the interrupt to
547 */
548METHOD int bind_intr {
549	device_t	_dev;
550	device_t	_child;
551	struct resource *_irq;
552	int		_cpu;
553} DEFAULT bus_generic_bind_intr;
554
555/**
556 * @brief Allow (bus) drivers to specify the trigger mode and polarity
557 * of the specified interrupt.
558 *
559 * @param _dev		the bus device
560 * @param _irq		the interrupt number to modify
561 * @param _trig		the trigger mode required
562 * @param _pol		the interrupt polarity required
563 */
564METHOD int config_intr {
565	device_t	_dev;
566	int		_irq;
567	enum intr_trigger _trig;
568	enum intr_polarity _pol;
569} DEFAULT bus_generic_config_intr;
570
571/**
572 * @brief Allow drivers to associate a description with an active
573 * interrupt handler.
574 *
575 * @param _dev		the parent device of @p _child
576 * @param _child	the device which allocated the resource
577 * @param _irq		the resource representing the interrupt
578 * @param _cookie	the cookie value returned when the interrupt
579 *			was originally registered
580 * @param _descr	the description to associate with the interrupt
581 */
582METHOD int describe_intr {
583	device_t	_dev;
584	device_t	_child;
585	struct resource *_irq;
586	void		*_cookie;
587	const char	*_descr;
588} DEFAULT bus_generic_describe_intr;
589
590/**
591 * @brief Notify a (bus) driver about a child that the hints mechanism
592 * believes it has discovered.
593 *
594 * The bus is responsible for then adding the child in the right order
595 * and discovering other things about the child.  The bus driver is
596 * free to ignore this hint, to do special things, etc.  It is all up
597 * to the bus driver to interpret.
598 *
599 * This method is only called in response to the parent bus asking for
600 * hinted devices to be enumerated.
601 *
602 * @param _dev		the bus device
603 * @param _dname	the name of the device w/o unit numbers
604 * @param _dunit	the unit number of the device
605 */
606METHOD void hinted_child {
607	device_t	_dev;
608	const char	*_dname;
609	int		_dunit;
610};
611
612/**
613 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
614 *
615 * @param _dev		the parent device of @p _child
616 * @param _child	the device to which the tag will belong
617 */
618METHOD bus_dma_tag_t get_dma_tag {
619	device_t	_dev;
620	device_t	_child;
621} DEFAULT bus_generic_get_dma_tag;
622
623/**
624 * @brief Allow the bus to determine the unit number of a device.
625 *
626 * @param _dev		the parent device of @p _child
627 * @param _child	the device whose unit is to be wired
628 * @param _name		the name of the device's new devclass
629 * @param _unitp	a pointer to the device's new unit value
630 */
631METHOD void hint_device_unit {
632	device_t	_dev;
633	device_t	_child;
634	const char	*_name;
635	int		*_unitp;
636};
637
638/**
639 * @brief Notify a bus that the bus pass level has been changed
640 *
641 * @param _dev		the bus device
642 */
643METHOD void new_pass {
644	device_t	_dev;
645} DEFAULT bus_generic_new_pass;
646
647/**
648 * @brief Notify a bus that specified child's IRQ should be remapped.
649 *
650 * @param _dev		the bus device
651 * @param _child	the child device
652 * @param _irq		the irq number
653 */
654METHOD int remap_intr {
655	device_t	_dev;
656	device_t	_child;
657	u_int		_irq;
658} DEFAULT null_remap_intr;
659