bus.c 25 KB

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