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)
81 null_translate_resource(device_t bus, int type, rman_res_t start,
84 if (device_get_parent(bus) != NULL)
85 return (BUS_TRANSLATE_RESOURCE(device_get_parent(bus),
86 type, start, newstart));
94 * @brief Print a description of a child device
96 * This is called from system code which prints out a description of a
97 * device. It should describe the attachment that the child has with
98 * the parent. For instance the TurboLaser bus prints which node the
99 * device is attached to. See bus_generic_print_child() for more
102 * @param _dev the device whose child is being printed
103 * @param _child the child device to describe
105 * @returns the number of characters output.
107 METHOD int print_child {
110 } DEFAULT bus_generic_print_child;
113 * @brief Print a notification about an unprobed child device.
115 * Called for each child device that did not succeed in probing for a
118 * @param _dev the device whose child was being probed
119 * @param _child the child device which failed to probe
121 METHOD void probe_nomatch {
127 * @brief Read the value of a bus-specific attribute of a device
129 * This method, along with BUS_WRITE_IVAR() manages a bus-specific set
130 * of instance variables of a child device. The intention is that
131 * each different type of bus defines a set of appropriate instance
132 * variables (such as ports and irqs for ISA bus etc.)
134 * This information could be given to the child device as a struct but
135 * that makes it hard for a bus to add or remove variables without
136 * forcing an edit and recompile for all drivers which may not be
137 * possible for vendor supplied binary drivers.
139 * This method copies the value of an instance variable to the
140 * location specified by @p *_result.
142 * @param _dev the device whose child was being examined
143 * @param _child the child device whose instance variable is
145 * @param _index the instance variable to read
146 * @param _result a location to receive the instance variable
150 * @retval ENOENT no such instance variable is supported by @p
153 METHOD int read_ivar {
161 * @brief Write the value of a bus-specific attribute of a device
163 * This method sets the value of an instance variable to @p _value.
165 * @param _dev the device whose child was being updated
166 * @param _child the child device whose instance variable is
168 * @param _index the instance variable to write
169 * @param _value the value to write to that instance variable
172 * @retval ENOENT no such instance variable is supported by @p
174 * @retval EINVAL the instance variable was recognised but
175 * contains a read-only value
177 METHOD int write_ivar {
185 * @brief Notify a bus that a child was deleted
187 * Called at the beginning of device_delete_child() to allow the parent
188 * to teardown any bus-specific state for the child.
190 * @param _dev the device whose child is being deleted
191 * @param _child the child device which is being deleted
193 METHOD void child_deleted {
199 * @brief Notify a bus that a child was detached
201 * Called after the child's DEVICE_DETACH() method to allow the parent
202 * to reclaim any resources allocated on behalf of the child.
204 * @param _dev the device whose child changed state
205 * @param _child the child device which changed state
207 METHOD void child_detached {
213 * @brief Notify a bus that a new driver was added
215 * Called when a new driver is added to the devclass which owns this
216 * bus. The generic implementation of this method attempts to probe and
217 * attach any un-matched children of the bus.
219 * @param _dev the device whose devclass had a new driver
221 * @param _driver the new driver which was added
223 METHOD void driver_added {
226 } DEFAULT bus_generic_driver_added;
229 * @brief Create a new child device
231 * For buses which use use drivers supporting DEVICE_IDENTIFY() to
232 * enumerate their devices, this method is used to create new
233 * device instances. The new device will be added after the last
234 * existing child with the same order. Implementations of bus_add_child
235 * call device_add_child_ordered to add the child and often add
236 * a suitable ivar to the device specific to that bus.
238 * @param _dev the bus device which will be the parent of the
240 * @param _order a value which is used to partially sort the
241 * children of @p _dev - devices created using
242 * lower values of @p _order appear first in @p
243 * _dev's list of children
244 * @param _name devclass name for new device or @c NULL if not
246 * @param _unit unit number for new device or @c -1 if not
249 METHOD device_t add_child {
254 } DEFAULT null_add_child;
257 * @brief Rescan the bus
259 * This method is called by a parent bridge or devctl to trigger a bus
260 * rescan. The rescan should delete devices no longer present and
261 * enumerate devices that have newly arrived.
263 * @param _dev the bus device
270 * @brief Allocate a system resource
272 * This method is called by child devices of a bus to allocate resources.
273 * The types are defined in <machine/resource.h>; the meaning of the
274 * resource-ID field varies from bus to bus (but @p *rid == 0 is always
275 * valid if the resource type is). If a resource was allocated and the
276 * caller did not use the RF_ACTIVE to specify that it should be
277 * activated immediately, the caller is responsible for calling
278 * BUS_ACTIVATE_RESOURCE() when it actually uses the resource.
280 * @param _dev the parent device of @p _child
281 * @param _child the device which is requesting an allocation
282 * @param _type the type of resource to allocate
283 * @param _rid a pointer to the resource identifier
284 * @param _start hint at the start of the resource range - pass
285 * @c 0 for any start address
286 * @param _end hint at the end of the resource range - pass
287 * @c ~0 for any end address
288 * @param _count hint at the size of range required - pass @c 1
290 * @param _flags any extra flags to control the resource
291 * allocation - see @c RF_XXX flags in
292 * <sys/rman.h> for details
294 * @returns the resource which was allocated or @c NULL if no
295 * resource could be allocated
297 METHOD struct resource * alloc_resource {
306 } DEFAULT null_alloc_resource;
309 * @brief Activate a resource
311 * Activate a resource previously allocated with
312 * BUS_ALLOC_RESOURCE(). This may enable decoding of this resource in a
313 * device for instance. It will also establish a mapping for the resource
314 * unless RF_UNMAPPED was set when allocating the resource.
316 * @param _dev the parent device of @p _child
317 * @param _child the device which allocated the resource
318 * @param _type the type of resource
319 * @param _rid the resource identifier
320 * @param _r the resource to activate
322 METHOD int activate_resource {
332 * @brief Map a resource
334 * Allocate a mapping for a range of an active resource. The mapping
335 * is described by a struct resource_map object. This may for instance
336 * map a memory region into the kernel's virtual address space.
338 * @param _dev the parent device of @p _child
339 * @param _child the device which allocated the resource
340 * @param _type the type of resource
341 * @param _r the resource to map
342 * @param _args optional attributes of the mapping
343 * @param _map the mapping
345 METHOD int map_resource {
350 struct resource_map_request *_args;
351 struct resource_map *_map;
352 } DEFAULT bus_generic_map_resource;
356 * @brief Unmap a resource
358 * Release a mapping previously allocated with
359 * BUS_MAP_RESOURCE(). This may for instance unmap a memory region
360 * from the kernel's virtual address space.
362 * @param _dev the parent device of @p _child
363 * @param _child the device which allocated the resource
364 * @param _type the type of resource
365 * @param _r the resource
366 * @param _map the mapping to release
368 METHOD int unmap_resource {
373 struct resource_map *_map;
374 } DEFAULT bus_generic_unmap_resource;
378 * @brief Deactivate a resource
380 * Deactivate a resource previously allocated with
381 * BUS_ALLOC_RESOURCE().
383 * @param _dev the parent device of @p _child
384 * @param _child the device which allocated the resource
385 * @param _type the type of resource
386 * @param _rid the resource identifier
387 * @param _r the resource to deactivate
389 METHOD int deactivate_resource {
398 * @brief Adjust a resource
400 * Adjust the start and/or end of a resource allocated by
401 * BUS_ALLOC_RESOURCE. At least part of the new address range must overlap
402 * with the existing address range. If the successful, the resource's range
403 * will be adjusted to [start, end] on return.
405 * @param _dev the parent device of @p _child
406 * @param _child the device which allocated the resource
407 * @param _type the type of resource
408 * @param _res the resource to adjust
409 * @param _start the new starting address of the resource range
410 * @param _end the new ending address of the resource range
412 METHOD int adjust_resource {
416 struct resource *_res;
423 * @brief translate a resource value
426 * @param _dev the device associated with the resource
427 * @param _type the type of resource
428 * @param _start the starting address of the resource range
429 * @param _newstart the new starting address of the resource range
431 METHOD int translate_resource {
435 rman_res_t *_newstart;
436 } DEFAULT null_translate_resource;
439 * @brief Release a resource
441 * Free a resource allocated by the BUS_ALLOC_RESOURCE. The @p _rid
442 * value must be the same as the one returned by BUS_ALLOC_RESOURCE()
443 * (which is not necessarily the same as the one the client passed).
445 * @param _dev the parent device of @p _child
446 * @param _child the device which allocated the resource
447 * @param _type the type of resource
448 * @param _rid the resource identifier
449 * @param _r the resource to release
451 METHOD int release_resource {
456 struct resource *_res;
460 * @brief Install an interrupt handler
462 * This method is used to associate an interrupt handler function with
463 * an irq resource. When the interrupt triggers, the function @p _intr
464 * will be called with the value of @p _arg as its single
465 * argument. The value returned in @p *_cookiep is used to cancel the
466 * interrupt handler - the caller should save this value to use in a
467 * future call to BUS_TEARDOWN_INTR().
469 * @param _dev the parent device of @p _child
470 * @param _child the device which allocated the resource
471 * @param _irq the resource representing the interrupt
472 * @param _flags a set of bits from enum intr_type specifying
473 * the class of interrupt
474 * @param _intr the function to call when the interrupt
476 * @param _arg a value to use as the single argument in calls
478 * @param _cookiep a pointer to a location to receive a cookie
479 * value that may be used to remove the interrupt
482 METHOD int setup_intr {
485 struct resource *_irq;
487 driver_filter_t *_filter;
488 driver_intr_t *_intr;
494 * @brief Uninstall an interrupt handler
496 * This method is used to disassociate an interrupt handler function
497 * with an irq resource. The value of @p _cookie must be the value
498 * returned from a previous call to BUS_SETUP_INTR().
500 * @param _dev the parent device of @p _child
501 * @param _child the device which allocated the resource
502 * @param _irq the resource representing the interrupt
503 * @param _cookie the cookie value returned when the interrupt
504 * was originally registered
506 METHOD int teardown_intr {
509 struct resource *_irq;
514 * @brief Suspend an interrupt handler
516 * This method is used to mark a handler as suspended in the case
517 * that the associated device is powered down and cannot be a source
518 * for the, typically shared, interrupt.
519 * The value of @p _irq must be the interrupt resource passed
520 * to a previous call to BUS_SETUP_INTR().
522 * @param _dev the parent device of @p _child
523 * @param _child the device which allocated the resource
524 * @param _irq the resource representing the interrupt
526 METHOD int suspend_intr {
529 struct resource *_irq;
530 } DEFAULT bus_generic_suspend_intr;
533 * @brief Resume an interrupt handler
535 * This method is used to clear suspended state of a handler when
536 * the associated device is powered up and can be an interrupt source
538 * The value of @p _irq must be the interrupt resource passed
539 * to a previous call to BUS_SETUP_INTR().
541 * @param _dev the parent device of @p _child
542 * @param _child the device which allocated the resource
543 * @param _irq the resource representing the interrupt
545 METHOD int resume_intr {
548 struct resource *_irq;
549 } DEFAULT bus_generic_resume_intr;
552 * @brief Define a resource which can be allocated with
553 * BUS_ALLOC_RESOURCE().
555 * This method is used by some buses (typically ISA) to allow a
556 * driver to describe a resource range that it would like to
557 * allocate. The resource defined by @p _type and @p _rid is defined
558 * to start at @p _start and to include @p _count indices in its
561 * @param _dev the parent device of @p _child
562 * @param _child the device which owns the resource
563 * @param _type the type of resource
564 * @param _rid the resource identifier
565 * @param _start the start of the resource range
566 * @param _count the size of the resource range
568 METHOD int set_resource {
578 * @brief Describe a resource
580 * This method allows a driver to examine the range used for a given
581 * resource without actually allocating it.
583 * @param _dev the parent device of @p _child
584 * @param _child the device which owns the resource
585 * @param _type the type of resource
586 * @param _rid the resource identifier
587 * @param _start the address of a location to receive the start
588 * index of the resource range
589 * @param _count the address of a location to receive the size
590 * of the resource range
592 METHOD int get_resource {
602 * @brief Delete a resource.
604 * Use this to delete a resource (possibly one previously added with
605 * BUS_SET_RESOURCE()).
607 * @param _dev the parent device of @p _child
608 * @param _child the device which owns the resource
609 * @param _type the type of resource
610 * @param _rid the resource identifier
612 METHOD void delete_resource {
620 * @brief Return a struct resource_list.
622 * Used by drivers which use bus_generic_rl_alloc_resource() etc. to
623 * implement their resource handling. It should return the resource
624 * list of the given child device.
626 * @param _dev the parent device of @p _child
627 * @param _child the device which owns the resource list
629 METHOD struct resource_list * get_resource_list {
632 } DEFAULT bus_generic_get_resource_list;
635 * @brief Is the hardware described by @p _child still attached to the
638 * This method should return 0 if the device is not present. It
639 * should return -1 if it is present. Any errors in determining
640 * should be returned as a normal errno value. Client drivers are to
641 * assume that the device is present, even if there is an error
642 * determining if it is there. Buses are to try to avoid returning
643 * errors, but newcard will return an error if the device fails to
644 * implement this method.
646 * @param _dev the parent device of @p _child
647 * @param _child the device which is being examined
649 METHOD int child_present {
652 } DEFAULT bus_generic_child_present;
655 * @brief Returns the pnp info for this device.
657 * Return it as a string. If the storage is insufficient for the
658 * string, then return EOVERFLOW.
660 * The string must be formatted as a space-separated list of
661 * name=value pairs. Names may only contain alphanumeric characters,
662 * underscores ('_') and hyphens ('-'). Values can contain any
663 * non-whitespace characters. Values containing whitespace can be
664 * quoted with double quotes ('"'). Double quotes and backslashes in
665 * quoted values can be escaped with backslashes ('\').
667 * @param _dev the parent device of @p _child
668 * @param _child the device which is being examined
669 * @param _buf the address of a buffer to receive the pnp
671 * @param _buflen the size of the buffer pointed to by @p _buf
673 METHOD int child_pnpinfo_str {
681 * @brief Returns the location for this device.
683 * Return it as a string. If the storage is insufficient for the
684 * string, then return EOVERFLOW.
686 * The string must be formatted as a space-separated list of
687 * name=value pairs. Names may only contain alphanumeric characters,
688 * underscores ('_') and hyphens ('-'). Values can contain any
689 * non-whitespace characters. Values containing whitespace can be
690 * quoted with double quotes ('"'). Double quotes and backslashes in
691 * quoted values can be escaped with backslashes ('\').
693 * @param _dev the parent device of @p _child
694 * @param _child the device which is being examined
695 * @param _buf the address of a buffer to receive the location
697 * @param _buflen the size of the buffer pointed to by @p _buf
699 METHOD int child_location_str {
707 * @brief Allow drivers to request that an interrupt be bound to a specific
710 * @param _dev the parent device of @p _child
711 * @param _child the device which allocated the resource
712 * @param _irq the resource representing the interrupt
713 * @param _cpu the CPU to bind the interrupt to
715 METHOD int bind_intr {
718 struct resource *_irq;
720 } DEFAULT bus_generic_bind_intr;
723 * @brief Allow (bus) drivers to specify the trigger mode and polarity
724 * of the specified interrupt.
726 * @param _dev the bus device
727 * @param _irq the interrupt number to modify
728 * @param _trig the trigger mode required
729 * @param _pol the interrupt polarity required
731 METHOD int config_intr {
734 enum intr_trigger _trig;
735 enum intr_polarity _pol;
736 } DEFAULT bus_generic_config_intr;
739 * @brief Allow drivers to associate a description with an active
742 * @param _dev the parent device of @p _child
743 * @param _child the device which allocated the resource
744 * @param _irq the resource representing the interrupt
745 * @param _cookie the cookie value returned when the interrupt
746 * was originally registered
747 * @param _descr the description to associate with the interrupt
749 METHOD int describe_intr {
752 struct resource *_irq;
755 } DEFAULT bus_generic_describe_intr;
758 * @brief Notify a (bus) driver about a child that the hints mechanism
759 * believes it has discovered.
761 * The bus is responsible for then adding the child in the right order
762 * and discovering other things about the child. The bus driver is
763 * free to ignore this hint, to do special things, etc. It is all up
764 * to the bus driver to interpret.
766 * This method is only called in response to the parent bus asking for
767 * hinted devices to be enumerated.
769 * @param _dev the bus device
770 * @param _dname the name of the device w/o unit numbers
771 * @param _dunit the unit number of the device
773 METHOD void hinted_child {
780 * @brief Returns bus_dma_tag_t for use w/ devices on the bus.
782 * @param _dev the parent device of @p _child
783 * @param _child the device to which the tag will belong
785 METHOD bus_dma_tag_t get_dma_tag {
788 } DEFAULT bus_generic_get_dma_tag;
791 * @brief Returns bus_space_tag_t for use w/ devices on the bus.
793 * @param _dev the parent device of @p _child
794 * @param _child the device to which the tag will belong
796 METHOD bus_space_tag_t get_bus_tag {
799 } DEFAULT bus_generic_get_bus_tag;
802 * @brief Allow the bus to determine the unit number of a device.
804 * @param _dev the parent device of @p _child
805 * @param _child the device whose unit is to be wired
806 * @param _name the name of the device's new devclass
807 * @param _unitp a pointer to the device's new unit value
809 METHOD void hint_device_unit {
817 * @brief Notify a bus that the bus pass level has been changed
819 * @param _dev the bus device
821 METHOD void new_pass {
823 } DEFAULT bus_generic_new_pass;
826 * @brief Notify a bus that specified child's IRQ should be remapped.
828 * @param _dev the bus device
829 * @param _child the child device
830 * @param _irq the irq number
832 METHOD int remap_intr {
836 } DEFAULT null_remap_intr;
839 * @brief Suspend a given child
841 * @param _dev the parent device of @p _child
842 * @param _child the device to suspend
844 METHOD int suspend_child {
847 } DEFAULT bus_generic_suspend_child;
850 * @brief Resume a given child
852 * @param _dev the parent device of @p _child
853 * @param _child the device to resume
855 METHOD int resume_child {
858 } DEFAULT bus_generic_resume_child;
861 * @brief Get the VM domain handle for the given bus and child.
863 * @param _dev the bus device
864 * @param _child the child device
865 * @param _domain a pointer to the bus's domain handle identifier
867 METHOD int get_domain {
871 } DEFAULT bus_generic_get_domain;
874 * @brief Request a set of CPUs
876 * @param _dev the bus device
877 * @param _child the child device
878 * @param _op type of CPUs to request
879 * @param _setsize the size of the set passed in _cpuset
880 * @param _cpuset a pointer to a cpuset to receive the requested
883 METHOD int get_cpus {
888 struct _cpuset *_cpuset;
889 } DEFAULT bus_generic_get_cpus;
892 * @brief Prepares the given child of the bus for reset
894 * Typically bus detaches or suspends children' drivers, and then
895 * calls this method to save bus-specific information, for instance,
896 * PCI config space, which is damaged by reset.
898 * The bus_helper_reset_prepare() helper is provided to ease
899 * implementing bus reset methods.
901 * @param _dev the bus device
902 * @param _child the child device
904 METHOD int reset_prepare {
907 } DEFAULT null_reset_prepare;
910 * @brief Restores the child operations after the reset
912 * The bus_helper_reset_post() helper is provided to ease
913 * implementing bus reset methods.
915 * @param _dev the bus device
916 * @param _child the child device
918 METHOD int reset_post {
921 } DEFAULT null_reset_post;
924 * @brief Performs reset of the child
926 * @param _dev the bus device
927 * @param _child the child device
928 * @param _flags DEVF_RESET_ flags
930 METHOD int reset_child {