bus.c 26 KB

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