2 # Copyright (c) 1998-2004 Doug Rabson
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/systm.h>
34 * @defgroup BUS bus - KObj methods for drivers of devices with children
35 * @brief A set of methods required device drivers that support
42 # Default implementations of some methods.
45 static struct resource *
46 null_alloc_resource(device_t dev, device_t child,
47 int type, int *rid, rman_res_t start, rman_res_t end,
48 rman_res_t count, u_int flags)
54 null_remap_intr(device_t bus, device_t dev, u_int irq)
58 return (BUS_REMAP_INTR(dev, NULL, irq));
63 null_add_child(device_t bus, int order, const char *name,
67 panic("bus_add_child is not implemented");
71 null_reset_post(device_t bus, device_t dev)
77 null_reset_prepare(device_t bus, device_t dev)
84 * @brief Print a description of a child device
86 * This is called from system code which prints out a description of a
87 * device. It should describe the attachment that the child has with
88 * the parent. For instance the TurboLaser bus prints which node the
89 * device is attached to. See bus_generic_print_child() for more
92 * @param _dev the device whose child is being printed
93 * @param _child the child device to describe
95 * @returns the number of characters output.
97 METHOD int print_child {
100 } DEFAULT bus_generic_print_child;
103 * @brief Print a notification about an unprobed child device.
105 * Called for each child device that did not succeed in probing for a
108 * @param _dev the device whose child was being probed
109 * @param _child the child device which failed to probe
111 METHOD void probe_nomatch {
117 * @brief Read the value of a bus-specific attribute of a device
119 * This method, along with BUS_WRITE_IVAR() manages a bus-specific set
120 * of instance variables of a child device. The intention is that
121 * each different type of bus defines a set of appropriate instance
122 * variables (such as ports and irqs for ISA bus etc.)
124 * This information could be given to the child device as a struct but
125 * that makes it hard for a bus to add or remove variables without
126 * forcing an edit and recompile for all drivers which may not be
127 * possible for vendor supplied binary drivers.
129 * This method copies the value of an instance variable to the
130 * location specified by @p *_result.
132 * @param _dev the device whose child was being examined
133 * @param _child the child device whose instance variable is
135 * @param _index the instance variable to read
136 * @param _result a location to receive the instance variable
140 * @retval ENOENT no such instance variable is supported by @p
143 METHOD int read_ivar {
151 * @brief Write the value of a bus-specific attribute of a device
153 * This method sets the value of an instance variable to @p _value.
155 * @param _dev the device whose child was being updated
156 * @param _child the child device whose instance variable is
158 * @param _index the instance variable to write
159 * @param _value the value to write to that instance variable
162 * @retval ENOENT no such instance variable is supported by @p
164 * @retval EINVAL the instance variable was recognised but
165 * contains a read-only value
167 METHOD int write_ivar {
175 * @brief Notify a bus that a child was deleted
177 * Called at the beginning of device_delete_child() to allow the parent
178 * to teardown any bus-specific state for the child.
180 * @param _dev the device whose child is being deleted
181 * @param _child the child device which is being deleted
183 METHOD void child_deleted {
189 * @brief Notify a bus that a child was detached
191 * Called after the child's DEVICE_DETACH() method to allow the parent
192 * to reclaim any resources allocated on behalf of the child.
194 * @param _dev the device whose child changed state
195 * @param _child the child device which changed state
197 METHOD void child_detached {
203 * @brief Notify a bus that a new driver was added
205 * Called when a new driver is added to the devclass which owns this
206 * bus. The generic implementation of this method attempts to probe and
207 * attach any un-matched children of the bus.
209 * @param _dev the device whose devclass had a new driver
211 * @param _driver the new driver which was added
213 METHOD void driver_added {
216 } DEFAULT bus_generic_driver_added;
219 * @brief Create a new child device
221 * For buses which use use drivers supporting DEVICE_IDENTIFY() to
222 * enumerate their devices, this method is used to create new
223 * device instances. The new device will be added after the last
224 * existing child with the same order. Implementations of bus_add_child
225 * call device_add_child_ordered to add the child and often add
226 * a suitable ivar to the device specific to that bus.
228 * @param _dev the bus device which will be the parent of the
230 * @param _order a value which is used to partially sort the
231 * children of @p _dev - devices created using
232 * lower values of @p _order appear first in @p
233 * _dev's list of children
234 * @param _name devclass name for new device or @c NULL if not
236 * @param _unit unit number for new device or @c -1 if not
239 METHOD device_t add_child {
244 } DEFAULT null_add_child;
247 * @brief Rescan the bus
249 * This method is called by a parent bridge or devctl to trigger a bus
250 * rescan. The rescan should delete devices no longer present and
251 * enumerate devices that have newly arrived.
253 * @param _dev the bus device
257 } DEFAULT bus_null_rescan;
260 * @brief Allocate a system resource
262 * This method is called by child devices of a bus to allocate resources.
263 * The types are defined in <machine/resource.h>; the meaning of the
264 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
265 * valid if the resource type is). If a resource was allocated and the
266 * caller did not use the RF_ACTIVE to specify that it should be
267 * activated immediately, the caller is responsible for calling
268 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
270 * @param _dev the parent device of @p _child
271 * @param _child the device which is requesting an allocation
272 * @param _type the type of resource to allocate
273 * @param _rid a pointer to the resource identifier
274 * @param _start hint at the start of the resource range - pass
275 * @c 0 for any start address
276 * @param _end hint at the end of the resource range - pass
277 * @c ~0 for any end address
278 * @param _count hint at the size of range required - pass @c 1
280 * @param _flags any extra flags to control the resource
281 * allocation - see @c RF_XXX flags in
282 * <sys/rman.h> for details
284 * @returns the resource which was allocated or @c NULL if no
285 * resource could be allocated
287 METHOD struct resource * alloc_resource {
296 } DEFAULT null_alloc_resource;
299 * @brief Activate a resource
301 * Activate a resource previously allocated with
302 * BUS_ALLOC_RESOURCE(). This may enable decoding of this resource in a
303 * device for instance. It will also establish a mapping for the resource
304 * unless RF_UNMAPPED was set when allocating the resource.
306 * @param _dev the parent device of @p _child
307 * @param _child the device which allocated the resource
308 * @param _type the type of resource
309 * @param _rid the resource identifier
310 * @param _r the resource to activate
312 METHOD int activate_resource {
322 * @brief Map a resource
324 * Allocate a mapping for a range of an active resource. The mapping
325 * is described by a struct resource_map object. This may for instance
326 * map a memory region into the kernel's virtual address space.
328 * @param _dev the parent device of @p _child
329 * @param _child the device which allocated the resource
330 * @param _type the type of resource
331 * @param _r the resource to map
332 * @param _args optional attributes of the mapping
333 * @param _map the mapping
335 METHOD int map_resource {
340 struct resource_map_request *_args;
341 struct resource_map *_map;
342 } DEFAULT bus_generic_map_resource;
346 * @brief Unmap a resource
348 * Release a mapping previously allocated with
349 * BUS_MAP_RESOURCE(). This may for instance unmap a memory region
350 * from the kernel's virtual address space.
352 * @param _dev the parent device of @p _child
353 * @param _child the device which allocated the resource
354 * @param _type the type of resource
355 * @param _r the resource
356 * @param _map the mapping to release
358 METHOD int unmap_resource {
363 struct resource_map *_map;
364 } DEFAULT bus_generic_unmap_resource;
368 * @brief Deactivate a resource
370 * Deactivate a resource previously allocated with
371 * BUS_ALLOC_RESOURCE().
373 * @param _dev the parent device of @p _child
374 * @param _child the device which allocated the resource
375 * @param _type the type of resource
376 * @param _rid the resource identifier
377 * @param _r the resource to deactivate
379 METHOD int deactivate_resource {
388 * @brief Adjust a resource
390 * Adjust the start and/or end of a resource allocated by
391 * BUS_ALLOC_RESOURCE. At least part of the new address range must overlap
392 * with the existing address range. If the successful, the resource's range
393 * will be adjusted to [start, end] on return.
395 * @param _dev the parent device of @p _child
396 * @param _child the device which allocated the resource
397 * @param _type the type of resource
398 * @param _res the resource to adjust
399 * @param _start the new starting address of the resource range
400 * @param _end the new ending address of the resource range
402 METHOD int adjust_resource {
406 struct resource *_res;
412 * @brief translate a resource value
414 * Give a bus driver the opportunity to translate resource ranges. If
415 * successful, the host's view of the resource starting at @p _start is
416 * returned in @p _newstart, otherwise an error is returned.
418 * @param _dev the device associated with the resource
419 * @param _type the type of resource
420 * @param _start the starting address of the resource range
421 * @param _newstart the new starting address of the resource range
423 METHOD int translate_resource {
427 rman_res_t *_newstart;
428 } DEFAULT bus_generic_translate_resource;
431 * @brief Release a resource
433 * Free a resource allocated by the BUS_ALLOC_RESOURCE. The @p _rid
434 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
435 * (which is not necessarily the same as the one the client passed).
437 * @param _dev the parent device of @p _child
438 * @param _child the device which allocated the resource
439 * @param _type the type of resource
440 * @param _rid the resource identifier
441 * @param _r the resource to release
443 METHOD int release_resource {
448 struct resource *_res;
452 * @brief Install an interrupt handler
454 * This method is used to associate an interrupt handler function with
455 * an irq resource. When the interrupt triggers, the function @p _intr
456 * will be called with the value of @p _arg as its single
457 * argument. The value returned in @p *_cookiep is used to cancel the
458 * interrupt handler - the caller should save this value to use in a
459 * future call to BUS_TEARDOWN_INTR().
461 * @param _dev the parent device of @p _child
462 * @param _child the device which allocated the resource
463 * @param _irq the resource representing the interrupt
464 * @param _flags a set of bits from enum intr_type specifying
465 * the class of interrupt
466 * @param _intr the function to call when the interrupt
468 * @param _arg a value to use as the single argument in calls
470 * @param _cookiep a pointer to a location to receive a cookie
471 * value that may be used to remove the interrupt
474 METHOD int setup_intr {
477 struct resource *_irq;
479 driver_filter_t *_filter;
480 driver_intr_t *_intr;
486 * @brief Uninstall an interrupt handler
488 * This method is used to disassociate an interrupt handler function
489 * with an irq resource. The value of @p _cookie must be the value
490 * returned from a previous call to BUS_SETUP_INTR().
492 * @param _dev the parent device of @p _child
493 * @param _child the device which allocated the resource
494 * @param _irq the resource representing the interrupt
495 * @param _cookie the cookie value returned when the interrupt
496 * was originally registered
498 METHOD int teardown_intr {
501 struct resource *_irq;
506 * @brief Suspend an interrupt handler
508 * This method is used to mark a handler as suspended in the case
509 * that the associated device is powered down and cannot be a source
510 * for the, typically shared, interrupt.
511 * The value of @p _irq must be the interrupt resource passed
512 * to a previous call to BUS_SETUP_INTR().
514 * @param _dev the parent device of @p _child
515 * @param _child the device which allocated the resource
516 * @param _irq the resource representing the interrupt
518 METHOD int suspend_intr {
521 struct resource *_irq;
522 } DEFAULT bus_generic_suspend_intr;
525 * @brief Resume an interrupt handler
527 * This method is used to clear suspended state of a handler when
528 * the associated device is powered up and can be an interrupt source
530 * The value of @p _irq must be the interrupt resource passed
531 * to a previous call to BUS_SETUP_INTR().
533 * @param _dev the parent device of @p _child
534 * @param _child the device which allocated the resource
535 * @param _irq the resource representing the interrupt
537 METHOD int resume_intr {
540 struct resource *_irq;
541 } DEFAULT bus_generic_resume_intr;
544 * @brief Define a resource which can be allocated with
545 * BUS_ALLOC_RESOURCE().
547 * This method is used by some buses (typically ISA) to allow a
548 * driver to describe a resource range that it would like to
549 * allocate. The resource defined by @p _type and @p _rid is defined
550 * to start at @p _start and to include @p _count indices in its
553 * @param _dev the parent device of @p _child
554 * @param _child the device which owns the resource
555 * @param _type the type of resource
556 * @param _rid the resource identifier
557 * @param _start the start of the resource range
558 * @param _count the size of the resource range
560 METHOD int set_resource {
570 * @brief Describe a resource
572 * This method allows a driver to examine the range used for a given
573 * resource without actually allocating it.
575 * @param _dev the parent device of @p _child
576 * @param _child the device which owns the resource
577 * @param _type the type of resource
578 * @param _rid the resource identifier
579 * @param _start the address of a location to receive the start
580 * index of the resource range
581 * @param _count the address of a location to receive the size
582 * of the resource range
584 METHOD int get_resource {
594 * @brief Delete a resource.
596 * Use this to delete a resource (possibly one previously added with
597 * BUS_SET_RESOURCE()).
599 * @param _dev the parent device of @p _child
600 * @param _child the device which owns the resource
601 * @param _type the type of resource
602 * @param _rid the resource identifier
604 METHOD void delete_resource {
612 * @brief Return a struct resource_list.
614 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
615 * implement their resource handling. It should return the resource
616 * list of the given child device.
618 * @param _dev the parent device of @p _child
619 * @param _child the device which owns the resource list
621 METHOD struct resource_list * get_resource_list {
624 } DEFAULT bus_generic_get_resource_list;
627 * @brief Is the hardware described by @p _child still attached to the
630 * This method should return 0 if the device is not present. It
631 * should return -1 if it is present. Any errors in determining
632 * should be returned as a normal errno value. Client drivers are to
633 * assume that the device is present, even if there is an error
634 * determining if it is there. Buses are to try to avoid returning
635 * errors, but newcard will return an error if the device fails to
636 * implement this method.
638 * @param _dev the parent device of @p _child
639 * @param _child the device which is being examined
641 METHOD int child_present {
644 } DEFAULT bus_generic_child_present;
647 * @brief Returns the pnp info for this device.
649 * Return it as a string, appended to @p _sb
651 * The string must be formatted as a space-separated list of
652 * name=value pairs. Names may only contain alphanumeric characters,
653 * underscores ('_') and hyphens ('-'). Values can contain any
654 * non-whitespace characters. Values containing whitespace can be
655 * quoted with double quotes ('"'). Double quotes and backslashes in
656 * quoted values can be escaped with backslashes ('\').
658 * @param _dev the parent device of @p _child
659 * @param _child the device which is being examined
660 * @param _sb sbuf for results string
662 METHOD int child_pnpinfo {
666 } DEFAULT bus_generic_child_pnpinfo;
669 * @brief Returns the location for this device.
671 * Return it as a string, appended to @p _sb
673 * The string must be formatted as a space-separated list of
674 * name=value pairs. Names may only contain alphanumeric characters,
675 * underscores ('_') and hyphens ('-'). Values can contain any
676 * non-whitespace characters. Values containing whitespace can be
677 * quoted with double quotes ('"'). Double quotes and backslashes in
678 * quoted values can be escaped with backslashes ('\').
680 * @param _dev the parent device of @p _child
681 * @param _child the device which is being examined
682 * @param _sb sbuf for results string
684 METHOD int child_location {
688 } DEFAULT bus_generic_child_location;
691 * @brief Allow drivers to request that an interrupt be bound to a specific
694 * @param _dev the parent device of @p _child
695 * @param _child the device which allocated the resource
696 * @param _irq the resource representing the interrupt
697 * @param _cpu the CPU to bind the interrupt to
699 METHOD int bind_intr {
702 struct resource *_irq;
704 } DEFAULT bus_generic_bind_intr;
707 * @brief Allow (bus) drivers to specify the trigger mode and polarity
708 * of the specified interrupt.
710 * @param _dev the bus device
711 * @param _irq the interrupt number to modify
712 * @param _trig the trigger mode required
713 * @param _pol the interrupt polarity required
715 METHOD int config_intr {
718 enum intr_trigger _trig;
719 enum intr_polarity _pol;
720 } DEFAULT bus_generic_config_intr;
723 * @brief Allow drivers to associate a description with an active
726 * @param _dev the parent device of @p _child
727 * @param _child the device which allocated the resource
728 * @param _irq the resource representing the interrupt
729 * @param _cookie the cookie value returned when the interrupt
730 * was originally registered
731 * @param _descr the description to associate with the interrupt
733 METHOD int describe_intr {
736 struct resource *_irq;
739 } DEFAULT bus_generic_describe_intr;
742 * @brief Notify a (bus) driver about a child that the hints mechanism
743 * believes it has discovered.
745 * The bus is responsible for then adding the child in the right order
746 * and discovering other things about the child. The bus driver is
747 * free to ignore this hint, to do special things, etc. It is all up
748 * to the bus driver to interpret.
750 * This method is only called in response to the parent bus asking for
751 * hinted devices to be enumerated.
753 * @param _dev the bus device
754 * @param _dname the name of the device w/o unit numbers
755 * @param _dunit the unit number of the device
757 METHOD void hinted_child {
764 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
766 * @param _dev the parent device of @p _child
767 * @param _child the device to which the tag will belong
769 METHOD bus_dma_tag_t get_dma_tag {
772 } DEFAULT bus_generic_get_dma_tag;
775 * @brief Returns bus_space_tag_t for use w/ devices on the bus.
777 * @param _dev the parent device of @p _child
778 * @param _child the device to which the tag will belong
780 METHOD bus_space_tag_t get_bus_tag {
783 } DEFAULT bus_generic_get_bus_tag;
786 * @brief Allow the bus to determine the unit number of a device.
788 * @param _dev the parent device of @p _child
789 * @param _child the device whose unit is to be wired
790 * @param _name the name of the device's new devclass
791 * @param _unitp a pointer to the device's new unit value
793 METHOD void hint_device_unit {
801 * @brief Notify a bus that the bus pass level has been changed
803 * @param _dev the bus device
805 METHOD void new_pass {
807 } DEFAULT bus_generic_new_pass;
810 * @brief Notify a bus that specified child's IRQ should be remapped.
812 * @param _dev the bus device
813 * @param _child the child device
814 * @param _irq the irq number
816 METHOD int remap_intr {
820 } DEFAULT null_remap_intr;
823 * @brief Suspend a given child
825 * @param _dev the parent device of @p _child
826 * @param _child the device to suspend
828 METHOD int suspend_child {
831 } DEFAULT bus_generic_suspend_child;
834 * @brief Resume a given child
836 * @param _dev the parent device of @p _child
837 * @param _child the device to resume
839 METHOD int resume_child {
842 } DEFAULT bus_generic_resume_child;
845 * @brief Get the VM domain handle for the given bus and child.
847 * @param _dev the bus device
848 * @param _child the child device
849 * @param _domain a pointer to the bus's domain handle identifier
851 METHOD int get_domain {
855 } DEFAULT bus_generic_get_domain;
858 * @brief Request a set of CPUs
860 * @param _dev the bus device
861 * @param _child the child device
862 * @param _op type of CPUs to request
863 * @param _setsize the size of the set passed in _cpuset
864 * @param _cpuset a pointer to a cpuset to receive the requested
867 METHOD int get_cpus {
872 struct _cpuset *_cpuset;
873 } DEFAULT bus_generic_get_cpus;
876 * @brief Prepares the given child of the bus for reset
878 * Typically bus detaches or suspends children' drivers, and then
879 * calls this method to save bus-specific information, for instance,
880 * PCI config space, which is damaged by reset.
882 * The bus_helper_reset_prepare() helper is provided to ease
883 * implementing bus reset methods.
885 * @param _dev the bus device
886 * @param _child the child device
888 METHOD int reset_prepare {
891 } DEFAULT null_reset_prepare;
894 * @brief Restores the child operations after the reset
896 * The bus_helper_reset_post() helper is provided to ease
897 * implementing bus reset methods.
899 * @param _dev the bus device
900 * @param _child the child device
902 METHOD int reset_post {
905 } DEFAULT null_reset_post;
908 * @brief Performs reset of the child
910 * @param _dev the bus device
911 * @param _child the child device
912 * @param _flags DEVF_RESET_ flags
914 METHOD int reset_child {
921 * @brief Gets child's specific property
923 * The bus_get_property can be used to access device
924 * specific properties stored on the bus. If _propvalue
925 * is NULL or _size is 0, then method only returns size
928 * @param _dev the bus device
929 * @param _child the child device
930 * @param _propname property name
931 * @param _propvalue property value destination
932 * @param _size property value size
934 * @returns size of property if successful otherwise -1
936 METHOD ssize_t get_property {
939 const char *_propname;
942 device_property_type_t type;
943 } DEFAULT bus_generic_get_property;
946 * @brief Gets a child's full path to the device
948 * The get_device_path method retrieves a device's
949 * full path to the device using one of several
950 * locators present in the system.
952 * @param _bus the bus device
953 * @param _child the child device
954 * @param _locator locator name
955 * @param _sb buffer loaction string
957 METHOD int get_device_path {
960 const char *_locator;
962 } DEFAULT bus_generic_get_device_path;