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");
70 static int null_reset_post(device_t bus, device_t dev)
75 static int null_reset_prepare(device_t bus, device_t dev)
83 * @brief Print a description of a child device
85 * This is called from system code which prints out a description of a
86 * device. It should describe the attachment that the child has with
87 * the parent. For instance the TurboLaser bus prints which node the
88 * device is attached to. See bus_generic_print_child() for more
91 * @param _dev the device whose child is being printed
92 * @param _child the child device to describe
94 * @returns the number of characters output.
96 METHOD int print_child {
99 } DEFAULT bus_generic_print_child;
102 * @brief Print a notification about an unprobed child device.
104 * Called for each child device that did not succeed in probing for a
107 * @param _dev the device whose child was being probed
108 * @param _child the child device which failed to probe
110 METHOD void probe_nomatch {
116 * @brief Read the value of a bus-specific attribute of a device
118 * This method, along with BUS_WRITE_IVAR() manages a bus-specific set
119 * of instance variables of a child device. The intention is that
120 * each different type of bus defines a set of appropriate instance
121 * variables (such as ports and irqs for ISA bus etc.)
123 * This information could be given to the child device as a struct but
124 * that makes it hard for a bus to add or remove variables without
125 * forcing an edit and recompile for all drivers which may not be
126 * possible for vendor supplied binary drivers.
128 * This method copies the value of an instance variable to the
129 * location specified by @p *_result.
131 * @param _dev the device whose child was being examined
132 * @param _child the child device whose instance variable is
134 * @param _index the instance variable to read
135 * @param _result a location to receive the instance variable
139 * @retval ENOENT no such instance variable is supported by @p
142 METHOD int read_ivar {
150 * @brief Write the value of a bus-specific attribute of a device
152 * This method sets the value of an instance variable to @p _value.
154 * @param _dev the device whose child was being updated
155 * @param _child the child device whose instance variable is
157 * @param _index the instance variable to write
158 * @param _value the value to write to that instance variable
161 * @retval ENOENT no such instance variable is supported by @p
163 * @retval EINVAL the instance variable was recognised but
164 * contains a read-only value
166 METHOD int write_ivar {
174 * @brief Notify a bus that a child was deleted
176 * Called at the beginning of device_delete_child() to allow the parent
177 * to teardown any bus-specific state for the child.
179 * @param _dev the device whose child is being deleted
180 * @param _child the child device which is being deleted
182 METHOD void child_deleted {
188 * @brief Notify a bus that a child was detached
190 * Called after the child's DEVICE_DETACH() method to allow the parent
191 * to reclaim any resources allocated on behalf of the child.
193 * @param _dev the device whose child changed state
194 * @param _child the child device which changed state
196 METHOD void child_detached {
202 * @brief Notify a bus that a new driver was added
204 * Called when a new driver is added to the devclass which owns this
205 * bus. The generic implementation of this method attempts to probe and
206 * attach any un-matched children of the bus.
208 * @param _dev the device whose devclass had a new driver
210 * @param _driver the new driver which was added
212 METHOD void driver_added {
215 } DEFAULT bus_generic_driver_added;
218 * @brief Create a new child device
220 * For buses which use use drivers supporting DEVICE_IDENTIFY() to
221 * enumerate their devices, this method is used to create new
222 * device instances. The new device will be added after the last
223 * existing child with the same order. Implementations of bus_add_child
224 * call device_add_child_ordered to add the child and often add
225 * a suitable ivar to the device specific to that bus.
227 * @param _dev the bus device which will be the parent of the
229 * @param _order a value which is used to partially sort the
230 * children of @p _dev - devices created using
231 * lower values of @p _order appear first in @p
232 * _dev's list of children
233 * @param _name devclass name for new device or @c NULL if not
235 * @param _unit unit number for new device or @c -1 if not
238 METHOD device_t add_child {
243 } DEFAULT null_add_child;
246 * @brief Rescan the bus
248 * This method is called by a parent bridge or devctl to trigger a bus
249 * rescan. The rescan should delete devices no longer present and
250 * enumerate devices that have newly arrived.
252 * @param _dev the bus device
259 * @brief Allocate a system resource
261 * This method is called by child devices of a bus to allocate resources.
262 * The types are defined in <machine/resource.h>; the meaning of the
263 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
264 * valid if the resource type is). If a resource was allocated and the
265 * caller did not use the RF_ACTIVE to specify that it should be
266 * activated immediately, the caller is responsible for calling
267 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
269 * @param _dev the parent device of @p _child
270 * @param _child the device which is requesting an allocation
271 * @param _type the type of resource to allocate
272 * @param _rid a pointer to the resource identifier
273 * @param _start hint at the start of the resource range - pass
274 * @c 0 for any start address
275 * @param _end hint at the end of the resource range - pass
276 * @c ~0 for any end address
277 * @param _count hint at the size of range required - pass @c 1
279 * @param _flags any extra flags to control the resource
280 * allocation - see @c RF_XXX flags in
281 * <sys/rman.h> for details
283 * @returns the resource which was allocated or @c NULL if no
284 * resource could be allocated
286 METHOD struct resource * alloc_resource {
295 } DEFAULT null_alloc_resource;
298 * @brief Activate a resource
300 * Activate a resource previously allocated with
301 * BUS_ALLOC_RESOURCE(). This may enable decoding of this resource in a
302 * device for instance. It will also establish a mapping for the resource
303 * unless RF_UNMAPPED was set when allocating the resource.
305 * @param _dev the parent device of @p _child
306 * @param _child the device which allocated the resource
307 * @param _type the type of resource
308 * @param _rid the resource identifier
309 * @param _r the resource to activate
311 METHOD int activate_resource {
321 * @brief Map a resource
323 * Allocate a mapping for a range of an active resource. The mapping
324 * is described by a struct resource_map object. This may for instance
325 * map a memory region into the kernel's virtual address space.
327 * @param _dev the parent device of @p _child
328 * @param _child the device which allocated the resource
329 * @param _type the type of resource
330 * @param _r the resource to map
331 * @param _args optional attributes of the mapping
332 * @param _map the mapping
334 METHOD int map_resource {
339 struct resource_map_request *_args;
340 struct resource_map *_map;
341 } DEFAULT bus_generic_map_resource;
345 * @brief Unmap a resource
347 * Release a mapping previously allocated with
348 * BUS_MAP_RESOURCE(). This may for instance unmap a memory region
349 * from the kernel's virtual address space.
351 * @param _dev the parent device of @p _child
352 * @param _child the device which allocated the resource
353 * @param _type the type of resource
354 * @param _r the resource
355 * @param _map the mapping to release
357 METHOD int unmap_resource {
362 struct resource_map *_map;
363 } DEFAULT bus_generic_unmap_resource;
367 * @brief Deactivate a resource
369 * Deactivate a resource previously allocated with
370 * BUS_ALLOC_RESOURCE().
372 * @param _dev the parent device of @p _child
373 * @param _child the device which allocated the resource
374 * @param _type the type of resource
375 * @param _rid the resource identifier
376 * @param _r the resource to deactivate
378 METHOD int deactivate_resource {
387 * @brief Adjust a resource
389 * Adjust the start and/or end of a resource allocated by
390 * BUS_ALLOC_RESOURCE. At least part of the new address range must overlap
391 * with the existing address range. If the successful, the resource's range
392 * will be adjusted to [start, end] on return.
394 * @param _dev the parent device of @p _child
395 * @param _child the device which allocated the resource
396 * @param _type the type of resource
397 * @param _res the resource to adjust
398 * @param _start the new starting address of the resource range
399 * @param _end the new ending address of the resource range
401 METHOD int adjust_resource {
405 struct resource *_res;
411 * @brief translate a resource value
413 * Give a bus driver the opportunity to translate resource ranges. If
414 * successful, the host's view of the resource starting at @p _start is
415 * returned in @p _newstart, otherwise an error is returned.
417 * @param _dev the device associated with the resource
418 * @param _type the type of resource
419 * @param _start the starting address of the resource range
420 * @param _newstart the new starting address of the resource range
422 METHOD int translate_resource {
426 rman_res_t *_newstart;
427 } DEFAULT bus_generic_translate_resource;
430 * @brief Release a resource
432 * Free a resource allocated by the BUS_ALLOC_RESOURCE. The @p _rid
433 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
434 * (which is not necessarily the same as the one the client passed).
436 * @param _dev the parent device of @p _child
437 * @param _child the device which allocated the resource
438 * @param _type the type of resource
439 * @param _rid the resource identifier
440 * @param _r the resource to release
442 METHOD int release_resource {
447 struct resource *_res;
451 * @brief Install an interrupt handler
453 * This method is used to associate an interrupt handler function with
454 * an irq resource. When the interrupt triggers, the function @p _intr
455 * will be called with the value of @p _arg as its single
456 * argument. The value returned in @p *_cookiep is used to cancel the
457 * interrupt handler - the caller should save this value to use in a
458 * future call to BUS_TEARDOWN_INTR().
460 * @param _dev the parent device of @p _child
461 * @param _child the device which allocated the resource
462 * @param _irq the resource representing the interrupt
463 * @param _flags a set of bits from enum intr_type specifying
464 * the class of interrupt
465 * @param _intr the function to call when the interrupt
467 * @param _arg a value to use as the single argument in calls
469 * @param _cookiep a pointer to a location to receive a cookie
470 * value that may be used to remove the interrupt
473 METHOD int setup_intr {
476 struct resource *_irq;
478 driver_filter_t *_filter;
479 driver_intr_t *_intr;
485 * @brief Uninstall an interrupt handler
487 * This method is used to disassociate an interrupt handler function
488 * with an irq resource. The value of @p _cookie must be the value
489 * returned from a previous call to BUS_SETUP_INTR().
491 * @param _dev the parent device of @p _child
492 * @param _child the device which allocated the resource
493 * @param _irq the resource representing the interrupt
494 * @param _cookie the cookie value returned when the interrupt
495 * was originally registered
497 METHOD int teardown_intr {
500 struct resource *_irq;
505 * @brief Suspend an interrupt handler
507 * This method is used to mark a handler as suspended in the case
508 * that the associated device is powered down and cannot be a source
509 * for the, typically shared, interrupt.
510 * The value of @p _irq must be the interrupt resource passed
511 * to a previous call to BUS_SETUP_INTR().
513 * @param _dev the parent device of @p _child
514 * @param _child the device which allocated the resource
515 * @param _irq the resource representing the interrupt
517 METHOD int suspend_intr {
520 struct resource *_irq;
521 } DEFAULT bus_generic_suspend_intr;
524 * @brief Resume an interrupt handler
526 * This method is used to clear suspended state of a handler when
527 * the associated device is powered up and can be an interrupt source
529 * The value of @p _irq must be the interrupt resource passed
530 * to a previous call to BUS_SETUP_INTR().
532 * @param _dev the parent device of @p _child
533 * @param _child the device which allocated the resource
534 * @param _irq the resource representing the interrupt
536 METHOD int resume_intr {
539 struct resource *_irq;
540 } DEFAULT bus_generic_resume_intr;
543 * @brief Define a resource which can be allocated with
544 * BUS_ALLOC_RESOURCE().
546 * This method is used by some buses (typically ISA) to allow a
547 * driver to describe a resource range that it would like to
548 * allocate. The resource defined by @p _type and @p _rid is defined
549 * to start at @p _start and to include @p _count indices in its
552 * @param _dev the parent device of @p _child
553 * @param _child the device which owns the resource
554 * @param _type the type of resource
555 * @param _rid the resource identifier
556 * @param _start the start of the resource range
557 * @param _count the size of the resource range
559 METHOD int set_resource {
569 * @brief Describe a resource
571 * This method allows a driver to examine the range used for a given
572 * resource without actually allocating it.
574 * @param _dev the parent device of @p _child
575 * @param _child the device which owns the resource
576 * @param _type the type of resource
577 * @param _rid the resource identifier
578 * @param _start the address of a location to receive the start
579 * index of the resource range
580 * @param _count the address of a location to receive the size
581 * of the resource range
583 METHOD int get_resource {
593 * @brief Delete a resource.
595 * Use this to delete a resource (possibly one previously added with
596 * BUS_SET_RESOURCE()).
598 * @param _dev the parent device of @p _child
599 * @param _child the device which owns the resource
600 * @param _type the type of resource
601 * @param _rid the resource identifier
603 METHOD void delete_resource {
611 * @brief Return a struct resource_list.
613 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
614 * implement their resource handling. It should return the resource
615 * list of the given child device.
617 * @param _dev the parent device of @p _child
618 * @param _child the device which owns the resource list
620 METHOD struct resource_list * get_resource_list {
623 } DEFAULT bus_generic_get_resource_list;
626 * @brief Is the hardware described by @p _child still attached to the
629 * This method should return 0 if the device is not present. It
630 * should return -1 if it is present. Any errors in determining
631 * should be returned as a normal errno value. Client drivers are to
632 * assume that the device is present, even if there is an error
633 * determining if it is there. Buses are to try to avoid returning
634 * errors, but newcard will return an error if the device fails to
635 * implement this method.
637 * @param _dev the parent device of @p _child
638 * @param _child the device which is being examined
640 METHOD int child_present {
643 } DEFAULT bus_generic_child_present;
646 * @brief Returns the pnp info for this device.
648 * Return it as a string, appended to @p _sb
650 * The string must be formatted as a space-separated list of
651 * name=value pairs. Names may only contain alphanumeric characters,
652 * underscores ('_') and hyphens ('-'). Values can contain any
653 * non-whitespace characters. Values containing whitespace can be
654 * quoted with double quotes ('"'). Double quotes and backslashes in
655 * quoted values can be escaped with backslashes ('\').
657 * @param _dev the parent device of @p _child
658 * @param _child the device which is being examined
659 * @param _sb sbuf for results string
661 METHOD int child_pnpinfo {
665 } DEFAULT bus_generic_child_pnpinfo;
668 * @brief Returns the location for this device.
670 * Return it as a string, appended to @p _sb
672 * The string must be formatted as a space-separated list of
673 * name=value pairs. Names may only contain alphanumeric characters,
674 * underscores ('_') and hyphens ('-'). Values can contain any
675 * non-whitespace characters. Values containing whitespace can be
676 * quoted with double quotes ('"'). Double quotes and backslashes in
677 * quoted values can be escaped with backslashes ('\').
679 * @param _dev the parent device of @p _child
680 * @param _child the device which is being examined
681 * @param _sb sbuf for results string
683 METHOD int child_location {
687 } DEFAULT bus_generic_child_location;
690 * @brief Allow drivers to request that an interrupt be bound to a specific
693 * @param _dev the parent device of @p _child
694 * @param _child the device which allocated the resource
695 * @param _irq the resource representing the interrupt
696 * @param _cpu the CPU to bind the interrupt to
698 METHOD int bind_intr {
701 struct resource *_irq;
703 } DEFAULT bus_generic_bind_intr;
706 * @brief Allow (bus) drivers to specify the trigger mode and polarity
707 * of the specified interrupt.
709 * @param _dev the bus device
710 * @param _irq the interrupt number to modify
711 * @param _trig the trigger mode required
712 * @param _pol the interrupt polarity required
714 METHOD int config_intr {
717 enum intr_trigger _trig;
718 enum intr_polarity _pol;
719 } DEFAULT bus_generic_config_intr;
722 * @brief Allow drivers to associate a description with an active
725 * @param _dev the parent device of @p _child
726 * @param _child the device which allocated the resource
727 * @param _irq the resource representing the interrupt
728 * @param _cookie the cookie value returned when the interrupt
729 * was originally registered
730 * @param _descr the description to associate with the interrupt
732 METHOD int describe_intr {
735 struct resource *_irq;
738 } DEFAULT bus_generic_describe_intr;
741 * @brief Notify a (bus) driver about a child that the hints mechanism
742 * believes it has discovered.
744 * The bus is responsible for then adding the child in the right order
745 * and discovering other things about the child. The bus driver is
746 * free to ignore this hint, to do special things, etc. It is all up
747 * to the bus driver to interpret.
749 * This method is only called in response to the parent bus asking for
750 * hinted devices to be enumerated.
752 * @param _dev the bus device
753 * @param _dname the name of the device w/o unit numbers
754 * @param _dunit the unit number of the device
756 METHOD void hinted_child {
763 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
765 * @param _dev the parent device of @p _child
766 * @param _child the device to which the tag will belong
768 METHOD bus_dma_tag_t get_dma_tag {
771 } DEFAULT bus_generic_get_dma_tag;
774 * @brief Returns bus_space_tag_t for use w/ devices on the bus.
776 * @param _dev the parent device of @p _child
777 * @param _child the device to which the tag will belong
779 METHOD bus_space_tag_t get_bus_tag {
782 } DEFAULT bus_generic_get_bus_tag;
785 * @brief Allow the bus to determine the unit number of a device.
787 * @param _dev the parent device of @p _child
788 * @param _child the device whose unit is to be wired
789 * @param _name the name of the device's new devclass
790 * @param _unitp a pointer to the device's new unit value
792 METHOD void hint_device_unit {
800 * @brief Notify a bus that the bus pass level has been changed
802 * @param _dev the bus device
804 METHOD void new_pass {
806 } DEFAULT bus_generic_new_pass;
809 * @brief Notify a bus that specified child's IRQ should be remapped.
811 * @param _dev the bus device
812 * @param _child the child device
813 * @param _irq the irq number
815 METHOD int remap_intr {
819 } DEFAULT null_remap_intr;
822 * @brief Suspend a given child
824 * @param _dev the parent device of @p _child
825 * @param _child the device to suspend
827 METHOD int suspend_child {
830 } DEFAULT bus_generic_suspend_child;
833 * @brief Resume a given child
835 * @param _dev the parent device of @p _child
836 * @param _child the device to resume
838 METHOD int resume_child {
841 } DEFAULT bus_generic_resume_child;
844 * @brief Get the VM domain handle for the given bus and child.
846 * @param _dev the bus device
847 * @param _child the child device
848 * @param _domain a pointer to the bus's domain handle identifier
850 METHOD int get_domain {
854 } DEFAULT bus_generic_get_domain;
857 * @brief Request a set of CPUs
859 * @param _dev the bus device
860 * @param _child the child device
861 * @param _op type of CPUs to request
862 * @param _setsize the size of the set passed in _cpuset
863 * @param _cpuset a pointer to a cpuset to receive the requested
866 METHOD int get_cpus {
871 struct _cpuset *_cpuset;
872 } DEFAULT bus_generic_get_cpus;
875 * @brief Prepares the given child of the bus for reset
877 * Typically bus detaches or suspends children' drivers, and then
878 * calls this method to save bus-specific information, for instance,
879 * PCI config space, which is damaged by reset.
881 * The bus_helper_reset_prepare() helper is provided to ease
882 * implementing bus reset methods.
884 * @param _dev the bus device
885 * @param _child the child device
887 METHOD int reset_prepare {
890 } DEFAULT null_reset_prepare;
893 * @brief Restores the child operations after the reset
895 * The bus_helper_reset_post() helper is provided to ease
896 * implementing bus reset methods.
898 * @param _dev the bus device
899 * @param _child the child device
901 METHOD int reset_post {
904 } DEFAULT null_reset_post;
907 * @brief Performs reset of the child
909 * @param _dev the bus device
910 * @param _child the child device
911 * @param _flags DEVF_RESET_ flags
913 METHOD int reset_child {
920 * @brief Gets child's specific property
922 * The bus_get_property can be used to access device
923 * specific properties stored on the bus. If _propvalue
924 * is NULL or _size is 0, then method only returns size
927 * @param _dev the bus device
928 * @param _child the child device
929 * @param _propname property name
930 * @param _propvalue property value destination
931 * @param _size property value size
933 * @returns size of property if successful otherwise -1
935 METHOD ssize_t get_property {
938 const char *_propname;
941 device_property_type_t type;
942 } DEFAULT bus_generic_get_property;
945 * @brief Gets a child's full path to the device
947 * The get_device_path method retrieves a device's
948 * full path to the device using one of several
949 * locators present in the system.
951 * @param _bus the bus device
952 * @param _child the child device
953 * @param _locator locator name
954 * @param _sb buffer loaction string
956 METHOD int get_device_path {
959 const char *_locator;
961 } DEFAULT bus_generic_get_device_path;