scan.c 37 KB

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