bus.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
  3. *
  4. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/sched.h>
  30. #include <linux/pm.h>
  31. #include <linux/device.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/acpi.h>
  34. #include <linux/slab.h>
  35. #ifdef CONFIG_X86
  36. #include <asm/mpspec.h>
  37. #endif
  38. #include <linux/pci.h>
  39. #include <acpi/acpi_bus.h>
  40. #include <acpi/acpi_drivers.h>
  41. #include <linux/dmi.h>
  42. #include "internal.h"
  43. #define _COMPONENT ACPI_BUS_COMPONENT
  44. ACPI_MODULE_NAME("bus");
  45. struct acpi_device *acpi_root;
  46. struct proc_dir_entry *acpi_root_dir;
  47. EXPORT_SYMBOL(acpi_root_dir);
  48. #define STRUCT_TO_INT(s) (*((int*)&s))
  49. static int set_power_nocheck(const struct dmi_system_id *id)
  50. {
  51. printk(KERN_NOTICE PREFIX "%s detected - "
  52. "disable power check in power transition\n", id->ident);
  53. acpi_power_nocheck = 1;
  54. return 0;
  55. }
  56. static struct dmi_system_id __cpuinitdata power_nocheck_dmi_table[] = {
  57. {
  58. set_power_nocheck, "HP Pavilion 05", {
  59. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
  60. DMI_MATCH(DMI_SYS_VENDOR, "HP Pavilion 05"),
  61. DMI_MATCH(DMI_PRODUCT_VERSION, "2001211RE101GLEND") }, NULL},
  62. {},
  63. };
  64. #ifdef CONFIG_X86
  65. static int set_copy_dsdt(const struct dmi_system_id *id)
  66. {
  67. printk(KERN_NOTICE "%s detected - "
  68. "force copy of DSDT to local memory\n", id->ident);
  69. acpi_gbl_copy_dsdt_locally = 1;
  70. return 0;
  71. }
  72. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  73. /*
  74. * Invoke DSDT corruption work-around on all Toshiba Satellite.
  75. * https://bugzilla.kernel.org/show_bug.cgi?id=14679
  76. */
  77. {
  78. .callback = set_copy_dsdt,
  79. .ident = "TOSHIBA Satellite",
  80. .matches = {
  81. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  82. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  83. },
  84. },
  85. {}
  86. };
  87. #else
  88. static struct dmi_system_id dsdt_dmi_table[] __initdata = {
  89. {}
  90. };
  91. #endif
  92. /* --------------------------------------------------------------------------
  93. Device Management
  94. -------------------------------------------------------------------------- */
  95. int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
  96. {
  97. acpi_status status = AE_OK;
  98. if (!device)
  99. return -EINVAL;
  100. /* TBD: Support fixed-feature devices */
  101. status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
  102. if (ACPI_FAILURE(status) || !*device) {
  103. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  104. handle));
  105. return -ENODEV;
  106. }
  107. return 0;
  108. }
  109. EXPORT_SYMBOL(acpi_bus_get_device);
  110. acpi_status acpi_bus_get_status_handle(acpi_handle handle,
  111. unsigned long long *sta)
  112. {
  113. acpi_status status;
  114. status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
  115. if (ACPI_SUCCESS(status))
  116. return AE_OK;
  117. if (status == AE_NOT_FOUND) {
  118. *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
  119. ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
  120. return AE_OK;
  121. }
  122. return status;
  123. }
  124. int acpi_bus_get_status(struct acpi_device *device)
  125. {
  126. acpi_status status;
  127. unsigned long long sta;
  128. status = acpi_bus_get_status_handle(device->handle, &sta);
  129. if (ACPI_FAILURE(status))
  130. return -ENODEV;
  131. STRUCT_TO_INT(device->status) = (int) sta;
  132. if (device->status.functional && !device->status.present) {
  133. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
  134. "functional but not present;\n",
  135. device->pnp.bus_id,
  136. (u32) STRUCT_TO_INT(device->status)));
  137. }
  138. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
  139. device->pnp.bus_id,
  140. (u32) STRUCT_TO_INT(device->status)));
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(acpi_bus_get_status);
  144. void acpi_bus_private_data_handler(acpi_handle handle,
  145. void *context)
  146. {
  147. return;
  148. }
  149. EXPORT_SYMBOL(acpi_bus_private_data_handler);
  150. int acpi_bus_get_private_data(acpi_handle handle, void **data)
  151. {
  152. acpi_status status = AE_OK;
  153. if (!*data)
  154. return -EINVAL;
  155. status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
  156. if (ACPI_FAILURE(status) || !*data) {
  157. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
  158. handle));
  159. return -ENODEV;
  160. }
  161. return 0;
  162. }
  163. EXPORT_SYMBOL(acpi_bus_get_private_data);
  164. /* --------------------------------------------------------------------------
  165. Power Management
  166. -------------------------------------------------------------------------- */
  167. int acpi_bus_get_power(acpi_handle handle, int *state)
  168. {
  169. int result = 0;
  170. acpi_status status = 0;
  171. struct acpi_device *device = NULL;
  172. unsigned long long psc = 0;
  173. result = acpi_bus_get_device(handle, &device);
  174. if (result)
  175. return result;
  176. *state = ACPI_STATE_UNKNOWN;
  177. if (!device->flags.power_manageable) {
  178. /* TBD: Non-recursive algorithm for walking up hierarchy */
  179. if (device->parent)
  180. *state = device->parent->power.state;
  181. else
  182. *state = ACPI_STATE_D0;
  183. } else {
  184. /*
  185. * Get the device's power state either directly (via _PSC) or
  186. * indirectly (via power resources).
  187. */
  188. if (device->power.flags.power_resources) {
  189. result = acpi_power_get_inferred_state(device);
  190. if (result)
  191. return result;
  192. } else if (device->power.flags.explicit_get) {
  193. status = acpi_evaluate_integer(device->handle, "_PSC",
  194. NULL, &psc);
  195. if (ACPI_FAILURE(status))
  196. return -ENODEV;
  197. device->power.state = (int)psc;
  198. }
  199. *state = device->power.state;
  200. }
  201. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
  202. device->pnp.bus_id, device->power.state));
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(acpi_bus_get_power);
  206. int acpi_bus_set_power(acpi_handle handle, int state)
  207. {
  208. int result = 0;
  209. acpi_status status = AE_OK;
  210. struct acpi_device *device = NULL;
  211. char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
  212. result = acpi_bus_get_device(handle, &device);
  213. if (result)
  214. return result;
  215. if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  216. return -EINVAL;
  217. /* Make sure this is a valid target state */
  218. if (!device->flags.power_manageable) {
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
  220. kobject_name(&device->dev.kobj)));
  221. return -ENODEV;
  222. }
  223. /*
  224. * Get device's current power state
  225. */
  226. if (!acpi_power_nocheck) {
  227. /*
  228. * Maybe the incorrect power state is returned on the bogus
  229. * bios, which is different with the real power state.
  230. * For example: the bios returns D0 state and the real power
  231. * state is D3. OS expects to set the device to D0 state. In
  232. * such case if OS uses the power state returned by the BIOS,
  233. * the device can't be transisted to the correct power state.
  234. * So if the acpi_power_nocheck is set, it is unnecessary to
  235. * get the power state by calling acpi_bus_get_power.
  236. */
  237. acpi_bus_get_power(device->handle, &device->power.state);
  238. }
  239. if ((state == device->power.state) && !device->flags.force_power_state) {
  240. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
  241. state));
  242. return 0;
  243. }
  244. if (!device->power.states[state].flags.valid) {
  245. printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
  246. return -ENODEV;
  247. }
  248. if (device->parent && (state < device->parent->power.state)) {
  249. printk(KERN_WARNING PREFIX
  250. "Cannot set device to a higher-powered"
  251. " state than parent\n");
  252. return -ENODEV;
  253. }
  254. /*
  255. * Transition Power
  256. * ----------------
  257. * On transitions to a high-powered state we first apply power (via
  258. * power resources) then evalute _PSx. Conversly for transitions to
  259. * a lower-powered state.
  260. */
  261. if (state < device->power.state) {
  262. if (device->power.flags.power_resources) {
  263. result = acpi_power_transition(device, state);
  264. if (result)
  265. goto end;
  266. }
  267. if (device->power.states[state].flags.explicit_set) {
  268. status = acpi_evaluate_object(device->handle,
  269. object_name, NULL, NULL);
  270. if (ACPI_FAILURE(status)) {
  271. result = -ENODEV;
  272. goto end;
  273. }
  274. }
  275. } else {
  276. if (device->power.states[state].flags.explicit_set) {
  277. status = acpi_evaluate_object(device->handle,
  278. object_name, NULL, NULL);
  279. if (ACPI_FAILURE(status)) {
  280. result = -ENODEV;
  281. goto end;
  282. }
  283. }
  284. if (device->power.flags.power_resources) {
  285. result = acpi_power_transition(device, state);
  286. if (result)
  287. goto end;
  288. }
  289. }
  290. end:
  291. if (result)
  292. printk(KERN_WARNING PREFIX
  293. "Device [%s] failed to transition to D%d\n",
  294. device->pnp.bus_id, state);
  295. else {
  296. device->power.state = state;
  297. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  298. "Device [%s] transitioned to D%d\n",
  299. device->pnp.bus_id, state));
  300. }
  301. return result;
  302. }
  303. EXPORT_SYMBOL(acpi_bus_set_power);
  304. bool acpi_bus_power_manageable(acpi_handle handle)
  305. {
  306. struct acpi_device *device;
  307. int result;
  308. result = acpi_bus_get_device(handle, &device);
  309. return result ? false : device->flags.power_manageable;
  310. }
  311. EXPORT_SYMBOL(acpi_bus_power_manageable);
  312. bool acpi_bus_can_wakeup(acpi_handle handle)
  313. {
  314. struct acpi_device *device;
  315. int result;
  316. result = acpi_bus_get_device(handle, &device);
  317. return result ? false : device->wakeup.flags.valid;
  318. }
  319. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  320. static void acpi_print_osc_error(acpi_handle handle,
  321. struct acpi_osc_context *context, char *error)
  322. {
  323. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
  324. int i;
  325. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
  326. printk(KERN_DEBUG "%s\n", error);
  327. else {
  328. printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
  329. kfree(buffer.pointer);
  330. }
  331. printk(KERN_DEBUG"_OSC request data:");
  332. for (i = 0; i < context->cap.length; i += sizeof(u32))
  333. printk("%x ", *((u32 *)(context->cap.pointer + i)));
  334. printk("\n");
  335. }
  336. static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
  337. {
  338. int i;
  339. static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
  340. 24, 26, 28, 30, 32, 34};
  341. if (strlen(str) != 36)
  342. return AE_BAD_PARAMETER;
  343. for (i = 0; i < 36; i++) {
  344. if (i == 8 || i == 13 || i == 18 || i == 23) {
  345. if (str[i] != '-')
  346. return AE_BAD_PARAMETER;
  347. } else if (!isxdigit(str[i]))
  348. return AE_BAD_PARAMETER;
  349. }
  350. for (i = 0; i < 16; i++) {
  351. uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
  352. uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
  353. }
  354. return AE_OK;
  355. }
  356. acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
  357. {
  358. acpi_status status;
  359. struct acpi_object_list input;
  360. union acpi_object in_params[4];
  361. union acpi_object *out_obj;
  362. u8 uuid[16];
  363. u32 errors;
  364. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  365. if (!context)
  366. return AE_ERROR;
  367. if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
  368. return AE_ERROR;
  369. context->ret.length = ACPI_ALLOCATE_BUFFER;
  370. context->ret.pointer = NULL;
  371. /* Setting up input parameters */
  372. input.count = 4;
  373. input.pointer = in_params;
  374. in_params[0].type = ACPI_TYPE_BUFFER;
  375. in_params[0].buffer.length = 16;
  376. in_params[0].buffer.pointer = uuid;
  377. in_params[1].type = ACPI_TYPE_INTEGER;
  378. in_params[1].integer.value = context->rev;
  379. in_params[2].type = ACPI_TYPE_INTEGER;
  380. in_params[2].integer.value = context->cap.length/sizeof(u32);
  381. in_params[3].type = ACPI_TYPE_BUFFER;
  382. in_params[3].buffer.length = context->cap.length;
  383. in_params[3].buffer.pointer = context->cap.pointer;
  384. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  385. if (ACPI_FAILURE(status))
  386. return status;
  387. if (!output.length)
  388. return AE_NULL_OBJECT;
  389. out_obj = output.pointer;
  390. if (out_obj->type != ACPI_TYPE_BUFFER
  391. || out_obj->buffer.length != context->cap.length) {
  392. acpi_print_osc_error(handle, context,
  393. "_OSC evaluation returned wrong type");
  394. status = AE_TYPE;
  395. goto out_kfree;
  396. }
  397. /* Need to ignore the bit0 in result code */
  398. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  399. if (errors) {
  400. if (errors & OSC_REQUEST_ERROR)
  401. acpi_print_osc_error(handle, context,
  402. "_OSC request failed");
  403. if (errors & OSC_INVALID_UUID_ERROR)
  404. acpi_print_osc_error(handle, context,
  405. "_OSC invalid UUID");
  406. if (errors & OSC_INVALID_REVISION_ERROR)
  407. acpi_print_osc_error(handle, context,
  408. "_OSC invalid revision");
  409. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  410. if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
  411. & OSC_QUERY_ENABLE)
  412. goto out_success;
  413. status = AE_SUPPORT;
  414. goto out_kfree;
  415. }
  416. status = AE_ERROR;
  417. goto out_kfree;
  418. }
  419. out_success:
  420. context->ret.length = out_obj->buffer.length;
  421. context->ret.pointer = kmalloc(context->ret.length, GFP_KERNEL);
  422. if (!context->ret.pointer) {
  423. status = AE_NO_MEMORY;
  424. goto out_kfree;
  425. }
  426. memcpy(context->ret.pointer, out_obj->buffer.pointer,
  427. context->ret.length);
  428. status = AE_OK;
  429. out_kfree:
  430. kfree(output.pointer);
  431. if (status != AE_OK)
  432. context->ret.pointer = NULL;
  433. return status;
  434. }
  435. EXPORT_SYMBOL(acpi_run_osc);
  436. static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
  437. static void acpi_bus_osc_support(void)
  438. {
  439. u32 capbuf[2];
  440. struct acpi_osc_context context = {
  441. .uuid_str = sb_uuid_str,
  442. .rev = 1,
  443. .cap.length = 8,
  444. .cap.pointer = capbuf,
  445. };
  446. acpi_handle handle;
  447. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  448. capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
  449. #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
  450. defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
  451. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
  452. #endif
  453. #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
  454. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
  455. #endif
  456. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  457. return;
  458. if (ACPI_SUCCESS(acpi_run_osc(handle, &context)))
  459. kfree(context.ret.pointer);
  460. /* do we need to check the returned cap? Sounds no */
  461. }
  462. /* --------------------------------------------------------------------------
  463. Event Management
  464. -------------------------------------------------------------------------- */
  465. #ifdef CONFIG_ACPI_PROC_EVENT
  466. static DEFINE_SPINLOCK(acpi_bus_event_lock);
  467. LIST_HEAD(acpi_bus_event_list);
  468. DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
  469. extern int event_is_open;
  470. int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
  471. {
  472. struct acpi_bus_event *event;
  473. unsigned long flags = 0;
  474. /* drop event on the floor if no one's listening */
  475. if (!event_is_open)
  476. return 0;
  477. event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
  478. if (!event)
  479. return -ENOMEM;
  480. strcpy(event->device_class, device_class);
  481. strcpy(event->bus_id, bus_id);
  482. event->type = type;
  483. event->data = data;
  484. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  485. list_add_tail(&event->node, &acpi_bus_event_list);
  486. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  487. wake_up_interruptible(&acpi_bus_event_queue);
  488. return 0;
  489. }
  490. EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
  491. int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
  492. {
  493. if (!device)
  494. return -EINVAL;
  495. return acpi_bus_generate_proc_event4(device->pnp.device_class,
  496. device->pnp.bus_id, type, data);
  497. }
  498. EXPORT_SYMBOL(acpi_bus_generate_proc_event);
  499. int acpi_bus_receive_event(struct acpi_bus_event *event)
  500. {
  501. unsigned long flags = 0;
  502. struct acpi_bus_event *entry = NULL;
  503. DECLARE_WAITQUEUE(wait, current);
  504. if (!event)
  505. return -EINVAL;
  506. if (list_empty(&acpi_bus_event_list)) {
  507. set_current_state(TASK_INTERRUPTIBLE);
  508. add_wait_queue(&acpi_bus_event_queue, &wait);
  509. if (list_empty(&acpi_bus_event_list))
  510. schedule();
  511. remove_wait_queue(&acpi_bus_event_queue, &wait);
  512. set_current_state(TASK_RUNNING);
  513. if (signal_pending(current))
  514. return -ERESTARTSYS;
  515. }
  516. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  517. if (!list_empty(&acpi_bus_event_list)) {
  518. entry = list_entry(acpi_bus_event_list.next,
  519. struct acpi_bus_event, node);
  520. list_del(&entry->node);
  521. }
  522. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  523. if (!entry)
  524. return -ENODEV;
  525. memcpy(event, entry, sizeof(struct acpi_bus_event));
  526. kfree(entry);
  527. return 0;
  528. }
  529. #endif /* CONFIG_ACPI_PROC_EVENT */
  530. /* --------------------------------------------------------------------------
  531. Notification Handling
  532. -------------------------------------------------------------------------- */
  533. static void acpi_bus_check_device(acpi_handle handle)
  534. {
  535. struct acpi_device *device;
  536. acpi_status status;
  537. struct acpi_device_status old_status;
  538. if (acpi_bus_get_device(handle, &device))
  539. return;
  540. if (!device)
  541. return;
  542. old_status = device->status;
  543. /*
  544. * Make sure this device's parent is present before we go about
  545. * messing with the device.
  546. */
  547. if (device->parent && !device->parent->status.present) {
  548. device->status = device->parent->status;
  549. return;
  550. }
  551. status = acpi_bus_get_status(device);
  552. if (ACPI_FAILURE(status))
  553. return;
  554. if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
  555. return;
  556. /*
  557. * Device Insertion/Removal
  558. */
  559. if ((device->status.present) && !(old_status.present)) {
  560. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
  561. /* TBD: Handle device insertion */
  562. } else if (!(device->status.present) && (old_status.present)) {
  563. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
  564. /* TBD: Handle device removal */
  565. }
  566. }
  567. static void acpi_bus_check_scope(acpi_handle handle)
  568. {
  569. /* Status Change? */
  570. acpi_bus_check_device(handle);
  571. /*
  572. * TBD: Enumerate child devices within this device's scope and
  573. * run acpi_bus_check_device()'s on them.
  574. */
  575. }
  576. static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
  577. int register_acpi_bus_notifier(struct notifier_block *nb)
  578. {
  579. return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
  580. }
  581. EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
  582. void unregister_acpi_bus_notifier(struct notifier_block *nb)
  583. {
  584. blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
  585. }
  586. EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
  587. /**
  588. * acpi_bus_notify
  589. * ---------------
  590. * Callback for all 'system-level' device notifications (values 0x00-0x7F).
  591. */
  592. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  593. {
  594. struct acpi_device *device = NULL;
  595. struct acpi_driver *driver;
  596. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
  597. type, handle));
  598. blocking_notifier_call_chain(&acpi_bus_notify_list,
  599. type, (void *)handle);
  600. switch (type) {
  601. case ACPI_NOTIFY_BUS_CHECK:
  602. acpi_bus_check_scope(handle);
  603. /*
  604. * TBD: We'll need to outsource certain events to non-ACPI
  605. * drivers via the device manager (device.c).
  606. */
  607. break;
  608. case ACPI_NOTIFY_DEVICE_CHECK:
  609. acpi_bus_check_device(handle);
  610. /*
  611. * TBD: We'll need to outsource certain events to non-ACPI
  612. * drivers via the device manager (device.c).
  613. */
  614. break;
  615. case ACPI_NOTIFY_DEVICE_WAKE:
  616. /* TBD */
  617. break;
  618. case ACPI_NOTIFY_EJECT_REQUEST:
  619. /* TBD */
  620. break;
  621. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  622. /* TBD: Exactly what does 'light' mean? */
  623. break;
  624. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  625. /* TBD */
  626. break;
  627. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  628. /* TBD */
  629. break;
  630. case ACPI_NOTIFY_POWER_FAULT:
  631. /* TBD */
  632. break;
  633. default:
  634. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  635. "Received unknown/unsupported notification [%08x]\n",
  636. type));
  637. break;
  638. }
  639. acpi_bus_get_device(handle, &device);
  640. if (device) {
  641. driver = device->driver;
  642. if (driver && driver->ops.notify &&
  643. (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
  644. driver->ops.notify(device, type);
  645. }
  646. }
  647. /* --------------------------------------------------------------------------
  648. Initialization/Cleanup
  649. -------------------------------------------------------------------------- */
  650. static int __init acpi_bus_init_irq(void)
  651. {
  652. acpi_status status = AE_OK;
  653. union acpi_object arg = { ACPI_TYPE_INTEGER };
  654. struct acpi_object_list arg_list = { 1, &arg };
  655. char *message = NULL;
  656. /*
  657. * Let the system know what interrupt model we are using by
  658. * evaluating the \_PIC object, if exists.
  659. */
  660. switch (acpi_irq_model) {
  661. case ACPI_IRQ_MODEL_PIC:
  662. message = "PIC";
  663. break;
  664. case ACPI_IRQ_MODEL_IOAPIC:
  665. message = "IOAPIC";
  666. break;
  667. case ACPI_IRQ_MODEL_IOSAPIC:
  668. message = "IOSAPIC";
  669. break;
  670. case ACPI_IRQ_MODEL_PLATFORM:
  671. message = "platform specific model";
  672. break;
  673. default:
  674. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  675. return -ENODEV;
  676. }
  677. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  678. arg.integer.value = acpi_irq_model;
  679. status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
  680. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  681. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  682. return -ENODEV;
  683. }
  684. return 0;
  685. }
  686. u8 acpi_gbl_permanent_mmap;
  687. void __init acpi_early_init(void)
  688. {
  689. acpi_status status = AE_OK;
  690. if (acpi_disabled)
  691. return;
  692. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  693. /* enable workarounds, unless strict ACPI spec. compliance */
  694. if (!acpi_strict)
  695. acpi_gbl_enable_interpreter_slack = TRUE;
  696. acpi_gbl_permanent_mmap = 1;
  697. /*
  698. * If the machine falls into the DMI check table,
  699. * DSDT will be copied to memory
  700. */
  701. dmi_check_system(dsdt_dmi_table);
  702. status = acpi_reallocate_root_table();
  703. if (ACPI_FAILURE(status)) {
  704. printk(KERN_ERR PREFIX
  705. "Unable to reallocate ACPI tables\n");
  706. goto error0;
  707. }
  708. status = acpi_initialize_subsystem();
  709. if (ACPI_FAILURE(status)) {
  710. printk(KERN_ERR PREFIX
  711. "Unable to initialize the ACPI Interpreter\n");
  712. goto error0;
  713. }
  714. status = acpi_load_tables();
  715. if (ACPI_FAILURE(status)) {
  716. printk(KERN_ERR PREFIX
  717. "Unable to load the System Description Tables\n");
  718. goto error0;
  719. }
  720. #ifdef CONFIG_X86
  721. if (!acpi_ioapic) {
  722. /* compatible (0) means level (3) */
  723. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  724. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  725. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  726. }
  727. /* Set PIC-mode SCI trigger type */
  728. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  729. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  730. } else {
  731. /*
  732. * now that acpi_gbl_FADT is initialized,
  733. * update it with result from INT_SRC_OVR parsing
  734. */
  735. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  736. }
  737. #endif
  738. status =
  739. acpi_enable_subsystem(~
  740. (ACPI_NO_HARDWARE_INIT |
  741. ACPI_NO_ACPI_ENABLE));
  742. if (ACPI_FAILURE(status)) {
  743. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  744. goto error0;
  745. }
  746. return;
  747. error0:
  748. disable_acpi();
  749. return;
  750. }
  751. static int __init acpi_bus_init(void)
  752. {
  753. int result = 0;
  754. acpi_status status = AE_OK;
  755. extern acpi_status acpi_os_initialize1(void);
  756. acpi_os_initialize1();
  757. status =
  758. acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
  759. if (ACPI_FAILURE(status)) {
  760. printk(KERN_ERR PREFIX
  761. "Unable to start the ACPI Interpreter\n");
  762. goto error1;
  763. }
  764. /*
  765. * ACPI 2.0 requires the EC driver to be loaded and work before
  766. * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
  767. * is called).
  768. *
  769. * This is accomplished by looking for the ECDT table, and getting
  770. * the EC parameters out of that.
  771. */
  772. status = acpi_ec_ecdt_probe();
  773. /* Ignore result. Not having an ECDT is not fatal. */
  774. acpi_bus_osc_support();
  775. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  776. if (ACPI_FAILURE(status)) {
  777. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  778. goto error1;
  779. }
  780. acpi_early_processor_set_pdc();
  781. /*
  782. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  783. * is necessary to enable it as early as possible.
  784. */
  785. acpi_boot_ec_enable();
  786. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  787. /* Initialize sleep structures */
  788. acpi_sleep_init();
  789. /*
  790. * Get the system interrupt model and evaluate \_PIC.
  791. */
  792. result = acpi_bus_init_irq();
  793. if (result)
  794. goto error1;
  795. /*
  796. * Register the for all standard device notifications.
  797. */
  798. status =
  799. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  800. &acpi_bus_notify, NULL);
  801. if (ACPI_FAILURE(status)) {
  802. printk(KERN_ERR PREFIX
  803. "Unable to register for device notifications\n");
  804. goto error1;
  805. }
  806. /*
  807. * Create the top ACPI proc directory
  808. */
  809. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  810. return 0;
  811. /* Mimic structured exception handling */
  812. error1:
  813. acpi_terminate();
  814. return -ENODEV;
  815. }
  816. struct kobject *acpi_kobj;
  817. static int __init acpi_init(void)
  818. {
  819. int result = 0;
  820. if (acpi_disabled) {
  821. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  822. return -ENODEV;
  823. }
  824. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  825. if (!acpi_kobj) {
  826. printk(KERN_WARNING "%s: kset create error\n", __func__);
  827. acpi_kobj = NULL;
  828. }
  829. init_acpi_device_notify();
  830. result = acpi_bus_init();
  831. if (!result) {
  832. pci_mmcfg_late_init();
  833. if (!(pm_flags & PM_APM))
  834. pm_flags |= PM_ACPI;
  835. else {
  836. printk(KERN_INFO PREFIX
  837. "APM is already active, exiting\n");
  838. disable_acpi();
  839. result = -ENODEV;
  840. }
  841. } else
  842. disable_acpi();
  843. if (acpi_disabled)
  844. return result;
  845. /*
  846. * If the laptop falls into the DMI check table, the power state check
  847. * will be disabled in the course of device power transition.
  848. */
  849. dmi_check_system(power_nocheck_dmi_table);
  850. acpi_scan_init();
  851. acpi_ec_init();
  852. acpi_power_init();
  853. acpi_sysfs_init();
  854. acpi_debugfs_init();
  855. acpi_sleep_proc_init();
  856. acpi_wakeup_device_init();
  857. return result;
  858. }
  859. subsys_initcall(acpi_init);