scan.c 34 KB

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