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