bus_if.m revision 133588
1132354Sdfr 236973Sdfr# 3133588Simp# Copyright (c) 1998-2004 Doug Rabson 436973Sdfr# All rights reserved. 536973Sdfr# 636973Sdfr# Redistribution and use in source and binary forms, with or without 736973Sdfr# modification, are permitted provided that the following conditions 836973Sdfr# are met: 936973Sdfr# 1. Redistributions of source code must retain the above copyright 1036973Sdfr# notice, this list of conditions and the following disclaimer. 1136973Sdfr# 2. Redistributions in binary form must reproduce the above copyright 1236973Sdfr# notice, this list of conditions and the following disclaimer in the 1336973Sdfr# documentation and/or other materials provided with the distribution. 1436973Sdfr# 1536973Sdfr# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1636973Sdfr# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1736973Sdfr# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1836973Sdfr# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1936973Sdfr# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2036973Sdfr# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2136973Sdfr# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2236973Sdfr# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2336973Sdfr# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2436973Sdfr# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2536973Sdfr# SUCH DAMAGE. 2636973Sdfr# 2750477Speter# $FreeBSD: head/sys/kern/bus_if.m 133588 2004-08-12 17:26:22Z imp $ 2836973Sdfr# 2936973Sdfr 3059093Sdfr#include <sys/bus.h> 3159093Sdfr 32132354Sdfr/** 33132354Sdfr * @defgroup BUS bus - KObj methods for drivers of devices with children 34132354Sdfr * @brief A set of methods required device drivers that support 35132354Sdfr * child devices. 36132354Sdfr * @{ 37132354Sdfr */ 3841017SnsouchINTERFACE bus; 3936973Sdfr 4036973Sdfr# 4146913Sdfr# Default implementations of some methods. 4246913Sdfr# 4346913SdfrCODE { 4446913Sdfr static struct resource * 4546913Sdfr null_alloc_resource(device_t dev, device_t child, 46100421Simp int type, int *rid, u_long start, u_long end, 47100421Simp u_long count, u_int flags) 4846913Sdfr { 49100421Simp return (0); 5046913Sdfr } 5146913Sdfr}; 5246913Sdfr 53132354Sdfr/** 54132354Sdfr * @brief Print a description of a child device 55132354Sdfr * 56132354Sdfr * This is called from system code which prints out a description of a 57132354Sdfr * device. It should describe the attachment that the child has with 58132354Sdfr * the parent. For instance the TurboLaser bus prints which node the 59132354Sdfr * device is attached to. See bus_generic_print_child() for more 60132354Sdfr * information. 61132354Sdfr * 62132354Sdfr * @param _dev the device whose child is being printed 63132354Sdfr * @param _child the child device to describe 64132354Sdfr * 65132354Sdfr * @returns the number of characters output. 66132354Sdfr */ 6749195SmdoddMETHOD int print_child { 68132354Sdfr device_t _dev; 69132354Sdfr device_t _child; 70112588Smdodd} DEFAULT bus_generic_print_child; 7136973Sdfr 72132354Sdfr/** 73132354Sdfr * @brief Print a notification about an unprobed child device. 74132354Sdfr * 75132354Sdfr * Called for each child device that did not succeed in probing for a 76132354Sdfr * driver. 77132354Sdfr * 78132354Sdfr * @param _dev the device whose child was being probed 79132354Sdfr * @param _child the child device which failed to probe 80132354Sdfr */ 8148754SdfrMETHOD void probe_nomatch { 82132354Sdfr device_t _dev; 83132354Sdfr device_t _child; 8448754Sdfr}; 8548754Sdfr 86132354Sdfr/** 87132354Sdfr * @brief Read the value of a bus-specific attribute of a device 88132354Sdfr * 89132354Sdfr * This method, along with BUS_WRITE_IVAR() manages a bus-specific set 90132354Sdfr * of instance variables of a child device. The intention is that 91132354Sdfr * each different type of bus defines a set of appropriate instance 92132354Sdfr * variables (such as ports and irqs for ISA bus etc.) 93132354Sdfr * 94132354Sdfr * This information could be given to the child device as a struct but 95132354Sdfr * that makes it hard for a bus to add or remove variables without 96132354Sdfr * forcing an edit and recompile for all drivers which may not be 97132354Sdfr * possible for vendor supplied binary drivers. 98132354Sdfr * 99132354Sdfr * This method copies the value of an instance variable to the 100132354Sdfr * location specified by @p *_result. 101132354Sdfr * 102132354Sdfr * @param _dev the device whose child was being examined 103132354Sdfr * @param _child the child device whose instance variable is 104132354Sdfr * being read 105132354Sdfr * @param _index the instance variable to read 106132354Sdfr * @param _result a loction to recieve the instance variable 107132354Sdfr * value 108132354Sdfr * 109132354Sdfr * @retval 0 success 110132354Sdfr * @retval ENOENT no such instance variable is supported by @p 111132354Sdfr * _dev 112132354Sdfr */ 11336973SdfrMETHOD int read_ivar { 11495201Smarkm device_t _dev; 11595201Smarkm device_t _child; 116132354Sdfr int _index; 11795201Smarkm uintptr_t *_result; 11836973Sdfr}; 11936973Sdfr 120132354Sdfr/** 121132354Sdfr * @brief Write the value of a bus-specific attribute of a device 122132354Sdfr * 123132354Sdfr * This method sets the value of an instance variable to @p _value. 124132354Sdfr * 125132354Sdfr * @param _dev the device whose child was being updated 126132354Sdfr * @param _child the child device whose instance variable is 127132354Sdfr * being written 128132354Sdfr * @param _index the instance variable to write 129132354Sdfr * @param _value the value to write to that instance variable 130132354Sdfr * 131132354Sdfr * @retval 0 success 132132354Sdfr * @retval ENOENT no such instance variable is supported by @p 133132354Sdfr * _dev 134132354Sdfr * @retval EINVAL the instance variable was recognised but 135132354Sdfr * contains a read-only value 136132354Sdfr */ 13736973SdfrMETHOD int write_ivar { 13895201Smarkm device_t _dev; 13995201Smarkm device_t _child; 14095201Smarkm int _indx; 14195201Smarkm uintptr_t _value; 14236973Sdfr}; 14336973Sdfr 144132354Sdfr/** 145132354Sdfr * @brief Notify a bus that a child was detached 146132354Sdfr * 147132354Sdfr * Called after the child's DEVICE_DETACH() method to allow the parent 148132354Sdfr * to reclaim any resources allocated on behalf of the child. 149132354Sdfr * 150132354Sdfr * @param _dev the device whose child changed state 151132354Sdfr * @param _child the child device which changed state 152132354Sdfr */ 15345107SdfrMETHOD void child_detached { 15495201Smarkm device_t _dev; 15595201Smarkm device_t _child; 15645107Sdfr}; 15745107Sdfr 158132354Sdfr/** 159132354Sdfr * @brief Notify a bus that a new driver was added 160132354Sdfr * 161132354Sdfr * Called when a new driver is added to the devclass which owns this 162132354Sdfr * bus. The generic implementation of this method attempts to probe and 163132354Sdfr * attach any un-matched children of the bus. 164132354Sdfr * 165132354Sdfr * @param _dev the device whose devclass had a new driver 166132354Sdfr * added to it 167132354Sdfr * @param _driver the new driver which was added 168132354Sdfr */ 16945720SpeterMETHOD void driver_added { 17095201Smarkm device_t _dev; 17195201Smarkm driver_t *_driver; 17252045Simp} DEFAULT bus_generic_driver_added; 17345720Speter 174132354Sdfr/** 175132354Sdfr * @brief Create a new child device 176132354Sdfr * 177132354Sdfr * For busses which use use drivers supporting DEVICE_IDENTIFY() to 178132354Sdfr * enumerate their devices, this method is used to create new 179132354Sdfr * device instances. The new device will be added after the last 180132354Sdfr * existing child with the same order. 181132354Sdfr * 182132354Sdfr * @param _dev the bus device which will be the parent of the 183132354Sdfr * new child device 184132354Sdfr * @param _order a value which is used to partially sort the 185132354Sdfr * children of @p _dev - devices created using 186132354Sdfr * lower values of @p _order appear first in @p 187132354Sdfr * _dev's list of children 188132354Sdfr * @param _name devclass name for new device or @c NULL if not 189132354Sdfr * specified 190132354Sdfr * @param _unit unit number for new device or @c -1 if not 191132354Sdfr * specified 192132354Sdfr */ 19347178SdfrMETHOD device_t add_child { 19495201Smarkm device_t _dev; 19595201Smarkm int _order; 19695201Smarkm const char *_name; 19795201Smarkm int _unit; 19847178Sdfr}; 19947178Sdfr 200132354Sdfr/** 201132354Sdfr * @brief Allocate a system resource 202132354Sdfr * 203132354Sdfr * This method is called by child devices of a bus to allocate resources. 204132354Sdfr * The types are defined in <machine/resource.h>; the meaning of the 205132354Sdfr * resource-ID field varies from bus to bus (but @p *rid == 0 is always 206132354Sdfr * valid if the resource type is). If a resource was allocated and the 207132354Sdfr * caller did not use the RF_ACTIVE to specify that it should be 208132354Sdfr * activated immediately, the caller is responsible for calling 209132354Sdfr * BUS_ACTIVATE_RESOURCE() when it actually uses the resource. 210132354Sdfr * 211132354Sdfr * @param _dev the parent device of @p _child 212132354Sdfr * @param _child the device which is requesting an allocation 213132354Sdfr * @param _type the type of resource to allocate 214132354Sdfr * @param _rid a pointer to the resource identifier 215132354Sdfr * @param _start hint at the start of the resource range - pass 216132354Sdfr * @c 0UL for any start address 217132354Sdfr * @param _end hint at the end of the resource range - pass 218132354Sdfr * @c ~0UL for any end address 219132354Sdfr * @param _count hint at the size of range required - pass @c 1 220132354Sdfr * for any size 221132354Sdfr * @param _flags any extra flags to control the resource 222132354Sdfr * allocation - see @c RF_XXX flags in 223132354Sdfr * <sys/rman.h> for details 224132354Sdfr * 225132354Sdfr * @returns the resource which was allocated or @c NULL if no 226132354Sdfr * resource could be allocated 227132354Sdfr */ 22841153SwollmanMETHOD struct resource * alloc_resource { 22995201Smarkm device_t _dev; 23095201Smarkm device_t _child; 23195201Smarkm int _type; 23295201Smarkm int *_rid; 23395201Smarkm u_long _start; 23495201Smarkm u_long _end; 23595201Smarkm u_long _count; 23695201Smarkm u_int _flags; 23746913Sdfr} DEFAULT null_alloc_resource; 23837592Sdfr 239132354Sdfr/** 240132354Sdfr * @brief Activate a resource 241132354Sdfr * 242132354Sdfr * Activate a resource previously allocated with 243132354Sdfr * BUS_ALLOC_RESOURCE(). This may for instance map a memory region 244132354Sdfr * into the kernel's virtual address space. 245132354Sdfr * 246132354Sdfr * @param _dev the parent device of @p _child 247132354Sdfr * @param _child the device which allocated the resource 248132354Sdfr * @param _type the type of resource 249132354Sdfr * @param _rid the resource identifier 250132354Sdfr * @param _r the resource to activate 251132354Sdfr */ 25241153SwollmanMETHOD int activate_resource { 25395201Smarkm device_t _dev; 25495201Smarkm device_t _child; 25595201Smarkm int _type; 25695201Smarkm int _rid; 25795201Smarkm struct resource *_r; 25841153Swollman}; 25941153Swollman 260132354Sdfr/** 261132354Sdfr * @brief Deactivate a resource 262132354Sdfr * 263132354Sdfr * Deactivate a resource previously allocated with 264132354Sdfr * BUS_ALLOC_RESOURCE(). This may for instance unmap a memory region 265132354Sdfr * from the kernel's virtual address space. 266132354Sdfr * 267132354Sdfr * @param _dev the parent device of @p _child 268132354Sdfr * @param _child the device which allocated the resource 269132354Sdfr * @param _type the type of resource 270132354Sdfr * @param _rid the resource identifier 271132354Sdfr * @param _r the resource to deactivate 272132354Sdfr */ 27341153SwollmanMETHOD int deactivate_resource { 27495201Smarkm device_t _dev; 27595201Smarkm device_t _child; 27695201Smarkm int _type; 27795201Smarkm int _rid; 27895201Smarkm struct resource *_r; 27941153Swollman}; 28041153Swollman 281132354Sdfr/** 282132354Sdfr * @brief Release a resource 283132354Sdfr * 284132354Sdfr * Free a resource allocated by the BUS_ALLOC_RESOURCE. The @p _rid 285132354Sdfr * value must be the same as the one returned by BUS_ALLOC_RESOURCE() 286132354Sdfr * (which is not necessarily the same as the one the client passed). 287132354Sdfr * 288132354Sdfr * @param _dev the parent device of @p _child 289132354Sdfr * @param _child the device which allocated the resource 290132354Sdfr * @param _type the type of resource 291132354Sdfr * @param _rid the resource identifier 292132354Sdfr * @param _r the resource to release 293132354Sdfr */ 29441153SwollmanMETHOD int release_resource { 29595201Smarkm device_t _dev; 29695201Smarkm device_t _child; 29795201Smarkm int _type; 29895201Smarkm int _rid; 29995201Smarkm struct resource *_res; 30037592Sdfr}; 30141153Swollman 302132354Sdfr/** 303132354Sdfr * @brief Install an interrupt handler 304132354Sdfr * 305132354Sdfr * This method is used to associate an interrupt handler function with 306132354Sdfr * an irq resource. When the interrupt triggers, the function @p _intr 307132354Sdfr * will be called with the value of @p _arg as its single 308132354Sdfr * argument. The value returned in @p *_cookiep is used to cancel the 309132354Sdfr * interrupt handler - the caller should save this value to use in a 310132354Sdfr * future call to BUS_TEARDOWN_INTR(). 311132354Sdfr * 312132354Sdfr * @param _dev the parent device of @p _child 313132354Sdfr * @param _child the device which allocated the resource 314132354Sdfr * @param _irq the resource representing the interrupt 315132354Sdfr * @param _flags a set of bits from enum intr_type specifying 316132354Sdfr * the class of interrupt 317132354Sdfr * @param _intr the function to call when the interrupt 318132354Sdfr * triggers 319132354Sdfr * @param _arg a value to use as the single argument in calls 320132354Sdfr * to @p _intr 321132354Sdfr * @param _cookiep a pointer to a location to recieve a cookie 322132354Sdfr * value that may be used to remove the interrupt 323132354Sdfr * handler 324132354Sdfr */ 32541153SwollmanMETHOD int setup_intr { 32695201Smarkm device_t _dev; 32795201Smarkm device_t _child; 32895201Smarkm struct resource *_irq; 32995201Smarkm int _flags; 33095201Smarkm driver_intr_t *_intr; 33195201Smarkm void *_arg; 33295201Smarkm void **_cookiep; 33341153Swollman}; 33441153Swollman 335132354Sdfr/** 336132354Sdfr * @brief Uninstall an interrupt handler 337132354Sdfr * 338132354Sdfr * This method is used to disassociate an interrupt handler function 339132354Sdfr * with an irq resource. The value of @p _cookie must be the value 340132354Sdfr * returned from a previous call to BUS_SETUP_INTR(). 341132354Sdfr * 342132354Sdfr * @param _dev the parent device of @p _child 343132354Sdfr * @param _child the device which allocated the resource 344132354Sdfr * @param _irq the resource representing the interrupt 345132354Sdfr * @param _cookie the cookie value returned when the interrupt 346132354Sdfr * was originally registered 347132354Sdfr */ 34841153SwollmanMETHOD int teardown_intr { 34995201Smarkm device_t _dev; 35095201Smarkm device_t _child; 35195201Smarkm struct resource *_irq; 35295201Smarkm void *_cookie; 35341153Swollman}; 35452174Sdfr 355132354Sdfr/** 356132354Sdfr * @brief Define a resource which can be allocated with 357132354Sdfr * BUS_ALLOC_RESOURCE(). 358132354Sdfr * 359132354Sdfr * This method is used by some busses (typically ISA) to allow a 360132354Sdfr * driver to describe a resource range that it would like to 361132354Sdfr * allocate. The resource defined by @p _type and @p _rid is defined 362132354Sdfr * to start at @p _start and to include @p _count indices in its 363132354Sdfr * range. 364132354Sdfr * 365132354Sdfr * @param _dev the parent device of @p _child 366132354Sdfr * @param _child the device which owns the resource 367132354Sdfr * @param _type the type of resource 368132354Sdfr * @param _rid the resource identifier 369132354Sdfr * @param _start the start of the resource range 370132354Sdfr * @param _count the size of the resource range 371132354Sdfr */ 37252174SdfrMETHOD int set_resource { 37395201Smarkm device_t _dev; 37495201Smarkm device_t _child; 37595201Smarkm int _type; 37695201Smarkm int _rid; 37795201Smarkm u_long _start; 37895201Smarkm u_long _count; 37952174Sdfr}; 38052174Sdfr 381132354Sdfr/** 382132354Sdfr * @brief Describe a resource 383132354Sdfr * 384132354Sdfr * This method allows a driver to examine the range used for a given 385132354Sdfr * resource without actually allocating it. 386132354Sdfr * 387132354Sdfr * @param _dev the parent device of @p _child 388132354Sdfr * @param _child the device which owns the resource 389132354Sdfr * @param _type the type of resource 390132354Sdfr * @param _rid the resource identifier 391132354Sdfr * @param _start the address of a location to recieve the start 392132354Sdfr * index of the resource range 393132354Sdfr * @param _count the address of a location to recieve the size 394132354Sdfr * of the resource range 395132354Sdfr */ 39652174SdfrMETHOD int get_resource { 39795201Smarkm device_t _dev; 39895201Smarkm device_t _child; 39995201Smarkm int _type; 40095201Smarkm int _rid; 40195201Smarkm u_long *_startp; 40295201Smarkm u_long *_countp; 40352174Sdfr}; 40452174Sdfr 405132354Sdfr/** 406132354Sdfr * @brief Delete a resource. 407132354Sdfr * 408132354Sdfr * Use this to delete a resource (possibly one previously added with 409132354Sdfr * BUS_SET_RESOURCE()). 410132354Sdfr * 411132354Sdfr * @param _dev the parent device of @p _child 412132354Sdfr * @param _child the device which owns the resource 413132354Sdfr * @param _type the type of resource 414132354Sdfr * @param _rid the resource identifier 415132354Sdfr */ 41652174SdfrMETHOD void delete_resource { 41795201Smarkm device_t _dev; 41895201Smarkm device_t _child; 41995201Smarkm int _type; 42095201Smarkm int _rid; 42152174Sdfr}; 42267278Smdodd 423132354Sdfr/** 424132354Sdfr * @brief Return a struct resource_list. 425132354Sdfr * 426132354Sdfr * Used by drivers which use bus_generic_rl_alloc_resource() etc. to 427132354Sdfr * implement their resource handling. It should return the resource 428132354Sdfr * list of the given child device. 429132354Sdfr * 430132354Sdfr * @param _dev the parent device of @p _child 431132354Sdfr * @param _child the device which owns the resource list 432132354Sdfr */ 43369294SmdoddMETHOD struct resource_list * get_resource_list { 43495201Smarkm device_t _dev; 43595201Smarkm device_t _child; 43667278Smdodd} DEFAULT bus_generic_get_resource_list; 437100421Simp 438132354Sdfr/** 439132354Sdfr * @brief Is the hardware described by @p _child still attached to the 440132354Sdfr * system? 441132354Sdfr * 442133588Simp * This method should return 0 if the device is not present. It 443133588Simp * should return -1 if it is present. Any errors in determining 444133588Simp * should be returned as a normal errno value. Client drivers are to 445133588Simp * assume that the device is present, even if there is an error 446133588Simp * determining if it is there. Busses are to try to avoid returning 447133588Simp * errors, but newcard will return an error if the device fails to 448133588Simp * implement this method. 449132354Sdfr * 450132354Sdfr * @param _dev the parent device of @p _child 451132354Sdfr * @param _child the device which is being examined 452132354Sdfr */ 453100421SimpMETHOD int child_present { 454100421Simp device_t _dev; 455100421Simp device_t _child; 456100421Simp} DEFAULT bus_generic_child_present; 457104597Simp 458132354Sdfr/** 459132354Sdfr * @brief Returns the pnp info for this device. 460132354Sdfr * 461132354Sdfr * Return it as a string. If the string is insufficient for the 462132354Sdfr * storage, then return EOVERFLOW. 463132354Sdfr * 464132354Sdfr * @param _dev the parent device of @p _child 465132354Sdfr * @param _child the device which is being examined 466132354Sdfr * @param _buf the address of a buffer to receive the pnp 467132354Sdfr * string 468132354Sdfr * @param _buflen the size of the buffer pointed to by @p _buf 469132354Sdfr */ 470104597SimpMETHOD int child_pnpinfo_str { 471104597Simp device_t _dev; 472104597Simp device_t _child; 473104597Simp char *_buf; 474104597Simp size_t _buflen; 475104597Simp}; 476104597Simp 477132354Sdfr/** 478132354Sdfr * @brief Returns the location for this device. 479132354Sdfr * 480132354Sdfr * Return it as a string. If the string is insufficient for the 481132354Sdfr * storage, then return EOVERFLOW. 482132354Sdfr * 483132354Sdfr * @param _dev the parent device of @p _child 484132354Sdfr * @param _child the device which is being examined 485132354Sdfr * @param _buf the address of a buffer to receive the location 486132354Sdfr * string 487132354Sdfr * @param _buflen the size of the buffer pointed to by @p _buf 488132354Sdfr */ 489104597SimpMETHOD int child_location_str { 490104597Simp device_t _dev; 491104597Simp device_t _child; 492104597Simp char *_buf; 493104597Simp size_t _buflen; 494104597Simp}; 495119967Smarcel 496132354Sdfr/** 497132354Sdfr * @brief Allow (bus) drivers to specify the trigger mode and polarity 498132354Sdfr * of the specified interrupt. 499132354Sdfr * 500132354Sdfr * @param _dev the bus device 501132354Sdfr * @param _irq the interrupt number to modify 502132354Sdfr * @param _trig the trigger mode required 503132354Sdfr * @param _pol the interrupt polarity required 504132354Sdfr */ 505119967SmarcelMETHOD int config_intr { 506119967Smarcel device_t _dev; 507119967Smarcel int _irq; 508119967Smarcel enum intr_trigger _trig; 509119967Smarcel enum intr_polarity _pol; 510119967Smarcel} DEFAULT bus_generic_config_intr; 511