scan.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * scan.c - support for transforming the ACPI namespace into individual objects
  3. */
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/acpi.h>
  8. #include <acpi/acpi_drivers.h>
  9. #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
  10. #define _COMPONENT ACPI_BUS_COMPONENT
  11. ACPI_MODULE_NAME("scan")
  12. #define STRUCT_TO_INT(s) (*((int*)&s))
  13. extern struct acpi_device *acpi_root;
  14. #define ACPI_BUS_CLASS "system_bus"
  15. #define ACPI_BUS_HID "ACPI_BUS"
  16. #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
  17. #define ACPI_BUS_DEVICE_NAME "System Bus"
  18. static LIST_HEAD(acpi_device_list);
  19. DEFINE_SPINLOCK(acpi_device_lock);
  20. LIST_HEAD(acpi_wakeup_device_list);
  21. static int acpi_eject_operation(acpi_handle handle, int lockable)
  22. {
  23. struct acpi_object_list arg_list;
  24. union acpi_object arg;
  25. acpi_status status = AE_OK;
  26. /*
  27. * TBD: evaluate _PS3?
  28. */
  29. if (lockable) {
  30. arg_list.count = 1;
  31. arg_list.pointer = &arg;
  32. arg.type = ACPI_TYPE_INTEGER;
  33. arg.integer.value = 0;
  34. acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
  35. }
  36. arg_list.count = 1;
  37. arg_list.pointer = &arg;
  38. arg.type = ACPI_TYPE_INTEGER;
  39. arg.integer.value = 1;
  40. /*
  41. * TBD: _EJD support.
  42. */
  43. status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
  44. if (ACPI_FAILURE(status)) {
  45. return (-ENODEV);
  46. }
  47. return (0);
  48. }
  49. static ssize_t
  50. acpi_eject_store(struct device *d, struct device_attribute *attr,
  51. const char *buf, size_t count)
  52. {
  53. int result;
  54. int ret = count;
  55. int islockable;
  56. acpi_status status;
  57. acpi_handle handle;
  58. acpi_object_type type = 0;
  59. struct acpi_device *acpi_device = to_acpi_device(d);
  60. if ((!count) || (buf[0] != '1')) {
  61. return -EINVAL;
  62. }
  63. #ifndef FORCE_EJECT
  64. if (acpi_device->driver == NULL) {
  65. ret = -ENODEV;
  66. goto err;
  67. }
  68. #endif
  69. status = acpi_get_type(acpi_device->handle, &type);
  70. if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
  71. ret = -ENODEV;
  72. goto err;
  73. }
  74. islockable = acpi_device->flags.lockable;
  75. handle = acpi_device->handle;
  76. result = acpi_bus_trim(acpi_device, 1);
  77. if (!result)
  78. result = acpi_eject_operation(handle, islockable);
  79. if (result) {
  80. ret = -EBUSY;
  81. }
  82. err:
  83. return ret;
  84. }
  85. static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
  86. static void acpi_device_setup_files(struct acpi_device *dev)
  87. {
  88. acpi_status status;
  89. acpi_handle temp;
  90. /*
  91. * If device has _EJ0, 'eject' file is created that is used to trigger
  92. * hot-removal function from userland.
  93. */
  94. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  95. if (ACPI_SUCCESS(status))
  96. device_create_file(&dev->dev, &dev_attr_eject);
  97. }
  98. static void acpi_device_remove_files(struct acpi_device *dev)
  99. {
  100. acpi_status status;
  101. acpi_handle temp;
  102. /*
  103. * If device has _EJ0, 'eject' file is created that is used to trigger
  104. * hot-removal function from userland.
  105. */
  106. status = acpi_get_handle(dev->handle, "_EJ0", &temp);
  107. if (ACPI_SUCCESS(status))
  108. device_remove_file(&dev->dev, &dev_attr_eject);
  109. }
  110. /* --------------------------------------------------------------------------
  111. ACPI Bus operations
  112. -------------------------------------------------------------------------- */
  113. static void acpi_device_release(struct device *dev)
  114. {
  115. struct acpi_device *acpi_dev = to_acpi_device(dev);
  116. kfree(acpi_dev->pnp.cid_list);
  117. kfree(acpi_dev);
  118. }
  119. static int acpi_device_suspend(struct device *dev, pm_message_t state)
  120. {
  121. struct acpi_device *acpi_dev = to_acpi_device(dev);
  122. struct acpi_driver *acpi_drv = acpi_dev->driver;
  123. if (acpi_drv && acpi_drv->ops.suspend)
  124. return acpi_drv->ops.suspend(acpi_dev, state);
  125. return 0;
  126. }
  127. static int acpi_device_resume(struct device *dev)
  128. {
  129. struct acpi_device *acpi_dev = to_acpi_device(dev);
  130. struct acpi_driver *acpi_drv = acpi_dev->driver;
  131. if (acpi_drv && acpi_drv->ops.resume)
  132. return acpi_drv->ops.resume(acpi_dev);
  133. return 0;
  134. }
  135. static int acpi_bus_match(struct device *dev, struct device_driver *drv)
  136. {
  137. struct acpi_device *acpi_dev = to_acpi_device(dev);
  138. struct acpi_driver *acpi_drv = to_acpi_driver(drv);
  139. if (acpi_drv->ops.match)
  140. return !acpi_drv->ops.match(acpi_dev, acpi_drv);
  141. return !acpi_match_ids(acpi_dev, acpi_drv->ids);
  142. }
  143. static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
  144. char *buffer, int buffer_size)
  145. {
  146. struct acpi_device *acpi_dev = to_acpi_device(dev);
  147. int i = 0, length = 0, ret = 0;
  148. if (acpi_dev->flags.hardware_id)
  149. ret = add_uevent_var(envp, num_envp, &i,
  150. buffer, buffer_size, &length,
  151. "HWID=%s", acpi_dev->pnp.hardware_id);
  152. if (ret)
  153. return -ENOMEM;
  154. if (acpi_dev->flags.compatible_ids) {
  155. int j;
  156. struct acpi_compatible_id_list *cid_list;
  157. cid_list = acpi_dev->pnp.cid_list;
  158. for (j = 0; j < cid_list->count; j++) {
  159. ret = add_uevent_var(envp, num_envp, &i, buffer,
  160. buffer_size, &length, "COMPTID=%s",
  161. cid_list->id[j].value);
  162. if (ret)
  163. return -ENOMEM;
  164. }
  165. }
  166. envp[i] = NULL;
  167. return 0;
  168. }
  169. static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
  170. static int acpi_start_single_object(struct acpi_device *);
  171. static int acpi_device_probe(struct device * dev)
  172. {
  173. struct acpi_device *acpi_dev = to_acpi_device(dev);
  174. struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
  175. int ret;
  176. ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
  177. if (!ret) {
  178. if (acpi_dev->bus_ops.acpi_op_start)
  179. acpi_start_single_object(acpi_dev);
  180. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  181. "Found driver [%s] for device [%s]\n",
  182. acpi_drv->name, acpi_dev->pnp.bus_id));
  183. get_device(dev);
  184. }
  185. return ret;
  186. }
  187. static int acpi_device_remove(struct device * dev)
  188. {
  189. struct acpi_device *acpi_dev = to_acpi_device(dev);
  190. struct acpi_driver *acpi_drv = acpi_dev->driver;
  191. if (acpi_drv) {
  192. if (acpi_drv->ops.stop)
  193. acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
  194. if (acpi_drv->ops.remove)
  195. acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
  196. }
  197. acpi_dev->driver = NULL;
  198. acpi_driver_data(dev) = NULL;
  199. put_device(dev);
  200. return 0;
  201. }
  202. static void acpi_device_shutdown(struct device *dev)
  203. {
  204. struct acpi_device *acpi_dev = to_acpi_device(dev);
  205. struct acpi_driver *acpi_drv = acpi_dev->driver;
  206. if (acpi_drv && acpi_drv->ops.shutdown)
  207. acpi_drv->ops.shutdown(acpi_dev);
  208. return ;
  209. }
  210. static struct bus_type acpi_bus_type = {
  211. .name = "acpi",
  212. .suspend = acpi_device_suspend,
  213. .resume = acpi_device_resume,
  214. .shutdown = acpi_device_shutdown,
  215. .match = acpi_bus_match,
  216. .probe = acpi_device_probe,
  217. .remove = acpi_device_remove,
  218. .uevent = acpi_device_uevent,
  219. };
  220. static void acpi_device_register(struct acpi_device *device,
  221. struct acpi_device *parent)
  222. {
  223. /*
  224. * Linkage
  225. * -------
  226. * Link this device to its parent and siblings.
  227. */
  228. INIT_LIST_HEAD(&device->children);
  229. INIT_LIST_HEAD(&device->node);
  230. INIT_LIST_HEAD(&device->g_list);
  231. INIT_LIST_HEAD(&device->wakeup_list);
  232. spin_lock(&acpi_device_lock);
  233. if (device->parent) {
  234. list_add_tail(&device->node, &device->parent->children);
  235. list_add_tail(&device->g_list, &device->parent->g_list);
  236. } else
  237. list_add_tail(&device->g_list, &acpi_device_list);
  238. if (device->wakeup.flags.valid)
  239. list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
  240. spin_unlock(&acpi_device_lock);
  241. if (device->parent)
  242. device->dev.parent = &parent->dev;
  243. device->dev.bus = &acpi_bus_type;
  244. device_initialize(&device->dev);
  245. sprintf(device->dev.bus_id, "%s", device->pnp.bus_id);
  246. device->dev.release = &acpi_device_release;
  247. device_add(&device->dev);
  248. acpi_device_setup_files(device);
  249. device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
  250. }
  251. static void acpi_device_unregister(struct acpi_device *device, int type)
  252. {
  253. spin_lock(&acpi_device_lock);
  254. if (device->parent) {
  255. list_del(&device->node);
  256. list_del(&device->g_list);
  257. } else
  258. list_del(&device->g_list);
  259. list_del(&device->wakeup_list);
  260. spin_unlock(&acpi_device_lock);
  261. acpi_detach_data(device->handle, acpi_bus_data_handler);
  262. acpi_device_remove_files(device);
  263. device_unregister(&device->dev);
  264. }
  265. /* --------------------------------------------------------------------------
  266. Driver Management
  267. -------------------------------------------------------------------------- */
  268. /**
  269. * acpi_bus_driver_init - add a device to a driver
  270. * @device: the device to add and initialize
  271. * @driver: driver for the device
  272. *
  273. * Used to initialize a device via its device driver. Called whenever a
  274. * driver is bound to a device. Invokes the driver's add() ops.
  275. */
  276. static int
  277. acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
  278. {
  279. int result = 0;
  280. if (!device || !driver)
  281. return -EINVAL;
  282. if (!driver->ops.add)
  283. return -ENOSYS;
  284. result = driver->ops.add(device);
  285. if (result) {
  286. device->driver = NULL;
  287. acpi_driver_data(device) = NULL;
  288. return result;
  289. }
  290. device->driver = driver;
  291. /*
  292. * TBD - Configuration Management: Assign resources to device based
  293. * upon possible configuration and currently allocated resources.
  294. */
  295. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  296. "Driver successfully bound to device\n"));
  297. return 0;
  298. }
  299. static int acpi_start_single_object(struct acpi_device *device)
  300. {
  301. int result = 0;
  302. struct acpi_driver *driver;
  303. if (!(driver = device->driver))
  304. return 0;
  305. if (driver->ops.start) {
  306. result = driver->ops.start(device);
  307. if (result && driver->ops.remove)
  308. driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
  309. }
  310. return result;
  311. }
  312. /**
  313. * acpi_bus_register_driver - register a driver with the ACPI bus
  314. * @driver: driver being registered
  315. *
  316. * Registers a driver with the ACPI bus. Searches the namespace for all
  317. * devices that match the driver's criteria and binds. Returns zero for
  318. * success or a negative error status for failure.
  319. */
  320. int acpi_bus_register_driver(struct acpi_driver *driver)
  321. {
  322. int ret;
  323. if (acpi_disabled)
  324. return -ENODEV;
  325. driver->drv.name = driver->name;
  326. driver->drv.bus = &acpi_bus_type;
  327. driver->drv.owner = driver->owner;
  328. ret = driver_register(&driver->drv);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(acpi_bus_register_driver);
  332. /**
  333. * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
  334. * @driver: driver to unregister
  335. *
  336. * Unregisters a driver with the ACPI bus. Searches the namespace for all
  337. * devices that match the driver's criteria and unbinds.
  338. */
  339. void acpi_bus_unregister_driver(struct acpi_driver *driver)
  340. {
  341. driver_unregister(&driver->drv);
  342. }
  343. EXPORT_SYMBOL(acpi_bus_unregister_driver);
  344. /* --------------------------------------------------------------------------
  345. Device Enumeration
  346. -------------------------------------------------------------------------- */
  347. acpi_status
  348. acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
  349. {
  350. acpi_status status;
  351. acpi_handle tmp;
  352. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  353. union acpi_object *obj;
  354. status = acpi_get_handle(handle, "_EJD", &tmp);
  355. if (ACPI_FAILURE(status))
  356. return status;
  357. status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
  358. if (ACPI_SUCCESS(status)) {
  359. obj = buffer.pointer;
  360. status = acpi_get_handle(NULL, obj->string.pointer, ejd);
  361. kfree(buffer.pointer);
  362. }
  363. return status;
  364. }
  365. EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
  366. void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
  367. {
  368. /* TBD */
  369. return;
  370. }
  371. int acpi_match_ids(struct acpi_device *device, char *ids)
  372. {
  373. if (device->flags.hardware_id)
  374. if (strstr(ids, device->pnp.hardware_id))
  375. return 0;
  376. if (device->flags.compatible_ids) {
  377. struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
  378. int i;
  379. /* compare multiple _CID entries against driver ids */
  380. for (i = 0; i < cid_list->count; i++) {
  381. if (strstr(ids, cid_list->id[i].value))
  382. return 0;
  383. }
  384. }
  385. return -ENOENT;
  386. }
  387. static int acpi_bus_get_perf_flags(struct acpi_device *device)
  388. {
  389. device->performance.state = ACPI_STATE_UNKNOWN;
  390. return 0;
  391. }
  392. static acpi_status
  393. acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
  394. union acpi_object *package)
  395. {
  396. int i = 0;
  397. union acpi_object *element = NULL;
  398. if (!device || !package || (package->package.count < 2))
  399. return AE_BAD_PARAMETER;
  400. element = &(package->package.elements[0]);
  401. if (!element)
  402. return AE_BAD_PARAMETER;
  403. if (element->type == ACPI_TYPE_PACKAGE) {
  404. if ((element->package.count < 2) ||
  405. (element->package.elements[0].type !=
  406. ACPI_TYPE_LOCAL_REFERENCE)
  407. || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
  408. return AE_BAD_DATA;
  409. device->wakeup.gpe_device =
  410. element->package.elements[0].reference.handle;
  411. device->wakeup.gpe_number =
  412. (u32) element->package.elements[1].integer.value;
  413. } else if (element->type == ACPI_TYPE_INTEGER) {
  414. device->wakeup.gpe_number = element->integer.value;
  415. } else
  416. return AE_BAD_DATA;
  417. element = &(package->package.elements[1]);
  418. if (element->type != ACPI_TYPE_INTEGER) {
  419. return AE_BAD_DATA;
  420. }
  421. device->wakeup.sleep_state = element->integer.value;
  422. if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
  423. return AE_NO_MEMORY;
  424. }
  425. device->wakeup.resources.count = package->package.count - 2;
  426. for (i = 0; i < device->wakeup.resources.count; i++) {
  427. element = &(package->package.elements[i + 2]);
  428. if (element->type != ACPI_TYPE_ANY) {
  429. return AE_BAD_DATA;
  430. }
  431. device->wakeup.resources.handles[i] = element->reference.handle;
  432. }
  433. return AE_OK;
  434. }
  435. static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
  436. {
  437. acpi_status status = 0;
  438. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  439. union acpi_object *package = NULL;
  440. /* _PRW */
  441. status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
  442. if (ACPI_FAILURE(status)) {
  443. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
  444. goto end;
  445. }
  446. package = (union acpi_object *)buffer.pointer;
  447. status = acpi_bus_extract_wakeup_device_power_package(device, package);
  448. if (ACPI_FAILURE(status)) {
  449. ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
  450. goto end;
  451. }
  452. kfree(buffer.pointer);
  453. device->wakeup.flags.valid = 1;
  454. /* Power button, Lid switch always enable wakeup */
  455. if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
  456. device->wakeup.flags.run_wake = 1;
  457. end:
  458. if (ACPI_FAILURE(status))
  459. device->flags.wake_capable = 0;
  460. return 0;
  461. }
  462. static int acpi_bus_get_power_flags(struct acpi_device *device)
  463. {
  464. acpi_status status = 0;
  465. acpi_handle handle = NULL;
  466. u32 i = 0;
  467. /*
  468. * Power Management Flags
  469. */
  470. status = acpi_get_handle(device->handle, "_PSC", &handle);
  471. if (ACPI_SUCCESS(status))
  472. device->power.flags.explicit_get = 1;
  473. status = acpi_get_handle(device->handle, "_IRC", &handle);
  474. if (ACPI_SUCCESS(status))
  475. device->power.flags.inrush_current = 1;
  476. /*
  477. * Enumerate supported power management states
  478. */
  479. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
  480. struct acpi_device_power_state *ps = &device->power.states[i];
  481. char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
  482. /* Evaluate "_PRx" to se if power resources are referenced */
  483. acpi_evaluate_reference(device->handle, object_name, NULL,
  484. &ps->resources);
  485. if (ps->resources.count) {
  486. device->power.flags.power_resources = 1;
  487. ps->flags.valid = 1;
  488. }
  489. /* Evaluate "_PSx" to see if we can do explicit sets */
  490. object_name[2] = 'S';
  491. status = acpi_get_handle(device->handle, object_name, &handle);
  492. if (ACPI_SUCCESS(status)) {
  493. ps->flags.explicit_set = 1;
  494. ps->flags.valid = 1;
  495. }
  496. /* State is valid if we have some power control */
  497. if (ps->resources.count || ps->flags.explicit_set)
  498. ps->flags.valid = 1;
  499. ps->power = -1; /* Unknown - driver assigned */
  500. ps->latency = -1; /* Unknown - driver assigned */
  501. }
  502. /* Set defaults for D0 and D3 states (always valid) */
  503. device->power.states[ACPI_STATE_D0].flags.valid = 1;
  504. device->power.states[ACPI_STATE_D0].power = 100;
  505. device->power.states[ACPI_STATE_D3].flags.valid = 1;
  506. device->power.states[ACPI_STATE_D3].power = 0;
  507. /* TBD: System wake support and resource requirements. */
  508. device->power.state = ACPI_STATE_UNKNOWN;
  509. return 0;
  510. }
  511. static int acpi_bus_get_flags(struct acpi_device *device)
  512. {
  513. acpi_status status = AE_OK;
  514. acpi_handle temp = NULL;
  515. /* Presence of _STA indicates 'dynamic_status' */
  516. status = acpi_get_handle(device->handle, "_STA", &temp);
  517. if (ACPI_SUCCESS(status))
  518. device->flags.dynamic_status = 1;
  519. /* Presence of _CID indicates 'compatible_ids' */
  520. status = acpi_get_handle(device->handle, "_CID", &temp);
  521. if (ACPI_SUCCESS(status))
  522. device->flags.compatible_ids = 1;
  523. /* Presence of _RMV indicates 'removable' */
  524. status = acpi_get_handle(device->handle, "_RMV", &temp);
  525. if (ACPI_SUCCESS(status))
  526. device->flags.removable = 1;
  527. /* Presence of _EJD|_EJ0 indicates 'ejectable' */
  528. status = acpi_get_handle(device->handle, "_EJD", &temp);
  529. if (ACPI_SUCCESS(status))
  530. device->flags.ejectable = 1;
  531. else {
  532. status = acpi_get_handle(device->handle, "_EJ0", &temp);
  533. if (ACPI_SUCCESS(status))
  534. device->flags.ejectable = 1;
  535. }
  536. /* Presence of _LCK indicates 'lockable' */
  537. status = acpi_get_handle(device->handle, "_LCK", &temp);
  538. if (ACPI_SUCCESS(status))
  539. device->flags.lockable = 1;
  540. /* Presence of _PS0|_PR0 indicates 'power manageable' */
  541. status = acpi_get_handle(device->handle, "_PS0", &temp);
  542. if (ACPI_FAILURE(status))
  543. status = acpi_get_handle(device->handle, "_PR0", &temp);
  544. if (ACPI_SUCCESS(status))
  545. device->flags.power_manageable = 1;
  546. /* Presence of _PRW indicates wake capable */
  547. status = acpi_get_handle(device->handle, "_PRW", &temp);
  548. if (ACPI_SUCCESS(status))
  549. device->flags.wake_capable = 1;
  550. /* TBD: Peformance management */
  551. return 0;
  552. }
  553. static void acpi_device_get_busid(struct acpi_device *device,
  554. acpi_handle handle, int type)
  555. {
  556. char bus_id[5] = { '?', 0 };
  557. struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
  558. int i = 0;
  559. /*
  560. * Bus ID
  561. * ------
  562. * The device's Bus ID is simply the object name.
  563. * TBD: Shouldn't this value be unique (within the ACPI namespace)?
  564. */
  565. switch (type) {
  566. case ACPI_BUS_TYPE_SYSTEM:
  567. strcpy(device->pnp.bus_id, "ACPI");
  568. break;
  569. case ACPI_BUS_TYPE_POWER_BUTTON:
  570. strcpy(device->pnp.bus_id, "PWRF");
  571. break;
  572. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  573. strcpy(device->pnp.bus_id, "SLPF");
  574. break;
  575. default:
  576. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  577. /* Clean up trailing underscores (if any) */
  578. for (i = 3; i > 1; i--) {
  579. if (bus_id[i] == '_')
  580. bus_id[i] = '\0';
  581. else
  582. break;
  583. }
  584. strcpy(device->pnp.bus_id, bus_id);
  585. break;
  586. }
  587. }
  588. static void acpi_device_set_id(struct acpi_device *device,
  589. struct acpi_device *parent, acpi_handle handle,
  590. int type)
  591. {
  592. struct acpi_device_info *info;
  593. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  594. char *hid = NULL;
  595. char *uid = NULL;
  596. struct acpi_compatible_id_list *cid_list = NULL;
  597. acpi_status status;
  598. switch (type) {
  599. case ACPI_BUS_TYPE_DEVICE:
  600. status = acpi_get_object_info(handle, &buffer);
  601. if (ACPI_FAILURE(status)) {
  602. printk("%s: Error reading device info\n", __FUNCTION__);
  603. return;
  604. }
  605. info = buffer.pointer;
  606. if (info->valid & ACPI_VALID_HID)
  607. hid = info->hardware_id.value;
  608. if (info->valid & ACPI_VALID_UID)
  609. uid = info->unique_id.value;
  610. if (info->valid & ACPI_VALID_CID)
  611. cid_list = &info->compatibility_id;
  612. if (info->valid & ACPI_VALID_ADR) {
  613. device->pnp.bus_address = info->address;
  614. device->flags.bus_address = 1;
  615. }
  616. break;
  617. case ACPI_BUS_TYPE_POWER:
  618. hid = ACPI_POWER_HID;
  619. break;
  620. case ACPI_BUS_TYPE_PROCESSOR:
  621. hid = ACPI_PROCESSOR_HID;
  622. break;
  623. case ACPI_BUS_TYPE_SYSTEM:
  624. hid = ACPI_SYSTEM_HID;
  625. break;
  626. case ACPI_BUS_TYPE_THERMAL:
  627. hid = ACPI_THERMAL_HID;
  628. break;
  629. case ACPI_BUS_TYPE_POWER_BUTTON:
  630. hid = ACPI_BUTTON_HID_POWERF;
  631. break;
  632. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  633. hid = ACPI_BUTTON_HID_SLEEPF;
  634. break;
  635. }
  636. /*
  637. * \_SB
  638. * ----
  639. * Fix for the system root bus device -- the only root-level device.
  640. */
  641. if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
  642. hid = ACPI_BUS_HID;
  643. strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
  644. strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
  645. }
  646. if (hid) {
  647. strcpy(device->pnp.hardware_id, hid);
  648. device->flags.hardware_id = 1;
  649. }
  650. if (uid) {
  651. strcpy(device->pnp.unique_id, uid);
  652. device->flags.unique_id = 1;
  653. }
  654. if (cid_list) {
  655. device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
  656. if (device->pnp.cid_list)
  657. memcpy(device->pnp.cid_list, cid_list, cid_list->size);
  658. else
  659. printk(KERN_ERR "Memory allocation error\n");
  660. }
  661. kfree(buffer.pointer);
  662. }
  663. static int acpi_device_set_context(struct acpi_device *device, int type)
  664. {
  665. acpi_status status = AE_OK;
  666. int result = 0;
  667. /*
  668. * Context
  669. * -------
  670. * Attach this 'struct acpi_device' to the ACPI object. This makes
  671. * resolutions from handle->device very efficient. Note that we need
  672. * to be careful with fixed-feature devices as they all attach to the
  673. * root object.
  674. */
  675. if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
  676. type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
  677. status = acpi_attach_data(device->handle,
  678. acpi_bus_data_handler, device);
  679. if (ACPI_FAILURE(status)) {
  680. printk("Error attaching device data\n");
  681. result = -ENODEV;
  682. }
  683. }
  684. return result;
  685. }
  686. static void acpi_device_get_debug_info(struct acpi_device *device,
  687. acpi_handle handle, int type)
  688. {
  689. #ifdef CONFIG_ACPI_DEBUG_OUTPUT
  690. char *type_string = NULL;
  691. char name[80] = { '?', '\0' };
  692. struct acpi_buffer buffer = { sizeof(name), name };
  693. switch (type) {
  694. case ACPI_BUS_TYPE_DEVICE:
  695. type_string = "Device";
  696. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  697. break;
  698. case ACPI_BUS_TYPE_POWER:
  699. type_string = "Power Resource";
  700. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  701. break;
  702. case ACPI_BUS_TYPE_PROCESSOR:
  703. type_string = "Processor";
  704. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  705. break;
  706. case ACPI_BUS_TYPE_SYSTEM:
  707. type_string = "System";
  708. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  709. break;
  710. case ACPI_BUS_TYPE_THERMAL:
  711. type_string = "Thermal Zone";
  712. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  713. break;
  714. case ACPI_BUS_TYPE_POWER_BUTTON:
  715. type_string = "Power Button";
  716. sprintf(name, "PWRB");
  717. break;
  718. case ACPI_BUS_TYPE_SLEEP_BUTTON:
  719. type_string = "Sleep Button";
  720. sprintf(name, "SLPB");
  721. break;
  722. }
  723. printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
  724. #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
  725. }
  726. static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
  727. {
  728. if (!dev)
  729. return -EINVAL;
  730. dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
  731. device_release_driver(&dev->dev);
  732. if (!rmdevice)
  733. return 0;
  734. if (dev->flags.bus_address) {
  735. if ((dev->parent) && (dev->parent->ops.unbind))
  736. dev->parent->ops.unbind(dev);
  737. }
  738. acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
  739. return 0;
  740. }
  741. static int
  742. acpi_add_single_object(struct acpi_device **child,
  743. struct acpi_device *parent, acpi_handle handle, int type,
  744. struct acpi_bus_ops *ops)
  745. {
  746. int result = 0;
  747. struct acpi_device *device = NULL;
  748. if (!child)
  749. return -EINVAL;
  750. device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
  751. if (!device) {
  752. printk(KERN_ERR PREFIX "Memory allocation error\n");
  753. return -ENOMEM;
  754. }
  755. memset(device, 0, sizeof(struct acpi_device));
  756. device->handle = handle;
  757. device->parent = parent;
  758. device->bus_ops = *ops; /* workround for not call .start */
  759. acpi_device_get_busid(device, handle, type);
  760. /*
  761. * Flags
  762. * -----
  763. * Get prior to calling acpi_bus_get_status() so we know whether
  764. * or not _STA is present. Note that we only look for object
  765. * handles -- cannot evaluate objects until we know the device is
  766. * present and properly initialized.
  767. */
  768. result = acpi_bus_get_flags(device);
  769. if (result)
  770. goto end;
  771. /*
  772. * Status
  773. * ------
  774. * See if the device is present. We always assume that non-Device
  775. * and non-Processor objects (e.g. thermal zones, power resources,
  776. * etc.) are present, functioning, etc. (at least when parent object
  777. * is present). Note that _STA has a different meaning for some
  778. * objects (e.g. power resources) so we need to be careful how we use
  779. * it.
  780. */
  781. switch (type) {
  782. case ACPI_BUS_TYPE_PROCESSOR:
  783. case ACPI_BUS_TYPE_DEVICE:
  784. result = acpi_bus_get_status(device);
  785. if (ACPI_FAILURE(result) || !device->status.present) {
  786. result = -ENOENT;
  787. goto end;
  788. }
  789. break;
  790. default:
  791. STRUCT_TO_INT(device->status) = 0x0F;
  792. break;
  793. }
  794. /*
  795. * Initialize Device
  796. * -----------------
  797. * TBD: Synch with Core's enumeration/initialization process.
  798. */
  799. /*
  800. * Hardware ID, Unique ID, & Bus Address
  801. * -------------------------------------
  802. */
  803. acpi_device_set_id(device, parent, handle, type);
  804. /*
  805. * Power Management
  806. * ----------------
  807. */
  808. if (device->flags.power_manageable) {
  809. result = acpi_bus_get_power_flags(device);
  810. if (result)
  811. goto end;
  812. }
  813. /*
  814. * Wakeup device management
  815. *-----------------------
  816. */
  817. if (device->flags.wake_capable) {
  818. result = acpi_bus_get_wakeup_device_flags(device);
  819. if (result)
  820. goto end;
  821. }
  822. /*
  823. * Performance Management
  824. * ----------------------
  825. */
  826. if (device->flags.performance_manageable) {
  827. result = acpi_bus_get_perf_flags(device);
  828. if (result)
  829. goto end;
  830. }
  831. if ((result = acpi_device_set_context(device, type)))
  832. goto end;
  833. acpi_device_get_debug_info(device, handle, type);
  834. acpi_device_register(device, parent);
  835. /*
  836. * Bind _ADR-Based Devices
  837. * -----------------------
  838. * If there's a a bus address (_ADR) then we utilize the parent's
  839. * 'bind' function (if exists) to bind the ACPI- and natively-
  840. * enumerated device representations.
  841. */
  842. if (device->flags.bus_address) {
  843. if (device->parent && device->parent->ops.bind)
  844. device->parent->ops.bind(device);
  845. }
  846. end:
  847. if (!result)
  848. *child = device;
  849. else {
  850. kfree(device->pnp.cid_list);
  851. kfree(device);
  852. }
  853. return result;
  854. }
  855. static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
  856. {
  857. acpi_status status = AE_OK;
  858. struct acpi_device *parent = NULL;
  859. struct acpi_device *child = NULL;
  860. acpi_handle phandle = NULL;
  861. acpi_handle chandle = NULL;
  862. acpi_object_type type = 0;
  863. u32 level = 1;
  864. if (!start)
  865. return -EINVAL;
  866. parent = start;
  867. phandle = start->handle;
  868. /*
  869. * Parse through the ACPI namespace, identify all 'devices', and
  870. * create a new 'struct acpi_device' for each.
  871. */
  872. while ((level > 0) && parent) {
  873. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  874. chandle, &chandle);
  875. /*
  876. * If this scope is exhausted then move our way back up.
  877. */
  878. if (ACPI_FAILURE(status)) {
  879. level--;
  880. chandle = phandle;
  881. acpi_get_parent(phandle, &phandle);
  882. if (parent->parent)
  883. parent = parent->parent;
  884. continue;
  885. }
  886. status = acpi_get_type(chandle, &type);
  887. if (ACPI_FAILURE(status))
  888. continue;
  889. /*
  890. * If this is a scope object then parse it (depth-first).
  891. */
  892. if (type == ACPI_TYPE_LOCAL_SCOPE) {
  893. level++;
  894. phandle = chandle;
  895. chandle = NULL;
  896. continue;
  897. }
  898. /*
  899. * We're only interested in objects that we consider 'devices'.
  900. */
  901. switch (type) {
  902. case ACPI_TYPE_DEVICE:
  903. type = ACPI_BUS_TYPE_DEVICE;
  904. break;
  905. case ACPI_TYPE_PROCESSOR:
  906. type = ACPI_BUS_TYPE_PROCESSOR;
  907. break;
  908. case ACPI_TYPE_THERMAL:
  909. type = ACPI_BUS_TYPE_THERMAL;
  910. break;
  911. case ACPI_TYPE_POWER:
  912. type = ACPI_BUS_TYPE_POWER;
  913. break;
  914. default:
  915. continue;
  916. }
  917. if (ops->acpi_op_add)
  918. status = acpi_add_single_object(&child, parent,
  919. chandle, type, ops);
  920. else
  921. status = acpi_bus_get_device(chandle, &child);
  922. if (ACPI_FAILURE(status))
  923. continue;
  924. if (ops->acpi_op_start && !(ops->acpi_op_add)) {
  925. status = acpi_start_single_object(child);
  926. if (ACPI_FAILURE(status))
  927. continue;
  928. }
  929. /*
  930. * If the device is present, enabled, and functioning then
  931. * parse its scope (depth-first). Note that we need to
  932. * represent absent devices to facilitate PnP notifications
  933. * -- but only the subtree head (not all of its children,
  934. * which will be enumerated when the parent is inserted).
  935. *
  936. * TBD: Need notifications and other detection mechanisms
  937. * in place before we can fully implement this.
  938. */
  939. if (child->status.present) {
  940. status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
  941. NULL, NULL);
  942. if (ACPI_SUCCESS(status)) {
  943. level++;
  944. phandle = chandle;
  945. chandle = NULL;
  946. parent = child;
  947. }
  948. }
  949. }
  950. return 0;
  951. }
  952. int
  953. acpi_bus_add(struct acpi_device **child,
  954. struct acpi_device *parent, acpi_handle handle, int type)
  955. {
  956. int result;
  957. struct acpi_bus_ops ops;
  958. memset(&ops, 0, sizeof(ops));
  959. ops.acpi_op_add = 1;
  960. result = acpi_add_single_object(child, parent, handle, type, &ops);
  961. if (!result)
  962. result = acpi_bus_scan(*child, &ops);
  963. return result;
  964. }
  965. EXPORT_SYMBOL(acpi_bus_add);
  966. int acpi_bus_start(struct acpi_device *device)
  967. {
  968. int result;
  969. struct acpi_bus_ops ops;
  970. if (!device)
  971. return -EINVAL;
  972. result = acpi_start_single_object(device);
  973. if (!result) {
  974. memset(&ops, 0, sizeof(ops));
  975. ops.acpi_op_start = 1;
  976. result = acpi_bus_scan(device, &ops);
  977. }
  978. return result;
  979. }
  980. EXPORT_SYMBOL(acpi_bus_start);
  981. int acpi_bus_trim(struct acpi_device *start, int rmdevice)
  982. {
  983. acpi_status status;
  984. struct acpi_device *parent, *child;
  985. acpi_handle phandle, chandle;
  986. acpi_object_type type;
  987. u32 level = 1;
  988. int err = 0;
  989. parent = start;
  990. phandle = start->handle;
  991. child = chandle = NULL;
  992. while ((level > 0) && parent && (!err)) {
  993. status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
  994. chandle, &chandle);
  995. /*
  996. * If this scope is exhausted then move our way back up.
  997. */
  998. if (ACPI_FAILURE(status)) {
  999. level--;
  1000. chandle = phandle;
  1001. acpi_get_parent(phandle, &phandle);
  1002. child = parent;
  1003. parent = parent->parent;
  1004. if (level == 0)
  1005. err = acpi_bus_remove(child, rmdevice);
  1006. else
  1007. err = acpi_bus_remove(child, 1);
  1008. continue;
  1009. }
  1010. status = acpi_get_type(chandle, &type);
  1011. if (ACPI_FAILURE(status)) {
  1012. continue;
  1013. }
  1014. /*
  1015. * If there is a device corresponding to chandle then
  1016. * parse it (depth-first).
  1017. */
  1018. if (acpi_bus_get_device(chandle, &child) == 0) {
  1019. level++;
  1020. phandle = chandle;
  1021. chandle = NULL;
  1022. parent = child;
  1023. }
  1024. continue;
  1025. }
  1026. return err;
  1027. }
  1028. EXPORT_SYMBOL_GPL(acpi_bus_trim);
  1029. static int acpi_bus_scan_fixed(struct acpi_device *root)
  1030. {
  1031. int result = 0;
  1032. struct acpi_device *device = NULL;
  1033. struct acpi_bus_ops ops;
  1034. if (!root)
  1035. return -ENODEV;
  1036. memset(&ops, 0, sizeof(ops));
  1037. ops.acpi_op_add = 1;
  1038. ops.acpi_op_start = 1;
  1039. /*
  1040. * Enumerate all fixed-feature devices.
  1041. */
  1042. if (acpi_fadt.pwr_button == 0) {
  1043. result = acpi_add_single_object(&device, acpi_root,
  1044. NULL,
  1045. ACPI_BUS_TYPE_POWER_BUTTON,
  1046. &ops);
  1047. }
  1048. if (acpi_fadt.sleep_button == 0) {
  1049. result = acpi_add_single_object(&device, acpi_root,
  1050. NULL,
  1051. ACPI_BUS_TYPE_SLEEP_BUTTON,
  1052. &ops);
  1053. }
  1054. return result;
  1055. }
  1056. static int __init acpi_scan_init(void)
  1057. {
  1058. int result;
  1059. struct acpi_bus_ops ops;
  1060. if (acpi_disabled)
  1061. return 0;
  1062. memset(&ops, 0, sizeof(ops));
  1063. ops.acpi_op_add = 1;
  1064. ops.acpi_op_start = 1;
  1065. result = bus_register(&acpi_bus_type);
  1066. if (result) {
  1067. /* We don't want to quit even if we failed to add suspend/resume */
  1068. printk(KERN_ERR PREFIX "Could not register bus type\n");
  1069. }
  1070. /*
  1071. * Create the root device in the bus's device tree
  1072. */
  1073. result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
  1074. ACPI_BUS_TYPE_SYSTEM, &ops);
  1075. if (result)
  1076. goto Done;
  1077. /*
  1078. * Enumerate devices in the ACPI namespace.
  1079. */
  1080. result = acpi_bus_scan_fixed(acpi_root);
  1081. if (!result)
  1082. result = acpi_bus_scan(acpi_root, &ops);
  1083. if (result)
  1084. acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
  1085. Done:
  1086. return result;
  1087. }
  1088. subsys_initcall(acpi_scan_init);