1 /*- 2 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000 BSDi 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 #include "bus_if.h" 32 #include <sys/eventhandler.h> 33 #include <sys/sysctl.h> 34 35 extern devclass_t acpi_devclass; 36 37 struct acpi_softc { 38 device_t acpi_dev; 39 dev_t acpi_dev_t; 40 41 struct resource *acpi_irq; 42 int acpi_irq_rid; 43 void *acpi_irq_handle; 44 45 int acpi_enabled; 46 int acpi_sstate; 47 48 struct sysctl_ctx_list acpi_sysctl_ctx; 49 struct sysctl_oid *acpi_sysctl_tree; 50 #define ACPI_POWER_BUTTON_DEFAULT_SX ACPI_STATE_S5; 51 #define ACPI_SLEEP_BUTTON_DEFAULT_SX ACPI_STATE_S1; 52 #define ACPI_LID_SWITCH_DEFAULT_SX ACPI_STATE_S1; 53 int acpi_power_button_sx; 54 int acpi_sleep_button_sx; 55 int acpi_lid_switch_sx; 56 57 struct sysctl_ctx_list acpi_battery_sysctl_ctx; 58 struct sysctl_oid *acpi_battery_sysctl_tree; 59 }; 60 61 struct acpi_device { 62 /* ACPI ivars */ 63 ACPI_HANDLE ad_handle; 64 int ad_magic; 65 void *ad_private; 66 67 /* resources */ 68 struct resource_list ad_rl; 69 70 }; 71 72 /* 73 * The ACPI subsystem lives under a single mutex. You *must* 74 * acquire this mutex before calling any of the acpi_ or Acpi* functions. 75 * 76 * XXX the ACPI_MSLEEP macro should go away once locking is resolved 77 */ 78 extern struct mtx acpi_mutex; 79 #if 0 80 # define ACPI_LOCK mtx_lock(&acpi_mutex) 81 # define ACPI_UNLOCK mtx_unlock(&acpi_mutex) 82 # define ACPI_ASSERTLOCK mtx_assert(&acpi_mutex, MA_OWNED) 83 # define ACPI_MSLEEP(a, b, c, d, e) msleep(a, b, c, d, e) 84 #else 85 # define ACPI_LOCK 86 # define ACPI_UNLOCK 87 # define ACPI_ASSERTLOCK 88 # define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e) 89 #endif 90 91 /* 92 * This is a cheap and nasty way to get around the horrid counted list 93 * argument format that AcpiEvalateObject uses. 94 */ 95 #define ACPI_OBJECTLIST_MAX 16 96 struct acpi_object_list { 97 UINT32 count; 98 ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX]; 99 ACPI_OBJECT object[ACPI_OBJECTLIST_MAX]; 100 }; 101 102 static __inline struct acpi_object_list * 103 acpi_AllocObjectList(int nobj) { 104 struct acpi_object_list *l; 105 int i; 106 107 if (nobj > ACPI_OBJECTLIST_MAX) 108 return(NULL); 109 if ((l = AcpiOsAllocate(sizeof(*l))) == NULL) 110 return(NULL); 111 bzero(l, sizeof(*l)); 112 for (i = 0; i < ACPI_OBJECTLIST_MAX; i++) 113 l->pointer[i] = &l->object[i]; 114 l->count = nobj; 115 return(l); 116 } 117 118 #define ACPI_IVAR_HANDLE 0x100 119 #define ACPI_IVAR_MAGIC 0x101 120 #define ACPI_IVAR_PRIVATE 0x102 121 122 static __inline ACPI_HANDLE 123 acpi_get_handle(device_t dev) { 124 ACPI_HANDLE h; 125 126 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, (uintptr_t *)&h)) 127 return(NULL); 128 return(h); 129 } 130 131 static __inline int 132 acpi_set_handle(device_t dev, ACPI_HANDLE h) { 133 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, (uintptr_t)h)); 134 } 135 136 static __inline int 137 acpi_get_magic(device_t dev) { 138 int m; 139 140 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, (uintptr_t *)&m)) 141 return(0); 142 return(m); 143 } 144 145 static __inline int 146 acpi_set_magic(device_t dev, int m) { 147 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, (uintptr_t)m)); 148 } 149 150 static __inline void * 151 acpi_get_private(device_t dev) { 152 void *p; 153 154 if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, (uintptr_t *)&p)) 155 return(NULL); 156 return(p); 157 } 158 159 static __inline int 160 acpi_set_private(device_t dev, void *p) { 161 return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, (uintptr_t)p)); 162 } 163 164 static __inline ACPI_OBJECT_TYPE 165 acpi_get_type(device_t dev) { 166 ACPI_HANDLE h; 167 ACPI_OBJECT_TYPE t; 168 169 if ((h = acpi_get_handle(dev)) == NULL) 170 return(ACPI_TYPE_NOT_FOUND); 171 if (AcpiGetType(h, &t) != AE_OK) 172 return(ACPI_TYPE_NOT_FOUND); 173 return(t); 174 } 175 176 #ifdef ENABLE_DEBUGGER 177 extern void acpi_EnterDebugger(void); 178 #endif 179 180 #ifdef ACPI_DEBUG 181 #include <sys/cons.h> 182 #define STEP(x) do {printf x, printf("\n"); cngetc();} while (0) 183 #else 184 #define STEP(x) 185 #endif 186 187 extern BOOLEAN acpi_DeviceIsPresent(device_t dev); 188 extern BOOLEAN acpi_MatchHid(device_t dev, char *hid); 189 extern ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result); 190 extern ACPI_BUFFER *acpi_AllocBuffer(int size); 191 extern ACPI_STATUS acpi_GetIntoBuffer(ACPI_HANDLE handle, 192 ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), 193 ACPI_BUFFER *buf); 194 extern ACPI_STATUS acpi_EvaluateIntoBuffer(ACPI_HANDLE object, ACPI_STRING pathname, 195 ACPI_OBJECT_LIST *params, ACPI_BUFFER *buf); 196 extern ACPI_STATUS acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number); 197 extern ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj, 198 void (* func)(ACPI_OBJECT *comp, void *arg), 199 void *arg); 200 extern ACPI_STATUS acpi_FindIndexedResource(ACPI_RESOURCE *resbuf, int index, ACPI_RESOURCE **resp); 201 202 extern ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state); 203 extern ACPI_STATUS acpi_Enable(struct acpi_softc *sc); 204 extern ACPI_STATUS acpi_Disable(struct acpi_softc *sc); 205 206 struct acpi_parse_resource_set { 207 void (* set_init)(device_t dev, void **context); 208 void (* set_done)(device_t dev, void *context); 209 void (* set_ioport)(device_t dev, void *context, u_int32_t base, u_int32_t length); 210 void (* set_iorange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 211 u_int32_t length, u_int32_t align); 212 void (* set_memory)(device_t dev, void *context, u_int32_t base, u_int32_t length); 213 void (* set_memoryrange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 214 u_int32_t length, u_int32_t align); 215 void (* set_irq)(device_t dev, void *context, u_int32_t irq); 216 void (* set_drq)(device_t dev, void *context, u_int32_t drq); 217 void (* set_start_dependant)(device_t dev, void *context, int preference); 218 void (* set_end_dependant)(device_t dev, void *context); 219 }; 220 221 extern struct acpi_parse_resource_set acpi_res_parse_set; 222 extern ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle, 223 struct acpi_parse_resource_set *set); 224 225 /* XXX this is ugly */ 226 extern char *acpi_strerror(ACPI_STATUS excep); 227 228 /* 229 * ACPI event handling 230 */ 231 extern UINT32 acpi_eventhandler_power_button_for_sleep(void *context); 232 extern UINT32 acpi_eventhandler_power_button_for_wakeup(void *context); 233 extern UINT32 acpi_eventhandler_sleep_button_for_sleep(void *context); 234 extern UINT32 acpi_eventhandler_sleep_button_for_wakeup(void *context); 235 236 #define ACPI_EVENT_PRI_FIRST 0 237 #define ACPI_EVENT_PRI_DEFAULT 10000 238 #define ACPI_EVENT_PRI_LAST 20000 239 240 typedef void (*acpi_event_handler_t) __P((void *, int)); 241 242 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t); 243 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t); 244 245 /* 246 * Device power control. 247 */ 248 extern ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state); 249 250 /* 251 * Misc. 252 */ 253 static __inline struct acpi_softc * 254 acpi_device_get_parent_softc(device_t child) 255 { 256 device_t parent; 257 258 parent = device_get_parent(child); 259 if (parent == NULL) { 260 return(NULL); 261 } 262 return(device_get_softc(parent)); 263 } 264 265 extern char *acpi_name(ACPI_HANDLE handle); 266 extern int acpi_avoid(ACPI_HANDLE handle); 267 extern int acpi_disabled(char *subsys); 268 269 /* 270 * Battery Abstruction and Generalized Power Management interface. 271 */ 272 struct acpi_battinfo; 273 274 extern int acpi_battery_register(int, int); 275 extern int acpi_acad_get_acline(void); 276 extern int acpi_cmbat_get_battinfo(int, struct acpi_battinfo *); 277 278