bus.c 26 KB

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