bus.c 26 KB

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