scan.c 37 KB

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