scan.c 40 KB

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