scan.c 38 KB

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