1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1997,1998,2003 Doug Rabson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31#ifndef _SYS_BUS_H_
32#define _SYS_BUS_H_
33
34#include <machine/_limits.h>
35#include <machine/_bus.h>
36#include <sys/_bus_dma.h>
37#include <sys/ioccom.h>
38
39/**
40 * @defgroup NEWBUS newbus - a generic framework for managing devices
41 * @{
42 */
43
44/**
45 * @brief Interface information structure.
46 */
47struct u_businfo {
48	int	ub_version;		/**< @brief interface version */
49#define BUS_USER_VERSION	2
50	int	ub_generation;		/**< @brief generation count */
51};
52
53/**
54 * @brief State of the device.
55 */
56typedef enum device_state {
57	DS_NOTPRESENT = 10,		/**< @brief not probed or probe failed */
58	DS_ALIVE = 20,			/**< @brief probe succeeded */
59	DS_ATTACHING = 25,		/**< @brief currently attaching */
60	DS_ATTACHED = 30,		/**< @brief attach method called */
61	DS_BUSY = 40			/**< @brief device is open */
62} device_state_t;
63
64/**
65 * @brief Device information exported to userspace.
66 * The strings are placed one after the other, separated by NUL characters.
67 * Fields should be added after the last one and order maintained for compatibility
68 */
69#define BUS_USER_BUFFER		(3*1024)
70struct u_device {
71	uintptr_t	dv_handle;
72	uintptr_t	dv_parent;
73	uint32_t	dv_devflags;		/**< @brief API Flags for device */
74	uint16_t	dv_flags;		/**< @brief flags for dev state */
75	device_state_t	dv_state;		/**< @brief State of attachment */
76	char		dv_fields[BUS_USER_BUFFER]; /**< @brief NUL terminated fields */
77	/* name (name of the device in tree) */
78	/* desc (driver description) */
79	/* drivername (Name of driver without unit number) */
80	/* pnpinfo (Plug and play information from bus) */
81	/* location (Location of device on parent */
82	/* NUL */
83};
84
85/* Flags exported via dv_flags. */
86#define	DF_ENABLED	0x01		/* device should be probed/attached */
87#define	DF_FIXEDCLASS	0x02		/* devclass specified at create time */
88#define	DF_WILDCARD	0x04		/* unit was originally wildcard */
89#define	DF_DESCMALLOCED	0x08		/* description was malloced */
90#define	DF_QUIET	0x10		/* don't print verbose attach message */
91#define	DF_DONENOMATCH	0x20		/* don't execute DEVICE_NOMATCH again */
92#define	DF_EXTERNALSOFTC 0x40		/* softc not allocated by us */
93#define	DF_REBID	0x80		/* Can rebid after attach */
94#define	DF_SUSPENDED	0x100		/* Device is suspended. */
95#define	DF_QUIET_CHILDREN 0x200		/* Default to quiet for all my children */
96#define	DF_ATTACHED_ONCE 0x400		/* Has been attached at least once */
97#define	DF_NEEDNOMATCH	0x800		/* Has a pending NOMATCH event */
98
99/**
100 * @brief Device request structure used for ioctl's.
101 *
102 * Used for ioctl's on /dev/devctl2.  All device ioctl's
103 * must have parameter definitions which begin with dr_name.
104 */
105struct devreq_buffer {
106	void	*buffer;
107	size_t	length;
108};
109
110struct devreq {
111	char		dr_name[128];
112	int		dr_flags;		/* request-specific flags */
113	union {
114		struct devreq_buffer dru_buffer;
115		void	*dru_data;
116	} dr_dru;
117#define	dr_buffer	dr_dru.dru_buffer	/* variable-sized buffer */
118#define	dr_data		dr_dru.dru_data		/* fixed-size buffer */
119};
120
121#define	DEV_ATTACH	_IOW('D', 1, struct devreq)
122#define	DEV_DETACH	_IOW('D', 2, struct devreq)
123#define	DEV_ENABLE	_IOW('D', 3, struct devreq)
124#define	DEV_DISABLE	_IOW('D', 4, struct devreq)
125#define	DEV_SUSPEND	_IOW('D', 5, struct devreq)
126#define	DEV_RESUME	_IOW('D', 6, struct devreq)
127#define	DEV_SET_DRIVER	_IOW('D', 7, struct devreq)
128#define	DEV_CLEAR_DRIVER _IOW('D', 8, struct devreq)
129#define	DEV_RESCAN	_IOW('D', 9, struct devreq)
130#define	DEV_DELETE	_IOW('D', 10, struct devreq)
131#define	DEV_FREEZE	_IOW('D', 11, struct devreq)
132#define	DEV_THAW	_IOW('D', 12, struct devreq)
133#define	DEV_RESET	_IOW('D', 13, struct devreq)
134
135/* Flags for DEV_DETACH and DEV_DISABLE. */
136#define	DEVF_FORCE_DETACH	0x0000001
137
138/* Flags for DEV_SET_DRIVER. */
139#define	DEVF_SET_DRIVER_DETACH	0x0000001	/* Detach existing driver. */
140
141/* Flags for DEV_CLEAR_DRIVER. */
142#define	DEVF_CLEAR_DRIVER_DETACH 0x0000001	/* Detach existing driver. */
143
144/* Flags for DEV_DELETE. */
145#define	DEVF_FORCE_DELETE	0x0000001
146
147/* Flags for DEV_RESET */
148#define	DEVF_RESET_DETACH	0x0000001	/* Detach drivers vs suspend
149						   device */
150
151#ifdef _KERNEL
152
153#include <sys/_eventhandler.h>
154#include <sys/kobj.h>
155#include <sys/systm.h>
156#include <sys/devctl.h>
157
158/**
159 * Device name parsers.  Hook to allow device enumerators to map
160 * scheme-specific names to a device.
161 */
162typedef void (*dev_lookup_fn)(void *arg, const char *name,
163    device_t *result);
164EVENTHANDLER_DECLARE(dev_lookup, dev_lookup_fn);
165
166/**
167 * @brief A device driver (included mainly for compatibility with
168 * FreeBSD 4.x).
169 */
170typedef struct kobj_class	driver_t;
171
172/**
173 * @brief A device class
174 *
175 * The devclass object has two main functions in the system. The first
176 * is to manage the allocation of unit numbers for device instances
177 * and the second is to hold the list of device drivers for a
178 * particular bus type. Each devclass has a name and there cannot be
179 * two devclasses with the same name. This ensures that unique unit
180 * numbers are allocated to device instances.
181 *
182 * Drivers that support several different bus attachments (e.g. isa,
183 * pci, pccard) should all use the same devclass to ensure that unit
184 * numbers do not conflict.
185 *
186 * Each devclass may also have a parent devclass. This is used when
187 * searching for device drivers to allow a form of inheritance. When
188 * matching drivers with devices, first the driver list of the parent
189 * device's devclass is searched. If no driver is found in that list,
190 * the search continues in the parent devclass (if any).
191 */
192typedef struct devclass		*devclass_t;
193
194/**
195 * @brief A device method
196 */
197#define device_method_t		kobj_method_t
198
199/**
200 * @brief Driver interrupt filter return values
201 *
202 * If a driver provides an interrupt filter routine it must return an
203 * integer consisting of oring together zero or more of the following
204 * flags:
205 *
206 *	FILTER_STRAY	- this device did not trigger the interrupt
207 *	FILTER_HANDLED	- the interrupt has been fully handled and can be EOId
208 *	FILTER_SCHEDULE_THREAD - the threaded interrupt handler should be
209 *			  scheduled to execute
210 *
211 * If the driver does not provide a filter, then the interrupt code will
212 * act is if the filter had returned FILTER_SCHEDULE_THREAD.  Note that it
213 * is illegal to specify any other flag with FILTER_STRAY and that it is
214 * illegal to not specify either of FILTER_HANDLED or FILTER_SCHEDULE_THREAD
215 * if FILTER_STRAY is not specified.
216 */
217#define	FILTER_STRAY		0x01
218#define	FILTER_HANDLED		0x02
219#define	FILTER_SCHEDULE_THREAD	0x04
220
221/**
222 * @brief Driver interrupt service routines
223 *
224 * The filter routine is run in primary interrupt context and may not
225 * block or use regular mutexes.  It may only use spin mutexes for
226 * synchronization.  The filter may either completely handle the
227 * interrupt or it may perform some of the work and defer more
228 * expensive work to the regular interrupt handler.  If a filter
229 * routine is not registered by the driver, then the regular interrupt
230 * handler is always used to handle interrupts from this device.
231 *
232 * The regular interrupt handler executes in its own thread context
233 * and may use regular mutexes.  However, it is prohibited from
234 * sleeping on a sleep queue.
235 */
236typedef int driver_filter_t(void*);
237typedef void driver_intr_t(void*);
238
239/**
240 * @brief Interrupt type bits.
241 *
242 * These flags are used both by newbus interrupt
243 * registration (nexus.c) and also in struct intrec, which defines
244 * interrupt properties.
245 *
246 * XXX We should probably revisit this and remove the vestiges of the
247 * spls implicit in names like INTR_TYPE_TTY. In the meantime, don't
248 * confuse things by renaming them (Grog, 18 July 2000).
249 *
250 * Buses which do interrupt remapping will want to change their type
251 * to reflect what sort of devices are underneath.
252 */
253enum intr_type {
254	INTR_TYPE_TTY = 1,
255	INTR_TYPE_BIO = 2,
256	INTR_TYPE_NET = 4,
257	INTR_TYPE_CAM = 8,
258	INTR_TYPE_MISC = 16,
259	INTR_TYPE_CLK = 32,
260	INTR_TYPE_AV = 64,
261	INTR_EXCL = 256,		/* exclusive interrupt */
262	INTR_MPSAFE = 512,		/* this interrupt is SMP safe */
263	INTR_ENTROPY = 1024,		/* this interrupt provides entropy */
264	INTR_MD1 = 4096,		/* flag reserved for MD use */
265	INTR_MD2 = 8192,		/* flag reserved for MD use */
266	INTR_MD3 = 16384,		/* flag reserved for MD use */
267	INTR_MD4 = 32768		/* flag reserved for MD use */
268};
269
270enum intr_trigger {
271	INTR_TRIGGER_INVALID = -1,
272	INTR_TRIGGER_CONFORM = 0,
273	INTR_TRIGGER_EDGE = 1,
274	INTR_TRIGGER_LEVEL = 2
275};
276
277enum intr_polarity {
278	INTR_POLARITY_CONFORM = 0,
279	INTR_POLARITY_HIGH = 1,
280	INTR_POLARITY_LOW = 2
281};
282
283/**
284 * CPU sets supported by bus_get_cpus().  Note that not all sets may be
285 * supported for a given device.  If a request is not supported by a
286 * device (or its parents), then bus_get_cpus() will fail with EINVAL.
287 */
288enum cpu_sets {
289	LOCAL_CPUS = 0,
290	INTR_CPUS
291};
292
293typedef int (*devop_t)(void);
294
295/**
296 * @brief This structure is deprecated.
297 *
298 * Use the kobj(9) macro DEFINE_CLASS to
299 * declare classes which implement device drivers.
300 */
301struct driver {
302	KOBJ_CLASS_FIELDS;
303};
304
305/**
306 * @brief A resource mapping.
307 */
308struct resource_map {
309	bus_space_tag_t r_bustag;
310	bus_space_handle_t r_bushandle;
311	bus_size_t r_size;
312	void	*r_vaddr;
313};
314
315/**
316 * @brief Optional properties of a resource mapping request.
317 */
318struct resource_map_request {
319	size_t	size;
320	rman_res_t offset;
321	rman_res_t length;
322	vm_memattr_t memattr;
323};
324
325void	resource_init_map_request_impl(struct resource_map_request *_args,
326	    size_t _sz);
327#define	resource_init_map_request(rmr) 					\
328	resource_init_map_request_impl((rmr), sizeof(*(rmr)))
329
330/*
331 * Definitions for drivers which need to keep simple lists of resources
332 * for their child devices.
333 */
334struct	resource;
335
336/**
337 * @brief An entry for a single resource in a resource list.
338 */
339struct resource_list_entry {
340	STAILQ_ENTRY(resource_list_entry) link;
341	int	type;			/**< @brief type argument to alloc_resource */
342	int	rid;			/**< @brief resource identifier */
343	int	flags;			/**< @brief resource flags */
344	struct	resource *res;		/**< @brief the real resource when allocated */
345	rman_res_t	start;		/**< @brief start of resource range */
346	rman_res_t	end;		/**< @brief end of resource range */
347	rman_res_t	count;			/**< @brief count within range */
348};
349STAILQ_HEAD(resource_list, resource_list_entry);
350
351#define	RLE_RESERVED		0x0001	/* Reserved by the parent bus. */
352#define	RLE_ALLOCATED		0x0002	/* Reserved resource is allocated. */
353#define	RLE_PREFETCH		0x0004	/* Resource is a prefetch range. */
354
355void	resource_list_init(struct resource_list *rl);
356void	resource_list_free(struct resource_list *rl);
357struct resource_list_entry *
358	resource_list_add(struct resource_list *rl,
359			  int type, int rid,
360			  rman_res_t start, rman_res_t end, rman_res_t count);
361int	resource_list_add_next(struct resource_list *rl,
362			  int type,
363			  rman_res_t start, rman_res_t end, rman_res_t count);
364int	resource_list_busy(struct resource_list *rl,
365			   int type, int rid);
366int	resource_list_reserved(struct resource_list *rl, int type, int rid);
367struct resource_list_entry*
368	resource_list_find(struct resource_list *rl,
369			   int type, int rid);
370void	resource_list_delete(struct resource_list *rl,
371			     int type, int rid);
372struct resource *
373	resource_list_alloc(struct resource_list *rl,
374			    device_t bus, device_t child,
375			    int type, int *rid,
376			    rman_res_t start, rman_res_t end,
377			    rman_res_t count, u_int flags);
378int	resource_list_release(struct resource_list *rl,
379			      device_t bus, device_t child,
380			      int type, int rid, struct resource *res);
381int	resource_list_release_active(struct resource_list *rl,
382				     device_t bus, device_t child,
383				     int type);
384struct resource *
385	resource_list_reserve(struct resource_list *rl,
386			      device_t bus, device_t child,
387			      int type, int *rid,
388			      rman_res_t start, rman_res_t end,
389			      rman_res_t count, u_int flags);
390int	resource_list_unreserve(struct resource_list *rl,
391				device_t bus, device_t child,
392				int type, int rid);
393void	resource_list_purge(struct resource_list *rl);
394int	resource_list_print_type(struct resource_list *rl,
395				 const char *name, int type,
396				 const char *format);
397
398/*
399 * The root bus, to which all top-level buses are attached.
400 */
401extern device_t root_bus;
402extern devclass_t root_devclass;
403void	root_bus_configure(void);
404
405/*
406 * Useful functions for implementing buses.
407 */
408
409struct _cpuset;
410
411int	bus_generic_activate_resource(device_t dev, device_t child, int type,
412				      int rid, struct resource *r);
413device_t
414	bus_generic_add_child(device_t dev, u_int order, const char *name,
415			      int unit);
416int	bus_generic_adjust_resource(device_t bus, device_t child, int type,
417				    struct resource *r, rman_res_t start,
418				    rman_res_t end);
419struct resource *
420	bus_generic_alloc_resource(device_t bus, device_t child, int type,
421				   int *rid, rman_res_t start, rman_res_t end,
422				   rman_res_t count, u_int flags);
423int	bus_generic_translate_resource(device_t dev, int type, rman_res_t start,
424			      rman_res_t *newstart);
425int	bus_generic_attach(device_t dev);
426int	bus_generic_bind_intr(device_t dev, device_t child,
427			      struct resource *irq, int cpu);
428int	bus_generic_child_present(device_t dev, device_t child);
429int	bus_generic_config_intr(device_t, int, enum intr_trigger,
430				enum intr_polarity);
431int	bus_generic_describe_intr(device_t dev, device_t child,
432				  struct resource *irq, void *cookie,
433				  const char *descr);
434int	bus_generic_deactivate_resource(device_t dev, device_t child, int type,
435					int rid, struct resource *r);
436int	bus_generic_detach(device_t dev);
437void	bus_generic_driver_added(device_t dev, driver_t *driver);
438int	bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
439			     size_t setsize, struct _cpuset *cpuset);
440bus_dma_tag_t
441	bus_generic_get_dma_tag(device_t dev, device_t child);
442bus_space_tag_t
443	bus_generic_get_bus_tag(device_t dev, device_t child);
444int	bus_generic_get_domain(device_t dev, device_t child, int *domain);
445struct resource_list *
446	bus_generic_get_resource_list (device_t, device_t);
447int	bus_generic_map_resource(device_t dev, device_t child, int type,
448				 struct resource *r,
449				 struct resource_map_request *args,
450				 struct resource_map *map);
451void	bus_generic_new_pass(device_t dev);
452int	bus_print_child_header(device_t dev, device_t child);
453int	bus_print_child_domain(device_t dev, device_t child);
454int	bus_print_child_footer(device_t dev, device_t child);
455int	bus_generic_print_child(device_t dev, device_t child);
456int	bus_generic_probe(device_t dev);
457int	bus_generic_read_ivar(device_t dev, device_t child, int which,
458			      uintptr_t *result);
459int	bus_generic_release_resource(device_t bus, device_t child,
460				     int type, int rid, struct resource *r);
461int	bus_generic_resume(device_t dev);
462int	bus_generic_resume_child(device_t dev, device_t child);
463int	bus_generic_setup_intr(device_t dev, device_t child,
464			       struct resource *irq, int flags,
465			       driver_filter_t *filter, driver_intr_t *intr,
466			       void *arg, void **cookiep);
467
468struct resource *
469	bus_generic_rl_alloc_resource (device_t, device_t, int, int *,
470				       rman_res_t, rman_res_t, rman_res_t, u_int);
471void	bus_generic_rl_delete_resource (device_t, device_t, int, int);
472int	bus_generic_rl_get_resource (device_t, device_t, int, int, rman_res_t *,
473				     rman_res_t *);
474int	bus_generic_rl_set_resource (device_t, device_t, int, int, rman_res_t,
475				     rman_res_t);
476int	bus_generic_rl_release_resource (device_t, device_t, int, int,
477					 struct resource *);
478
479int	bus_generic_shutdown(device_t dev);
480int	bus_generic_suspend(device_t dev);
481int	bus_generic_suspend_child(device_t dev, device_t child);
482int	bus_generic_teardown_intr(device_t dev, device_t child,
483				  struct resource *irq, void *cookie);
484int	bus_generic_suspend_intr(device_t dev, device_t child,
485				  struct resource *irq);
486int	bus_generic_resume_intr(device_t dev, device_t child,
487				  struct resource *irq);
488int	bus_generic_unmap_resource(device_t dev, device_t child, int type,
489				   struct resource *r,
490				   struct resource_map *map);
491int	bus_generic_write_ivar(device_t dev, device_t child, int which,
492			       uintptr_t value);
493int	bus_helper_reset_post(device_t dev, int flags);
494int	bus_helper_reset_prepare(device_t dev, int flags);
495int	bus_null_rescan(device_t dev);
496
497/*
498 * Wrapper functions for the BUS_*_RESOURCE methods to make client code
499 * a little simpler.
500 */
501
502struct resource_spec {
503	int	type;
504	int	rid;
505	int	flags;
506};
507#define	RESOURCE_SPEC_END	{-1, 0, 0}
508
509int	bus_alloc_resources(device_t dev, struct resource_spec *rs,
510			    struct resource **res);
511void	bus_release_resources(device_t dev, const struct resource_spec *rs,
512			      struct resource **res);
513
514int	bus_adjust_resource(device_t child, int type, struct resource *r,
515			    rman_res_t start, rman_res_t end);
516struct	resource *bus_alloc_resource(device_t dev, int type, int *rid,
517				     rman_res_t start, rman_res_t end,
518				     rman_res_t count, u_int flags);
519int	bus_activate_resource(device_t dev, int type, int rid,
520			      struct resource *r);
521int	bus_deactivate_resource(device_t dev, int type, int rid,
522				struct resource *r);
523int	bus_map_resource(device_t dev, int type, struct resource *r,
524			 struct resource_map_request *args,
525			 struct resource_map *map);
526int	bus_unmap_resource(device_t dev, int type, struct resource *r,
527			   struct resource_map *map);
528int	bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize,
529		     struct _cpuset *cpuset);
530bus_dma_tag_t bus_get_dma_tag(device_t dev);
531bus_space_tag_t bus_get_bus_tag(device_t dev);
532int	bus_get_domain(device_t dev, int *domain);
533int	bus_release_resource(device_t dev, int type, int rid,
534			     struct resource *r);
535int	bus_free_resource(device_t dev, int type, struct resource *r);
536int	bus_setup_intr(device_t dev, struct resource *r, int flags,
537		       driver_filter_t filter, driver_intr_t handler,
538		       void *arg, void **cookiep);
539int	bus_teardown_intr(device_t dev, struct resource *r, void *cookie);
540int	bus_suspend_intr(device_t dev, struct resource *r);
541int	bus_resume_intr(device_t dev, struct resource *r);
542int	bus_bind_intr(device_t dev, struct resource *r, int cpu);
543int	bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
544			  const char *fmt, ...) __printflike(4, 5);
545int	bus_set_resource(device_t dev, int type, int rid,
546			 rman_res_t start, rman_res_t count);
547int	bus_get_resource(device_t dev, int type, int rid,
548			 rman_res_t *startp, rman_res_t *countp);
549rman_res_t	bus_get_resource_start(device_t dev, int type, int rid);
550rman_res_t	bus_get_resource_count(device_t dev, int type, int rid);
551void	bus_delete_resource(device_t dev, int type, int rid);
552int	bus_child_present(device_t child);
553int	bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen);
554int	bus_child_location_str(device_t child, char *buf, size_t buflen);
555void	bus_enumerate_hinted_children(device_t bus);
556int	bus_delayed_attach_children(device_t bus);
557
558static __inline struct resource *
559bus_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
560{
561	return (bus_alloc_resource(dev, type, rid, 0, ~0, 1, flags));
562}
563
564static __inline struct resource *
565bus_alloc_resource_anywhere(device_t dev, int type, int *rid,
566    rman_res_t count, u_int flags)
567{
568	return (bus_alloc_resource(dev, type, rid, 0, ~0, count, flags));
569}
570
571/*
572 * Access functions for device.
573 */
574device_t	device_add_child(device_t dev, const char *name, int unit);
575device_t	device_add_child_ordered(device_t dev, u_int order,
576					 const char *name, int unit);
577void	device_busy(device_t dev);
578int	device_delete_child(device_t dev, device_t child);
579int	device_delete_children(device_t dev);
580int	device_attach(device_t dev);
581int	device_detach(device_t dev);
582void	device_disable(device_t dev);
583void	device_enable(device_t dev);
584device_t	device_find_child(device_t dev, const char *classname,
585				  int unit);
586const char	*device_get_desc(device_t dev);
587devclass_t	device_get_devclass(device_t dev);
588driver_t	*device_get_driver(device_t dev);
589u_int32_t	device_get_flags(device_t dev);
590device_t	device_get_parent(device_t dev);
591int	device_get_children(device_t dev, device_t **listp, int *countp);
592void	*device_get_ivars(device_t dev);
593void	device_set_ivars(device_t dev, void *ivars);
594const	char *device_get_name(device_t dev);
595const	char *device_get_nameunit(device_t dev);
596void	*device_get_softc(device_t dev);
597device_state_t	device_get_state(device_t dev);
598int	device_get_unit(device_t dev);
599struct sysctl_ctx_list *device_get_sysctl_ctx(device_t dev);
600struct sysctl_oid *device_get_sysctl_tree(device_t dev);
601int	device_has_quiet_children(device_t dev);
602int	device_is_alive(device_t dev);	/* did probe succeed? */
603int	device_is_attached(device_t dev);	/* did attach succeed? */
604int	device_is_enabled(device_t dev);
605int	device_is_suspended(device_t dev);
606int	device_is_quiet(device_t dev);
607device_t device_lookup_by_name(const char *name);
608int	device_print_prettyname(device_t dev);
609int	device_printf(device_t dev, const char *, ...) __printflike(2, 3);
610int	device_probe(device_t dev);
611int	device_probe_and_attach(device_t dev);
612int	device_probe_child(device_t bus, device_t dev);
613int	device_quiesce(device_t dev);
614void	device_quiet(device_t dev);
615void	device_quiet_children(device_t dev);
616void	device_set_desc(device_t dev, const char* desc);
617void	device_set_desc_copy(device_t dev, const char* desc);
618int	device_set_devclass(device_t dev, const char *classname);
619int	device_set_devclass_fixed(device_t dev, const char *classname);
620bool	device_is_devclass_fixed(device_t dev);
621int	device_set_driver(device_t dev, driver_t *driver);
622void	device_set_flags(device_t dev, u_int32_t flags);
623void	device_set_softc(device_t dev, void *softc);
624void	device_free_softc(void *softc);
625void	device_claim_softc(device_t dev);
626int	device_set_unit(device_t dev, int unit);	/* XXX DONT USE XXX */
627int	device_shutdown(device_t dev);
628void	device_unbusy(device_t dev);
629void	device_verbose(device_t dev);
630
631/*
632 * Access functions for devclass.
633 */
634int		devclass_add_driver(devclass_t dc, driver_t *driver,
635				    int pass, devclass_t *dcp);
636devclass_t	devclass_create(const char *classname);
637int		devclass_delete_driver(devclass_t busclass, driver_t *driver);
638devclass_t	devclass_find(const char *classname);
639const char	*devclass_get_name(devclass_t dc);
640device_t	devclass_get_device(devclass_t dc, int unit);
641void	*devclass_get_softc(devclass_t dc, int unit);
642int	devclass_get_devices(devclass_t dc, device_t **listp, int *countp);
643int	devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp);
644int	devclass_get_count(devclass_t dc);
645int	devclass_get_maxunit(devclass_t dc);
646int	devclass_find_free_unit(devclass_t dc, int unit);
647void	devclass_set_parent(devclass_t dc, devclass_t pdc);
648devclass_t	devclass_get_parent(devclass_t dc);
649struct sysctl_ctx_list *devclass_get_sysctl_ctx(devclass_t dc);
650struct sysctl_oid *devclass_get_sysctl_tree(devclass_t dc);
651
652/*
653 * Access functions for device resources.
654 */
655int	resource_int_value(const char *name, int unit, const char *resname,
656			   int *result);
657int	resource_long_value(const char *name, int unit, const char *resname,
658			    long *result);
659int	resource_string_value(const char *name, int unit, const char *resname,
660			      const char **result);
661int	resource_disabled(const char *name, int unit);
662int	resource_find_match(int *anchor, const char **name, int *unit,
663			    const char *resname, const char *value);
664int	resource_find_dev(int *anchor, const char *name, int *unit,
665			  const char *resname, const char *value);
666int	resource_unset_value(const char *name, int unit, const char *resname);
667
668/*
669 * Functions for maintaining and checking consistency of
670 * bus information exported to userspace.
671 */
672int	bus_data_generation_check(int generation);
673void	bus_data_generation_update(void);
674
675/**
676 * Some convenience defines for probe routines to return.  These are just
677 * suggested values, and there's nothing magical about them.
678 * BUS_PROBE_SPECIFIC is for devices that cannot be reprobed, and that no
679 * possible other driver may exist (typically legacy drivers who don't follow
680 * all the rules, or special needs drivers).  BUS_PROBE_VENDOR is the
681 * suggested value that vendor supplied drivers use.  This is for source or
682 * binary drivers that are not yet integrated into the FreeBSD tree.  Its use
683 * in the base OS is prohibited.  BUS_PROBE_DEFAULT is the normal return value
684 * for drivers to use.  It is intended that nearly all of the drivers in the
685 * tree should return this value.  BUS_PROBE_LOW_PRIORITY are for drivers that
686 * have special requirements like when there are two drivers that support
687 * overlapping series of hardware devices.  In this case the one that supports
688 * the older part of the line would return this value, while the one that
689 * supports the newer ones would return BUS_PROBE_DEFAULT.  BUS_PROBE_GENERIC
690 * is for drivers that wish to have a generic form and a specialized form,
691 * like is done with the pci bus and the acpi pci bus.  BUS_PROBE_HOOVER is
692 * for those buses that implement a generic device placeholder for devices on
693 * the bus that have no more specific driver for them (aka ugen).
694 * BUS_PROBE_NOWILDCARD or lower means that the device isn't really bidding
695 * for a device node, but accepts only devices that its parent has told it
696 * use this driver.
697 */
698#define BUS_PROBE_SPECIFIC	0	/* Only I can use this device */
699#define BUS_PROBE_VENDOR	(-10)	/* Vendor supplied driver */
700#define BUS_PROBE_DEFAULT	(-20)	/* Base OS default driver */
701#define BUS_PROBE_LOW_PRIORITY	(-40)	/* Older, less desirable drivers */
702#define BUS_PROBE_GENERIC	(-100)	/* generic driver for dev */
703#define BUS_PROBE_HOOVER	(-1000000) /* Driver for any dev on bus */
704#define BUS_PROBE_NOWILDCARD	(-2000000000) /* No wildcard device matches */
705
706/**
707 * During boot, the device tree is scanned multiple times.  Each scan,
708 * or pass, drivers may be attached to devices.  Each driver
709 * attachment is assigned a pass number.  Drivers may only probe and
710 * attach to devices if their pass number is less than or equal to the
711 * current system-wide pass number.  The default pass is the last pass
712 * and is used by most drivers.  Drivers needed by the scheduler are
713 * probed in earlier passes.
714 */
715#define	BUS_PASS_ROOT		0	/* Used to attach root0. */
716#define	BUS_PASS_BUS		10	/* Buses and bridges. */
717#define	BUS_PASS_CPU		20	/* CPU devices. */
718#define	BUS_PASS_RESOURCE	30	/* Resource discovery. */
719#define	BUS_PASS_INTERRUPT	40	/* Interrupt controllers. */
720#define	BUS_PASS_TIMER		50	/* Timers and clocks. */
721#define	BUS_PASS_SCHEDULER	60	/* Start scheduler. */
722#define	BUS_PASS_SUPPORTDEV	100000	/* Drivers which support DEFAULT drivers. */
723#define	BUS_PASS_DEFAULT	__INT_MAX /* Everything else. */
724
725#define	BUS_PASS_ORDER_FIRST	0
726#define	BUS_PASS_ORDER_EARLY	2
727#define	BUS_PASS_ORDER_MIDDLE	5
728#define	BUS_PASS_ORDER_LATE	7
729#define	BUS_PASS_ORDER_LAST	9
730
731extern int bus_current_pass;
732
733void	bus_set_pass(int pass);
734
735/**
736 * Shorthands for constructing method tables.
737 */
738#define	DEVMETHOD	KOBJMETHOD
739#define	DEVMETHOD_END	KOBJMETHOD_END
740
741/*
742 * Some common device interfaces.
743 */
744#include "device_if.h"
745#include "bus_if.h"
746
747struct	module;
748
749int	driver_module_handler(struct module *, int, void *);
750
751/**
752 * Module support for automatically adding drivers to buses.
753 */
754struct driver_module_data {
755	int		(*dmd_chainevh)(struct module *, int, void *);
756	void		*dmd_chainarg;
757	const char	*dmd_busname;
758	kobj_class_t	dmd_driver;
759	devclass_t	*dmd_devclass;
760	int		dmd_pass;
761};
762
763#define	EARLY_DRIVER_MODULE_ORDERED(name, busname, driver, devclass,	\
764    evh, arg, order, pass)						\
765									\
766static struct driver_module_data name##_##busname##_driver_mod = {	\
767	evh, arg,							\
768	#busname,							\
769	(kobj_class_t) &driver,						\
770	&devclass,							\
771	pass								\
772};									\
773									\
774static moduledata_t name##_##busname##_mod = {				\
775	#busname "/" #name,						\
776	driver_module_handler,						\
777	&name##_##busname##_driver_mod					\
778};									\
779DECLARE_MODULE(name##_##busname, name##_##busname##_mod,		\
780	       SI_SUB_DRIVERS, order)
781
782#define	EARLY_DRIVER_MODULE(name, busname, driver, devclass, evh, arg, pass) \
783	EARLY_DRIVER_MODULE_ORDERED(name, busname, driver, devclass,	\
784	    evh, arg, SI_ORDER_MIDDLE, pass)
785
786#define	DRIVER_MODULE_ORDERED(name, busname, driver, devclass, evh, arg,\
787    order)								\
788	EARLY_DRIVER_MODULE_ORDERED(name, busname, driver, devclass,	\
789	    evh, arg, order, BUS_PASS_DEFAULT)
790
791#define	DRIVER_MODULE(name, busname, driver, devclass, evh, arg)	\
792	EARLY_DRIVER_MODULE(name, busname, driver, devclass, evh, arg,	\
793	    BUS_PASS_DEFAULT)
794
795/**
796 * Generic ivar accessor generation macros for bus drivers
797 */
798#define __BUS_ACCESSOR(varp, var, ivarp, ivar, type)			\
799									\
800static __inline type varp ## _get_ ## var(device_t dev)			\
801{									\
802	uintptr_t v;							\
803	int e;								\
804	e = BUS_READ_IVAR(device_get_parent(dev), dev,			\
805	    ivarp ## _IVAR_ ## ivar, &v);				\
806	KASSERT(e == 0, ("%s failed for %s on bus %s, error = %d",	\
807	    __func__, device_get_nameunit(dev),				\
808	    device_get_nameunit(device_get_parent(dev)), e));		\
809	return ((type) v);						\
810}									\
811									\
812static __inline void varp ## _set_ ## var(device_t dev, type t)		\
813{									\
814	uintptr_t v = (uintptr_t) t;					\
815	int e;								\
816	e = BUS_WRITE_IVAR(device_get_parent(dev), dev,			\
817	    ivarp ## _IVAR_ ## ivar, v);				\
818	KASSERT(e == 0, ("%s failed for %s on bus %s, error = %d",	\
819	    __func__, device_get_nameunit(dev),				\
820	    device_get_nameunit(device_get_parent(dev)), e));		\
821}
822
823/**
824 * Shorthand macros, taking resource argument
825 * Generated with sys/tools/bus_macro.sh
826 */
827
828#define bus_barrier(r, o, l, f) \
829	bus_space_barrier((r)->r_bustag, (r)->r_bushandle, (o), (l), (f))
830#define bus_poke_1(r, o, v) \
831	bus_space_poke_1((r)->r_bustag, (r)->r_bushandle, (o), (v))
832#define bus_peek_1(r, o, vp) \
833	bus_space_peek_1((r)->r_bustag, (r)->r_bushandle, (o), (vp))
834#define bus_read_1(r, o) \
835	bus_space_read_1((r)->r_bustag, (r)->r_bushandle, (o))
836#define bus_read_multi_1(r, o, d, c) \
837	bus_space_read_multi_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
838#define bus_read_region_1(r, o, d, c) \
839	bus_space_read_region_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
840#define bus_set_multi_1(r, o, v, c) \
841	bus_space_set_multi_1((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
842#define bus_set_region_1(r, o, v, c) \
843	bus_space_set_region_1((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
844#define bus_write_1(r, o, v) \
845	bus_space_write_1((r)->r_bustag, (r)->r_bushandle, (o), (v))
846#define bus_write_multi_1(r, o, d, c) \
847	bus_space_write_multi_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
848#define bus_write_region_1(r, o, d, c) \
849	bus_space_write_region_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
850#define bus_read_stream_1(r, o) \
851	bus_space_read_stream_1((r)->r_bustag, (r)->r_bushandle, (o))
852#define bus_read_multi_stream_1(r, o, d, c) \
853	bus_space_read_multi_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
854#define bus_read_region_stream_1(r, o, d, c) \
855	bus_space_read_region_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
856#define bus_set_multi_stream_1(r, o, v, c) \
857	bus_space_set_multi_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
858#define bus_set_region_stream_1(r, o, v, c) \
859	bus_space_set_region_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
860#define bus_write_stream_1(r, o, v) \
861	bus_space_write_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (v))
862#define bus_write_multi_stream_1(r, o, d, c) \
863	bus_space_write_multi_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
864#define bus_write_region_stream_1(r, o, d, c) \
865	bus_space_write_region_stream_1((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
866#define bus_poke_2(r, o, v) \
867	bus_space_poke_2((r)->r_bustag, (r)->r_bushandle, (o), (v))
868#define bus_peek_2(r, o, vp) \
869	bus_space_peek_2((r)->r_bustag, (r)->r_bushandle, (o), (vp))
870#define bus_read_2(r, o) \
871	bus_space_read_2((r)->r_bustag, (r)->r_bushandle, (o))
872#define bus_read_multi_2(r, o, d, c) \
873	bus_space_read_multi_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
874#define bus_read_region_2(r, o, d, c) \
875	bus_space_read_region_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
876#define bus_set_multi_2(r, o, v, c) \
877	bus_space_set_multi_2((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
878#define bus_set_region_2(r, o, v, c) \
879	bus_space_set_region_2((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
880#define bus_write_2(r, o, v) \
881	bus_space_write_2((r)->r_bustag, (r)->r_bushandle, (o), (v))
882#define bus_write_multi_2(r, o, d, c) \
883	bus_space_write_multi_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
884#define bus_write_region_2(r, o, d, c) \
885	bus_space_write_region_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
886#define bus_read_stream_2(r, o) \
887	bus_space_read_stream_2((r)->r_bustag, (r)->r_bushandle, (o))
888#define bus_read_multi_stream_2(r, o, d, c) \
889	bus_space_read_multi_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
890#define bus_read_region_stream_2(r, o, d, c) \
891	bus_space_read_region_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
892#define bus_set_multi_stream_2(r, o, v, c) \
893	bus_space_set_multi_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
894#define bus_set_region_stream_2(r, o, v, c) \
895	bus_space_set_region_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
896#define bus_write_stream_2(r, o, v) \
897	bus_space_write_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (v))
898#define bus_write_multi_stream_2(r, o, d, c) \
899	bus_space_write_multi_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
900#define bus_write_region_stream_2(r, o, d, c) \
901	bus_space_write_region_stream_2((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
902#define bus_poke_4(r, o, v) \
903	bus_space_poke_4((r)->r_bustag, (r)->r_bushandle, (o), (v))
904#define bus_peek_4(r, o, vp) \
905	bus_space_peek_4((r)->r_bustag, (r)->r_bushandle, (o), (vp))
906#define bus_read_4(r, o) \
907	bus_space_read_4((r)->r_bustag, (r)->r_bushandle, (o))
908#define bus_read_multi_4(r, o, d, c) \
909	bus_space_read_multi_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
910#define bus_read_region_4(r, o, d, c) \
911	bus_space_read_region_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
912#define bus_set_multi_4(r, o, v, c) \
913	bus_space_set_multi_4((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
914#define bus_set_region_4(r, o, v, c) \
915	bus_space_set_region_4((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
916#define bus_write_4(r, o, v) \
917	bus_space_write_4((r)->r_bustag, (r)->r_bushandle, (o), (v))
918#define bus_write_multi_4(r, o, d, c) \
919	bus_space_write_multi_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
920#define bus_write_region_4(r, o, d, c) \
921	bus_space_write_region_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
922#define bus_read_stream_4(r, o) \
923	bus_space_read_stream_4((r)->r_bustag, (r)->r_bushandle, (o))
924#define bus_read_multi_stream_4(r, o, d, c) \
925	bus_space_read_multi_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
926#define bus_read_region_stream_4(r, o, d, c) \
927	bus_space_read_region_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
928#define bus_set_multi_stream_4(r, o, v, c) \
929	bus_space_set_multi_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
930#define bus_set_region_stream_4(r, o, v, c) \
931	bus_space_set_region_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
932#define bus_write_stream_4(r, o, v) \
933	bus_space_write_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (v))
934#define bus_write_multi_stream_4(r, o, d, c) \
935	bus_space_write_multi_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
936#define bus_write_region_stream_4(r, o, d, c) \
937	bus_space_write_region_stream_4((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
938#define bus_poke_8(r, o, v) \
939	bus_space_poke_8((r)->r_bustag, (r)->r_bushandle, (o), (v))
940#define bus_peek_8(r, o, vp) \
941	bus_space_peek_8((r)->r_bustag, (r)->r_bushandle, (o), (vp))
942#define bus_read_8(r, o) \
943	bus_space_read_8((r)->r_bustag, (r)->r_bushandle, (o))
944#define bus_read_multi_8(r, o, d, c) \
945	bus_space_read_multi_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
946#define bus_read_region_8(r, o, d, c) \
947	bus_space_read_region_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
948#define bus_set_multi_8(r, o, v, c) \
949	bus_space_set_multi_8((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
950#define bus_set_region_8(r, o, v, c) \
951	bus_space_set_region_8((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
952#define bus_write_8(r, o, v) \
953	bus_space_write_8((r)->r_bustag, (r)->r_bushandle, (o), (v))
954#define bus_write_multi_8(r, o, d, c) \
955	bus_space_write_multi_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
956#define bus_write_region_8(r, o, d, c) \
957	bus_space_write_region_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
958#define bus_read_stream_8(r, o) \
959	bus_space_read_stream_8((r)->r_bustag, (r)->r_bushandle, (o))
960#define bus_read_multi_stream_8(r, o, d, c) \
961	bus_space_read_multi_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
962#define bus_read_region_stream_8(r, o, d, c) \
963	bus_space_read_region_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
964#define bus_set_multi_stream_8(r, o, v, c) \
965	bus_space_set_multi_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
966#define bus_set_region_stream_8(r, o, v, c) \
967	bus_space_set_region_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (v), (c))
968#define bus_write_stream_8(r, o, v) \
969	bus_space_write_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (v))
970#define bus_write_multi_stream_8(r, o, d, c) \
971	bus_space_write_multi_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
972#define bus_write_region_stream_8(r, o, d, c) \
973	bus_space_write_region_stream_8((r)->r_bustag, (r)->r_bushandle, (o), (d), (c))
974#endif /* _KERNEL */
975
976#endif /* !_SYS_BUS_H_ */
977