scan.c 35 KB

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