bus.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. static int __acpi_bus_get_power(struct acpi_device *device, int *state)
  168. {
  169. int result = 0;
  170. acpi_status status = 0;
  171. unsigned long long psc = 0;
  172. if (!device || !state)
  173. return -EINVAL;
  174. *state = ACPI_STATE_UNKNOWN;
  175. if (device->flags.power_manageable) {
  176. /*
  177. * Get the device's power state either directly (via _PSC) or
  178. * indirectly (via power resources).
  179. */
  180. if (device->power.flags.power_resources) {
  181. result = acpi_power_get_inferred_state(device, state);
  182. if (result)
  183. return result;
  184. } else if (device->power.flags.explicit_get) {
  185. status = acpi_evaluate_integer(device->handle, "_PSC",
  186. NULL, &psc);
  187. if (ACPI_FAILURE(status))
  188. return -ENODEV;
  189. *state = (int)psc;
  190. }
  191. } else {
  192. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  193. *state = device->parent ?
  194. device->parent->power.state : ACPI_STATE_D0;
  195. }
  196. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
  197. device->pnp.bus_id, *state));
  198. return 0;
  199. }
  200. int acpi_bus_get_power(acpi_handle handle, int *state)
  201. {
  202. struct acpi_device *device;
  203. int result;
  204. result = acpi_bus_get_device(handle, &device);
  205. if (result)
  206. return result;
  207. result = __acpi_bus_get_power(device, state);
  208. if (result)
  209. return result;
  210. device->power.state = *state;
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(acpi_bus_get_power);
  214. static int __acpi_bus_set_power(struct acpi_device *device, int state)
  215. {
  216. int result = 0;
  217. acpi_status status = AE_OK;
  218. char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
  219. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  220. return -EINVAL;
  221. /* Make sure this is a valid target state */
  222. if (state == device->power.state) {
  223. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
  224. state));
  225. return 0;
  226. }
  227. if (!device->power.states[state].flags.valid) {
  228. printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
  229. return -ENODEV;
  230. }
  231. if (device->parent && (state < device->parent->power.state)) {
  232. printk(KERN_WARNING PREFIX
  233. "Cannot set device to a higher-powered"
  234. " state than parent\n");
  235. return -ENODEV;
  236. }
  237. /*
  238. * Transition Power
  239. * ----------------
  240. * On transitions to a high-powered state we first apply power (via
  241. * power resources) then evalute _PSx. Conversly for transitions to
  242. * a lower-powered state.
  243. */
  244. if (state < device->power.state) {
  245. if (device->power.flags.power_resources) {
  246. result = acpi_power_transition(device, state);
  247. if (result)
  248. goto end;
  249. }
  250. if (device->power.states[state].flags.explicit_set) {
  251. status = acpi_evaluate_object(device->handle,
  252. object_name, NULL, NULL);
  253. if (ACPI_FAILURE(status)) {
  254. result = -ENODEV;
  255. goto end;
  256. }
  257. }
  258. } else {
  259. if (device->power.states[state].flags.explicit_set) {
  260. status = acpi_evaluate_object(device->handle,
  261. object_name, NULL, NULL);
  262. if (ACPI_FAILURE(status)) {
  263. result = -ENODEV;
  264. goto end;
  265. }
  266. }
  267. if (device->power.flags.power_resources) {
  268. result = acpi_power_transition(device, state);
  269. if (result)
  270. goto end;
  271. }
  272. }
  273. end:
  274. if (result)
  275. printk(KERN_WARNING PREFIX
  276. "Device [%s] failed to transition to D%d\n",
  277. device->pnp.bus_id, state);
  278. else {
  279. device->power.state = state;
  280. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  281. "Device [%s] transitioned to D%d\n",
  282. device->pnp.bus_id, state));
  283. }
  284. return result;
  285. }
  286. int acpi_bus_set_power(acpi_handle handle, int state)
  287. {
  288. struct acpi_device *device;
  289. int result;
  290. result = acpi_bus_get_device(handle, &device);
  291. if (result)
  292. return result;
  293. if (!device->flags.power_manageable) {
  294. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  295. "Device [%s] is not power manageable\n",
  296. dev_name(&device->dev)));
  297. return -ENODEV;
  298. }
  299. /*
  300. * Get device's current power state
  301. */
  302. if (!acpi_power_nocheck) {
  303. /*
  304. * Maybe the incorrect power state is returned on the bogus
  305. * bios, which is different with the real power state.
  306. * For example: the bios returns D0 state and the real power
  307. * state is D3. OS expects to set the device to D0 state. In
  308. * such case if OS uses the power state returned by the BIOS,
  309. * the device can't be transisted to the correct power state.
  310. * So if the acpi_power_nocheck is set, it is unnecessary to
  311. * get the power state by calling acpi_bus_get_power.
  312. */
  313. __acpi_bus_get_power(device, &device->power.state);
  314. }
  315. return __acpi_bus_set_power(device, state);
  316. }
  317. EXPORT_SYMBOL(acpi_bus_set_power);
  318. int acpi_bus_init_power(struct acpi_device *device)
  319. {
  320. int state;
  321. int result;
  322. if (!device)
  323. return -EINVAL;
  324. device->power.state = ACPI_STATE_UNKNOWN;
  325. result = __acpi_bus_get_power(device, &state);
  326. if (result)
  327. return result;
  328. if (device->power.flags.power_resources)
  329. result = acpi_power_on_resources(device, state);
  330. if (!result)
  331. device->power.state = state;
  332. return result;
  333. }
  334. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  335. {
  336. struct acpi_device *device;
  337. int state;
  338. int result;
  339. result = acpi_bus_get_device(handle, &device);
  340. if (result)
  341. return result;
  342. result = __acpi_bus_get_power(device, &state);
  343. if (result)
  344. return result;
  345. result = __acpi_bus_set_power(device, state);
  346. if (!result && state_p)
  347. *state_p = state;
  348. return result;
  349. }
  350. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  351. bool acpi_bus_power_manageable(acpi_handle handle)
  352. {
  353. struct acpi_device *device;
  354. int result;
  355. result = acpi_bus_get_device(handle, &device);
  356. return result ? false : device->flags.power_manageable;
  357. }
  358. EXPORT_SYMBOL(acpi_bus_power_manageable);
  359. bool acpi_bus_can_wakeup(acpi_handle handle)
  360. {
  361. struct acpi_device *device;
  362. int result;
  363. result = acpi_bus_get_device(handle, &device);
  364. return result ? false : device->wakeup.flags.valid;
  365. }
  366. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  367. static void acpi_print_osc_error(acpi_handle handle,
  368. struct acpi_osc_context *context, char *error)
  369. {
  370. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
  371. int i;
  372. if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
  373. printk(KERN_DEBUG "%s\n", error);
  374. else {
  375. printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
  376. kfree(buffer.pointer);
  377. }
  378. printk(KERN_DEBUG"_OSC request data:");
  379. for (i = 0; i < context->cap.length; i += sizeof(u32))
  380. printk("%x ", *((u32 *)(context->cap.pointer + i)));
  381. printk("\n");
  382. }
  383. static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
  384. {
  385. int i;
  386. static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
  387. 24, 26, 28, 30, 32, 34};
  388. if (strlen(str) != 36)
  389. return AE_BAD_PARAMETER;
  390. for (i = 0; i < 36; i++) {
  391. if (i == 8 || i == 13 || i == 18 || i == 23) {
  392. if (str[i] != '-')
  393. return AE_BAD_PARAMETER;
  394. } else if (!isxdigit(str[i]))
  395. return AE_BAD_PARAMETER;
  396. }
  397. for (i = 0; i < 16; i++) {
  398. uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
  399. uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
  400. }
  401. return AE_OK;
  402. }
  403. acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
  404. {
  405. acpi_status status;
  406. struct acpi_object_list input;
  407. union acpi_object in_params[4];
  408. union acpi_object *out_obj;
  409. u8 uuid[16];
  410. u32 errors;
  411. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  412. if (!context)
  413. return AE_ERROR;
  414. if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
  415. return AE_ERROR;
  416. context->ret.length = ACPI_ALLOCATE_BUFFER;
  417. context->ret.pointer = NULL;
  418. /* Setting up input parameters */
  419. input.count = 4;
  420. input.pointer = in_params;
  421. in_params[0].type = ACPI_TYPE_BUFFER;
  422. in_params[0].buffer.length = 16;
  423. in_params[0].buffer.pointer = uuid;
  424. in_params[1].type = ACPI_TYPE_INTEGER;
  425. in_params[1].integer.value = context->rev;
  426. in_params[2].type = ACPI_TYPE_INTEGER;
  427. in_params[2].integer.value = context->cap.length/sizeof(u32);
  428. in_params[3].type = ACPI_TYPE_BUFFER;
  429. in_params[3].buffer.length = context->cap.length;
  430. in_params[3].buffer.pointer = context->cap.pointer;
  431. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  432. if (ACPI_FAILURE(status))
  433. return status;
  434. if (!output.length)
  435. return AE_NULL_OBJECT;
  436. out_obj = output.pointer;
  437. if (out_obj->type != ACPI_TYPE_BUFFER
  438. || out_obj->buffer.length != context->cap.length) {
  439. acpi_print_osc_error(handle, context,
  440. "_OSC evaluation returned wrong type");
  441. status = AE_TYPE;
  442. goto out_kfree;
  443. }
  444. /* Need to ignore the bit0 in result code */
  445. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  446. if (errors) {
  447. if (errors & OSC_REQUEST_ERROR)
  448. acpi_print_osc_error(handle, context,
  449. "_OSC request failed");
  450. if (errors & OSC_INVALID_UUID_ERROR)
  451. acpi_print_osc_error(handle, context,
  452. "_OSC invalid UUID");
  453. if (errors & OSC_INVALID_REVISION_ERROR)
  454. acpi_print_osc_error(handle, context,
  455. "_OSC invalid revision");
  456. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  457. if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
  458. & OSC_QUERY_ENABLE)
  459. goto out_success;
  460. status = AE_SUPPORT;
  461. goto out_kfree;
  462. }
  463. status = AE_ERROR;
  464. goto out_kfree;
  465. }
  466. out_success:
  467. context->ret.length = out_obj->buffer.length;
  468. context->ret.pointer = kmalloc(context->ret.length, GFP_KERNEL);
  469. if (!context->ret.pointer) {
  470. status = AE_NO_MEMORY;
  471. goto out_kfree;
  472. }
  473. memcpy(context->ret.pointer, out_obj->buffer.pointer,
  474. context->ret.length);
  475. status = AE_OK;
  476. out_kfree:
  477. kfree(output.pointer);
  478. if (status != AE_OK)
  479. context->ret.pointer = NULL;
  480. return status;
  481. }
  482. EXPORT_SYMBOL(acpi_run_osc);
  483. static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
  484. static void acpi_bus_osc_support(void)
  485. {
  486. u32 capbuf[2];
  487. struct acpi_osc_context context = {
  488. .uuid_str = sb_uuid_str,
  489. .rev = 1,
  490. .cap.length = 8,
  491. .cap.pointer = capbuf,
  492. };
  493. acpi_handle handle;
  494. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  495. capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
  496. #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
  497. defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
  498. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
  499. #endif
  500. #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
  501. capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
  502. #endif
  503. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
  504. return;
  505. if (ACPI_SUCCESS(acpi_run_osc(handle, &context)))
  506. kfree(context.ret.pointer);
  507. /* do we need to check the returned cap? Sounds no */
  508. }
  509. /* --------------------------------------------------------------------------
  510. Event Management
  511. -------------------------------------------------------------------------- */
  512. #ifdef CONFIG_ACPI_PROC_EVENT
  513. static DEFINE_SPINLOCK(acpi_bus_event_lock);
  514. LIST_HEAD(acpi_bus_event_list);
  515. DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
  516. extern int event_is_open;
  517. int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
  518. {
  519. struct acpi_bus_event *event;
  520. unsigned long flags = 0;
  521. /* drop event on the floor if no one's listening */
  522. if (!event_is_open)
  523. return 0;
  524. event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
  525. if (!event)
  526. return -ENOMEM;
  527. strcpy(event->device_class, device_class);
  528. strcpy(event->bus_id, bus_id);
  529. event->type = type;
  530. event->data = data;
  531. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  532. list_add_tail(&event->node, &acpi_bus_event_list);
  533. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  534. wake_up_interruptible(&acpi_bus_event_queue);
  535. return 0;
  536. }
  537. EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
  538. int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
  539. {
  540. if (!device)
  541. return -EINVAL;
  542. return acpi_bus_generate_proc_event4(device->pnp.device_class,
  543. device->pnp.bus_id, type, data);
  544. }
  545. EXPORT_SYMBOL(acpi_bus_generate_proc_event);
  546. int acpi_bus_receive_event(struct acpi_bus_event *event)
  547. {
  548. unsigned long flags = 0;
  549. struct acpi_bus_event *entry = NULL;
  550. DECLARE_WAITQUEUE(wait, current);
  551. if (!event)
  552. return -EINVAL;
  553. if (list_empty(&acpi_bus_event_list)) {
  554. set_current_state(TASK_INTERRUPTIBLE);
  555. add_wait_queue(&acpi_bus_event_queue, &wait);
  556. if (list_empty(&acpi_bus_event_list))
  557. schedule();
  558. remove_wait_queue(&acpi_bus_event_queue, &wait);
  559. set_current_state(TASK_RUNNING);
  560. if (signal_pending(current))
  561. return -ERESTARTSYS;
  562. }
  563. spin_lock_irqsave(&acpi_bus_event_lock, flags);
  564. if (!list_empty(&acpi_bus_event_list)) {
  565. entry = list_entry(acpi_bus_event_list.next,
  566. struct acpi_bus_event, node);
  567. list_del(&entry->node);
  568. }
  569. spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
  570. if (!entry)
  571. return -ENODEV;
  572. memcpy(event, entry, sizeof(struct acpi_bus_event));
  573. kfree(entry);
  574. return 0;
  575. }
  576. #endif /* CONFIG_ACPI_PROC_EVENT */
  577. /* --------------------------------------------------------------------------
  578. Notification Handling
  579. -------------------------------------------------------------------------- */
  580. static void acpi_bus_check_device(acpi_handle handle)
  581. {
  582. struct acpi_device *device;
  583. acpi_status status;
  584. struct acpi_device_status old_status;
  585. if (acpi_bus_get_device(handle, &device))
  586. return;
  587. if (!device)
  588. return;
  589. old_status = device->status;
  590. /*
  591. * Make sure this device's parent is present before we go about
  592. * messing with the device.
  593. */
  594. if (device->parent && !device->parent->status.present) {
  595. device->status = device->parent->status;
  596. return;
  597. }
  598. status = acpi_bus_get_status(device);
  599. if (ACPI_FAILURE(status))
  600. return;
  601. if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
  602. return;
  603. /*
  604. * Device Insertion/Removal
  605. */
  606. if ((device->status.present) && !(old_status.present)) {
  607. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
  608. /* TBD: Handle device insertion */
  609. } else if (!(device->status.present) && (old_status.present)) {
  610. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
  611. /* TBD: Handle device removal */
  612. }
  613. }
  614. static void acpi_bus_check_scope(acpi_handle handle)
  615. {
  616. /* Status Change? */
  617. acpi_bus_check_device(handle);
  618. /*
  619. * TBD: Enumerate child devices within this device's scope and
  620. * run acpi_bus_check_device()'s on them.
  621. */
  622. }
  623. static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
  624. int register_acpi_bus_notifier(struct notifier_block *nb)
  625. {
  626. return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
  627. }
  628. EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
  629. void unregister_acpi_bus_notifier(struct notifier_block *nb)
  630. {
  631. blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
  632. }
  633. EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
  634. /**
  635. * acpi_bus_notify
  636. * ---------------
  637. * Callback for all 'system-level' device notifications (values 0x00-0x7F).
  638. */
  639. static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
  640. {
  641. struct acpi_device *device = NULL;
  642. struct acpi_driver *driver;
  643. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
  644. type, handle));
  645. blocking_notifier_call_chain(&acpi_bus_notify_list,
  646. type, (void *)handle);
  647. switch (type) {
  648. case ACPI_NOTIFY_BUS_CHECK:
  649. acpi_bus_check_scope(handle);
  650. /*
  651. * TBD: We'll need to outsource certain events to non-ACPI
  652. * drivers via the device manager (device.c).
  653. */
  654. break;
  655. case ACPI_NOTIFY_DEVICE_CHECK:
  656. acpi_bus_check_device(handle);
  657. /*
  658. * TBD: We'll need to outsource certain events to non-ACPI
  659. * drivers via the device manager (device.c).
  660. */
  661. break;
  662. case ACPI_NOTIFY_DEVICE_WAKE:
  663. /* TBD */
  664. break;
  665. case ACPI_NOTIFY_EJECT_REQUEST:
  666. /* TBD */
  667. break;
  668. case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
  669. /* TBD: Exactly what does 'light' mean? */
  670. break;
  671. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  672. /* TBD */
  673. break;
  674. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  675. /* TBD */
  676. break;
  677. case ACPI_NOTIFY_POWER_FAULT:
  678. /* TBD */
  679. break;
  680. default:
  681. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  682. "Received unknown/unsupported notification [%08x]\n",
  683. type));
  684. break;
  685. }
  686. acpi_bus_get_device(handle, &device);
  687. if (device) {
  688. driver = device->driver;
  689. if (driver && driver->ops.notify &&
  690. (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
  691. driver->ops.notify(device, type);
  692. }
  693. }
  694. /* --------------------------------------------------------------------------
  695. Initialization/Cleanup
  696. -------------------------------------------------------------------------- */
  697. static int __init acpi_bus_init_irq(void)
  698. {
  699. acpi_status status = AE_OK;
  700. union acpi_object arg = { ACPI_TYPE_INTEGER };
  701. struct acpi_object_list arg_list = { 1, &arg };
  702. char *message = NULL;
  703. /*
  704. * Let the system know what interrupt model we are using by
  705. * evaluating the \_PIC object, if exists.
  706. */
  707. switch (acpi_irq_model) {
  708. case ACPI_IRQ_MODEL_PIC:
  709. message = "PIC";
  710. break;
  711. case ACPI_IRQ_MODEL_IOAPIC:
  712. message = "IOAPIC";
  713. break;
  714. case ACPI_IRQ_MODEL_IOSAPIC:
  715. message = "IOSAPIC";
  716. break;
  717. case ACPI_IRQ_MODEL_PLATFORM:
  718. message = "platform specific model";
  719. break;
  720. default:
  721. printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
  722. return -ENODEV;
  723. }
  724. printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
  725. arg.integer.value = acpi_irq_model;
  726. status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
  727. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  728. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
  729. return -ENODEV;
  730. }
  731. return 0;
  732. }
  733. u8 acpi_gbl_permanent_mmap;
  734. void __init acpi_early_init(void)
  735. {
  736. acpi_status status = AE_OK;
  737. if (acpi_disabled)
  738. return;
  739. printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
  740. /* enable workarounds, unless strict ACPI spec. compliance */
  741. if (!acpi_strict)
  742. acpi_gbl_enable_interpreter_slack = TRUE;
  743. acpi_gbl_permanent_mmap = 1;
  744. /*
  745. * If the machine falls into the DMI check table,
  746. * DSDT will be copied to memory
  747. */
  748. dmi_check_system(dsdt_dmi_table);
  749. status = acpi_reallocate_root_table();
  750. if (ACPI_FAILURE(status)) {
  751. printk(KERN_ERR PREFIX
  752. "Unable to reallocate ACPI tables\n");
  753. goto error0;
  754. }
  755. status = acpi_initialize_subsystem();
  756. if (ACPI_FAILURE(status)) {
  757. printk(KERN_ERR PREFIX
  758. "Unable to initialize the ACPI Interpreter\n");
  759. goto error0;
  760. }
  761. status = acpi_load_tables();
  762. if (ACPI_FAILURE(status)) {
  763. printk(KERN_ERR PREFIX
  764. "Unable to load the System Description Tables\n");
  765. goto error0;
  766. }
  767. #ifdef CONFIG_X86
  768. if (!acpi_ioapic) {
  769. /* compatible (0) means level (3) */
  770. if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
  771. acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
  772. acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
  773. }
  774. /* Set PIC-mode SCI trigger type */
  775. acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
  776. (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
  777. } else {
  778. /*
  779. * now that acpi_gbl_FADT is initialized,
  780. * update it with result from INT_SRC_OVR parsing
  781. */
  782. acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
  783. }
  784. #endif
  785. status =
  786. acpi_enable_subsystem(~
  787. (ACPI_NO_HARDWARE_INIT |
  788. ACPI_NO_ACPI_ENABLE));
  789. if (ACPI_FAILURE(status)) {
  790. printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
  791. goto error0;
  792. }
  793. return;
  794. error0:
  795. disable_acpi();
  796. return;
  797. }
  798. static int __init acpi_bus_init(void)
  799. {
  800. int result = 0;
  801. acpi_status status = AE_OK;
  802. extern acpi_status acpi_os_initialize1(void);
  803. acpi_os_initialize1();
  804. status =
  805. acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
  806. if (ACPI_FAILURE(status)) {
  807. printk(KERN_ERR PREFIX
  808. "Unable to start the ACPI Interpreter\n");
  809. goto error1;
  810. }
  811. /*
  812. * ACPI 2.0 requires the EC driver to be loaded and work before
  813. * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
  814. * is called).
  815. *
  816. * This is accomplished by looking for the ECDT table, and getting
  817. * the EC parameters out of that.
  818. */
  819. status = acpi_ec_ecdt_probe();
  820. /* Ignore result. Not having an ECDT is not fatal. */
  821. acpi_bus_osc_support();
  822. status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
  823. if (ACPI_FAILURE(status)) {
  824. printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
  825. goto error1;
  826. }
  827. /*
  828. * _PDC control method may load dynamic SSDT tables,
  829. * and we need to install the table handler before that.
  830. */
  831. acpi_sysfs_init();
  832. acpi_early_processor_set_pdc();
  833. /*
  834. * Maybe EC region is required at bus_scan/acpi_get_devices. So it
  835. * is necessary to enable it as early as possible.
  836. */
  837. acpi_boot_ec_enable();
  838. printk(KERN_INFO PREFIX "Interpreter enabled\n");
  839. /* Initialize sleep structures */
  840. acpi_sleep_init();
  841. /*
  842. * Get the system interrupt model and evaluate \_PIC.
  843. */
  844. result = acpi_bus_init_irq();
  845. if (result)
  846. goto error1;
  847. /*
  848. * Register the for all standard device notifications.
  849. */
  850. status =
  851. acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
  852. &acpi_bus_notify, NULL);
  853. if (ACPI_FAILURE(status)) {
  854. printk(KERN_ERR PREFIX
  855. "Unable to register for device notifications\n");
  856. goto error1;
  857. }
  858. /*
  859. * Create the top ACPI proc directory
  860. */
  861. acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
  862. return 0;
  863. /* Mimic structured exception handling */
  864. error1:
  865. acpi_terminate();
  866. return -ENODEV;
  867. }
  868. struct kobject *acpi_kobj;
  869. static int __init acpi_init(void)
  870. {
  871. int result = 0;
  872. if (acpi_disabled) {
  873. printk(KERN_INFO PREFIX "Interpreter disabled.\n");
  874. return -ENODEV;
  875. }
  876. acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
  877. if (!acpi_kobj) {
  878. printk(KERN_WARNING "%s: kset create error\n", __func__);
  879. acpi_kobj = NULL;
  880. }
  881. init_acpi_device_notify();
  882. result = acpi_bus_init();
  883. if (!result) {
  884. pci_mmcfg_late_init();
  885. if (!(pm_flags & PM_APM))
  886. pm_flags |= PM_ACPI;
  887. else {
  888. printk(KERN_INFO PREFIX
  889. "APM is already active, exiting\n");
  890. disable_acpi();
  891. result = -ENODEV;
  892. }
  893. } else
  894. disable_acpi();
  895. if (acpi_disabled)
  896. return result;
  897. /*
  898. * If the laptop falls into the DMI check table, the power state check
  899. * will be disabled in the course of device power transition.
  900. */
  901. dmi_check_system(power_nocheck_dmi_table);
  902. acpi_scan_init();
  903. acpi_ec_init();
  904. acpi_debugfs_init();
  905. acpi_sleep_proc_init();
  906. acpi_wakeup_device_init();
  907. return result;
  908. }
  909. subsys_initcall(acpi_init);